package com.zt.plat.module.erp.controller.admin.erp;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomDetailPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomDetailRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomDetailSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpBomDetailDO;
import com.zt.plat.module.erp.service.erp.ErpBomDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP物料清单(BOM)明细")
@RestController
@RequestMapping("/base/erp-bom-detail")
@Validated
public class ErpBomDetailController {


    @Resource
    private ErpBomDetailService erpBomDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP物料清单(BOM)明细")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:create')")
    public CommonResult<ErpBomDetailRespVO> createErpBomDetail(@Valid @RequestBody ErpBomDetailSaveReqVO createReqVO) {
        return success(erpBomDetailService.createErpBomDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP物料清单(BOM)明细")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:update')")
    public CommonResult<Boolean> updateErpBomDetail(@Valid @RequestBody ErpBomDetailSaveReqVO updateReqVO) {
        erpBomDetailService.updateErpBomDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP物料清单(BOM)明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:delete')")
    public CommonResult<Boolean> deleteErpBomDetail(@RequestParam("id") Long id) {
        erpBomDetailService.deleteErpBomDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP物料清单(BOM)明细")
                @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:delete')")
    public CommonResult<Boolean> deleteErpBomDetailList(@RequestBody BatchDeleteReqVO req) {
        erpBomDetailService.deleteErpBomDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP物料清单(BOM)明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:query')")
    public CommonResult<ErpBomDetailRespVO> getErpBomDetail(@RequestParam("id") Long id) {
        ErpBomDetailDO erpBomDetail = erpBomDetailService.getErpBomDetail(id);
        return success(BeanUtils.toBean(erpBomDetail, ErpBomDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP物料清单(BOM)明细分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:query')")
    public CommonResult<PageResult<ErpBomDetailRespVO>> getErpBomDetailPage(@Valid ErpBomDetailPageReqVO pageReqVO) {
        PageResult<ErpBomDetailDO> pageResult = erpBomDetailService.getErpBomDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpBomDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP物料清单(BOM)明细 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpBomDetailExcel(@Valid ErpBomDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpBomDetailDO> list = erpBomDetailService.getErpBomDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP物料清单(BOM)明细.xls", "数据", ErpBomDetailRespVO.class,
                        BeanUtils.toBean(list, ErpBomDetailRespVO.class));
    }

}