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

feat:“添加设备离线提醒功能“

soobin 1 год назад
Родитель
Сommit
4e0b3c6688

+ 10 - 0
src/main/java/com/szwl/constant/ConfigConsts.java

@@ -47,6 +47,16 @@ public class ConfigConsts {
     public static final String SC_WX_APP_SECRET = "f131c85c06a80e466bd7e366cd477b93";
 
     /**
+     * 申泽商户模板ID
+     */
+    public static final String SZ_TEMPLATE_ID = "4h0WwP60QR_6nzhkhEJTcYJHeZApCwxGYggKUNProW4";
+
+    /**
+     * 七云商户模板ID
+     */
+    public static final String SC_TEMPLATE_ID = "ilJA4oCUn_4ffPzS0PrWFKnNS7UX_j-PxjkNqLwYaCw";
+
+    /**
      *
      */
     public static final String SZ_RETURN_URL = "http://szwlh.sunzee.com.cn";

+ 8 - 0
src/main/java/com/szwl/controller/ScheduledService.java

@@ -140,4 +140,12 @@ public class ScheduledService {
             }
         }
     }
+
+    // 设备状态检测
+    @Scheduled(cron = "0 30 * * * ?")
+    public void checkEquipmentStatus() {
+        if (isDo()) {
+            equipmentService.checkEquipmentStatus();
+        }
+    }
 }

+ 13 - 20
src/main/java/com/szwl/controller/TEquipmentController.java

@@ -29,10 +29,12 @@ import com.szwl.model.entity.*;
 import com.szwl.model.param.PasswordParam;
 import com.szwl.model.query.StatisticsParam;
 import com.szwl.model.utils.DateUtils;
+import com.szwl.model.utils.MailUtil;
 import com.szwl.model.utils.PushUtils;
 import com.szwl.model.vo.EquipmentVO;
 import com.szwl.service.*;
 import com.szwl.util.DownloadUtils;
