Kaynağa Gözat

:ghost: 设备清理提醒

Ritchie 1 yıl önce
ebeveyn
işleme
271b0ed32b

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 1
src/main/java/com/szwl/aspect/MyWebMvcConfigurer.java


+ 18 - 17
src/main/java/com/szwl/controller/AlarmRecordIndexController.java

@@ -35,6 +35,7 @@ public class AlarmRecordIndexController {
     private TAdminService adminService;
 
     private static final String appid = "07784f5fedb508046c841b391005b7de";
+
     /**
      * 添加报警记录
      */
@@ -47,7 +48,7 @@ public class AlarmRecordIndexController {
 
         String clientId = alarmRecordVo.getClientId();
         LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
-        query.eq(TEquipment::getClientId,clientId);
+        query.eq(TEquipment::getClientId, clientId);
         List<TEquipment> list = equipmentService.list(query);
         TEquipment equipment = list.get(0);
         if (equipment == null) {
@@ -78,17 +79,17 @@ public class AlarmRecordIndexController {
         String ifForeign = admin.getIfForeign();
         String companyType = admin.getCompanyType();
         String messageReceiver = equipment.getMessageReceiver();
-        if(ifForeign.equals("1")){
+        if (ifForeign.equals("1")) {
             String subject = "Error message from Magic Candy Machine";
             StringBuffer contnet = new StringBuffer();
-            String str1="Dear customer<br>" +
+            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>" +
+            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>" +
                     "<br>" +
                     " Thank you for choosing our machine!<br>" +
                     "<br>" +
@@ -96,17 +97,17 @@ public class AlarmRecordIndexController {
                     "Best Regards.<br>" +
                     "Magic Candy Service Team";
             contnet.append(str1).append(name).append(str2).append(alarmRecordVo.getOccurrenceTime()).append(str3).append(alarmContent).append(str4);
-            if(StringUtils.isNotEmpty(messageReceiver)) {
+            if (StringUtils.isNotEmpty(messageReceiver)) {
                 String[] split = messageReceiver.split(",");
                 for (String s : split) {
-                    new MailUtil().send(s,subject,contnet.toString());
+                    new MailUtil().send(s, subject, contnet.toString());
                 }
                 return "报警记录添加成功";
             } else {
-                if(email!=null){
-                    new MailUtil().send(email,subject,contnet.toString());
+                if (email != null) {
+                    new MailUtil().send(email, subject, contnet.toString());
                     return "报警记录添加成功";
-                }else {
+                } else {
                     return "email is null";
                 }
             }
@@ -166,16 +167,16 @@ public class AlarmRecordIndexController {
      */
     @GetMapping(value = "/alarmDate", produces = "text/html;charset=utf-8")
     @ResponseBody
-    public String endDate(String clientId){
+    public String endDate(String clientId) {
         LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
-        query.eq(TEquipment::getClientId,clientId);
+        query.eq(TEquipment::getClientId, clientId);
         List<TEquipment> list = equipmentService.list(query);
         TEquipment equipment = list.get(0);
         Date endDate = equipment.getEndDate();
         long endDateTime = endDate.getTime();
         long time = System.currentTimeMillis();
-        if(endDateTime>time){
-            if(endDateTime-time<=3*24*3600*1000L){
+        if (endDateTime > time) {
+            if (endDateTime - time <= 3 * 24 * 3600 * 1000L) {
                 //发短信
                 String result = null;
                 try {
@@ -192,7 +193,7 @@ public class AlarmRecordIndexController {
 //                    logger.info("短信发送结果:" + result);
                 }
             }
-        }else{
+        } else {
 //            return JsonMessage.success("已过期");
             return "已过期";
         }
@@ -209,7 +210,7 @@ public class AlarmRecordIndexController {
      */
     @GetMapping(value = "/eliminate.htm", produces = "text/html;charset=utf-8")
     @ResponseBody
-    public String eliminate(String id){
+    public String eliminate(String id) {
         TAlarmRecord alarmRecord = alarmRecordService.getById(Long.valueOf(id));
         alarmRecord.setIsEliminate(1);
         alarmRecordService.updateById(alarmRecord);

+ 195 - 0
src/main/java/com/szwl/controller/TAlarmCleanController.java

@@ -0,0 +1,195 @@
+package com.szwl.controller;
+
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.szwl.constant.ResponseCodesEnum;
+import com.szwl.model.bo.R;
+import com.szwl.model.bo.ResponseModel;
+import com.szwl.model.entity.TAdmin;
+import com.szwl.model.entity.TAlarmClean;
+import com.szwl.model.entity.TEquipment;
+import com.szwl.model.param.AlarmCleanParam;
+import com.szwl.model.utils.YunPianSms;
+import com.szwl.service.TAdminService;
+import com.szwl.service.TAlarmCleanService;
+import com.szwl.service.TEquipmentService;
+import com.szwl.util.IDGenerator;
+import io.swagger.annotations.ApiModelProperty;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Date;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * <p>
+ * 前端控制器
+ * </p>
+ *
+ * @author wuhs
+ * @since 2024-04-26
+ */
+@RestController
+@RequestMapping("/tAlarmClean")
+public class TAlarmCleanController {
+
+    @Autowired
+    TAlarmCleanService alarmCleanService;
+
+    @Autowired
+    TEquipmentService equipmentService;
+
+    @Autowired
+    TAdminService adminService;
+
+    /**
+     * 超过7d,且做糖大于等于15个,发送提醒,重置时间和数量
+     * 超过7d, 但做糖小于15个,不发提醒,仅重置时间
+     * 不超过7d,做糖量大于等于200,发提醒,重置时间和数量
+     * 国内发短信,海外发邮件
+     */
+    @ApiOperation("清洗提醒")
+    @PostMapping("/alarmClean")
+    public ResponseModel<TAlarmClean> alarmClean(@RequestBody AlarmCleanParam params) {
+        String clientId = params.getClientId();
+        Integer makeCandyNum = params.getMakeCandyNum();
+        String isOversea = params.getIsOversea();
+
+        // 根据 clientId 查找清洗提醒信息
+        LambdaQueryWrapper<TAlarmClean> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.eq(TAlarmClean::getClientId, clientId);
+        TAlarmClean alarmClean = alarmCleanService.getOne(queryWrapper);
+
+        Integer candyNum = null;
+        // 首次调用
+        if (Objects.isNull(alarmClean)) {
+            TAlarmClean tAlarmClean = new TAlarmClean();
+            LambdaQueryWrapper<TEquipment> qw = Wrappers.lambdaQuery();
+            qw.eq(TEquipment::getClientId, clientId);
+            TEquipment equipment = equipmentService.getOne(qw);
+            Long equipmentId = equipment.getId();
+            Long adminId = equipment.getAdminId();
+            LambdaQueryWrapper<TAdmin> wrapper = Wrappers.lambdaQuery();
+            wrapper.eq(TAdmin::getId, adminId);
+            TAdmin admin = adminService.getOne(wrapper);
+            String phone = admin.getPhone();
+            String email = admin.getEmail();
+
+            tAlarmClean.setId(IDGenerator.commonID());
+            tAlarmClean.setClientId(clientId);
+            tAlarmClean.setEquipmentId(equipmentId);
+            tAlarmClean.setAdminId(adminId);
+            tAlarmClean.setPhone(phone);
+            tAlarmClean.setEmail(email);
+            tAlarmClean.setIsOversea(isOversea);
+            tAlarmClean.setLastSendTime(new Date());
+            tAlarmClean.setCandyNum(makeCandyNum);
+
+            alarmCleanService.save(tAlarmClean);
+        } else {
+            candyNum = alarmClean.getCandyNum();
+            candyNum += makeCandyNum;
+            alarmClean.setCandyNum(candyNum);
+
+            alarmCleanService.saveOrUpdate(alarmClean);
+        }
+
+        LambdaQueryWrapper<TAlarmClean> wrapper = Wrappers.lambdaQuery();
+        wrapper.eq(TAlarmClean::getClientId, clientId);
+        TAlarmClean alarmCleanOne = alarmCleanService.getOne(wrapper);
+        Date lastSendTime = alarmCleanOne.getLastSendTime();
+        Integer candyNumNow = alarmCleanOne.getCandyNum();
+        switch (isOversea) {
+            case "0": { // 国内
+                String phone = alarmCleanOne.getPhone();
+                if (StringUtils.isEmpty(phone)) {
+                    return R.fail(ResponseCodesEnum.A0100, "手机号不存在");
+                }
+                if (moreThan7d(lastSendTime)) { // 超过7d
+                    if (candyNumNow >= 15) { // 做糖>=15个
+                        // 发送短信
+                        alarmCleanService.sendNote(phone, clientId);
+                        // 重置时间
+                        alarmCleanOne.setLastSendTime(new Date());
+                        // 清零
+                        alarmCleanOne.setCandyNum(0);
+                    } else {
+                        // 仅重置时间
+                        alarmCleanOne.setLastSendTime(new Date());
+                    }
+                    alarmCleanService.saveOrUpdate(alarmCleanOne);
+                } else { // 不超过7d
+                    if (candyNumNow >= 200) { // 做糖>=200个
+                        // 发送短信
+                        alarmCleanService.sendNote(phone, clientId);
+                        // 重置时间
+                        alarmClean.setLastSendTime(new Date());
+                        // 清零
+                        alarmClean.setCandyNum(0);
+                        alarmCleanService.saveOrUpdate(alarmCleanOne);
+                    }
+                }
+                break;
+            }
+            case "1": { // 海外
+                String email = alarmCleanOne.getEmail();
+                if (StringUtils.isEmpty(email)) {
+                    return R.fail(ResponseCodesEnum.A0001, "邮箱地址不存在");
+                }
+                if (moreThan7d(lastSendTime)) {
+                    if (candyNumNow >= 15) {
+                        // 发送邮件
+                        alarmCleanService.sendEmail(email, clientId);
+                        // 重置时间
+                        alarmCleanOne.setLastSendTime(new Date());
+                        // 清零
+                        alarmCleanOne.setCandyNum(0);
+                    } else {
+                        // 仅重置时间
+                        alarmCleanOne.setLastSendTime(new Date());
+                    }
+                    alarmCleanService.saveOrUpdate(alarmCleanOne);
+                } else { // 不超过7d
+                    if (candyNumNow >= 200) { // 做糖>=200个
+                        // 发送邮件
+                        alarmCleanService.sendEmail(email, clientId);
+                        // 重置时间
+                        alarmCleanOne.setLastSendTime(new Date());
+                        // 清零
+                        alarmCleanOne.setCandyNum(0);
+                        alarmCleanService.saveOrUpdate(alarmCleanOne);
+                    }
+                }
+                break;
+            }
+            default:
+                return R.fail(ResponseCodesEnum.A0001, "地区参数有误" + isOversea);
+        }
+
+        return R.ok();
+
+    }
+
+    /**
+     * 是否超过7天
+     */
+    public Boolean moreThan7d (Date lastSendTime) {
+
+        Date now = new Date();
+
+        // 计算两个日期之间的时间差,单位为毫秒
+        long timeDifference = now.getTime() - lastSendTime.getTime();
+
+        // 将时间差转换为小时
+        long hoursDifference = TimeUnit.HOURS.convert(timeDifference, TimeUnit.MILLISECONDS);
+
+        // 判断时间差是否大于168小时(7天)
+        return hoursDifference >= 168;
+
+    }
+}
+

+ 16 - 0
src/main/java/com/szwl/mapper/TAlarmCleanMapper.java

@@ -0,0 +1,16 @@
+package com.szwl.mapper;
+
+import com.szwl.model.entity.TAlarmClean;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author wuhs
+ * @since 2024-04-26
+ */
+public interface TAlarmCleanMapper extends BaseMapper<TAlarmClean> {
+
+}

+ 23 - 0
src/main/java/com/szwl/mapper/xml/TAlarmCleanMapper.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.szwl.mapper.TAlarmCleanMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.szwl.model.entity.TAlarmClean">
+        <id column="id" property="id" />
+        <result column="admin_id" property="adminId" />
+        <result column="equipment_id" property="equipmentId" />
+        <result column="client_id" property="clientId" />
+        <result column="is_oversea" property="isOversea" />
+        <result column="phone" property="phone" />
+        <result column="email" property="email" />
+        <result column="last_send_time" property="lastSendTime" />
+        <result column="candy_num" property="candyNum" />
+    </resultMap>
+
+    <!-- 通用查询结果列 -->
+    <sql id="Base_Column_List">
+        id, admin_id, equipment_id, client_id, is_oversea, phone, email, last_send_time, candy_num
+    </sql>
+
+</mapper>

+ 55 - 0
src/main/java/com/szwl/model/entity/TAlarmClean.java

@@ -0,0 +1,55 @@
+package com.szwl.model.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import java.util.Date;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.io.Serializable;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author wuhs
+ * @since 2024-04-26
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@ApiModel(value="TAlarmClean对象", description="")
+public class TAlarmClean implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.ASSIGN_ID)
+    private Long id;
+
+    @ApiModelProperty(value = "用户id")
+    private Long adminId;
+
+    @ApiModelProperty(value = "设备id")
+    private Long equipmentId;
+
+    @ApiModelProperty(value = "设备编号")
+    private String clientId;
+
+    @ApiModelProperty(value = "是否海外:0否,1是")
+    private String isOversea;
+
+    @ApiModelProperty(value = "通知电话")
+    private String phone;
+
+    @ApiModelProperty(value = "通知邮箱")
+    private String email;
+
+    @ApiModelProperty(value = "上次通知时间")
+    private Date lastSendTime;
+
+    @ApiModelProperty(value = "本轮做糖数量")
+    private Integer candyNum;
+
+
+}

+ 22 - 0
src/main/java/com/szwl/model/param/AlarmCleanParam.java

@@ -0,0 +1,22 @@
+package com.szwl.model.param;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+@Accessors(chain = true)
+@ApiModel(description = "设备清洗提醒 参数")
+@Data
+public class AlarmCleanParam {
+
+    @ApiModelProperty(value = "是否海外 0不是 1是")
+    private String isOversea;
+
+    @ApiModelProperty(value = "设备编号")
+    private String clientId;
+
+    @ApiModelProperty(value = "做糖数量")
+    private Integer makeCandyNum;
+
+}

+ 30 - 0
src/main/java/com/szwl/service/TAlarmCleanService.java

@@ -0,0 +1,30 @@
+package com.szwl.service;
+
+import com.szwl.model.entity.TAlarmClean;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author wuhs
+ * @since 2024-04-26
+ */
+public interface TAlarmCleanService extends IService<TAlarmClean> {
+
+    /**
+     * 判断是否满足条件
+     */
+//    boolean sendOrNo();
+
+    /**
+     * 发送邮件
+     */
+    void sendEmail(String email, String clientId);
+
+    /**
+     * 发送短信
+     */
+    String sendNote(String phone, String clientId);
+}

+ 69 - 0
src/main/java/com/szwl/service/impl/TAlarmCleanServiceImpl.java

@@ -0,0 +1,69 @@
+package com.szwl.service.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.szwl.model.entity.TAlarmClean;
+import com.szwl.mapper.TAlarmCleanMapper;
+import com.szwl.model.utils.MailUtil;
+import com.szwl.model.utils.SampleMail;
+import com.szwl.model.utils.YunPianSms;
+import com.szwl.service.TAlarmCleanService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+
+/**
+ * <p>
+ * 服务实现类
+ * </p>
+ *
+ * @author wuhs
+ * @since 2024-04-26
+ */
+@Service
+public class TAlarmCleanServiceImpl extends ServiceImpl<TAlarmCleanMapper, TAlarmClean> implements TAlarmCleanService {
+
+    private static final String appid = "07784f5fedb508046c841b391005b7de";
+
+    public String getContent(String clientId) {
+        StringBuffer content = new StringBuffer();
+
+        String str = "Dear customer:<br>" +
+                "<br>" +
+                "Your machine number is <" + clientId + "> marshmallow robot, the stove maintenance cycle has arrived, please clean the stove cover to keep the pattern in the best condition, if it has been cleaned, please ignore this reminder." +
+                "<br>" +
+                "<br>" +
+                "Best Regards.<br>" +
+                "Sunzee Intelligent";;
+        content.append(str);
+        return content.toString();
+    }
+
+    @Override
+    public void sendEmail(String email, String clientId) {
+        String content = "";
+        content = getContent(clientId);
+        String subject = "Cleaning reminders";
+        new MailUtil().send(email, subject, content);
+    }
+
+    @Override
+    public String sendNote(String phone, String clientId) {
+        String msg = null;
+        try {
+            String result = YunPianSms.sendSms(appid, getAlarmCleanMessage(clientId), phone);
+            ObjectMapper om = new ObjectMapper();
+            JsonNode jsonNode = om.readTree(result);
+            msg = jsonNode.get("msg").asText();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        return phone + ": " + msg;
+    }
+
+
+    private String getAlarmCleanMessage(String clientId) {
+        return "【申泽智能】您机器编号为 " + clientId + " 的棉花糖机器人,炉头保养周期已到,请清洁炉头盖,以保持花型在最佳状态,如已清洁请忽略本提醒。";
+    }
+}

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

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