SmsService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.xunmei.sms.service;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.dc.eai.data.CompositeData;
  7. import com.dcfs.esb.client.ESBClient;
  8. import com.xunmei.common.core.constant.*;
  9. import com.xunmei.common.core.domain.reminder.dto.ReminderScheduleMsgDto;
  10. import com.xunmei.common.core.domain.reminder.vo.ReminderScheduleMsgVo;
  11. import com.xunmei.common.core.web.domain.AjaxResult;
  12. import com.xunmei.common.security.utils.SecurityUtils;
  13. import com.xunmei.sms.utils.SmsNotifyType;
  14. import com.xunmei.sms.utils.SmsUtil;
  15. import com.xunmei.system.api.RemoteOrgService;
  16. import com.xunmei.system.api.RemoteRoleService;
  17. import com.xunmei.system.api.RemoteUserService;
  18. import com.xunmei.system.api.domain.SysOrg;
  19. import com.xunmei.system.api.domain.SysUser;
  20. import com.xunmei.system.api.function.RemoteCallHandlerExecutor;
  21. import com.xunmei.system.api.vo.SmsInfoVo;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Value;
  27. import org.springframework.scheduling.annotation.Async;
  28. import org.springframework.stereotype.Service;
  29. import java.util.ArrayList;
  30. import java.util.Date;
  31. import java.util.List;
  32. @Service
  33. @Slf4j
  34. public class SmsService implements ISmsService {
  35. private static Logger logger = LoggerFactory.getLogger(SmsService.class);
  36. @Autowired
  37. private RemoteRoleService remoteRoleService;
  38. @Autowired
  39. private RemoteOrgService remoteOrgService;
  40. @Autowired
  41. private ISmsSendRecordService recordService;
  42. @Autowired
  43. private RemoteUserService remoteUserService;
  44. //告警代码
  45. private static final String ALARM_CODE = "854002";
  46. //逾期提醒代码
  47. private static final String TODO_CODE = "854001";
  48. /**
  49. * 在ESB系统里的 系统编码
  50. */
  51. @Value("${sms.system.code}")
  52. private String systemCode;
  53. /**
  54. * 系统名称
  55. */
  56. @Value("${sms.system.name}")
  57. private String systemName;
  58. /**
  59. * 服务代码
  60. */
  61. @Value("${sms.service.code}")
  62. private String serviceCode;
  63. /**
  64. * 服务场景
  65. */
  66. @Value("${sms.service.scene}")
  67. private String serviceScene;
  68. /**
  69. * 发送短信
  70. *
  71. * @param data
  72. * @return {@link AjaxResult}
  73. */
  74. @Override
  75. @Async
  76. public AjaxResult send(SmsInfoVo data) {
  77. try {
  78. logger.debug("【发送短信】开始,数据:" + JSONObject.toJSON(data));
  79. CompositeData smsCompositeData = SmsUtil.createSmsBody(data, systemCode, systemName, serviceCode, serviceScene);
  80. logger.debug("【发送短信】转换后数据:" + JSONObject.toJSON(smsCompositeData));
  81. if (smsCompositeData == null) {
  82. logger.error("【发送短信】转换数据出错:原数据:" + JSONObject.toJSON(data));
  83. return AjaxResult.error("【发送短信】转换数据出错");
  84. }
  85. CompositeData rspData = null;
  86. try {
  87. rspData =ESBClient.request(smsCompositeData);
  88. } catch (Exception e) {
  89. logger.error("短信发送时发生异常 :", e);
  90. }
  91. logger.debug("【发送短信】返回数据:" + JSONObject.toJSON(rspData));
  92. AjaxResult result = getResult(rspData);
  93. SysOrg tempOrg= new SysOrg();
  94. tempOrg.setId(ObjectUtil.isNotEmpty(SecurityUtils.getLoginUser())? SecurityUtils.getLoginUser().getOrgId():4352539158904832L);
  95. tempOrg.setShortName("短信发送测试");
  96. SysUser tempUser=new SysUser();
  97. tempUser.setId(ObjectUtil.isNotEmpty(SecurityUtils.getLoginUser()) ? SecurityUtils.getLoginUser().getUserid():1L);
  98. tempUser.setName(ObjectUtil.isNotEmpty(SecurityUtils.getLoginUser()) ? SecurityUtils.getLoginUser().getName():"管理员");
  99. recordService.saveSmsRecord(tempOrg, tempUser, result, data,smsCompositeData);
  100. return result;
  101. } catch (Exception ex) {
  102. logger.error("【发送短信】发送时内部异常:数据:" + JSONObject.toJSON(data), ex);
  103. return AjaxResult.error("【发送短信】发送时内部异常");
  104. }
  105. }
  106. /**
  107. * 根据机构获取机构下网点负责人角色,并发送告警短信
  108. *
  109. * @param orgId
  110. * @param alarmContent
  111. * @return
  112. */
  113. @Override
  114. public void sendSmsIot(Long orgId, String alarmType, String alarmContent, String alarmTime) {
  115. List<SysUser> userList = RemoteCallHandlerExecutor.executeRemoteCall(() ->
  116. remoteRoleService.selectUserByRoleNameAndOrgId("网点负责人", orgId, 0, SecurityConstants.INNER), ErrorMsgConstants.QUERY_USER_DATA_ERROR);
  117. SysOrg sysOrg = remoteOrgService.selectOrgById(orgId, SecurityConstants.INNER);
  118. Date now = new Date();
  119. String date = DateUtil.format(now, "yyyy-MM-dd");
  120. String time = DateUtil.format(now, "HH:mm:ss");
  121. if (ObjectUtil.isNotEmpty(userList)) {
  122. for (SysUser sysUser : userList) {
  123. SmsInfoVo smsInfoVo = new SmsInfoVo();
  124. smsInfoVo.setBelongBranchId(sysOrg.getGuid());
  125. smsInfoVo.setTranType(ALARM_CODE);
  126. smsInfoVo.setMobile(sysUser.getPhone());
  127. smsInfoVo.setOccurDate(date);
  128. smsInfoVo.setOccurTime(time);
  129. smsInfoVo.setRetMsg(alarmType);
  130. smsInfoVo.setBak3(alarmContent);
  131. deal(sysOrg, sysUser, smsInfoVo);
  132. }
  133. }
  134. }
  135. /**
  136. * 给指定人员发送告警短信
  137. *
  138. * @param userId
  139. * @param alarmContent
  140. * @return
  141. */
  142. @Override
  143. public void sendSmsSensor(Long userId, String alarmTypeDesc, String alarmContent, String alarmTime) {
  144. SysUser sysUser = remoteUserService.getUserById(userId, SecurityConstants.INNER);
  145. if (sysUser != null){
  146. SysOrg sysOrg = remoteOrgService.selectOrgById(sysUser.getOrgId(), SecurityConstants.INNER);
  147. Date now = new Date();
  148. String date = DateUtil.format(now, "yyyy-MM-dd");
  149. String time = DateUtil.format(now, "HH:mm:ss");
  150. SmsInfoVo smsInfoVo = new SmsInfoVo();
  151. smsInfoVo.setBelongBranchId(sysOrg.getGuid());
  152. smsInfoVo.setMobile(sysUser.getPhone());
  153. smsInfoVo.setTranType(ALARM_CODE);
  154. smsInfoVo.setOccurDate(date);
  155. smsInfoVo.setOccurTime(time);
  156. smsInfoVo.setRetMsg(alarmTypeDesc);
  157. smsInfoVo.setBak3(alarmContent);
  158. deal(sysOrg, sysUser, smsInfoVo);
  159. }
  160. }
  161. private void deal(SysOrg sysOrg, SysUser sysUser, SmsInfoVo smsInfoVo) {
  162. if (ObjectUtil.isEmpty(sysUser.getPhone())) {
  163. AjaxResult result = AjaxResult.error("用户" + sysUser.getName() + "手机号为空");
  164. recordService.saveSmsRecord(sysOrg, sysUser, result, smsInfoVo,null);
  165. return;
  166. }
  167. CompositeData smsCompositeData = SmsUtil.createSmsBody(smsInfoVo, systemCode, systemName, serviceCode, serviceScene);
  168. CompositeData rspData = null;
  169. try {
  170. rspData = ESBClient.request(smsCompositeData);
  171. logger.info("短信发送返回结果: {}", JSONObject.toJSONString(rspData));
  172. } catch (Exception e) {
  173. logger.error("短信发送时发生异常 :", e);
  174. }
  175. AjaxResult result = getResult(rspData);
  176. recordService.saveSmsRecord(sysOrg, sysUser, result, smsInfoVo,smsCompositeData);
  177. }
  178. @Override
  179. public void sendSmsTodo(Long orgId, String type, String content, Date time) {
  180. //checkParam(orgId, type, content, time);
  181. List<SysUser> userList = RemoteCallHandlerExecutor.executeRemoteCall(() ->
  182. remoteRoleService.selectUserByRoleNameAndOrgId("网点负责人", orgId, 0, SecurityConstants.INNER), ErrorMsgConstants.QUERY_USER_DATA_ERROR);
  183. SysOrg sysOrg = remoteOrgService.selectOrgById(orgId, SecurityConstants.INNER);
  184. String date = DateUtil.format(time, "yyyy-MM-dd");
  185. Date now = new Date();
  186. String day = DateUtil.format(now, "yyyy-MM-dd");
  187. String sfm = DateUtil.format(now, "HH:mm:ss");
  188. if (ObjectUtil.isNotEmpty(userList)) {
  189. for (SysUser sysUser : userList) {
  190. SmsInfoVo smsInfoVo = new SmsInfoVo();
  191. smsInfoVo.setBelongBranchId(sysOrg.getGuid());
  192. smsInfoVo.setTranType(TODO_CODE);
  193. smsInfoVo.setMobile(sysUser.getPhone());
  194. smsInfoVo.setOccurDate(day);
  195. smsInfoVo.setOccurTime(sfm);
  196. smsInfoVo.setRetMsg(type);
  197. if (SmsNotifyType.OVERDUE_REMINDER.getDesc().equals(type)) {
  198. smsInfoVo.setBak3("任务【" + content + "】未完成,将于" + DateUtil.format(time, Constants.HMS_FORMAT) + "逾期。");
  199. } else {
  200. smsInfoVo.setBak3("任务【" + content + "】将于" + DateUtil.format(time, Constants.HMS_FORMAT) + "开始,请及时完成。");
  201. }
  202. deal(sysOrg, sysUser, smsInfoVo);
  203. }
  204. }
  205. }
  206. private void checkParam(Long orgId, String type, String content, Date time) {
  207. if (ObjectUtil.hasEmpty(orgId, type, content, time)) {
  208. log.error("短信发送时发生异常,有参数为空,orgId:{},type:{},content:{},time:{}", orgId, type, content, time);
  209. }
  210. if (!type.equals(SmsNotifyType.OVERDUE_REMINDER.getDesc()) && !type.equals(SmsNotifyType.EXPIRY_REMINDER.getDesc())) {
  211. log.error("短信发送时发生异常,type参数错误,type:{}", type);
  212. }
  213. }
  214. @Override
  215. public List<ReminderScheduleMsgVo> sendReminderSms(List<ReminderScheduleMsgDto> msgDto) {
  216. Date now = new Date();
  217. String date = DateUtil.format(now, "yyyy-MM-dd");
  218. String time = DateUtil.format(now, "HH:mm:ss");
  219. List<ReminderScheduleMsgVo> returnData = new ArrayList<>();
  220. for (ReminderScheduleMsgDto dto : msgDto) {
  221. SmsInfoVo smsInfoVo = new SmsInfoVo();
  222. smsInfoVo.setBelongBranchId(dto.getOrgGuid());
  223. smsInfoVo.setTranType(TODO_CODE);
  224. smsInfoVo.setMobile(dto.getPhone());
  225. smsInfoVo.setOccurDate(date);
  226. smsInfoVo.setOccurTime(time);
  227. smsInfoVo.setRetMsg(dto.getType());
  228. smsInfoVo.setBak3(dto.getMsgContent());
  229. AjaxResult result;
  230. CompositeData smsCompositeData=null;
  231. if (ObjectUtil.isEmpty(dto.getPhone())) {
  232. result = AjaxResult.error("用户" + dto.getRecipientName() + "手机号为空");
  233. } else {
  234. CompositeData rspData = null;
  235. smsCompositeData = SmsUtil.createSmsBody(smsInfoVo, systemCode, systemName, serviceCode, serviceScene);
  236. try {
  237. rspData = ESBClient.request(smsCompositeData);
  238. } catch (Exception e) {
  239. logger.error("短信发送时发生异常 :", e);
  240. }
  241. result = getResult(rspData);
  242. }
  243. if (result.get(AjaxResult.CODE_TAG).equals(String.valueOf(HttpStatus.ERROR))) {
  244. ReminderScheduleMsgVo msgVo = new ReminderScheduleMsgVo();
  245. String msg = (String) result.get(AjaxResult.MSG_TAG);
  246. msgVo.setMsg(msg);
  247. msgVo.setStatus(2);
  248. msgVo.setScheduleId(dto.getScheduleId());
  249. returnData.add(msgVo);
  250. }
  251. recordService.saveSmsRecord(dto, result, smsInfoVo,smsCompositeData);
  252. }
  253. return returnData;
  254. }
  255. private AjaxResult getResult(CompositeData repData) {
  256. if (repData == null) {
  257. return AjaxResult.error("【发送短信】发送短信失败,短信发送返回结果为空");
  258. }
  259. logger.info("【发送短信】发送短信返回结果:{}", JSON.toJSONString(repData));
  260. boolean result = false;
  261. try {
  262. //"RET_STATUS" : Field { type=FieldType[string] length=1 scale=0 pin=false value= {S} }
  263. String retStatus = repData.getStruct("SYS_HEAD")
  264. .getField("RET_STATUS").strValue();
  265. String retCode = repData.getStruct("SYS_HEAD").getArray("RET")
  266. .getStruct(0).getField("RET_CODE").strValue();
  267. if ("S".equals(retStatus) && "000000".equals(retCode)) {
  268. return AjaxResult.success();
  269. } else {
  270. String retMsg = repData.getStruct("SYS_HEAD").getArray("RET")
  271. .getStruct(0).getField("RET_MSG").strValue();
  272. return AjaxResult.error(retMsg);
  273. }
  274. } catch (Exception e) {
  275. return AjaxResult.error("【发送短信】解析发送结果时内部异常");
  276. }
  277. }
  278. }