Просмотр исходного кода

:sparkles: 报警邮件时间可指定账户所在时区

Ritchie 1 год назад
Родитель
Сommit
a6c17f417e

+ 15 - 11
src/main/java/com/szwl/controller/AlarmRecordIndexController.java

@@ -13,6 +13,7 @@ import com.szwl.service.TAdminService;
 import com.szwl.service.TAlarmRecordService;
 import com.szwl.service.TEquipmentService;
 import com.szwl.service.TWechatService;
+import com.szwl.util.TimeZoneUtil;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.StringRedisTemplate;
@@ -52,7 +53,6 @@ public class AlarmRecordIndexController {
     @ResponseBody
     public String addAlarmRecord(@RequestBody AlarmRecordVo alarmRecordVo) {
 
-
         TAlarmRecord alarmRecord = new TAlarmRecord();
 
         String clientId = alarmRecordVo.getClientId();
@@ -91,6 +91,10 @@ public class AlarmRecordIndexController {
         String ifForeign = admin.getIfForeign();
         String companyType = admin.getCompanyType();
         String messageReceiver = equipment.getMessageReceiver();
+        String timeZone = admin.getTimeZone();
+        timeZone = StringUtils.isEmpty(timeZone) ? "Asia/Shanghai" : timeZone;
+        // 根据时区获取时间
+        String localTime = TimeZoneUtil.getTimeByZoneID(timeZone);
         // 查询是否绑定微信
         LambdaQueryWrapper<TWechat> wechatQuery = Wrappers.lambdaQuery();
         wechatQuery.eq(TWechat::getAdminId, admin.getId());
@@ -106,31 +110,31 @@ public class AlarmRecordIndexController {
         }
         if (ifForeign.equals("1")) {
             String subject = "Error message from Magic Candy Machine";
-            StringBuffer contnet = new StringBuffer();
+            StringBuffer content = new StringBuffer();
             String str1 = "Dear customer<br>" +
                     "<br>" +
-                    "This is an automatic-sent mail to inform you that there is an error occurred on one of your Magic Candy machines, please kindly check the detail as below.<br>" +
-                    "<br>" +
-                    " Machine name:";
-            String str2 = "<br>" + " Time&Date:";
-            String str3 = "<br>" + " Error Message:";
-            String str4 = "<br>" + " <br>You don't have to reply this mail . What you need to do is to follow the instructions on the touch screen to clear the error and recover the machine. If there is any question or more information you need . Please do not be hesitated to contact your distributor.<br>" +
+                    " Machine name: ";
+            String str2 = "<br>" + " Time&Date: ";
+            String str3 = "<br>" + " Error Message: ";
+            String str4 = "<br>" + "<br>" +
+                    " This is an automatic-sent mail to inform you that there is an error occurred on one of your Magic Candy machines, please kindly check the details as above.<br>";
+            String str5 = " <br>You don't have to reply this mail. What you need to do is to follow the instructions on the touch screen to clear the error and recover the machine. If there is any question or more information you need. Please do not be hesitated to contact your distributor.<br>" +
                     "<br>" +
                     " Thank you for choosing our machine!<br>" +
                     "<br>" +
                     "<br>" +
                     "Best Regards.<br>" +
                     "Magic Candy Service Team";
-            contnet.append(str1).append(name).append(str2).append(alarmRecordVo.getOccurrenceTime()).append(str3).append(alarmContent).append(str4);
+            content.append(str1).append(name).append(str2).append(localTime).append(str3).append(alarmContent).append(str4).append(str5);
             if (StringUtils.isNotEmpty(messageReceiver)) {
                 String[] split = messageReceiver.split(",");
                 for (String s : split) {
-                    new MailUtil().send(s, subject, contnet.toString());
+                    new MailUtil().send(s, subject, content.toString());
                 }
                 return "报警记录添加成功";
             } else {
                 if (email != null) {
-                    new MailUtil().send(email, subject, contnet.toString());
+                    new MailUtil().send(email, subject, content.toString());
                     return "报警记录添加成功";
                 } else {
                     return "email is null";

+ 2 - 3
src/main/java/com/szwl/controller/TAdminController.java

@@ -3,7 +3,6 @@ package com.szwl.controller;
 
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
-//import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -42,11 +41,11 @@ import java.util.stream.Collectors;
 
 /**
  * <p>
- * 短信验证码 前端控制器
+ *  前端控制器
  * </p>
  *
  * @author wuhs
- * @since 2023-09-26
+ * @since 2024-07-01
  */
 @Slf4j
 @Api(value = "/tAdmin", tags = {"账户"})

+ 1 - 1
src/main/java/com/szwl/mapper/TAdminMapper.java

@@ -14,7 +14,7 @@ import java.util.List;
  * </p>
  *
  * @author wuhs
- * @since 2023-09-26
+ * @since 2024-07-01
  */
 public interface TAdminMapper extends BaseMapper<TAdmin> {
 

Разница между файлами не показана из-за своего большого размера
+ 2 - 1
src/main/java/com/szwl/mapper/xml/TAdminMapper.xml


+ 0 - 1
src/main/java/com/szwl/model/dto/AlarmRecordVo.java

@@ -6,7 +6,6 @@ import java.util.Date;
 
 public class AlarmRecordVo {
 
-
     /**
      * 设备唯一码
      */

+ 4 - 1
src/main/java/com/szwl/model/entity/TAdmin.java

@@ -19,7 +19,7 @@ import lombok.EqualsAndHashCode;
  * </p>
  *
  * @author wuhs
- * @since 2024-06-24
+ * @since 2024-07-01
  */
 @Data
 @EqualsAndHashCode(callSuper = false)
@@ -168,5 +168,8 @@ public class TAdmin implements Serializable {
     @ApiModelProperty(value = "设备上线提醒,0:关闭,1:开启,默认开启")
     private String onOffNotice;
 
+    @ApiModelProperty(value = "账户所在时区,默认北京时区")
+    private String timeZone;
+
 
 }

+ 1 - 1
src/main/java/com/szwl/service/TAdminService.java

@@ -15,7 +15,7 @@ import java.util.List;
  * </p>
  *
  * @author wuhs
- * @since 2023-09-26
+ * @since 2024-07-01
  */
 public interface TAdminService extends MyIService<TAdmin> {
     List<TAdmin> listByXml(TAdminParam param);

+ 1 - 7
src/main/java/com/szwl/service/impl/TAdminServiceImpl.java

@@ -7,24 +7,18 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.szwl.manager.TokenManager;
-import com.szwl.model.bo.R;
-import com.szwl.model.bo.ResponseModel;
 import com.szwl.model.bo.UserDetailBO;
 import com.szwl.model.entity.SysRole;
 import com.szwl.model.entity.SysUserRole;
 import com.szwl.model.entity.TAdmin;
 import com.szwl.mapper.TAdminMapper;
-import com.szwl.model.entity.TArea;
 import com.szwl.model.query.TAdminParam;
 import com.szwl.service.SysRoleService;
 import com.szwl.service.SysUserRoleService;
 import com.szwl.service.TAdminService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import javax.annotation.Resource;
 import java.util.List;
 import java.util.Set;
 
@@ -34,7 +28,7 @@ import java.util.Set;
  * </p>
  *
  * @author wuhs
- * @since 2023-09-26
+ * @since 2024-07-01
  */
 @Service
 public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements TAdminService {

+ 71 - 0
src/main/java/com/szwl/util/TimeZoneUtil.java

@@ -0,0 +1,71 @@
+package com.szwl.util;
+
+import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+public class TimeZoneUtil {
+    public static String getTimeZoneByID(String timeZoneID) {
+        // 获取时区对象
+        TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
+
+        // 设置时区
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        sdf.setTimeZone(timeZone);
+
+        // 获取当前时间
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeZone(timeZone);
+        Date date = calendar.getTime();
+//        String displayName = timeZone.getDisplayName();
+
+        // 计算UTC偏移量
+        int rawOffset = timeZone.getRawOffset();
+        int rawOffsetHours = rawOffset / (1000 * 60 * 60);
+
+        int hours = 0;
+//        String formatted = null;
+        if (ZoneId.getAvailableZoneIds().contains(timeZoneID)) {
+            ZoneId zoneId = ZoneId.of(timeZoneID);
+
+//            ZonedDateTime now = ZonedDateTime.now(zoneId);
+//            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
+//            formatted = now.format(dateTimeFormatter);
+
+
+            ZoneOffset offset = zoneId.getRules().getOffset(Instant.now());  // 获取当前时刻的偏移量
+            int totalSeconds = offset.getTotalSeconds();
+            hours = totalSeconds / (60 * 60);
+        } else {
+            System.out.println("zoneID有误!");
+        }
+
+        return sdf.format(date) + " UTC " + hours;
+//        return formatted;
+    }
+
+    public static void main(String[] args) {
+        String timeZoneByID = getTimeByZoneID("America/New_York");  // America/New_York, Asia/Shanghai
+        System.out.println(timeZoneByID);
+    }
+
+
+    public static String getTimeByZoneID(String zoneID) {
+        String formatted = null;
+        if (ZoneId.getAvailableZoneIds().contains(zoneID)) {
+            ZoneId zoneId = ZoneId.of(zoneID);
+            ZonedDateTime now = ZonedDateTime.now(zoneId);
+            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
+            formatted = now.format(dateTimeFormatter);
+        } else {
+            System.out.println("zoneID有误!");
+        }
+        return formatted;
+    }
+}

+ 13 - 13
src/main/java/com/szwl/util/WechatSendUtil.java

@@ -23,6 +23,7 @@ public class WechatSendUtil {
 
     /**
      * 获取小程序token
+     *
      * @return
      */
 //    public static String getAccessToken(String companyType) {
@@ -107,28 +108,27 @@ public class WechatSendUtil {
 //        }
 //        return null;
 //    }
-
-    public static void sentEmail(String email,String name) {
+    public static void sentEmail(String email, String name) {
         String subject = "Error message from Magic Candy Machine";
-        StringBuffer contnet = new StringBuffer();
-        String str1="Dear customer<br>" +
-                "<br>" +
-                "This is an automatic-sent mail to inform you that there is an error occurred on one of your Magic Candy machines, please kindly check the detail as below.<br>" +
+        StringBuffer content = new StringBuffer();
+        String str1 = "Dear customer<br>" +
                 "<br>" +
-                " Machine name:";
-        String str2="<br>" +" China Time(UTC/GMT +8:00):";
-        String str3="<br>"+" Error Message:";
-        String scontent="Abnormal shutdown/The network is disconnected,";
-        String str4= "<br>" +" <br>You don't have to reply this mail . What you need to do is to follow the instructions on the touch screen to clear the error and recover the machine. If there is any question or more information you need . Please do not be hesitated to contact your distributor.<br>" +
+                " Machine name: ";
+        String str2 = "<br>" + " Time&Date: ";
+        String str3 = "<br>" + " Error Message: ";
+        String sContent = "Abnormal shutdown/The network is disconnected.";
+        String str4 = "<br>" + "<br>" +
+                " This is an automatic-sent mail to inform you that there is an error occurred on one of your Magic Candy machines, please kindly check the details as above.<br>";
+        String str5 = " <br>You don't have to reply this mail. What you need to do is to follow the instructions on the touch screen to clear the error and recover the machine. If there is any question or more information you need. Please do not be hesitated to contact your distributor.<br>" +
                 "<br>" +
                 " Thank you for choosing our machine!<br>" +
                 "<br>" +
                 "<br>" +
                 "Best Regards.<br>" +
                 "Magic Candy Service Team";
-        contnet.append(str1).append(name).append(str2).append(new Date()).append(str3).append(scontent).append(str4);
+        content.append(str1).append(name).append(str2).append(new Date()).append(str3).append(sContent).append(str4).append(str5);
         MailUtil mailUtil = new MailUtil();
-        mailUtil.send(email, subject, contnet.toString());
+        mailUtil.send(email, subject, content.toString());
         System.out.println("邮件发送成功");
     }
 }

+ 1 - 1
src/test/java/com/szwl/AutoGeneratorTests.java

@@ -47,7 +47,7 @@ class AutoGeneratorTests {
 		strategyConfig
 //				.setCapitalMode(true)//设置全局大写命名
 				.setInclude(new String[]{
-						"t_alarm_clean"
+						"t_admin"
 				})//只会生成该表
 				.setEntityLombokModel(true)//实体类生成之后自动添加lombok注解
 				.setNaming(NamingStrategy.underline_to_camel)//数据库表映射到实体的命名策略