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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.dy.pipIrrProject.intake;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.mw.protocol.Command;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pipIrrGlobal.config.DingTalk;
import com.dy.pipIrrGlobal.daoPr.PrDivideMapper;
import com.dy.pipIrrGlobal.daoPr.PrIntakeMapper;
import com.dy.pipIrrGlobal.pojoPr.PrIntake;
import com.dy.pipIrrGlobal.voPr.VoIntake;
import com.dy.pipIrrGlobal.voPr.VoOnLineIntake;
import com.dy.pipIrrProject.intake.qo.OnLineIntakesQO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.util.*;
 
/**
 * @author wuzeyu
 * @date 2023/12/26 11:12
 * @LastEditTime 2023/12/26 11:12
 * @Description 取水口服务类
 */
 
@Slf4j
@Service
public class IntakeSv {
    @Autowired
    private PrIntakeMapper prIntakeMapper;
 
    @Autowired
    private PrDivideMapper prDivideMapper;
 
    @Autowired
    private RestTemplate restTemplate;
 
    protected String comSendUrl;
 
    /**
     * pro_mw:属性
     * tag从控制器中获取
     * key_mw:url的key
     */
    private Environment env = null;
    private String pro_mw = "mw";
    private String key_mw = "comSendUrl";
 
    @Autowired
    public IntakeSv(Environment env) {
        this.env = env;
    }
 
    @Autowired
    private DingTalk dingTalk;
 
    public String sendMessage(String message) {
        try {
            dingTalk.sendMessage(DataSourceContext.get(), this.env, message);
        } catch (Exception e) {
            return "发送失败";
        }
        return "发送成功";
    }
 
    /**
     * 添加取水口
     *
     * @param po
     * @return
     */
    @Transactional
    Integer addIntake(PrIntake po) {
        Long villageId = Optional.ofNullable(po.getVillageId()).orElse(0L);
 
        if (villageId != 0) {
            if (po.getTownId() == null || po.getTownId() == 0) {
                Long townId = getSupperByVillageId(po.getVillageId());
                po.setTownId(townId);
            }
            if (po.getCountyId() == null || po.getCountyId() == 0) {
                Long countyId = getSupperByVillageId(getSupperByVillageId(po.getVillageId()));
                po.setCountyId(countyId);
            }
        }
        if (po.getBlockId() == null || po.getBlockId() == 0) {
            Long blockId = prDivideMapper.getBlockIdById(po.getDivideId());
            po.setBlockId(blockId);
        }
        return prIntakeMapper.insertSelective(po);
    }
 
    public Long getSupperByVillageId(long vaId) {
        return prIntakeMapper.getSupperByVillageId(vaId);
    }
 
    /**
     * 保存修改实体
     *
     * @param po 实体
     * @return 数量
     */
 
    public int update(PrIntake po) {
        if (po.getTownId() == null || po.getTownId() == 0) {
            Long townId = getSupperByVillageId(po.getVillageId());
            po.setTownId(townId);
        }
        if (po.getCountyId() == null || po.getCountyId() == 0) {
            Long countyId = getSupperByVillageId(getSupperByVillageId(po.getVillageId()));
            po.setCountyId(countyId);
        }
        return this.prIntakeMapper.updateByPrimaryKeySelective(po);
//        return this.prIntakeMapper.updateByPrimaryKey(po);
    }
 
    /**
     * 根据指定条件获取取水口记录
     *
     * @param queryVo 查询视图
     * @return 取水口记录列表
     */
    public QueryResultVo<List<VoIntake>> getIntakes(QueryVo queryVo) {
        Command com = new Command();
        com.id = Command.defaultId;
        com.code = "LCD0001";
        com.type = "innerCommand";
        comSendUrl = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw);
        BaseResponse baseResponse = sendCom2Mw(com);
        JSONObject response = (JSONObject) JSON.toJSON(baseResponse);
 
