郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.MajorEquipment;
import com.gk.hotwork.Domain.MajorEquipmentAlarm;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Mapper.MajorEquipmentAlarmMapper;
import com.gk.hotwork.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@Service("MajorEquipmentAlarmService")
@Transactional
public class MajorEquipmentAlarmImpl extends ServiceImpl<MajorEquipmentAlarmMapper,MajorEquipmentAlarm> implements MajorEquipmentAlarmService {
 
    @Autowired
    private MajorEquipmentAlarmMapper majorEquipmentAlarmMapper;
    @Autowired
    MajorEquipmentService majorEquipmentService;
    @Autowired
    private UserService userService;
    @Autowired
    private SmsService smsService;
 
 
 
    /**
     * @Description: 分页
     */
    @Override
    public IPage<MajorEquipmentAlarm> selectPage(Page<MajorEquipmentAlarm> page, Map<String, Object> filter, UserInfo user) {
        return majorEquipmentAlarmMapper.selectPages(page,filter);
    }
 
 
    /**
     * @Description: 新增
     */
    @Override
    public void addOne(MajorEquipmentAlarm param, UserInfo user) {
        requiredVerification(param);
 
        param.setValidFlag(Boolean.TRUE);
        param.setUpdateBy(user.getRealname());
        param.setCreateBy(user.getRealname());
        param.setUpdateTime(new Date());
        param.setCreateTime(new Date());
        this.save(param);
        //短信任务单独跑个线程
        MajorEquipment equipment = majorEquipmentService.getById(param.getEquipmentId());
        Long userId = equipment.getUserId();
 
        Runnable smsTask = () -> {
            if (userId == null) return;
            UserInfo userInfo = userService.getById(userId);
            if (userInfo == null) return;
            if (StringUtils.isBlank(userInfo.getUsername()) || StringUtils.isBlank(userInfo.getRealname())) return;
            Map<String, Object> params = new HashMap<>();
            params.put("name", equipment.getName());
            params.put("info", param.getAlarmInfo());
            params.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(param.getAlarmTime()));
            smsService.send(userInfo.getUsername(), "245614", userInfo.getRealname(), params);
        };
 
        new Thread(smsTask).start();
    }
 
 
    /**
     * @Description: 修改
     */
    @Override
    public void modOne(MajorEquipmentAlarm param, UserInfo user) {
        selectVerification(param.getId());
        requiredVerification(param);
 
        param.setUpdateTime(new Date());
        param.setUpdateBy(user.getRealname());
        this.updateById(param);
    }
 
    /**
     * @Description: 删除
     */
    @Override
    public void delOne(Long id, UserInfo user) {
        selectVerification(id);
 
        MajorEquipmentAlarm delOne = new MajorEquipmentAlarm();
        delOne.setId(id);
        delOne.setUpdateTime(new Date());
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
 
    }
 
    /**
     * 查询验证
     * 验证对象存在
     */
    public void selectVerification(Long id){
        if (id == null) throw new BusinessException("id传参不能为空");
        MajorEquipmentAlarm MajorEquipmentAlarm = this.getById(id);
        if (MajorEquipmentAlarm ==  null) throw new BusinessException("找不到对应实体");
    }
 
    /**
     * 操作验证
     * 验证必填项
     *
     */
    public void requiredVerification(MajorEquipmentAlarm param){
        if (param.getEquipmentId() == null) throw new BusinessException("请选择设备");
        if (param.getStatus() == null) throw new BusinessException("请选择状态");
        if (param.getAlarmTime() == null) throw new BusinessException("请选择报警时间");
        if(StringUtils.isBlank(param.getAlarmInfo())) throw new BusinessException("请填写报警信息");
    }
 
}