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.ErpContractPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpContractRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpContractSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpContractDO;
import com.zt.plat.module.erp.service.erp.ErpContractService;
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-contract")
@Validated
public class ErpContractController {


    @Resource
    private ErpContractService erpContractService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP合同映射")
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:create')")
    public CommonResult<ErpContractRespVO> createErpContract(@Valid @RequestBody ErpContractSaveReqVO createReqVO) {
        return success(erpContractService.createErpContract(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP合同映射")
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:update')")
    public CommonResult<Boolean> updateErpContract(@Valid @RequestBody ErpContractSaveReqVO updateReqVO) {
        erpContractService.updateErpContract(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP合同映射")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:delete')")
    public CommonResult<Boolean> deleteErpContract(@RequestParam("id") Long id) {
        erpContractService.deleteErpContract(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP合同映射")
                @PreAuthorize("@ss.hasPermission('sply:erp-contract:delete')")
    public CommonResult<Boolean> deleteErpContractList(@RequestBody BatchDeleteReqVO req) {
        erpContractService.deleteErpContractListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP合同映射")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:query')")
    public CommonResult<ErpContractRespVO> getErpContract(@RequestParam("id") Long id) {
        ErpContractDO erpContract = erpContractService.getErpContract(id);
        return success(BeanUtils.toBean(erpContract, ErpContractRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP合同映射分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:query')")
    public CommonResult<PageResult<ErpContractRespVO>> getErpContractPage(@Valid ErpContractPageReqVO pageReqVO) {
        PageResult<ErpContractDO> pageResult = erpContractService.getErpContractPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpContractRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP合同映射 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpContractExcel(@Valid ErpContractPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpContractDO> list = erpContractService.getErpContractPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP合同映射.xls", "数据", ErpContractRespVO.class,
                        BeanUtils.toBean(list, ErpContractRespVO.class));
    }

    @PostMapping("/getErpContractTask")
    @Operation(summary = "定时获得erp更新合同")
    @PreAuthorize("@ss.hasPermission('sply:erp-contract:sync')")
    public CommonResult<Boolean> getErpContractTask() {
        erpContractService.callErpRfcInterface();
        return success(true);
    }
}