CZT
2023-10-24 f27f3ea5055888f7f1c797d0fd7fb26a2013c89c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.ld.igds.weather;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.WeatherCity;
import com.ld.igds.models.WeatherConf;
import com.ld.igds.models.WeatherInfo;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
 
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author: andy.jia
 * @description: 气象信息业务处理
 * @version:
 * @data:2019年12月7日
 */
@Slf4j
@Component(CoreWeatherService.BEAN_ID)
public class CoreWeatherServiceImpl extends HibernateDao implements CoreWeatherService {
 
    @Autowired
    private RedisUtil redisUtil;
 
    public void updateCacheWeather(WeatherInfo weather) {
        if (null == weather.getCompanyId()) {
            weather.setCompanyId(ContextUtil.getDefaultCompanyId());
        }
        String key = RedisConst.buildKey(weather.getCompanyId(), RedisConst.KEY_WEATHER_INFO);
 
        log.debug("气象信息更新到缓存中,info={}", weather.toString());
        redisUtil.set(key, weather);
    }
 
    @Override
    public WeatherInfo getCacheWeather(String companyId) {
        String key = RedisConst
                .buildKey(companyId, RedisConst.KEY_WEATHER_INFO);
        WeatherInfo info = (WeatherInfo) redisUtil.get(key);
        if (null == info) {
            log.error("缓存中没有获取到气象信息!!");
        }
        return info;
    }
 
    @Override
    public void onCreate(String address, Integer port) {
        log.debug("气象站自动连接通知暂不处理……");
    }
 
    @Override
    public void onDestroy(String address, Integer port) {
        log.debug("气象站自动断开通知暂不处理……");
    }
 
    @Override
    public void updateCacheAndSave(WeatherInfo weather) {
        if (null == weather.getCompanyId()) {
            weather.setCompanyId(ContextUtil.getDefaultCompanyId());
        }
        try {
            // 更新缓存
            updateCacheWeather(weather);
            // 判断是不是需要保存
            if (WeatherUtil.isSave(weather.getUpdateTime())) {
                addWeatherInfo(weather);
            }
 
        } catch (Exception e) {
            String msg = e.getMessage();
            if (msg.indexOf("PRIMARY") > 0) {
                log.error("持久化气象信息异常,主键冲突,请注意更新时间,出现异常是否合理");
            } else {
                log.error("更新保存气象信息异常:", e);
            }
        }
 
    }
 
    private void addWeatherInfo(WeatherInfo data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.save(data);
        } finally {
            session.flush();
            session.close();
        }
    }
 
    @Override
    public List<WeatherConf> getConfData(String companyId, String deptId) {
        String hql = " from " + WeatherConf.class.getName() + " where 1=1 ";
        Map<String, Object> args = new HashMap<>();
        if (StringUtils.isNotEmpty(companyId)) {
            hql += " and companyId=:companyId ";
            args.put("companyId", companyId);
        }
        if (StringUtils.isNotEmpty(deptId)) {
            hql += " and deptId=:deptId ";
            args.put("deptId", deptId);
        }
        return this.query(hql, args);
    }
 
    @Override
    public String saveConf(WeatherConf data) {
        Session session = this.getSessionFactory().openSession();
        try {
            if (StringUtils.isEmpty(data.getId())) {
                data.setId(ContextUtil.getCurTimeMillis());
                data.setCompanyId(ContextUtil.getCompanyId());
                data.setDeptId(ContextUtil.subDeptId(null));
                session.save(data);
            } else {
                session.update(data);
            }
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    @Override
    public String delConf(WeatherConf data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    @Override
    public void getInfoData(Page<WeatherInfo> page) throws Exception {
        String hql = " from " + WeatherInfo.class.getName()
                + " where companyId=:companyId  ";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        String coutSql = "select count(1) " + hql;
 
        hql += " order by updateTime desc";
 
        this.pagingQuery(page, hql, coutSql, args);
    }
 
    @Override
    public void pageCity(Page<WeatherCity> page, String key) throws Exception {
        String hql = " from " + WeatherCity.class.getName()
                + " where 1=1  ";
        Map<String, Object> args = new HashMap<>();
        if (StringUtils.isNotEmpty(key)) {
            hql += " and (cityEn like:cityEn or cityZh like:cityZh)";
            args.put("cityEn", "%" + key + "%");
            args.put("cityZh", "%" + key + "%");
        }
        String countHql = "select count(1) " + hql;
 
        if (args.isEmpty()) {
            pagingQuery(page, hql, countHql);
        } else {
            pagingQuery(page, hql, countHql, args);
        }
    }
 
}