jiangping
2025-06-11 3e76748af46dcc2602205f80caf8e9ea357c1e53
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package com.doumee.service.business.impl;
 
import cn.hutool.core.util.IdcardUtil;
import com.alibaba.fastjson.JSONObject;
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.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.annotation.excel.ExcelExporter;
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.core.utils.aliyun.ALiYunUtil;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dto.*;
import com.doumee.dao.business.join.*;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.vo.ApplyPowerVO;
import com.doumee.dao.business.vo.CountCyclePriceVO;
import com.doumee.dao.system.SystemUserMapper;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.InsuranceApplyOnService;
import com.doumee.service.business.InsuranceApplyService;
import com.doumee.service.business.SmsEmailService;
import com.doumee.service.business.third.SignService;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jzq.common.bean.sign.NotifyDataReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 投保申请信息表Service实现
 * @author 江蹄蹄
 * @date 2024/01/16 10:03
 */
@Service
public class InsuranceApplyOnServiceImpl implements InsuranceApplyOnService {
    @Value("${debug_model}")
    private boolean debugModel;
    @Autowired
    private InsuranceApplyMapper insuranceApplyMapper;
    @Autowired
    private SmsEmailService smsEmailService;
    @Autowired
    private ApplyChangeJoinMapper applyChangeMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private InsuranceApplyJoinMapper insuranceApplyJoinMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private MemberInsuranceJoinMapper memberInsuranceJoinMapper;
    @Autowired
    private ApplyDetailMapper applyDetailMapper;
    @Autowired
    private ApplyDetailJoinMapper applyDetailJoinMapper;
    @Autowired
    private ApplyLogMapper applyLogMapper;
    @Autowired
    private SignService signService;
    @Autowired
    private MultifileMapper multifileMapper;
    @Autowired
    private ApplyLogJoinMapper applyLogJoinMapper;
    @Autowired
    private SolutionsMapper solutionsMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberJoinMapper memberJoinMapper;
    @Autowired
    private DuSolutionJoinMapper duSolutionJoinMapper;
    @Autowired
    private DuWorkTypeJoinMapper duWorkTypeJoinMapper;
    @Autowired
    private SolutionWorktypeJoinMapper solutionWorktypeJoinMapper;
    @Autowired
    private NoticesMapper noticesMapper;
    @Autowired
    private UnionApplyJoinMapper unionApplyJoinMapper;
    @Autowired
    private UnionChangeJoinMapper unionChangeJoinMapper;
    @Autowired
    private CompanySolutionMapper companySolutionMapper;
    @Autowired
    private ContractJoinMapper contractJoinMapper;
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private CompanyDepartmentMapper companyDepartmentMapper;
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer back(InsuranceApply insuranceApply) {
        if(insuranceApply.getId() == null ||StringUtils.isBlank(insuranceApply.getCheckInfo())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        InsuranceApply model = insuranceApplyMapper.selectById(insuranceApply.getId());
        if(model == null ||!Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        Solutions solutions = solutionsMapper.selectById(model.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询方案信息");
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        InsuranceApply update = new InsuranceApply();
        if(Constants.equalsInteger(solutions.getType(),Constants.ZERO)){
            if(Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.UPLOAD_INSURANCE.getKey())){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
            }
            update.setStatus(Constants.InsuranceApplyStatus.FAIL_RETURN.getKey());
        }else{
            if( !(Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.WTB_COMPANY_MEMBER_LIST_SIGNATURE.getKey())
            || Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.WTB_COMPANY_APPLY_SIGNATURE.getKey())
            || Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.WTB_UPLOAD.getKey()))){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
            }
            update.setStatus(Constants.InsuranceApplyStatus.WTB_RETURN.getKey());
        }
        update.setEditDate(new Date());
        update.setEditor(user.getId());
        update.setCheckDate(update.getEditDate());
        update.setCheckInfo(insuranceApply.getCheckInfo());
        update.setCheckUserId(user.getId());
        update.setId(model.getId());
        insuranceApplyMapper.updateById(update);
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.INSURANCE_APPLY;
        //删除其他待办
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey())
                .ne(Notices::getType,Constants.NoticeType.SIX.getStatus())
                .eq(Notices::getObjId,insuranceApply.getId()));
        Notices notices = new Notices(noticeObjectType,Constants.ONE,insuranceApply.getId(),solutions.getName(),
                model.getCompanyId(), Constants.NoticeType.FOUR);
        noticesMapper.insert(notices);
        Constants.ApplyLogType applyLogType = Constants.ApplyLogType.PLATFORM_RETURN;
        String info =applyLogType.getInfo();
        info = info.replace("${param}", update.getCheckInfo());
        ApplyLog log = new ApplyLog(update,applyLogType.getName(),info,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update));
        applyLogMapper.insert(log);
        return  1;
 
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer closeWtbForShop(InsuranceApply insuranceApply) {
        if(insuranceApply.getId() == null ||StringUtils.isBlank(insuranceApply.getCheckInfo())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        InsuranceApply model = insuranceApplyMapper.selectById(insuranceApply.getId());
 
        if(model == null ||!Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        //只有审核通过的状态可关闭
        if(!Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.WTB_BUSINESS_CHECK_PASS.getKey())){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        InsuranceApply update = new InsuranceApply();
        update.setEditDate(new Date());
        update.setEditor(user.getId());
        update.setStatus(Constants.InsuranceApplyStatus.WTB_CLOSED.getKey());
        update.setCheckDate(update.getEditDate());
        update.setCheckInfo(insuranceApply.getCheckInfo());
        update.setCheckUserId(user.getId());
        update.setId(model.getId());
        insuranceApplyMapper.updateById(update);
 
        Constants.ApplyLogType applyLogType = Constants.ApplyLogType.SHOP_CLOSE_WTB_APPLY;
        String info =applyLogType.getInfo();
        info = info.replace("${param}", update.getCheckInfo());
        ApplyLog log = new ApplyLog(update,applyLogType.getName(),info,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update));
        applyLogMapper.insert(log);
        return  1;
    }
 
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer uploadToubaodan(InsuranceApply insuranceApply) {
        if(insuranceApply.getId() == null
                || insuranceApply.getToubaodanFile() == null
                ||StringUtils.isBlank( insuranceApply.getToubaodanFile() .getFileurl())
                ||StringUtils.isBlank( insuranceApply.getToubaodanFile() .getName())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        InsuranceApply model = insuranceApplyMapper.selectById(insuranceApply.getId());
        if(model == null ||!Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!(Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.UPLOAD.getKey())
                || Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.WAIT_SIGNATURE.getKey())
                || Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.PLATFORM_CHECK_PASS.getKey()))){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
        }
 
        Solutions solutions = solutionsMapper.selectById(model.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询方案信息");
        }
 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        InsuranceApply update = new InsuranceApply();
        update.setEditDate(new Date());
        update.setEditor(user.getId());
        update.setStatus(Constants.InsuranceApplyStatus.WAIT_SIGNATURE.getKey());
        update.setCheckDate(update.getEditDate());
        update.setCheckInfo(insuranceApply.getCheckInfo());
        update.setCheckUserId(user.getId());
        update.setId(model.getId());
        insuranceApplyMapper.updateById(update);
 
        multifileMapper.update(null,new UpdateWrapper<Multifile>().lambda()
                .eq(Multifile::getIsdeleted,Constants.ZERO)
                .eq(Multifile::getObjType,Constants.MultiFile.BD_APPLY_PDF.getKey())
                .eq(Multifile::getObjId,update.getId())
                .set(Multifile::getIsdeleted,Constants.ONE)
                .set(Multifile::getEditDate,update.getEditDate())
                .set(Multifile::getEditor,update.getEditor())
        );
        insuranceApply.getToubaodanFile().setIsdeleted(Constants.ZERO);
        insuranceApply.getToubaodanFile().setCreator(user.getId());
        insuranceApply.getToubaodanFile().setObjId(update.getId());
        insuranceApply.getToubaodanFile().setCreateDate(update.getEditDate());
        insuranceApply.getToubaodanFile().setObjType(Constants.MultiFile.BD_APPLY_PDF.getKey());
        insuranceApply.getToubaodanFile().setType(Constants.TWO);
        multifileMapper.insert(insuranceApply.getToubaodanFile());
        update.setToubaodanFile(insuranceApply.getToubaodanFile());
 
        Constants.ApplyLogType applyLogType = Constants.ApplyLogType.WAIT_SIGNATURE;
        String info =applyLogType.getInfo();
