From c3ecdee6d7914937c9ac2d92598960654e388a90 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Mon, 4 Jul 2022 11:01:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BB=A3=E7=90=86=E5=85=AC=E5=8F=B8=E6=A6=82=E5=BF=B5?= =?UTF-8?q?=EF=BC=8C=E7=94=B1=E4=BB=A3=E7=90=86=E5=85=AC=E5=8F=B8=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=85=B6=E6=89=80=E7=AE=A1=E8=BE=96=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E5=86=85=E7=9A=84=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xkrs/dao/AgentCompanyDao.java | 20 +++ .../xkrs/model/entity/AgentCompanyEntity.java | 155 ++++++++++++++++++ .../service/impl/FirePointServiceImpl.java | 90 +++------- .../com/xkrs/utils/FirePointCodeUtils.java | 123 ++++++++++++++ .../utils/{SendSms.java => SMSUtils.java} | 104 +++++++++++- 5 files changed, 426 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/xkrs/dao/AgentCompanyDao.java create mode 100644 src/main/java/com/xkrs/model/entity/AgentCompanyEntity.java rename src/main/java/com/xkrs/utils/{SendSms.java => SMSUtils.java} (56%) diff --git a/src/main/java/com/xkrs/dao/AgentCompanyDao.java b/src/main/java/com/xkrs/dao/AgentCompanyDao.java new file mode 100644 index 0000000..c8d4ecc --- /dev/null +++ b/src/main/java/com/xkrs/dao/AgentCompanyDao.java @@ -0,0 +1,20 @@ +package com.xkrs.dao; + +import com.xkrs.model.entity.AgentCompanyEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public interface AgentCompanyDao extends JpaRepository, JpaSpecificationExecutor { + + @Query(value = "SELECT * FROM agent_company WHERE company_name = ?1 LIMIT 1", nativeQuery = true) + AgentCompanyEntity selectCompanyNameEquals(String companyName); + + @Query(value = "SELECT * FROM agent_company WHERE company_name != ?1", nativeQuery = true) + List selectCompanyNameNotEquals(String companyName); + +} diff --git a/src/main/java/com/xkrs/model/entity/AgentCompanyEntity.java b/src/main/java/com/xkrs/model/entity/AgentCompanyEntity.java new file mode 100644 index 0000000..4318f37 --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/AgentCompanyEntity.java @@ -0,0 +1,155 @@ +package com.xkrs.model.entity; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@Table(name = "agent_company") +public class AgentCompanyEntity implements Serializable { + + /** + * 主键id + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "agent_company_seq_gen") + @SequenceGenerator(name = "agent_company_seq_gen", sequenceName = "agent_company_id_seq", allocationSize = 1) + private Long id; + + /** + * 公司名称 + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String companyName; + + /** + * 省市区编码 + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String countyCode; + + /** + * 签约截止日期 + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String signDeadLine; + + /** + * 密钥ID + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String secretId; + + /** + * 密钥KEY + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String secretKey; + + /** + * APPID + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String appId; + + /** + * 签名信息 + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String sign; + + /** + * 短信模板ID + */ + @Column(length = 128, columnDefinition = "varchar(128)") + private String templateId; + + public AgentCompanyEntity() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getCompanyName() { + return companyName; + } + + public void setCompanyName(String companyName) { + this.companyName = companyName; + } + + public String getCountyCode() { + return countyCode; + } + + public void setCountyCode(String countyCode) { + this.countyCode = countyCode; + } + + public String getSignDeadLine() { + return signDeadLine; + } + + public void setSignDeadLine(String signDeadLine) { + this.signDeadLine = signDeadLine; + } + + public String getSecretId() { + return secretId; + } + + public void setSecretId(String secretId) { + this.secretId = secretId; + } + + public String getSecretKey() { + return secretKey; + } + + public void setSecretKey(String secretKey) { + this.secretKey = secretKey; + } + + public String getAppId() { + return appId; + } + + public void setAppId(String appId) { + this.appId = appId; + } + + public String getSign() { + return sign; + } + + public void setSign(String sign) { + this.sign = sign; + } + + public String getTemplateId() { + return templateId; + } + + public void setTemplateId(String templateId) { + this.templateId = templateId; + } + + @Override + public String toString() { + return "AgentCompanyEntity{" + + "id=" + id + + ", companyName='" + companyName + '\'' + + ", countyCode='" + countyCode + '\'' + + ", signDeadLine='" + signDeadLine + '\'' + + ", secretId='" + secretId + '\'' + + ", secretKey='" + secretKey + '\'' + + ", appId='" + appId + '\'' + + ", sign='" + sign + '\'' + + ", templateId='" + templateId + '\'' + + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index 3244123..95d8fe8 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -89,6 +89,9 @@ public class FirePointServiceImpl implements FirePointService { @Resource private GlobalConfigService globalConfigService; + @Resource + private AgentCompanyDao agentCompanyDao; + /** * 添加火点信息 * @@ -408,75 +411,36 @@ public class FirePointServiceImpl implements FirePointService { /** * 发送短信消息 - * - * @param firePointEntity */ private void sendSMSMessage(FirePointEntity firePointEntity) { List sysUserList = sysUserDao.findAll(); - if (sysUserList.isEmpty()) { - return; - } - List administratorList = relRoleAuthorityDao.selectByAuthorityId(1); - List telephoneList = new ArrayList<>(); - for (SysUserEntity sysUser : sysUserList) { - try { - String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(sysUser.getCountyCode()); - if (firePointEntity.getStreetCode().startsWith(codeNotZeroEnd)) { - if (sysUser.getDeleteFlag() != 0) {//被删除的用户不发短信 - continue; - } - if (sysUser.getActiveFlag() != 0) {//被禁用的用户不发短信 - continue; - } - if (sysUser.getReceiveSms() != 1) {//关掉开关的用户不发短信 - continue; - } - LocalDateTime sysUserOverDateTime = DateTimeUtil.stringToDateTimeFormatter(sysUser.getOverTime(), DateTimeUtil.COMMON_FORMATTER_DATETIME); - if (sysUserOverDateTime.isBefore(DateTimeUtil.getNowTime())) {//普通用户 - boolean smsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L); - if (!smsEnable) {//根据配置信息决定是否发送短信 - continue; - } - } else {//VIP用户 - boolean smsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L); - if (!smsEnable) {//根据配置信息决定是否发送短信 - continue; - } - } - if (isAdministrator(administratorList, sysUser)) {//管理员用户不发短信 - continue; - } - telephoneList.add("86" + sysUser.getUserName()); + List adminList = relRoleAuthorityDao.selectByAuthorityId(1); + boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L); + boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L); + List smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable); + //对收信人进行分组 + AgentCompanyEntity master = agentCompanyDao.selectCompanyNameEquals("青岛星科瑞升信息科技有限公司"); + List agentCompanyList = agentCompanyDao.selectCompanyNameNotEquals("青岛星科瑞升信息科技有限公司"); + Map> agentCompanyGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, master, agentCompanyList); + //批量发送短信通知 + for (Map.Entry> entry : agentCompanyGroupMap.entrySet()) { + AgentCompanyEntity groupAgentCompany = entry.getKey(); + List groupSysUserList = entry.getValue(); + if (groupAgentCompany != null && groupSysUserList != null && groupSysUserList.size() > 0) { + String[] telephoneArray = new String[groupSysUserList.size()]; + for (int i = 0; i < groupSysUserList.size(); i++) { + telephoneArray[i] = "86" + groupSysUserList.get(i).getUserName(); } - } catch (Exception e) { - e.printStackTrace(); + SMSUtils.sendSmsToUser(groupAgentCompany, telephoneArray, new String[]{firePointEntity.getFirePointAddress()}); + //记录短信通知日志 + StringBuilder stringBuilder = new StringBuilder("发送短信消息通知:\n"); + for (String telephone : telephoneArray) { + stringBuilder.append("telephone:").append(telephone).append("\n"); + } + stringBuilder.append(groupAgentCompany.toString()).append("短信内容:").append(firePointEntity.getFirePointAddress()); + log.info(stringBuilder.toString()); } } - if (telephoneList.size() > 0) { - String[] telephoneArray = telephoneList.toArray(new String[]{}); - SendSms.sendSmsToUser(telephoneArray, "1425995", new String[]{firePointEntity.getFirePointAddress()}); - StringBuilder stringBuilder = new StringBuilder("发送短信消息通知:\n"); - for (String telephone : telephoneArray) { - stringBuilder.append("telephone:").append(telephone).append("\n"); - } - stringBuilder.append("短信模版ID:1425995\n").append("短信内容:").append(firePointEntity.getFirePointAddress()); - log.info(stringBuilder.toString()); - } - } - - /** - * 判断是否是管理员 - */ - private boolean isAdministrator(List administratorList, SysUserEntity sysUser) { - if (administratorList == null || administratorList.size() == 0) { - return false; - } - for (RelRoleAuthorityEntity administrator : administratorList) { - if (administrator.getUserId().intValue() == sysUser.getId().intValue()) { - return true; - } - } - return false; } /** diff --git a/src/main/java/com/xkrs/utils/FirePointCodeUtils.java b/src/main/java/com/xkrs/utils/FirePointCodeUtils.java index bdefe87..6195d49 100644 --- a/src/main/java/com/xkrs/utils/FirePointCodeUtils.java +++ b/src/main/java/com/xkrs/utils/FirePointCodeUtils.java @@ -1,5 +1,16 @@ package com.xkrs.utils; +import com.xkrs.model.entity.AgentCompanyEntity; +import com.xkrs.model.entity.RelRoleAuthorityEntity; +import com.xkrs.model.entity.SysUserEntity; +import org.apache.hc.core5.util.TextUtils; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class FirePointCodeUtils { private FirePointCodeUtils() { @@ -13,4 +24,116 @@ public class FirePointCodeUtils { return codeNotZeroEnd; } + /** + * 过滤短信通知收信人 + * + * @param firePointStreetCode 火点的街道编号 + * @param sysUserList 系统用户列表 + * @param adminList 管理员列表 + * @param normalSmsEnable 普通用户能否接收短信通知 + * @param vipSmsEnable VIP用户能否接收短信通知 + * @return 短信通知收信人列表 + */ + public static List filterSmsReceiver(String firePointStreetCode, List sysUserList, List adminList, boolean normalSmsEnable, boolean vipSmsEnable) { + List smsReceiverList = new ArrayList<>(); + if (sysUserList == null || sysUserList.isEmpty()) { + return smsReceiverList; + } + for (SysUserEntity sysUser : sysUserList) { + try { + String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(sysUser.getCountyCode()); + if (firePointStreetCode.startsWith(codeNotZeroEnd)) { + if (sysUser.getDeleteFlag() != 0) {//被删除的用户不发短信 + continue; + } + if (sysUser.getActiveFlag() != 0) {//被禁用的用户不发短信 + continue; + } + if (sysUser.getReceiveSms() != 1) {//关掉开关的用户不发短信 + continue; + } + LocalDateTime sysUserOverDateTime = DateTimeUtil.stringToDateTimeFormatter(sysUser.getOverTime(), DateTimeUtil.COMMON_FORMATTER_DATETIME); + if (sysUserOverDateTime.isBefore(DateTimeUtil.getNowTime())) { + if (!normalSmsEnable) {//根据配置信息决定普通用户是否发送短信 + continue; + } + } else { + if (!vipSmsEnable) {//根据配置信息决定VIP用户是否发送短信 + continue; + } + } + if (isAdmin(adminList, sysUser)) {//管理员用户不发短信 + continue; + } + smsReceiverList.add(sysUser); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return smsReceiverList; + } + + /** + * 判断是否是管理员 + */ + private static boolean isAdmin(List administratorList, SysUserEntity sysUser) { + if (administratorList == null || administratorList.size() == 0) { + return false; + } + for (RelRoleAuthorityEntity administrator : administratorList) { + if (administrator.getUserId().intValue() == sysUser.getId().intValue()) { + return true; + } + } + return false; + } + + /** + * 对短信通知收信人进行分组 + * + * @param smsReceiverList 短信通知收信人列表 + * @param master 星科瑞升 + * @param agentCompanyList 代理公司列表 + * @return 分组后的短信通知收信人列表 + */ + public static Map> groupSmsReceiver(List smsReceiverList, AgentCompanyEntity master, List agentCompanyList) { + //数据仓库 + Map> groupMap = new HashMap<>(); + //填充代理公司数据 + if (agentCompanyList != null && agentCompanyList.size() > 0) { + for (AgentCompanyEntity agentCompany : agentCompanyList) { + try { + if (TextUtils.isEmpty(agentCompany.getCountyCode())) {//区划编码为空的不发短信 + continue; + } + String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(agentCompany.getCountyCode()); + if (TextUtils.isEmpty(codeNotZeroEnd)) {//区划编码后切完0为空的不发短信 + continue; + } + List receiverList = new ArrayList<>(); + for (SysUserEntity smsReceiver : smsReceiverList) { + if (smsReceiver.getCountyCode().startsWith(codeNotZeroEnd)) { + receiverList.add(smsReceiver); + } + } + groupMap.put(agentCompany, receiverList); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + //填充星科瑞升数据 + List masterReceiverList = new ArrayList<>(); + for (Map.Entry> entry : groupMap.entrySet()) { + for (SysUserEntity smsReceiver : entry.getValue()) { + if (!smsReceiverList.contains(smsReceiver)) { + masterReceiverList.add(smsReceiver); + } + } + } + groupMap.put(master, masterReceiverList); + return groupMap; + } + } diff --git a/src/main/java/com/xkrs/utils/SendSms.java b/src/main/java/com/xkrs/utils/SMSUtils.java similarity index 56% rename from src/main/java/com/xkrs/utils/SendSms.java rename to src/main/java/com/xkrs/utils/SMSUtils.java index 65325c3..e178b5a 100644 --- a/src/main/java/com/xkrs/utils/SendSms.java +++ b/src/main/java/com/xkrs/utils/SMSUtils.java @@ -9,13 +9,111 @@ import com.tencentcloudapi.sms.v20190711.models.PullSmsReplyStatusByPhoneNumberR import com.tencentcloudapi.sms.v20190711.models.PullSmsReplyStatusByPhoneNumberResponse; import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; +import com.xkrs.model.entity.AgentCompanyEntity; /** * 短信工具 - * - * @author tajochen */ -public class SendSms { +public class SMSUtils { + + public static int sendSmsToUser(AgentCompanyEntity agentCompanyEntity, String[] phoneNumbers, String[] templateParams) { + + int resValue = 0; + try { + /* 必要步骤: + * 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey + * 可以直接在代码中写入密钥对,但需谨防泄露,不要将代码复制、上传或者分享给他人 + * CAM 密钥查询:https://console.cloud.tencent.com/cam/capi*/ + String secretId = agentCompanyEntity.getSecretId(); + String secretKey = agentCompanyEntity.getSecretKey(); + Credential cred = new Credential(secretId, secretKey); + + // 实例化一个 http 选项,可选,无特殊需求时可以跳过 + HttpProfile httpProfile = new HttpProfile(); + // 设置代理 + // httpProfile.setProxyHost("host"); + // httpProfile.setProxyPort(6000); + httpProfile.setReqMethod("POST"); + /* SDK 有默认的超时时间,非必要请不要进行调整 + * 如有需要请在代码中查阅以获取最新的默认值 */ + httpProfile.setConnTimeout(60); + /* SDK 会自动指定域名,通常无需指定域名,但访问金融区的服务时必须手动指定域名 + * 例如 SMS 的上海金融区域名为 sms.ap-shanghai-fsi.tencentcloudapi.com */ + httpProfile.setEndpoint("sms.tencentcloudapi.com"); + + /* 非必要步骤: + * 实例化一个客户端配置对象,可以指定超时时间等配置 */ + ClientProfile clientProfile = new ClientProfile(); + /* SDK 默认用 TC3-HMAC-SHA256 进行签名 + * 非必要请不要修改该字段 */ + clientProfile.setSignMethod("HmacSHA256"); + clientProfile.setHttpProfile(httpProfile); + /* 实例化 SMS 的 client 对象 + * 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 */ + SmsClient client = new SmsClient(cred, "", clientProfile); + /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数 + * 您可以直接查询 SDK 源码确定接口有哪些属性可以设置 + * 属性可能是基本类型,也可能引用了另一个数据结构 + * 推荐使用 IDE 进行开发,可以方便地跳转查阅各个接口和数据结构的文档说明 */ + SendSmsRequest req = new SendSmsRequest(); + + /* 填充请求参数,这里 request 对象的成员变量即对应接口的入参 + * 您可以通过官网接口文档或跳转到 request 对象的定义处查看请求参数的定义 + * 基本类型的设置: + * 帮助链接: + * 短信控制台:https://console.cloud.tencent.com/smsv2 + * sms helper:https://cloud.tencent.com/document/product/382/3773 */ + + /* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666 */ + String appid = agentCompanyEntity.getAppId(); + req.setSmsSdkAppid(appid); + + /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */ + String sign = agentCompanyEntity.getSign(); + req.setSign(sign); + + /* 国际/港澳台短信 senderid: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */ + // String senderid = "xxx"; + String senderid = ""; + req.setSenderId(senderid); + + /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */ + String session = ""; + req.setSessionContext(session); + + /* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */ + String extendcode = ""; + req.setExtendCode(extendcode); + + /* 模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID */ + // String templateID = "730983"; + req.setTemplateID(agentCompanyEntity.getTemplateId()); + + /* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号] + * 例如+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/ + // String[] phoneNumbers = { "+8615726240251", "+8617854119839", "+8615269088338"}; + req.setPhoneNumberSet(phoneNumbers); + + /* 模板参数: 若无模板参数,则设置为空*/ + // String[] templateParams = {"test","100"}; + req.setTemplateParamSet(templateParams); + + /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的 + * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */ + SendSmsResponse res = client.SendSms(req); + + // 输出 JSON 格式的字符串回包 + System.out.println(SendSmsResponse.toJsonString(res)); + + // 可以取出单个值,您可以通过官网接口文档或跳转到 response 对象的定义处查看返回字段的定义 + System.out.println(res.getRequestId()); + } catch (TencentCloudSDKException e) { + e.printStackTrace(); + resValue = 2; + } + return resValue; + } + public static int sendSmsToUser(String[] phoneNumbers, String templateId, String[] templateParams) { int resValue = 0;