银收客 模板消息修改
This commit is contained in:
@@ -21,7 +21,7 @@ wx:
|
|||||||
appId: wx212769170d2c6b2a
|
appId: wx212769170d2c6b2a
|
||||||
secrete: 8492a7e8d55bbb1b57f5c8276ea1add0
|
secrete: 8492a7e8d55bbb1b57f5c8276ea1add0
|
||||||
operationMsgTmpId: wFdoUG-dUT7bDRHq8bMJD9CF5TjyH9x_uJQgQByZqHg
|
operationMsgTmpId: wFdoUG-dUT7bDRHq8bMJD9CF5TjyH9x_uJQgQByZqHg
|
||||||
warnMsgTmpId: C08OUr80x6wGmUN1zpFhSQ3Sv7VF5vksdZigiEx2pD0
|
warnMsgTmpId: C08OUr80x6wGmUN1zpFhSZyFA2G6b9_jiZgEppzLB70
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -53,18 +53,6 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
|
|||||||
@Resource
|
@Resource
|
||||||
private WxAccountUtil wxAccountUtil;
|
private WxAccountUtil wxAccountUtil;
|
||||||
|
|
||||||
private QueryWrapper buildQueryWrapper(ConsStockFlowDTO param) {
|
|
||||||
QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
|
|
||||||
/*if (StrUtil.isNotEmpty(param.getName())) {
|
|
||||||
queryWrapper.like(ConsStockFlow::getName, param.getName());
|
|
||||||
}*/
|
|
||||||
Long shopId = StpKit.USER.getShopId(0L);
|
|
||||||
queryWrapper.eq(ConsStockFlow::getShopId, shopId);
|
|
||||||
queryWrapper.orderBy(ConsStockFlow::getId, false);
|
|
||||||
return queryWrapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ConsInOutStockHeadParam inStock(ConsInOutStockHeadParam param) {
|
public ConsInOutStockHeadParam inStock(ConsInOutStockHeadParam param) {
|
||||||
@@ -72,7 +60,6 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
|
|||||||
Long createUserId = StpKit.USER.getLoginIdAsLong();
|
Long createUserId = StpKit.USER.getLoginIdAsLong();
|
||||||
String createUserName = StpKit.USER.getAccount();
|
String createUserName = StpKit.USER.getAccount();
|
||||||
ConsStockFlow head = BeanUtil.copyProperties(param, ConsStockFlow.class);
|
ConsStockFlow head = BeanUtil.copyProperties(param, ConsStockFlow.class);
|
||||||
List<ConsStockFlow> entityList = BeanUtil.copyToList(param.getBodyList(), ConsStockFlow.class);
|
|
||||||
List<ConsStockFlow> insertList = new ArrayList<>();
|
List<ConsStockFlow> insertList = new ArrayList<>();
|
||||||
List<ConsInfo> updateStockList = new ArrayList<>();
|
List<ConsInfo> updateStockList = new ArrayList<>();
|
||||||
for (ConsInOutStockBodyParam entity : param.getBodyList()) {
|
for (ConsInOutStockBodyParam entity : param.getBodyList()) {
|
||||||
@@ -251,15 +238,7 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
|
|||||||
Long shopId = entity.getShopId();
|
Long shopId = entity.getShopId();
|
||||||
BigDecimal afterNumber = entity.getAfterNumber();
|
BigDecimal afterNumber = entity.getAfterNumber();
|
||||||
ConsInfo consInfo = consInfoMapper.selectOneById(entity.getConId());
|
ConsInfo consInfo = consInfoMapper.selectOneById(entity.getConId());
|
||||||
String shopName = "";
|
String shopName = productMapper.getShopName(shopId);
|
||||||
try {
|
|
||||||
shopName = StpKit.USER.getShopName();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("获取店铺名称失败");
|
|
||||||
}
|
|
||||||
if (StrUtil.isEmpty(shopName)) {
|
|
||||||
shopName = productMapper.getShopName(shopId);
|
|
||||||
}
|
|
||||||
BigDecimal conWarning = consInfo.getConWarning();
|
BigDecimal conWarning = consInfo.getConWarning();
|
||||||
// 库存小于警告值,发送消息提醒
|
// 库存小于警告值,发送消息提醒
|
||||||
if (NumberUtil.isLess(afterNumber, conWarning)) {
|
if (NumberUtil.isLess(afterNumber, conWarning)) {
|
||||||
@@ -268,12 +247,8 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String conName = StrUtil.format("{}数量<预警值{}", consInfo.getConName(), conWarning);
|
String conName = StrUtil.format("{}数量<预警值{}", consInfo.getConName(), conWarning);
|
||||||
String finalShopName = shopName;
|
ThreadUtil.execAsync(() -> openIdList.parallelStream().forEach(openId ->
|
||||||
ThreadUtil.execAsync(() -> {
|
wxAccountUtil.sendStockMsg("耗材库存预警", shopName, conName, afterNumber, openId)));
|
||||||
openIdList.parallelStream().forEach(openId -> {
|
|
||||||
wxAccountUtil.sendStockMsg(finalShopName, conName, afterNumber, openId);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,12 +53,8 @@ public class ProductStockFlowServiceImpl extends ServiceImpl<ProductStockFlowMap
|
|||||||
}
|
}
|
||||||
String productName = StrUtil.sub(product.getName(), 0, 10).concat("...");
|
String productName = StrUtil.sub(product.getName(), 0, 10).concat("...");
|
||||||
String conName = StrUtil.format("{}数量<预警值{}", productName, warnLine);
|
String conName = StrUtil.format("{}数量<预警值{}", productName, warnLine);
|
||||||
String finalShopName = shopName;
|
ThreadUtil.execAsync(() -> openIdList.parallelStream().forEach(openId ->
|
||||||
ThreadUtil.execAsync(() -> {
|
wxAccountUtil.sendStockMsg("商品库存预警", shopName, conName, afterNumber, openId)));
|
||||||
openIdList.parallelStream().forEach(openId -> {
|
|
||||||
wxAccountUtil.sendStockMsg(finalShopName, conName, afterNumber, openId);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.czg.exception.CzgException;
|
||||||
import com.czg.service.RedisService;
|
import com.czg.service.RedisService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -16,7 +17,6 @@ import org.springframework.context.annotation.Lazy;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -32,6 +32,7 @@ import java.util.Map;
|
|||||||
@Component
|
@Component
|
||||||
public class WxAccountUtil {
|
public class WxAccountUtil {
|
||||||
|
|
||||||
|
//银收客公众号推送
|
||||||
@Value("${wx.ysk.appId}")
|
@Value("${wx.ysk.appId}")
|
||||||
private String appId = "wx212769170d2c6b2a";
|
private String appId = "wx212769170d2c6b2a";
|
||||||
@Value("${wx.ysk.secrete}")
|
@Value("${wx.ysk.secrete}")
|
||||||
@@ -59,84 +60,73 @@ public class WxAccountUtil {
|
|||||||
linkedHashMap.put("47003", "参数错误");
|
linkedHashMap.put("47003", "参数错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAccessToken() {
|
|
||||||
String accessToken = Convert.toStr(redisService.get("accessToken"));
|
|
||||||
if (StrUtil.isNotEmpty(accessToken)) {
|
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete));
|
|
||||||
JSONObject respInfo = JSONObject.parseObject(resp);
|
|
||||||
if (!respInfo.containsKey("access_token")) {
|
|
||||||
log.warn("公众号获取token失败, 响应内容: {}", resp);
|
|
||||||
throw new RuntimeException(resp);
|
|
||||||
}
|
|
||||||
accessToken = respInfo.getString("access_token");
|
|
||||||
int expiresIn = respInfo.getInteger("expires_in");
|
|
||||||
redisService.set("accessToken", accessToken, expiresIn - 10);
|
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject sendTemplateMsg(String templateId, String toUserOpenId, Map<String, Object> data) {
|
|
||||||
log.info("开始发送微信模板消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data);
|
|
||||||
String accessToken = getAccessToken();
|
|
||||||
|
|
||||||
JSONObject object1 = new JSONObject();
|
|
||||||
|
|
||||||
object1.put("template_id", templateId);
|
|
||||||
object1.put("touser", toUserOpenId);
|
|
||||||
object1.put("data", data);
|
|
||||||
|
|
||||||
String response = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".concat(accessToken)).body(object1.toString()).execute().body();
|
|
||||||
log.info("微信模板消息发送成功,响应内容:{}", response);
|
|
||||||
JSONObject resObj = JSONObject.parseObject(response);
|
|
||||||
if (ObjectUtil.isNotEmpty(resObj) && ObjectUtil.isNotNull(resObj) && "0".equals(resObj.get("errcode") + "")) {
|
|
||||||
return resObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new RuntimeException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendOperationMsg(List<String> openIdList, String userName, String operationDesc) {
|
public void sendOperationMsg(List<String> openIdList, String userName, String operationDesc) {
|
||||||
openIdList.forEach(openId -> {
|
openIdList.forEach(openId -> {
|
||||||
Map<String, Object> data = new HashMap<String, Object>() {{
|
Map<String, Object> data = Map.of(
|
||||||
put("thing19", new HashMap<String, Object>() {{
|
"thing19", Map.of("value", userName),
|
||||||
put("value", userName);
|
"thing8", Map.of("value", StrUtil.sub(operationDesc, 0, 20)),
|
||||||
}});
|
"time21", Map.of("value", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"))
|
||||||
put("thing8", new HashMap<String, Object>() {{
|
);
|
||||||
put("value", StrUtil.sub(operationDesc,0,20));
|
|
||||||
}});
|
|
||||||
put("time21", new HashMap<String, Object>() {{
|
|
||||||
put("value", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
|
|
||||||
}});
|
|
||||||
}};
|
|
||||||
log.info("开始发送敏感操作消息, 接收用户openId: {}, 操作用户: {}, 操作描述: {}", openId, userName, operationDesc);
|
|
||||||
try {
|
try {
|
||||||
sendTemplateMsg(operationMsgTmpId, openId, data);
|
sendTemplateMsg("敏感操作", operationMsgTmpId, openId, data);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("发送失败, openId: {}, 响应: {}", openId, e.getMessage());
|
log.error("发送失败, openId: {}, 响应: {}", openId, e.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendStockMsg(String shopName, String productName, BigDecimal stockNum, String openId) {
|
public void sendStockMsg(String typeName, String shopName, String productName, BigDecimal stockNum, String openId) {
|
||||||
String stockNumStr = stockNum.toPlainString();
|
String stockNumStr = stockNum.toPlainString();
|
||||||
Map<String, Object> data = new HashMap<String, Object>() {{
|
Map<String, Object> data = Map.of(
|
||||||
put("thing22", new HashMap<String, Object>() {{
|
//品名
|
||||||
put("value", shopName);
|
"thing34", Map.of("value", typeName),
|
||||||
}});
|
//商品名称
|
||||||
put("thing4", new HashMap<String, Object>() {{
|
"thing4", Map.of("value", productName),
|
||||||
put("value", productName);
|
//商品数量
|
||||||
}});
|
"number5", Map.of("value", stockNumStr),
|
||||||
put("number5", new HashMap<String, Object>() {{
|
//商家名称
|
||||||
put("value", stockNumStr);
|
"thing22", Map.of("value", shopName)
|
||||||
}});
|
);
|
||||||
}};
|
|
||||||
log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", openId, data);
|
|
||||||
try {
|
try {
|
||||||
sendTemplateMsg(warnMsgTmpId, openId, data);
|
sendTemplateMsg("库存预警", warnMsgTmpId, openId, data);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("发送失败, openId:{}, msg: {}", openId, e.getMessage());
|
log.error("发送失败, openId:{}, msg: {}", openId, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getAccessToken() {
|
||||||
|
synchronized (this) {
|
||||||
|
String accessToken = Convert.toStr(redisService.get("accessToken"));
|
||||||
|
if (StrUtil.isNotEmpty(accessToken)) {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete));
|
||||||
|
JSONObject respInfo = JSONObject.parseObject(resp);
|
||||||
|
if (!respInfo.containsKey("access_token")) {
|
||||||
|
log.warn("公众号获取token失败, 响应内容: {}", resp);
|
||||||
|
throw new RuntimeException(resp);
|
||||||
|
}
|
||||||
|
accessToken = respInfo.getString("access_token");
|
||||||
|
int expiresIn = respInfo.getInteger("expires_in");
|
||||||
|
redisService.set("accessToken", accessToken, expiresIn - 10);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject sendTemplateMsg(String detail, String templateId, String toUserOpenId, Map<String, Object> data) {
|
||||||
|
log.info("银收客公众号消息推送,消息类型:{} 接收用户openId: {}, 消息数据: {}", detail, toUserOpenId, data);
|
||||||
|
String accessToken = getAccessToken();
|
||||||
|
JSONObject object1 = new JSONObject();
|
||||||
|
object1.put("template_id", templateId);
|
||||||
|
object1.put("touser", toUserOpenId);
|
||||||
|
object1.put("data", data);
|
||||||
|
|
||||||
|
String response = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".concat(accessToken)).body(object1.toString()).execute().body();
|
||||||
|
log.info("银收客公众号消息推送,响应内容:{}", response);
|
||||||
|
JSONObject resObj = JSONObject.parseObject(response);
|
||||||
|
if (ObjectUtil.isNotEmpty(resObj) && ObjectUtil.isNotNull(resObj) && "0".equals(resObj.get("errcode") + "")) {
|
||||||
|
return resObj;
|
||||||
|
}
|
||||||
|
throw new CzgException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user