jiangping
2024-01-22 6fb727669795f9b62ea103da8874ebe856b294da
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package com.doumee.service.business.impl;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
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.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dto.ApplyChangeOptDTO;
import com.doumee.dao.business.dto.InsuranceApplyOptDTO;
import com.doumee.dao.business.join.ApplyChagneDetailJoinMapper;
import com.doumee.dao.business.join.ApplyChangeJoinMapper;
import com.doumee.dao.business.join.ApplyDetailJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.service.business.ApplyChangeService;
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.xiaoymin.knife4j.core.util.CollectionUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 加减保换厂申请信息表Service实现
 * @author 江蹄蹄
 * @date 2024/01/16 10:03
 */
@Service
public class ApplyChangeServiceImpl implements ApplyChangeService {
 
    @Autowired
    private ApplyChangeMapper applyChangeMapper;
 
    @Autowired
    private InsuranceApplyMapper insuranceApplyMapper;
 
    @Autowired
    private ApplyChangeJoinMapper applyChangeJoinMapper;
 
    @Autowired
    private ApplyChagneDetailJoinMapper applyChagneDetailJoinMapper;
 
    @Autowired
    private ApplyDetailJoinMapper applyDetailJoinMapper;
 
    @Autowired
    private MemberMapper memberMapper;
 
    @Autowired
    private DuSolutionMapper duSolutionMapper;
    @Autowired
    private DuWorktypeMapper duWorktypeMapper;
 
    @Autowired
    private ApplyLogMapper applyLogMapper;
 
