瀏覽代碼

:art: 邮件时间更换为世界标准时UTC格式

Ritchie 1 年之前
父節點
當前提交
9bc926fa52

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

@@ -13,7 +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 com.szwl.util.TimezoneFmtUtil;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.StringRedisTemplate;
@@ -94,7 +94,7 @@ public class AlarmRecordIndexController {
         String timeZone = admin.getTimeZone();
         timeZone = StringUtils.isEmpty(timeZone) ? "Asia/Shanghai" : timeZone;
         // 根据时区获取时间
-        String localTime = TimeZoneUtil.getTimeByZoneID(timeZone);
+        String localTime = TimezoneFmtUtil.getTimeByZoneID(timeZone);
         // 查询是否绑定微信
         LambdaQueryWrapper<TWechat> wechatQuery = Wrappers.lambdaQuery();
         wechatQuery.eq(TWechat::getAdminId, admin.getId());
@@ -109,23 +109,21 @@ public class AlarmRecordIndexController {
             wechatService.sendAlarmMessage(wechat.getOpenId(), clientId, name, companyType, sendContent, alarmRecordVo.getOccurrenceTime());
         }
         if (ifForeign.equals("1")) {
-            String subject = "Error message from Magic Candy Machine";
+            String subject = "Error message from Cotton Candy Machine";
             StringBuffer content = new StringBuffer();
-            String str1 = "Dear customer<br>" +
-                    "<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>" +
+            String str1 = " <b>Machine name: </b>";
+            String str2 = "<br>" + " <b>Error message: </b>";
+            String str3 = "<br>" + " Time&Date: ";
+            String str4 = "<br>" + "<br>" + "Dear customer:<br>";
+            String str5 = " This is an automatic-sent mail to inform you that there is an error occurred on one of your Cotton Candy machines, please kindly check the details as above.<br>";
+            String str6 = " <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";
-            content.append(str1).append(name).append(str2).append(localTime).append(str3).append(alarmContent).append(str4).append(str5);
+                    "Cotton Candy Service Team";
+            content.append(str1).append(name).append(str2).append(alarmContent).append(str3).append(localTime).append(str4).append(str5).append(str6);
             if (StringUtils.isNotEmpty(messageReceiver)) {
                 String[] split = messageReceiver.split(",");
                 for (String s : split) {

+ 6 - 4
src/main/java/com/szwl/service/impl/TEquipmentServiceImpl.java

@@ -284,6 +284,8 @@ public class TEquipmentServiceImpl extends ServiceImpl<TEquipmentMapper, TEquipm
                         // 说明机器没有上传信号,判断为网络不好或者关机,发送信号通知客户 查机器归属国内还是国外
                         TAdmin admin = adminService.getById(equipment.getAdminId());
                         String ifForeign = admin.getIfForeign();
+                        String timeZone = admin.getTimeZone();
+                        timeZone = StringUtils.isEmpty(timeZone) ? "Asia/Shanghai" : timeZone;
                         // 查询是申泽还是七云的
                         String companyType = admin.getCompanyType();
                         LambdaQueryWrapper<TWechat> wechatQuery = Wrappers.lambdaQuery();
@@ -305,11 +307,11 @@ public class TEquipmentServiceImpl extends ServiceImpl<TEquipmentMapper, TEquipm
                                 if(StringUtils.isNotEmpty(messageReceiver)) {
                                     String[] split = messageReceiver.split(",");
                                     for (String s : split) {
-                                        WechatSendUtil.sentEmail(s, name);
+                                        WechatSendUtil.sentEmail(s, name, timeZone);
                                     }
                                 } else {
                                     if(StringUtils.isNotEmpty(admin.getEmail())){
-                                        WechatSendUtil.sentEmail(admin.getEmail(), name);
+                                        WechatSendUtil.sentEmail(admin.getEmail(), name, timeZone);
                                     }
                                 }
                             }
@@ -323,11 +325,11 @@ public class TEquipmentServiceImpl extends ServiceImpl<TEquipmentMapper, TEquipm
                                 if(StringUtils.isNotEmpty(messageReceiver)) {
                                     String[] split = messageReceiver.split(",");
                                     for (String s : split) {
-                                        WechatSendUtil.sentEmail(s, name);
+                                        WechatSendUtil.sentEmail(s, name, timeZone);
                                     }
                                 } else {
                                     if(StringUtils.isNotEmpty(admin.getEmail())){
-                                        WechatSendUtil.sentEmail(admin.getEmail(), name);
+                                        WechatSendUtil.sentEmail(admin.getEmail(), name, timeZone);
                                     }
                                 }
 

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

@@ -1,71 +0,0 @@
-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;
-    }
-}

+ 87 - 0
src/main/java/com/szwl/util/TimezoneFmtUtil.java

@@ -0,0 +1,87 @@
+package com.szwl.util;
+
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+
+public class TimezoneFmtUtil {
+
+
+    public static void main(String[] args) {
+//        String timeZoneByID = getTimeByZoneID("Asia/Shanghai");  // America/New_York, Asia/Shanghai
+//        System.out.println(timeZoneByID);
+
+//        String nowDateUTC = getNowDateUTC();
+//        System.out.println("nowDateUTC >>> " + nowDateUTC);
+    }
+
+
+    /**
+     * 获取 zoneID 时区的时间
+     *
+     */
+    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;
+    }
+
+
+    /**
+     * 获取中国时区的UTC格式
+     *
+     */
+//    public static String getNowDateUTC() {
+//        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
+//        ZonedDateTime now = ZonedDateTime.now(zoneId);
+//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
+//        return now.format(dateTimeFormatter);
+//    }
+
+//    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;
+//    }
+
+}

