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

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.entry.vo.OaEntryPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.entry.vo.OaEntryRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.entry.vo.OaEntrySaveReqVO;
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.entry.OaEntryService;
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-entry")
@Validated
public class OaEntryController {

    @Resource
    private OaEntryService oaEntryService;

    @PostMapping("/staging")
    @Operation(summary = "暂存入职审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-entry:staging')")
    public CommonResult<Long> stagingOaEntry(@RequestBody OaEntrySaveReqVO stagingReqVO) {
        Long oaEntryId = oaEntryService.stagingOaEntry(stagingReqVO);
        return success(oaEntryId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交入职审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-entry:commit')")
    public CommonResult<Long> commitOaEntry(@Valid @RequestBody OaEntrySaveReqVO commitReqVO) {
        Long oaEntryId = oaEntryService.commitOaEntry(commitReqVO);
        return success(oaEntryId, "提交成功");
    }

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

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

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

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

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

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

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

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

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