业务代码
private final ThreadPoolTaskExecutor executorService = SpringUtils.getBean("threadPoolTaskExecutor");@Overridepublic void batchSendWxMsgByDiy(sendDiyWxMsgVo entity) {if (CollectionUtils.isEmpty(entity.getAcceptIds())) {throw new TipUserException("接收人不能为空");}if (StringUtils.isBlank(entity.getDataType())) {throw new TipUserException("类型不能为空");}List<TrainSignupMan> signupManList = signupManMapper.selectByIds(entity.getAcceptIds());if (CollectionUtils.isEmpty(signupManList)) {throw new TipUserException("人员删除或不可见");}Map<String, SysDictData> templateConfig = dictTypeService.selectDictDataByType("train_wx_learn_notice").stream().collect(Collectors.toMap(SysDictData::getDictLabel, o -> o));if (MapUtils.isEmpty(templateConfig)) {throw new TipUserException("请联系管理员配置微信消息模板-培训学习通知");}List<Long> lyUserIdIds = signupManList.stream().map(TrainSignupMan::getLyUserId).collect(Collectors.toList());if (CollectionUtils.isEmpty(lyUserIdIds)) {return;}Map<Long, TzLyUser> tzLyUserMap = tzLyUserService.selectOpenIdListByUserIds(lyUserIdIds).stream().collect(Collectors.toMap(TzLyUser::getId, o -> o));if (MapUtils.isEmpty(tzLyUserMap)) {return;}String className = entity.getClassName();String dataTip = null;Date dataTime = null;if (StringUtils.equals(entity.getDataType(), "diy")) {dataTip = entity.getDataTip();dataTime = entity.getDataTime();if (StringUtils.isBlank(dataTip)) {throw new TipUserException("温馨提示不能为空");}if (ObjectUtils.isEmpty(dataTime)) {throw new TipUserException("时间不能为空");}} else if (StringUtils.equals(entity.getDataType(), "dict")) {dataTime = DateUtils.getNowDate();SysDictData defaultSysDictData = templateConfig.get("default");SysDictData trainSysDictData = templateConfig.get(entity.getTrainingCourseId());if (ObjectUtils.isEmpty(defaultSysDictData)) {throw new TipUserException("请配置该培训班-培训学习通知默认模板");}dataTip = defaultSysDictData.getDictValue();if (ObjectUtils.isNotEmpty(trainSysDictData)) {dataTip = trainSysDictData.getDictValue();}} else {throw new TipUserException("请携带类型参数");}final String finalDataTip = dataTip;final Date finalDataTime = dataTime;ConcurrentLinkedQueue<SmsPreventBody> smsPreventBodyQueue = new ConcurrentLinkedQueue<>();CountDownLatch latch = new CountDownLatch(signupManList.size());for (TrainSignupMan signupMan : signupManList) {TzLyUser tzLyUser = tzLyUserMap.get(signupMan.getLyUserId());if (ObjectUtils.isEmpty(tzLyUser)) {latch.countDown();} else {try {executorService.execute(()->{SmsPreventBody smsPreventBody = sendNoticeWxMsg(signupMan, templateConfig, tzLyUser.getOpenId(), className, finalDataTip, finalDataTime);smsPreventBodyQueue.add(smsPreventBody);latch.countDown();});} catch (Exception e) {latch.countDown();}}}try {latch.await();} catch (InterruptedException e) {log.error(e.getMessage());}executorService.shutdown();List<SmsPreventBody> smsPreventBodyList = new ArrayList<>(smsPreventBodyQueue);smsPreventService.saveBatch(smsPreventBodyList);}
微信发送消息工具类
private SmsPreventBody sendNoticeWxMsg(TrainSignupMan signupMan, Map<String, SysDictData> templateConfig, String openId, String className, String dataTip, Date dataTime) {String env_version = ConfigUtils.getString("wx.mp.is.env.version", "release");SendMessageTemplateVo messageTemplateVo = new SendMessageTemplateVo();messageTemplateVo.setMiniprogram_state(env_version);messageTemplateVo.setTemplate_id(templateConfig.get("template_id").getDictValue());messageTemplateVo.setPage(templateConfig.get("page").getDictValue());messageTemplateVo.setTouser(openId);messageTemplateVo.putParam("thing1", className);String dateFormat = DateUtil.format(dataTime, "yyyy年MM月dd日");messageTemplateVo.putParam("time2", dateFormat);messageTemplateVo.putParam("thing3", dataTip);SmsPreventBody smsPreventBody = new SmsPreventBody();smsPreventBody.setMobile(signupMan.getTel());smsPreventBody.setType("train-notice");smsPreventBody.setTipStr("微信订阅消息-培训学习通知");com.alibaba.fastjson.JSONObject jsonObject = WeChatUtils.sendWxMessage(messageTemplateVo);String errcode = jsonObject.getString("errcode");String errmsg = jsonObject.getString("errmsg");if (StringUtils.equals(errcode, "0")) {smsPreventBody.setState("success");smsPreventBody.setContent(errmsg);} else {smsPreventBody.setState("error");smsPreventBody.setContent(errcode + ":" + errmsg);}return smsPreventBody;}
消息实体
package com.huida.tzly.domain.vo;import com.alibaba.fastjson.JSONObject;
import lombok.Data;import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@Data
public class SendMessageTemplateVo implements Serializable {private String touser;private String template_id;private String page;private JSONObject data = new JSONObject();private String miniprogram_state;
public void putParam(String key, String value) {Map<String, Object> temp = new HashMap<>();temp.put("value", value);this.data.put(key, temp);}}