shenyijian
2024-06-24 7d44d7a2a9ee8f55e911d678d78ff25f047cab4d
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
package cn.gistack.sm.sjztmd.service.impl;
 
import cn.gistack.sm.intelligentCall.mapper.CallTaskMapper;
import cn.gistack.sm.sjztmd.constant.RainfallConstant;
import cn.gistack.sm.sjztmd.dto.AttResBaseUserDTO;
import cn.gistack.sm.sjztmd.entity.AttAdBase;
import cn.gistack.sm.sjztmd.entity.AttResBase;
import cn.gistack.sm.sjztmd.entity.RelResLabel;
import cn.gistack.sm.sjztmd.excel.AttResBaseUserExcel;
import cn.gistack.sm.sjztmd.mapper.AttResBaseMapper;
import cn.gistack.sm.sjztmd.service.IAttResBaseService;
import cn.gistack.sm.sjztmd.service.IRelResLabelService;
import cn.gistack.sm.sjztmd.vo.*;
import cn.gistack.system.feign.ISysClient;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @ClassName AttResBaseServiceImpl
 * @Description TODO
 * @Author aix
 * @Date 2023/4/21 20:01
 * @Version 1.0
 */
 
@Slf4j
@Service
@DS("zt")
public class AttResBaseServiceImpl extends ServiceImpl<AttResBaseMapper, AttResBase> implements IAttResBaseService {
    private static final ObjectMapper objectMapper = new ObjectMapper();
 
    @Autowired
    private CallTaskMapper callTaskMapper;
 
    @Autowired
    private IRelResLabelService relResLabelService;
 
    @Autowired
    private ISysClient sysClient;
 
 
    @Override
    public IPage<AttResBase> selectAttResBasePage(IPage<AttResBase> page, AttResBase attResBase) {
        return page.setRecords(baseMapper.selectAttResBasePage(page,attResBase));
    }
 
    @Override
    public Object selectAttResBaseGeneralInvestigation(IPage<AttResBaseGeneralInvestigationVO> page, AttResBaseGeneralInvestigationVO attResBase) {
 
        if (attResBase.getIsPage()){
            return page.setRecords(baseMapper.selectAttResBaseGeneralInvestigation(page,attResBase));
        }else {
            return baseMapper.selectAttResBaseGeneralInvestigation(null,attResBase);
        }
    }
 
    @Override
    public IPage<AttResBaseVO> getListByRegion(IPage<AttResBaseVO> page,Map<String, Object> obj) {
        return page.setRecords(baseMapper.getListByRegion(page,obj));
    }
 
    @Override
    public AttResBase getDetailByCode(String code) {
        return baseMapper.getDetailByCode(code);
    }
 
    /**
     * 获取水库对应责任人分页数据
     * @param params 条件查询参数对象
     * @param page 分页查询参数对象
     * @return
     */
    @Override
    public Object getAttResBaseUserPage(IPage<AttResBaseUserDTO> page,Map<String, Object> params) {
 
        Object isExport = params.get("isExport");
        // 判断是否导出,导出不分页
        if (null != isExport && !isExport.equals("")) {
            return baseMapper.getAttResBaseUserPage(null,params);
        }
        List<AttResBaseUserDTO> attResBaseUserDTOList = baseMapper.getAttResBaseUserPage(page,params);
        // 返回数据
        return page.setRecords(attResBaseUserDTOList);
    }
 
 
    @Override
    public List<AttResBaseUserExcel> exportAttResBaseUser(Map<String, Object> params) {
 
 
        List<AttResBaseUserExcel> attResBaseUserDTOList = baseMapper.getAttResBaseUserExcel(params);
        return attResBaseUserDTOList;
    }
 
