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.ErpMaterialPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialSaveReqVO;
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 {


    @Resource
    private ErpMaterialService erpMaterialService;

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

    @PutMapping("/update")
    @Operation(summary = "更新ERP物料数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-material: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.hasPermission('sply:erp-material: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:create')")
    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));
    }

}