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

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.receive.vo.OaReceivePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.receive.vo.OaReceiveRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.receive.vo.OaReceiveSaveReqVO;
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.receive.OaReceiveService;
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-receive")
@Validated
public class OaReceiveController {


    @Resource
    private OaReceiveService oaReceiveService;

    @PostMapping("/staging")
    @Operation(summary = "暂存领用审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:staging')")
    public CommonResult<Long> stagingOaReceive(@RequestBody OaReceiveSaveReqVO stagingReqVO) {
        Long oaReceiveId = oaReceiveService.stagingOaReceive(stagingReqVO);
        return success(oaReceiveId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交领用审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:commit')")
    public CommonResult<Long> commitOaReceive(@Valid @RequestBody OaReceiveSaveReqVO commitReqVO) {
        Long oaReceiveId = oaReceiveService.commitOaReceive(commitReqVO);
        return success(oaReceiveId, "提交成功");
    }

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

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

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

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得领用审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:query-page')")
    public CommonResult<PageResult<OaReceiveRespVO>> getOaReceivePage(@Valid @ParameterObject OaReceivePageReqVO pageReqVO) {
        return success(oaReceiveService.getOaReceivePage(pageReqVO), "查询列表成功");
    }

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

    // @Resource
    // private OaReceiveService oaReceiveService;
    //
    // @PostMapping("/create")
    // @Operation(summary = "创建领用流程信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:create')")
    // public CommonResult<Long> createOaReceive(@Valid @RequestBody OaReceiveSaveReqVO createReqVO) {
    //     return success(oaReceiveService.createOaReceive(createReqVO));
    // }
    //
    // @PutMapping("/update")
    // @Operation(summary = "更新领用流程信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:update')")
    // public CommonResult<Boolean> updateOaReceive(@Valid @RequestBody OaReceiveSaveReqVO updateReqVO) {
    //     oaReceiveService.updateOaReceive(updateReqVO);
    //     return success(true);
    // }
    //
    // @DeleteMapping("/delete")
    // @Operation(summary = "删除领用流程信息")
    // @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:delete')")
    // public CommonResult<Boolean> deleteOaReceive(@RequestParam("id") Long id) {
    //     oaReceiveService.deleteOaReceive(id);
    //     return success(true);
    // }
    //
    // @GetMapping("/get")
    // @Operation(summary = "获得领用流程信息")
    // @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:query')")
    // public CommonResult<OaReceiveRespVO> getOaReceive(@RequestParam("id") Long id) {
    //     OaReceiveDO oaReceive = oaReceiveService.getOaReceive(id);
    //     return success(BeanUtils.toBean(oaReceive, OaReceiveRespVO.class));
    // }
    //
    // @GetMapping("/page")
    // @Operation(summary = "获得领用流程信息分页")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:query')")
    // public CommonResult<PageResult<OaReceiveRespVO>> getOaReceivePage(@Valid OaReceivePageReqVO pageReqVO) {
    //     PageResult<OaReceiveDO> pageResult = oaReceiveService.getOaReceivePage(pageReqVO);
    //     return success(BeanUtils.toBean(pageResult, OaReceiveRespVO.class));
    // }
    //
    // @GetMapping("/export-excel")
    // @Operation(summary = "导出领用流程信息 Excel")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:export')")
    // @ApiAccessLog(operateType = EXPORT)
    // public void exportOaReceiveExcel(@Valid OaReceivePageReqVO pageReqVO,
    //           HttpServletResponse response) throws IOException {
    //     pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    //     List<OaReceiveDO> list = oaReceiveService.getOaReceivePage(pageReqVO).getList();
    //     // 导出 Excel
    //     ExcelUtils.write(response, "领用流程信息.xls", "数据", OaReceiveRespVO.class,
    //                     BeanUtils.toBean(list, OaReceiveRespVO.class));
    // }
    //
    // // ==================== 子表（领用流程信息子表） ====================
    //
    // @GetMapping("/oa-receive-obj/list-by-receive-id")
    // @Operation(summary = "获得领用流程信息子表列表")
    // @Parameter(name = "receiveId", description = "领用主表id")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-receive:query')")
    // public CommonResult<List<OaReceiveObjDO>> getOaReceiveObjListByReceiveId(@RequestParam("receiveId") Long receiveId) {
    //     return success(oaReceiveService.getOaReceiveObjListByReceiveId(receiveId));
    // }

}