    @Override
    public List<AttResWithRainfall> getAttResByRainfall(String minRainfall, String maxRainfall, String dayIndex) {
        if (StrUtil.isNotEmpty(dayIndex) && !Arrays.asList(0,1,2).contains(Integer.parseInt(dayIndex))) {
            throw new ServiceException("无效的dayIndex");
        }
        Double minRainfallDouble = null;
        Double maxRainfallDouble = null;
        if (StrUtil.isNotEmpty(minRainfall)) {
            minRainfallDouble = Double.parseDouble(minRainfall);
        }
        if (StrUtil.isNotEmpty(minRainfall)) {
            maxRainfallDouble = Double.parseDouble(maxRainfall);
        }
        // 调用气象预报服务接口
        String resJsonNodeStr;
        try (HttpResponse httpResponse = HttpRequest.post(RainfallConstant.FORECAST_SERVICE_URL)
            .header("Content-Type", "application/json")
            .header("X-token", RainfallConstant.X_TOKEN)
            .contentType("application/json")
            .body(String.format(RainfallConstant.REQUEST_JSON_BODY, StrUtil.isEmpty(dayIndex) ? "0" : dayIndex)).execute()) {
            JsonNode resJsonNode = objectMapper.readTree(httpResponse.body());
            resJsonNodeStr = resJsonNode.toString();
        } catch (Exception e) {
            throw new ServiceException("调用气象预报服务接口失败,详情:" + e.getMessage());
        }
        RainfallGridData gridData;
        try {
            gridData = objectMapper.readValue(resJsonNodeStr, RainfallGridData.class);
        } catch (JsonProcessingException e) {
            throw new ServiceException("转换降雨栅格数据失败,详情:" + e.getMessage());
        }
        RainfallGridData.Data data = gridData.getData();
        String fileName = data.getFileName();
        RainfallGridData.GridInfo gridInfo = data.getGridInfo();
        List<List<Double>> gridDataList = data.getGridData();
        if (gridDataList == null || gridDataList.isEmpty()) {
            log.warn("gridDataList为空!");
            return new ArrayList<>();
        }
        // 获取网格信息
        int lonNum = gridInfo.getLonNum();    // 网格横向(经度)
        double lonMax = gridInfo.getLonMax();
        double lonSpan = gridInfo.getLonSpan();  // 每个网格单元的经度跨度
        double latSpan = gridInfo.getLatSpan();   // 每个网格单元的纬度跨度
        double latMin = gridInfo.getLatMin();    // 网格的最小纬度
        double lonMin = gridInfo.getLonMin();   // 网格的最小经度
        double latMax = gridInfo.getLatMax();
        int latNum = gridInfo.getLatNum();    // 网格纵向(纬度)
 
        // 将网格数据转换为二维数组
        double[][] grid = new double[latNum][lonNum];
        for (int i = 0; i < latNum; i++) {
            List<Double> row = gridDataList.get(i);
            for (int j = 0; j < lonNum; j++) {
                grid[i][j] = row.get(j);
            }
        }
 
        LambdaQueryWrapper<AttResBase> queryWrapper = Wrappers.<AttResBase>query().lambda();
        List<AttResBase> attResBases = baseMapper.selectList(queryWrapper);
        List<AttResWithRainfall> attResWithRainfallList = new ArrayList<>();
 
        for (AttResBase attResBase : attResBases) {
            if (null == attResBase.getCenterLat() || attResBase.getCenterLong() == null) {
                log.warn("过滤掉经度或者纬度为空的水库,水库名称:{}", attResBase.getName());
                continue;
            }
            // 横向索引(lonIndex):(站点的经度 - 网格最小经度) / 每个网格单元的经度跨度
            int lonIndex = (int) ((Double.parseDouble(attResBase.getCenterLong()) - lonMin) / lonSpan);
            //  纵向索引(latIndex):(站点的纬度 - 网格最小纬度) / 每个网格单元的纬度跨度
            int latIndex = (int) ((Double.parseDouble(attResBase.getCenterLat()) - latMin) / latSpan);
            if (lonIndex >= 0 && lonIndex < lonNum && latIndex >= 0 && latIndex < latNum) {
                double rainfall = grid[latIndex][lonIndex];
                // 降雨量 <= 最小值的站点,都过滤掉, 降雨量为0除外
                if (null != minRainfallDouble &&  rainfall != 0 && rainfall <= minRainfallDouble) {
                    continue;
                }
                // 降雨量 > 最大值的站点,都过滤掉
                if (null != maxRainfallDouble &&  rainfall > maxRainfallDouble) {
                    continue;
                }
                AttResWithRainfall attResWithRainfall = new AttResWithRainfall();
                BeanUtil.copyProperties(attResBase, attResWithRainfall);
                attResWithRainfall.setRainfall(String.valueOf(rainfall));
                attResWithRainfall.setFileName(fileName);
                attResWithRainfallList.add(attResWithRainfall);
            } else {
                log.warn("水库 {} 超出栅格范围. 中心点经度: {}, 中心点纬度: {}", attResBase.getName(),
                        attResBase.getCenterLong(), attResBase.getCenterLat());
            }
        }
        return attResWithRainfallList;
    }
 