//        info = info.replace("${param}", update.getCheckInfo());
        ApplyLog log = new ApplyLog(update,applyLogType.getName(),StringUtils.isNotBlank(update.getCheckInfo())?info:"",update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update));
        applyLogMapper.insert(log);
 
        //删除其他待办
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.INSURANCE_APPLY;
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,insuranceApply.getId()));
 
        Notices notices = new Notices(noticeObjectType,Constants.ONE,model.getId(),solutions.getName(),
                model.getCompanyId(), Constants.NoticeType.ONE);
        noticesMapper.insert(notices);
 
        return  1;
 
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer dealBackApply(InsuranceApply insuranceApply) {
        if(insuranceApply.getId() == null ||StringUtils.isBlank(insuranceApply.getCheckInfo())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        InsuranceApply model = insuranceApplyMapper.selectById(insuranceApply.getId());
 
        if(model == null ||!Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Constants.ApplyLogType applyLogType = null;
        String info = "";
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.INSURANCE_APPLY;
        //处理退回申请通知
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey())
                .ne(Notices::getType,Constants.NoticeType.SIX.getStatus())
                .eq(Notices::getObjId,insuranceApply.getId()));
        InsuranceApply update = new InsuranceApply();
        if(insuranceApply.getDealBackApply() ==1){
            //如果是驳回,只能可驳回已签章状态下的退回申请状态进行操作
            if(!(Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_SIGNATURE.getKey())||
                    Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_WAIT_SIGNATURE.getKey())||
                    Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_UPLOAD.getKey())||
                    Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_PASS.getKey()))
            ){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
            }
            applyLogType = Constants.ApplyLogType.PLATFORM_UN_AGREE_BACK;
            info = applyLogType.getInfo();
            info = info.replace("${param}", insuranceApply.getCheckInfo());
            if( Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_SIGNATURE.getKey())){
                update.setStatus(Constants.InsuranceApplyStatus.SIGNATURE.getKey());
            }else if( Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_WAIT_SIGNATURE.getKey())){
                update.setStatus(Constants.InsuranceApplyStatus.WAIT_SIGNATURE.getKey());
            }else if(  Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_UPLOAD.getKey())){
                update.setStatus(Constants.InsuranceApplyStatus.UPLOAD.getKey());
            }else if(  Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_PASS.getKey())){
                update.setStatus(Constants.InsuranceApplyStatus.PLATFORM_CHECK_PASS.getKey());
            }
        }else{
            //如果是同意,两种申请退回状态都可操作
            if(!(Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_SIGNATURE.getKey())
                    ||Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_WAIT_SIGNATURE.getKey())
                    ||Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_UPLOAD.getKey()))){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
            }
            applyLogType = Constants.ApplyLogType.PLATFORM_AGREE_BACK;
            update.setStatus(Constants.InsuranceApplyStatus.PLATFORM_RETURN.getKey());
 
 
            //通知企业 已退回
            Solutions solutions = solutionsMapper.selectById(model.getSolutionId());
            if(Objects.nonNull(solutions)){
                Notices notices = new Notices(noticeObjectType,Constants.ONE,insuranceApply.getId(),solutions.getName(),
                        insuranceApply.getCompanyId(), Constants.NoticeType.FOUR);
                noticesMapper.insert(notices);
            }
        }
        update.setEditDate(new Date());
        update.setEditor(user.getId());
        update.setCheckDate(update.getEditDate());
        update.setCheckInfo(insuranceApply.getCheckInfo());
        update.setCheckUserId(user.getId());
        update.setId(model.getId());
        insuranceApplyMapper.updateById(update);
        ApplyLog log = new ApplyLog(update,applyLogType.getName(),info,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update));
        applyLogMapper.insert(log);
        return  1;
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer uploadBaoxiandan(InsuranceApply insuranceApply) {
        if(insuranceApply.getId() == null
                || insuranceApply.getCode() == null
                || insuranceApply.getStartTime() == null
                || insuranceApply.getBaoxiandanFile() == null
                ||StringUtils.isBlank( insuranceApply.getBaoxiandanFile() .getFileurl())
                ||StringUtils.isBlank( insuranceApply.getBaoxiandanFile() .getName())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        InsuranceApply model = insuranceApplyMapper.selectById(insuranceApply.getId());
        if(model == null ||!Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.SIGNATURE.getKey())){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请状态已流转,当前不支持该操作~");
        }
        if(model.getApplyEndTime()== null || model.getStartTime().getTime()>model.getApplyEndTime().getTime() ){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该申请生效周期时间不符合要求,请确认后修改重试~");
        }
        Solutions solutions = solutionsMapper.selectById(model.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询方案信息");
        }
        //计算实际截止时间
        Date actEndTime = new Date(model.getApplyEndTime().getTime() + (insuranceApply.getStartTime().getTime() - model.getApplyStartTime().getTime()));
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        InsuranceApply update = new InsuranceApply();
        update.setEditDate(new Date());
        update.setEditor(user.getId());
        update.setStatus(Constants.InsuranceApplyStatus.UPLOAD_INSURANCE.getKey());
        update.setCheckDate(update.getEditDate());
        update.setCheckInfo(insuranceApply.getCheckInfo());
        update.setCheckUserId(user.getId());
        update.setId(model.getId());
        update.setCode(insuranceApply.getCode());
        update.setEndTime(actEndTime);
        update.setStartTime(insuranceApply.getStartTime());
        CountCyclePriceDTO countCyclePriceDTO = new CountCyclePriceDTO();
        countCyclePriceDTO.setSolutionsId(model.getSolutionId());
        countCyclePriceDTO.setStartDate(insuranceApply.getStartTime());
        update.setFinalEndTime(this.getCountCyclePriceVO(countCyclePriceDTO).getEndDate());
        insuranceApplyMapper.updateById(update);
 
        //修改明细行的开始结束日期
        applyDetailMapper.update(null,new UpdateWrapper<ApplyDetail>().lambda()
                .set(ApplyDetail::getStartTime,insuranceApply.getStartTime())
                .set(ApplyDetail::getEndTime,actEndTime)
                .eq(ApplyDetail::getApplyId,model.getId()));
 
        multifileMapper.update(null,new UpdateWrapper<Multifile>().lambda()
                .eq(Multifile::getIsdeleted,Constants.ZERO)
                .eq(Multifile::getObjType,Constants.MultiFile.BD_DONE_PDF.getKey())
                .eq(Multifile::getObjId,update.getId())
                .set(Multifile::getIsdeleted,Constants.ONE)
                .set(Multifile::getEditDate,update.getEditDate())
                .set(Multifile::getEditor,update.getEditor())
        );
 
        insuranceApply.getBaoxiandanFile().setIsdeleted(Constants.ZERO);
        insuranceApply.getBaoxiandanFile().setCreator(user.getId());
        insuranceApply.getBaoxiandanFile().setObjId(update.getId());
        insuranceApply.getBaoxiandanFile().setCreateDate(update.getEditDate());
        insuranceApply.getBaoxiandanFile().setObjType(Constants.MultiFile.BD_DONE_PDF.getKey());
        insuranceApply.getBaoxiandanFile().setType(Constants.TWO);
        multifileMapper.insert(insuranceApply.getBaoxiandanFile());
        update.setBaoxiandanFile(insuranceApply.getBaoxiandanFile());
 
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,Constants.NoticeObjectType.INSURANCE_APPLY.getKey())
                .ne(Notices::getType,Constants.NoticeType.SIX.getStatus())
                .eq(Notices::getObjId,insuranceApply.getId()));
 
        Constants.ApplyLogType applyLogType = Constants.ApplyLogType.UPLOAD_INSURANCE;
        String info = "";
        if(model.getApplyStartTime()!=null && model.getApplyStartTime().getTime()/1000!= insuranceApply.getStartTime().getTime()/1000){
            info =applyLogType.getInfo();
            info = info.replace("${param1}",DateUtil.getPlusTime2(model.getStartTime()));
            info = info.replace("${param2}",DateUtil.getPlusTime2(insuranceApply.getStartTime()));
        }
        ApplyLog log = new ApplyLog(update,applyLogType.getName(), info,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update));
        applyLogMapper.insert(log);
 
 
        //存储员工信息投保记录
        //查询单据明细记录
        MPJLambdaWrapper<ApplyDetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(ApplyDetail.class);
        queryWrapper.selectAs(DispatchUnit::getName,ApplyDetail::getDuName);
        queryWrapper.selectAs(InsuranceApply::getCode,ApplyDetail::getApplyCode);
        queryWrapper.selectAs(Solutions::getId,ApplyDetail::getSolutionId);
        queryWrapper.selectAs(Solutions::getName,ApplyDetail::getSolutionName);
        queryWrapper.selectAs(Worktype::getName,ApplyDetail::getWorkTypeName);
        queryWrapper.selectAs(Member::getIdcardNo,ApplyDetail::getIdcardNo);
        queryWrapper.selectAs(Member::getName,ApplyDetail::getMemberName);
        queryWrapper.leftJoin(DispatchUnit.class,DispatchUnit::getId,ApplyDetail::getDuId);
        queryWrapper.leftJoin(Worktype.class,Worktype::getId,ApplyDetail::getWorktypeId);
        queryWrapper.leftJoin(Member.class,Member::getId,ApplyDetail::getMemberId);
        queryWrapper.leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyDetail::getApplyId);
        queryWrapper.leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId);
        queryWrapper.eq(ApplyDetail::getApplyId,insuranceApply.getId());
        List<ApplyDetail> applyDetailList = applyDetailJoinMapper.selectJoinList(ApplyDetail.class,queryWrapper);
 
        if(CollectionUtils.isNotEmpty(applyDetailList)){
            List<MemberInsurance> memberInsuranceList = new ArrayList<>();
            List<String> idCodeList = new ArrayList<>();
            for (ApplyDetail applyDetail:applyDetailList) {
                idCodeList.add(applyDetail.getIdcardNo());
            }
            List<ApplyDetail> allDetailList =   this.getMemberSolutionList(solutions.getParentId(),idCodeList, update.getStartTime()
                    ,update.getEndTime(),insuranceApply.getId());
 
            for (ApplyDetail applyDetail:applyDetailList) {
                //查询人员信息是否存在相同的方案下是否存在 冲突数据
                this.checkMemberSolution( applyDetail.getIdcardNo(),allDetailList,applyDetail.getMemberName());
                MemberInsurance memberInsurance = new MemberInsurance(applyDetail,user.getId());
                memberInsurance.setRelationType(Constants.ZERO);
                memberInsuranceList.add(memberInsurance);
                memberMapper.update(null,new UpdateWrapper<Member>()
                        .lambda()
                        .set(Member::getStartTime,memberInsurance.getStartTime())
                        .set(Member::getEndTime,memberInsurance.getEndTime())
                        .set(Member::getDuId,memberInsurance.getDuId())
                        .set(Member::getWorktypeId,memberInsurance.getWorktypeId())
                        .eq(Member::getId, memberInsurance.getMemberId())
                );
 
            }
            memberInsuranceJoinMapper.insert(memberInsuranceList);
        }
 
        this.updateApplyCurrentFee(insuranceApply.getId(),null);
        return  1;
    }
 
 
 
 
    public CountCyclePriceVO getCountCyclePriceVO(CountCyclePriceDTO countCyclePriceDTO){
        if(Objects.isNull(countCyclePriceDTO)
                || Objects.isNull(countCyclePriceDTO.getSolutionsId())
                || Objects.isNull(countCyclePriceDTO.getStartDate())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        Solutions solutions = solutionsMapper.selectById(countCyclePriceDTO.getSolutionsId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询方案信息");
        }
        return Constants.countPriceVO(countCyclePriceDTO.getStartDate(),solutions);
    }
 
 
 
 
    /**
     * 更新保单实际金额
     */
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void updateApplyCurrentFee(Integer id,Integer unionId){
        List<InsuranceApply> insuranceApplyList = insuranceApplyJoinMapper.selectJoinList(InsuranceApply.class,new MPJLambdaWrapper<InsuranceApply>()
                .selectAll(InsuranceApply.class)
                .eq(InsuranceApply::getIsdeleted,Constants.ZERO)
                .in(InsuranceApply::getStatus,Constants.InsuranceApplyStatus.UPLOAD_INSURANCE.getKey(),Constants.InsuranceApplyStatus.WTB_DONE.getKey())
                .eq(!Objects.isNull(id),InsuranceApply::getId,id)
                .eq(!Objects.isNull(unionId),InsuranceApply::getUnionApplyId,unionId)
                .le(InsuranceApply::getStartTime,DateUtil.getDate(new Date(),"yyyy-MM-dd") + " 00:00:00")
                .ge(InsuranceApply::getEndTime,DateUtil.getDate(new Date(),"yyyy-MM-dd") + " 23:59:59")
        );
        if(CollectionUtils.isNotEmpty(insuranceApplyList)){
            for (InsuranceApply insuranceApply:insuranceApplyList) {
                Solutions solutions = solutionsMapper.selectById(insuranceApply.getSolutionId());
                if(Objects.isNull(solutions)){
                    continue;
                }
                BigDecimal sumAmount = BigDecimal.ZERO;
                List<ApplyDetail> applyDetailList = applyDetailMapper
                        .selectList(new QueryWrapper<ApplyDetail>().lambda()
                                .ne(ApplyDetail::getChangeStatus,Constants.TWO)
                                .eq(ApplyDetail::getApplyId,insuranceApply.getId()));
                if(CollectionUtils.isNotEmpty(applyDetailList)){
                    for (ApplyDetail applyDetail: applyDetailList) {
                        BigDecimal fee =  Objects.isNull(insuranceApply.getServerCost())?
                                solutions.getPrice():
                                solutions.getPrice().add(insuranceApply.getServerCost());
                        //在开始结束日期之间
                        if(applyDetail.getStartTime().compareTo(new Date()) <= 0 && applyDetail.getEndTime().compareTo(new Date()) >= 0 ){
                            applyDetail.setCurrentFee(Constants.produceFee(solutions,fee,insuranceApply.getStartTime(),insuranceApply.getFinalEndTime(),applyDetail.getStartTime()));
                            applyDetailMapper.updateById(applyDetail);
                        }else  if(applyDetail.getStartTime().compareTo(new Date()) <= 0&&applyDetail.getEndTime().compareTo(new Date()) <= 0){
                            //在结束日期之后
                            applyDetail.setCurrentFee(applyDetail.getFee());
                            applyDetailMapper.updateById(applyDetail);
                        }
                        if(Constants.equalsInteger(applyDetail.getChangeStatus(),Constants.ZERO) || applyDetail.getEndTime().getTime()>System.currentTimeMillis()){
                            sumAmount = sumAmount.add(applyDetail.getCurrentFee());
                        }
                    }
                }
                insuranceApply.setCurrentFee(sumAmount);
                insuranceApplyMapper.updateById(insuranceApply);
            }
        }
    }
 
 
 
    public  List<ApplyDetail>  getMemberSolutionList(Integer solutionParentId,List<String> idCodeList, Date startTime,Date endTime,Integer applyId){
        List<ApplyDetail> applyDetailList = new ArrayList<ApplyDetail>();
        try {
            applyDetailList = applyDetailJoinMapper.selectJoinList(ApplyDetail.class,
                    new MPJLambdaWrapper<ApplyDetail>()
                            .selectAll(ApplyDetail.class)
                            .selectAs(InsuranceApply::getCode,ApplyDetail::getApplyCode)
                            .selectAs(Company::getName,ApplyDetail::getCompanyName)
                            .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyDetail::getApplyId)
                            .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId)
                            .leftJoin(Company.class,Company::getId,InsuranceApply::getCompanyId)
                            .in(ApplyDetail::getIdcardNo,idCodeList)
                            .eq(ApplyDetail::getIsdeleted,Constants.ZERO)
                            .eq(Solutions::getParentId,solutionParentId)
                            .ne(Objects.nonNull(applyId),InsuranceApply::getId,applyId)
                            .notIn(InsuranceApply::getStatus
                                    ,Constants.InsuranceApplyStatus.FAIL_RETURN.getKey()
                                    ,Constants.InsuranceApplyStatus.COMPANY_BACK_APPLY_PASS.getKey()
                                    ,Constants.InsuranceApplyStatus.CLOSE.getKey()
                                    ,Constants.InsuranceApplyStatus.WTB_RETURN.getKey()
                                    ,Constants.InsuranceApplyStatus.WTB_CLOSED.getKey() )
                            .apply(" ( " +
                                    " '"+DateUtil.getPlusTime(startTime)+"'  <= t.start_time AND t.start_time < '"+DateUtil.getPlusTime(endTime)+"' " +
                                    " or " +
                                    "  ( '"+DateUtil.getPlusTime(startTime)+"' < t.end_time AND t.end_time < '"+DateUtil.getPlusTime(endTime)+"' )  " +
                                    " or " +
                                    " ( '"+DateUtil.getPlusTime(startTime)+"' > t.start_time AND '"+DateUtil.getPlusTime(endTime)+"' < t.end_time )" +
                                    " ) " ));
        }catch (Exception e){
        }
        return applyDetailList;
    }
 
 
    /**
     * 查询 会员信息在方案下是否存在冲突数据
     */
    public void checkMemberSolution(String idCode,List<ApplyDetail> allDetailList,String memberName){
        List<ApplyDetail> applyDetailList = getMemberSolutionListByIdcode(idCode,allDetailList);
        if(applyDetailList.size() >Constants.ZERO){
            String companyName = applyDetailList.get(Constants.ZERO).getCompanyName();
            if(StringUtils.isNotBlank(companyName)){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该员工【"+memberName+" "+idCode+"】已在【"+companyName+"】存在保险,请联系客服确认");
            }else{
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该员工【"+memberName+" "+idCode+"】在该保险方案下已存在投保记录,无法进行该操作");
            }
 
        };
    }
 
    private List<ApplyDetail> getMemberSolutionListByIdcode(String idCode, List<ApplyDetail> allDetailList) {
        if(allDetailList == null || allDetailList.size()==0){
            return  new ArrayList<>();
        }
        List<ApplyDetail> list = new ArrayList<>();
        for(ApplyDetail d : allDetailList){
            if(StringUtils.equals(d.getIdcardNo(),idCode)){
                list.add(d);
            }
        }
        return list;
    }
}