//package com.gree.mall.manager.logic.sop;
//
//
//import cn.hutool.core.convert.Convert;
//import cn.hutool.core.date.DateUnit;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.json.JSON;
//import cn.hutool.json.JSONUtil;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.gree.mall.manager.bean.admin.AdminUserCom;
//import com.gree.mall.manager.bean.delay.Job;
//import com.gree.mall.manager.bean.sop.SopPushTaskData;
//import com.gree.mall.manager.bean.sop.SopRuleAdd;
//import com.gree.mall.manager.bean.sop.SopRuleData;
//import com.gree.mall.manager.bean.sop.SopWorkerPushCount;
//import com.gree.mall.manager.commonmapper.SopRulePushMapper;
//import com.gree.mall.manager.constant.DelayConfig;
//import com.gree.mall.manager.constant.JobStatus;
//import com.gree.mall.manager.enums.JobTopic;
//import com.gree.mall.manager.exception.RemoteServiceException;
//import com.gree.mall.manager.logic.common.CommonLogic;
//import com.gree.mall.manager.logic.common.WorkWechatLogic;
//import com.gree.mall.manager.plus.entity.*;
//import com.gree.mall.manager.plus.service.*;
//import com.gree.mall.manager.utils.DateUtils;
//import com.gree.mall.manager.utils.StringUtil;
//import cn.hutool.core.util.XmlUtil;
//import lombok.extern.slf4j.Slf4j;
//import me.chanjar.weixin.common.error.WxErrorException;
//import me.chanjar.weixin.common.service.WxService;
//import me.chanjar.weixin.cp.bean.WxCpUser;
//import me.chanjar.weixin.cp.bean.external.contact.FollowedUser;
//import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.CollectionUtils;
//
//import javax.servlet.http.HttpServletRequest;
//import java.text.ParseException;
//import java.util.*;
//
///**
// * @author change
// */
//@Slf4j
//@Service
//public class SopRuleLogic {
//
//    @Autowired
//    private SopRulePushMapper sopRuleTaskMapper;
//
//    @Autowired
//    private CommonLogic commonLogic;
//
//    @Autowired
//    private SopRuleService sopRuleService;
//
//    @Autowired
//    private SopRuleTaskService sopRuleTaskService;
//
//    @Autowired
//    private SopRuleWorkerService sopRuleWorkerService;
//
//    @Autowired
//    private SopPushTaskService sopPushTaskService;
//
//    @Autowired
//    private SopPushTaskTagService sopPushTaskTagService;
//
//    @Autowired
//    private SopWorkerPushService sopWorkerPushService;
//
//    @Autowired
//    private ExternalUserService externalUserService;
//
//    @Autowired
//    private AdminCompanyWechatService adminCompanyWechatService;
//
//    @Autowired
//    private EnterpriseWechatWorkerService enterpriseWechatWorkerService;
//
//    @Autowired
//    private MarketingMaterialsService marketingMaterialsService;
//
//
//    @Autowired
//    private WorkWechatLogic workWechatLogic;
//
//    @Autowired
//    private UserTagService userTagService;
//
//
////    @Autowired
////    private JobLogic jobLogic;
//
//    public IPage<SopRuleData> list(String ruleName, Integer status, HttpServletRequest request, Integer pageNum, Integer pageSize) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        IPage<SopRuleData> list = sopRuleTaskMapper.list(new Page(pageNum, pageSize), ruleName, status, adminUser.getCompanyWechatIds(), null);
//        List<SopRuleData> records = list.getRecords();
//        for (SopRuleData record : records) {
//            List<SopPushTaskData> sopPushTask = sopRuleTaskMapper.getTaskPush(record.getId());
//            record.setSopPushTask(sopPushTask);
//        }
//        list.setRecords(records);
//        return list;
//    }
//
//
//    @Transactional(rollbackFor = Exception.class)
//    public void add(SopRuleAdd sopRuleAdd, HttpServletRequest request) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        if (sopRuleService.lambdaQuery().eq(SopRule::getRuleName, sopRuleAdd.getRuleName()).count() > 0) {
//            throw new RemoteServiceException("规则名重复");
//        }
//        //添加规则
//        SopRule sopRule = new SopRule();
//        sopRule.setRuleName(sopRuleAdd.getRuleName());
//        sopRule.setCompanyWechatId(adminUser.getCompanyWechatId());
//        sopRule.setCompanyName(adminUser.getCompanyName());
//        sopRule.setCreateTime(new Date());
//        sopRule.setUpdateTime(new Date());
//        sopRule.setCreateBy(adminUser.getNickName());
//        sopRule.setStatus(1);
//        boolean save = sopRuleService.save(sopRule);
//        //添加规则id
//        for (SopPushTaskData sopPushTaskDatum : sopRuleAdd.getSopPushTaskData()) {
//            sopPushTaskDatum.setCreateTime(new Date());
//            sopPushTaskDatum.setUpdateTime(new Date());
//            sopPushTaskDatum.setCompanyWechatId(adminUser.getCompanyWechatId());
//            sopPushTaskDatum.setCompanyName(adminUser.getCompanyName());
//            sopPushTaskService.save(sopPushTaskDatum);
//            if (!CollectionUtils.isEmpty(sopPushTaskDatum.getTags())) {
//                for (Tag tag : sopPushTaskDatum.getTags()) {
//                    SopPushTaskTag sopPushTaskTag = new SopPushTaskTag();
//                    sopPushTaskTag.setTaskId(sopPushTaskDatum.getId());
//                    sopPushTaskTag.setGroupId(tag.getGroupId());
//                    sopPushTaskTag.setTagId(tag.getTagId());
//                    sopPushTaskTagService.save(sopPushTaskTag);
//                }
//
//            }
//            SopRuleTask sopRuleTask = new SopRuleTask();
//            sopRuleTask.setTaskId(sopPushTaskDatum.getId());
//            sopRuleTask.setRuleId(sopRule.getId());
//            sopRuleTaskService.save(sopRuleTask);
//        }
//
//        //添加员工
//        List<SopRuleWorker> sopRuleTaskList = new ArrayList<>();
//        for (String worker : sopRuleAdd.getWorkerIds()) {
//            EnterpriseWechatWorker byId = enterpriseWechatWorkerService.getById(worker);
//            SopRuleWorker sopRuleWorker = new SopRuleWorker();
//            sopRuleWorker.setWechatUserId(byId.getWechatUserId());
//            sopRuleWorker.setCorpId(adminUser.getCorpId());
//            sopRuleWorker.setRuleId(sopRule.getId());
//            sopRuleTaskList.add(sopRuleWorker);
//        }
//        sopRuleWorkerService.saveBatch(sopRuleTaskList);
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    public void edit(SopRuleAdd sopRuleAdd, HttpServletRequest request) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        //添加规则
//        SopRule sopRule = new SopRule();
//        sopRule.setRuleName(sopRuleAdd.getRuleName());
//        sopRule.setId(sopRuleAdd.getId());
//        sopRule.setUpdateTime(new Date());
//        sopRule.setStatus(sopRuleAdd.getStatus());
//        boolean b = sopRuleService.updateById(sopRule);
//        //删除员工，添加员工
//        sopRuleWorkerService.lambdaUpdate().eq(SopRuleWorker::getRuleId, sopRule.getId()).remove();
//        List<SopRuleWorker> sopRuleTaskList = new ArrayList<>();
//        for (String worker : sopRuleAdd.getWorkerIds()) {
//            EnterpriseWechatWorker byId = enterpriseWechatWorkerService.getById(worker);
//            SopRuleWorker sopRuleWorker = new SopRuleWorker();
//            sopRuleWorker.setWechatUserId(byId.getWechatUserId());
//            sopRuleWorker.setCorpId(adminUser.getCorpId());
//            sopRuleWorker.setRuleId(sopRule.getId());
//            sopRuleTaskList.add(sopRuleWorker);
//        }
//
//        //添加规则id
//        sopRuleTaskService.lambdaUpdate().eq(SopRuleTask::getRuleId, sopRule.getId()).remove();
//        for (SopPushTaskData sopPushTaskDatum : sopRuleAdd.getSopPushTaskData()) {
//            sopPushTaskDatum.setCreateTime(new Date());
//            sopPushTaskDatum.setUpdateTime(new Date());
//            sopPushTaskDatum.setCompanyWechatId(adminUser.getCompanyWechatId());
//            sopPushTaskDatum.setCompanyName(adminUser.getCompanyName());
//            sopPushTaskDatum.setId(null);
//            sopPushTaskService.save(sopPushTaskDatum);
//            if (!CollectionUtils.isEmpty(sopPushTaskDatum.getTags())) {
//                for (Tag tag : sopPushTaskDatum.getTags()) {
//                    SopPushTaskTag sopPushTaskTag = new SopPushTaskTag();
//                    sopPushTaskTag.setTaskId(sopPushTaskDatum.getId());
//                    sopPushTaskTag.setGroupId(tag.getGroupId());
//                    sopPushTaskTag.setTagId(tag.getTagId());
//                    sopPushTaskTagService.save(sopPushTaskTag);
//                }
//
//            }
//            SopRuleTask sopRuleTask = new SopRuleTask();
//            sopRuleTask.setTaskId(sopPushTaskDatum.getId());
//            sopRuleTask.setRuleId(sopRule.getId());
//            sopRuleTaskService.save(sopRuleTask);
//        }
//        sopRuleWorkerService.saveBatch(sopRuleTaskList);
//    }
//
//    public SopRuleData detail(String id) {
//        SopRuleData sopRuleData = sopRuleTaskMapper.list(new Page(1, 1), null, null, null, id).getRecords().get(0);
//        List<SopPushTaskData> sopPushTask = sopRuleTaskMapper.getTaskPush(sopRuleData.getId());
//        sopRuleData.setSopPushTask(sopPushTask);
//        return sopRuleData;
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    public void editPushTask(SopPushTaskData sopPushTaskData) {
//        sopPushTaskData.setUpdateTime(new Date());
//        sopPushTaskService.updateById(sopPushTaskData);
//        if (!CollectionUtils.isEmpty(sopPushTaskData.getTags())) {
//            sopPushTaskTagService.lambdaUpdate().eq(SopPushTaskTag::getTaskId, sopPushTaskData.getId()).remove();
//            for (Tag tag : sopPushTaskData.getTags()) {
//                SopPushTaskTag sopPushTaskTag = new SopPushTaskTag();
//                sopPushTaskTag.setTaskId(sopPushTaskData.getId());
//                sopPushTaskTag.setGroupId(tag.getGroupId());
//                sopPushTaskTag.setTagId(tag.getTagId());
//                sopPushTaskTagService.save(sopPushTaskTag);
//            }
//
//        }
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    public void deleteTask(String id) {
//        sopRuleTaskService.lambdaUpdate().eq(SopRuleTask::getTaskId, id).remove();
//        sopPushTaskService.removeById(id);
//    }
//
//    public SopPushTaskData detailTask(String id) {
//        return sopRuleTaskMapper.detailTask(id);
//    }
//
//
//    public IPage<SopWorkerPush> taskList(String taskId, String ruleId, String externalName, String startTime, String endTime, Integer workerStatus, HttpServletRequest request, Integer pageNum, Integer pageSize) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        return sopWorkerPushService.lambdaQuery()
//                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), SopWorkerPush::getCompanyWechatId, adminUser.getCompanyWechatIds())
//                .eq(SopWorkerPush::getTaskId, taskId)
//                .eq(SopWorkerPush::getRuleId, ruleId)
//                .eq(!StringUtil.isEmpty(externalName), SopWorkerPush::getExternalName, externalName)
//                .eq(!StringUtil.isEmpty(externalName), SopWorkerPush::getExternalName, externalName)
//                .between(!StringUtil.isEmpty(startTime) && !StringUtil.isEmpty(endTime), SopWorkerPush::getTaskTime, startTime, endTime)
//                .orderByDesc(SopWorkerPush::getCreateTime)
//                .page(new Page<>(pageNum, pageSize));
//    }
//
//    public SopWorkerPushCount taskCount(String taskId, String ruleId, HttpServletRequest request) {
//        SopWorkerPushCount sopWorkerPushCount = new SopWorkerPushCount();
//        sopWorkerPushCount.setAllCount(sopWorkerPushService.lambdaQuery().eq(SopWorkerPush::getTaskId, taskId)
//                .eq(SopWorkerPush::getRuleId, ruleId).count());
//        sopWorkerPushCount.setWorkerCount(sopWorkerPushService.lambdaQuery().eq(SopWorkerPush::getTaskId, taskId)
//                .eq(SopWorkerPush::getRuleId, ruleId).eq(SopWorkerPush::getWorkerStatus, 1).count());
//        sopWorkerPushCount.setDayCount(sopWorkerPushService.lambdaQuery().eq(SopWorkerPush::getTaskId, taskId)
//                .eq(SopWorkerPush::getRuleId, ruleId).between(SopWorkerPush::getTaskTime, DateUtils.getStartTime(), DateUtils.getEndTime()).count());
//        sopWorkerPushCount.setDayWorkerCount(sopWorkerPushService.lambdaQuery().eq(SopWorkerPush::getTaskId, taskId)
//                .eq(SopWorkerPush::getRuleId, ruleId).between(SopWorkerPush::getTaskTime, DateUtils.getStartTime(), DateUtils.getEndTime()).eq(SopWorkerPush::getWorkerStatus, 1).count());
//        return sopWorkerPushCount;
//    }
//
//
//    public void addPushTask(SopPushTaskData sopPushTaskData, String id, HttpServletRequest request) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        sopPushTaskData.setCreateTime(new Date());
//        sopPushTaskData.setUpdateTime(new Date());
//        sopPushTaskData.setCompanyWechatId(adminUser.getCompanyWechatId());
//        sopPushTaskData.setCompanyName(adminUser.getCompanyName());
//        sopPushTaskService.save(sopPushTaskData);
//        if (!CollectionUtils.isEmpty(sopPushTaskData.getTags())) {
//            for (Tag tag : sopPushTaskData.getTags()) {
//                SopPushTaskTag sopPushTaskTag = new SopPushTaskTag();
//                sopPushTaskTag.setTaskId(sopPushTaskData.getId());
//                sopPushTaskTag.setGroupId(tag.getGroupId());
//                sopPushTaskTag.setTagId(tag.getTagId());
//                sopPushTaskTagService.save(sopPushTaskTag);
//            }
//
//        }
//        SopRuleTask sopRuleTask = new SopRuleTask();
//        sopRuleTask.setTaskId(sopPushTaskData.getId());
//        sopRuleTask.setRuleId(id);
//        sopRuleTaskService.save(sopRuleTask);
//
//    }
//
//    public void pushTaskWorker(String decrypt, String corpId) {
//        Map<String, Object> map = XmlUtil.xmlToMap(decrypt);
//        System.out.println(map);
//        if (map.get("ChangeType").equals("edit_external_contact"))
//            sendTag(map);
//
//        if (map.get("ChangeType").equals("add_external_contact") || map.get("ChangeType").equals("add_half_external_contact")) {
//
//            //查询用户是否关联了规则
//            if (sopRuleWorkerService.lambdaQuery().eq(SopRuleWorker::getWechatUserId, map.get("UserID")).eq(SopRuleWorker::getCorpId, corpId).count() < 1)
//                return;
//            //查询用户得规则得任务
//            AdminCompanyWechat adminCompanyWechat = adminCompanyWechatService.getOne(Wrappers.<AdminCompanyWechat>lambdaQuery().eq(AdminCompanyWechat::getCorpId, map.get("ToUserName")).last("limit 1"));
//            List<SopRuleWorker> sopRuleWorkers = sopRuleWorkerService.lambdaQuery().eq(SopRuleWorker::getWechatUserId, map.get("UserID")).eq(SopRuleWorker::getCorpId, corpId).list();
//            sopRuleWorkers.stream().forEach(item -> {
//                List<SopRuleTask> list = sopRuleTaskService.lambdaQuery().eq(SopRuleTask::getRuleId, item.getRuleId()).list();
//                List<SopRule> sopRules = sopRuleService.lambdaQuery().eq(SopRule::getId, item.getRuleId()).eq(SopRule::getStatus, 1).list();
//
//                for (SopRuleTask sopRuleTask : list) {
//                    if (CollectionUtils.isEmpty(sopRules))
//                        break;
//                    SopPushTask one = sopPushTaskService.getOne(Wrappers.<SopPushTask>lambdaQuery().eq(SopPushTask::getId, sopRuleTask.getTaskId()).eq(SopPushTask::getStatus, 1).last("limit 1"));
//                    if (!one.getType().equals(1))
//                        continue;
//
//                    Job job = new Job();
//                    job.setId(UUID.randomUUID().toString());
//                    if (one.getPushType().equals(2)) {
//                        try {
//                            job.setDelayTime(delayTime(one.getPushDay(), (one.getPushDayMin()) + 10) * 1000);
//                        } catch (ParseException e) {
//                            e.printStackTrace();
//                            log.error("添加客户时间计算错误");
//                        }
//                    } else {
//                        job.setDelayTime(((one.getPushMin() * 60) + 10) * 1000);
//                    }
//
//                    Map<String, Object> taskMap = new HashMap<>();
//                    taskMap.put("id", one.getId());
//                    taskMap.put("toUser", map.get("UserID"));
//                    taskMap.put("externalUserID", map.get("ExternalUserID"));
//                    taskMap.put("companyName", adminCompanyWechat.getCompanyName());
//                    taskMap.put("companyWechatId", adminCompanyWechat.getCompanyWechatId());
//                    taskMap.put("agentId1", adminCompanyWechat.getAgentId1());
//
//
//                    job.setTtrTime(DelayConfig.PROCESS_TIME);
//                    job.setTopic(JobTopic.TOPIC_SOP.getTopic());
//                    job.setStatus(JobStatus.DELAY);
//                    job.setRetryCount(DelayConfig.RETRY_COUNT);
//
//                    SopWorkerPush sopWorkerPush = new SopWorkerPush();
//                    List<ExternalUser> externalUserID = externalUserService.lambdaQuery().eq(ExternalUser::getExternalUserId, Convert.toStr(map.get("ExternalUserID"))).list();
//                    if (!CollectionUtils.isEmpty(externalUserID)) {
//                        sopWorkerPush.setExternalName(externalUserID.get(0).getName());
//                    }
//                    sopWorkerPush.setExternalUserId(Convert.toStr(map.get("ExternalUserID")));
//                    sopWorkerPush.setWechatUserId(Convert.toStr(map.get("UserID")));
//                    List<EnterpriseWechatWorker> userID = enterpriseWechatWorkerService.lambdaQuery().eq(EnterpriseWechatWorker::getWechatUserId, Convert.toStr(map.get("UserID")))
//                            .eq(EnterpriseWechatWorker::getCorpId, adminCompanyWechat.getCorpId()).list();
//                    if (!CollectionUtils.isEmpty(externalUserID)) {
//                        sopWorkerPush.setWechatName(userID.get(0).getName());
//                    }
//                    sopWorkerPush.setWechatName(Convert.toStr(map.get("UserID")));
//                    sopWorkerPush.setCorpId(adminCompanyWechat.getCorpId());
//                    sopWorkerPush.setTaskTime(DateUtil.date(System.currentTimeMillis() + job.getDelayTime()));
//                    sopWorkerPush.setWorkerStatus(0);
//                    sopWorkerPush.setCreateTime(new Date());
//                    sopWorkerPush.setUpdateTime(new Date());
//                    sopWorkerPush.setTaskId(sopRuleTask.getTaskId());
//                    sopWorkerPush.setRuleId(sopRuleTask.getRuleId());
//                    sopWorkerPush.setCompanyWechatId(adminCompanyWechat.getCompanyWechatId());
//                    sopWorkerPush.setCompanyName(adminCompanyWechat.getCompanyName());
//                    sopWorkerPushService.save(sopWorkerPush);
//
//                    taskMap.put("pushWorkerId", sopWorkerPush.getId());
//                    job.setMessage(JSONUtil.toJsonStr(taskMap));
//                    //jobLogic.addDefJob(job);
//
//                }
//
//            });
//        }
//    }
//
//    public void sendTag(Map<String, Object> map) {
//        //查询用户是否关联了规则
//        AdminCompanyWechat adminCompanyWechat = adminCompanyWechatService.getOne(Wrappers.<AdminCompanyWechat>lambdaQuery().eq(AdminCompanyWechat::getCorpId, map.get("ToUserName")).last("limit 1"));
//        if (sopRuleWorkerService.lambdaQuery().eq(SopRuleWorker::getWechatUserId, map.get("UserID")).eq(SopRuleWorker::getCorpId, adminCompanyWechat.getCorpId()).count() < 1)
//            return;
//        List<SopRuleWorker> sopRuleWorkers = sopRuleWorkerService.lambdaQuery().eq(SopRuleWorker::getWechatUserId, map.get("UserID")).eq(SopRuleWorker::getCorpId, adminCompanyWechat.getCorpId()).list();
//        sopRuleWorkers.stream().forEach(item -> {
//            List<SopRule> sopRules = sopRuleService.lambdaQuery().eq(SopRule::getId, item.getRuleId()).eq(SopRule::getStatus, 1).list();
//            List<SopRuleTask> list = sopRuleTaskService.lambdaQuery().eq(SopRuleTask::getRuleId, item.getRuleId()).list();
//            for (SopRuleTask sopRuleTask : list) {
//                if (CollectionUtils.isEmpty(sopRules))
//                    break;
//                SopPushTask one = sopPushTaskService.getOne(Wrappers.<SopPushTask>lambdaQuery().eq(SopPushTask::getId, sopRuleTask.getTaskId()).eq(SopPushTask::getStatus, 1).last("limit 1"));
//                if (!one.getType().equals(2))
//                    continue;
//                //查关联标签的，对比是否客户添加了此标签
//                List<SopPushTaskTag> list1 = sopPushTaskTagService.lambdaQuery().eq(SopPushTaskTag::getTaskId, sopRuleTask.getTaskId()).list();
//                Integer count = 0;
//                for (SopPushTaskTag sopPushTaskTag : list1) {
//                    try {
//                        WxCpExternalContactInfo wxCpExternalContactInfo = workWechatLogic.getCustomerDetail(Convert.toStr(map.get("ExternalUserID")), adminCompanyWechat);
//
//                        if (count > 0)
//                            break;
//                        for (FollowedUser followedUser : wxCpExternalContactInfo.getFollowedUsers()) {
//                            if (!followedUser.getUserId().equals(map.get("UserID")))
//                                continue;
//                            //用户拥有的标签
//                            FollowedUser.Tag[] tags = followedUser.getTags();
//
//                            for (FollowedUser.Tag tagId : tags) {
//                                //判断标签是否一致
//                                if (!sopPushTaskTag.getTagId().equals(tagId.getTagId()))
//                                    continue;
//
//                                //找不到用户的此标签发送消息
//                                List<UserTag> list2 = userTagService.lambdaQuery().eq(UserTag::getUserid, map.get("UserID")).eq(UserTag::getExternalUserId, map.get("ExternalUserID")).eq(UserTag::getTagId, tagId.getTagId()).list();
//                                if (!CollectionUtils.isEmpty(list2))
//                                    continue;
//
//                                if (count > 0)
//                                    continue;
//
//                                Job job = new Job();
//                                job.setId(UUID.randomUUID().toString());
//                                if (one.getPushType().equals(2)) {
//                                    try {
//                                        job.setDelayTime(delayTime(one.getPushDay(), (one.getPushDayMin()) + 10) * 1000);
//                                    } catch (ParseException e) {
//                                        e.printStackTrace();
//                                        log.error("添加客户时间计算错误");
//                                    }
//                                } else {
//                                    job.setDelayTime(((one.getPushMin() * 60) + 10) * 1000);
//                                }
//
//                                Map<String, Object> taskMap = new HashMap<>();
//                                taskMap.put("id", one.getId());
//                                taskMap.put("toUser", map.get("UserID"));
//                                taskMap.put("externalUserID", map.get("ExternalUserID"));
//                                taskMap.put("companyName", adminCompanyWechat.getCompanyName());
//                                taskMap.put("companyWechatId", adminCompanyWechat.getCompanyWechatId());
//                                taskMap.put("agentId1", adminCompanyWechat.getAgentId1());
//
//
//                                job.setTtrTime(DelayConfig.PROCESS_TIME);
//                                job.setTopic(JobTopic.TOPIC_SOP.getTopic());
//                                job.setStatus(JobStatus.DELAY);
//                                job.setRetryCount(DelayConfig.RETRY_COUNT);
//
//                                SopWorkerPush sopWorkerPush = new SopWorkerPush();
//                                List<ExternalUser> externalUserID = externalUserService.lambdaQuery().eq(ExternalUser::getExternalUserId, Convert.toStr(map.get("ExternalUserID"))).list();
//                                if (!CollectionUtils.isEmpty(externalUserID)) {
//                                    sopWorkerPush.setExternalName(externalUserID.get(0).getName());
//                                }
//                                sopWorkerPush.setExternalUserId(Convert.toStr(map.get("ExternalUserID")));
//                                sopWorkerPush.setWechatUserId(Convert.toStr(map.get("UserID")));
//                                List<EnterpriseWechatWorker> userID = enterpriseWechatWorkerService.lambdaQuery().eq(EnterpriseWechatWorker::getWechatUserId, Convert.toStr(map.get("UserID")))
//                                        .eq(EnterpriseWechatWorker::getCorpId, adminCompanyWechat.getCorpId()).list();
//                                if (!CollectionUtils.isEmpty(externalUserID)) {
//                                    sopWorkerPush.setWechatName(userID.get(0).getName());
//                                }
//                                sopWorkerPush.setWechatName(Convert.toStr(map.get("UserID")));
//                                sopWorkerPush.setCorpId(adminCompanyWechat.getCorpId());
//                                sopWorkerPush.setTaskTime(DateUtil.date(System.currentTimeMillis() + job.getDelayTime()));
//                                sopWorkerPush.setWorkerStatus(0);
//                                sopWorkerPush.setCreateTime(new Date());
//                                sopWorkerPush.setUpdateTime(new Date());
//                                sopWorkerPush.setTaskId(sopRuleTask.getTaskId());
//                                sopWorkerPush.setRuleId(sopRuleTask.getRuleId());
//                                sopWorkerPush.setCompanyWechatId(adminCompanyWechat.getCompanyWechatId());
//                                sopWorkerPush.setCompanyName(adminCompanyWechat.getCompanyName());
//                                sopWorkerPushService.save(sopWorkerPush);
//
//                                taskMap.put("pushWorkerId", sopWorkerPush.getId());
//                                job.setMessage(JSONUtil.toJsonStr(taskMap));
//                                //jobLogic.addDefJob(job);
//                                count ++ ;
//
//                            }
//                        }
//                    } catch (WxErrorException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//    }
//
//    public Long delayTime(Integer pushDay, String pushDayMin) throws ParseException {
//        Date date = DateUtils.getTimeByDay(pushDay);
//        String s1 = DateUtils.formatDate1(date);
//        String s2 = s1 + " " + pushDayMin;
//        ;
//        Date date1 = DateUtils.parseDate(s2);
//        Long gs = DateUtil.between(new Date(), date1, DateUnit.SECOND);
//        return gs;
//    }
//
//    public void addCompanyToken(String token, String encodingAES_key, HttpServletRequest request) {
//
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        AdminCompanyWechat adminCompanyWechat = new AdminCompanyWechat();
//        adminCompanyWechat.setCompanyWechatId(adminUser.getCompanyWechatId());
//        adminCompanyWechat.setToken(token);
//        adminCompanyWechat.setEncodingaesKey(encodingAES_key);
//        adminCompanyWechatService.updateById(adminCompanyWechat);
//    }
//
//    public AdminCompanyWechat detailCompany(HttpServletRequest request) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        return adminCompanyWechatService.getById(adminUser.getCompanyWechatId());
//    }
//
//    public void delete(String id) {
//        sopRuleService.removeById(id);
//    }
//
//    public void addKeFu(SopRuleAdd sopRuleAdd, HttpServletRequest request) {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//        //添加员工
//        List<SopRuleWorker> sopRuleTaskList = new ArrayList<>();
//        for (String worker : sopRuleAdd.getWorkerIds()) {
//            EnterpriseWechatWorker byId = enterpriseWechatWorkerService.getById(worker);
//            List<SopRuleWorker> list = sopRuleWorkerService.lambdaQuery().eq(SopRuleWorker::getRuleId, sopRuleAdd.getId()).eq(SopRuleWorker::getWechatUserId, byId.getWechatUserId()).list();
//            if (!CollectionUtils.isEmpty(list)) {
//                continue;
//            }
//            SopRuleWorker sopRuleWorker = new SopRuleWorker();
//            sopRuleWorker.setWechatUserId(byId.getWechatUserId());
//            sopRuleWorker.setCorpId(adminUser.getCorpId());
//            sopRuleWorker.setRuleId(sopRuleAdd.getId());
//            sopRuleTaskList.add(sopRuleWorker);
//        }
//        sopRuleWorkerService.saveBatch(sopRuleTaskList);
//    }
//}
