jiangping
2024-10-28 e895b240ff143f989e29908e510fe7157c29a2b1
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
408
package com.doumee.service.business.impl;
 
import com.doumee.core.annotation.excel.ExcelImporter;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.LoginUserModel;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.ScientificNotationTUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.admin.request.MemberImport;
import com.doumee.dao.admin.request.PlatformGasImport;
import com.doumee.dao.business.PlatformWaterGasMapper;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.business.model.PlatformReason;
import com.doumee.dao.business.model.PlatformWaterGas;
import com.doumee.dao.openapi.request.GasByMonthRequest;
import com.doumee.dao.openapi.request.WaterByMonthRequest;
import com.doumee.dao.openapi.response.GasByMonthResponse;
import com.doumee.dao.openapi.response.PlatformLastMonthListResponse;
import com.doumee.dao.openapi.response.WaterByMonthResponse;
import com.doumee.service.business.PlatformWaterGasService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
 
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 月台_用水用气信息记录表Service实现
 * @author 江蹄蹄
 * @date 2024/08/26 16:22
 */
@Service
public class PlatformWaterGasServiceImpl implements PlatformWaterGasService {
 
    @Autowired
    private PlatformWaterGasMapper platformWaterGasMapper;
 
    @Override
    public Integer create(PlatformWaterGas platformWaterGas) {
        if(platformWaterGas.getTimeInfo() ==null || platformWaterGas.getNum()==null
        ||(Constants.equalsInteger(platformWaterGas.getType(),Constants.TWO) && StringUtils.isBlank(platformWaterGas.getCarCode()))){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,参数错误!");
        }
        if( platformWaterGasMapper.selectCount(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted, Constants.ZERO)
                .apply("to_days(time_info) = to_days('"+ DateUtil.getPlusTime2(platformWaterGas.getTimeInfo()) +"')")
                .eq(PlatformWaterGas::getType,platformWaterGas.getType())
                .eq(Constants.equalsInteger(platformWaterGas.getType(),Constants.TWO),PlatformWaterGas::getCarCode,platformWaterGas.getCarCode()))>0){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,该年月数据已存在,请勿重新录入,可以尝试搜索后进行数据修改!");
        }
        platformWaterGas.setIsdeleted(Constants.ZERO);
        platformWaterGas.setType(Constants.formatIntegerNum(platformWaterGas.getType()));
        platformWaterGas.setCreator(platformWaterGas.getLoginUserInfo().getId());
        platformWaterGas.setCreateDate(new Date());
        platformWaterGas.setEditDate(platformWaterGas.getCreateDate());
        platformWaterGas.setEditor(platformWaterGas.getCreator());
        platformWaterGasMapper.insert(platformWaterGas);
        return platformWaterGas.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
 
