系统用户添加代理公司概念,由代理公司管理其所管辖范围内的系统用户。

This commit is contained in:
liuchengqian 2022-07-04 11:01:58 +08:00
parent d4de89b9fd
commit c3ecdee6d7
5 changed files with 426 additions and 66 deletions

View File

@ -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<AgentCompanyEntity, Long>, JpaSpecificationExecutor<AgentCompanyEntity> {
@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<AgentCompanyEntity> selectCompanyNameNotEquals(String companyName);
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -89,6 +89,9 @@ public class FirePointServiceImpl implements FirePointService {
@Resource @Resource
private GlobalConfigService globalConfigService; private GlobalConfigService globalConfigService;
@Resource
private AgentCompanyDao agentCompanyDao;
/** /**
* 添加火点信息 * 添加火点信息
* *
@ -408,75 +411,36 @@ public class FirePointServiceImpl implements FirePointService {
/** /**
* 发送短信消息 * 发送短信消息
*
* @param firePointEntity
*/ */
private void sendSMSMessage(FirePointEntity firePointEntity) { private void sendSMSMessage(FirePointEntity firePointEntity) {
List<SysUserEntity> sysUserList = sysUserDao.findAll(); List<SysUserEntity> sysUserList = sysUserDao.findAll();
if (sysUserList.isEmpty()) { List<RelRoleAuthorityEntity> adminList = relRoleAuthorityDao.selectByAuthorityId(1);
return; boolean normalSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(9L);
boolean vipSmsEnable = 1L == globalConfigService.selectGlobalConfigValue(10L);
List<SysUserEntity> smsReceiverList = FirePointCodeUtils.filterSmsReceiver(firePointEntity.getStreetCode(), sysUserList, adminList, normalSmsEnable, vipSmsEnable);
//对收信人进行分组
AgentCompanyEntity master = agentCompanyDao.selectCompanyNameEquals("青岛星科瑞升信息科技有限公司");
List<AgentCompanyEntity> agentCompanyList = agentCompanyDao.selectCompanyNameNotEquals("青岛星科瑞升信息科技有限公司");
Map<AgentCompanyEntity, List<SysUserEntity>> agentCompanyGroupMap = FirePointCodeUtils.groupSmsReceiver(smsReceiverList, master, agentCompanyList);
//批量发送短信通知
for (Map.Entry<AgentCompanyEntity, List<SysUserEntity>> entry : agentCompanyGroupMap.entrySet()) {
AgentCompanyEntity groupAgentCompany = entry.getKey();
List<SysUserEntity> 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();
} }
List<RelRoleAuthorityEntity> administratorList = relRoleAuthorityDao.selectByAuthorityId(1); SMSUtils.sendSmsToUser(groupAgentCompany, telephoneArray, new String[]{firePointEntity.getFirePointAddress()});
List<String> 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());
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (telephoneList.size() > 0) {
String[] telephoneArray = telephoneList.toArray(new String[]{});
SendSms.sendSmsToUser(telephoneArray, "1425995", new String[]{firePointEntity.getFirePointAddress()});
StringBuilder stringBuilder = new StringBuilder("发送短信消息通知:\n"); StringBuilder stringBuilder = new StringBuilder("发送短信消息通知:\n");
for (String telephone : telephoneArray) { for (String telephone : telephoneArray) {
stringBuilder.append("telephone:").append(telephone).append("\n"); stringBuilder.append("telephone:").append(telephone).append("\n");
} }
stringBuilder.append("短信模版ID1425995\n").append("短信内容:").append(firePointEntity.getFirePointAddress()); stringBuilder.append(groupAgentCompany.toString()).append("短信内容:").append(firePointEntity.getFirePointAddress());
log.info(stringBuilder.toString()); log.info(stringBuilder.toString());
} }
} }
/**
* 判断是否是管理员
*/
private boolean isAdministrator(List<RelRoleAuthorityEntity> 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;
} }
/** /**

View File

@ -1,5 +1,16 @@
package com.xkrs.utils; 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 { public class FirePointCodeUtils {
private FirePointCodeUtils() { private FirePointCodeUtils() {
@ -13,4 +24,116 @@ public class FirePointCodeUtils {
return codeNotZeroEnd; return codeNotZeroEnd;
} }
/**
* 过滤短信通知收信人
*
* @param firePointStreetCode 火点的街道编号
* @param sysUserList 系统用户列表
* @param adminList 管理员列表
* @param normalSmsEnable 普通用户能否接收短信通知
* @param vipSmsEnable VIP用户能否接收短信通知
* @return 短信通知收信人列表
*/
public static List<SysUserEntity> filterSmsReceiver(String firePointStreetCode, List<SysUserEntity> sysUserList, List<RelRoleAuthorityEntity> adminList, boolean normalSmsEnable, boolean vipSmsEnable) {
List<SysUserEntity> 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<RelRoleAuthorityEntity> 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<AgentCompanyEntity, List<SysUserEntity>> groupSmsReceiver(List<SysUserEntity> smsReceiverList, AgentCompanyEntity master, List<AgentCompanyEntity> agentCompanyList) {
//数据仓库
Map<AgentCompanyEntity, List<SysUserEntity>> 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<SysUserEntity> 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<SysUserEntity> masterReceiverList = new ArrayList<>();
for (Map.Entry<AgentCompanyEntity, List<SysUserEntity>> entry : groupMap.entrySet()) {
for (SysUserEntity smsReceiver : entry.getValue()) {
if (!smsReceiverList.contains(smsReceiver)) {
masterReceiverList.add(smsReceiver);
}
}
}
groupMap.put(master, masterReceiverList);
return groupMap;
}
} }

View File

@ -9,13 +9,111 @@ import com.tencentcloudapi.sms.v20190711.models.PullSmsReplyStatusByPhoneNumberR
import com.tencentcloudapi.sms.v20190711.models.PullSmsReplyStatusByPhoneNumberResponse; import com.tencentcloudapi.sms.v20190711.models.PullSmsReplyStatusByPhoneNumberResponse;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; 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 helperhttps://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) { public static int sendSmsToUser(String[] phoneNumbers, String templateId, String[] templateParams) {
int resValue = 0; int resValue = 0;