diff --git a/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/RedisKeyConstants.java b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/RedisKeyConstants.java new file mode 100644 index 0000000..2d90bd2 --- /dev/null +++ b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/RedisKeyConstants.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.message.constants; + +/** + * redis中所用到的key常量池 + * @author hx + */ +public interface RedisKeyConstants { + + /**消息推送redis键值**/ + String MESSAGE_SMS_PUSH_KEY = "hzims:sms:message:messagePushKey"; + +} diff --git a/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/SmsConstants.java b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/SmsConstants.java new file mode 100644 index 0000000..5649811 --- /dev/null +++ b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/SmsConstants.java @@ -0,0 +1,13 @@ +package com.hnac.hzims.message.constants; + +/** + * @author hx + */ +public interface SmsConstants { + + /**即时推送**/ + String PUSH_TYPE_IMMEDIATE = "0"; + /**延时推送**/ + String PUSH_TYPE_DELAY = "1"; + +} diff --git a/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/XxlJobConstants.java b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/XxlJobConstants.java new file mode 100644 index 0000000..a1ef43d --- /dev/null +++ b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/constants/XxlJobConstants.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.message.constants; + +/** + * 任务调度方法定义常量池 + * @author hx + */ +public interface XxlJobConstants { + + /**消息中心消息按通知人分时段集中推送**/ + String PUSH_SMS_MESSAGE_BY_NOTICE = "pushSmsMessageByNotice"; + +} diff --git a/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/PushMessageEntity.java b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/PushMessageEntity.java new file mode 100644 index 0000000..ca31bd9 --- /dev/null +++ b/hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/PushMessageEntity.java @@ -0,0 +1,50 @@ +package com.hnac.hzims.message.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springblade.core.mp.support.QueryField; +import org.springblade.core.mp.support.SqlCondition; +import org.springblade.core.tenant.mp.TenantEntity; +import org.springblade.core.tool.utils.DateUtil; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.time.LocalDate; +import java.time.LocalDateTime; + +/** + * @author hx + */ +@TableName("hzims_push_message") +@ApiModel("待推送消息列表") +@Data +public class PushMessageEntity extends TenantEntity implements Serializable { + + @ApiModelProperty("消息类型") + @QueryField(condition = SqlCondition.EQUAL) + private String messageType; + + @ApiModelProperty("业务key值") + @QueryField(condition = SqlCondition.EQUAL) + private String businessKey; + + @ApiModelProperty("消息推送内容体") + private String messageContent; + + @ApiModelProperty("消息是否推送") + @QueryField(condition = SqlCondition.EQUAL) + private Boolean isPush; + + @ApiModelProperty("是否推送成功") + @QueryField(condition = SqlCondition.EQUAL) + private Boolean isSuccess; + + @ApiModelProperty("推送时间") + @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME) + @JsonFormat(pattern = DateUtil.PATTERN_DATETIME) + private LocalDateTime pushTime; + +} diff --git a/hzims-service/message/src/main/java/com/hnac/hzims/message/dto/PushMessageDTO.java b/hzims-service/message/src/main/java/com/hnac/hzims/message/dto/PushMessageDTO.java new file mode 100644 index 0000000..b3c271a --- /dev/null +++ b/hzims-service/message/src/main/java/com/hnac/hzims/message/dto/PushMessageDTO.java @@ -0,0 +1,24 @@ +package com.hnac.hzims.message.dto; + +import com.hnac.hzims.message.entity.PushMessageEntity; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * @author hx + */ +@Data +@EqualsAndHashCode +public class PushMessageDTO extends PushMessageEntity implements Serializable { + + @ApiModelProperty("开始时间") + private LocalDateTime startTime; + + @ApiModelProperty("结束时间") + private LocalDateTime endTime; + +} diff --git a/hzims-service/message/src/main/java/com/hnac/hzims/message/mapper/PushMessageMapper.java b/hzims-service/message/src/main/java/com/hnac/hzims/message/mapper/PushMessageMapper.java new file mode 100644 index 0000000..0ecc374 --- /dev/null +++ b/hzims-service/message/src/main/java/com/hnac/hzims/message/mapper/PushMessageMapper.java @@ -0,0 +1,11 @@ +package com.hnac.hzims.message.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.hnac.hzims.message.entity.PushMessageEntity; + +/** + * @author hx + */ +public interface PushMessageMapper extends BaseMapper { + +} diff --git a/hzims-service/message/src/main/java/com/hnac/hzims/message/schedule/MessagePushSchedule.java b/hzims-service/message/src/main/java/com/hnac/hzims/message/schedule/MessagePushSchedule.java new file mode 100644 index 0000000..19047e5 --- /dev/null +++ b/hzims-service/message/src/main/java/com/hnac/hzims/message/schedule/MessagePushSchedule.java @@ -0,0 +1,133 @@ +package com.hnac.hzims.message.schedule; + +import cn.hutool.core.map.MapUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.hnac.hzims.common.utils.DateUtil; +import com.hnac.hzims.message.configure.service.ISmsConfigService; +import com.hnac.hzims.message.constants.RedisKeyConstants; +import com.hnac.hzims.message.dto.PushMessageDTO; +import com.hnac.hzims.message.dto.SmsPushDto; +import com.hnac.hzims.message.entity.PushMessageEntity; +import com.hnac.hzims.message.entity.config.SmsConfigEntity; +import com.hnac.hzims.message.push.service.IPushService; +import com.hnac.hzims.message.service.IPushMessageService; +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.annotation.XxlJob; +import com.xxl.job.core.log.XxlJobLogger; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.CollectionUtil; +import org.springblade.core.tool.utils.Func; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import javax.validation.constraints.NotNull; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.*; +import java.util.stream.Collectors; + +import static com.hnac.hzims.message.constants.XxlJobConstants.*; + +/** + * 消息中心定时推送任务调度 + * @author hx + */ +@Component +@Slf4j +public class MessagePushSchedule { + @Autowired + private ISmsConfigService smsConfigService; + @Autowired + private IPushMessageService pushMessageService; + @Autowired + @Qualifier("smsPushService") + private IPushService pushService; + + public static final String MESSAGE_ID = "messageId"; + + /** + * 消息推送根据推送人分时段推送任务调度 + * @param params 暂不支持参数参入执行调度任务 + * @return + */ + @XxlJob(PUSH_SMS_MESSAGE_BY_NOTICE) + public ReturnT pushSmsMessageByNotice(String params) { + List list = smsConfigService.list(); + list.stream().filter(e -> Func.isNotEmpty(e.getDuration()) && Func.isNotEmpty(e.getTimeUnit())).forEach(smsConfigEntity -> { + //查询出规定时段内的短信数量统一发送 + // 获取配置的时间范围内的消息 + LocalDateTime endTime = DateUtil.plus(LocalDateTime.now(), smsConfigEntity.getDuration(), smsConfigEntity.getTimeUnit()); + PushMessageDTO pushMessageDTO = new PushMessageDTO(); + pushMessageDTO.setStartTime(LocalDateTime.now()); + pushMessageDTO.setEndTime(endTime); + pushMessageDTO.setBusinessKey(smsConfigEntity.getBusinessKey()); + pushMessageDTO.setIsPush(false); + List pushMessageList = pushMessageService.list(pushMessageDTO); + XxlJobLogger.log("开始推送短信,推送条数为:"+pushMessageList.size()); + // 获取推送消息内容体 根据用户拆分出多条短信推送 + List splitSmsList = this.transform(pushMessageList); + if(CollectionUtil.isEmpty(splitSmsList)) return; + this.push(splitSmsList,smsConfigEntity); + + }); + return ReturnT.SUCCESS; + } + + /** + * 推送短信 + * @param splitSmsList 短信内容 + * @param configEntity 短信配置 + */ + private void push(List splitSmsList,SmsConfigEntity configEntity) { + Map> phoneMap = splitSmsList.stream().collect(Collectors.groupingBy(sms -> sms.getString("phones"))); + phoneMap.forEach((phone,messages) -> { + String messageIds = messages.stream().map(message -> message.getString(MESSAGE_ID)).collect(Collectors.joining(",")); + List smsPushDtoList = JSONArray.parseArray(JSON.toJSONString(messages), SmsPushDto.class); + SmsPushDto templatePush = smsPushDtoList.get(0); + Map params = new HashMap<>(); + Arrays.stream(configEntity.getContentVariables().split(",")).forEachOrdered(contentVariable ->{ + String value = smsPushDtoList.stream().map(SmsPushDto::getParams) + .map(pm -> MapUtil.getStr(pm,contentVariable)).collect(Collectors.joining(",")); + params.put(contentVariable,value); + }); + templatePush.setParams(params); + XxlJobLogger.log(">>>>>>>>>>开始推送消息,推送内容为:"+JSONObject.toJSONString(templatePush)+"<<<<<<<<<<"); + LambdaUpdateWrapper pushLU = Wrappers.lambdaUpdate() + .set(PushMessageEntity::getIsPush, true) + .in(PushMessageEntity::getId, messageIds.split(",")); + pushMessageService.update(pushLU); + R pushResult = pushService.send(templatePush); + LambdaUpdateWrapper successLU = Wrappers.lambdaUpdate(); + if(pushResult.isSuccess()) { + successLU.set(PushMessageEntity::getIsSuccess,true); + } + else { + successLU.set(PushMessageEntity::getIsSuccess,false); + } + successLU.in(PushMessageEntity::getId, messageIds.split(",")); + pushMessageService.update(successLU); + }); + } + + /** + * 获取推送消息内容体 根据用户拆分出多条短信推送 + * @param pushMessageList + * @return + */ + private List transform(List pushMessageList) { + return pushMessageList.stream().map(m -> { + JSONObject object = JSONObject.parseObject(m.getMessageContent()); + object.put(MESSAGE_ID,m.getId()); + return object; + }).filter(object -> Func.isNotEmpty(object.getString("phones"))).collect(Collectors.toList()); + } + +}