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

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.turnover.vo.OaTurnoverPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.turnover.vo.OaTurnoverRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.turnover.vo.OaTurnoverSaveReqVO;
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.turnover.OaTurnoverService;
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-turnover")
@Validated
public class OaTurnoverController {

    @Resource
    private OaTurnoverService oaTurnoverService;

    @PostMapping("/staging")
    @Operation(summary = "暂存离职审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-turnover:staging')")
    public CommonResult<Long> stagingOaTurnover(@RequestBody OaTurnoverSaveReqVO stagingReqVO) {
        Long oaTurnoverId = oaTurnoverService.stagingOaTurnover(stagingReqVO);
        return success(oaTurnoverId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交离职审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-turnover:commit')")
    public CommonResult<Long> commitOaTurnover(@Valid @RequestBody OaTurnoverSaveReqVO commitReqVO) {
        Long oaTurnoverId = oaTurnoverService.commitOaTurnover(commitReqVO);
        return success(oaTurnoverId, "提交成功");
    }

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

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

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

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得离职审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-turnover:query-page')")
    public CommonResult<PageResult<OaTurnoverRespVO>> getOaTurnoverPage(@Valid @ParameterObject OaTurnoverPageReqVO pageReqVO) {
        return success(oaTurnoverService.getOaTurnoverPage(pageReqVO), "查询列表成功");
    }

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

}