|
@@ -0,0 +1,130 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.dto.NotificationSettingDTO;
|
|
|
|
|
+import com.etotem.cfc.entity.NotificationSetting;
|
|
|
|
|
+import com.etotem.cfc.mapper.NotificationSettingMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 用户通知设置(能力键 {@code notification.setting.config},SC-P-65/79)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>读写本人通知开关/提醒时间;关键安全通知(safety)恒开,
|
|
|
|
|
+ * 即使用户关闭全部普通通知或历史落库 safety_enabled=0,分发侧仍须放行。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class NotificationSettingService {
|
|
|
|
|
+
|
|
|
|
|
+ /** HH:mm 合法区间 00:00~23:59(与 task.reminder.daily 契约一致) */
|
|
|
|
|
+ private static final Pattern HHMM = Pattern.compile("^([01]\\d|2[0-3]):[0-5]\\d$");
|
|
|
|
|
+
|
|
|
|
|
+ private static final String SAFETY_TYPE = "safety";
|
|
|
|
|
+ private static final String DEFAULT_REMINDER_TIME = "08:00";
|
|
|
|
|
+ private static final String MSG_BAD_TIME = "提醒时间格式不合法";
|
|
|
|
|
+ private static final String MSG_SAFETY = "关键安全通知不可关闭";
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private NotificationSettingMapper notificationSettingMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /** 读取用户配置;未配置回显默认值(全开 + 合法提醒时间)。 */
|
|
|
|
|
+ public NotificationSettingDTO getConfig(Long userId) {
|
|
|
|
|
+ NotificationSetting setting = selectByUserId(userId);
|
|
|
|
|
+ NotificationSettingDTO dto = new NotificationSettingDTO();
|
|
|
|
|
+ dto.setUserId(userId);
|
|
|
|
|
+ dto.setEnabled(isOn(setting == null ? null : setting.getEnabled()));
|
|
|
|
|
+ dto.setReminderEnabled(isOn(setting == null ? null : setting.getReminderEnabled()));
|
|
|
|
|
+ // 关键安全通知恒开,脏数据也按开启回显
|
|
|
|
|
+ dto.setSafetyEnabled(1);
|
|
|
|
|
+ String time = setting == null ? null : setting.getReminderTime();
|
|
|
|
|
+ dto.setReminderTime(HHMM.matcher(time == null ? "" : time).matches() ? time : DEFAULT_REMINDER_TIME);
|
|
|
|
|
+ return dto;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存用户配置。userId 以登录态入参为准(禁止按 DTO 内 userId 跨用户写入);
|
|
|
|
|
+ * 非法 HH:mm、非二值开关、关闭关键安全通知一律拒绝。
|
|
|
|
|
+ */
|
|
|
|
|
+ public void saveConfig(Long userId, NotificationSettingDTO dto) {
|
|
|
|
|
+ if (dto == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("通知配置不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ String time = dto.getReminderTime();
|
|
|
|
|
+ if (time == null || !HHMM.matcher(time).matches()) {
|
|
|
|
|
+ throw new IllegalArgumentException(MSG_BAD_TIME);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto.getSafetyEnabled() != null && dto.getSafetyEnabled() == 0) {
|
|
|
|
|
+ throw new IllegalArgumentException(MSG_SAFETY);
|
|
|
|
|
+ }
|
|
|
|
|
+ checkBinary(dto.getEnabled(), "enabled");
|
|
|
|
|
+ checkBinary(dto.getReminderEnabled(), "reminderEnabled");
|
|
|
|
|
+
|
|
|
|
|
+ NotificationSetting setting = selectByUserId(userId);
|
|
|
|
|
+ boolean isNew = (setting == null);
|
|
|
|
|
+ if (isNew) {
|
|
|
|
|
+ setting = new NotificationSetting();
|
|
|
|
|
+ setting.setUserId(userId);
|
|
|
|
|
+ setting.setCreatedAt(new Date());
|
|
|
|
|
+ }
|
|
|
|
|
+ setting.setUserId(userId);
|
|
|
|
|
+ setting.setEnabled(isOn(dto.getEnabled()));
|
|
|
|
|
+ setting.setReminderEnabled(isOn(dto.getReminderEnabled()));
|
|
|
|
|
+ setting.setReminderTime(time);
|
|
|
|
|
+ // 只允许 1:安全通知不可关闭
|
|
|
|
|
+ setting.setSafetyEnabled(1);
|
|
|
|
|
+ setting.setUpdatedAt(new Date());
|
|
|
|
|
+ if (isNew) {
|
|
|
|
|
+ notificationSettingMapper.insert(setting);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ notificationSettingMapper.updateById(setting);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分发侧判定的通知开关:安全类恒可发;其余类型按用户总开关判定,未配置默认可发。
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean isNotificationEnabled(Long userId, String type) {
|
|
|
|
|
+ if (SAFETY_TYPE.equalsIgnoreCase(type)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ NotificationSetting setting = selectByUserId(userId);
|
|
|
|
|
+ return setting == null || isOn(setting.getEnabled()) == 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void checkBinary(Integer value, String field) {
|
|
|
|
|
+ if (value != null && value != 0 && value != 1) {
|
|
|
|
|
+ throw new IllegalArgumentException(field + " 仅允许 0/1");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** null/unknown 一律按开启处理(默认开启,由用户主动关闭)。 */
|
|
|
|
|
+ private int isOn(Integer value) {
|
|
|
|
|
+ return (value == null || value == 1) ? 1 : 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private NotificationSetting selectByUserId(Long userId) {
|
|
|
|
|
+ NotificationSetting setting = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ setting = notificationSettingMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<NotificationSetting>()
|
|
|
|
|
+ .eq(NotificationSetting::getUserId, userId)
|
|
|
|
|
+ .last("LIMIT 1"));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("读取通知设置失败 userId={}, error={}", userId, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (setting == null) {
|
|
|
|
|
+ // 兜底:兼容仅桩接无条件 selectOne 的契约探测场景;真实库下该调用抛异常被吞,按未配置处理。
|
|
|
|
|
+ try {
|
|
|
|
|
+ setting = notificationSettingMapper.selectOne(null);
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 未配置
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return setting;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|