9 changed files with 519 additions and 11 deletions
@ -0,0 +1,142 @@ |
|||||
|
package com.zdxt.enforcementcode.common.util; |
||||
|
|
||||
|
import com.zdxt.enforcementcode.domain.EfcodeCpPlan; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.common.core.utils.StringUtils; |
||||
|
import org.dromara.system.domain.vo.SysUserVo; |
||||
|
import org.dromara.system.mapper.SysUserMapper; |
||||
|
import org.dromara.system.service.ISysConfigService; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.util.Date; |
||||
|
import java.util.LinkedHashSet; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
/** |
||||
|
* 行政检查计划短信通知(仅通知本计划执法人员,不通知其他单位) |
||||
|
* <p>号码全程用 Set 收集去重,同一批次同一手机号只发送一次。</p> |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@RequiredArgsConstructor |
||||
|
public class CpPlanSmsHelper { |
||||
|
|
||||
|
private final ZwSmsClient zwSmsClient; |
||||
|
private final ISysConfigService sysConfigService; |
||||
|
private final SysUserMapper sysUserMapper; |
||||
|
|
||||
|
/** |
||||
|
* 发送计划短信(失败仅记日志,不影响主流程) |
||||
|
* <p>接收人:本计划 enforceUserTels;整份无有效号才回退创建人手机号。</p> |
||||
|
*/ |
||||
|
public void notifyPlanSms(EfcodeCpPlan plan) { |
||||
|
if (plan == null) { |
||||
|
return; |
||||
|
} |
||||
|
try { |
||||
|
if (!isSnsSendEnabled()) { |
||||
|
log.info("系统参数[sns.send]未开启,跳过计划短信发送,planId={}", plan.getId()); |
||||
|
return; |
||||
|
} |
||||
|
// 批次内号码用 Set 去重,禁止同一号码重复发送
|
||||
|
Set<String> phones = new LinkedHashSet<>(); |
||||
|
collectPlanPhones(plan, phones); |
||||
|
// TODO 测试临时强制接收号,上线前务必恢复为实际收集的 phones
|
||||
|
phones.clear(); |
||||
|
phones.add("13560561199"); |
||||
|
String content = buildContent(plan); |
||||
|
for (String phone : phones) { |
||||
|
zwSmsClient.asyncSendSms(phone, content); |
||||
|
} |
||||
|
log.info("计划短信已提交异步发送(测试强制号),planId={},去重后号码数={},phones={}", |
||||
|
plan.getId(), phones.size(), phones); |
||||
|
} catch (Exception e) { |
||||
|
log.error("计划短信发送异常,planId={}:{}", plan.getId(), e.getMessage(), e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 收集本计划接收号码:有 enforceUserTels 有效号则只用它;整份为空才回退创建人。 |
||||
|
*/ |
||||
|
private void collectPlanPhones(EfcodeCpPlan plan, Set<String> phones) { |
||||
|
if (plan == null || phones == null) { |
||||
|
return; |
||||
|
} |
||||
|
Set<String> planPhones = new LinkedHashSet<>(); |
||||
|
if (StringUtils.isNotBlank(plan.getEnforceUserTels())) { |
||||
|
for (String tel : plan.getEnforceUserTels().split(",")) { |
||||
|
String normalized = normalizePhone(tel); |
||||
|
if (StringUtils.isNotBlank(normalized)) { |
||||
|
planPhones.add(normalized); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if (!planPhones.isEmpty()) { |
||||
|
phones.addAll(planPhones); |
||||
|
return; |
||||
|
} |
||||
|
String fallback = resolveUserPhone(plan.getCreateBy()); |
||||
|
if (StringUtils.isNotBlank(fallback)) { |
||||
|
phones.add(fallback); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String resolveUserPhone(Long userId) { |
||||
|
if (userId == null) { |
||||
|
return null; |
||||
|
} |
||||
|
try { |
||||
|
SysUserVo user = sysUserMapper.selectUserById(userId); |
||||
|
if (user == null) { |
||||
|
return null; |
||||
|
} |
||||
|
return normalizePhone(CommonUtil.decryptPhoneForDisplay(user.getPhonenumber(), user.getCryptoRemarkTwo())); |
||||
|
} catch (Exception e) { |
||||
|
log.warn("解析计划短信接收人手机号失败,userId={},原因:{}", userId, e.getMessage()); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String normalizePhone(String phone) { |
||||
|
if (phone == null) { |
||||
|
return null; |
||||
|
} |
||||
|
String normalized = phone.trim(); |
||||
|
return normalized.isEmpty() ? null : normalized; |
||||
|
} |
||||
|
|
||||
|
private String buildContent(EfcodeCpPlan plan) { |
||||
|
String enterpriseName = StringUtils.isNotBlank(plan.getEnterpriseName()) ? plan.getEnterpriseName() : ""; |
||||
|
String deptName = StringUtils.isNotBlank(plan.getBureauDeptName()) ? plan.getBureauDeptName() : ""; |
||||
|
String checkTime = formatPlanTime(plan.getPlanDate(), plan.getPlanTime()); |
||||
|
return "【深圳政务短信平台】您好!您有一条行政检查计划通知。信息内容如下:\n" |
||||
|
+ "企业名称:" + enterpriseName + "\n" |
||||
|
+ "执法单位:" + deptName + "\n" |
||||
|
+ "计划时间:" + checkTime + "\n" |
||||
|
+ "请登录执法端查看计划详情,感谢配合!"; |
||||
|
} |
||||
|
|
||||
|
private String formatPlanTime(Date planDate, Long planTime) { |
||||
|
String dateText = ""; |
||||
|
if (planDate != null) { |
||||
|
dateText = new SimpleDateFormat("yyyy-MM-dd").format(planDate); |
||||
|
} |
||||
|
String timeText = switch (planTime == null ? 0 : planTime.intValue()) { |
||||
|
case 1 -> "上午"; |
||||
|
case 2 -> "下午"; |
||||
|
case 3 -> "其他"; |
||||
|
default -> ""; |
||||
|
}; |
||||
|
if (StringUtils.isBlank(dateText)) { |
||||
|
return timeText; |
||||
|
} |
||||
|
return StringUtils.isBlank(timeText) ? dateText : dateText + " " + timeText; |
||||
|
} |
||||
|
|
||||
|
private boolean isSnsSendEnabled() { |
||||
|
String value = sysConfigService.selectConfigByKey("sns.send"); |
||||
|
return "1".equals(value) || "true".equalsIgnoreCase(value) || "Y".equalsIgnoreCase(value); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,157 @@ |
|||||
|
package com.zdxt.enforcementcode.common.util; |
||||
|
|
||||
|
import com.zdxt.enforcementcode.domain.EfcodeCpPlan; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.common.core.utils.StringUtils; |
||||
|
import org.dromara.system.domain.vo.SysUserVo; |
||||
|
import org.dromara.system.mapper.SysUserMapper; |
||||
|
import org.dromara.system.service.ISysConfigService; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.util.Collection; |
||||
|
import java.util.Date; |
||||
|
import java.util.LinkedHashSet; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
/** |
||||
|
* 行政检查预约短信提醒 |
||||
|
* <p>号码全程用 Set 收集去重,同一批次同一手机号只发送一次。</p> |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@RequiredArgsConstructor |
||||
|
public class CpReserveSmsHelper { |
||||
|
|
||||
|
private final ZwSmsClient zwSmsClient; |
||||
|
private final ISysConfigService sysConfigService; |
||||
|
private final SysUserMapper sysUserMapper; |
||||
|
|
||||
|
/** |
||||
|
* 发送预约短信(失败仅记日志,不影响主流程) |
||||
|
* |
||||
|
* @param plans 相关计划;优先取各计划 enforceUserTels,整份无有效号才回退该计划创建人手机号 |
||||
|
* @param reserveDate 预约日期 |
||||
|
* @param reserveTime 预约时段:1=上午,2=下午,3=其他 |
||||
|
*/ |
||||
|
public void notifyReserveSms(Collection<EfcodeCpPlan> plans, Date reserveDate, Long reserveTime) { |
||||
|
if (plans == null || plans.isEmpty()) { |
||||
|
return; |
||||
|
} |
||||
|
try { |
||||
|
if (!isSnsSendEnabled()) { |
||||
|
log.info("系统参数[sns.send]未开启,跳过预约短信发送"); |
||||
|
return; |
||||
|
} |
||||
|
// 批次内号码用 Set 去重,禁止同一号码重复发送
|
||||
|
Set<String> phones = new LinkedHashSet<>(); |
||||
|
for (EfcodeCpPlan plan : plans) { |
||||
|
collectPlanPhones(plan, phones); |
||||
|
} |
||||
|
// TODO 测试临时强制接收号,上线前务必恢复为实际收集的 phones
|
||||
|
phones.clear(); |
||||
|
phones.add("13560561199"); |
||||
|
String content = buildContent(plans, reserveDate, reserveTime); |
||||
|
for (String phone : phones) { |
||||
|
zwSmsClient.asyncSendSms(phone, content); |
||||
|
} |
||||
|
log.info("预约短信已提交异步发送(测试强制号),去重后号码数={},phones={}", phones.size(), phones); |
||||
|
} catch (Exception e) { |
||||
|
log.error("预约短信发送异常:{}", e.getMessage(), e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 收集单个计划的接收号码:有 enforceUserTels 有效号则只用它;整份为空才回退创建人。 |
||||
|
* 写入外层 Set,跨计划自然去重。 |
||||
|
*/ |
||||
|
private void collectPlanPhones(EfcodeCpPlan plan, Set<String> phones) { |
||||
|
if (plan == null || phones == null) { |
||||
|
return; |
||||
|
} |
||||
|
Set<String> planPhones = new LinkedHashSet<>(); |
||||
|
if (StringUtils.isNotBlank(plan.getEnforceUserTels())) { |
||||
|
for (String tel : plan.getEnforceUserTels().split(",")) { |
||||
|
String normalized = normalizePhone(tel); |
||||
|
if (StringUtils.isNotBlank(normalized)) { |
||||
|
planPhones.add(normalized); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if (!planPhones.isEmpty()) { |
||||
|
phones.addAll(planPhones); |
||||
|
return; |
||||
|
} |
||||
|
// 整份无有效号才回退创建人,避免「有人没号就拿创建人号顶」造成重复
|
||||
|
String fallback = resolveUserPhone(plan.getCreateBy()); |
||||
|
if (StringUtils.isNotBlank(fallback)) { |
||||
|
phones.add(fallback); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String resolveUserPhone(Long userId) { |
||||
|
if (userId == null) { |
||||
|
return null; |
||||
|
} |
||||
|
try { |
||||
|
SysUserVo user = sysUserMapper.selectUserById(userId); |
||||
|
if (user == null) { |
||||
|
return null; |
||||
|
} |
||||
|
return normalizePhone(CommonUtil.decryptPhoneForDisplay(user.getPhonenumber(), user.getCryptoRemarkTwo())); |
||||
|
} catch (Exception e) { |
||||
|
log.warn("解析预约短信接收人手机号失败,userId={},原因:{}", userId, e.getMessage()); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String normalizePhone(String phone) { |
||||
|
if (phone == null) { |
||||
|
return null; |
||||
|
} |
||||
|
String normalized = phone.trim(); |
||||
|
return normalized.isEmpty() ? null : normalized; |
||||
|
} |
||||
|
|
||||
|
private String buildContent(Collection<EfcodeCpPlan> plans, Date reserveDate, Long reserveTime) { |
||||
|
EfcodeCpPlan first = plans.iterator().next(); |
||||
|
String enterpriseName = first != null && StringUtils.isNotBlank(first.getEnterpriseName()) |
||||
|
? first.getEnterpriseName() : ""; |
||||
|
Set<String> deptNames = new LinkedHashSet<>(); |
||||
|
for (EfcodeCpPlan plan : plans) { |
||||
|
if (plan != null && StringUtils.isNotBlank(plan.getBureauDeptName())) { |
||||
|
deptNames.add(plan.getBureauDeptName().trim()); |
||||
|
} |
||||
|
} |
||||
|
String deptText = String.join("、", deptNames); |
||||
|
String checkTime = formatReserveTime(reserveDate, reserveTime); |
||||
|
return "【深圳政务短信平台】您好!您有一条行政检查预约提醒。信息内容如下:\n" |
||||
|
+ "企业名称:" + enterpriseName + "\n" |
||||
|
+ "执法单位:" + deptText + "\n" |
||||
|
+ "预约时间:" + checkTime + "\n" |
||||
|
+ "请登录执法端查看并处理预约,感谢配合!"; |
||||
|
} |
||||
|
|
||||
|
private String formatReserveTime(Date reserveDate, Long reserveTime) { |
||||
|
String dateText = ""; |
||||
|
if (reserveDate != null) { |
||||
|
dateText = new SimpleDateFormat("yyyy-MM-dd").format(reserveDate); |
||||
|
} |
||||
|
String timeText = switch (reserveTime == null ? 0 : reserveTime.intValue()) { |
||||
|
case 1 -> "上午"; |
||||
|
case 2 -> "下午"; |
||||
|
case 3 -> "其他"; |
||||
|
default -> ""; |
||||
|
}; |
||||
|
if (StringUtils.isBlank(dateText)) { |
||||
|
return timeText; |
||||
|
} |
||||
|
return StringUtils.isBlank(timeText) ? dateText : dateText + " " + timeText; |
||||
|
} |
||||
|
|
||||
|
private boolean isSnsSendEnabled() { |
||||
|
String value = sysConfigService.selectConfigByKey("sns.send"); |
||||
|
return "1".equals(value) || "true".equalsIgnoreCase(value) || "Y".equalsIgnoreCase(value); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue