package com.zt.plat.module.qms.resource.material.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
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.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.resource.material.controller.vo.*;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialLifecycleDO;
import com.zt.plat.module.qms.resource.material.listener.MaterialAssayResultListener;
import com.zt.plat.module.qms.resource.material.service.MaterialLifecycleService;
import com.zt.plat.module.qms.resource.material.valid.AddGroup;
import com.zt.plat.module.qms.resource.material.valid.UpdateGroup;
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.beans.factory.annotation.Autowired;
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 = "管理后台 - 物料通用流程,物料验收、退换货")
@RestController
@RequestMapping("/qms/resource/material-lifecycle")
@Validated
@FileUploadController(source = "resource.materiallifecycle")
@DeptDataPermissionIgnore(enable = "true")
public class MaterialLifecycleController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = MaterialLifecycleController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private MaterialLifecycleService materialLifecycleService;
    @Autowired
    private MaterialAssayResultListener materialAssayResultListener;

    @PostMapping("/create")
    @Operation(summary = "新建物料流程")
//    @PreAuthorize("@ss.hasPermission('qms:material-lifecycle:create')")
    public CommonResult<MaterialLifecycleRespVO> createMaterialLifecycle(@Validated(AddGroup.class) @RequestBody MaterialLifecycleSaveReqVO createReqVO) {
        return success(materialLifecycleService.createMaterialLifecycle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料流程")
//    @PreAuthorize("@ss.hasPermission('qms:material-lifecycle:update')")
    public CommonResult<MaterialLifecycleRespVO> updateMaterialLifecycle(@Validated(UpdateGroup.class) @RequestBody MaterialLifecycleSaveReqVO updateReqVO) {
        MaterialLifecycleDO lifecycleDO = materialLifecycleService.updateMaterialLifecycle(updateReqVO);
        return success(BeanUtils.toBean(lifecycleDO, MaterialLifecycleRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料流程")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:material-lifecycle:delete')")
    public CommonResult<Boolean> deleteMaterialLifecycle(@RequestParam("id") Long id) {
        materialLifecycleService.deleteMaterialLifecycle(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料流程")
                @PreAuthorize("@ss.hasPermission('t:material-lifecycle:delete')")
    public CommonResult<Boolean> deleteMaterialLifecycleList(@RequestBody BatchDeleteReqVO req) {
        materialLifecycleService.deleteMaterialLifecycleListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料流程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:material-lifecycle:query')")
    public CommonResult<MaterialLifecycleRespVO> getMaterialLifecycle(@RequestParam("id") Long id) {
        MaterialLifecycleRespVO lifecycleRespVO = materialLifecycleService.getMaterialLifecycleWithDetailList(id);
        return success(lifecycleRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料流程分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-lifecycle:query')")
    public CommonResult<PageResult<MaterialLifecycleRespVO>> getMaterialLifecycleRespVOPage(@Valid MaterialLifecyclePageReqVO pageReqVO) {
        PageResult<MaterialLifecycleRespVO> pageResult = materialLifecycleService.getMaterialLifecycleRespVOPage(pageReqVO);
        return success(pageResult);
    }

    @PutMapping("/submit")
    @Operation(summary = "提交（验收等流程）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> submitLifecycle(@RequestParam("id") Long id) {

        return success(materialLifecycleService.submitLifecycle(id));
    }

    @PutMapping("/operator-submit")
    @Operation(summary = "执行人提交（验收等流程）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> operatorSubmitLifecycle(@RequestParam("id") Long id) {

        return success(materialLifecycleService.operatorSubmitLifecycle(id));
    }

    @PutMapping("/inventory-inf")
    @Operation(summary = "盘点物料")
    public CommonResult<MaterialInventoryCheckDetailRespVO> inventoryCheckInfomation(@Valid @RequestBody MaterialInventoryCheckDetailSaveReqVO inventoryReqVO) {
        return success(materialLifecycleService.inventoryCheckInfomation(inventoryReqVO));
    }

    @PutMapping("/manual-execute-flow")
    @Operation(summary = "手动执行流程，主要用于测试")
    @Parameter(name = "flowId", description = "流程id", required = true)
    public void test(@RequestParam("flowId") String flowId) {
        materialAssayResultListener.approveTask(flowId);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料通用流程,物料验收、退换货 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-lifecycle:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialLifecycleExcel(@Valid MaterialLifecyclePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialLifecycleDO> list = materialLifecycleService.getMaterialLifecyclePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料通用流程,物料验收、退换货.xls", "数据", MaterialLifecycleRespVO.class,
                        BeanUtils.toBean(list, MaterialLifecycleRespVO.class));
    }

}