package com.ruoyi.project.pssr.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.listener.pssr.ConfirmTaskCreateListener;
import com.ruoyi.project.pssr.domain.*;
import com.ruoyi.project.pssr.service.*;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/pssr/safety")
public class TPssrSafetyController extends BaseController {
    @Autowired
    private ITPssrFileService tPssrFileService;

    @Autowired
    private ITPssrTurndownService tPssrTurndownService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private ITPssrSafetyBleedService tPssrSafetyBleedService;

    @Autowired
    private ITPssrSafetyBreathService tPssrSafetyBreathService;

    @Autowired
    private ITPssrSafetyBrustService tPssrSafetyBrustService;

    @Autowired
    private ITPssrSafetyFlamearresterService tPssrSafetyFlamearresterService;

    @Autowired
    private ITPssrSafetyValveService tPssrSafetyValveService;

    @Autowired
    private ITPssrApproveService tPssrApproveService;

    @Autowired
    private ITPssrSubcontentService tPssrSubcontentService;


    /**
     * 确认安全设施-泄放阀
     */
    @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')")
    @Log(title = "安全设施-泄放阀", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmBleed")
    public AjaxResult confirmBleed(@RequestBody TPssrSafetyBleed tPssrSafetyBleed) {
        long queryStatus = 0;
        long approveStatus = 0;
        Date date = null;
        TPssrSafetyBleed bleed = new TPssrSafetyBleed();
        if (tPssrSafetyBleed.getTaskType() == 4) {
            //确认人1确认
            queryStatus = 1;
            approveStatus = 3;
            bleed.setConfirmer1(getUserId().toString());
            date = new Date();
        } else if (tPssrSafetyBleed.getTaskType() == 5) {
            //确认人2确认
            queryStatus = 3;
            approveStatus = 2;
            bleed.setConfirmer2(getUserId().toString());
            date = new Date();
        }
        if (tPssrSafetyBleed.getIds() != null && tPssrSafetyBleed.getIds().length > 0) {
            for (Long id : tPssrSafetyBleed.getIds()) {
                bleed = tPssrSafetyBleedService.selectTPssrSafetyBleedById(id);
                bleed.setConfirmationTime(new Date());
                bleed.setApproveStatus(approveStatus);
                bleed.setUpdatedate(new Date());
                bleed.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyBleedService.updateTPssrSafetyBleed(bleed);
            }
        } else {
            bleed.setSubId(tPssrSafetyBleed.getSubId());
            bleed.setApproveStatus(queryStatus);
            for (TPssrSafetyBleed safetyBleed : tPssrSafetyBleedService.selectTPssrSafetyBleedList(bleed)) {
                safetyBleed.setConfirmationTime(new Date());
                safetyBleed.setApproveStatus(approveStatus);
                safetyBleed.setUpdatedate(new Date());
                safetyBleed.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyBleedService.updateTPssrSafetyBleed(safetyBleed);
            }
        }
        //查询当前待审批的确认人
        TPssrSafetyBleed entity = new TPssrSafetyBleed();
        entity.setSubId(tPssrSafetyBleed.getSubId());
        entity.setApproveStatus(queryStatus);
        TPssrSafetyBleed safetyBleed = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
        if (safetyBleed != null) {
            //如果当前用户还有待审批任务
            if (tPssrSafetyBleed.getTaskType() == 4 && StringUtils.isNotEmpty(safetyBleed.getConfirmer1())) {
                if (safetyBleed.getConfirmer1().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }

            if (tPssrSafetyBleed.getTaskType() == 5 && StringUtils.isNotEmpty(safetyBleed.getConfirmer2())) {
                if (safetyBleed.getConfirmer2().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }
        }
        //无待审批任务结束当前用户流程

        // 因为流程关系所以approve一定会有且只有一条数据
        TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrSafetyBleed.getSubId());
        TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
        return AjaxResult.success();
    }

    /**
     * 确认安全设施-呼吸阀
     */
    @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')")
    @Log(title = "安全设施-呼吸阀", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmBreath")
    public AjaxResult confirmBreath(@RequestBody TPssrSafetyBreath tPssrSafetyBreath) {
        long queryStatus = 0;
        long approveStatus = 0;
        Date date = null;
        TPssrSafetyBreath breath = new TPssrSafetyBreath();
        if (tPssrSafetyBreath.getTaskType() == 4) {
            //确认人1确认
            queryStatus = 1;
            approveStatus = 3;
            breath.setConfirmer1(getUserId().toString());
            date = new Date();
        } else if (tPssrSafetyBreath.getTaskType() == 5) {
            //确认人2确认
            queryStatus = 3;
            approveStatus = 2;
            breath.setConfirmer2(getUserId().toString());
            date = new Date();
        }
        if (tPssrSafetyBreath.getIds() != null && tPssrSafetyBreath.getIds().length > 0) {
            for (Long id : tPssrSafetyBreath.getIds()) {
                breath = tPssrSafetyBreathService.selectTPssrSafetyBreathById(id);
                breath.setConfirmationTime(new Date());
                breath.setApproveStatus(approveStatus);
                breath.setUpdatedate(new Date());
                breath.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyBreathService.updateTPssrSafetyBreath(breath);
            }
        } else {
            breath.setSubId(tPssrSafetyBreath.getSubId());
            breath.setApproveStatus(queryStatus);
            for (TPssrSafetyBreath safetyBreath : tPssrSafetyBreathService.selectTPssrSafetyBreathList(breath)) {
                safetyBreath.setConfirmationTime(new Date());
                safetyBreath.setApproveStatus(approveStatus);
                safetyBreath.setUpdatedate(new Date());
                safetyBreath.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyBreathService.updateTPssrSafetyBreath(safetyBreath);
            }
        }
        //查询当前待审批的确认人
        TPssrSafetyBleed entity = new TPssrSafetyBleed();
        entity.setSubId(tPssrSafetyBreath.getSubId());
        entity.setApproveStatus(queryStatus);
        TPssrSafetyBleed safetyBreath = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
        if (safetyBreath != null) {
            //如果当前用户还有待审批任务
            if (tPssrSafetyBreath.getTaskType() == 4 && StringUtils.isNotEmpty(safetyBreath.getConfirmer1())) {
                if (safetyBreath.getConfirmer1().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }

            if (tPssrSafetyBreath.getTaskType() == 5 && StringUtils.isNotEmpty(safetyBreath.getConfirmer2())) {
                if (safetyBreath.getConfirmer2().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }
        }
        //无待审批任务结束当前用户流程

        // 因为流程关系所以approve一定会有且只有一条数据
        TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrSafetyBreath.getSubId());
        TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
        return AjaxResult.success();
    }

    /**
     * 确认安全设施-爆破片
     */
    @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')")
    @Log(title = "安全设施-爆破片", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmBrust")
    public AjaxResult confirmBrust(@RequestBody TPssrSafetyBrust tPssrSafetyBrust) {
        long queryStatus = 0;
        long approveStatus = 0;
        Date date = null;
        TPssrSafetyBrust brust = new TPssrSafetyBrust();
        if (tPssrSafetyBrust.getTaskType() == 4) {
            //确认人1确认
            queryStatus = 1;
            approveStatus = 3;
            brust.setConfirmer1(getUserId().toString());
            date = new Date();
        } else if (tPssrSafetyBrust.getTaskType() == 5) {
            //确认人2确认
            queryStatus = 3;
            approveStatus = 2;
            brust.setConfirmer2(getUserId().toString());
            date = new Date();
        }
        if (tPssrSafetyBrust.getIds() != null && tPssrSafetyBrust.getIds().length > 0) {
            for (Long id : tPssrSafetyBrust.getIds()) {
                brust = tPssrSafetyBrustService.selectTPssrSafetyBrustById(id);
                brust.setConfirmationTime(new Date());
                brust.setApproveStatus(approveStatus);
                brust.setUpdatedate(new Date());
                brust.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyBrustService.updateTPssrSafetyBrust(brust);
            }
        } else {
            brust.setSubId(tPssrSafetyBrust.getSubId());
            brust.setApproveStatus(queryStatus);
            for (TPssrSafetyBrust safetyBrust : tPssrSafetyBrustService.selectTPssrSafetyBrustList(brust)) {
                safetyBrust.setConfirmationTime(new Date());
                safetyBrust.setApproveStatus(approveStatus);
                safetyBrust.setUpdatedate(new Date());
                safetyBrust.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyBrustService.updateTPssrSafetyBrust(safetyBrust);
            }
        }
        //查询当前待审批的确认人
        TPssrSafetyBleed entity = new TPssrSafetyBleed();
        entity.setSubId(tPssrSafetyBrust.getSubId());
        entity.setApproveStatus(queryStatus);
        TPssrSafetyBleed safetyBrust = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
        if (safetyBrust != null) {
            //如果当前用户还有待审批任务
            if (tPssrSafetyBrust.getTaskType() == 4 && StringUtils.isNotEmpty(safetyBrust.getConfirmer1())) {
                if (safetyBrust.getConfirmer1().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }

            if (tPssrSafetyBrust.getTaskType() == 5 && StringUtils.isNotEmpty(safetyBrust.getConfirmer2())) {
                if (safetyBrust.getConfirmer2().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }
        }
        //无待审批任务结束当前用户流程

        // 因为流程关系所以approve一定会有且只有一条数据
        TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrSafetyBrust.getSubId());
        TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
        return AjaxResult.success();
    }

    /**
     * 确认安全设施-阻火器
     */
    @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')")
    @Log(title = "安全设施-阻火器", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmFlamearrester")
    public AjaxResult confirmFlamearrester(@RequestBody TPssrSafetyFlamearrester tPssrSafetyFlamearrester) {
        long queryStatus = 0;
        long approveStatus = 0;
        Date date = null;
        TPssrSafetyFlamearrester flamearrester = new TPssrSafetyFlamearrester();
        if (tPssrSafetyFlamearrester.getTaskType() == 4) {
            //确认人1确认
            queryStatus = 1;
            approveStatus = 3;
            flamearrester.setConfirmer1(getUserId().toString());
            date = new Date();
        } else if (tPssrSafetyFlamearrester.getTaskType() == 5) {
            //确认人2确认
            queryStatus = 3;
            approveStatus = 2;
            flamearrester.setConfirmer2(getUserId().toString());
            date = new Date();
        }
        if (tPssrSafetyFlamearrester.getIds() != null && tPssrSafetyFlamearrester.getIds().length > 0) {
            for (Long id : tPssrSafetyFlamearrester.getIds()) {
                flamearrester = tPssrSafetyFlamearresterService.selectTPssrSafetyFlamearresterById(id);
                flamearrester.setConfirmationTime(new Date());
                flamearrester.setApproveStatus(approveStatus);
                flamearrester.setUpdatedate(new Date());
                flamearrester.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyFlamearresterService.updateTPssrSafetyFlamearrester(flamearrester);
            }
        } else {
            flamearrester.setSubId(tPssrSafetyFlamearrester.getSubId());
            flamearrester.setApproveStatus(queryStatus);
            for (TPssrSafetyFlamearrester safetyFlamearrester : tPssrSafetyFlamearresterService.selectTPssrSafetyFlamearresterList(flamearrester)) {
                safetyFlamearrester.setConfirmationTime(new Date());
                safetyFlamearrester.setApproveStatus(approveStatus);
                safetyFlamearrester.setUpdatedate(new Date());
                safetyFlamearrester.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyFlamearresterService.updateTPssrSafetyFlamearrester(safetyFlamearrester);
            }
        }
        //查询当前待审批的确认人
        TPssrSafetyBleed entity = new TPssrSafetyBleed();
        entity.setSubId(tPssrSafetyFlamearrester.getSubId());
        entity.setApproveStatus(queryStatus);
        TPssrSafetyBleed safetyFlamearrester = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
        if (safetyFlamearrester != null) {
            //如果当前用户还有待审批任务
            if (tPssrSafetyFlamearrester.getTaskType() == 4 && StringUtils.isNotEmpty(safetyFlamearrester.getConfirmer1())) {
                if (safetyFlamearrester.getConfirmer1().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }

            if (tPssrSafetyFlamearrester.getTaskType() == 5 && StringUtils.isNotEmpty(safetyFlamearrester.getConfirmer2())) {
                if (safetyFlamearrester.getConfirmer2().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }
        }
        //无待审批任务结束当前用户流程

        // 因为流程关系所以approve一定会有且只有一条数据
        TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrSafetyFlamearrester.getSubId());
        TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
        return AjaxResult.success();
    }

    /**
     * 确认安全设施-安全阀
     */
    @PreAuthorize("@ss.hasPermi('pssr:safetyBleed:edit')")
    @Log(title = "安全设施-安全阀", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmValve")
    public AjaxResult confirmValve(@RequestBody TPssrSafetyValve tPssrSafetyValve) {
        long queryStatus = 0;
        long approveStatus = 0;
        Date date = null;
        TPssrSafetyValve valve = new TPssrSafetyValve();
        if (tPssrSafetyValve.getTaskType() == 4) {
            //确认人1确认
            queryStatus = 1;
            approveStatus = 3;
            valve.setConfirmer1(getUserId().toString());
            date = new Date();
        } else if (tPssrSafetyValve.getTaskType() == 5) {
            //确认人2确认
            queryStatus = 3;
            approveStatus = 2;
            valve.setConfirmer2(getUserId().toString());
            date = new Date();
        }
        if (tPssrSafetyValve.getIds() != null && tPssrSafetyValve.getIds().length > 0) {
            for (Long id : tPssrSafetyValve.getIds()) {
                valve = tPssrSafetyValveService.selectTPssrSafetyValveById(id);
                valve.setConfirmationTime(new Date());
                valve.setApproveStatus(approveStatus);
                valve.setUpdatedate(new Date());
                valve.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyValveService.updateTPssrSafetyValve(valve);
            }
        } else {
            valve.setSubId(tPssrSafetyValve.getSubId());
            valve.setApproveStatus(queryStatus);
            for (TPssrSafetyValve safetyValve : tPssrSafetyValveService.selectTPssrSafetyValveList(valve)) {
                safetyValve.setConfirmationTime(new Date());
                safetyValve.setApproveStatus(approveStatus);
                safetyValve.setUpdatedate(new Date());
                safetyValve.setUpdaterCode(String.valueOf(getUserId()));
                tPssrSafetyValveService.updateTPssrSafetyValve(safetyValve);
            }
        }
        //查询当前待审批的确认人
        TPssrSafetyBleed entity = new TPssrSafetyBleed();
        entity.setSubId(tPssrSafetyValve.getSubId());
        entity.setApproveStatus(queryStatus);
        TPssrSafetyBleed safetyBleed = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
        if (safetyBleed != null) {
            //如果当前用户还有待审批任务
            if (tPssrSafetyValve.getTaskType() == 4 && StringUtils.isNotEmpty(safetyBleed.getConfirmer1())) {
                if (safetyBleed.getConfirmer1().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }

            if (tPssrSafetyValve.getTaskType() == 5 && StringUtils.isNotEmpty(safetyBleed.getConfirmer2())) {
                if (safetyBleed.getConfirmer2().contains(getUserId().toString())) {
                    return AjaxResult.success();
                }
            }
        }
        //无待审批任务结束当前用户流程

        // 因为流程关系所以approve一定会有且只有一条数据
        TPssrApprove tPssrApprove = tPssrApproveService.selectTPssrApproveBySubId(tPssrSafetyValve.getSubId());
        TPssrApproveController.handleConfirmApprove(tPssrApprove, getUserId().toString());
        return AjaxResult.success();
    }

    /**
     * 驳回泄放阀
     */
    @PutMapping("/turnDownBleed")
    public AjaxResult turnDownBleed(@RequestBody List<TPssrSafetyBleed> tPssrSafetyBleed) {
        if (CollectionUtils.isNotEmpty(tPssrSafetyBleed)) {
            String userId = getUserId().toString();
            Long subId = tPssrSafetyBleed.get(0).getSubId();
            // 修改已选择数据的状态
            for (TPssrSafetyBleed item : tPssrSafetyBleed) {
                TPssrSafetyBleed blind = new TPssrSafetyBleed();
                blind.setId(item.getId());
                blind.setApproveStatus(1L);
                blind.setUpdatedate(new Date());
                blind.setUpdaterCode(getUserId().toString());
                tPssrSafetyBleedService.updateTPssrSafetyBleed(blind);
                // 新增驳回原因数据
                TPssrTurndown turndown = new TPssrTurndown();
                turndown.setForShort("aqss-bd");
                turndown.setSubId(item.getSubId());
                turndown.setItemId(item.getId());
                turndown.setReason(item.getReason());
                turndown.setCreatedate(new Date());
                turndown.setCreaterCode(getUserId().toString());
                tPssrTurndownService.insertTPssrTurndown(turndown);
            }
            // 查询当前流程
            TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);

            try {
                runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
                historyService.deleteHistoricProcessInstance(approve.getProcessId());
            } catch (Exception e) {
                logger.info("无运行时流程");
            }

            // 驳回 查询所有待审批的人员
            // 查询确认人
            TPssrSafetyBleed entity = new TPssrSafetyBleed();
            entity.setSubId(subId);
            entity.setApproveStatus(1L);
            TPssrSafetyBleed safety = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
            String confirmer1s = null;
            String confirmer2s = null;
            if (safety != null) {
                confirmer1s = safety.getConfirmer1();
                confirmer2s = safety.getConfirmer2();
            }
            logger.info("=======================confirmer1s:{}", confirmer1s);
            logger.info("=======================confirmer2s:{}", confirmer2s);

            Set<String> confirmUsers1 = new HashSet<>();
            Set<String> confirmUsers2 = new HashSet<>();
            if (StringUtils.isNotEmpty(confirmer1s)) {
                confirmUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
            }
            if (StringUtils.isNotEmpty(confirmer2s)) {
                confirmUsers2.addAll(Arrays.asList(confirmer2s.split(",")));
            }

            // 开始申请流程
            long businessKey = approve.getApproveId();
            //开始工作流、监听
            Authentication.setAuthenticatedUserId(userId);//设置当前申请人
            Map<String, Object> variables = new HashMap<>();
            variables.put("applyUser", userId);
            variables.put("confirmUsers1", new ArrayList<>(confirmUsers1));
            variables.put("confirmUsers2", new ArrayList<>(confirmUsers2));
            variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
            variables.put("chargePerson", approve.getSubCharge());
            //采用key来启动流程定义并设置流程变量，返回流程实例
            ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);

            // 修改审批表和sub表
            approve.setProcessId(pi.getProcessInstanceId());
            approve.setApproveStatus(1L);
            approve.setUpdatedate(new Date());
            approve.setUpdaterCode(getUserId().toString());
            tPssrApproveService.updateTPssrApprove(approve);

            TPssrSubcontent subcontent = new TPssrSubcontent();
            subcontent.setId(approve.getSubId());
            subcontent.setApproveStatus(1L);
            subcontent.setUpdatedate(new Date());
            subcontent.setUpdaterCode(getUserId().toString());
            tPssrSubcontentService.updateTPssrSubcontent(subcontent);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 驳回呼吸阀
     */
    @PutMapping("/turnDownBreath")
    public AjaxResult turnDownBreath(@RequestBody List<TPssrSafetyBreath> tPssrSafetyBreath) {
        if (CollectionUtils.isNotEmpty(tPssrSafetyBreath)) {
            String userId = getUserId().toString();
            Long subId = tPssrSafetyBreath.get(0).getSubId();
            // 修改已选择数据的状态
            for (TPssrSafetyBreath item : tPssrSafetyBreath) {
                TPssrSafetyBreath blind = new TPssrSafetyBreath();
                blind.setId(item.getId());
                blind.setApproveStatus(1L);
                blind.setUpdatedate(new Date());
                blind.setUpdaterCode(getUserId().toString());
                tPssrSafetyBreathService.updateTPssrSafetyBreath(blind);
                // 新增驳回原因数据
                TPssrTurndown turndown = new TPssrTurndown();
                turndown.setForShort("aqss-bh");
                turndown.setSubId(item.getSubId());
                turndown.setItemId(item.getId());
                turndown.setReason(item.getReason());
                turndown.setCreatedate(new Date());
                turndown.setCreaterCode(getUserId().toString());
                tPssrTurndownService.insertTPssrTurndown(turndown);
            }
            // 查询当前流程
            TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);

            try {
                runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
                historyService.deleteHistoricProcessInstance(approve.getProcessId());
            } catch (Exception e) {
                logger.info("无运行时流程");
            }

            // 驳回 查询所有待审批的人员
            // 查询确认人
            TPssrSafetyBleed entity = new TPssrSafetyBleed();
            entity.setSubId(approve.getSubId());
            entity.setApproveStatus(1L);
            TPssrSafetyBleed safety = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
            String confirmer1s = null;
            String confirmer2s = null;
            if (safety != null) {
                confirmer1s = safety.getConfirmer1();
                confirmer2s = safety.getConfirmer2();
            }
            logger.info("=======================confirmer1s:{}", confirmer1s);
            logger.info("=======================confirmer2s:{}", confirmer2s);

            Set<String> confirmUsers1 = new HashSet<>();
            Set<String> confirmUsers2 = new HashSet<>();
            if (StringUtils.isNotEmpty(confirmer1s)) {
                confirmUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
            }
            if (StringUtils.isNotEmpty(confirmer2s)) {
                confirmUsers2.addAll(Arrays.asList(confirmer2s.split(",")));
            }

            // 开始申请流程
            long businessKey = approve.getApproveId();
            //开始工作流、监听
            Authentication.setAuthenticatedUserId(userId);//设置当前申请人
            Map<String, Object> variables = new HashMap<>();
            variables.put("applyUser", userId);
            variables.put("confirmUsers1", new ArrayList<>(confirmUsers1));
            variables.put("confirmUsers2", new ArrayList<>(confirmUsers2));
            variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
            variables.put("chargePerson", approve.getSubCharge());
            //采用key来启动流程定义并设置流程变量，返回流程实例
            ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);

            // 修改审批表和sub表
            approve.setProcessId(pi.getProcessInstanceId());
            approve.setApproveStatus(1L);
            approve.setUpdatedate(new Date());
            approve.setUpdaterCode(getUserId().toString());
            tPssrApproveService.updateTPssrApprove(approve);

            TPssrSubcontent subcontent = new TPssrSubcontent();
            subcontent.setId(approve.getSubId());
            subcontent.setApproveStatus(1L);
            subcontent.setUpdatedate(new Date());
            subcontent.setUpdaterCode(getUserId().toString());
            tPssrSubcontentService.updateTPssrSubcontent(subcontent);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 驳回爆破片
     */
    @PutMapping("/turnDownBrust")
    public AjaxResult turnDownBrust(@RequestBody List<TPssrSafetyBrust> tPssrSafetyBrust) {
        if (CollectionUtils.isNotEmpty(tPssrSafetyBrust)) {
            String userId = getUserId().toString();
            Long subId = tPssrSafetyBrust.get(0).getSubId();
            // 修改已选择数据的状态
            for (TPssrSafetyBrust item : tPssrSafetyBrust) {
                TPssrSafetyBrust blind = new TPssrSafetyBrust();
                blind.setId(item.getId());
                blind.setApproveStatus(1L);
                blind.setUpdatedate(new Date());
                blind.setUpdaterCode(getUserId().toString());
                tPssrSafetyBrustService.updateTPssrSafetyBrust(blind);
                // 新增驳回原因数据
                TPssrTurndown turndown = new TPssrTurndown();
                turndown.setForShort("aqss-bt");
                turndown.setSubId(item.getSubId());
                turndown.setItemId(item.getId());
                turndown.setReason(item.getReason());
                turndown.setCreatedate(new Date());
                turndown.setCreaterCode(getUserId().toString());
                tPssrTurndownService.insertTPssrTurndown(turndown);
            }
            // 查询当前流程
            TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);

            try {
                runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
                historyService.deleteHistoricProcessInstance(approve.getProcessId());
            } catch (Exception e) {
                logger.info("无运行时流程");
            }

            // 驳回 查询所有待审批的人员
            // 查询确认人
            TPssrSafetyBleed entity = new TPssrSafetyBleed();
            entity.setSubId(approve.getSubId());
            entity.setApproveStatus(1L);
            TPssrSafetyBleed safety = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
            String confirmer1s = null;
            String confirmer2s = null;
            if (safety != null) {
                confirmer1s = safety.getConfirmer1();
                confirmer2s = safety.getConfirmer2();
            }
            logger.info("=======================confirmer1s:{}", confirmer1s);
            logger.info("=======================confirmer2s:{}", confirmer2s);

            Set<String> confirmUsers1 = new HashSet<>();
            Set<String> confirmUsers2 = new HashSet<>();
            if (StringUtils.isNotEmpty(confirmer1s)) {
                confirmUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
            }
            if (StringUtils.isNotEmpty(confirmer2s)) {
                confirmUsers2.addAll(Arrays.asList(confirmer2s.split(",")));
            }

            // 开始申请流程
            long businessKey = approve.getApproveId();
            //开始工作流、监听
            Authentication.setAuthenticatedUserId(userId);//设置当前申请人
            Map<String, Object> variables = new HashMap<>();
            variables.put("applyUser", userId);
            variables.put("confirmUsers1", new ArrayList<>(confirmUsers1));
            variables.put("confirmUsers2", new ArrayList<>(confirmUsers2));
            variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
            variables.put("chargePerson", approve.getSubCharge());
            //采用key来启动流程定义并设置流程变量，返回流程实例
            ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);

            // 修改审批表和sub表
            approve.setProcessId(pi.getProcessInstanceId());
            approve.setApproveStatus(1L);
            approve.setUpdatedate(new Date());
            approve.setUpdaterCode(getUserId().toString());
            tPssrApproveService.updateTPssrApprove(approve);

            TPssrSubcontent subcontent = new TPssrSubcontent();
            subcontent.setId(approve.getSubId());
            subcontent.setApproveStatus(1L);
            subcontent.setUpdatedate(new Date());
            subcontent.setUpdaterCode(getUserId().toString());
            tPssrSubcontentService.updateTPssrSubcontent(subcontent);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 驳回阻火器
     */
    @PutMapping("/turnDownFlamearrester")
    public AjaxResult turnDownFlamearrester(@RequestBody List<TPssrSafetyFlamearrester> tPssrSafetyFlamearrester) {
        if (CollectionUtils.isNotEmpty(tPssrSafetyFlamearrester)) {
            String userId = getUserId().toString();
            Long subId = tPssrSafetyFlamearrester.get(0).getSubId();
            // 修改已选择数据的状态
            for (TPssrSafetyFlamearrester item : tPssrSafetyFlamearrester) {
                TPssrSafetyFlamearrester blind = new TPssrSafetyFlamearrester();
                blind.setId(item.getId());
                blind.setApproveStatus(1L);
                blind.setUpdatedate(new Date());
                blind.setUpdaterCode(getUserId().toString());
                tPssrSafetyFlamearresterService.updateTPssrSafetyFlamearrester(blind);
                // 新增驳回原因数据
                TPssrTurndown turndown = new TPssrTurndown();
                turndown.setForShort("aqss-f");
                turndown.setSubId(item.getSubId());
                turndown.setItemId(item.getId());
                turndown.setReason(item.getReason());
                turndown.setCreatedate(new Date());
                turndown.setCreaterCode(getUserId().toString());
                tPssrTurndownService.insertTPssrTurndown(turndown);
            }
            // 查询当前流程
            TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);

            try {
                runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
                historyService.deleteHistoricProcessInstance(approve.getProcessId());
            } catch (Exception e) {
                logger.info("无运行时流程");
            }

            // 驳回 查询所有待审批的人员
            // 查询确认人
            TPssrSafetyBleed entity = new TPssrSafetyBleed();
            entity.setSubId(approve.getSubId());
            entity.setApproveStatus(1L);
            TPssrSafetyBleed safety = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
            String confirmer1s = null;
            String confirmer2s = null;
            if (safety != null) {
                confirmer1s = safety.getConfirmer1();
                confirmer2s = safety.getConfirmer2();
            }
            logger.info("=======================confirmer1s:{}", confirmer1s);
            logger.info("=======================confirmer2s:{}", confirmer2s);

            Set<String> confirmUsers1 = new HashSet<>();
            Set<String> confirmUsers2 = new HashSet<>();
            if (StringUtils.isNotEmpty(confirmer1s)) {
                confirmUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
            }
            if (StringUtils.isNotEmpty(confirmer2s)) {
                confirmUsers2.addAll(Arrays.asList(confirmer2s.split(",")));
            }

            // 开始申请流程
            long businessKey = approve.getApproveId();
            //开始工作流、监听
            Authentication.setAuthenticatedUserId(userId);//设置当前申请人
            Map<String, Object> variables = new HashMap<>();
            variables.put("applyUser", userId);
            variables.put("confirmUsers1", new ArrayList<>(confirmUsers1));
            variables.put("confirmUsers2", new ArrayList<>(confirmUsers2));
            variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
            variables.put("chargePerson", approve.getSubCharge());
            //采用key来启动流程定义并设置流程变量，返回流程实例
            ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);

            // 修改审批表和sub表
            approve.setProcessId(pi.getProcessInstanceId());
            approve.setApproveStatus(1L);
            approve.setUpdatedate(new Date());
            approve.setUpdaterCode(getUserId().toString());
            tPssrApproveService.updateTPssrApprove(approve);

            TPssrSubcontent subcontent = new TPssrSubcontent();
            subcontent.setId(approve.getSubId());
            subcontent.setApproveStatus(1L);
            subcontent.setUpdatedate(new Date());
            subcontent.setUpdaterCode(getUserId().toString());
            tPssrSubcontentService.updateTPssrSubcontent(subcontent);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 驳回安全阀
     */
    @PutMapping("/turnDownValve")
    public AjaxResult turnDownValve(@RequestBody List<TPssrSafetyValve> tPssrSafetyValve) {
        if (CollectionUtils.isNotEmpty(tPssrSafetyValve)) {
            String userId = getUserId().toString();
            Long subId = tPssrSafetyValve.get(0).getSubId();
            // 修改已选择数据的状态
            for (TPssrSafetyValve item : tPssrSafetyValve) {
                TPssrSafetyValve blind = new TPssrSafetyValve();
                blind.setId(item.getId());
                blind.setApproveStatus(1L);
                blind.setUpdatedate(new Date());
                blind.setUpdaterCode(getUserId().toString());
                tPssrSafetyValveService.updateTPssrSafetyValve(blind);
                // 新增驳回原因数据
                TPssrTurndown turndown = new TPssrTurndown();
                turndown.setForShort("aqss-v");
                turndown.setSubId(item.getSubId());
                turndown.setItemId(item.getId());
                turndown.setReason(item.getReason());
                turndown.setCreatedate(new Date());
                turndown.setCreaterCode(getUserId().toString());
                tPssrTurndownService.insertTPssrTurndown(turndown);
            }
            // 查询当前流程
            TPssrApprove approve = tPssrApproveService.selectTPssrApproveBySubId(subId);

            try {
                runtimeService.deleteProcessInstance(approve.getProcessId(), "pssr2confirm");
                historyService.deleteHistoricProcessInstance(approve.getProcessId());
            } catch (Exception e) {
                logger.info("无运行时流程");
            }

            // 驳回 查询所有待审批的人员
            // 查询确认人
            TPssrSafetyBleed entity = new TPssrSafetyBleed();
            entity.setSubId(approve.getSubId());
            entity.setApproveStatus(1L);
            TPssrSafetyBleed safety = tPssrSafetyBleedService.selectAllConfirmedPersonBySubId(entity);
            String confirmer1s = null;
            String confirmer2s = null;
            if (safety != null) {
                confirmer1s = safety.getConfirmer1();
                confirmer2s = safety.getConfirmer2();
            }
            logger.info("=======================confirmer1s:{}", confirmer1s);
            logger.info("=======================confirmer2s:{}", confirmer2s);

            Set<String> confirmUsers1 = new HashSet<>();
            Set<String> confirmUsers2 = new HashSet<>();
            if (StringUtils.isNotEmpty(confirmer1s)) {
                confirmUsers1.addAll(Arrays.asList(confirmer1s.split(",")));
            }
            if (StringUtils.isNotEmpty(confirmer2s)) {
                confirmUsers2.addAll(Arrays.asList(confirmer2s.split(",")));
            }

            // 开始申请流程
            long businessKey = approve.getApproveId();
            //开始工作流、监听
            Authentication.setAuthenticatedUserId(userId);//设置当前申请人
            Map<String, Object> variables = new HashMap<>();
            variables.put("applyUser", userId);
            variables.put("confirmUsers1", new ArrayList<>(confirmUsers1));
            variables.put("confirmUsers2", new ArrayList<>(confirmUsers2));
            variables.put("confirmTaskCreateListener", new ConfirmTaskCreateListener());//发送邮件
            variables.put("chargePerson", approve.getSubCharge());
            //采用key来启动流程定义并设置流程变量，返回流程实例
            ProcessInstance pi = runtimeService.startProcessInstanceByKey("pssr2confirm", String.valueOf(businessKey), variables);

            // 修改审批表和sub表
            approve.setProcessId(pi.getProcessInstanceId());
            approve.setApproveStatus(1L);
            approve.setUpdatedate(new Date());
            approve.setUpdaterCode(getUserId().toString());
            tPssrApproveService.updateTPssrApprove(approve);

            TPssrSubcontent subcontent = new TPssrSubcontent();
            subcontent.setId(approve.getSubId());
            subcontent.setApproveStatus(1L);
            subcontent.setUpdatedate(new Date());
            subcontent.setUpdaterCode(getUserId().toString());
            tPssrSubcontentService.updateTPssrSubcontent(subcontent);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
