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

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
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.base.api.departmentmaterial.dto.DepartmentMaterialPageReqDTO;
import com.zt.plat.module.base.api.materialinfomation.dto.MaterialInfomationPageReqDTO;
import com.zt.plat.module.erp.controller.admin.erp.vo.*;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpMaterialDO;
import com.zt.plat.module.erp.service.erp.ErpMaterialService;
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物料信息")
@RestController
@RequestMapping("/base/erp-material")
@Validated
public class ErpMaterialController implements BusinessControllerMarker {


    @Resource
    private ErpMaterialService erpMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP物料数据")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:create','basic:material-config:create'})")
    public CommonResult<ErpMaterialRespVO> createErpMaterial(@Valid @RequestBody ErpMaterialSaveReqVO createReqVO) {
        return success(erpMaterialService.createErpMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP物料数据")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:update','basic:material-config:update'})")
    public CommonResult<Boolean> updateErpMaterial(@Valid @RequestBody ErpMaterialSaveReqVO updateReqVO) {
        erpMaterialService.updateErpMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP物料数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:delete','basic:material-config:delete'})")
    public CommonResult<Boolean> deleteErpMaterial(@RequestParam("id") Long id) {
        erpMaterialService.deleteErpMaterial(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP物料数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-material:delete')")
    public CommonResult<Boolean> deleteErpMaterialList(@RequestBody BatchDeleteReqVO req) {
        erpMaterialService.deleteErpMaterialListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP物料数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-material:query')")
    public CommonResult<ErpMaterialRespVO> getErpMaterial(@RequestParam("id") Long id) {
        ErpMaterialDO erpMaterial = erpMaterialService.getErpMaterial(id);
        return success(BeanUtils.toBean(erpMaterial, ErpMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP物料数据分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-material:query')")
    public CommonResult<PageResult<ErpMaterialRespVO>> getErpMaterialPage(@Valid ErpMaterialPageReqVO pageReqVO) {
        PageResult<ErpMaterialDO> pageResult = erpMaterialService.getErpMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP物料数据 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpMaterialExcel(@Valid ErpMaterialPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMaterialDO> list = erpMaterialService.getErpMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP物料数据.xls", "数据", ErpMaterialRespVO.class,
                BeanUtils.toBean(list, ErpMaterialRespVO.class));
    }

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

    @GetMapping("/pageAndOther")
    @Operation(summary = "获得ERP物料数据分页并关联拓展数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-material:query')")
    public CommonResult<PageResult<ErpMaterialRespVO>> getErpMaterialPageAndOther(@Valid ErpMaterialPageReqVO pageReqVO) {
        PageResult<ErpMaterialRespVO> pageResult = erpMaterialService.getErpMaterialPageAndOther(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpMaterialRespVO.class));
    }

    //创建物料拓展关系
    @PostMapping("/createErpMaterialCorr")
    @Operation(summary = "创建ERP物料关系")
    @PreAuthorize("@ss.hasAnyPermissions({'base:warehouse-factory:create','basic:warehouse-config:create'})")
    public CommonResult<List<ErpMaterialRespVO>> createErpMaterialCorr(@Valid @RequestBody ErpMaterialCorrSaveReqVO erpMaterialCorrSaveReqVO) {
        return success(erpMaterialService.createErpMaterialCorr(erpMaterialCorrSaveReqVO));
    }

    //删除物料关系
    @DeleteMapping("/deleteErpMaterialCorr")
    @Operation(summary = "删除ERP物料关系")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:delete','basic:material-config:delete'})")
    public CommonResult<Boolean> deleteErpMaterialCorr(@RequestBody DeleteErpMaterialCorrVO req) {
        erpMaterialService.deleteErpMaterialCorr(req);
        return success(true);
    }

    //通过物料id查询物料详情
    @GetMapping("/getErpMaterialById")
    @Operation(summary = "通过物料id查询物料详情")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:query','basic:material-config:query'})")
    public CommonResult<ErpMaterialRespVO> getErpMaterialById(@RequestParam("id") Long id) {
        ErpMaterialDO erpMaterial = erpMaterialService.getErpMaterialById(id);
        return success(BeanUtils.toBean(erpMaterial, ErpMaterialRespVO.class));
    }

    //通过主物料查询子物料信息
    @GetMapping("/getErpMaterialByMainMaterialById")
    @Operation(summary = "通过主物料查询子物料信息")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:query','basic:material-config:query'})")
    public CommonResult<List<ErpMaterialRespVO>> getErpMaterialByMainMaterial(@RequestParam("id") Long mainMaterialId, @RequestParam(value = "deptId",required = false) String companyCode) {
        List<ErpMaterialRespVO> erpMaterial = erpMaterialService.getErpMaterialByMainMaterial(mainMaterialId, companyCode);
        return success(BeanUtils.toBean(erpMaterial, ErpMaterialRespVO.class));
    }

    //查询物料
    @PostMapping("/api-erp-material")
    @Operation(summary = "通过接口查询物料")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:query','basic:material-config:query'})")
    public CommonResult<PageResult<ErpMaterialRespVO>> getErpMaterialByApi(@RequestBody MaterialInfomationApiVO vo) {
        PageResult<ErpMaterialDO> erpMaterialByApi = erpMaterialService.getErpMaterialByApi(vo);

        return success(BeanUtils.toBean(erpMaterialByApi, ErpMaterialRespVO.class));
    }

    @PostMapping("/api-erp-prod-material")
    @Operation(summary = "查询物料,产品专用")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:query','basic:material-config:query'})")
    public CommonResult<PageResult<ErpMaterialRespVO>> getErpProdMaterialByApi(@RequestBody MaterialInfomationApiVO vo) {
        PageResult<ErpMaterialDO> erpMaterialByApi = erpMaterialService.getErpProdMaterialByApi(vo);

        return success(BeanUtils.toBean(erpMaterialByApi, ErpMaterialRespVO.class));
    }
    //通过主物料查询子物料信息
    @GetMapping("/erpMaterial-mainMaterial-code")
    @Operation(summary = "通过主物料编号查询子物料信息")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:query','basic:material-config:query'})")
    public CommonResult<List<ErpMaterialRespVO>> getErpMaterialByMainMaterialByCode(@RequestParam("materialNumber") String code,@RequestParam(value = "deptId",required = false) String deptId) {
        return success(BeanUtils.toBean(erpMaterialService.getErpMaterialByMainMaterialByCode(code,deptId), ErpMaterialRespVO.class));
    }

    //查询物料
    @GetMapping("/api-erp-material-code")
    @Operation(summary = "通过编号接口查询物料")
    @PreAuthorize("@ss.hasAnyPermissions({'sply:erp-material:query','basic:material-config:query'})")
    public CommonResult<List<ErpMaterialRespVO>> getErpMaterialByApiByCode(@RequestParam("materialNumber")String code,@RequestParam(value = "deptId",required = false) String deptId) {
        return success(BeanUtils.toBean(erpMaterialService.getErpMaterialByApiByCode(code,deptId), ErpMaterialRespVO.class));
    }
}