package com.jpsoft.shinestar.modules.workflow.service.impl;

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

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.business.dto.UpdatePersonAttendanceDTO;
import com.jpsoft.shinestar.modules.business.entity.*;
import com.jpsoft.shinestar.modules.business.service.*;
import com.jpsoft.shinestar.modules.common.utils.JwtUtil2;
import com.jpsoft.shinestar.modules.common.utils.TypeNameUtil;
import com.jpsoft.shinestar.modules.common.utils.WechatMessageUtil;
import com.jpsoft.shinestar.modules.workflow.entity.*;
import com.jpsoft.shinestar.modules.workflow.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.workflow.dao.ProcinstActUserDAO;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;
import springfox.documentation.schema.TypeNameExtractor;

@Slf4j
@Transactional(rollbackFor = Exception.class)
@Component(value = "procinstActUserService")
public class ProcinstActUserServiceImpl implements ProcinstActUserService {
    @Resource(name = "procinstActUserDAO")
    private ProcinstActUserDAO procinstActUserDAO;

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Autowired
    private AdjustShiftService adjustShiftService;

    @Autowired
    private RequestForLeaveInfoService requestForLeaveInfoService;

    @Autowired
    private YearRequestForLeaveInfoService yearRequestForLeaveInfoService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private BusinessOutService businessOutService;

    @Autowired
    private WorkOverPersonService workOverPersonService;

    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private CarryThingsOutFactoryService carryThingsOutFactoryService;

    @Autowired
    private WorkScheduleAttendanceService workScheduleAttendanceService;

    @Autowired
    private WorkPersonSchedulingService workPersonSchedulingService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private WorkOverService workOverService;

    @Autowired
    private ProcessTemplateService processTemplateService;

    @Autowired
    private ProcinstService procinstService;

    @Autowired
    private CustomerDiningRecordService customerDiningRecordService;

    @Autowired
    private ProcessSetService processSetService;

    @Autowired
    private SupplementWorkService supplementWorkService;

    @Autowired
    private SupplementWorkPersonService supplementWorkPersonService;


    @Autowired
    private AdjustShiftPersonService adjustShiftPersonService;

    @Autowired
    private ProcessActivityService processActivityService;

    @Autowired
    private ProcinstActivityService procinstActivityService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ProcinstActUser get(String id) {
        // TODO Auto-generated method stub
        return procinstActUserDAO.get(id);
    }

    @Override
    public int insert(ProcinstActUser model) {
        // TODO Auto-generated method stub
        //model.setId(UUID.randomUUID().toString());

        return procinstActUserDAO.insert(model);
    }

    @Override
    public int update(ProcinstActUser model) {
        // TODO Auto-generated method stub
        return procinstActUserDAO.update(model);
    }

    @Override
    public int delete(String id) {
        // TODO Auto-generated method stub
        return procinstActUserDAO.delete(id);
    }

    @Override
    public boolean exist(String id) {
        // TODO Auto-generated method stub
        int count = procinstActUserDAO.exist(id);

        return count > 0;
    }

    @Override
    public List<ProcinstActUser> list() {
        // TODO Auto-generated method stub
        return procinstActUserDAO.list();
    }

