李宇
2021-01-21 4ac43fa87c355ff4a001110f5de7fe32acfd5289
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
package com.nanometer.smartlab.service;
 
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import com.nanometer.smartlab.dao.SysWarningDao;
import com.nanometer.smartlab.entity.SysWarning;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.util.MessageUtil;
 
 
@Service("sysWarningService")
public class SysWarningServiceImpl implements SysWarningService {
    private static Logger logger = Logger.getLogger(SysWarningServiceImpl.class);
 
    @Resource(name = "sysWarningDao")
    SysWarningDao sysWarningDao;
    @Value("${alarm.url}")
    String alarmUrl;
 
 
    @Override
    public void insert(SysWarning sysWarning) {
        sysWarningDao.insertWatning(sysWarning);
    }
 
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysWarning> getSysWarningList(String code, Timestamp startTime,Timestamp endTime, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(code)) {
                params.put("containerCode", "%" + code + "%");
            }
            params.put("startWarningTime", startTime);
            params.put("endWarningTime", endTime);
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.sysWarningDao.getSysWarningList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public SysWarning getSysWarning(String id) {
        try {
            return this.sysWarningDao.getSysWarning(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public void updateSysWarning(SysWarning selectedWarning) throws IOException {
        try {
            Map<String,Object> params = new HashMap<>();
            params.put("memo", selectedWarning.getMemo());
            params.put("status", selectedWarning.getStatus());
            params.put("id", selectedWarning.getId());
            //给大屏信息发送处理信息
            //页面只允许修改完成转台
            if (StringUtils.isNotBlank(selectedWarning.getAlarmId()) && StringUtils.isNotBlank(alarmUrl)){
                JSONObject json=new JSONObject();
                json.put("dataType", "data");
                json.put("id", selectedWarning.getAlarmId());
                CloseableHttpClient client2 = HttpClients.createDefault();
                HttpPost post2 = new HttpPost(alarmUrl);
                post2.setHeader("Content-Type", "application/json;charset=UTF-8");
                StringEntity se = new StringEntity(json.toString(), "UTF-8");
                se.setContentEncoding("UTF-8");
                se.setContentType("application/json");
                post2.setEntity(se);
                CloseableHttpResponse response2 = client2.execute(post2);
                String resData2 = EntityUtils.toString(response2.getEntity());
                client2.close();
                logger.info(resData2);
            }else{
                logger.info("关联daping的告警id为空 ");
            }
            this.sysWarningDao.updateSysWarning(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
 
    @Override
    public int getSysWarningTotalCount(String code, Timestamp startTime, Timestamp endTime) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(code)) {
                params.put("containerCode", "%" + code + "%");
            }
            if(startTime != null)
            {
                    params.put("startWarningTime", startTime);
            }
            if(endTime != null)
            {
                    params.put("endWarningTime", endTime);
            }
 
            return this.sysWarningDao.getSysWarningTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
}