tangzy
2022-07-21 a98b1db4392c357304036e8b3cdd01646920300d
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.floodinundation.service.impl;
 
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springblade.modules.floodinundation.entity.floodinundation;
import org.springblade.modules.floodinundation.mapper.FloodinundationMapper;
import org.springblade.modules.floodinundation.service.IfloodinundationService;
import org.springblade.modules.floodinundation.vo.FloodinundationVO;
import org.springblade.modules.water.entity.water;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2020-02-27
 */
@Service
@DS("slaves")
public class FloodinundationServiceImpl extends ServiceImpl<FloodinundationMapper, floodinundation> implements IfloodinundationService {
 
    @Override
    public IPage<FloodinundationVO> selectfloodinundationPage(IPage<FloodinundationVO> page, FloodinundationVO floodinundationVO) {
        return baseMapper.selectfloodinundationPage(page, floodinundationVO);
    }
 
    @Override
    public void upLoadFile(MultipartFile file) {
        try{
            InputStream in = file.getInputStream();
            Workbook workbook = null;
            try{
                workbook = new HSSFWorkbook(in);
            }catch(Exception e){
                workbook = new XSSFWorkbook(in);
            }
 
            int numberOfSheets = workbook.getNumberOfSheets();
            //第一个sheet
            for (int j=0;j<numberOfSheets;j++) {
                Sheet sheet1 = workbook.getSheetAt(j);
 
                //有效行数
                int rowNubms = sheet1.getPhysicalNumberOfRows();
                for (int i = 1; i < rowNubms; i++) {
                    Row rowi = sheet1.getRow(i);
 
                    if (rowi == null) {
                        continue;
                    }
                    //创建数据库表对应的实体类
                    floodinundation floodinundation = new floodinundation();
                    //Cell cell1 = rowi.getCell(0);
                    Cell cell0 = rowi.getCell(0);//危险期编码
                    Cell cell1 = rowi.getCell(1);//行政区
                    Cell cell2 = rowi.getCell(2);//乡镇
                    Cell cell3 = rowi.getCell(3);//行政村
                    Cell cell4 = rowi.getCell(4);//村小组
                    Cell cell5 = rowi.getCell(5);//东经
                    Cell cell6 = rowi.getCell(6);//北纬
                    Cell cell7 = rowi.getCell(7);//成灾水位(85黄海)
                    Cell cell8 = rowi.getCell(8);//河段编码
                    Cell cell9 = rowi.getCell(9);//起点距
                    Cell cell10 = rowi.getCell(10);//总人口
                    Cell cell11 = rowi.getCell(11);//灾频率
                    Cell cell12 = rowi.getCell(12);//5年一遇人口
                    Cell cell13 = rowi.getCell(13);//5年一遇户数
                    Cell cell14 = rowi.getCell(14);//5年一遇耕地
                    Cell cell15 = rowi.getCell(15);//20年一遇耕地
                    Cell cell16 = rowi.getCell(16);//20年一遇耕地
                    Cell cell17 = rowi.getCell(17);//20年一遇耕地
                    Cell cell18 = rowi.getCell(18);//50年一遇耕地
                    Cell cell19 = rowi.getCell(19);//50年一遇耕地
                    Cell cell20 = rowi.getCell(20);//50年一遇耕地
 
 
                    //此处判断为null,以防get值的时候 报空指针
                    DecimalFormat df = new DecimalFormat("0");
                    NumberFormat nf = NumberFormat.getInstance();
                    nf.setGroupingUsed(false);
                    String wcode = (cell0 != null) ? getCellValue(cell0) : null;//危险期编码
                    int number = FloodinundationServiceImpl.super.baseMapper.selectInfo(wcode);
                    if (number==1){
                        FloodinundationServiceImpl.super.baseMapper.deleteInfo(wcode);
                    }
                    String district = (cell1 != null) ? getCellValue(cell1) : null;//行政区
                    String township = (cell2 != null) ? getCellValue(cell2) : null;//乡镇
                    String village = (cell3 != null) ? getCellValue(cell3) : null;//行政村
                    String village_group = (cell4 != null) ? getCellValue(cell4) : null;//村小组
                    String dj = (cell5 != null) ? getCellValue(cell5) : null;//东经
                    String bw = (cell6 != null) ? getCellValue(cell6) : null;//北纬
                    String z = (cell7 != null) ? getCellValue(cell7) : null;//成灾水位(85黄海)
                    String hcode = (cell8 != null) ? getCellValue(cell8) : null;//河段编码
                    String origin = (cell9 != null) ? getCellValue(cell9) : null;//河段编码
                    //String totalp = (cell10 != null) ? getCellValue(cell10) : null;//总人口
                    String totalp = (cell10 != null) ? nf.format(cell10.getNumericCellValue()):null;
                    //String disaster = (cell11 != null) ? getCellValue(cell11) : null;//受灾频率
                    String disaster = (cell11 != null) ? getCellValue2(cell11) : null;
                    //String fpopulation = (cell12 != null) ? getCellValue(cell12) : null;//5年一遇人口
                    String fpopulation =(cell12 != null) ? nf.format(cell12.getNumericCellValue()):null;
                    //String fhouseholds = (cell13 != null) ? getCellValue(cell13) : null;//5年一遇户数
                    String fhouseholds = (cell13 != null) ? nf.format(cell13.getNumericCellValue()):null;
                    //String ftill = (cell14 != null) ? getCellValue(cell14) : null;//5年一遇耕地
                    String ftill = (cell14 != null) ? nf.format(cell14.getNumericCellValue()):null;
                    //String tpopulation = (cell15 != null) ? getCellValue(cell15) : null;//20年一遇耕地
                    String tpopulation = (cell15 != null) ? nf.format(cell15.getNumericCellValue()):null;
                    //String thouseholds = (cell16 != null) ? getCellValue(cell16) : null;//20年一遇耕地
                    String thouseholds = (cell16 != null) ? nf.format(cell16.getNumericCellValue()):null;
                    //String ttill = (cell17 != null) ? getCellValue(cell17) : null;//20年一遇耕地
                    String ttill = (cell17 != null) ?nf.format(cell17.getNumericCellValue()):null;
                    //String ftpopulation = (cell18 != null) ? getCellValue(cell18) : null;//50年一遇耕地
                    String ftpopulation = (cell18 != null) ?nf.format(cell18.getNumericCellValue()):null;
                    //String fthouseholds = (cell19 != null) ? getCellValue(cell19) : null;//50年一遇耕地
                    String fthouseholds = (cell19 != null) ?nf.format(cell19.getNumericCellValue()):null;
                    //String fttill = (cell20 != null) ? getCellValue(cell20) : null;//50年一遇耕地
                    String fttill = (cell20 != null) ?nf.format(cell20.getNumericCellValue()):null;
 
 
                    //填充实体类
                    floodinundation.setWcode(wcode);
                    floodinundation.setDistrict(district);
                    floodinundation.setTownship(township);
                    floodinundation.setVillage(village);
                    floodinundation.setVillagegroup(village_group);
                    floodinundation.setDj(dj);
                    floodinundation.setBw(bw);
                    floodinundation.setZ(z);
                    floodinundation.setHcode(hcode);
                    floodinundation.setOrigin(origin);
                    floodinundation.setTotalp(totalp);
                    floodinundation.setDisaster(disaster);
                    floodinundation.setFpopulation(fpopulation);
                    floodinundation.setFhouseholds(fhouseholds);
                    floodinundation.setFtill(ftill);
                    floodinundation.setTpopulation(tpopulation);
                    floodinundation.setThouseholds(thouseholds);
                    floodinundation.setTtill(ttill);
                    floodinundation.setFtpopulation(ftpopulation);
                    floodinundation.setFthouseholds(fthouseholds);
                    floodinundation.setFttill(fttill);
 
                    //使用JPA 保存到数据库中
                    FloodinundationServiceImpl.super.save(floodinundation);
 
                    in.close();  //关闭流
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public FloodinundationVO selectfId(String id) {
        return baseMapper.selectfId(id);
    }
 
    @Override
    public List<floodinundation> selectNoticeList(String query) {
        return baseMapper.selectNoticeList(query);
    }
 
    @Override
    public List<Map<Object, String>> selectC(int code, String times, String dateEnd) {
        return baseMapper.selectC(code, times, dateEnd);
    }
 
    @Override
    public List<Map<Object, String>> selectCS(String times, String dateEnd) {
        return baseMapper.selectCS(times, dateEnd);
    }
 
    private String getCellValue(Cell cell){
        String value ="";
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                break;
            case  Cell.CELL_TYPE_NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
                break;
            case  Cell.CELL_TYPE_BLANK:
                value = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                value = String.valueOf(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                value = String.valueOf(cell.getCellFormula());
                break;
        }
        return value;
    }
    private String getCellValue2(Cell cell){
        String value ="";
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                break;
            case  Cell.CELL_TYPE_NUMERIC:
                NumberFormat nf = NumberFormat.getInstance();
                value = nf.format(cell.getNumericCellValue());
                break;
            case  Cell.CELL_TYPE_BLANK:
                value = "";
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                value = String.valueOf(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                value = String.valueOf(cell.getCellFormula());
                break;
        }
        return value;
    }
    @Override
    public Double selectSS(List<water> waters, Double z, Double ds, Double bzgc) {
        /*//根据名称查询
        List<water> watersInfo = waterService.queryList(null, log);*/
        //根据id查询
        //List<water> waters = waterService.queryList(pid, "");
        int size = waters.size();
        Double[] arr1 = new Double[size];
        int length = arr1.length;
        int length1 = arr1.length - 1;
        //根据名称查询查出预警水面线值,通过实时水位值推算出最新水位差
        Double water = bzgc;
        Double number = z;//实时水面线值
        Double numbers = ds;//淹没区起点距
        double vs = number - water;
        Double v = Double.parseDouble(String.format("%.2f", vs));
        //定义一个返回的值
        Double count = 0.0;
        Double water1;
        Double origin;
        Double sums;
        Double sum;
        //循环遍历水面线值,然后通过最新水位差进行更新
        for (int i = 0; i < size; i++) {
            water1 = Double.parseDouble(waters.get(i).getWater());
            origin = Double.parseDouble(waters.get(i).getOrigin());
            //预警水面值+差值
            sums = water1 + v;
            sum = Double.parseDouble(String.format("%.2f", sums));
            waters.get(i).setWater(sum + "");
            //把起点距存放到数组里
            arr1[i] = origin;
        }
        //循环判断淹没区起点距位置
        Double water0;
        Double water2;
        Double origin1;
        Double origin2;
        Double origins1;
        Double waters1;
        Double origins2;
        Double waters2;
        for (int j = 0; j < length; j++) {
            if (numbers <= arr1[j]) {
                water0 = Double.parseDouble(waters.get(j).getWater());
                count = water0;
                break;
            }
            //判断是不是超过数组最大值
            else if (numbers < arr1[length1]) {
                if (numbers > arr1[j+1] && numbers < arr1[j + 2]) {
                    if(numbers==arr1[j+1]){
                        water2 = Double.parseDouble(waters.get(j + 1).getWater());
                        count = water2;
                        break;
                    }
                    else if (numbers==arr1[j+2]){
                        water2 = Double.parseDouble(waters.get(j + 2).getWater());
                        count = water2;
                        break;
                    }
                    else {
                        //获取范围前的水面线值和起点距
                        origin1 = Double.parseDouble(waters.get(j + 1).getOrigin());
                        water1 = Double.parseDouble(waters.get(j + 1).getWater());
                        //获取范围后的水面线值和起点距
                        origin2 = Double.parseDouble(waters.get(j + 2).getOrigin());
                        water2 = Double.parseDouble(waters.get(j + 2).getWater());
                        count = algorithm(origin1, water1, origin2, water2, ds);
                        break;
                    }
                }
                /*//出现等于起点距的时候
                else {
                    Double water2 = Double.parseDouble(waters.get(j + 2).getWater());
                    count = water2;
                    break;
                }*/
            }
            //出现超过最大值的时候
            else {
                origins1 = Double.parseDouble(waters.get(length1 - 1).getOrigin());
                waters1 = Double.parseDouble(waters.get(length1 - 1).getWater());
                //获取范围后的水面线值和起点距
                origins2 = Double.parseDouble(waters.get(length1).getOrigin());
                waters2 = Double.parseDouble(waters.get(length1).getWater());
                count= algorithm(origins1, waters1, origins2, waters2, ds);
                break;
            }
        }
        return count;
    }
 
    /**
     * @param origin1 前一个起点距
     * @param water1  前一个水面线
     * @param origin2 后一个起点距
     * @param water2  后一个水面线
     * @param ds      淹没区起点距
     * @return
     */
    @Override
    public Double algorithm(Double origin1, Double water1, Double origin2, Double water2, Double ds) {
        //算X的值
        double v = water2 - water1;//水面线差
        double v1 = origin2 - origin1;//起点差
        double count = 0.0;
        if (v < 0) {
            //如果水面线差是负数就先转正数
            double abs = Math.abs(v);
            //去小数点2位数
            Double parseDouble = Double.parseDouble(String.format("%.2f", abs));
            //通过2点水面线和以及起点距,求出平均每米的差值
            double v2 = parseDouble / v1;
            //把V2的值不以科学计数法输出,保留6位有效数字
            Double s = Double.parseDouble(big2(v2));
            //通过淹没区水面线起点距,算水面线值
            double v3s = ds - origin1;
            Double v3 = Double.parseDouble(String.format("%.2f", v3s));
            //通过起点距差*每米的差
            double v4s = v3 * s;
            Double v4 = Double.parseDouble(String.format("%.2f", v4s));
            //距离第一个起点距的水面线-计算出每米的差
            count = water1 - v4;
 
        } else {
            //去小数点2位数
            Double parseDouble = Double.parseDouble(String.format("%.2f", v));
            //算2点水面线,起点距每米的差
            double v2 = parseDouble / v1;
            //把V2的值不以科学计数法输出,保留6位有效数字
            Double s = Double.parseDouble(big2(v2));
            //通过淹没区水面线起点距,算水面线值
            double v3s = ds - origin1;
            Double v3 = Double.parseDouble(String.format("%.2f", v3s));
            //通过起点距差*每米的差
            double v4s = v3 * s;
            Double v4 = Double.parseDouble(String.format("%.2f", v4s));
            //距离第一个起点距的水面线-计算出每米的差
            count = water1 + v4;
 
        }
        return count;
    }
    //把科学计数法转化成String
    private static String big2(double d) {
        BigDecimal d1 = new BigDecimal(Double.toString(d));
        BigDecimal d2 = new BigDecimal(Integer.toString(1));
        // 四舍五入,保留2位小数
        return d1.divide(d2, 5, BigDecimal.ROUND_HALF_UP).toString();
    }
 
}