package cn.iocoder.yudao.module.bpm.controller.admin.oa.purchase;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.purchase.vo.OaPurchasePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.purchase.vo.OaPurchaseRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.purchase.vo.OaPurchaseSaveReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskApproveReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskReturnReqVO;
import cn.iocoder.yudao.module.bpm.service.oa.purchase.OaPurchaseService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 采购流程信息")
@RestController
@RequestMapping("/bpm/oa-purchase")
@Validated
public class OaPurchaseController {


    @Resource
    private OaPurchaseService oaPurchaseService;

    @PostMapping("/staging")
    @Operation(summary = "暂存采购审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:staging')")
    public CommonResult<Long> stagingOaPurchase(@RequestBody OaPurchaseSaveReqVO stagingReqVO) {
        Long oaPurchaseId = oaPurchaseService.stagingOaPurchase(stagingReqVO);
        return success(oaPurchaseId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交采购审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:commit')")
    public CommonResult<Long> commitOaPurchase(@Valid @RequestBody OaPurchaseSaveReqVO commitReqVO) {
        Long oaPurchaseId = oaPurchaseService.commitOaPurchase(commitReqVO);
        return success(oaPurchaseId, "提交成功");
    }

    @PostMapping("/agree")
    @Operation(summary = "审批同意采购审批流程信息")
    @ApiOperationSupport(order = 3)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:agree')")
    public CommonResult<Long> agreeOaPurchase(@Valid @RequestBody BpmTaskApproveReqVO agreeReqVO) {
        Long result = oaPurchaseService.agreeOaPurchase(agreeReqVO);
        return success(result, "审批成功");
    }

    @PostMapping("/disagree")
    @Operation(summary = "驳回采购审批流程信息")
    @ApiOperationSupport(order = 4)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:disagree')")
    public CommonResult<Long> disagreeOaPurchase(@Valid @RequestBody BpmTaskReturnReqVO disagreeReqVO) {
        Long result = oaPurchaseService.disagreeOaPurchase(disagreeReqVO);
        return success(result, "驳回成功");
    }

    @PostMapping("/revocation")
    @Operation(summary = "撤回采购审批流程信息")
    @ApiOperationSupport(order = 5)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:revocation')")
    public CommonResult<Long> revocationOaPurchase(@Valid @RequestBody BpmTaskApproveReqVO revocationReqVO) {
        Long result = oaPurchaseService.revocationOaPurchase(revocationReqVO);
        return success(result, "撤回成功");
    }

    @PostMapping("/reCommit")
    @Operation(summary = "驳回或撤回后再次提交采购审批流程信息")
    @ApiOperationSupport(order = 6)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:re-commit')")
    public CommonResult<Long> reCommitOaPurchase(@Valid @RequestBody OaPurchaseSaveReqVO reCommitReqVO) {
        Long result = oaPurchaseService.reCommitOaPurchase(reCommitReqVO);
        return success(result, "再次提交成功");
    }

    @DeleteMapping("/close")
    @Operation(summary = "驳回或撤回后关闭采购审批流程信息")
    @ApiOperationSupport(order = 7)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-entry:close')")
    public CommonResult<Long> closeOaPurchase(@RequestParam("id") Long id) {
        Long result = oaPurchaseService.closeOaPurchase(id);

        return success(result, "关闭成功");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购审批流程信息")
    @ApiOperationSupport(order = 8)
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:delete')")
    public CommonResult<Boolean> deleteOaPurchase(@RequestParam("id") Long id) {
        oaPurchaseService.deleteOaPurchase(id);
        return success(true, "删除成功");
    }

    @GetMapping("/get")
    @Operation(summary = "根据id获得采购审批流程信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @ApiOperationSupport(order = 9)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:query-id')")
    public CommonResult<OaPurchaseRespVO> getOaPurchase(@RequestParam("id") Long id) {
        OaPurchaseRespVO respVO = oaPurchaseService.getOaPurchase(id);
        return success(respVO, "查询对象成功");
    }

    @GetMapping("/getByProcInstId")
    @Operation(summary = "根据流程实例id获得采购审批流程信息")
    @Parameter(name = "procInstId", description = "流程实例id", required = true, example = "9528a78d-457e-11ef-853c-4c034fce6445")
    @ApiOperationSupport(order = 10)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:query-proc-inst-id')")
    public CommonResult<OaPurchaseRespVO> getOaPurchaseByProcInstId(@RequestParam("procInstId") String procInstId) {
        OaPurchaseRespVO respVO = oaPurchaseService.getOaPurchaseByProcInstId(procInstId);
        return success(respVO, "查询对象成功");
    }

    @GetMapping("/page")
    @Operation(summary = "获得采购审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:query-page')")
    public CommonResult<PageResult<OaPurchaseRespVO>> getOaPurchasePage(@Valid @ParameterObject OaPurchasePageReqVO pageReqVO) {
        return success(oaPurchaseService.getOaPurchasePage(pageReqVO), "查询列表成功");
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购审批流程信息 Excel")
    @ApiOperationSupport(order = 12)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaPurchaseExcel(@ParameterObject OaPurchasePageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        PageResult<OaPurchaseRespVO> pageResult = oaPurchaseService.exportOaPurchaseExcel(pageReqVO);
        List<OaPurchaseRespVO> list = pageResult.getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购审批流程信息.xls", "数据", OaPurchaseRespVO.class,
                BeanUtils.toBean(list, OaPurchaseRespVO.class));
    }

    // @Resource
    // private OaPurchaseService oaPurchaseService;
    //
    // @PostMapping("/create")
    // @Operation(summary = "创建采购流程信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:create')")
    // public CommonResult<Long> createOaPurchase(@Valid @RequestBody OaPurchaseSaveReqVO createReqVO) {
    //     return success(oaPurchaseService.createOaPurchase(createReqVO));
    // }
    //
    // @PutMapping("/update")
    // @Operation(summary = "更新采购流程信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:update')")
    // public CommonResult<Boolean> updateOaPurchase(@Valid @RequestBody OaPurchaseSaveReqVO updateReqVO) {
    //     oaPurchaseService.updateOaPurchase(updateReqVO);
    //     return success(true);
    // }
    //
    // @DeleteMapping("/delete")
    // @Operation(summary = "删除采购流程信息")
    // @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:delete')")
    // public CommonResult<Boolean> deleteOaPurchase(@RequestParam("id") Long id) {
    //     oaPurchaseService.deleteOaPurchase(id);
    //     return success(true);
    // }
    //
    // @GetMapping("/get")
    // @Operation(summary = "获得采购流程信息")
    // @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:query')")
    // public CommonResult<OaPurchaseRespVO> getOaPurchase(@RequestParam("id") Long id) {
    //     OaPurchaseDO oaPurchase = oaPurchaseService.getOaPurchase(id);
    //     return success(BeanUtils.toBean(oaPurchase, OaPurchaseRespVO.class));
    // }
    //
    // @GetMapping("/page")
    // @Operation(summary = "获得采购流程信息分页")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:query')")
    // public CommonResult<PageResult<OaPurchaseRespVO>> getOaPurchasePage(@Valid OaPurchasePageReqVO pageReqVO) {
    //     PageResult<OaPurchaseDO> pageResult = oaPurchaseService.getOaPurchasePage(pageReqVO);
    //     return success(BeanUtils.toBean(pageResult, OaPurchaseRespVO.class));
    // }
    //
    // @GetMapping("/export-excel")
    // @Operation(summary = "导出采购流程信息 Excel")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:export')")
    // @ApiAccessLog(operateType = EXPORT)
    // public void exportOaPurchaseExcel(@Valid OaPurchasePageReqVO pageReqVO,
    //           HttpServletResponse response) throws IOException {
    //     pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    //     List<OaPurchaseDO> list = oaPurchaseService.getOaPurchasePage(pageReqVO).getList();
    //     // 导出 Excel
    //     ExcelUtils.write(response, "采购流程信息.xls", "数据", OaPurchaseRespVO.class,
    //                     BeanUtils.toBean(list, OaPurchaseRespVO.class));
    // }
    //
    // // ==================== 子表（采购流程信息子表） ====================
    //
    // @GetMapping("/oa-purchase-obj/list-by-purchase-id")
    // @Operation(summary = "获得采购流程信息子表列表")
    // @Parameter(name = "purchaseId", description = "采购主表id")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-purchase:query')")
    // public CommonResult<List<OaPurchaseObjDO>> getOaPurchaseObjListByPurchaseId(@RequestParam("purchaseId") Long purchaseId) {
    //     return success(oaPurchaseService.getOaPurchaseObjListByPurchaseId(purchaseId));
    // }

}