package com.shucha.signalnotification.biz.service.impl;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shucha.signalnotification.biz.constants.Constants;
import com.shucha.signalnotification.biz.dto.h5dto.MessageDTO.TroubleMessageDTO;
import com.shucha.signalnotification.biz.model.Subscribe;
import com.shucha.signalnotification.biz.model.UserInfo;
import com.shucha.signalnotification.biz.service.MessageService;
import com.shucha.signalnotification.biz.service.SubscribeService;
import com.shucha.signalnotification.biz.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private UserInfoService userInfoService;
@Autowired
private SubscribeService subscribeService;
@Override
public String sendMessage(Long transportationUnitId, TroubleMessageDTO troubleMessageDTO) {
List<UserInfo> userInfos = userInfoService.listSubscribe(transportationUnitId, troubleMessageDTO.getTemplateId());
List<String> wxcodes = userInfos.stream().map(UserInfo::getWxCode).collect(Collectors.toList());
String msg = "";
if(wxcodes.size()>0) {
for (String wxcode: wxcodes) {
JSONObject body = new JSONObject();
body.set("touser", wxcode);
body.set("template_id", troubleMessageDTO.getTemplateId());
body.set("miniprogram_state", "developer");
body.set("page", troubleMessageDTO.getPage());
JSONObject json = new JSONObject();
json.set("thing11", new JSONObject().set("value", troubleMessageDTO.getEquipmentLocation()));
json.set("thing1", new JSONObject().set("value", troubleMessageDTO.getFaultType()));
json.set("thing2", new JSONObject().set("value", troubleMessageDTO.getFaultDescribe()));
json.set("thing3", new JSONObject().set("value", troubleMessageDTO.getGeographicPeople()));
json.set("time5", new JSONObject().set("value",troubleMessageDTO.getGeographicTime()));
body.set("data", json);
msg = HttpUtil.post(Constants.OA_TEMPLATE_MSG_SEND.replace("${ACCESS_TOKEN}",troubleMessageDTO.getAccessToken()), body.toString());
log.info("msg="+msg);
JSONObject jsonObj = new JSONObject(msg);
String errcode = jsonObj.get("errcode").toString();
switch (errcode) {
case "43101":
subscribeService.update(Wrappers.<Subscribe>lambdaUpdate()
.eq(Subscribe::getWxOpenid, wxcode)
.set(Subscribe::getStatus, "reject")
.set(Subscribe::getUpdateTime, new Date()));
break;
default:
break;
}
}
}
return msg;
}
}
public static final String WX_TROUBLE_MSG_TEMPLATE_ID = "订阅消息模板ID";
public static final String WX_TROUBLE_MSG_JUMP_PATH = "pages/details/details?index=1&id=";
public static final String OA_TEMPLATE_MSG_SEND = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${ACCESS_TOKEN}";
HttpUtil是使用的微信小程序的jar
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>