+ 20 - 26
src/main/java/com/szwl/util/WechatSendUtil.java

@@ -1,21 +1,7 @@
 package com.szwl.util;
 
-import com.alibaba.fastjson.JSONObject;
-import com.szwl.constant.ConfigConsts;
 import com.szwl.model.utils.MailUtil;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.RedisTemplate;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Date;
-import java.util.concurrent.TimeUnit;
 
 @Slf4j
 public class WechatSendUtil {
@@ -108,27 +94,35 @@ public class WechatSendUtil {
 //        }
 //        return null;
 //    }
-    public static void sentEmail(String email, String name) {
-        String subject = "Error message from Magic Candy Machine";
+
+    /**
+     * 发送邮件
+     * @param email
+     * @param name
+     * @param zoneID
+     */
+    public static void sentEmail(String email, String name, String zoneID) {
+        String subject = "Error message from Cotton Candy Machine";
         StringBuffer content = new StringBuffer();
-        String str1 = "Dear customer<br>" +
-                "<br>" +
-                " Machine name: ";
-        String str2 = "<br>" + " Time&Date: ";
-        String str3 = "<br>" + " Error Message: ";
+        String str1 = " <b>Machine name: </b>";
+        String str2 = "<br>" + " <b>Error message: </b>";
         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>" +
+        String str3 = "<br>" + " Time&Date: ";
+//        String nowDateUTC = DateTimeFmtUtil.getNowDateUTC();
+        String timeByZoneID = TimezoneFmtUtil.getTimeByZoneID(zoneID);
+        String str4 = "<br>" + "<br>" + "Dear customer:<br>";
+        String str5 = " This is an automatic-sent mail to inform you that there is an error occurred on one of your Cotton Candy machines, please kindly check the details as above.<br>";
+        String str6 = " <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";
-        content.append(str1).append(name).append(str2).append(new Date()).append(str3).append(sContent).append(str4).append(str5);
+                "Cotton Candy Service Team";
+        content.append(str1).append(name).append(str2).append(sContent).append(str3).append(timeByZoneID).append(str4).append(str5).append(str6);
         MailUtil mailUtil = new MailUtil();
         mailUtil.send(email, subject, content.toString());
         System.out.println("邮件发送成功");
     }
+
 }