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

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.renew.vo.OaRenewPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.renew.vo.OaRenewRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.renew.vo.OaRenewSaveReqVO;
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.renew.OaRenewService;
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-renew")
@Validated
public class OaRenewController {

    @Resource
    private OaRenewService oaRenewService;

    @PostMapping("/staging")
    @Operation(summary = "暂存续签审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-renew:staging')")
    public CommonResult<Long> stagingOaRenew(@RequestBody OaRenewSaveReqVO stagingReqVO) {
        Long oaRenewId = oaRenewService.stagingOaRenew(stagingReqVO);
        return success(oaRenewId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交续签审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-renew:commit')")
    public CommonResult<Long> commitOaRenew(@Valid @RequestBody OaRenewSaveReqVO commitReqVO) {
        Long oaRenewId = oaRenewService.commitOaRenew(commitReqVO);
        return success(oaRenewId, "提交成功");
    }

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

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

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

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

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

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

    @DeleteMapping("/delete")
    @Operation(summary = "删除续签审批流程信息")
    @ApiOperationSupport(order = 8)
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-renew:delete')")
    public CommonResult<Boolean> deleteOaRenew(@RequestParam("id") Long id) {
        oaRenewService.deleteOaRenew(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-renew:query-id')")
    public CommonResult<OaRenewRespVO> getOaRenew(@RequestParam("id") Long id) {
        OaRenewRespVO respVO = oaRenewService.getOaRenew(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-renew:query-proc-inst-id')")
    public CommonResult<OaRenewRespVO> getOaRenewByProcInstId(@RequestParam("procInstId") String procInstId) {
        OaRenewRespVO respVO = oaRenewService.getOaRenewByProcInstId(procInstId);
        return success(respVO, "查询对象成功");
    }

    @GetMapping("/page")
    @Operation(summary = "获得续签审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-renew:query-page')")
    public CommonResult<PageResult<OaRenewRespVO>> getOaRenewPage(@Valid @ParameterObject OaRenewPageReqVO pageReqVO) {
        return success(oaRenewService.getOaRenewPage(pageReqVO), "查询列表成功");
    }

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

}