| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package com.xunmei.sms.service;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.ObjectUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.dc.eai.data.CompositeData;
- import com.dcfs.esb.client.ESBClient;
- import com.xunmei.common.core.constant.*;
- import com.xunmei.common.core.domain.reminder.dto.ReminderScheduleMsgDto;
- import com.xunmei.common.core.domain.reminder.vo.ReminderScheduleMsgVo;
- import com.xunmei.common.core.web.domain.AjaxResult;
- import com.xunmei.common.security.utils.SecurityUtils;
- import com.xunmei.sms.utils.SmsNotifyType;
- import com.xunmei.sms.utils.SmsUtil;
- import com.xunmei.system.api.RemoteOrgService;
- import com.xunmei.system.api.RemoteRoleService;
- import com.xunmei.system.api.RemoteUserService;
- import com.xunmei.system.api.domain.SysOrg;
- import com.xunmei.system.api.domain.SysUser;
- import com.xunmei.system.api.function.RemoteCallHandlerExecutor;
- import com.xunmei.system.api.vo.SmsInfoVo;
- import lombok.extern.slf4j.Slf4j;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- @Service
- @Slf4j
- public class SmsService implements ISmsService {
- private static Logger logger = LoggerFactory.getLogger(SmsService.class);
- @Autowired
- private RemoteRoleService remoteRoleService;
- @Autowired
- private RemoteOrgService remoteOrgService;
- @Autowired
- private ISmsSendRecordService recordService;
- @Autowired
- private RemoteUserService remoteUserService;
- //告警代码
- private static final String ALARM_CODE = "854002";
- //逾期提醒代码
- private static final String TODO_CODE = "854001";
- /**
- * 在ESB系统里的 系统编码
- */
- @Value("${sms.system.code}")
- private String systemCode;
- /**
- * 系统名称
- */
- @Value("${sms.system.name}")
- private String systemName;
- /**
- * 服务代码
- */
- @Value("${sms.service.code}")
- private String serviceCode;
- /**
- * 服务场景
- */
- @Value("${sms.service.scene}")
- private String serviceScene;
- /**
- * 发送短信
- *
- * @param data
- * @return {@link AjaxResult}
- */
- @Override
- @Async
- public AjaxResult send(SmsInfoVo data) {
- try {
- logger.debug("【发送短信】开始,数据:" + JSONObject.toJSON(data));
- CompositeData smsCompositeData = SmsUtil.createSmsBody(data, systemCode, systemName, serviceCode, serviceScene);
- logger.debug("【发送短信】转换后数据:" + JSONObject.toJSON(smsCompositeData));
- if (smsCompositeData == null) {
- logger.error("【发送短信】转换数据出错:原数据:" + JSONObject.toJSON(data));
- return AjaxResult.error("【发送短信】转换数据出错");
- }
- CompositeData rspData = null;
- try {
- rspData =ESBClient.request(smsCompositeData);
- } catch (Exception e) {
- logger.error("短信发送时发生异常 :", e);
- }
- logger.debug("【发送短信】返回数据:" + JSONObject.toJSON(rspData));
- AjaxResult result = getResult(rspData);
- SysOrg tempOrg= new SysOrg();
- tempOrg.setId(ObjectUtil.isNotEmpty(SecurityUtils.getLoginUser())? SecurityUtils.getLoginUser().getOrgId():4352539158904832L);
- tempOrg.setShortName("短信发送测试");
- SysUser tempUser=new SysUser();
- tempUser.setId(ObjectUtil.isNotEmpty(SecurityUtils.getLoginUser()) ? SecurityUtils.getLoginUser().getUserid():1L);
- tempUser.setName(ObjectUtil.isNotEmpty(SecurityUtils.getLoginUser()) ? SecurityUtils.getLoginUser().getName():"管理员");
- recordService.saveSmsRecord(tempOrg, tempUser, result, data,smsCompositeData);
- return result;
- } catch (Exception ex) {
- logger.error("【发送短信】发送时内部异常:数据:" + JSONObject.toJSON(data), ex);
- return AjaxResult.error("【发送短信】发送时内部异常");
- }
- }
- /**
- * 根据机构获取机构下网点负责人角色,并发送告警短信
- *
- * @param orgId
- * @param alarmContent
- * @return
- */
- @Override
- public void sendSmsIot(Long orgId, String alarmType, String alarmContent, String alarmTime) {
- List<SysUser> userList = RemoteCallHandlerExecutor.executeRemoteCall(() ->
- remoteRoleService.selectUserByRoleNameAndOrgId("网点负责人", orgId, 0, SecurityConstants.INNER), ErrorMsgConstants.QUERY_USER_DATA_ERROR);
- SysOrg sysOrg = remoteOrgService.selectOrgById(orgId, SecurityConstants.INNER);
- Date now = new Date();
- String date = DateUtil.format(now, "yyyy-MM-dd");
- String time = DateUtil.format(now, "HH:mm:ss");
- if (ObjectUtil.isNotEmpty(userList)) {
- for (SysUser sysUser : userList) {
- SmsInfoVo smsInfoVo = new SmsInfoVo();
- smsInfoVo.setBelongBranchId(sysOrg.getGuid());
- smsInfoVo.setTranType(ALARM_CODE);
- smsInfoVo.setMobile(sysUser.getPhone());
- smsInfoVo.setOccurDate(date);
- smsInfoVo.setOccurTime(time);
- smsInfoVo.setRetMsg(alarmType);
- smsInfoVo.setBak3(alarmContent);
- deal(sysOrg, sysUser, smsInfoVo);
- }
- }
- }
- /**
- * 给指定人员发送告警短信
- *
- * @param userId
- * @param alarmContent
- * @return
- */
- @Override
- public void sendSmsSensor(Long userId, String alarmTypeDesc, String alarmContent, String alarmTime) {
- SysUser sysUser = remoteUserService.getUserById(userId, SecurityConstants.INNER);
- if (sysUser != null){
- SysOrg sysOrg = remoteOrgService.selectOrgById(sysUser.getOrgId(), SecurityConstants.INNER);
- Date now = new Date();
- String date = DateUtil.format(now, "yyyy-MM-dd");
- String time = DateUtil.format(now, "HH:mm:ss");
- SmsInfoVo smsInfoVo = new SmsInfoVo();
- smsInfoVo.setBelongBranchId(sysOrg.getGuid());
- smsInfoVo.setMobile(sysUser.getPhone());
- smsInfoVo.setTranType(ALARM_CODE);
- smsInfoVo.setOccurDate(date);
- smsInfoVo.setOccurTime(time);
- smsInfoVo.setRetMsg(alarmTypeDesc);
- smsInfoVo.setBak3(alarmContent);
- deal(sysOrg, sysUser, smsInfoVo);
- }
- }
- private void deal(SysOrg sysOrg, SysUser sysUser, SmsInfoVo smsInfoVo) {
- if (ObjectUtil.isEmpty(sysUser.getPhone())) {
- AjaxResult result = AjaxResult.error("用户" + sysUser.getName() + "手机号为空");
- recordService.saveSmsRecord(sysOrg, sysUser, result, smsInfoVo,null);
- return;
- }
- CompositeData smsCompositeData = SmsUtil.createSmsBody(smsInfoVo, systemCode, systemName, serviceCode, serviceScene);
- CompositeData rspData = null;
- try {
- rspData = ESBClient.request(smsCompositeData);
- logger.info("短信发送返回结果: {}", JSONObject.toJSONString(rspData));
- } catch (Exception e) {
- logger.error("短信发送时发生异常 :", e);
- }
- AjaxResult result = getResult(rspData);
- recordService.saveSmsRecord(sysOrg, sysUser, result, smsInfoVo,smsCompositeData);
- }
- @Override
- public void sendSmsTodo(Long orgId, String type, String content, Date time) {
- //checkParam(orgId, type, content, time);
- List<SysUser> userList = RemoteCallHandlerExecutor.executeRemoteCall(() ->
- remoteRoleService.selectUserByRoleNameAndOrgId("网点负责人", orgId, 0, SecurityConstants.INNER), ErrorMsgConstants.QUERY_USER_DATA_ERROR);
- SysOrg sysOrg = remoteOrgService.selectOrgById(orgId, SecurityConstants.INNER);
- String date = DateUtil.format(time, "yyyy-MM-dd");
- Date now = new Date();
- String day = DateUtil.format(now, "yyyy-MM-dd");
- String sfm = DateUtil.format(now, "HH:mm:ss");
- if (ObjectUtil.isNotEmpty(userList)) {
- for (SysUser sysUser : userList) {
- SmsInfoVo smsInfoVo = new SmsInfoVo();
- smsInfoVo.setBelongBranchId(sysOrg.getGuid());
- smsInfoVo.setTranType(TODO_CODE);
- smsInfoVo.setMobile(sysUser.getPhone());
- smsInfoVo.setOccurDate(day);
- smsInfoVo.setOccurTime(sfm);
- smsInfoVo.setRetMsg(type);
- if (SmsNotifyType.OVERDUE_REMINDER.getDesc().equals(type)) {
- smsInfoVo.setBak3("任务【" + content + "】未完成,将于" + DateUtil.format(time, Constants.HMS_FORMAT) + "逾期。");
- } else {
- smsInfoVo.setBak3("任务【" + content + "】将于" + DateUtil.format(time, Constants.HMS_FORMAT) + "开始,请及时完成。");
- }
- deal(sysOrg, sysUser, smsInfoVo);
- }
- }
- }
- private void checkParam(Long orgId, String type, String content, Date time) {
- if (ObjectUtil.hasEmpty(orgId, type, content, time)) {
- log.error("短信发送时发生异常,有参数为空,orgId:{},type:{},content:{},time:{}", orgId, type, content, time);
- }
- if (!type.equals(SmsNotifyType.OVERDUE_REMINDER.getDesc()) && !type.equals(SmsNotifyType.EXPIRY_REMINDER.getDesc())) {
- log.error("短信发送时发生异常,type参数错误,type:{}", type);
- }
- }
- @Override
- public List<ReminderScheduleMsgVo> sendReminderSms(List<ReminderScheduleMsgDto> msgDto) {
- Date now = new Date();
- String date = DateUtil.format(now, "yyyy-MM-dd");
- String time = DateUtil.format(now, "HH:mm:ss");
- List<ReminderScheduleMsgVo> returnData = new ArrayList<>();
- for (ReminderScheduleMsgDto dto : msgDto) {
- SmsInfoVo smsInfoVo = new SmsInfoVo();
- smsInfoVo.setBelongBranchId(dto.getOrgGuid());
- smsInfoVo.setTranType(TODO_CODE);
- smsInfoVo.setMobile(dto.getPhone());
- smsInfoVo.setOccurDate(date);
- smsInfoVo.setOccurTime(time);
- smsInfoVo.setRetMsg(dto.getType());
- smsInfoVo.setBak3(dto.getMsgContent());
- AjaxResult result;
- CompositeData smsCompositeData=null;
- if (ObjectUtil.isEmpty(dto.getPhone())) {
- result = AjaxResult.error("用户" + dto.getRecipientName() + "手机号为空");
- } else {
- CompositeData rspData = null;
- smsCompositeData = SmsUtil.createSmsBody(smsInfoVo, systemCode, systemName, serviceCode, serviceScene);
- try {
- rspData = ESBClient.request(smsCompositeData);
- } catch (Exception e) {
- logger.error("短信发送时发生异常 :", e);
- }
- result = getResult(rspData);
- }
- if (result.get(AjaxResult.CODE_TAG).equals(String.valueOf(HttpStatus.ERROR))) {
- ReminderScheduleMsgVo msgVo = new ReminderScheduleMsgVo();
- String msg = (String) result.get(AjaxResult.MSG_TAG);
- msgVo.setMsg(msg);
- msgVo.setStatus(2);
- msgVo.setScheduleId(dto.getScheduleId());
- returnData.add(msgVo);
- }
- recordService.saveSmsRecord(dto, result, smsInfoVo,smsCompositeData);
- }
- return returnData;
- }
- private AjaxResult getResult(CompositeData repData) {
- if (repData == null) {
- return AjaxResult.error("【发送短信】发送短信失败,短信发送返回结果为空");
- }
- logger.info("【发送短信】发送短信返回结果:{}", JSON.toJSONString(repData));
- boolean result = false;
- try {
- //"RET_STATUS" : Field { type=FieldType[string] length=1 scale=0 pin=false value= {S} }
- String retStatus = repData.getStruct("SYS_HEAD")
- .getField("RET_STATUS").strValue();
- String retCode = repData.getStruct("SYS_HEAD").getArray("RET")
- .getStruct(0).getField("RET_CODE").strValue();
- if ("S".equals(retStatus) && "000000".equals(retCode)) {
- return AjaxResult.success();
- } else {
- String retMsg = repData.getStruct("SYS_HEAD").getArray("RET")
- .getStruct(0).getField("RET_MSG").strValue();
- return AjaxResult.error(retMsg);
- }
- } catch (Exception e) {
- return AjaxResult.error("【发送短信】解析发送结果时内部异常");
- }
- }
- }
|