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

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.universal.vo.OaUniversalPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.universal.vo.OaUniversalRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.universal.vo.OaUniversalSaveReqVO;
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.universal.OaUniversalService;
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-universal")
@Validated
public class OaUniversalController {

    @Resource
    private OaUniversalService oaUniversalService;

    @PostMapping("/staging")
    @Operation(summary = "暂存通用事项审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-universal:staging')")
    public CommonResult<Long> stagingOaUniversal(@RequestBody OaUniversalSaveReqVO stagingReqVO) {
        Long oaUniversalId = oaUniversalService.stagingOaUniversal(stagingReqVO);
        return success(oaUniversalId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交通用事项审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-universal:commit')")
    public CommonResult<Long> commitOaUniversal(@Valid @RequestBody OaUniversalSaveReqVO commitReqVO) {
        Long oaUniversalId = oaUniversalService.commitOaUniversal(commitReqVO);
        return success(oaUniversalId, "提交成功");
    }

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

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

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

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

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

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

    @DeleteMapping("/delete")
    @Operation(summary = "删除通用事项审批流程信息")
    @ApiOperationSupport(order = 8)
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-universal:delete')")
    public CommonResult<Boolean> deleteOaUniversal(@RequestParam("id") Long id) {
        oaUniversalService.deleteOaUniversal(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-universal:query-id')")
    public CommonResult<OaUniversalRespVO> getOaUniversal(@RequestParam("id") Long id) {
        OaUniversalRespVO respVO = oaUniversalService.getOaUniversal(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-universal:query-proc-inst-id')")
    public CommonResult<OaUniversalRespVO> getOaUniversalByProcInstId(@RequestParam("procInstId") String procInstId) {
        OaUniversalRespVO respVO = oaUniversalService.getOaUniversalByProcInstId(procInstId);
        return success(respVO, "查询对象成功");
    }

    @GetMapping("/page")
    @Operation(summary = "获得通用事项审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-universal:query-page')")
    public CommonResult<PageResult<OaUniversalRespVO>> getOaUniversalPage(@Valid @ParameterObject OaUniversalPageReqVO pageReqVO) {
        return success(oaUniversalService.getOaUniversalPage(pageReqVO), "查询列表成功");
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通用事项审批流程信息 Excel")
    @ApiOperationSupport(order = 12)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-universal:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaUniversalExcel(@ParameterObject OaUniversalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        PageResult<OaUniversalRespVO> pageResult = oaUniversalService.exportOaUniversalExcel(pageReqVO);
        List<OaUniversalRespVO> list = pageResult.getList();
        // 导出 Excel
        ExcelUtils.write(response, "通用事项审批流程信息.xls", "数据", OaUniversalRespVO.class,
                        BeanUtils.toBean(list, OaUniversalRespVO.class));
    }

}