fei.wang
2025-04-16 c386374de5ff4a65904124ef839e6862518d27fa
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.hxzkmonitor.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzkmonitor.mapper.TbDuanxinMapper;
import com.hxzkmonitor.pojo.TbDuanxin;
import com.hxzkmonitor.pojo.TbDuanxinlog;
import com.hxzkmonitor.pojo.TbEquipment;
import com.hxzkmonitor.pojo.TbSystem;
import com.hxzkmonitor.service.TbDuanxinlogService;
import com.hxzkmonitor.service.TbSystemService;
import com.hxzkmonitor.util.R;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.web.bind.annotation.RestController;
 
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangfei
 * @since 2024-08-05
 */
@RestController
@RequestMapping("/")
@Component
public class TbSystemController {
 
    @Autowired
    com.hxzkmonitor.service.TbSystemService TbSystemService;
 
    @Autowired
    TbDuanxinMapper tbDuanxinMapper;
    private String SecretId = "AKIDrjZOI4e6KLZu9FAsSRN8eOwSt52lbMBz";
    private String SecretKey = "xnRtHUL3YqdMyOLVSBjKkz1pQoxujS4d";
    @Autowired
    TbDuanxinlogService tbDuanxinlogService;
    Integer st = -1;
 
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/api/getSystemPage")
    public R getSystem(Page page) {
        IPage<TbSystem> ipage = TbSystemService.getSystemPage(page);
 
        return R.ok(ipage);
    }
 
    @PostMapping("/api/addorupSystem")
    public R addorupSystem(TbSystem tbSystem) {
        return R.ok(TbSystemService.addorupSystem(tbSystem));
    }
 
    @GetMapping("/api/getSystemOne")
    public R getSystemOne() {
 
        List<TbSystem> list1 = TbSystemService.findsystem();
        return R.ok(list1);
    }
 
    public static boolean isDifferenceGreaterThan60Seconds(String timeStr) {
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串时间解析为 LocalDateTime 对象
        LocalDateTime givenTime = LocalDateTime.parse(timeStr, formatter);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 计算两个时间之间的差值
        Duration duration = Duration.between(givenTime, currentTime);
        // 判断差值是否大于 60 秒
        return duration.getSeconds() > 60;
    }
 
//    @Scheduled(cron = "0 24 15 * * ?")
@Scheduled(cron = "*/10 * * * * ?")//测试10一次
//    @Scheduled(cron = "0 0/10 8-22 * * ?")//8点到10点每10分钟执行一次
    public void sms() throws Exception {
        System.out.println("短信定时器执行============》》》》》》》》》》》》》》》》");
        //查询数据库中所有的公司数据
        List<TbSystem> list1 = TbSystemService.list();
 
        System.out.println(list1.get(0).getAlerttime());//当前时间-获取时间大于60
        boolean result = isDifferenceGreaterThan60Seconds(list1.get(0).getAlerttime());
        System.out.println("时间差是否大于 60 秒: " + result);
        //获取当前年月日时间
        if (result) {
            if (st == 1){
                try {
                    Credential cred = new Credential(this.SecretId, this.SecretKey);
                    HttpProfile httpProfile = new HttpProfile();
                    httpProfile.setEndpoint("sms.tencentcloudapi.com");
                    ClientProfile clientProfile = new ClientProfile();
                    clientProfile.setHttpProfile(httpProfile);
                    SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
                    SendSmsRequest req = new SendSmsRequest();
//                String[] phoneNumberSet1 = new String[]{GsPhone.split("[,,]")[p]};
                    QueryWrapper sms = new QueryWrapper();
                    sms.eq("type", "引擎离线报警");
                    List<TbDuanxin> managerList = tbDuanxinMapper.selectList(sms);
                    for (int s = 0; s < managerList.size(); s++) {
//                        System.out.println(managerList.get(s).getPhone());
//                        phoneNumberSet1[s] = managerList.get(s).getPhone();
                        String[] phoneNumberSet12 = {managerList.get(s).getPhone()};
                        System.out.println(phoneNumberSet12);
                        req.setPhoneNumberSet(phoneNumberSet12);
 
                        req.setSmsSdkAppId("1400838975");
                        req.setSignName("北京华星北斗智控");
                        req.setTemplateId("2082020");
 
//                    String[] templateParamSet1 = {String.valueOf(+num11 / 24 / 60 / 60 / 1000)};
                        String[] templateParamSet1 = new String[]{managerList.get(s).getDuanxin()};
                        req.setTemplateParamSet(templateParamSet1);
                        // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
                        SendSmsResponse resp = client.SendSms(req);
                        TbDuanxinlog duanxinlog = new TbDuanxinlog();
                        duanxinlog.setName(managerList.get(s).getManager());
                        duanxinlog.setPhone(managerList.get(s).getPhone());
 
                        duanxinlog.setMsg("告警提示:" + managerList.get(s).getDuanxin() + ",请尽快处理,如已处理请忽略该提示信息,如需退订短信可联系管理员删除");
                        tbDuanxinlogService.addorupDuanxinlog(duanxinlog);
                    }
//                req.setPhoneNumberSet(phoneNumberSet1);
//                req.setSmsSdkAppId("1400838975");
//                req.setSignName("北京华星北斗智控");
//                req.setTemplateId("1908206");
//                String[] templateParamSet1 = new String[]{cardNum, "150MB"};
//                req.setTemplateParamSet(templateParamSet1);
//                SendSmsResponse resp = client.SendSms(req);
//                System.out.println(SendSmsResponse.toJsonString(resp));
                } catch (TencentCloudSDKException var15) {
                    System.out.println(var15.toString());
                }
                st = 0;
            }
 
 
        }else{
            st=1;
        }
    }
}