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.ErpBomPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpBomDO;
import com.zt.plat.module.erp.service.erp.ErpBomService;
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")
@Validated
public class ErpBomController {


    @Resource
    private ErpBomService erpBomService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP物料清单(BOM)")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom:create')")
    public CommonResult<ErpBomRespVO> createErpBom(@Valid @RequestBody ErpBomSaveReqVO createReqVO) {
        return success(erpBomService.createErpBom(createReqVO));
    }

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

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

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP物料清单(BOM)")
                @PreAuthorize("@ss.hasPermission('sply:erp-bom:delete')")
    public CommonResult<Boolean> deleteErpBomList(@RequestBody BatchDeleteReqVO req) {
        erpBomService.deleteErpBomListByIds(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:query')")
    public CommonResult<ErpBomRespVO> getErpBom(@RequestParam("id") Long id) {
        ErpBomDO erpBom = erpBomService.getErpBom(id);
        return success(BeanUtils.toBean(erpBom, ErpBomRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP物料清单(BOM)分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom:query')")
    public CommonResult<PageResult<ErpBomRespVO>> getErpBomPage(@Valid ErpBomPageReqVO pageReqVO) {
        PageResult<ErpBomDO> pageResult = erpBomService.getErpBomPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpBomRespVO.class));
    }

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

    @PostMapping("/getErpBomTask")
    @Operation(summary = "定时获得erp更新物料清单(BOM)")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom:sync')")
    public CommonResult<Boolean> getErpBomTask() {
      erpBomService.callErpRfcInterface();
      return CommonResult.success(true);
    }

    @PostMapping("/submitDataToErp")
    @Operation(summary = "推送")
    @PreAuthorize("@ss.hasPermission('sply:erp-bom:create')")
    public void submitDataToErp() {
        erpBomService.submitDataToErp();
    }


}