cuilei
2025-07-11 bab67d03c5d77190001f2f06b2104b12dbfcbcdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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.WeekMaintenanceStatusEnum;
import org.jeecg.modules.eam.entity.EamWeekMaintenanceOrder;
import org.jeecg.modules.eam.service.IEamWeekMaintenanceOrderService;
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.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.ThirdAppWechatEnterpriseServiceImpl;
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.stream.Collectors;
 
/**
 * 待领取周保工单企业微信消息推送定时任务
 */
@Slf4j
@Component
public class WeekMaintenanceOrderWxMessageSendJob implements Job {
 
    @Autowired
    private IEamWeekMaintenanceOrderService eamWeekMaintenanceOrderService;
    @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<QuartzJob> byJobClassName = quartzJobService.findByJobClassName(this.getClass().getName());
        if (byJobClassName != null && !byJobClassName.isEmpty()) {
            quartzLog.setJobId(byJobClassName.get(0).getId());
        }
        long startTime = System.currentTimeMillis();
        try {
            //查询所有待保养的周保工单
            List<EamWeekMaintenanceOrder> waitOrderList = eamWeekMaintenanceOrderService.list(new LambdaQueryWrapper<EamWeekMaintenanceOrder>()
                    .eq(EamWeekMaintenanceOrder::getMaintenanceStatus, WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name())
                    .eq(EamWeekMaintenanceOrder::getDelFlag, CommonConstant.DEL_FLAG_0));
            if (!waitOrderList.isEmpty()) {
                //有待保养的工单,推送企业微信消息
                //查询所有操作工
                List<SysUser> userList = sysUserService.list(new LambdaQueryWrapper<SysUser>()
                        .eq(SysUser::getPost, BusinessCodeConst.PCR0001)
                        .eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0)
                        .eq(SysUser::getStatus, CommonConstant.USER_UNFREEZE));
                if (userList.isEmpty()) {
                    log.error("系统未设置操作工用户,无法推送工单领取提醒消息");
                    quartzLog.setIsSuccess(0);
                    quartzLog.setParams("系统未设置操作工用户,无法推送工单领取提醒消息");
                } else {
                    TemplateCard templateCard = new TemplateCard();
                    String toUsers = userList.stream().map(SysUser::getUsername).collect(Collectors.joining(","));
                    String touser = wechatEnterpriseService.getTouser(toUsers, false);
                    templateCard.setTouser(touser);
                    TemplateCardEntity templateCardEntity = new TemplateCardEntity();
                    templateCard.setTemplate_card(templateCardEntity);
                    templateCardEntity.setTask_id(waitOrderList.get(0).getId());
                    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());
                        quartzLog.setIsSuccess(0);
                        quartzLog.setParams(jsonObject.toJSONString());
                    } else {
                        log.error("推送待领取周保工单企业微信消息失败,{}", jsonObject.toJSONString());
                        quartzLog.setIsSuccess(-1);
                        quartzLog.setExceptionDetail(jsonObject.toJSONString());
                    }
                }
            } 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);
    }
}