package org.jeecg.modules.eam.job; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.util.DateUtils; import org.jeecg.modules.eam.constant.BusinessCodeConst; import org.jeecg.modules.eam.constant.InspectionStatus; import org.jeecg.modules.eam.entity.EamInspectionOrder; import org.jeecg.modules.eam.service.IEamInspectionOrderService; import org.jeecg.modules.mdc.service.IMdcProductionEquipmentService; import org.jeecg.modules.mdc.util.ThrowableUtil; import org.jeecg.modules.quartz.entity.QuartzJob; import org.jeecg.modules.quartz.entity.SysQuartzLog; import org.jeecg.modules.quartz.service.IQuartzJobService; import org.jeecg.modules.quartz.service.ISysQuartzLogService; import org.jeecg.modules.qywx.message.vo.TemplateCard; import org.jeecg.modules.qywx.message.vo.TemplateCardEntity; import org.jeecg.modules.system.entity.MdcProductionEquipment; import org.jeecg.modules.system.entity.SysUser; import org.jeecg.modules.system.service.ISysUserService; import org.jeecg.modules.system.service.impl.ThirdAppWechatEnterpriseServiceImpl; import org.jeecg.modules.system.vo.UserSelector; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; /** * 待领取点检工单企业微信消息推送定时任务 */ @Slf4j @Component public class InspectionOrderWxMessageSendJob implements Job { @Autowired private IEamInspectionOrderService eamInspectionOrderService; @Autowired private ISysUserService sysUserService; @Autowired private ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService; @Autowired private ISysQuartzLogService sysQuartzLogService; @Autowired private IQuartzJobService quartzJobService; @Value("${wechatEnterprise.cardActionUrl}") private String cardActionUrl; @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { log.info("待点检工单企业微信消息推送任务开始执行,时间:{}", DateUtils.now()); //任务日志 SysQuartzLog quartzLog = new SysQuartzLog(); quartzLog.setCreateTime(new Date()); List byJobClassName = quartzJobService.findByJobClassName(this.getClass().getName()); if (byJobClassName != null && !byJobClassName.isEmpty()) { quartzLog.setJobId(byJobClassName.get(0).getId()); } long startTime = System.currentTimeMillis(); try { //查询所有未领取待点检的工单 List unReceivedOrderList = eamInspectionOrderService.selectUnReceivedInspectionOrder(); if (!unReceivedOrderList.isEmpty()) { //有待点检工单,推送企业微信消息 Map> unReceivedOrderMap = unReceivedOrderList.stream() .collect(Collectors.groupingBy(EamInspectionOrder::getOrgId)); StringBuilder message = new StringBuilder(); for (String orgId : unReceivedOrderMap.keySet()) { //查询该产线下的所有操作工 List userSelectorList = sysUserService.selectOperatorList(null, orgId, BusinessCodeConst.PCR0001); if (userSelectorList.isEmpty()) { log.error("系统未设置操作工用户,无法推送工单领取提醒消息"); message.append("系统未设置操作工用户,无法推送工单领取提醒消息").append("\n"); } else { String toUsers = userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.joining(",")); String touser = wechatEnterpriseService.getTouser(toUsers, false); //构造消息体内容 TemplateCard templateCard = new TemplateCard(); templateCard.setTouser(touser); TemplateCardEntity templateCardEntity = new TemplateCardEntity(); templateCard.setTemplate_card(templateCardEntity); templateCardEntity.setTask_id(UUID.randomUUID().toString().replaceAll("-", "")); TemplateCardEntity.MainTitle mainTitle = new TemplateCardEntity.MainTitle(); mainTitle.setTitle("设备点检"); templateCardEntity.setMain_title(mainTitle); templateCardEntity.setSub_title_text("您有待领取点检工单,请进入系统领取"); TemplateCardEntity.CardAction cardAction = new TemplateCardEntity.CardAction(); cardAction.setType(1); cardAction.setUrl(cardActionUrl); templateCardEntity.setCard_action(cardAction); JSONObject jsonObject = wechatEnterpriseService.sendTemplateCardMsg(templateCard, true); Integer errcode = (Integer) jsonObject.get("errcode"); if (errcode == 0) { log.info("推送待领取点检工单企业微信消息成功,{}", jsonObject.toJSONString()); message.append("推送待领取点检工单企业微信消息成功,").append(jsonObject.toJSONString()).append("\n"); } else { log.error("推送待领取点检工单企业微信消息失败,{}", jsonObject.toJSONString()); message.append("推送待领取点检工单企业微信消息失败,").append(jsonObject.toJSONString()).append("\n"); } } } quartzLog.setIsSuccess(0); quartzLog.setExceptionDetail(message.toString()); } else { log.info("无待领取点检工单,无需推送工单领取提醒消息"); quartzLog.setIsSuccess(0); quartzLog.setParams("无待领取点检工单,无需推送工单领取提醒消息"); } } catch (Exception e) { log.error("待点检工单企业微信消息推送任务执行失败,{}", e.getMessage(), e); quartzLog.setIsSuccess(-1); quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e)); } long endTime = System.currentTimeMillis(); quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime))); sysQuartzLogService.save(quartzLog); } }