    @Override
    public int[] getAttResNumByRainfall(String dayIndex) {
        List<AttResWithRainfall> attResByRainfall;
        // 获取站点降雨量失败或者返回站点列表为空时,直接返回空数组
        try {
            attResByRainfall = getAttResByRainfall(null, null, dayIndex);
        } catch (Exception e) {
            return RainfallConstant.EMPTY_ATT_RES_NUM_ARRAY;
        }
        if (attResByRainfall == null || attResByRainfall.isEmpty()) {
            return RainfallConstant.EMPTY_ATT_RES_NUM_ARRAY;
        }
        // 降雨范围-站点数量
        Map<String, Integer> rainfallRangeAttResNumMap = attResByRainfall.stream()
            .collect(Collectors.groupingBy(attResWithRainfall -> {
                int rainfall = Integer.parseInt(attResWithRainfall.getRainfall());
                if (rainfall > 250) {
                    return "(250,+)";
                } else if (rainfall > 100) {
                    return "(100,250]";
                } else if (rainfall > 50) {
                    return "(50,100]";
                } else if (rainfall > 25) {
                    return "(25,50]";
                } else if (rainfall > 10) {
                    return "(10,25]";
                } else if (rainfall > 5) {
                    return "(5,10]";
                } else if (rainfall > 0.1) {
                    return "(0.1,5]";
                } else if (rainfall >= 0) {
                    return "[0,0.1]";
                } else {
                    throw new ServiceException("无效的降雨值:" + rainfall);
                }
            }, Collectors.collectingAndThen(Collectors.counting(), Long::intValue)));
        int[] attResNumArray = new int[RainfallConstant.RAIN_FALL_RANGES.size()];
        for (int i = 0; i < RainfallConstant.RAIN_FALL_RANGES.size(); i++) {
            String range = RainfallConstant.RAIN_FALL_RANGES.get(i);
            attResNumArray[i] = rainfallRangeAttResNumMap.getOrDefault(range, 0);
        }
        return attResNumArray;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateResBaseByGuid(AttResBase attResBase) {
        attResBase.setUpdateTime(DateUtil.now());
 
        String updateBy = baseMapper.getUpdateLog(attResBase.getGuid());
        if (StringUtil.isBlank(updateBy)){
            updateBy = "";
        }
        BladeUser user = AuthUtil.getUser();
        //修改人基础信息
        String account = user.getAccount();
        String userName = user.getUserName();
        String nowTime = DateUtil.format(DateUtil.now(),DateUtil.PATTERN_DATETIME);
 
        String updateUser = StringUtil.format("[{}-{}-{}]",account,userName,nowTime);
 
        //修改的东西
        String interiorAdGuid = attResBase.getInteriorAdGuid();
        String label = attResBase.getLabel();
        String heightBaseStdTp = attResBase.getHeightBaseStdTp();
        String isGate = attResBase.getIsGate();
 
        String updateLog = "";
        List<String> strList = new ArrayList<>();
        if (StringUtil.isNotBlank(interiorAdGuid)){
            strList.add("行政区划修改为"+ interiorAdGuid);
        }
        strList.add("标签修改为"+label);
        strList.add("高程基准面修改为"+heightBaseStdTp);
        strList.add("是否有闸修改为"+isGate);
 
        String updateContent = String.join("、",strList);
 
        updateLog = updateUser + updateContent + ";";
        updateBy = updateBy + updateLog;
        //向水库标签表中更新数据
        relResLabelService.updateEntity(attResBase.getGuid(),attResBase.getLabel());
 
        //更新机构表中的水库位置
        sysClient.updateDeptByTb(interiorAdGuid,attResBase.getGuid());
 
        //向操作记录表中增加记录
        baseMapper.updateRelResUpdateLoc(attResBase.getGuid(),updateBy);
        return baseMapper.updateResBaseByGuid(attResBase);
    }
 
    /**
     * 查询当前水库对应的市级编号和县区编号
     * @param res_cd
     * @return
     */
    @Override
    public AttResBaseVO getCityCountyCodeByResGuid(String res_cd) {
        return baseMapper.getCityCountyCodeByResGuid(res_cd);
    }
 
    /**
     * 根据水库编码获取行政区数据
     * @param guid
     * @return
     */
    @Override
    public AttResAdVO getWaterRegionInfoByResGuid(String guid) {
        return baseMapper.getWaterRegionInfoByResGuid(guid);
    }
 
    @Override
    public int getDimResInfoACount() {
        return baseMapper.getDimResInfoACount();
    }
 
    @Override
    public Boolean updateIsShowStatus(String ids, Integer status) {
        return baseMapper.updateIsShowStatus(ids,status);
    }
 
    /**
     * 根据水库名称、所在区域查询改水库的数量
     * @param resName 水库名称
     * @param countyCode 区县
     * @return
     */
    @Override
    public Integer getWaterCountByResNameAndCountyName(String resName, String countyCode) {
        return baseMapper.getWaterCountByResNameAndCountyName(resName,countyCode);
    }
 
    @Override
    public AttResAdVO getResAd(String resId) {
        return baseMapper.getResAd(resId);
    }
 
    @Override
    public AttAdBase getYwxtResInfo(String cityName, String countyName) {
        return baseMapper.getYwxtResInfo(cityName,countyName);
    }
 
}