package com.ruoyi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsApprovalConfig;
import com.ruoyi.cs.domain.dto.CsApprovalConfigAddDTO;
import com.ruoyi.cs.domain.dto.CsApprovalRecordCheckDTO;
import com.ruoyi.cs.domain.dto.CsApprovalRecordCloseApplyDTO;
import com.ruoyi.cs.domain.vo.CsApprovalRecordVO;
import com.ruoyi.cs.service.ICsApprovalConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 管理端PC/审核流程
 *
 * @author cc
 * @date 2024-04-30
 */
@RestController
@RequestMapping("/cs/csApprovalConfig")
public class CsApprovalConfigController extends BaseController
{
    @Autowired
    private ICsApprovalConfigService csApprovalConfigService;

    /**
     * 查询审核流程节点规则配置列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsApprovalConfig csApprovalConfig)
    {
        startPage();
        List<CsApprovalConfig> list = csApprovalConfigService.selectCsApprovalConfigList(csApprovalConfig);
        return getDataTable(list);
    }

    /**
     * 导出审核流程节点规则配置列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:export')")
    @Log(title = "审核流程节点规则配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsApprovalConfig csApprovalConfig)
    {
        List<CsApprovalConfig> list = csApprovalConfigService.selectCsApprovalConfigList(csApprovalConfig);
        ExcelUtil<CsApprovalConfig> util = new ExcelUtil<CsApprovalConfig>(CsApprovalConfig.class);
        util.exportExcel(response, list, "审核流程节点规则配置数据");
    }

    /**
     * 获取审核流程节点规则配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:query')")
    @GetMapping(value = "/{approvalConfigId}")
    public AjaxResult getInfo(@PathVariable("approvalConfigId") String approvalConfigId)
    {
        return AjaxResult.success(csApprovalConfigService.selectCsApprovalConfigByApprovalConfigId(approvalConfigId));
    }

    /**
     * 新增审核流程节点规则配置
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:add')")
    @Log(title = "审核流程节点规则配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsApprovalConfig csApprovalConfig)
    {
        return toAjax(csApprovalConfigService.insertCsApprovalConfig(csApprovalConfig));
    }

    /**
     * 修改审核流程节点规则配置
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "审核流程节点规则配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsApprovalConfig csApprovalConfig)
    {
        return toAjax(csApprovalConfigService.updateCsApprovalConfig(csApprovalConfig));
    }

    /**
     * 删除审核流程节点规则配置
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:remove')")
    @Log(title = "审核流程节点规则配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{approvalConfigIds}")
    public AjaxResult remove(@PathVariable String[] approvalConfigIds)
    {
        return toAjax(csApprovalConfigService.deleteCsApprovalConfigByApprovalConfigIds(approvalConfigIds));
    }

    /**
     * 分配审核流程
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:add')")
    @Log(title = "分配审核流程", businessType = BusinessType.INSERT)
    @PostMapping("/addConfigs")
    @RepeatSubmit
    public AjaxResult addConfigs(@RequestBody CsApprovalConfigAddDTO dto)
    {
        return toAjax(csApprovalConfigService.insertCsApprovalConfigByAddDTO(dto));
    }


    /**
     * 获取工单流程、 审核信息列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:query')")
    @GetMapping(value = "/getFlowList")
    public AjaxResult getFlowList(CsApprovalConfig csApprovalConfig)
    {
        return AjaxResult.success(csApprovalConfigService.getOrderApprovalInfoList(csApprovalConfig));
    }

    /**
     * 提交审核(审核流程)
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:add')")
    @Log(title = "提交审核(审核流程)", businessType = BusinessType.INSERT)
    @PostMapping("/checkConfigs")
    public AjaxResult checkConfigs(@RequestBody CsApprovalRecordCheckDTO dto)
    {
        return toAjax(csApprovalConfigService.checkFlowNode(dto));
    }

    /**
     * 关闭审核流程及工单
     */
    @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:add')")
    @Log(title = "关闭工单", businessType = BusinessType.INSERT)
    @PostMapping("/closeApply")
    public AjaxResult closeApply(@RequestBody CsApprovalRecordCloseApplyDTO dto)
    {
        return toAjax(csApprovalConfigService.editRecordAndCloseApply(dto));
    }

    /**
     * 主管更改受理人员
     */
    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "主管更改受理人员", businessType = BusinessType.UPDATE)
    @PostMapping("/leaderUpdateAccept")
    public AjaxResult leaderUpdateAccept(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.leaderUpdateAccept(dto));
    }

    /**
     * 主管转交其他部门人员
     */
    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "主管转交其他部门人员", businessType = BusinessType.UPDATE)
    @PostMapping("/leaderTransferOther")
    public AjaxResult leaderTransferOther(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.leaderTransferOther(dto));
    }

    /**
     * 转交其他部门人员
     */
    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "转交其他部门人员", businessType = BusinessType.UPDATE)
    @PostMapping("/transferOther")
    public AjaxResult transferOther(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.transferOther(dto));
    }

    /**
     * 撤回转交
     */
    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "撤回转交", businessType = BusinessType.UPDATE)
    @PostMapping("/withdrawTransfer")
    public AjaxResult withdrawTransfer(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.withdrawTransfer(dto));
    }

    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "撤回委派", businessType = BusinessType.UPDATE)
    @PostMapping("/withdrawDelegation")
    public AjaxResult withdrawDelegation(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.withdrawDelegation(dto));
    }

    /**
     * 拒绝转交
     */
    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "拒绝转交", businessType = BusinessType.UPDATE)
    @PostMapping("/refuseTransfer")
    public AjaxResult refuseTransfer(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.refuseTransfer(dto));
    }

    /**
     * 审批回退
     */
    // @PreAuthorize("@ss.hasPermi('cs:csApprovalConfig:edit')")
    @Log(title = "审批回退", businessType = BusinessType.UPDATE)
    @PostMapping("/approvalRollback")
    public AjaxResult approvalRollback(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.approvalRollback(dto));
    }

    /**
     * (已过节点)修改审批意见
     */
    @Log(title = "修改审批意见", businessType = BusinessType.UPDATE)
    @PostMapping("/editApproverOpinions")
    public AjaxResult editApproverOpinions(@RequestBody CsApprovalRecordVO dto)
    {
        return toAjax(csApprovalConfigService.editApproverOpinions(dto));
    }
}


