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.ErpProcessPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpProcessDO;
import com.zt.plat.module.erp.service.erp.ErpProcessService;
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-process")
@Validated
public class ErpProcessController {


    @Resource
    private ErpProcessService erpProcessService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP工艺路线")
    @PreAuthorize("@ss.hasPermission('sply:erp-process:create')")
    public CommonResult<ErpProcessRespVO> createErpProcess(@Valid @RequestBody ErpProcessSaveReqVO createReqVO) {
        return success(erpProcessService.createErpProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP工艺路线")
    @PreAuthorize("@ss.hasPermission('sply:erp-process:update')")
    public CommonResult<Boolean> updateErpProcess(@Valid @RequestBody ErpProcessSaveReqVO updateReqVO) {
        erpProcessService.updateErpProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP工艺路线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-process:delete')")
    public CommonResult<Boolean> deleteErpProcess(@RequestParam("id") Long id) {
        erpProcessService.deleteErpProcess(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP工艺路线")
                @PreAuthorize("@ss.hasPermission('sply:erp-process:delete')")
    public CommonResult<Boolean> deleteErpProcessList(@RequestBody BatchDeleteReqVO req) {
        erpProcessService.deleteErpProcessListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP工艺路线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-process:query')")
    public CommonResult<ErpProcessRespVO> getErpProcess(@RequestParam("id") Long id) {
        ErpProcessDO erpProcess = erpProcessService.getErpProcess(id);
        return success(BeanUtils.toBean(erpProcess, ErpProcessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP工艺路线分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-process:query')")
    public CommonResult<PageResult<ErpProcessRespVO>> getErpProcessPage(@Valid ErpProcessPageReqVO pageReqVO) {
        PageResult<ErpProcessDO> pageResult = erpProcessService.getErpProcessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpProcessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP工艺路线 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-process:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpProcessExcel(@Valid ErpProcessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProcessDO> list = erpProcessService.getErpProcessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP工艺路线.xls", "数据", ErpProcessRespVO.class,
                        BeanUtils.toBean(list, ErpProcessRespVO.class));
    }

    @PostMapping("/getErpProcessTask")
    @Operation(summary = "定时获得erp更新工艺路线")
    @PreAuthorize("@ss.hasPermission('sply:erp-process:sync')")
    public CommonResult<Boolean> getErpProcessTask() {
        erpProcessService.callErpRfcInterface();
        return success(true);
    }

}