ke
2024-07-19 08a962d2925baa255207fdf979e6fee794f1773b
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
package cn.gistack.sm.sjztdw.service.impl;
 
import cn.gistack.sm.sjztdw.entity.*;
import cn.gistack.sm.sjztdw.excel.DbExcel;
import cn.gistack.sm.sjztdw.mapper.DwsResFormDiMapper;
import cn.gistack.sm.sjztdw.service.*;
import cn.gistack.sm.sjztdw.vo.DbExcelVO;
import cn.gistack.sm.sjztmd.service.IAttResBaseService;
import cn.gistack.sm.sjztmd.vo.AttResAdVO;
import cn.gistack.sm.sjztods.entity.DsmSpgSpprmp;
import cn.gistack.sm.sjztods.entity.DsmSpgSpqnmp;
import cn.gistack.sm.sjztods.service.IDsmSpgSpprmpService;
import cn.gistack.sm.sjztods.service.IDsmSpgSpqnmpService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
@DS("ztdw")
public class DwsResFormDiServiceImpl extends ServiceImpl<DwsResFormDiMapper, DwsResFormDi> implements IDwsResFormDiService {
    private IAttResBaseService attResBaseService;
 
    //日数据监测
    private IDwsRainResDiService dwsRainResDiService;
    private IDwsWaterResZEightDetailAllService dwsWaterResZEightDetailAllService;
    private IDwsResPressDiService dwsResPressDiService;
    private IDwsResSlDiService dwsResSlDiService;
 
    //测点表
    private IDsmSpgSpqnmpService dsmSpgSpqnmpService;
    private IDsmSpgSpprmpService dsmSpgSpprmpService;
 
    @Override
    @Transactional
    public void importDbPersonData(List<DbExcel> data, String resGuid,String ch) {
 
        //获取行政区划
        AttResAdVO resAd = attResBaseService.getResAd(resGuid);
 
        DsmSpgSpprmp paramSy = new DsmSpgSpprmp();
        paramSy.setCh(ch);
        paramSy.setRscd(resAd.getResRegCode());
 
        //根据水库注册登记码,断面获取渗压站列表,并按测点编号从小到大排序
        List<DsmSpgSpprmp> dsmSpgSpprmpList = dsmSpgSpprmpService.getList(paramSy);
 
        //从渗流测点表中根据断面和水库注册登记码获取测点编码
        DsmSpgSpqnmp paramSl = new DsmSpgSpqnmp();
        paramSl.setCh(ch);
        paramSl.setRscd(resAd.getResRegCode());
        List<DsmSpgSpqnmp> dsmSpgSpqnmpList = dsmSpgSpqnmpService.getList(paramSl);
 
        data.forEach(dbExcel->{
            dbExcel.setAvgValue(dbExcel.getAvgValue().replace(",",","));
            if (StringUtil.isBlank(dbExcel.getRz())){
                dbExcel.setRz("");
            }
            if (StringUtil.isBlank(dbExcel.getDrp())){
                dbExcel.setDrp("");
            }
            if (StringUtil.isBlank(dbExcel.getLowZ())){
                dbExcel.setLowZ("");
            }
            if (StringUtil.isBlank(dbExcel.getAvgValue())){
                dbExcel.setAvgValue("");
            }
            if (StringUtil.isBlank(dbExcel.getAvgSpqnValue())){
                dbExcel.setAvgSpqnValue("");
            }
 
            //存总表数据
            Integer i = baseMapper.updateCustomize(dbExcel,resAd,ch);
            if (i<=0){
                baseMapper.save(dbExcel,resAd,ch);
            }
 
            if (StringUtil.isNotBlank(dbExcel.getDrp())){
                //存雨情数据
                saveDayRainFall(dbExcel,resAd);
            }
 
            if (StringUtil.isNotBlank(dbExcel.getRz()) || StringUtil.isNotBlank(dbExcel.getLowZ())){
                //存水位数据
                saveWaterLevel(dbExcel,resAd);
            }
 
            if (StringUtil.isNotBlank(dbExcel.getAvgValue())){
                //存渗压数据
                saveSy(dbExcel,resAd,dsmSpgSpprmpList);
            }
 
            if (StringUtil.isNotBlank(dbExcel.getAvgSpqnValue())){
                //存渗流数据
                saveSl(dbExcel,resAd,dsmSpgSpqnmpList);
            }
 
 
        });
    }
 
