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

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.conversion.vo.OaConversionPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.conversion.vo.OaConversionRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.conversion.vo.OaConversionSaveReqVO;
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.conversion.OaConversionService;
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-conversion")
@Validated
public class OaConversionController {

    @Resource
    private OaConversionService oaConversionService;

    @PostMapping("/staging")
    @Operation(summary = "暂存转正审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-conversion:staging')")
    public CommonResult<Long> stagingOaConversion(@RequestBody OaConversionSaveReqVO stagingReqVO) {
        Long oaConversionId = oaConversionService.stagingOaConversion(stagingReqVO);
        return success(oaConversionId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交转正审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-conversion:commit')")
    public CommonResult<Long> commitOaConversion(@Valid @RequestBody OaConversionSaveReqVO commitReqVO) {
        Long oaConversionId = oaConversionService.commitOaConversion(commitReqVO);
        return success(oaConversionId, "提交成功");
    }

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

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

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

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得转正审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-conversion:query-page')")
    public CommonResult<PageResult<OaConversionRespVO>> getOaConversionPage(@Valid @ParameterObject OaConversionPageReqVO pageReqVO) {
        return success(oaConversionService.getOaConversionPage(pageReqVO), "查询列表成功");
    }

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

}