package cn.gistack.sm.sms.feign;
|
|
import cn.com.flaginfo.sdk.cmc.api.sms.send.SMSSendRequest;
|
import cn.gistack.common.utils.SpringContextUtil;
|
import cn.gistack.sm.sjztmd.entity.AttStBase;
|
import cn.gistack.sm.sjztmd.service.*;
|
import cn.gistack.sm.sjztods.constant.ZtApiUrlConstant;
|
import cn.gistack.sm.sjztods.constant.ZtConfigConstant;
|
import cn.gistack.sm.sms.util.SmsUtils;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.parser.Feature;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import lombok.AllArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springblade.core.tenant.annotation.NonDS;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpMethod;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.util.MultiValueMap;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.client.RestTemplate;
|
import springfox.documentation.annotations.ApiIgnore;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.time.temporal.ChronoUnit;
|
|
/**
|
* @PROJECT_NAME: skjcmanager
|
* @DESCRIPTION: 数据异常告警feign实现
|
* @USER: aix
|
* @DATE: 2023/12/5 15:39
|
*/
|
@NonDS
|
@ApiIgnore
|
@RestController
|
@AllArgsConstructor
|
@Slf4j
|
public class DataAbnormalWarnClient implements IDataAbnormalWarnClient{
|
|
@Autowired
|
private IAttResBaseService resBaseService;
|
|
@Autowired
|
private IAttStBaseService attStBaseService;
|
@Autowired
|
private IAttResStagCharService attResStagCharService;
|
|
@Autowired
|
private IAttResRsbNorspiService attResRsbNorspiService;
|
|
@Autowired
|
private IAttResFlseLimService attResFlseLimService;
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
@Override
|
public void sendOperate(String tableName, String phones) {
|
//汇聚任务水库基本信息表 用于判断是否有数据
|
long count = 1l;
|
if (tableName.equals("dim_res_info_a"))
|
count = resBaseService.getDimResInfoACount();
|
else if (tableName.equals("att_res_base"))
|
count = resBaseService.count();
|
else if (tableName.equals("att_st_base")) {
|
// 构造 QueryWrapper 对象
|
QueryWrapper<AttStBase> wrapper = new QueryWrapper<>();
|
wrapper.isNotNull("\"share_st_code\"").ne("\"share_st_code\"", "");
|
count = attStBaseService.count(wrapper);
|
} else if (tableName.equals("att_res_stag_char")) {
|
count = attResStagCharService.count();
|
} else if (tableName.equals("att_res_rsb_norspi")) {
|
count = attResRsbNorspiService.count();
|
} else if (tableName.equals("att_res_flse_lim")) {
|
count = attResFlseLimService.count();
|
}
|
|
log.info("预警数量count:" + count);
|
|
if (count == 0) {
|
sendMsg(tableName, phones);
|
}
|
|
}
|
|
@Override
|
public void sendOperateCZ(String phones, String rate) {
|
|
String url = "/services/1234567890ABCDEFGHIJKLMN/source_data_ssync_staus";
|
|
// 获取当前时间
|
LocalDateTime now = LocalDateTime.now();
|
|
// 减去一个小时
|
LocalDateTime oneHourAgo = now.minusHours(1);
|
|
// 调整为整点时间
|
LocalDateTime oneHourAgoRoundedDown = oneHourAgo.truncatedTo(ChronoUnit.HOURS);
|
|
// 获取整点时间的结束时间(该小时的最后一分钟)
|
LocalDateTime oneHourAgoRoundedDownEnd = oneHourAgoRoundedDown.plusHours(1).minusSeconds(1);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
String startTime = formatter.format(oneHourAgoRoundedDown);
|
String endTime = formatter.format(oneHourAgoRoundedDownEnd);
|
|
String param = "?start_dt=" + startTime + "&end_dt=" + endTime + "&over_rate=" + rate;
|
// String param = "?start_dt=2024-04-30 10:00:00&end_dt=2024-04-30 10:59:59&over_rate=20";
|
|
JSONObject jsonObject = getZtData(param, url);
|
int code = jsonObject.getInteger("code");
|
if (code == 200) {
|
JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
|
String str = "";
|
|
for (int i = 0; i < jsonArray.size(); i++) {
|
JSONObject data = jsonArray.getJSONObject(i);
|
String flag = data.getString("flag");
|
String title = data.getString("title");
|
String tm = data.getString("tm");
|
String rateB = data.getString("rate");
|
|
if ("".equals(str)) {
|
str += tm + flag + title + "数据比昨日少" + rateB;
|
} else {
|
str += ";" + tm + flag + title + "数据比昨日少" + rateB;
|
}
|
}
|
|
if (!str.equals("")) {
|
sendMsgByCZ(phones, str);
|
}
|
// else {
|
// jsonObject = getZtData(param, url);
|
// code = jsonObject.getInteger("code");
|
//
|
// if (code == 200) {
|
// jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
|
// str = "";
|
//
|
// for (int i = 0; i < jsonArray.size(); i++) {
|
// JSONObject data = jsonArray.getJSONObject(i);
|
// String flag = data.getString("flag");
|
// String title = data.getString("title");
|
// String tm = data.getString("tm");
|
// String rate = data.getString("rate");
|
//
|
// str += tm + flag + title + "当前比例为:" + rate + ";";
|
// }
|
//
|
// if (!str.equals("")) {
|
// sendMsgByCZ(phones, str);
|
// }
|
// }
|
// }
|
|
|
}
|
}
|
|
|
/**
|
* 调用中台接口查询数据
|
*
|
* @param params
|
* @param url
|
* @return
|
*/
|
public JSONObject getZtData(String params, String url) {
|
// 获取环境
|
String activeProfile = SpringContextUtil.getActiveProfile();
|
if (activeProfile.equals("dev")){
|
url = ZtApiUrlConstant.url_prefix_dev + url;
|
}
|
if (activeProfile.equals("prod")){
|
url = ZtApiUrlConstant.url_prefix_prod + url;
|
}
|
if (activeProfile.equals("test")){
|
url = ZtApiUrlConstant.url_prefix_test + url;
|
}
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.add(ZtConfigConstant.header_key, ZtConfigConstant.header_value);
|
//封装请求头
|
HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
|
try {
|
//有请求头,有参数请求
|
ResponseEntity<String> responseEntity =
|
restTemplate.exchange(url + params,
|
HttpMethod.GET,
|
formEntity,
|
String.class);
|
// Feature.IgnoreNotMatch 保留null值的属性
|
JSONObject jsonObject = JSON.parseObject(responseEntity.getBody(), Feature.IgnoreNotMatch);
|
// 返回
|
return jsonObject;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
private void sendMsg(String tableName, String phones) {
|
SMSSendRequest request = new SMSSendRequest();
|
String [] phoness = phones.split(",");
|
for (String n: phoness) {
|
request.setUserNumber(n);
|
request.setTemplateId("2431012268292");
|
request.setMessageContent("紧急通知:数据表异常,请立即检查数据源并填充缺失的数据!"+tableName+",数据为空");
|
SmsUtils.sendMsg(request);
|
}
|
}
|
|
private void sendMsgByCZ(String phones, String str) {
|
SMSSendRequest request = new SMSSendRequest();
|
String [] phoness = phones.split(",");
|
for (String n: phoness) {
|
request.setUserNumber(n);
|
request.setTemplateId("2431012268292");
|
request.setMessageContent("紧急通知:数据表异常,请立即检查数据源并填充缺失的数据!"+str);
|
SmsUtils.sendMsg(request);
|
}
|
}
|
|
}
|