    @Override
    public Boolean update(DbExcelVO dbExcel) {
 
 
        baseMapper.edit(dbExcel);
 
        //根据日期更新数据
        if (StringUtil.isNotBlank(dbExcel.getMonitorDate())){
 
 
 
            Integer a = dwsRainResDiService.updateByBasic(dbExcel);
            Integer b = dwsWaterResZEightDetailAllService.updateByBasic(dbExcel);
 
            Integer c = dwsResPressDiService.updateByBasic(dbExcel);
            Integer d = dwsResSlDiService.updateByBasic(dbExcel);
 
            boolean b1 = a > 0;
            boolean b2 = b > 0;
            boolean b3 = c > 0;
            boolean b4 = d > 0;
            return true;
        }
 
        return false;
    }
 
    @Override
    public Boolean remove(DbExcelVO dbExcelVO) {
 
        baseMapper.remove(dbExcelVO);
 
        //根据日期删除数据
        if (StringUtil.isNotBlank(dbExcelVO.getMonitorDate())){
            Integer a = dwsRainResDiService.removeByBasic(dbExcelVO);
            Integer b = dwsWaterResZEightDetailAllService.removeByBasic(dbExcelVO);
            Integer c = dwsResPressDiService.removeByBasic(dbExcelVO);
            Integer d = dwsResSlDiService.removeByBasic(dbExcelVO);
 
            boolean b1 = a > 0;
            boolean b2 = b > 0;
            boolean b3 = c > 0;
            boolean b4 = d > 0;
 
            return b1&&b2&&b3&&b4;
        }
 
        return null;
    }
 
    @Override
    public Boolean removeBatch(List<DbExcelVO> list) {
 
        list.forEach(e->{
            remove(e);
        });
 
        return true;
    }
 