    @Autowired
    private SolutionsMapper solutionsMapper;
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer create(ApplyChange applyChange) {
        if (Objects.isNull(applyChange)
                || Objects.isNull(applyChange.getApplyId())
                || Objects.isNull(applyChange.getValidTime())
                || Objects.isNull(applyChange.getType())
                || !(applyChange.getType().equals(Constants.ZERO) || applyChange.getType().equals(Constants.TWO))
        ) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if (!loginUserInfo.getType().equals(Constants.ONE)) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "用户类型错误:非企业用户无法进行该操作");
        }
        InsuranceApply insuranceApply = insuranceApplyMapper.selectById(applyChange.getApplyId());
        if (Objects.isNull(insuranceApply)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if (insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.UPLOAD_INSURANCE.getKey())) {
            throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "保单状态错误");
        }
        if (DateUtil.compareDate(new Date(), insuranceApply.getEndTime()) >= Constants.ZERO
        ) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "保单已过保,无法进行该操作");
        }
        Solutions solutions = solutionsMapper.selectById(insuranceApply.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到保险方案");
        }
        //申请时间必须处于保单的时间范围内
        if (!(DateUtil.compareDate(applyChange.getValidTime(), insuranceApply.getStartTime()) >= Constants.ZERO
                && DateUtil.compareDate(insuranceApply.getEndTime(), applyChange.getValidTime()) >= Constants.ZERO)) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "申请日期未处于保单日期内,无法进行该操作");
        }
        applyChange.setCreateDate(new Date());
        applyChange.setCreator(loginUserInfo.getId());
        applyChange.setIsdeleted(Constants.ZERO);
        applyChange.setStatus(Constants.ZERO);
        applyChangeMapper.insert(applyChange);
 
 
        List<DuSolution> duSolutionList = duSolutionMapper.selectList(new QueryWrapper<DuSolution>().lambda()
                .eq(DuSolution::getIsdeleted, Constants.ZERO)
                .eq(DuSolution::getStatus, Constants.ONE)
                .eq(DuSolution::getSolutionId, insuranceApply.getSolutionId()));
        if (!CollectionUtils.isNotEmpty(duSolutionList)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "数据异常:保险方案下未查询到派遣单位");
        }
 
        //查询所有派遣单位下的工种
        List<Integer> duSolutionIdList = duSolutionList.stream().map(i -> i.getId()).collect(Collectors.toList());
        List<DuWorktype> duWorktypeList = duWorktypeMapper.selectList(new QueryWrapper<DuWorktype>().lambda()
                .eq(DuWorktype::getIsdeleted, Constants.ZERO)
                .eq(DuWorktype::getStatus, Constants.ONE)
                .in(DuWorktype::getDuSolutionId, duSolutionIdList));
        if (!CollectionUtils.isNotEmpty(duWorktypeList)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "数据异常:保险方案下未查询到工种信息");
        }
        //加保数据
        List<ApplyChagneDetail> addDetailList = applyChange.getAddDetailList();
        if (CollectionUtils.isNotEmpty(addDetailList)) {
            this.addChangeDetail(applyChange,addDetailList,duWorktypeList,duSolutionList,insuranceApply,solutions,loginUserInfo);
        }
 
        //减保数据
        List<ApplyChagneDetail> delDetailList = applyChange.getDelDetailList();
        if (CollectionUtils.isNotEmpty(delDetailList)) {
            this.delChangeDetail(applyChange,delDetailList,insuranceApply,solutions,loginUserInfo);
        }
        //换厂业务
        List<ApplyChagneDetail> changeDetailList = applyChange.getChangeDetailList();
        if (CollectionUtils.isNotEmpty(changeDetailList)) {
            this.changeDetail(applyChange,changeDetailList,duWorktypeList,duSolutionList,loginUserInfo);
        }
 
        this.saveApplyLog(applyChange,Constants.ApplyChangeLogStatus.UPLOAD,null);
 
 
        return applyChange.getId();
    }
 
 
    public void saveApplyLog(ApplyChange applyChange,Constants.ApplyChangeLogStatus applyChangeLogStatus,String content){
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        ApplyLog applyLog = new ApplyLog();
        applyLog.setCreateDate(new Date());
        applyLog.setCreator(loginUserInfo.getId());
        applyLog.setIsdeleted(Constants.ZERO);
        applyLog.setApplyId(applyChange.getApplyId());
        applyLog.setTitle(applyChangeLogStatus.getName());
        applyLog.setContent(content);
        applyLog.setObjType(applyChangeLogStatus.getKey());
        applyLog.setObjId( applyChange.getId());
        applyLog.setStatus(applyChange.getStatus());
        applyLogMapper.insert(applyLog);
    }
 
 
    /**
     * 减保数据处理
     * @param applyChange
     * @param delDetailList
     * @param loginUserInfo
     */
    public void delChangeDetail(ApplyChange applyChange ,List<ApplyChagneDetail> delDetailList,InsuranceApply insuranceApply,Solutions solutions,LoginUserInfo loginUserInfo){
        for (ApplyChagneDetail applyChagneDetail : delDetailList) {
            if (Objects.isNull(applyChagneDetail.getMemberId())) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "减保人员【" + applyChagneDetail.getMemberName() + "】必填项缺失");
            }
            Member member = memberMapper.selectById(applyChagneDetail.getMemberId());
            if (Objects.isNull(member)) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "减保人员【" + applyChagneDetail.getMemberName() + "】未查询到系统人员信息");
            }
            //查询员工是否存在  0待签署 1已签章 的此类业务保数据 同一主单下
            if (applyChagneDetailJoinMapper.selectJoinCount(
                    new MPJLambdaWrapper<ApplyChagneDetail>()
                            .leftJoin(ApplyChange.class, ApplyChange::getId, ApplyChagneDetail::getApplyChangeId)
                            .eq(ApplyChagneDetail::getMemberId, applyChagneDetail.getMemberId())
                            .in(ApplyChange::getStatus, Constants.ZERO, Constants.ONE)
            ) > Constants.ZERO) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "减保人员【" + applyChagneDetail.getMemberName() + "】存在申请中的加减保/换厂单据");
            }
            //查询员工是在主单下 是否存在生效中的数据
            List<ApplyDetail> applyDetailList = applyDetailJoinMapper.selectList(new QueryWrapper<ApplyDetail>().lambda()
                    .eq(ApplyDetail::getApplyId, applyChange.getApplyId())
                    .eq(ApplyDetail::getMemberId, applyChagneDetail.getMemberId())
                    .le(ApplyDetail::getStartTime, "now()")
                    .ge(ApplyDetail::getEndTime, "now()"));
            if (applyDetailList.size() > Constants.ONE) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "减保人员【" + applyChagneDetail.getMemberName() + "】保单信息异常,存在多条数据");
            }
            ApplyDetail applyDetail = applyDetailList.get(Constants.ZERO);
            if (applyChange.getValidTime().compareTo(DateUtil.StringToDate(applyDetail.getEndTime())) <= 0) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "减保人员【" + applyChagneDetail.getMemberName() + "】保单保障日期至:【" + applyDetail.getEndTime() + "】无法通过本次申请");
            }
            applyChagneDetail.setCreateDate(new Date());
            applyChagneDetail.setCreator(loginUserInfo.getId());
            applyChagneDetail.setIsdeleted(Constants.ZERO);
            applyChagneDetail.setApplyChangeId(applyChange.getId());
            applyChagneDetail.setType(Constants.ONE);
            applyChagneDetail.setFee(
                    Constants.countDetailFee(solutions,DateUtil.stringToDate(applyDetail.getEndTime(),"yyyy-MM-dd HH:mm:ss"),DateUtil.stringToDate(applyDetail.getStartTime(),"yyyy-MM-dd HH:mm:ss"))
                            .subtract(Constants.countDetailFee(solutions,applyChange.getValidTime(),DateUtil.stringToDate(applyDetail.getStartTime(),"yyyy-MM-dd HH:mm:ss")))
            );
            applyChagneDetailJoinMapper.insert(applyChagneDetail);
        }
    }
 
 
 
    /**
     * 加保数据处理
     * @param applyChange
     * @param addDetailList
     * @param duWorktypeList
     * @param duSolutionList
     * @param loginUserInfo
     */
    public void addChangeDetail(ApplyChange applyChange ,List<ApplyChagneDetail> addDetailList,List<DuWorktype> duWorktypeList,List<DuSolution> duSolutionList,InsuranceApply insuranceApply,Solutions solutions,LoginUserInfo loginUserInfo){
        for (ApplyChagneDetail applyChagneDetail : addDetailList) {
            if (Objects.isNull(applyChagneDetail.getMemberId())
                    || Objects.isNull(applyChagneDetail.getDuId())
                    || Objects.isNull(applyChagneDetail.getWorktypeId())
            ) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "加保人员【" + applyChagneDetail.getMemberName() + "】必填项缺失");
            }
            Member member = memberMapper.selectById(applyChagneDetail.getMemberId());
            if (Objects.isNull(member)) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "加保人员【" + applyChagneDetail.getMemberName() + "】未查询到系统人员信息");
            }
            //查询员工是否存在  0待签署 1已签章 的此类业务保数据 同一主单下
            if (applyChagneDetailJoinMapper.selectJoinCount(
                    new MPJLambdaWrapper<ApplyChagneDetail>()
                            .leftJoin(ApplyChange.class, ApplyChange::getId, ApplyChagneDetail::getApplyChangeId)
                            .eq(ApplyChagneDetail::getMemberId, applyChagneDetail.getMemberId())
                            .in(ApplyChange::getStatus, Constants.ZERO, Constants.ONE)
            ) > Constants.ZERO) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "加保人员【" + applyChagneDetail.getMemberName() + "】存在申请中的加减保/换厂单据");
            }
            //查询员工是在主单下 是否存在生效中的数据
            if (applyDetailJoinMapper.selectCount(new QueryWrapper<ApplyDetail>().lambda()
                    .eq(ApplyDetail::getApplyId, applyChange.getApplyId())
                    .eq(ApplyDetail::getMemberId, applyChagneDetail.getMemberId())
                    .le(ApplyDetail::getStartTime, "now()")
                    .ge(ApplyDetail::getEndTime, "now()")
            ) > Constants.ZERO) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "加保人员【" + applyChagneDetail.getMemberName() + "】存在保障中的保单信息,无法进行加保");
            }
 
            //验证派遣单位信息 与工种信息 是否存在
            if (duSolutionList.stream().filter(d -> d.getDispatchUnitId().equals(applyChagneDetail.getDuId())).collect(Collectors.toList()).size() <= Constants.ZERO) {
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "【" + applyChagneDetail.getMemberName() + "】员工派遣单位未查询到!");
            }
            if (duWorktypeList.stream().filter(d -> d.getDuSolutionId().equals(applyChagneDetail.getDuId()) && d.getWorkTypeId().equals(applyChagneDetail.getWorktypeId()))
                    .collect(Collectors.toList()).size() <= Constants.ZERO) {
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "【" + applyChagneDetail.getMemberName() + "】员工工种信息未查询到!");
            }
            applyChagneDetail.setCreateDate(new Date());
            applyChagneDetail.setCreator(loginUserInfo.getId());
            applyChagneDetail.setIsdeleted(Constants.ZERO);
            applyChagneDetail.setApplyChangeId(applyChange.getId());
            applyChagneDetail.setType(Constants.ZERO);
            applyChagneDetail.setFee(
 
                    Constants.countDetailFee(solutions,insuranceApply.getEndTime(),applyChange.getValidTime())
            );
            applyChagneDetailJoinMapper.insert(applyChagneDetail);
        }
    }
 
 
    /**
     * 换厂业务
     * @param applyChange
     * @param duWorktypeList
     * @param duSolutionList
     * @param changeDetailList
     * @param loginUserInfo
     */
    public void changeDetail(ApplyChange applyChange ,List<ApplyChagneDetail> changeDetailList,List<DuWorktype> duWorktypeList,List<DuSolution> duSolutionList,LoginUserInfo loginUserInfo){
        for (ApplyChagneDetail applyChagneDetail : changeDetailList) {
            if (Objects.isNull(applyChagneDetail.getMemberId())
                    || Objects.isNull(applyChagneDetail.getOldDuId())
                    || Objects.isNull(applyChagneDetail.getOldWorktypeId())
                    || Objects.isNull(applyChagneDetail.getDuId())
                    || Objects.isNull(applyChagneDetail.getWorktypeId())
            ) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】必填项缺失");
            }
            Member member = memberMapper.selectById(applyChagneDetail.getMemberId());
            if (Objects.isNull(member)) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】未查询到系统人员信息");
            }
            //查询员工是否存在  0待签署 1已签章 的此类业务保数据 同一主单下
            if (applyChagneDetailJoinMapper.selectJoinCount(
                    new MPJLambdaWrapper<ApplyChagneDetail>()
                            .leftJoin(ApplyChange.class, ApplyChange::getId, ApplyChagneDetail::getApplyChangeId)
                            .eq(ApplyChagneDetail::getMemberId, applyChagneDetail.getMemberId())
                            .in(ApplyChange::getStatus, Constants.ZERO, Constants.ONE)
            ) > Constants.ZERO) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】存在申请中的加减保/换厂单据");
            }
            //查询员工是在主单下 是否存在生效中的数据
            List<ApplyDetail>  applyDetailList = applyDetailJoinMapper.selectList(new QueryWrapper<ApplyDetail>().lambda()
                    .eq(ApplyDetail::getApplyId, applyChange.getApplyId())
                    .eq(ApplyDetail::getMemberId, applyChagneDetail.getMemberId())
                    .le(ApplyDetail::getStartTime, "now()")
                    .ge(ApplyDetail::getEndTime, "now()"));
            if(applyDetailList.size()>Constants.ONE){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】保单信息异常,存在多条数据");
            }
            ApplyDetail applyDetail = applyDetailList.get(Constants.ZERO);
            if(applyChange.getValidTime().compareTo(DateUtil.StringToDate(applyDetail.getEndTime()))<=0){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】保单保障日期至:【"+applyDetail.getEndTime()+"】无法通过本次申请");
            }
            //验证派遣单位信息 与工种信息 是否存在
            if (duSolutionList.stream().filter(d -> d.getDispatchUnitId().equals(applyChagneDetail.getDuId())).collect(Collectors.toList()).size() <= Constants.ZERO) {
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】员工派遣单位未查询到!");
            }
            if (duWorktypeList.stream().filter(d -> d.getDuSolutionId().equals(applyChagneDetail.getDuId()) && d.getWorkTypeId().equals(applyChagneDetail.getWorktypeId()))
                    .collect(Collectors.toList()).size() <= Constants.ZERO) {
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "换厂人员【" + applyChagneDetail.getMemberName() + "】员工工种信息未查询到!");
            }
            applyChagneDetail.setCreateDate(new Date());
            applyChagneDetail.setCreator(loginUserInfo.getId());
            applyChagneDetail.setIsdeleted(Constants.ZERO);
            applyChagneDetail.setApplyChangeId(applyChange.getId());
            applyChagneDetail.setType(Constants.TWO);
            applyChagneDetailJoinMapper.insert(applyChagneDetail);
        }
    }
 
    @Override
    public void deleteById(Integer id) {
        applyChangeMapper.deleteById(id);
    }
 
    @Override
    public void delete(ApplyChange applyChange) {
        UpdateWrapper<ApplyChange> deleteWrapper = new UpdateWrapper<>(applyChange);
        applyChangeMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        applyChangeMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(ApplyChange applyChange) {
        applyChangeMapper.updateById(applyChange);
    }
 
    @Override
    public void updateByIdInBatch(List<ApplyChange> applyChanges) {
        if (CollectionUtils.isEmpty(applyChanges)) {
            return;
        }
        for (ApplyChange applyChange: applyChanges) {
            this.updateById(applyChange);
        }
    }
 
    @Override
    public ApplyChange findById(Integer id) {
        return applyChangeMapper.selectById(id);
    }
 
    @Override
    public ApplyChange findOne(ApplyChange applyChange) {
        QueryWrapper<ApplyChange> wrapper = new QueryWrapper<>(applyChange);
        return applyChangeMapper.selectOne(wrapper);
    }
 
    @Override
    public List<ApplyChange> findList(ApplyChange applyChange) {
        QueryWrapper<ApplyChange> wrapper = new QueryWrapper<>(applyChange);
        return applyChangeMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<ApplyChange> findPage(PageWrap<ApplyChange> pageWrap) {
        IPage<ApplyChange> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<ApplyChange> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(ApplyChange::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getCreator() != null) {
            queryWrapper.lambda().eq(ApplyChange::getCreator, pageWrap.getModel().getCreator());
        }
        if (pageWrap.getModel().getCreateDate() != null) {
            queryWrapper.lambda().ge(ApplyChange::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
            queryWrapper.lambda().le(ApplyChange::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
        }
        if (pageWrap.getModel().getEditor() != null) {
            queryWrapper.lambda().eq(ApplyChange::getEditor, pageWrap.getModel().getEditor());
        }
        if (pageWrap.getModel().getEditDate() != null) {
            queryWrapper.lambda().ge(ApplyChange::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
            queryWrapper.lambda().le(ApplyChange::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
        }
        if (pageWrap.getModel().getIsdeleted() != null) {
            queryWrapper.lambda().eq(ApplyChange::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getRemark() != null) {
            queryWrapper.lambda().eq(ApplyChange::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getSortnum() != null) {
            queryWrapper.lambda().eq(ApplyChange::getSortnum, pageWrap.getModel().getSortnum());
        }
        if (pageWrap.getModel().getApplyId() != null) {
            queryWrapper.lambda().eq(ApplyChange::getApplyId, pageWrap.getModel().getApplyId());
        }
        if (pageWrap.getModel().getCode() != null) {
            queryWrapper.lambda().eq(ApplyChange::getCode, pageWrap.getModel().getCode());
        }
        if (pageWrap.getModel().getApplyStartTime() != null) {
            queryWrapper.lambda().ge(ApplyChange::getApplyStartTime, Utils.Date.getStart(pageWrap.getModel().getApplyStartTime()));
            queryWrapper.lambda().le(ApplyChange::getApplyStartTime, Utils.Date.getEnd(pageWrap.getModel().getApplyStartTime()));
        }
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.lambda().eq(ApplyChange::getStatus, pageWrap.getModel().getStatus());
        }
        if (pageWrap.getModel().getValidTime() != null) {
            queryWrapper.lambda().ge(ApplyChange::getValidTime, Utils.Date.getStart(pageWrap.getModel().getValidTime()));
            queryWrapper.lambda().le(ApplyChange::getValidTime, Utils.Date.getEnd(pageWrap.getModel().getValidTime()));
        }
        if (pageWrap.getModel().getValidCode() != null) {
            queryWrapper.lambda().eq(ApplyChange::getValidCode, pageWrap.getModel().getValidCode());
        }
        if (pageWrap.getModel().getType() != null) {
            queryWrapper.lambda().eq(ApplyChange::getType, pageWrap.getModel().getType());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(applyChangeMapper.selectPage(page, queryWrapper));
    }
 
    @Override
    public long count(ApplyChange applyChange) {
        QueryWrapper<ApplyChange> wrapper = new QueryWrapper<>(applyChange);
        return applyChangeMapper.selectCount(wrapper);
    }
 
 
 
    @Override
    public PageData<ApplyChange> findPageForCompany(PageWrap<ApplyChange> pageWrap) {
        IPage<ApplyChange> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<ApplyChange> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        ApplyChange model = pageWrap.getModel() ;
        queryWrapper
                .selectAll(ApplyChange.class)
                .selectAs(InsuranceApply::getCode,ApplyChange::getApplyCode)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 0  )",ApplyChange::getAddNum)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 1  )",ApplyChange::getDelNum)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 2  )",ApplyChange::getChangeNum)
                .select("( select ifnull(sum(ad.FEE),0) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID  )",ApplyChange::getChangeMoney)
                .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyChange::getApplyId)
                .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId)
                .eq(!Objects.isNull(model.getType()),ApplyChange::getType,model.getType())
                .eq(!Objects.isNull(model.getStatus()),ApplyChange::getStatus,model.getStatus())
                .eq(!Objects.isNull(model.getCompanyId()),InsuranceApply::getCompanyId,model.getCompanyId())
                .eq(!Objects.isNull(model.getSolutionsId()),ApplyChange::getSolutionsId,model.getSolutionsId())
                .ge(StringUtils.isNotBlank(model.getCreateDateS()),ApplyChange::getCreateDate, model.getCreateDateS()+" 00:00:00" )
                .le(StringUtils.isNotBlank(model.getCreateDateE()),ApplyChange::getCreateDate, model.getCreateDateE()+" 23:59:59" );
 
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        PageData<ApplyChange> pageData = PageData.from(applyChangeJoinMapper.selectJoinPage(page,ApplyChange.class, queryWrapper));
        return pageData;
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void changeOpt(ApplyChangeOptDTO applyChangeOptDTO){
        if(Objects.isNull(applyChangeOptDTO)
                ||Objects.isNull(applyChangeOptDTO.getApplyId())
                ||StringUtils.isBlank(applyChangeOptDTO.getOptIllustration())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
 
        ApplyChange applyChange = applyChangeMapper.selectById(applyChangeOptDTO.getApplyId());
        if(Objects.isNull(applyChange)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到单据数据");
        }
        InsuranceApply insuranceApply = insuranceApplyMapper.selectById(applyChange.getApplyId());
        if(Objects.isNull(insuranceApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到保单数据");
        }
        if(applyChange.getIsdeleted().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"数据已删除,无法进行该操作");
        }
        Constants.ApplyChangeLogStatus applyChangeLogStatus = Constants.ApplyChangeLogStatus.RETURN_APPLY;
 
        if(applyChangeOptDTO.getOptType().equals(Constants.ApplyChangeStatus.RETURN_APPLY.getKey())){
            //发起退回申请
            if(loginUserInfo.getType().equals(Constants.ONE)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业端用户无法进行该操作");
            }
            if(!applyChange.getStatus().equals(Constants.ApplyChangeStatus.UPLOAD.getKey()) ){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据状态错误无法进行该操作!");
            }
        }else if(applyChangeOptDTO.getOptType().equals(Constants.ApplyChangeStatus.PLATFORM_AGREE.getKey())){
            applyChangeLogStatus = Constants.ApplyChangeLogStatus.PLATFORM_AGREE;
            //平台同意退回
            if(loginUserInfo.getType().equals(Constants.ZERO)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非平台端用户无法进行该操作");
            }
            if(!applyChange.getStatus().equals(Constants.ApplyChangeStatus.RETURN_APPLY.getKey())){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据状态错误无法进行该操作!");
            }
        }else if(applyChangeOptDTO.getOptType().equals(Constants.ApplyChangeStatus.UPLOAD.getKey())){
            applyChangeLogStatus = Constants.ApplyChangeLogStatus.PLATFORM_UN_AGREE;
            //平台驳回退回
            if(loginUserInfo.getType().equals(Constants.ZERO)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非平台端用户无法进行该操作");
            }
            if(!applyChange.getStatus().equals(Constants.ApplyChangeStatus.RETURN_APPLY.getKey())){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据状态错误无法进行该操作!");
            }
        }else if(applyChangeOptDTO.getOptType().equals(Constants.ApplyChangeStatus.CLOSE.getKey())){
            applyChangeLogStatus = Constants.ApplyChangeLogStatus.CLOSE;
            //企业关闭
            if(loginUserInfo.getType().equals(Constants.ONE)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业端用户无法进行该操作");
            }
            if(!applyChange.getStatus().equals(Constants.ApplyChangeStatus.PLATFORM_AGREE.getKey())){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据状态错误无法进行该操作!");
            }
        }else{
            throw new BusinessException(ResponseStatus.BAD_REQUEST  );
        }
        applyChange.setStatus(applyChangeOptDTO.getOptType());
        applyChangeMapper.updateById(applyChange);
//        //存储日志数据
        this.saveApplyLog(applyChange,applyChangeLogStatus,applyChangeOptDTO.getOptIllustration());
    }
 
 
    @Override
    public ApplyChange findDetail(Integer id){
        MPJLambdaWrapper<ApplyChange> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(ApplyChange.class)
                .selectAs(InsuranceApply::getCode,ApplyChange::getApplyCode)
                .selectAs(Company::getName,ApplyChange::getCompanyName)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 0  )",ApplyChange::getAddNum)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 1  )",ApplyChange::getDelNum)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 2  )",ApplyChange::getChangeNum)
                .select("( select ifnull(sum(ad.FEE),0) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID  )",ApplyChange::getChangeMoney)
                .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyChange::getApplyId)
                .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId)
                .leftJoin(Company.class,Company::getId,InsuranceApply::getCompanyId)
                .eq(ApplyChange::getId,id);
        ApplyChange applyChange =  applyChangeJoinMapper.selectOne(queryWrapper);
        return applyChange;
    }
 
 
 
 
}