package com.szwl.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.szwl.model.dto.AlarmRecordVo; import com.szwl.model.entity.TAdmin; import com.szwl.model.entity.TAlarmRecord; import com.szwl.model.entity.TEquipment; import com.szwl.model.entity.TWechat; import com.szwl.model.utils.MailUtil; import com.szwl.model.utils.YunPianSms; import com.szwl.service.TAdminService; import com.szwl.service.TAlarmRecordService; import com.szwl.service.TEquipmentService; import com.szwl.service.TWechatService; 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; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.util.Date; import java.util.List; @Controller("appAlarmRecordController") @RequestMapping("/api/app_alarmRecord/alarmRecodeIndex") public class AlarmRecordIndexController { @Autowired private TAlarmRecordService alarmRecordService; @Autowired private TEquipmentService equipmentService; @Autowired private TAdminService adminService; @Autowired private StringRedisTemplate redisTemplate; @Resource TWechatService wechatService; private static final String appid = "07784f5fedb508046c841b391005b7de"; /** * 添加报警记录 */ @RequestMapping(value = "/addAlarmRecord.htm", method = RequestMethod.POST, produces = "text/html;charset=utf-8") @ResponseBody public String addAlarmRecord(@RequestBody AlarmRecordVo alarmRecordVo) { TAlarmRecord alarmRecord = new TAlarmRecord(); String clientId = alarmRecordVo.getClientId(); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TEquipment::getClientId, clientId); TEquipment equipment = equipmentService.getOne(query); if (equipment == null) { return "添加报警记录失败"; } // // 获取今天的日期 // LocalDate today = LocalDate.now(); // // 获取今天的0时0分0秒 // LocalDateTime startOfDay = today.atTime(LocalTime.MIN); // // 获取今天的23时59分59秒999毫秒 // LocalDateTime endOfDay = today.atTime(LocalTime.MAX); // // 转换为Date对象 // Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()); // Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()); // // 查询当天是否已有报警,如果有就消除掉 // LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); // queryWrapper.eq(TAlarmRecord::getClientId, clientId); // queryWrapper.eq(TAlarmRecord::getIsEliminate, 0); // queryWrapper.gt(TAlarmRecord::getCreateDate, startDate); // queryWrapper.le(TAlarmRecord::getCreateDate, endDate); // List list = alarmRecordService.list(queryWrapper); // if (!list.isEmpty()) { // for (TAlarmRecord tAlarmRecord : list) { // tAlarmRecord.setIsEliminate(1); // alarmRecordService.updateById(tAlarmRecord); // } // } Integer type = equipment.getType(); Long adminId = equipment.getAdminId(); Long equipmentId = equipment.getId(); String name = equipment.getName(); if (StringUtils.isEmpty(name)) { name = clientId.substring(clientId.length() - 6); } String adminUserName = equipment.getAdminUserName(); alarmRecord.setEquipmentId(equipmentId); alarmRecord.setName(name); alarmRecord.setAdminLevel(equipment.getAdminLevel()); alarmRecord.setClientId(clientId); alarmRecord.setType(type); alarmRecord.setAdminId(adminId); alarmRecord.setCreateDate(new Date()); alarmRecord.setModifyDate(new Date()); alarmRecord.setAdminUserName(adminUserName); String alarmContent = alarmRecordVo.getAlarmContent(); alarmRecord.setAlarmContent(alarmContent); alarmRecord.setRemark(alarmRecordVo.getRemark()); alarmRecord.setOccurrenceTime(alarmRecordVo.getOccurrenceTime()); alarmRecord.setIsEliminate(0); if (alarmRecordVo.getLevel() != null) { alarmRecord.setLevel(alarmRecordVo.getLevel()); } alarmRecordService.save(alarmRecord); TAdmin admin = adminService.getById(adminId); String email = admin.getEmail(); 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 = TimezoneFmtUtil.getTimeByZoneID(timeZone); // 查询是否绑定微信 LambdaQueryWrapper wechatQuery = Wrappers.lambdaQuery(); wechatQuery.eq(TWechat::getAdminId, admin.getId()); TWechat wechat = wechatService.getOne(wechatQuery); // 微信公众号推送 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 (ifForeign.equals("1")) { String subject = "Error message from Cotton Candy Machine"; StringBuilder content = new StringBuilder(); String str1 = " Machine name: "; String str2 = "
" + " Error message: "; String str3 = "
" + " Time&Date: "; String str4 = "
" + "
" + "Dear customer:
"; 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.
"; String str6 = "
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.
" + "
" + " Thank you for choosing our machine!
" + "
" + "
" + "Best Regards.
" + "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()); } return "报警记录添加成功"; } else { if (email != null) { new MailUtil().send(email, subject, content.toString()); return "报警记录添加成功"; } else { return "email is null"; } } } String result = null; try { if (!"二次曲棍".equals(alarmContent)) { if (StringUtils.isNotEmpty(messageReceiver)) { String[] split = messageReceiver.split(","); for (int i = 0; i < split.length; i++) { if (StringUtils.isNotEmpty(split[i])) { if (StringUtils.isNotEmpty(companyType) && companyType.equals("1")) { // 如果是七云科技的用户 result = YunPianSms.sendSms(appid, getSCMessage(alarmContent, name, clientId.substring(clientId.length() - 6)), split[i]); } else { // 如果是申泽智能的用户 result = YunPianSms.sendSms(appid, getMessage(alarmContent, name, clientId.substring(clientId.length() - 6)), split[i]); } } } } } } catch (Exception e) { } return "报警记录添加成功"; } private String getMessage(String content, String name, String clientId) { String message = "【申泽智能】您好,机器" + content + ",机器名:" + name + ",设备号:" + clientId; return message; } private String getSCMessage(String content, String name, String clientId) { String message = "【七云科技】您好,机器报警:" + content + ",机器名:" + name + ",设备编号:" + clientId + "。"; return message; } /** * 获取系统时间 */ @RequestMapping(value = "/getTime", method = RequestMethod.GET, produces = "text/html;charset=utf-8") @ResponseBody public String getTime() { Date date = new Date(); SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式 return sformat.format(date); } /** * 机器将要到期 * * @param clientId * @return */ @GetMapping(value = "/alarmDate", produces = "text/html;charset=utf-8") @ResponseBody public String endDate(String clientId) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TEquipment::getClientId, clientId); List 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) { //发短信 String result = null; try { String messageReceiver = equipment.getMessageReceiver(); if (StringUtils.isNotEmpty(messageReceiver)) { String[] split = messageReceiver.split(","); for (int i = 0; i < split.length; i++) { if (StringUtils.isNotEmpty(split[i])) { result = YunPianSms.sendSms(appid, getMessage("使用时间即将到期", equipment.getName(), clientId), split[i]); } } } } catch (Exception e) { // logger.info("短信发送结果:" + result); } } } else { // return JsonMessage.success("已过期"); return "已过期"; } // return JsonMessage.success("success"); return "success"; } /** * 更改报警状态 消除 * * @param * @return */ @GetMapping(value = "/eliminate.htm", produces = "text/html;charset=utf-8") @ResponseBody public String eliminate(String id) { TAlarmRecord alarmRecord = alarmRecordService.getById(Long.valueOf(id)); alarmRecord.setIsEliminate(1); alarmRecordService.updateById(alarmRecord); return "success"; } /** * 消除报警记录 * * @param * @return */ @GetMapping(value = "/eliminateAlarm", produces = "text/html;charset=utf-8") @ResponseBody public String eliminateAlarm(String clientId) { // 获取今天的日期 LocalDate today = LocalDate.now(); // 获取今天的0时0分0秒 LocalDateTime startOfDay = today.atTime(LocalTime.MIN); // 获取今天的23时59分59秒999毫秒 LocalDateTime endOfDay = today.atTime(LocalTime.MAX); // 转换为Date对象 Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()); Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAlarmRecord::getClientId, clientId); query.gt(TAlarmRecord::getCreateDate, startDate); query.le(TAlarmRecord::getCreateDate, endDate); List recordList = alarmRecordService.list(query); if(recordList.size() > 0) { for (TAlarmRecord record : recordList) { record.setIsEliminate(1); alarmRecordService.updateById(record); } } return "success"; } }