        platformWaterGasMapper.deleteById(id);
    }
    @Override
    public void deleteById(Integer id, LoginUserInfo model) {
        PlatformWaterGas param = new PlatformWaterGas();
        param.setId(id);
        param.setIsdeleted(Constants.ONE);
        param.setEditor(model.getId());
        param.setEditDate(new Date());
        platformWaterGasMapper.updateById(param);
    }
 
    @Override
    public void delete(PlatformWaterGas platformWaterGas) {
        UpdateWrapper<PlatformWaterGas> deleteWrapper = new UpdateWrapper<>(platformWaterGas);
        platformWaterGasMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        platformWaterGasMapper.deleteBatchIds(ids);
    }
    @Override
    public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo model) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for(Integer id : ids){
            deleteById(id,model);
        }
    }
 
    @Override
    public void updateById(PlatformWaterGas platformWaterGas) {
        if(platformWaterGas.getTimeInfo() ==null
                || platformWaterGas.getId()==null
                || platformWaterGas.getNum()==null
                ||(Constants.equalsInteger(platformWaterGas.getType(),Constants.TWO) && StringUtils.isBlank(platformWaterGas.getCarCode()))){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,该年月数据已存在,请勿重新录入,可以尝试搜索后进行数据修改!");
        }
        if( platformWaterGasMapper.selectCount(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted, Constants.ZERO)
                .ne(PlatformWaterGas::getId,platformWaterGas.getId())
                .apply("to_days(time_info) = to_days('"+ DateUtil.getPlusTime2(platformWaterGas.getTimeInfo()) +"')")
                .eq(PlatformWaterGas::getType, platformWaterGas.getType())
                .eq(Constants.equalsInteger(platformWaterGas.getType(),Constants.TWO),PlatformWaterGas::getCarCode,platformWaterGas.getCarCode()))>0){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,该年月数据已存在,请勿重新录入,可以尝试搜索后进行数据修改!");
        }
 
        platformWaterGas.setEditDate(new Date());
        platformWaterGas.setEditor(platformWaterGas.getLoginUserInfo().getId());
        platformWaterGasMapper.updateById(platformWaterGas);
    }
 
    @Override
    public void updateByIdInBatch(List<PlatformWaterGas> platformWaterGass) {
        if (CollectionUtils.isEmpty(platformWaterGass)) {
            return;
        }
        for (PlatformWaterGas platformWaterGas: platformWaterGass) {
            this.updateById(platformWaterGas);
        }
    }
 
    @Override
    public PlatformWaterGas findById(Integer id) {
        return platformWaterGasMapper.selectById(id);
    }
 
    @Override
    public PlatformWaterGas findOne(PlatformWaterGas platformWaterGas) {
        QueryWrapper<PlatformWaterGas> wrapper = new QueryWrapper<>(platformWaterGas);
        return platformWaterGasMapper.selectOne(wrapper);
    }
 
    @Override
    public List<PlatformWaterGas> findList(PlatformWaterGas platformWaterGas) {
        QueryWrapper<PlatformWaterGas> wrapper = new QueryWrapper<>(platformWaterGas);
        return platformWaterGasMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<PlatformWaterGas> findPage(PageWrap<PlatformWaterGas> pageWrap) {
        IPage<PlatformWaterGas> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<PlatformWaterGas> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.lambda().eq(PlatformWaterGas::getIsdeleted, Constants.ZERO);
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getCreator() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getCreator, pageWrap.getModel().getCreator());
        }
        if (pageWrap.getModel().getCreateDate() != null) {
            queryWrapper.lambda().ge(PlatformWaterGas::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
            queryWrapper.lambda().le(PlatformWaterGas::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
        }
        if (pageWrap.getModel().getEditor() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getEditor, pageWrap.getModel().getEditor());
        }
        if (pageWrap.getModel().getEditDate() != null) {
            queryWrapper.lambda().ge(PlatformWaterGas::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
            queryWrapper.lambda().le(PlatformWaterGas::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
        }
        if (pageWrap.getModel().getIsdeleted() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getRemark() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getTimeInfo() != null) {
            queryWrapper.lambda().ge(PlatformWaterGas::getTimeInfo, Utils.Date.getStart(pageWrap.getModel().getTimeInfo()));
            queryWrapper.lambda().le(PlatformWaterGas::getTimeInfo, Utils.Date.getEnd(pageWrap.getModel().getTimeInfo()));
        }
        if (pageWrap.getModel().getSortnum() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getSortnum, pageWrap.getModel().getSortnum());
        }
        if (pageWrap.getModel().getType() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getType, pageWrap.getModel().getType());
        }
        if (pageWrap.getModel().getNum() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getNum, pageWrap.getModel().getNum());
        }
        if (pageWrap.getModel().getContent() != null) {
            queryWrapper.lambda().eq(PlatformWaterGas::getContent, pageWrap.getModel().getContent());
        }
        if (pageWrap.getModel().getCarCode() != null) {
            queryWrapper.lambda().like(PlatformWaterGas::getCarCode, pageWrap.getModel().getCarCode());
        }
        if (pageWrap.getModel().getStartTime() != null) {
            queryWrapper.lambda().ge(PlatformWaterGas::getTimeInfo, pageWrap.getModel().getStartTime());
        }
        if (pageWrap.getModel().getEndTime() != null) {
            queryWrapper.lambda().le(PlatformWaterGas::getTimeInfo,  pageWrap.getModel().getEndTime());
        }
        queryWrapper.lambda().orderByDesc(PlatformWaterGas::getTimeInfo ,PlatformWaterGas::getCarCode);
        return PageData.from(platformWaterGasMapper.selectPage(page, queryWrapper));
    }
 
    @Override
    public long count(PlatformWaterGas platformWaterGas) {
        QueryWrapper<PlatformWaterGas> wrapper = new QueryWrapper<>(platformWaterGas);
        return platformWaterGasMapper.selectCount(wrapper);
    }
 
 
    @Override
    public WaterByMonthResponse waterDataByMonth(WaterByMonthRequest param){
        Date queryDate = (Objects.nonNull(param)&&Objects.nonNull(param.getTimeInfo()))?param.getTimeInfo():new Date();
        //获取上个月日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(queryDate);
        calendar.add(Calendar.MONTH, -1);
        Date beforMonth = calendar.getTime();
        calendar.add(Calendar.MONTH, -11);
        Date beforYearMonth = calendar.getTime();
 
        WaterByMonthResponse waterByMonthResponse = new WaterByMonthResponse();
 
        List<PlatformWaterGas> list = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,Constants.ZERO)
                .like(PlatformWaterGas::getTimeInfo,DateUtil.getDate(queryDate,"yyyy-MM"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){
            waterByMonthResponse.setCurrentMonth(list.stream().map(m->m.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
        }
 
        list = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,Constants.ZERO)
                .like(PlatformWaterGas::getTimeInfo,DateUtil.getDate(beforMonth,"yyyy-MM"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){
            waterByMonthResponse.setLastMonth(list.stream().map(m->m.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
        }
 
        list = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,Constants.ZERO)
                .like(PlatformWaterGas::getTimeInfo,DateUtil.getDate(beforYearMonth,"yyyy-MM"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){
            waterByMonthResponse.setLastYearMonth(list.stream().map(m->m.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
        }
        return waterByMonthResponse;
    }
 
 
    @Override
    public GasByMonthResponse gasDataByMonth(GasByMonthRequest param){
        Date queryDate = (Objects.nonNull(param)&&Objects.nonNull(param.getTimeInfo()))?param.getTimeInfo():new Date();
        //获取上个月日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(queryDate);
        calendar.add(Calendar.MONTH, -1);
        Date beforMonth = calendar.getTime();
        calendar.add(Calendar.MONTH, -11);
        Date beforYearMonth = calendar.getTime();
 
        GasByMonthResponse response = new GasByMonthResponse();
 
        List<PlatformWaterGas> list = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,Constants.ONE)
                .like(PlatformWaterGas::getTimeInfo,DateUtil.getDate(queryDate,"yyyy-MM"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){
            response.setCurrentMonth(list.stream().map(m->m.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
        }
 
        list = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,Constants.ONE)
                .like(PlatformWaterGas::getTimeInfo,DateUtil.getDate(beforMonth,"yyyy-MM"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){
            response.setLastMonth(list.stream().map(m->m.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
        }
 
        list = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,Constants.ONE)
                .like(PlatformWaterGas::getTimeInfo,DateUtil.getDate(beforYearMonth,"yyyy-MM"))
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){
            response.setLastYearMonth(list.stream().map(m->m.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
        }
        return response;
    }
 
 
    /**
     *
     * @param type 0用水 1用气
     * @return
     */
    @Override
    public List<PlatformLastMonthListResponse> getPlatformLastMonthListResponse(Integer type){
        List<PlatformLastMonthListResponse> lastMonthListResponses = new ArrayList<>();
 
        List<PlatformWaterGas> platformWaterGasList = platformWaterGasMapper.selectList(new QueryWrapper<PlatformWaterGas>().lambda()
                .eq(PlatformWaterGas::getIsdeleted,Constants.ZERO)
                .eq(PlatformWaterGas::getType,type)
        );
        List<String> monthList = DateUtil.getBeforMonth(new Date(),12);
        for (String str:monthList) {
            PlatformLastMonthListResponse platformLastMonthListResponse = new PlatformLastMonthListResponse();
            platformLastMonthListResponse.setNum(platformWaterGasList.stream().filter(i->DateUtil.dateToString(i.getTimeInfo(),"yyyy-MM").equals(str)).map(i->i.getNum()).reduce(BigDecimal.ZERO,BigDecimal::add));
            platformLastMonthListResponse.setTimeInfo(str);
            lastMonthListResponses.add(platformLastMonthListResponse);
        }
        return lastMonthListResponses;
    }
 
 
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public String importBatch(MultipartFile file, LoginUserInfo loginUserInfo){
        try {
            ExcelImporter ie = null;
            List<PlatformGasImport> dataList =null;
            try {
                ie = new ExcelImporter(file,0,0);
                dataList = ie.getDataList(PlatformGasImport.class,null);
            }  catch (Exception e) {
                e.printStackTrace();
            }
            if(dataList == null || dataList.size() ==0){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入数据为空!");
            }
            List<PlatformWaterGas> newList = new ArrayList<>();
            for(int i=0;i<dataList.size();i++){
                PlatformGasImport model = dataList.get(i);
                if(Objects.isNull(model.getTimeInfo())
                || StringUtils.isBlank(model.getCarCode())
                || StringUtils.isBlank(model.getNum()) ){
                    throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+i+2+"行数据参数错误!");
                }
                PlatformWaterGas platformWaterGas = new PlatformWaterGas();
                try {
                    BigDecimal num = new BigDecimal(model.getNum());
                    platformWaterGas.setNum(num);
                }catch (Exception e){
                    throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+i+2+"行数量错误!");
                }
                if( platformWaterGasMapper.selectCount(new QueryWrapper<PlatformWaterGas>().lambda()
                        .eq(PlatformWaterGas::getIsdeleted, Constants.ZERO)
                        .apply("to_days(time_info) = to_days('"+ DateUtil.getPlusTime2(model.getTimeInfo()) +"')")
                        .eq(PlatformWaterGas::getType,Constants.TWO)
                        .eq(PlatformWaterGas::getCarCode,model.getCarCode()))>0){
                    throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,该["+DateUtil.getPlusTime2(model.getTimeInfo())+"]数据已存在,请勿重新录入,可以尝试搜索后进行数据修改!");
                }
 
                platformWaterGas.setTimeInfo(model.getTimeInfo());
                platformWaterGas.setCarCode(model.getCarCode());
                platformWaterGas.setContent(model.getContent());
                platformWaterGas.setIsdeleted(Constants.ZERO);
                platformWaterGas.setType(Constants.TWO);
                platformWaterGas.setCreator(loginUserInfo.getId());
                platformWaterGas.setCreateDate(new Date());
                platformWaterGas.setEditDate(platformWaterGas.getCreateDate());
                platformWaterGas.setEditor(platformWaterGas.getCreator());
                newList.add(platformWaterGas);
            }
            if(newList == null || newList.size() ==0){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入有效数据为空!");
            }
            platformWaterGasMapper.insert(newList);
            return "导入成功";
        }catch (BusinessException e){
            throw e;
        }catch (Exception e){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"信息导入失败,请稍后重试");
        }finally {
            redisTemplate.delete(Constants.RedisKeys.IMPORTING_MEMBER);
        }
    }
 
}