    private void saveSy(DbExcel dbExcel, AttResAdVO resAd,List<DsmSpgSpprmp> list) {
        try {
            //excel数据应是按测压管从小到大
            List<String> avgValueList = Arrays.asList(dbExcel.getAvgValue().split(","));
 
            //有渗压站才添加
            if (list !=null && list.size()>0){
                for (int i = 0; i < avgValueList.size(); i++) {
                    String avgValue = avgValueList.get(i);
                    DsmSpgSpprmp dsmSpgSpprmp = list.get(i);
 
                    DwsResPressDi dwsResPressDi = new DwsResPressDi();
                    //站点编码
                    dwsResPressDi.setCd(dsmSpgSpprmp.getMpcd());
                    //站点名称
                    dwsResPressDi.setCdNm(dsmSpgSpprmp.getMpcd());
 
                    dwsResPressDi.setResCd(resAd.getResGuid());
                    dwsResPressDi.setResNm(resAd.getResName());
                    dwsResPressDi.setAvgValue(avgValue);
 
                    Date date = dateFormatString(dbExcel.getMonitorDate(), "yyyy-MM-dd");
                    dwsResPressDi.setDt(date);
                    dwsResPressDi.setIsTb("y");
 
                    Integer result = dwsResPressDiService.updateCustomize(dwsResPressDi);
                    if (result<=0){
                        dwsResPressDiService.save(dwsResPressDi);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("表格渗压数据个数超过当前水库断面测点个数");
        }
 
 
    }
 
    private void saveSl(DbExcel dbExcel, AttResAdVO resAd,List<DsmSpgSpqnmp> list) {
 
        //有渗流站点才添加
        if (list != null && list.size()>0){
            DwsResSlDi dwsResSlDi = new DwsResSlDi();
 
            if (list != null && list.size()>0){
                dwsResSlDi.setCd(list.get(0).getMpcd());
            }
 
            dwsResSlDi.setResRegCode(resAd.getResRegCode());
            dwsResSlDi.setResCd(resAd.getResGuid());
 
            dwsResSlDi.setAvgSpqnValue(dbExcel.getAvgSpqnValue());
 
            Date date = dateFormatString(dbExcel.getMonitorDate(), "yyyy-MM-dd");
 
            dwsResSlDi.setDt(date);
            dwsResSlDi.setIsTb("y");
 
 
            Integer result = dwsResSlDiService.updateCustomize(dwsResSlDi);
            if (result<=0){
                dwsResSlDiService.save(dwsResSlDi);
            }
 
        }
 
    }
 
 
    /**
     * 保存雨情数据
     * @param dbExcel
     * @param resAd
     */
    private void saveDayRainFall(DbExcel dbExcel, AttResAdVO resAd) {
 
        DwsRainResDi dwsRainResDi = new DwsRainResDi();
 
        dwsRainResDi.setStCd("");
        dwsRainResDi.setStNm("人工上报");
        dwsRainResDi.setResCd(resAd.getResGuid());
        dwsRainResDi.setResNm(resAd.getResName());
        dwsRainResDi.setProvinceCd(resAd.getProvinceCode());
        dwsRainResDi.setProvinceNm(resAd.getProvinceName());
        dwsRainResDi.setCityCd(resAd.getCityCode());
        dwsRainResDi.setCityNm(resAd.getCityName());
        dwsRainResDi.setCountyCd(resAd.getCountyName());
        dwsRainResDi.setTownCd(resAd.getTownCode());
        dwsRainResDi.setTownNm(resAd.getTownName());
 
        dwsRainResDi.setFlag("人工");
 
        Date date = dateFormatString(dbExcel.getMonitorDate(), "yyyy-MM-dd");
        dwsRainResDi.setDt(date);
        dwsRainResDi.setIsTb("y");
        dwsRainResDi.setDrp(dbExcel.getDrp());
        //根据参数更新数据,如果受影响的行数为0说明这条数据没有,就添加
        Integer result =  dwsRainResDiService.updateCustomize(dwsRainResDi);
 
        if (result <= 0){
            dwsRainResDiService.save(dwsRainResDi);
        }
 
    }
 
    /**
     * 保存水位数据
     * @param dbExcel
     * @param resAd
     */
    private void saveWaterLevel(DbExcel dbExcel, AttResAdVO resAd) {
        DwsWaterResZEightDetailAll dwsWaterResZEightDetailAll = new DwsWaterResZEightDetailAll();
 
 
        dwsWaterResZEightDetailAll.setStCd("");
        dwsWaterResZEightDetailAll.setStNm("人工上报");
 
        dwsWaterResZEightDetailAll.setResCd(resAd.getResGuid());
        dwsWaterResZEightDetailAll.setResNm(resAd.getResName());
        dwsWaterResZEightDetailAll.setEngScal(resAd.getEngScal());
        dwsWaterResZEightDetailAll.setResLat(resAd.getResLat());
        dwsWaterResZEightDetailAll.setResLon(resAd.getResLon());
 
        dwsWaterResZEightDetailAll.setProvinceCd(resAd.getProvinceCode());
        dwsWaterResZEightDetailAll.setProvinceNm(resAd.getProvinceName());
        dwsWaterResZEightDetailAll.setCityCd(resAd.getCityCode());
        dwsWaterResZEightDetailAll.setCityNm(resAd.getCityName());
        dwsWaterResZEightDetailAll.setCountyCd(resAd.getCountyName());
        dwsWaterResZEightDetailAll.setTownCd(resAd.getTownCode());
        dwsWaterResZEightDetailAll.setTownNm(resAd.getTownName());
 
        dwsWaterResZEightDetailAll.setRz(dbExcel.getRz());
        dwsWaterResZEightDetailAll.setLowZ(dbExcel.getLowZ());
 
        dwsWaterResZEightDetailAll.setResourceFlag("人工");
        dwsWaterResZEightDetailAll.setIsTb("y");
 
        Date date = dateFormatString(dbExcel.getMonitorDate() +  " 08:00:00", "yyyy-MM-dd 08:00:00");
        dwsWaterResZEightDetailAll.setDt(date);
        dwsWaterResZEightDetailAll.setTm(date);
        dwsWaterResZEightDetailAll.setCollectDt(date);
        dwsWaterResZEightDetailAll.setParDt(date);
 
        Integer result = dwsWaterResZEightDetailAllService.updateCustomize(dwsWaterResZEightDetailAll);
        if (result<=0){
            dwsWaterResZEightDetailAllService.save(dwsWaterResZEightDetailAll);
        }
 
    }
 
 
    private Date dateFormatString(String date,String format){
 
        if (StringUtil.isNotBlank(date)){
            try {
 
                Date parse = new SimpleDateFormat(format).parse(date);
                return parse;
 
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}