qa-prevention-gwj/src/main/java/com/zcloud/service/messages/impl/MessagesServiceImpl.java

86 lines
3.4 KiB
Java

package com.zcloud.service.messages.impl;
import cn.hutool.core.util.StrUtil;
import com.zcloud.entity.PageData;
import com.zcloud.entity.messages.PushRecords;
import com.zcloud.mapper.datasource.messages.MessagesMapper;
import com.zcloud.service.bus.NoticeCorpService;
import com.zcloud.service.messages.MessagesService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.UuidUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@RequiredArgsConstructor
public class MessagesServiceImpl implements MessagesService {
private final MessagesMapper mapper;
private final NoticeCorpService noticeCorpService;
@Override
@Transactional(rollbackFor = Exception.class)
public void push(PushRecords pushRecords) throws Exception {
// 先记录数据
pushRecords.setId(UuidUtil.get32UUID());
pushRecords.setSendTime(DateUtil.getTime());
mapper.install(pushRecords);
// 根据手机号查找用户id
String userId = mapper.findUserIdByPhone(pushRecords.getPhone());
if (StrUtil.isEmpty(userId)) {
throw new RuntimeException("该用户不存在");
}
Integer type = pushRecords.getMessageType();
// 1-短信 2-平台信息 3-全发
if (type == 1) {
pushSms(pushRecords);
} else if (type == 2) {
PageData data = pushInfo(pushRecords, userId);
noticeCorpService.sendNotice(data);
} else if (type == 3) {
pushSms(pushRecords);
PageData data = pushInfo(pushRecords, userId);
noticeCorpService.sendNotice(data);
} else {
throw new RuntimeException("消息类型:【" + type + "】错误");
}
}
/**
* 推送平台信息(站内信)
* 注意 不能再此方法中调用数据库 再类中直接调用 会导致事务失效 private 也会导致事务失效
*
* @param pushRecords 消息
* @param userId 接收人id
*/
private PageData pushInfo(PushRecords pushRecords, String userId) throws Exception {
PageData mes = new PageData();
mes.put("BIANMA", "PUSH_RECORDS");
mes.put("SENDER_ID", "九公司应急管理"); // 发送人员ID
mes.put("SENDER_NAME", "九公司应急管理"); // 发送人员姓名
mes.put("SYNOPSIS", pushRecords.getMessageTitle()); // 站内信标题
//mes.put("WORKURL", "/pages/application/high-risk-work/limited-space/gas-analysis/list?NameLikes=" + pd.getString("WORK_PERMIT_NUMBER")); // 操作链接
mes.put("CORPINFO_ID", "48a8ca9815814c979814ddcf041c5cd5");// 企业id
mes.put("RECEIVER_ID", userId); // 接收人员ID
PageData content = new PageData();
content.put("msg", pushRecords.getMessageContent());// 作业编号
mes.put("CONTENT", content);// 站内信内容
return mes;
}
/**
* 推送平台信息(站内信)
* 注意 不能再此方法中调用数据库 再类中直接调用 会导致事务失效 private 也会导致事务失效
*
* @param pushRecords 消息
*/
private void pushSms(PushRecords pushRecords) {
// todo
log.error("===================>推送短信信息");
log.error("消息内容:{}", pushRecords);
}
}