    @Override
    public Page<ProcinstActUser> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize, boolean count, List<Sort> sortList) {
        Page<ProcinstActUser> page = PageHelper.startPage(pageNumber, pageSize, count).doSelectPage(() -> {
            procinstActUserDAO.search(searchParams, sortList);
        });

        return page;
    }

    @Override
    public List<ProcinstActUser> findByProcinstActivityId(String procinstActivityId) {
        return procinstActUserDAO.findByProcinstActivityId(procinstActivityId);
    }

    @Override
    public void examineSubmit(String procinstActUserIds, PersonPopedom personPopedom, String status, String content) throws Exception {
        List<String> idList = Arrays.asList(procinstActUserIds.split(","));

        if (idList.size() > 0) {
            for (String id : idList) {
                ProcinstActUser procinstActUser = get(id);

                log.warn("actUser={},action={}", procinstActUser.getId(), procinstActUser.getAction());

                if (procinstActUser != null && "0".equals(procinstActUser.getAction())) {
                    ProcinstActivity procinstActivity = procinstActivityService.get(procinstActUser.getProcinstActivityId());

                    ProcessActivity processActivity = processActivityService.get(procinstActivity.getActivityId());

                    Procinst procinst = procinstService.get(procinstActivity.getProcinstId());
                    ProcessTemplate processTemplate = processTemplateService.get(processActivity.getProcessTemplateId());
                    ProcessSet processSet = processSetService.get(processTemplate.getProcessSetId());

                    log.warn("procInstId={},type={}", procinst.getId(), procinst.getType());

                    String businessStatus = null;
                    AdjustShift adjustShift = null;

                    //调班
                    if ("1".equals(procinst.getType())) {
                        adjustShift = adjustShiftService.get(procinst.getBusinessKey());
                        businessStatus = adjustShift.getStatus();
                    }
                    //请假
                    RequestForLeaveInfo requestForLeaveInfo = null;
                    if ("2".equals(procinst.getType())) {
                        requestForLeaveInfo = requestForLeaveInfoService.get(procinst.getBusinessKey());
                        businessStatus = String.valueOf(requestForLeaveInfo.getStatus());

                    }
                    //出差
                    BusinessOut businessOut = null;
                    if ("3".equals(procinst.getType())) {
                        businessOut = businessOutService.get(procinst.getBusinessKey());
                        businessStatus = businessOut.getStatus();
                    }
                    //补卡，批量补卡
                    SupplementWork supplementWork = null;
                    if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                        supplementWork = supplementWorkService.get(procinst.getBusinessKey());
                        businessStatus = supplementWork.getStatus();
                    }
                    //加班，批量加班
                    WorkOver workOver = null;
                    if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                        workOver = workOverService.get(procinst.getBusinessKey());
                        businessStatus = workOver.getStatus();
                    }
                    //携物出厂
                    CarryThingsOutFactory carryThingsOutFactory = null;
                    if ("8".equals(procinst.getType())) {
                        carryThingsOutFactory = carryThingsOutFactoryService.get(procinst.getBusinessKey());
                        businessStatus = carryThingsOutFactory.getAuditStatus();
                    }
                    //接待就餐
                    CustomerDiningRecord customerDiningRecord = null;
                    if ("9".equals(procinst.getType())) {
                        customerDiningRecord = customerDiningRecordService.get(procinst.getBusinessKey());
                        businessStatus = customerDiningRecord.getStatus();
                    }
                    //年假
                    YearRequestForLeaveInfo yearRequestForLeaveInfo = null;
                    if ("11".equals(procinst.getType())) {
                        yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());
                        businessStatus = String.valueOf(yearRequestForLeaveInfo.getStatus());

                    }

                    if (!"0".equals(businessStatus)) {
                        throw new Exception("业务非待审核状态");
                    }

                    //查询此步骤还有多少人未审核
                    List<ProcinstActUser> procinstActUserList = findByProcinstActivityIdActionType(procinstActUser.getProcinstActivityId(), "0", "1");

                    log.warn("ProcInstActivity={},查询此步骤还有多少人未审核:{},operation={}",
                            procinstActUser.getProcinstActivityId(), procinstActUserList.size(), processActivity.getOperation());

                    //如果未审核人的数量大于1个，则查询此步骤是或签还是会签
                    if (procinstActUserList.size() > 1) {
                        //或签
                        if ("or".equals(processActivity.getOperation())) {
                            if ("1".equals(status)) {
                                log.warn("审核同意，删除此步骤的其他或签，在判断是否还有下个步骤来修改此申请的审核状态");

                                procinstActUserList.remove(procinstActUser);
                                procinstActUser.setAction(status);
                                procinstActUser.setRemark(content);
                                procinstActUser.setFinishTime(new Date());
                                update(procinstActUser);

                                //删除其他的或签
                                for (ProcinstActUser procinstActUser1 : procinstActUserList) {
                                    procinstActUser1.setDelFlag(true);
                                    procinstActUser1.setUpdateTime(new Date());
                                    update(procinstActUser1);
                                }

                                //此步骤审核状态修改
                                procinstActivity.setAction(status);
                                procinstActivity.setStatus("0");
                                procinstActivity.setRemark("同意");
                                procinstActivity.setFinishTime(new Date());
                                procinstActivity.setUpdateTime(new Date());
                                procinstActivityService.update(procinstActivity);

                                //查询是否有下个步骤
                                int activityIndex = procinstActivity.getActivityIndex() + 1;
                                ProcinstActivity procinstActivity1 = procinstActivityService.findByActivityIndexAndProcinstId(activityIndex, procinstActivity.getProcinstId());

                                if (procinstActivity1 == null) {
                                    log.warn("没有下个步骤则整个流程完毕");

                                    procinstActivity.setStatus("1");
                                    procinstActivityService.update(procinstActivity);

                                    //调班
                                    if ("1".equals(procinst.getType())) {

                                        if (adjustShift != null) {
                                            adjustShift.setStatus(status);
                                            adjustShift.setRemark("同意");
                                            adjustShift.setUpdateTime(new Date());
                                            adjustShiftService.update(adjustShift);

                                            //调整班次
                                            List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(adjustShift.getId());
                                            if (adjustShiftPersonList.size() > 0) {
                                                for (AdjustShiftPerson adjustShiftPerson : adjustShiftPersonList) {
                                                    WorkPersonScheduling workPersonScheduling = workPersonSchedulingService.get(adjustShiftPerson.getSchedulingId());
                                                    workPersonScheduling.setShiftIds(adjustShiftPerson.getNewShiftId());
                                                    workPersonScheduling.setShiftNumbers(adjustShiftPerson.getNewShiftNumber());
                                                    workPersonSchedulingService.update(workPersonScheduling);


                                                }
                                            }
                                        }
                                    }
                                    //请假
                                    if ("2".equals(procinst.getType())) {
                                        //	RequestForLeaveInfo requestForLeaveInfo = requestForLeaveInfoService.get(procinst.getBusinessKey());

                                        //请假审核
                                        if (requestForLeaveInfo.getStatus() == 0) {
                                            requestForLeaveInfo.setStatus(1);
                                            double days = 0;
                                            if (StringUtils.isNotEmpty(requestForLeaveInfo.getDays())) {
                                                days = Double.parseDouble(requestForLeaveInfo.getDays());
                                            }

                                            if (days >= 4) {
                                                requestForLeaveInfo.setEnableCancel(true);
                                            } else {
                                                if ("2".equals(requestForLeaveInfo.getLeaveType()) || "5".equals(requestForLeaveInfo.getLeaveType()) || "6".equals(requestForLeaveInfo.getLeaveType())) {
                                                    requestForLeaveInfo.setEnableCancel(true);
                                                }
                                            }

                                           /* if (("1".equals(requestForLeaveInfo.getLeaveType())&&days>=4) ||"2".equals(requestForLeaveInfo.getLeaveType()) || "5".equals(requestForLeaveInfo.getLeaveType()) || "6".equals(requestForLeaveInfo.getLeaveType())){
                                                requestForLeaveInfo.setEnableCancel(true);
                                            }*/

                                        } else {
                                            //销假审核
                                            if ("0".equals(requestForLeaveInfo.getCancelStatus())) {
                                                requestForLeaveInfo.setCancelStatus(status);
                                                requestForLeaveInfo.setCancelTime(new Date());
                                                requestForLeaveInfo.setEnableCancel(false);

                                            }
                                        }

                                        requestForLeaveInfo.setUpdateTime(new Date());
                                        requestForLeaveInfoService.update(requestForLeaveInfo);

                                    }
                                    //出差
                                    if ("3".equals(procinst.getType())) {
                                        businessOut.setStatus(status);
                                        businessOut.setUpdateTime(new Date());
                                        businessOutService.update(businessOut);
                                    }

                                    //补卡
                                    if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                        supplement(status, supplementWork);
                                    }

                                    //批量加班
                                    if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                        workOver.setStatus(status);
                                        workOver.setUpdateTime(new Date());
                                        workOver.setApprovalTime(new Date());

                                        workOverService.update(workOver);

                                    }
                                    //携物出厂
                                    if ("8".equals(procinst.getType())) {
                                        carryThingsOutFactory.setAuditStatus(status);
                                        carryThingsOutFactory.setUpdateTime(new Date());
                                        carryThingsOutFactoryService.update(carryThingsOutFactory);
                                    }

                                    //接口就餐
                                    if ("9".equals(procinst.getType())) {
                                        customerDiningRecord.setStatus(status);
                                        customerDiningRecord.setUpdateTime(new Date());
                                        customerDiningRecordService.update(customerDiningRecord);
                                    }

                                    //年假
                                    if ("11".equals(procinst.getType())) {
                                        yearRequestForLeaveInfo.setStatus(Integer.parseInt(status));
                                        yearRequestForLeaveInfo.setUpdateTime(new Date());
                                        yearRequestForLeaveInfoService.update(yearRequestForLeaveInfo);
                                    }

                                } else {
                                    log.warn("有下个步骤，修改下个步骤的状态");
                                    procinstActivity1.setStatus("1");
                                    procinstActivityService.update(procinstActivity1);

                                    log.warn("通知下个步骤的审核人,nextActivityId={}", procinstActivity1.getId());
                                    List<ProcinstActUser> procinstActUserList1 = findByProcinstActivityIdActionType(procinstActivity1.getId(), "0", "1");

                                    if (procinstActUserList1.size() > 0) {
                                        new Thread(() -> {
                                            for (ProcinstActUser procinstActUser1 : procinstActUserList1) {
                                                try {
                                                    log.warn("通知下个步骤的审核人,personId={}", procinstActUser1.getUserId());

                                                    PersonInfo personInfo = personInfoService.get(Long.parseLong(procinstActUser1.getUserId()));
                                                    String accessToken = JwtUtil2.createToken(jwtSecret, personInfo.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                                    String typeName = "";

                                                    if ("1".equals(procinst.getType())) {
                                                        typeName = "调班";
                                                    }
                                                    if ("2".equals(procinst.getType())) {
                                                        RequestForLeaveInfo requestForLeaveInfo1 = requestForLeaveInfoService.get(procinst.getBusinessKey());
                                                        typeName = "请假";
                                                        if (requestForLeaveInfo1.getStatus() == 1 && "0".equals(requestForLeaveInfo1.getCancelStatus())) {
                                                            typeName = "销假";
                                                        }

                                                    }
                                                    if ("3".equals(procinst.getType())) {
                                                        typeName = "出差";
                                                    }
                                                    if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                                        typeName = "补卡";
                                                    }
                                                    if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                                        typeName = "加班";
                                                    }
                                                    if ("8".equals(procinst.getType())) {
                                                        typeName = "携物出厂";
                                                    }
                                                    if ("9".equals(procinst.getType())) {
                                                        typeName = "接待就餐";
                                                    }
                                                    if ("11".equals(procinst.getType())) {
                                                        typeName = "年假";
                                                    }
                                                    WechatMessageUtil.sendEvenMessage(personInfo.getOpenId(), "有一条新的" + typeName + "信息待审核", personInfo.getName(), com.jpsoft.shinestar.modules.common.utils.StringUtils.getApprovalNo(), "前往处理", wxConfig.getPortalUrl() + "xsy/approval/infosp?id=" + procinst.getBusinessKey() + "&procinstActUserId=" + procinstActUser1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                                } catch (Exception ex) {
                                                    ex.printStackTrace();
                                                }
                                            }
                                        }).start();
                                    }
                                }

                                //查询此步骤还有多少人未抄送
                                List<ProcinstActUser> ccProcinstActUserList = findByProcinstActivityIdActionType(procinstActUser.getProcinstActivityId(), "0", "2");
                                if (ccProcinstActUserList.size() > 0) {
                                    //TODO 抄送发送模板消息
                                    new Thread(() -> {
                                        for (ProcinstActUser ccProcinstActUer : ccProcinstActUserList) {
                                            PersonInfo personInfo = personInfoService.get(Long.parseLong(ccProcinstActUer.getUserId()));
                                            String accessToken = JwtUtil2.createToken(jwtSecret, personInfo.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                            //发起人
                                            PersonInfo personInfo1 = null;
                                            String typeName = "";
                                            if ("1".equals(procinst.getType())) {
                                                typeName = "调班";
                                                AdjustShift adjustShift1 = adjustShiftService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(adjustShift1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                            }
                                            if ("2".equals(procinst.getType())) {
                                                RequestForLeaveInfo requestForLeaveInfo1 = requestForLeaveInfoService.get(procinst.getBusinessKey());
                                                typeName = "请假";
                                                if (requestForLeaveInfo1.getStatus() == 1 && "0".equals(requestForLeaveInfo1.getCancelStatus())) {
                                                    typeName = "销假";
                                                }
                                                PersonPopedom personPopedom1 = personPopedomService.get(requestForLeaveInfo1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                            }
                                            if ("3".equals(procinst.getType())) {
                                                typeName = "出差";
                                                BusinessOut businessOut1 = businessOutService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(businessOut1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                            }
                                            if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                                typeName = "补卡";
                                                SupplementWork supplementWork1 = supplementWorkService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(supplementWork1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());


                                            }
                                            if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                                typeName = "加班";
                                                WorkOver workOver1 = workOverService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(workOver1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                            }
                                            if ("8".equals(procinst.getType())) {
                                                typeName = "携物出厂";
                                                CarryThingsOutFactory carryThingsOutFactory1 = carryThingsOutFactoryService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(carryThingsOutFactory1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                            }
                                            if ("9".equals(procinst.getType())) {
                                                typeName = "接待就餐";
                                                CustomerDiningRecord customerDiningRecord1 = customerDiningRecordService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(customerDiningRecord1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                            }
                                            if ("11".equals(procinst.getType())) {
                                                typeName = "年假";
                                                YearRequestForLeaveInfo yearRequestForLeaveInfo1 = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());
                                                PersonPopedom personPopedom1 = personPopedomService.get(yearRequestForLeaveInfo1.getPersonPopedomId());
                                                personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                            }
                                            WechatMessageUtil.sendCCRemindMessage(personInfo.getOpenId(), "[抄送]" + typeName + "审核通过", personInfo1.getName(), typeName, typeName + "审核", DateUtil.format(procinst.getCreateTime(), "yyyy-MM-dd HH:mm:ss"), "已抄送", wxConfig.getPortalUrl() + "xsy/approval/infosp?id=" + procinst.getBusinessKey() + "&procinstActUserId=" + ccProcinstActUer.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                        }
                                    }).start();

                                }
                            } else if ("3".equals(status)) {
                                log.warn("审核拒绝,删除其他的或签,删除此申请流程其他未处理的待审核记录");
                                procinstActUserList.remove(procinstActUser);
                                procinstActUser.setAction(status);
                                procinstActUser.setRemark(content);
                                procinstActUser.setFinishTime(new Date());
                                update(procinstActUser);

                                //删除其他的或签
                                for (ProcinstActUser procinstActUser1 : procinstActUserList) {
                                    procinstActUser1.setDelFlag(true);
                                    procinstActUser1.setUpdateTime(new Date());
                                    update(procinstActUser1);
                                }

                                //此步骤审核状态修改
                                procinstActivity.setAction(status);
                                procinstActivity.setRemark("拒绝");
                                procinstActivity.setFinishTime(new Date());
                                procinstActivity.setUpdateTime(new Date());
                                procinstActivityService.update(procinstActivity);

                                //此申请流程其他未处理的待审核记录
                                List<ProcinstActivity> noExamineList = procinstActivityService.findProcinstIdAndAction(procinstActivity.getProcinstId(), "0");
                                if (noExamineList.size() > 0) {
                                    for (ProcinstActivity procinstActivity1 : noExamineList) {
                                        procinstActivity1.setDelFlag(true);
                                        procinstActivity1.setUpdateTime(new Date());
                                        procinstActivityService.update(procinstActivity1);
                                    }
                                }

                                if ("1".equals(procinst.getType())) {
                                    adjustShift.setStatus(status);
                                    adjustShift.setRemark("拒绝");
                                    adjustShift.setUpdateTime(new Date());
                                    adjustShiftService.update(adjustShift);
                                }

                                //请假
                                if ("2".equals(procinst.getType())) {
                                    //请假审核
                                    if (requestForLeaveInfo.getStatus() == 0) {
                                        requestForLeaveInfo.setStatus(Integer.parseInt(status));
                                    } else {
                                        //销假审核
                                        if ("0".equals(requestForLeaveInfo.getCancelStatus())) {
                                            requestForLeaveInfo.setCancelStatus(status);
                                            requestForLeaveInfo.setCancelTime(new Date());
                                            requestForLeaveInfo.setEnableCancel(true);
                                        }
                                    }
                                    /*requestForLeaveInfo.setStatus(Integer.parseInt(status));*/
                                    requestForLeaveInfo.setUpdateTime(new Date());
                                    requestForLeaveInfoService.update(requestForLeaveInfo);
                                }

                                //出差
                                if ("3".equals(procinst.getType())) {
                                    businessOut.setStatus(status);
                                    businessOut.setUpdateTime(new Date());
                                    businessOutService.update(businessOut);
                                }

                                //补卡
                                if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                    supplementWork.setStatus(status);
                                    supplementWork.setUpdateTime(new Date());
                                    supplementWorkService.update(supplementWork);
                                }

                                //批量加班
                                if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                    workOver.setStatus(status);
                                    workOver.setUpdateTime(new Date());
                                    workOver.setApprovalTime(new Date());
                                    workOverService.update(workOver);
                                }

                                //携物出厂
                                if ("8".equals(procinst.getType())) {
                                    carryThingsOutFactory.setAuditStatus(status);
                                    carryThingsOutFactory.setUpdateTime(new Date());
                                    carryThingsOutFactoryService.update(carryThingsOutFactory);
                                }

                                //接待就餐
                                if ("9".equals(procinst.getType())) {
                                    customerDiningRecord.setStatus(status);
                                    customerDiningRecord.setUpdateTime(new Date());
                                    customerDiningRecordService.update(customerDiningRecord);
                                }

                                //年假
                                if ("11".equals(procinst.getType())) {
                                    if (yearRequestForLeaveInfo.getStatus() == 0) {
                                        yearRequestForLeaveInfo.setStatus(Integer.parseInt(status));
                                    }
                                    yearRequestForLeaveInfo.setUpdateTime(new Date());
                                    yearRequestForLeaveInfoService.update(yearRequestForLeaveInfo);
                                }
                            }
                        }

                        //会签
                        if ("and".equals(processActivity.getOperation())) {
                            procinstActUser.setAction(status);
                            procinstActUser.setRemark(content);
                            procinstActUser.setFinishTime(new Date());
                            update(procinstActUser);

                            //审核拒绝
                            if ("3".equals(status)) {
                                log.warn("会签审核拒绝,actUserId={}", procinstActUser.getId());

                                for (ProcinstActUser procinstActUser1 : procinstActUserList) {
                                    procinstActUser1.setDelFlag(true);
                                    procinstActUser1.setUpdateTime(new Date());
                                    update(procinstActUser1);
                                }

                                log.warn("删除此申请流程其他未处理的待审核记录");

                                List<ProcinstActivity> noExamineList = procinstActivityService.findProcinstIdAndAction(procinstActivity.getProcinstId(), "0");
                                if (noExamineList.size() > 0) {
                                    for (ProcinstActivity procinstActivity1 : noExamineList) {
                                        procinstActivity1.setDelFlag(true);
                                        procinstActivity1.setUpdateTime(new Date());
                                        procinstActivityService.update(procinstActivity1);
                                    }
                                }

                                //此步骤审核状态修改
                                procinstActivity.setAction(status);
                                procinstActivity.setRemark("拒绝");
                                procinstActivity.setFinishTime(new Date());
                                procinstActivity.setUpdateTime(new Date());
                                procinstActivityService.update(procinstActivity);

                                if ("1".equals(procinst.getType())) {
                                    adjustShift.setStatus(status);
                                    adjustShift.setRemark("拒绝");
                                    adjustShift.setUpdateTime(new Date());
                                    adjustShiftService.update(adjustShift);
                                }
                                if ("2".equals(procinst.getType())) {
                                    //请假审核
                                    if (requestForLeaveInfo.getStatus() == 0) {
                                        requestForLeaveInfo.setStatus(Integer.parseInt(status));
                                    } else {
                                        //销假审核
                                        if ("0".equals(requestForLeaveInfo.getCancelStatus())) {
                                            requestForLeaveInfo.setCancelStatus(status);
                                            requestForLeaveInfo.setCancelTime(new Date());
                                            requestForLeaveInfo.setEnableCancel(true);
                                        }
                                    }
                                    //  requestForLeaveInfo.setStatus(Integer.parseInt(status));
                                    requestForLeaveInfo.setUpdateTime(new Date());
                                    requestForLeaveInfoService.update(requestForLeaveInfo);
                                }
                                //出差
                                if ("3".equals(procinst.getType())) {
                                    businessOut.setStatus(status);
                                    businessOut.setUpdateTime(new Date());
                                    businessOutService.update(businessOut);
                                }
                                //补卡
                                if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                    supplementWork.setStatus(status);
                                    supplementWork.setUpdateTime(new Date());
                                    supplementWorkService.update(supplementWork);
                                }
                                //批量加班
                                if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                    workOver.setStatus(status);
                                    workOver.setUpdateTime(new Date());
                                    workOver.setApprovalTime(new Date());
                                    workOverService.update(workOver);

                                }
                                if ("8".equals(procinst.getType())) {
                                    carryThingsOutFactory.setAuditStatus(status);
                                    carryThingsOutFactory.setUpdateTime(new Date());
                                    carryThingsOutFactoryService.update(carryThingsOutFactory);
                                }
                                //接待就餐
                                if ("9".equals(procinst.getType())) {
                                    customerDiningRecord.setStatus(status);
                                    customerDiningRecord.setUpdateTime(new Date());
                                    customerDiningRecordService.update(customerDiningRecord);
                                }

                                //年假
                                if ("11".equals(procinst.getType())) {
                                    yearRequestForLeaveInfo.setStatus(Integer.parseInt(status));
                                    yearRequestForLeaveInfo.setUpdateTime(new Date());
                                    yearRequestForLeaveInfoService.update(yearRequestForLeaveInfo);
                                }
                            }
                        }
                    } else {
                        log.warn("未审核人的数量等于1,不用区分或签和会签");

                        //审核通过
                        if ("1".equals(status)) {
                            log.warn("审核通过,actUserId={}", procinstActUser.getId());

                            procinstActUser.setAction(status);
                            procinstActUser.setRemark(content);
                            procinstActUser.setFinishTime(new Date());
                            update(procinstActUser);

                            //此步骤审核状态修改
                            procinstActivity.setAction(status);
                            procinstActivity.setStatus("0");
                            procinstActivity.setRemark("同意");
                            procinstActivity.setFinishTime(new Date());
                            procinstActivity.setUpdateTime(new Date());
                            procinstActivityService.update(procinstActivity);

                            //查询是否有下个步骤
                            int activityIndex = procinstActivity.getActivityIndex() + 1;
                            ProcinstActivity procinstActivity1 = procinstActivityService.findByActivityIndexAndProcinstId(activityIndex, procinstActivity.getProcinstId());

                            log.warn("查询是否有下个步骤,procInstId={},activityIndex={},result={}", procinstActivity.getProcinstId(), activityIndex, procinstActivity1 != null);

                            if (procinstActivity1 == null) {
                                log.warn("没有下个步骤则整个流程完毕");

                                procinstActivity.setStatus("1");
                                procinstActivityService.update(procinstActivity);

                                //调班
                                if ("1".equals(procinst.getType())) {
                                    if (adjustShift != null) {
                                        adjustShift.setStatus(status);
                                        //	adjustShift.setRemark("同意");
                                        adjustShift.setUpdateTime(new Date());
                                        adjustShiftService.update(adjustShift);

                                        //调整班次
                                        List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(adjustShift.getId());
                                        if (adjustShiftPersonList.size() > 0) {
                                            for (AdjustShiftPerson adjustShiftPerson : adjustShiftPersonList) {
                                                WorkPersonScheduling workPersonScheduling = workPersonSchedulingService.get(adjustShiftPerson.getSchedulingId());
                                                workPersonScheduling.setShiftIds(adjustShiftPerson.getNewShiftId());
                                                workPersonScheduling.setShiftNumbers(adjustShiftPerson.getNewShiftNumber());
                                                workPersonSchedulingService.update(workPersonScheduling);

                                                //todo 如果调整当天之前的班次则重新生成考勤
                                                Date now = new Date();

                                                if (workPersonScheduling.getSchedulingDay() != null && workPersonScheduling.getSchedulingDay().before(now)) {
                                                    DateTime startTime = new DateTime(workPersonScheduling.getSchedulingDay());
                                                    DateTime endTime = startTime; //updatePersonAttendance会自动加一天

                                                    PersonPopedom personPopedom1 = personPopedomService.get(adjustShiftPerson.getPersonPopedomId());

                                                    UpdatePersonAttendanceDTO updatePersonAttendanceDTO = new UpdatePersonAttendanceDTO(
                                                            personPopedom1.getPersonId(), startTime.toDate(), endTime.toDate());

                                                    rabbitTemplate.convertAndSend("updatePersonAttendanceQueue", updatePersonAttendanceDTO);
                                                }
                                            }
                                        }
                                    }
                                }

                                //请假
                                if ("2".equals(procinst.getType())) {
                                    //请假审核
                                    if (requestForLeaveInfo.getStatus() == 0) {
                                        requestForLeaveInfo.setStatus(Integer.parseInt(status));
//                                        if ("2".equals(requestForLeaveInfo.getLeaveType()) || "5".equals(requestForLeaveInfo.getLeaveType()) || "6".equals(requestForLeaveInfo.getLeaveType())){
//                                            requestForLeaveInfo.setEnableCancel(true)
//                                        }
                                        double days = 0;
                                        if (StringUtils.isNotEmpty(requestForLeaveInfo.getDays())) {
                                            days = Double.parseDouble(requestForLeaveInfo.getDays());
                                        }

                                        if (days >= 4) {
                                            requestForLeaveInfo.setEnableCancel(true);
                                        } else {
                                            if ("2".equals(requestForLeaveInfo.getLeaveType()) || "5".equals(requestForLeaveInfo.getLeaveType()) || "6".equals(requestForLeaveInfo.getLeaveType())) {
                                                requestForLeaveInfo.setEnableCancel(true);
                                            }
                                        }
                                        /*if (("1".equals(requestForLeaveInfo.getLeaveType())&&days>=4) ||"2".equals(requestForLeaveInfo.getLeaveType()) || "5".equals(requestForLeaveInfo.getLeaveType()) || "6".equals(requestForLeaveInfo.getLeaveType())){
                                            requestForLeaveInfo.setEnableCancel(true);
                                        }*/
                                    } else {
                                        //销假审核
                                        if ("0".equals(requestForLeaveInfo.getCancelStatus())) {
                                            requestForLeaveInfo.setCancelStatus(status);
                                            requestForLeaveInfo.setCancelTime(new Date());
                                        }
                                    }

                                    //  requestForLeaveInfo.setStatus(1);
                                    requestForLeaveInfo.setUpdateTime(new Date());
                                    requestForLeaveInfoService.update(requestForLeaveInfo);

                                }

                                //出差
                                if ("3".equals(procinst.getType())) {
                                    businessOut.setStatus(status);
                                    businessOut.setUpdateTime(new Date());
                                    businessOutService.update(businessOut);
                                }

                                //补卡
                                if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                    supplement(status, supplementWork);
                                }

                                //批量加班
                                if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                    workOver.setStatus(status);
                                    workOver.setUpdateTime(new Date());
                                    workOver.setApprovalTime(new Date());
                                    workOverService.update(workOver);
                                }

                                //携物出厂
                                if ("8".equals(procinst.getType())) {
                                    carryThingsOutFactory.setAuditStatus(status);
                                    carryThingsOutFactory.setUpdateTime(new Date());
                                    carryThingsOutFactoryService.update(carryThingsOutFactory);
                                }

                                //接待就餐
                                if ("9".equals(procinst.getType())) {
                                    customerDiningRecord.setStatus(status);
                                    customerDiningRecord.setUpdateTime(new Date());
                                    customerDiningRecordService.update(customerDiningRecord);
                                }

                                //年假
                                if ("11".equals(procinst.getType())) {
                                    yearRequestForLeaveInfo.setStatus(Integer.parseInt(status));
                                    yearRequestForLeaveInfo.setUpdateTime(new Date());
                                    yearRequestForLeaveInfoService.update(yearRequestForLeaveInfo);
                                }
                            } else {
                                log.warn("有下个步骤，修改下个步骤的状态");

                                procinstActivity1.setStatus("1");
                                procinstActivityService.update(procinstActivity1);

                                //通知下个步骤的审核人
                                List<ProcinstActUser> procinstActUserList1 = findByProcinstActivityIdActionType(procinstActivity1.getId(), "0", "1");

                                if (procinstActUserList1.size() > 0) {
                                    new Thread(() -> {
                                        for (ProcinstActUser procinstActUser1 : procinstActUserList1) {
                                            try {
                                                PersonInfo personInfo = personInfoService.get(Long.parseLong(procinstActUser1.getUserId()));
                                                String accessToken = JwtUtil2.createToken(jwtSecret, personInfo.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                                String typeName = "";
                                                if ("1".equals(procinst.getType())) {
                                                    typeName = "调班";
                                                }
                                                if ("2".equals(procinst.getType())) {
                                                    RequestForLeaveInfo requestForLeaveInfo1 = requestForLeaveInfoService.get(procinst.getBusinessKey());
                                                    typeName = "请假";
                                                    if (requestForLeaveInfo1.getStatus() == 1 && "0".equals(requestForLeaveInfo1.getCancelStatus())) {
                                                        typeName = "销假";
                                                    }
                                                    // typeName = "请假";
                                                }
                                                if ("3".equals(procinst.getType())) {
                                                    typeName = "出差";
                                                }
                                                if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                                    typeName = "补卡";
                                                }
                                                if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                                    typeName = "加班";
                                                }
                                                if ("8".equals(procinst.getType())) {
                                                    typeName = "携物出厂";
                                                }
                                                if ("9".equals(procinst.getType())) {
                                                    typeName = "接待就餐";
                                                }
                                                if ("11".equals(procinst.getType())) {
                                                    typeName = "年假";
                                                }
                                                WechatMessageUtil.sendEvenMessage(personInfo.getOpenId(), "有一条新的" + typeName + "信息待审核", personInfo.getName(), com.jpsoft.shinestar.modules.common.utils.StringUtils.getApprovalNo(), "前往处理", wxConfig.getPortalUrl() + "xsy/approval/infosp?id=" + procinst.getBusinessKey() + "&procinstActUserId=" + procinstActUser1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                            } catch (Exception ex) {
                                                ex.printStackTrace();
                                            }
                                        }
                                    }).start();
                                }
                            }

                            //查询此步骤还有多少人未抄送
                            List<ProcinstActUser> ccProcinstActUserList = findByProcinstActivityIdActionType(procinstActUser.getProcinstActivityId(), "0", "2");
                            if (ccProcinstActUserList.size() > 0) {
                                //TODO 抄送发送模板消息
                                new Thread(() -> {
                                    for (ProcinstActUser ccProcinstActUer : ccProcinstActUserList) {
                                        PersonInfo personInfo = personInfoService.get(Long.parseLong(ccProcinstActUer.getUserId()));
                                        String accessToken = JwtUtil2.createToken(jwtSecret, personInfo.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                        //发起人
                                        PersonInfo personInfo1 = null;
                                        String typeName = "";
                                        if ("1".equals(procinst.getType())) {
                                            typeName = "调班";
                                            AdjustShift adjustShift1 = adjustShiftService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(adjustShift1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                        }
                                        if ("2".equals(procinst.getType())) {
                                            RequestForLeaveInfo requestForLeaveInfo1 = requestForLeaveInfoService.get(procinst.getBusinessKey());
                                            typeName = "请假";
                                            if (requestForLeaveInfo1.getStatus() == 1 && "0".equals(requestForLeaveInfo1.getCancelStatus())) {
                                                typeName = "销假";
                                            }
                                            PersonPopedom personPopedom1 = personPopedomService.get(requestForLeaveInfo1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                        }
                                        if ("3".equals(procinst.getType())) {
                                            typeName = "出差";
                                            BusinessOut businessOut1 = businessOutService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(businessOut1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                        }
                                        if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                            typeName = "补卡";
                                            SupplementWork supplementWork1 = supplementWorkService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(supplementWork1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());


                                        }
                                        if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                            typeName = "加班";
                                            WorkOver workOver1 = workOverService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(workOver1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                        }
                                        if ("8".equals(procinst.getType())) {
                                            typeName = "携物出厂";
                                            CarryThingsOutFactory carryThingsOutFactory1 = carryThingsOutFactoryService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(carryThingsOutFactory1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                        }
                                        if ("9".equals(procinst.getType())) {
                                            typeName = "接待就餐";
                                            CustomerDiningRecord customerDiningRecord1 = customerDiningRecordService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(customerDiningRecord1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                                        }
                                        if ("11".equals(procinst.getType())) {
                                            typeName = "年假";
                                            YearRequestForLeaveInfo yearRequestForLeaveInfo1 = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());
                                            PersonPopedom personPopedom1 = personPopedomService.get(yearRequestForLeaveInfo1.getPersonPopedomId());
                                            personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                        }
                                        WechatMessageUtil.sendCCRemindMessage(personInfo.getOpenId(), "[抄送]" + typeName + "审核通过", personInfo1.getName(), typeName, typeName + "审核", DateUtil.format(procinst.getCreateTime(), "yyyy-MM-dd HH:mm:ss"), "已抄送", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + procinst.getBusinessKey() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                    }
                                }).start();
                            }
                        }

                        //审核拒绝
                        if ("3".equals(status)) {
                            log.warn("审核拒绝,actUserId={}", procinstActUser.getId());

                            procinstActUserList.remove(procinstActUser);
                            procinstActUser.setAction(status);
                            procinstActUser.setRemark(content);
                            procinstActUser.setFinishTime(new Date());
                            update(procinstActUser);

                            //此步骤审核状态修改
                            procinstActivity.setAction(status);
                            procinstActivity.setRemark("拒绝");
                            procinstActivity.setFinishTime(new Date());
                            procinstActivity.setUpdateTime(new Date());
                            procinstActivityService.update(procinstActivity);

                            //此申请流程其他未处理的待审核记录
                            List<ProcinstActivity> noExamineList = procinstActivityService.findProcinstIdAndAction(procinstActivity.getProcinstId(), "0");
                            if (noExamineList.size() > 0) {
                                for (ProcinstActivity procinstActivity1 : noExamineList) {
                                    procinstActivity1.setDelFlag(true);
                                    procinstActivity1.setUpdateTime(new Date());
                                    procinstActivityService.update(procinstActivity1);
                                }
                            }

                            if ("1".equals(procinst.getType())) {
                                adjustShift.setStatus(status);
                                adjustShift.setRemark("拒绝");
                                adjustShift.setUpdateTime(new Date());
                                adjustShiftService.update(adjustShift);
                            }
                            if ("2".equals(procinst.getType())) {
                                //请假审核
                                if (requestForLeaveInfo.getStatus() == 0) {
                                    requestForLeaveInfo.setStatus(Integer.parseInt(status));
                                } else {
                                    //销假审核
                                    if ("0".equals(requestForLeaveInfo.getCancelStatus())) {
                                        requestForLeaveInfo.setCancelStatus(status);
                                        requestForLeaveInfo.setCancelTime(new Date());
                                        requestForLeaveInfo.setEnableCancel(true);
                                    }
                                }
                                //  requestForLeaveInfo.setStatus(Integer.parseInt(status));
                                requestForLeaveInfo.setUpdateTime(new Date());
                                requestForLeaveInfoService.update(requestForLeaveInfo);
                            }
                            //出差
                            if ("3".equals(procinst.getType())) {
                                businessOut.setStatus(status);
                                businessOut.setUpdateTime(new Date());
                                businessOutService.update(businessOut);
                            }

                            //补卡
                            if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                                supplementWork.setStatus(status);
                                supplementWork.setUpdateTime(new Date());
                                supplementWorkService.update(supplementWork);
                            }

                            //批量加班
                            if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())) {
                                workOver.setStatus(status);
                                workOver.setUpdateTime(new Date());
                                workOver.setApprovalTime(new Date());
                                workOverService.update(workOver);
                            }

                            //携物出厂
                            if ("8".equals(procinst.getType())) {
                                carryThingsOutFactory.setAuditStatus(status);
                                carryThingsOutFactory.setUpdateTime(new Date());
                                carryThingsOutFactoryService.update(carryThingsOutFactory);
                            }

                            //接待就餐
                            if ("9".equals(procinst.getType())) {
                                customerDiningRecord.setStatus(status);
                                customerDiningRecord.setUpdateTime(new Date());
                                customerDiningRecordService.update(customerDiningRecord);
                            }

                            //年假
                            if ("11".equals(procinst.getType())) {
                                yearRequestForLeaveInfo.setStatus(Integer.parseInt(status));
                                yearRequestForLeaveInfo.setUpdateTime(new Date());
                                yearRequestForLeaveInfoService.update(yearRequestForLeaveInfo);
                            }
                        }
                    }

                    //调班
                    if ("1".equals(procinst.getType())) {
                        AdjustShift adjustShift1 = adjustShiftService.get(procinst.getBusinessKey());
                        if (!"0".equals(adjustShift1.getStatus())) {
                            List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(procinst.getBusinessKey());
                            new Thread(() -> {
                                String statusN = "";
                                if ("1".equals(adjustShift1.getStatus())) {
                                    statusN = "同意";
                                }
                                if ("3".equals(adjustShift1.getStatus())) {
                                    statusN = "拒绝";
                                }

                                if (adjustShiftPersonList.size() > 0) {
                                    for (AdjustShiftPerson adjustShiftPerson : adjustShiftPersonList) {
                                        try {
                                            PersonPopedom personPopedom1 = personPopedomService.get(adjustShiftPerson.getPersonPopedomId());
                                            PersonInfo person = personInfoService.get(personPopedom1.getPersonId());

                                            String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());

                                            WechatMessageUtil.sendExamine(person.getOpenId(), "调班审核" + statusN, person.getName(), "1", statusN, "调班", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + adjustShiftPerson.getAdjustShiftId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                }
                            }).start();
                        }
                    }

                    //请假
                    if ("2".equals(procinst.getType())) {
                        RequestForLeaveInfo requestForLeaveInfo1 = requestForLeaveInfoService.get(procinst.getBusinessKey());

                        if (requestForLeaveInfo1.getStatus() != 0) {
                            new Thread(() -> {
                                String statusN = "";
                                String typeNameN = "请假";
                                if ("5".equals(requestForLeaveInfo1.getCancelStatus())) {
                                    if (requestForLeaveInfo1.getStatus() == 1) {
                                        statusN = "同意";

                                        try {
                                            //请假修改人员工作状态
                                            requestForLeaveInfoService.logic(requestForLeaveInfo1);
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }

                                    }
                                    if (requestForLeaveInfo1.getStatus() == 3) {
                                        statusN = "拒绝";
                                    }
                                } else {
                                    typeNameN = "销假";
                                    if ("1".equals(requestForLeaveInfo1.getCancelStatus())) {
                                        statusN = "同意";
                                    }
                                    if ("3".equals(requestForLeaveInfo1.getCancelStatus())) {
                                        statusN = "拒绝";
                                    }
                                }

                                PersonInfo person = personInfoService.get(requestForLeaveInfo1.getPersonId());

                                String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                WechatMessageUtil.sendExamine(person.getOpenId(), typeNameN + "审核" + statusN, person.getName(), "1", statusN, typeNameN, "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + requestForLeaveInfo1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                            }).start();
                        }
                    }

                    //出差
                    if ("3".equals(procinst.getType())) {
                        BusinessOut businessOut1 = businessOutService.get(procinst.getBusinessKey());

                        if (!"0".equals(businessOut1.getStatus())) {
                            new Thread(() -> {
                                String statusN = "";
                                if ("1".equals(businessOut1.getStatus())) {
                                    statusN = "同意";

                                    try {
                                        //出差修改人员状态
                                        businessOutService.logic(businessOut1);
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                                if ("3".equals(businessOut1.getStatus())) {
                                    statusN = "拒绝";
                                }

                                businessOut1.setApprovalTime(new Date());
                                businessOut1.setApprovalPersonPopedomId(personPopedom.getId());
                                businessOut1.setApprovalContent(content);
                                businessOut1.setRemark(statusN);
                                businessOutService.update(businessOut1);

                                PersonPopedom personPopedom1 = personPopedomService.get(businessOut1.getPersonPopedomId());

                                PersonInfo person = personInfoService.get(personPopedom1.getPersonId());

                                String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                WechatMessageUtil.sendExamine(person.getOpenId(), "出差审核" + statusN, person.getName(), "1", statusN, "出差", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + businessOut1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                            }).start();
                        }
                    }

                    //补卡
                    if ("4".equals(procinst.getType()) || "5".equals(procinst.getType())) {
                        SupplementWork supplementWork1 = supplementWorkService.get(procinst.getBusinessKey());

                        List<SupplementWorkPerson> supplementWorkPersonList = supplementWorkPersonService.findBySupplementWorkId(supplementWork1.getId());
                        if (!"0".equals(supplementWork1.getStatus()) && supplementWorkPersonList.size() > 0) {

                            for (SupplementWorkPerson supplementWorkPerson : supplementWorkPersonList) {
                                new Thread(() -> {
                                    String statusN = "";
                                    if ("1".equals(supplementWork1.getStatus())) {
                                        statusN = "同意";
                                    }
                                    if ("3".equals(supplementWork1.getStatus())) {
                                        statusN = "拒绝";
                                    }
                                    PersonPopedom personPopedom1 = personPopedomService.get(supplementWorkPerson.getPersonPopedomId());

                                    PersonInfo person = personInfoService.get(personPopedom1.getPersonId());

                                    String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                    WechatMessageUtil.sendExamine(person.getOpenId(), "补卡审核" + statusN, person.getName(), "1", statusN, "补卡", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + supplementWork1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                }).start();
                            }

                        }
                    }

                    //加班
                    if (("6".equals(procinst.getType()) || "7".equals(procinst.getType())) && workOver != null) {
//                        WorkOver workOver1 = workOverService.get(procinst.getBusinessKey());
                        List<WorkOverPerson> workOverPersonList = workOverPersonService.findByWorkOverId(workOver.getId());
                        if (!"0".equals(workOver.getStatus()) && workOverPersonList.size() > 0) {
                            String statusN = "";
                            if ("1".equals(workOver.getStatus())) {
                                statusN = "同意";
                            }
                            if ("3".equals(workOver.getStatus())) {
                                statusN = "拒绝";
                            }

                            workOver.setApprovalTime(new Date());
                            workOver.setApprovalPersonPopedomId(personPopedom.getId());
                            workOver.setApprovalContent(content);
                            workOver.setRemark(statusN);
                            workOverService.update(workOver);

                            log.warn("更新加班记录,workOverId={},remark={}", workOver.getId(), workOver.getRemark());

                            final WorkOver fWorkOver = workOver;
                            final String fStatusN = statusN;

                            for (WorkOverPerson workOverPerson : workOverPersonList) {
                                new Thread(() -> {
                                    PersonPopedom personPopedom1 = personPopedomService.get(workOverPerson.getPersonPopedomId());

                                    PersonInfo person = personInfoService.get(personPopedom1.getPersonId());

                                    String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                    WechatMessageUtil.sendExamine(person.getOpenId(), "加班审核" + fStatusN, person.getName(), "1", fStatusN, "加班", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + fWorkOver.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                }).start();
                            }

                        }
                    }

                    //携物出厂
                    if ("8".equals(procinst.getType())) {
                        CarryThingsOutFactory carryThingsOutFactory1 = carryThingsOutFactoryService.get(procinst.getBusinessKey());

                        if (!"0".equals(carryThingsOutFactory1.getAuditStatus())) {
                            new Thread(() -> {
                                String statusN = "";
                                if ("1".equals(carryThingsOutFactory1.getAuditStatus())) {
                                    statusN = "同意";
                                }
                                if ("3".equals(carryThingsOutFactory1.getAuditStatus())) {
                                    statusN = "拒绝";
                                }
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(carryThingsOutFactory1.getPersonPopedomId());

                                PersonInfo person = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());

                                String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                WechatMessageUtil.sendExamine(person.getOpenId(), "携物出厂审核" + statusN, person.getName(), "1", statusN, "携物出厂", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + carryThingsOutFactory1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                            }).start();


                        }
                    }

                    //接待就餐
                    if ("9".equals(procinst.getType())) {
                        CustomerDiningRecord customerDiningRecord1 = customerDiningRecordService.get(procinst.getBusinessKey());

                        if (!"0".equals(customerDiningRecord1.getStatus())) {
                            new Thread(() -> {
                                String statusN = "";
                                if ("1".equals(customerDiningRecord1.getStatus())) {
                                    statusN = "同意";
                                }
                                if ("3".equals(customerDiningRecord1.getStatus())) {
                                    statusN = "拒绝";
                                }
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord1.getPersonPopedomId());

                                PersonInfo person = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());

                                String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                WechatMessageUtil.sendExamine(person.getOpenId(), "接待就餐审核" + statusN, person.getName(), "1", statusN, "接待就餐", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + customerDiningRecord1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                            }).start();
                        }
                    }


                    //年假
                    if ("11".equals(procinst.getType())) {
                        YearRequestForLeaveInfo yearRequestForLeaveInfo1 = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());

                        if (yearRequestForLeaveInfo1.getStatus() != 0) {
                            new Thread(() -> {
                                String statusN = "";
                                if (yearRequestForLeaveInfo1.getStatus() == 1) {
                                    statusN = "同意";

                                    try {
                                        //年假修改人员状态
                                        yearRequestForLeaveInfoService.logic(yearRequestForLeaveInfo1);
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                                if (yearRequestForLeaveInfo1.getStatus() == 3) {
                                    statusN = "拒绝";
                                }

                                PersonInfo person = personInfoService.get(yearRequestForLeaveInfo1.getPersonId());

                                String accessToken = JwtUtil2.createToken(jwtSecret, person.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                WechatMessageUtil.sendExamine(person.getOpenId(), "年假审核" + statusN, person.getName(), "1", statusN, "年假", "前往查看", wxConfig.getPortalUrl() + "xsy/approval/info?id=" + yearRequestForLeaveInfo1.getId() + "&formId=" + processSet.getFormId() + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                            }).start();
                        }
                    }
                }
            }
        }

    }

    private void supplement(String status, SupplementWork supplementWork) {
        if (supplementWork != null) {
            supplementWork.setStatus(status);
            supplementWork.setUpdateTime(new Date());
            supplementWorkService.update(supplementWork);
            List<SupplementWorkPerson> supplementWorkPersonList = supplementWorkPersonService.findBySupplementWorkId(supplementWork.getId());

            if (supplementWorkPersonList.size() > 0) {
                for (SupplementWorkPerson supplementWorkPerson : supplementWorkPersonList) {
                    WorkScheduleAttendance workScheduleAttendance = workScheduleAttendanceService.get(supplementWorkPerson.getScheduleAttendanceId());

                    if (workScheduleAttendance.getDelFlag() == null || workScheduleAttendance.getDelFlag()) {
                        workScheduleAttendance = workScheduleAttendanceService.findByScheduleAttendanceId(workScheduleAttendance.getPersonScheduleId(), workScheduleAttendance.getWorkShiftId(), workScheduleAttendance.getClassify());

                        if (workScheduleAttendance != null) {
                            supplementWorkPerson.setScheduleAttendanceId(workScheduleAttendance.getId());
                            supplementWorkPersonService.update(supplementWorkPerson);
                        }
                    }

                    if (workScheduleAttendance != null) {
                        workScheduleAttendance.setResult("1");
                        workScheduleAttendance.setRemark("补卡");
                        workScheduleAttendance.setSupplementWorkPersonId(supplementWorkPerson.getId());
                        workScheduleAttendance.setUpdateTime(new Date());

                        workScheduleAttendanceService.update(workScheduleAttendance);
                    }
                }
            }
        }
    }


    @Override
    public List<ProcinstActUser> findByProcinstActivityIdActionType(String procinstActivityId, String action, String type) {
        return procinstActUserDAO.findByProcinstActivityIdActionType(procinstActivityId, action, type);
    }
}