        if (response != null && response.getString("code").equals("0001")) {
            JSONObject attachment = response.getJSONObject("content").getJSONObject("attachment").getJSONObject("onLineMap");
            HashMap<String, Boolean> onLineMap = JSON.parseObject(attachment.toJSONString(), HashMap.class);
 
            JSONArray jsonArray = new JSONArray();
            for (Map.Entry<String, Boolean> entry : onLineMap.entrySet()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("rtuAddr", entry.getKey());
                jsonObject.put("isOnLine", entry.getValue());
                jsonArray.add(jsonObject);
            }
 
            queryVo.setOnLineMap(jsonArray.toJSONString());
 
            Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
            Long itemTotal = prIntakeMapper.getRecordCount(params);
 
            QueryResultVo<List<VoIntake>> rsVo = new QueryResultVo<>();
            rsVo.pageSize = queryVo.pageSize;
            rsVo.pageCurr = queryVo.pageCurr;
            rsVo.calculateAndSet(itemTotal, params);
            rsVo.obj = prIntakeMapper.getIntakes(params);
            return rsVo;
        } else {
            QueryResultVo<List<VoIntake>> rsVo = new QueryResultVo<>();
            return rsVo;
        }
    }
 
    /**
     * 根据指定条件导出取水口列表
     *
     * @param queryVo
     * @return
     */
    public List<VoIntake> exportIntakes(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        List<VoIntake> rsVo = prIntakeMapper.getIntakes(params);
        return rsVo;
    }
 
    /**
     * 得到所有取水口
     *
     * @return 所有取水口集合
     */
    public QueryResultVo<List<PrIntake>> selectAll() {
        QueryResultVo<List<PrIntake>> rsVo = new QueryResultVo<>();
        rsVo.obj = this.prIntakeMapper.selectAll();
        return rsVo;
    }
 
    /**
     * 得到一个取水口
     *
     * @param id 取水口ID
     * @return 取水口实体
     */
    public PrIntake selectById(Long id) {
        return this.prIntakeMapper.selectByPrimaryKey(id);
    }
 
    /**
     * 逻辑删除实体
     *
     * @param id 实体ID
     * @return 数量
     */
    @Transactional
    public int delete(Long id) {
        return this.prIntakeMapper.deleteLogicById(id);
    }
 
    /**
     * 根据取水口编号获取未删除的取水口数量
     *
     * @param intakeId
     * @return
     */
    public Integer getRecordCountOfIntake(Long intakeId) {
        return prIntakeMapper.getRecordCountOfIntake(intakeId);
    }
 
    /**
     * 根据村Id获取全部地址
     *
     * @param villageId 村Id
     * @return address  全部五级地址
     */
    public String getAddressByVillageId(long villageId) {
        return prIntakeMapper.getAddressByVillageId(villageId);
    }
 
    /**
     * 根据区域Id获取区域等级
     *
     * @param regionId 区域Id
     * @return Level  区域等级
     */
    public int getLevelByRegionId(long regionId) {
        return prIntakeMapper.getLevelByRegionId(regionId);
    }
 
    /**
     * 获取取水口列表
     *
     * @return
     */
    public QueryResultVo<List<VoOnLineIntake>> selectOnLineIntakes(OnLineIntakesQO qo) {
        Command com = new Command();
        com.id = Command.defaultId;
        com.code = "LCD0001";
        com.type = "innerCommand";
        comSendUrl = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw);
 
        JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com));
        if (response != null && response.getString("code").equals("0001")) {
            JSONObject attachment = response.getJSONObject("content").getJSONObject("attachment").getJSONObject("onLineMap");
            HashMap<String, Boolean> onLineMap = JSON.parseObject(attachment.toJSONString(), HashMap.class);
 
            JSONArray jsonArray = new JSONArray();
            for (Map.Entry<String, Boolean> entry : onLineMap.entrySet()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("rtuAddr", entry.getKey());
                jsonObject.put("isOnLine", entry.getValue());
                jsonArray.add(jsonObject);
            }
 
            qo.setOnLineMap(jsonArray.toJSONString());
            Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(qo);
            Long itemTotal = prIntakeMapper.getOnLineIntakesCount(params);
 
            QueryResultVo<List<VoOnLineIntake>> rsVo = new QueryResultVo<>();
            rsVo.pageSize = qo.pageSize;
            rsVo.pageCurr = qo.pageCurr;
            rsVo.calculateAndSet(itemTotal, params);
            rsVo.obj = prIntakeMapper.getOnLineIntakes(params);
            return rsVo;
        } else {
            QueryResultVo<List<VoOnLineIntake>> rsVo = new QueryResultVo<>();
            return rsVo;
        }
    }
 
    /**
     * 根据操作员获取常用取水口
     *
     * @param operator
     * @return
     */
    public List<VoOnLineIntake> getUsedIntakes(Long operator) {
        Command com = new Command();
        com.id = Command.defaultId;
        com.code = "LCD0001";
        com.type = "innerCommand";
        JSONObject response = (JSONObject) JSON.toJSON(sendCom2Mw(com));
 
        if (response != null && response.getString("code").equals("0001")) {
            JSONObject attachment = response.getJSONObject("content").getJSONObject("attachment").getJSONObject("onLineMap");
            HashMap<String, Boolean> onLineMap = JSON.parseObject(attachment.toJSONString(), HashMap.class);
 
            JSONArray jsonArray = new JSONArray();
            for (Map.Entry<String, Boolean> entry : onLineMap.entrySet()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("rtuAddr", entry.getKey());
                jsonObject.put("isOnLine", entry.getValue());
                jsonArray.add(jsonObject);
            }
            return prIntakeMapper.getUsedIntakes(jsonArray.toJSONString(), operator);
        } else {
            return new ArrayList<>();
        }
    }
 
    /**
     * 发送命令
     *
     * @return
     */
    protected BaseResponse sendCom2Mw(Command com) {
        String url = UriComponentsBuilder.fromUriString(comSendUrl)
                .build()
                .toUriString();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Command> httpEntity = new HttpEntity<>(com, headers);
        ResponseEntity<BaseResponse> response = null;
        try {
            // 通过Post方式调用接口
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.getBody();
    }
 
 
    /**
     * 取水口名称换取水口ID,验证是否重复名称使用
     *
     * @param intakeName
     * @return
     */
    public List<Long> getIntakeIdsByName(String intakeName) {
        return prIntakeMapper.getIntakeIdsByName(intakeName);
    }
 
    /**
     * 取水口名称换取水口ID,验证是否重复名称使用
     *
     * @param intakeName
     * @return
     */
    public List<Long> getIntakeIdByNameExcludeId(Long id, String intakeName) {
        return prIntakeMapper.getIntakeIdByNameExcludeId(id, intakeName);
    }
 
 
}