+import com.szwl.util.WechatSendUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang.StringUtils;
@@ -43,6 +45,7 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.net.URL;
@@ -1118,16 +1121,11 @@ public class TEquipmentController {
 
     @ApiOperation(value = "获取所有机器开关机状态")
     @PostMapping("/equipmentStatus")
-    public ResponseEntity<?> equipmentStatus(@RequestBody StatisticsParam param) {
+    public ResponseModel<?> equipmentStatus(@RequestBody StatisticsParam param) {
         String adminId = param.getAdminId();
         //碧桂园的机器太多,不查
         if (adminId.equals("236") || adminId.equals("31")) {
-            return ResponseEntity
-                    .status(HttpStatus.OK)
-                    .body(new ResultMessage()
-                            .setCode(true)
-                            .setData("SUCCESS")
-                            .setMessage("SUCCESS"));
+            return R.ok();
         }
         LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
         query.eq(TEquipment::getAdminId, Long.valueOf(adminId));
@@ -1138,24 +1136,23 @@ public class TEquipmentController {
 //                PushUtils.push(equipment.getGtClientId(), "", "", PushUtils.buildJson("onoffstatus", "0").toString());
             }
         }
-        return ResponseEntity
-                .status(HttpStatus.OK)
-                .body(new ResultMessage()
-                        .setCode(true)
-                        .setData("SUCCESS")
-                        .setMessage("SUCCESS"));
+        return R.ok();
     }
 
     @ApiOperation(value = "检验所有机器是否开机")
     @PostMapping("/checkEquipmentStatus")
-    public ResponseEntity<?> checkEquipmentStatus(@RequestBody StatisticsParam param) {
+    public ResponseModel<?> checkEquipmentStatus(@RequestBody StatisticsParam param) {
+        // 通过管理员ID查询所有设备
         String adminId = param.getAdminId();
         LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
         query.eq(TEquipment::getAdminId, Long.valueOf(adminId));
         List<TEquipment> equipmentList = tEquipmentService.list(query);
+
+        // 获取当前时间,并检查设备状态是否过期
         long nowTime = System.currentTimeMillis();
         if (equipmentList.size() > 0) {
             for (TEquipment equipment : equipmentList) {
+                // 如果设备的最后修改时间超过310秒前,则清除设备状态信息
                 long modifyTime = equipment.getModifyDate().getTime();
                 if (nowTime > modifyTime && (nowTime - modifyTime) > 310000) {
                     equipment.setCabinetTm("");
@@ -1166,12 +1163,8 @@ public class TEquipmentController {
                 }
             }
         }
-        return ResponseEntity
-                .status(HttpStatus.OK)
-                .body(new ResultMessage()
-                        .setCode(true)
-                        .setData("SUCCESS")
-                        .setMessage("SUCCESS"));
+        // 返回操作成功的响应
+        return R.ok();
     }
 
     @ApiOperation(value = "机器开关机")

+ 2 - 2
src/main/java/com/szwl/controller/TProportionController.java

@@ -278,12 +278,12 @@ public class TProportionController {
         if(tProportion.getAdminId()==null||StringUtils.isEmpty(tProportion.getClientId())){
             R.fail("信息有空!");
         }
-        TAdmin admin = adminService.getById(tProportion.getAdminId());
+//        TAdmin admin = adminService.getById(tProportion.getAdminId());
         //查找机器,看改商家是否有该机器
         LambdaQueryWrapper<TEquipment> query1 = Wrappers.lambdaQuery();
         query1.eq(TEquipment::getAdminId,tProportion.getAdminId());
         query1.eq(TEquipment::getClientId,tProportion.getClientId());
-        List<TEquipment> equipmentList = tEquipmentService.list(query1);
+//        List<TEquipment> equipmentList = tEquipmentService.list(query1);
         TEquipment equipment = tEquipmentService.getOne(query1);
         if(equipment == null){
             return R.fail("找不到该机器!");

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


+ 28 - 0
src/main/java/com/szwl/model/bean/WeChatTemplateMsg.java

@@ -0,0 +1,28 @@
+package com.szwl.model.bean;
+
+import lombok.Data;
+import lombok.ToString;
+
+@Data
+@ToString
+public class WeChatTemplateMsg {
+    /**
+     * 消息
+     */
+    private String value;
+    /**
+     * 消息颜色
+     */
+    private String color;
+
+
+    public WeChatTemplateMsg(String value) {
+        this.value = value;
+        this.color = "#173177";
+    }
+
+    public WeChatTemplateMsg(String value, String color) {
+        this.value = value;
+        this.color = color;
+    }
+}

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

@@ -19,7 +19,7 @@ import lombok.EqualsAndHashCode;
  * </p>
  *
  * @author wuhs
- * @since 2024-03-08
+ * @since 2024-04-13
  */
 @Data
 @EqualsAndHashCode(callSuper = false)
@@ -29,7 +29,6 @@ public class TAdmin implements Serializable {
     private static final long serialVersionUID = 1L;
 
     @TableId(value = "id", type = IdType.AUTO)
-//    @TableId(value = "id", type = IdType.ASSIGN_ID)
     private Long id;
 
     @ApiModelProperty(value = "创建时间")
@@ -154,5 +153,8 @@ public class TAdmin implements Serializable {
     @ApiModelProperty(value = "微信支付配置ID")
     private Long wechatPayId;
 
+    @ApiModelProperty(value = "支付宝支付配置ID")
+    private Long alipayId;
+
 
 }

+ 5 - 0
src/main/java/com/szwl/service/TEquipmentService.java

@@ -27,4 +27,9 @@ public interface TEquipmentService extends MyIService<TEquipment> {
     Optional<String> syncOneEquipmentByClientId(String clientId);
 
     int getForeignEquipmentCount(String area); // 0 国内,1 海外,2 全球
+
+    /**
+     * 检查设备
+     */
+    void checkEquipmentStatus();
 }

+ 1 - 0
src/main/java/com/szwl/service/TWechatService.java

@@ -13,4 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface TWechatService extends IService<TWechat> {
 
+    String sendNoworkMessage(String openId, String clientId, String name, String ifForeign, String companyType);
 }

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

@@ -7,11 +7,14 @@ import com.szwl.model.bo.R;
 import com.szwl.model.entity.TAdmin;
 import com.szwl.model.entity.TEquipment;
 import com.szwl.mapper.TEquipmentMapper;
+import com.szwl.model.entity.TWechat;
 import com.szwl.model.query.StatisticsParam;
 import com.szwl.model.utils.PushUtils;
 import com.szwl.service.TAdminService;
 import com.szwl.service.TEquipmentService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.szwl.service.TWechatService;
+import com.szwl.util.WechatSendUtil;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.amqp.core.AmqpTemplate;
 import org.springframework.amqp.core.MessageProperties;
@@ -34,17 +37,25 @@ import java.util.stream.Collectors;
  */
 @Service
 public class TEquipmentServiceImpl extends ServiceImpl<TEquipmentMapper, TEquipment> implements TEquipmentService {
+
     @Autowired
     private AmqpTemplate amqpTemplate;
+
     @Resource
     TEquipmentMapper tEquipmentMapper;
+
     @Autowired
     TAdminService adminService;
+
     @Resource
     SyncOldFeign syncOldFeign;
+
     @Resource
     TEquipmentService equipmentService;
 
+    @Resource
+    TWechatService wechatService;
+
     @Override
     public String sentMessage(String clientId, String json) {
         LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
@@ -241,10 +252,73 @@ public class TEquipmentServiceImpl extends ServiceImpl<TEquipmentMapper, TEquipm
         }
     }
 
-//    @Override
-//    public Integer insertBatchSomeColumn(List<TEquipment> entityList) {
-//        return tEquipmentMapper.insertBatchSomeColumn(entityList);
-//    }
+    @Override
+    public void checkEquipmentStatus() {
+        LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
+        query.eq(TEquipment::getEqeStatus, 1);
+        List<TEquipment> equipmentList = equipmentService.list(query);
+        if (equipmentList.size() > 0) {
+            // 获取当前时间,并检查设备状态是否过期
+            long nowTime = System.currentTimeMillis();
+            for (TEquipment equipment : equipmentList) {
+                // 如果设备的最后刷新时间超过480秒前,则清除设备状态信息,更改为关机状态
+                long updateTime = equipment.getLastUpdateTime().getTime();
+                if (nowTime > updateTime && (nowTime - updateTime) > 480000) {
+                    equipment.setCabinetTm("");
+                    equipment.setCabinetHd("");
+                    equipment.setFurnaceTm("");
+                    equipment.setFurnaceSp("");
+                    equipment.setEqeStatus(0);
+                    equipmentService.updateById(equipment);
+                    // 说明机器没有上传信号,判断为网络不好或者关机,发送信号通知客户 查机器归属国内还是国外
+                    TAdmin admin = adminService.getById(equipment.getAdminId());
+                    String ifForeign = admin.getIfForeign();
+                    // 查询是申泽还是七云的
+                    String companyType = admin.getCompanyType();
+                    LambdaQueryWrapper<TWechat> wechatQuery = Wrappers.lambdaQuery();
+                    wechatQuery.eq(TWechat::getAdminId, equipment.getAdminId());
+                    TWechat wechat = wechatService.getOne(wechatQuery);
+                    if(wechat != null) {
+                        // 有绑定微信的话发公众号
+                        String openId = wechat.getOpenId();
+                        wechatService.sendNoworkMessage(openId,equipment.getClientId(),equipment.getName(), ifForeign, companyType);
+                        if (ifForeign.equals("1")) {
+                            // 国外再发邮件
+                            String messageReceiver = equipment.getMessageReceiver();
+                            if(StringUtils.isNotEmpty(messageReceiver)) {
+                                String[] split = messageReceiver.split(",");
+                                for (String s : split) {
+                                    WechatSendUtil.sentEmail(s,equipment.getName());
+                                }
+                            } else {
+                                if(StringUtils.isNotEmpty(admin.getEmail())){
+                                    WechatSendUtil.sentEmail(admin.getEmail(),equipment.getName());
+                                }
+                            }
+                        }
+                    } else {
+                        // 0或空:国内,1:国外
+                        if (ifForeign.equals("0") || ifForeign.equals("")) {
+                            //TODO 国内发短信
+                        } else {
+                            // 国外发送邮件
+                            String messageReceiver = equipment.getMessageReceiver();
+                            if(StringUtils.isNotEmpty(messageReceiver)) {
+                                String[] split = messageReceiver.split(",");
+                                for (String s : split) {
+                                    WechatSendUtil.sentEmail(s,equipment.getName());
+                                }
+                            } else {
+                                if(StringUtils.isNotEmpty(admin.getEmail())){
+                                    WechatSendUtil.sentEmail(admin.getEmail(),equipment.getName());
+                                }
+                            }
 
+                        }
+                    }
+                }
+            }
+        }
+    }
 
 }

+ 86 - 0
src/main/java/com/szwl/service/impl/TWechatServiceImpl.java

@@ -1,10 +1,25 @@
 package com.szwl.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
+import com.szwl.constant.ConfigConsts;
+import com.szwl.model.bean.WeChatTemplateMsg;
+import com.szwl.model.entity.TAdmin;
 import com.szwl.model.entity.TWechat;
 import com.szwl.mapper.TWechatMapper;
+import com.szwl.service.TAdminService;
 import com.szwl.service.TWechatService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.szwl.util.WechatSendUtil;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * <p>
@@ -17,4 +32,75 @@ import org.springframework.stereotype.Service;
 @Service
 public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> implements TWechatService {
 
+    @Autowired
+    TAdminService adminService;
+
+    @Override
+    public String sendNoworkMessage(String openId, String clientId, String name, String ifForeign, String companyType) {
+        // 模板参数
+        Map<String, WeChatTemplateMsg> sendMag = new HashMap();
+        // 公众号的模板id(也有相应的接口可以查询到)
+        String templateId = "tNMH94OG5bRkTCK1RFi7_zdmj6Y-27iy5Fq1mBH6wTQ";
+        // 微信的基础accessToken
+        String accessToken = WechatSendUtil.getAccessToken(companyType);;
+//        TAdmin admin = adminService.getById(1L);
+//        accessToken = admin.getDepartment();
+//        if(StringUtils.isEmpty(accessToken)){
+//            accessToken = WechatSendUtil.getAccessToken();
+//        }
+        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
+
+//        StringBuffer cli = new StringBuffer();
+//        String keyword1 = cli.append(clientId.substring(clientId.length()-6)).append("-").append(name).toString();
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        String alarmTime = format.format(new Date());
+        if (StringUtils.isEmpty(ifForeign) || ifForeign.equals("0")) {
+            sendMag.put("thing11", new WeChatTemplateMsg(name));
+            sendMag.put("character_string14", new WeChatTemplateMsg(clientId.substring(clientId.length()-6)));
+            sendMag.put("thing4", new WeChatTemplateMsg("严重"));
+            sendMag.put("time5", new WeChatTemplateMsg(alarmTime));
+            sendMag.put("thing6", new WeChatTemplateMsg("机器可能出现断网或突然断电,请及时处理!"));
+            // 国内
+//            sendMag.put("first", new WeChatTemplateMsg("设备异常"));
+//            sendMag.put("keyword1", new WeChatTemplateMsg(keyword1));
+//            sendMag.put("keyword2", new WeChatTemplateMsg("现在"));
+//            sendMag.put("keyword3", new WeChatTemplateMsg("严重"));
+//            sendMag.put("keyword4", new WeChatTemplateMsg("信号"));
+//            sendMag.put("keyword5",
+//                    new WeChatTemplateMsg("机器可能出现断网或突然断电,请及时处理!"));
+//            sendMag.put("remark",
+//                    new WeChatTemplateMsg("Please pay attention to the time and status of the above alarms and take appropriate control measures/请注意以上告警发生的时间和状态,采取适当的控制措施."));
+        } else {
+            sendMag.put("thing11", new WeChatTemplateMsg(name));
+            sendMag.put("character_string14", new WeChatTemplateMsg(clientId.substring(clientId.length()-6)));
+            sendMag.put("thing4", new WeChatTemplateMsg("Serious"));
+            sendMag.put("time5", new WeChatTemplateMsg(alarmTime));
+            sendMag.put("thing6", new WeChatTemplateMsg("Power/Network loss."));
+            // 国外
+//            sendMag.put("first", new WeChatTemplateMsg("Equipment abnormality"));
+//            sendMag.put("keyword1", new WeChatTemplateMsg(keyword1));
+//            sendMag.put("keyword2", new WeChatTemplateMsg("Now"));
+//            sendMag.put("keyword3", new WeChatTemplateMsg("Serious"));
+//            sendMag.put("keyword4", new WeChatTemplateMsg("Network"));
+//            sendMag.put("keyword5",
+//                    new WeChatTemplateMsg("The machine may be disconnected or suddenly power off, please deal with it in time!"));
+        }
+        RestTemplate restTemplate = new RestTemplate();
+        Map<String, Object> sendBody = new HashMap<>();
+        sendBody.put("touser", openId);
+        sendBody.put("topcolor", "#FF0000");
+        sendBody.put("data", sendMag);
+        if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
+            sendBody.put("template_id", ConfigConsts.SZ_TEMPLATE_ID);
+        } else {
+            sendBody.put("template_id", ConfigConsts.SC_TEMPLATE_ID);
+        }
+//        sendBody.put("template_id", templateId);
+        ResponseEntity<String> forEntity = restTemplate.postForEntity(url, sendBody, String.class);
+        JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
+        String messageCode = jsonObject.getString("errcode");
+        String msgId = jsonObject.getString("msgid");
+        System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
+        return forEntity.getBody();
+    }
 }

+ 110 - 0
src/main/java/com/szwl/util/WechatSendUtil.java

@@ -0,0 +1,110 @@
+package com.szwl.util;
+
+import com.alibaba.fastjson.JSONObject;
+import com.szwl.constant.ConfigConsts;
+import com.szwl.model.utils.MailUtil;
+import org.apache.commons.lang.StringUtils;
+
+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;
+
+public class WechatSendUtil {
+
+    private static final String appId="wxcd5b1b2636c9f611";
+    private static final String secret="e2854aa99f8279f33b4f065b2ffb75b1";
+    private static final String orderPlacementNoticeTemplateId="tNMH94OG5bRkTCK1RFi7_zdmj6Y-27iy5Fq1mBH6wTQ";
+
+    /**
+     * 获取小程序token
+     * @return
+     */
+    public static String getAccessToken(String companyType) {
+        String url = "";
+        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());
+            jsonObject.getString("access_token");
+            return jsonObject.getString("access_token");
+
+        } 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;
+    }
+
+    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>" +
+                "<br>" +
+                " Machine name:";
+        String str2="<br>" +" Chinse Time&Date:";
+        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>" +
+                "<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);
+        MailUtil mailUtil = new MailUtil();
+        mailUtil.send(email, subject, contnet.toString());
+        System.out.println("邮件发送成功");
+    }
+}