Browse Source

fix:“修复报警邮件发送都为异常离线的问题"

soobin 10 months ago
parent
commit
c2d833348f

+ 3 - 28
src/main/java/com/szwl/controller/AlarmRecordIndexController.java

@@ -124,7 +124,7 @@ public class AlarmRecordIndexController {
                     if (equipmentIds.contains(equipmentId.toString()) || "0".equals(adminEquipment.getType())) {
                         // 查询子账号是否绑定微信
                         LambdaQueryWrapper<TWechat> wechatLambdaQueryWrapper = Wrappers.lambdaQuery();
-                        wechatLambdaQueryWrapper.eq(TWechat::getAdminId, admin.getId());
+                        wechatLambdaQueryWrapper.eq(TWechat::getAdminId, adminChild.getId());
                         TWechat childWechat = wechatService.getOne(wechatLambdaQueryWrapper);
                         if (childWechat != null) {
                             openIds.add(childWechat.getOpenId());
@@ -134,14 +134,6 @@ public class AlarmRecordIndexController {
             }
         }
         // 微信公众号推送
-//        if(wechat != null) {
-//            String sendContent = alarmContent;
-//            if (sendContent.length() > 20) {
-//                // 如果超过20个字符
-//                sendContent =  sendContent.substring(0, 16) + "...";
-//            }
-//            wechatService.sendAlarmMessage(wechat.getOpenId(), clientId, name, companyType, sendContent, alarmRecordVo.getOccurrenceTime());
-//        }
         if(!openIds.isEmpty()) {
             String sendContent = alarmContent;
             if (sendContent.length() > 20) {
@@ -157,32 +149,15 @@ public class AlarmRecordIndexController {
             if (StringUtils.isNotEmpty(machineType)) {
                 machineType = "0";
             }
-//            String subject = "Error message from Cotton Candy Machine";
-//            StringBuilder content = new StringBuilder();
-//            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>" +
-//                    "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) {
-//                    new MailUtil().send(s, subject, content.toString());
-                    WechatSendUtil.sentEmail(s, name, timeZone, machineType);
+                    WechatSendUtil.sentEmail(s, name, timeZone, machineType, alarmContent);
                 }
                 return "报警记录添加成功";
             } else {
                 if (email != null) {
-//                    new MailUtil().send(email, subject, content.toString());
-                    WechatSendUtil.sentEmail(admin.getEmail(), name, timeZone, machineType);
+                    WechatSendUtil.sentEmail(admin.getEmail(), name, timeZone, machineType, alarmContent);
                     return "报警记录添加成功";
                 } else {
                     return "email is null";

+ 43 - 88
src/main/java/com/szwl/util/WechatSendUtil.java

@@ -8,94 +8,6 @@ public class WechatSendUtil {
 
 
     /**
-     * 获取小程序token
-     *
-     * @return
-     */
-//    public static String getAccessToken(String companyType) {
-//        String url = "";
-//        String accessToken = "";
-//        try {
-//            if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
-//                accessToken = redisTemplate.opsForValue().get(SZ_ACCESS_TOKEN_KEY);
-//            } else {
-////                accessToken = accessTokenCache.getAccessToken(SC_ACCESS_TOKEN_KEY);
-//                accessToken = redisTemplate.opsForValue().get(SC_ACCESS_TOKEN_KEY);
-//            }
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//        if(StringUtils.isNotEmpty(accessToken)) {
-//            return accessToken;
-//        }
-//        if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
-//            url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
-//                    + ConfigConsts.SZ_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SZ_WX_APP_SECRET;
-//        } else {
-//            url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
-//                    + ConfigConsts.SC_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SC_WX_APP_SECRET;
-//        }
-//        PrintWriter out = null;
-//        BufferedReader in = null;
-//        String line;
-//        StringBuffer stringBuffer = new StringBuffer();
-//        try {
-//            URL realUrl = new URL(url);
-//            // 打开和URL之间的连接
-//            URLConnection conn = realUrl.openConnection();
-//
-//            // 设置通用的请求属性 设置请求格式
-//            //设置返回类型
-//            conn.setRequestProperty("contentType", "text/plain");
-//            //设置请求类型
-//            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
-//            //设置超时时间
-//            conn.setConnectTimeout(1000);
-//            conn.setReadTimeout(1000);
-//            conn.setDoOutput(true);
-//            conn.connect();
-//            // 获取URLConnection对象对应的输出流
-//            out = new PrintWriter(conn.getOutputStream());
-//            // flush输出流的缓冲
-//            out.flush();
-//            // 定义BufferedReader输入流来读取URL的响应    设置接收格式
-//            in = new BufferedReader(
-//                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
-//            while ((line = in.readLine()) != null) {
-//                stringBuffer.append(line);
-//            }
-//            JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
-//            log.info("获取token:{}",jsonObject.toString());
-//            accessToken = jsonObject.getString("access_token");
-//            if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
-////                accessTokenCache.setAccessToken(SZ_ACCESS_TOKEN_KEY, accessToken);
-//                redisTemplate.opsForValue().set(SZ_ACCESS_TOKEN_KEY, accessToken, EXPIRATION_TIME, TimeUnit.SECONDS);
-//            } else {
-////                accessTokenCache.setAccessToken(SC_ACCESS_TOKEN_KEY, accessToken);
-//                redisTemplate.opsForValue().set(SZ_ACCESS_TOKEN_KEY, accessToken, EXPIRATION_TIME, TimeUnit.SECONDS);
-//            }
-//            return accessToken;
-//
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//        //使用finally块来关闭输出流、输入流
-//        finally {
-//            try {
-//                if (out != null) {
-//                    out.close();
-//                }
-//                if (in != null) {
-//                    in.close();
-//                }
-//            } catch (IOException ex) {
-//                ex.printStackTrace();
-//            }
-//        }
-//        return null;
-//    }
-
-    /**
      * 发送邮件
      * @param email
      * @param name
@@ -139,4 +51,47 @@ public class WechatSendUtil {
         System.out.println("邮件发送成功");
     }
 
+    /**
+     * 发送邮件
+     * @param email
+     * @param name
+     * @param zoneID
+     */
+    public static void sentEmail(String email, String name, String zoneID, String machineType, String alarmContent) {
+        String machineTypeStr = "";
+        switch (machineType) {
+            case "0":
+                machineTypeStr = "Cotton Candy";  // 棉花糖
+                break;
+            case "1":
+                machineTypeStr = "Popcorn";  // 爆米花
+                break;
+            case "2":
+                machineTypeStr = "Ice Cream";  // 冰淇淋
+                break;
+            default:
+                machineTypeStr = "";
+        }
+        String subject = "Error message from " + machineTypeStr + " Machine";
+        StringBuffer content = new StringBuffer();
+        String str1 = " <b>Machine name: </b>";
+        String str2 = "<br>" + " <b>Error message: </b>";
+//        String sContent = "Abnormal shutdown/The network is disconnected.";
+        String str3 = "<br>" + " Time&Date: ";
+        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 " + machineTypeStr + " 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>" +
+                "Cotton Candy Service Team";
+        content.append(str1).append(name).append(str2).append(alarmContent).append(str3).append(timeByZoneID).append(str4).append(str5).append(str6);
+        MailUtil mailUtil = new MailUtil();
+        mailUtil.send(email, subject, content.toString());
+        System.out.println("邮件发送成功");
+    }
+
 }