MrShi
2024-11-01 deb4575b72443cf906946a9fb28a557c16556fb9
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
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.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dto.DirectInvoicingDTO;
import com.doumee.dao.business.dto.DirectInvoicingDetailDTO;
import com.doumee.dao.business.dto.EntrustInvoicingDTO;
import com.doumee.dao.business.join.ApplyChangeJoinMapper;
import com.doumee.dao.business.join.InsuranceApplyJoinMapper;
import com.doumee.dao.business.join.TaxDetailJoinMapper;
import com.doumee.dao.business.join.TaxesJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.vo.TaxesInvoicingVO;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.TaxesService;
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;
 
/**
 * 发票信息表Service实现
 * @author 江蹄蹄
 * @date 2024/01/16 10:03
 */
@Service
public class TaxesServiceImpl implements TaxesService {
 
    @Autowired
    private TaxesMapper taxesMapper;
    @Autowired
    private TaxDetialMapper taxDetialMapper;
    @Autowired
    private TaxesJoinMapper taxesJoinMapper;
    @Autowired
    private TaxDetailJoinMapper taxDetailJoinMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private InsuranceApplyJoinMapper insuranceApplyJoinMapper;
    @Autowired
    private ApplyChangeJoinMapper applyChangeJoinMapper;
    @Autowired
    private SolutionsMapper solutionsMapper;
    @Autowired
    private NoticesMapper noticesMapper;
    @Override
    public Integer create(Taxes taxes) {
        taxesMapper.insert(taxes);
        return taxes.getId();
    }
 
    /**
     * 退回申请
     * @param taxes
     */
    @Override
    @Transactional
    public void backApply(Taxes taxes) {
        if(taxes.getId() == null ){
            throw   new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        Taxes model = taxesMapper.selectById(taxes.getId());
        if(model == null ||Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(taxes.getStatus(), Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前发票状态不支持撤回申请!");
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Taxes update = new Taxes();
        update.setId(taxes.getId());
        update.setImgurl(taxes.getImgurl());
        update.setEditDate(new Date());
        update.setStatus(Constants.TWO);
        update.setEditor(user.getId());
        update.setCancelUserId(user.getId());
        update.setCancelDate(update.getEditDate());
        update.setCancelInfo(taxes.getCancelInfo());
        taxesMapper.updateById(update);
 
        List<TaxDetial> detialList = getDetailListById(taxes.getId());
        if(detialList!=null && detialList.size()>0){
            for(TaxDetial detial :detialList){
                //类型 0保单申请 1加减保申请 2冲红单
                if(Constants.equalsInteger(detial.getType(),Constants.TWO)){
                    //如果是冲红单,还原该申请为已开票
                    Taxes tu = new Taxes();
                    tu.setId(detial.getDelTaxId());
                    tu.setEditDate(new Date());
                    tu.setStatus(Constants.ONE);
                    tu.setEditor(user.getId());
                    taxesMapper.updateById(tu);
                }
            }
        }
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.TAXES;
        //删除其他待办
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,model.getId()));
    }
 
    private List<TaxDetial> getDetailListById(Integer id) {
        List<TaxDetial> detialList =  taxDetialMapper.selectList(new QueryWrapper<TaxDetial>().lambda().eq(TaxDetial::getTaxId,id).eq(TaxDetial::getIsdeleted,Constants.ZERO));
        return detialList;
    }
    private List<TaxDetial> getJoinDetailListById(Integer id) {
        MPJLambdaWrapper<TaxDetial> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TaxDetial.class);
        queryWrapper.selectAs(Solutions::getName,TaxDetial::getSolutionName);
        queryWrapper.selectAs(Taxes::getDoneCode,TaxDetial::getTaxDoneCode);
        queryWrapper.selectAs(Taxes::getCreateDate,TaxDetial::getTaxCreateDate);
        queryWrapper.selectAs(InsuranceApply::getCode,TaxDetial::getApplyCode);
        queryWrapper.selectAs(ApplyChange::getValidCode,TaxDetial::getChangApplyCode);
/*        queryWrapper.select("(CASE t.type\n" +
                "WHEN 0 THEN (select count(1) from apply_detail c where c.APPLY_ID=t.INSURANCE_APPLY_ID) \n" +
                "ELSE 0  \n" +
                "END) as applyNum,\n" +
                "(CASE t.type \n" +
                "WHEN 1 THEN (select count(1) from apply_chagne_detail c where c.APPLY_CHANGE_ID=t.INSURANCE_APPLY_ID)  \n" +
                "ELSE 0  \n" +
                "END) as applyChangeAddNum,\n" +
                "(CASE t.type \n" +
                "WHEN 1 THEN  (select count(1) from apply_chagne_detail c where c.APPLY_CHANGE_ID=t.APPLY_CHANGE_ID)  \n" +
                "ELSE 0  \n" +
                "END) as applyChangeAddNum");*/
        queryWrapper.leftJoin(InsuranceApply.class,InsuranceApply::getId,TaxDetial::getInsuranceApplyId);
        queryWrapper.leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId);
        queryWrapper.leftJoin(ApplyChange.class,ApplyChange::getId,TaxDetial::getApplyChangeId);
        queryWrapper.leftJoin(Taxes.class,Taxes::getId,TaxDetial::getDelTaxId);
        queryWrapper.eq(  TaxDetial::getTaxId,id);
        List<TaxDetial> detialList =  taxDetailJoinMapper.selectJoinList(TaxDetial.class,queryWrapper.orderByAsc(TaxDetial::getType));
 
 
        return detialList;
    }
 
 
    /**
     * 上传发票凭证
     * @param taxes
     */
    @Override
    public void doneApply(Taxes taxes) {
        if(taxes.getId() == null
         ||StringUtils.isBlank(taxes.getImgurl())
         ||StringUtils.isBlank(taxes.getDoneCode())){
            throw   new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        Taxes model = taxesMapper.selectById(taxes.getId());
        if(model == null ||Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(taxes.getStatus(), Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前发票状态不支持上传发票信息!");
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Taxes update = new Taxes();
        update.setId(taxes.getId());
        update.setImgurl(taxes.getImgurl());
        update.setEditDate(new Date());
        update.setStatus(Constants.ONE);
        update.setEditor(user.getId());
        update.setDoneCode(taxes.getDoneCode());
        update.setDoneUserId(user.getId());
        update.setDoneDate(update.getEditDate());
        update.setImgurl(taxes.getImgurl());
        taxesMapper.updateById(update);
 
        //查询开票明细记录
        List<TaxDetial> taxDetialList = taxDetialMapper.selectList(new QueryWrapper<TaxDetial>().lambda()
                .eq(TaxDetial::getTaxId,taxes.getId())
                .eq(TaxDetial::getType,Constants.TWO));
        if(CollectionUtils.isNotEmpty(taxDetialList)){
            for (TaxDetial taxDetial:taxDetialList) {
                taxesMapper.update(null,new UpdateWrapper<Taxes>().lambda()
                                .set(Taxes::getEditDate,new Date())
                                .set(Taxes::getEditor,user.getId())
                        .set(Taxes::getStatus,3).eq(Taxes::getId,taxDetial.getDelTaxId()));
            }
        }
 
 
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.TAXES;
        //删除其他待办
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,taxes.getId()));
 
    }
 
    @Override
    public void deleteById(Integer id) {
        taxesMapper.deleteById(id);
    }
 
    @Override
    public void delete(Taxes taxes) {
        UpdateWrapper<Taxes> deleteWrapper = new UpdateWrapper<>(taxes);
        taxesMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        taxesMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(Taxes taxes) {
        taxesMapper.updateById(taxes);
    }
 
    @Override
    public void updateByIdInBatch(List<Taxes> taxess) {
        if (CollectionUtils.isEmpty(taxess)) {
            return;
        }
        for (Taxes taxes: taxess) {
            this.updateById(taxes);
        }
    }
 
    @Override
    public Taxes findById(Integer id) {
        MPJLambdaWrapper<Taxes> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Taxes.class);
        queryWrapper.selectAs(Solutions::getName,Taxes::getSolutionName);
        queryWrapper.selectAs(SystemUser::getRealname,Taxes::getCancelUserName);
        queryWrapper.leftJoin(Solutions.class,Solutions::getId,Taxes::getSolutionId);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Taxes::getCancelUserId);
        Taxes model =taxesJoinMapper.selectById(id);
        if(model==null || !Constants.equalsInteger(model.getIsdeleted(),Constants.ZERO)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(model.getApplyType(),Constants.ZERO)){
            List<TaxDetial> detialList = getDetailsApplysListById(id);
            model.setApplyList(detialList);
        }else{
            //查询明细列表
            List<TaxDetial> detialList = getJoinDetailListById(id);
            List<TaxDetial> applyList = new ArrayList<>();//投保和加减保
            List<TaxDetial> delList = new ArrayList<>();//冲红单
            if(detialList!=null){
                for(TaxDetial d :detialList){
//                    d.setSolutionName(model.getSolutionName());
                    d.setFee(Constants.formatBigdecimal(d.getFee()));
                    d.setTotalFee(Constants.formatBigdecimal(d.getTotalFee()));
                    if(Constants.equalsInteger(d.getType(),Constants.ZERO)
                            || Constants.equalsInteger(d.getType(),Constants.ONE)){
                        applyList.add(d);
                    }else  if(Constants.equalsInteger(d.getType(),Constants.TWO)){
                        delList.add(d);
                    }
                }
            }
            model.setApplyList(applyList);
            model.setDelTaxList(delList);
        }
        return model;
    }
 
    private List<TaxDetial> getDetailsApplysListById(Integer id) {
            MPJLambdaWrapper<TaxDetial> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.selectAll(TaxDetial.class);
            queryWrapper.selectAs(InsuranceApply::getCode,TaxDetial::getApplyCode);
            queryWrapper.selectAs(Solutions::getName,TaxDetial::getSolutionName);
            queryWrapper.selectAs(ApplyChange::getValidCode,TaxDetial::getChangApplyCode);
            queryWrapper.leftJoin(InsuranceApply.class,InsuranceApply::getId,TaxDetial::getInsuranceApplyId);
            queryWrapper.leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId);
            queryWrapper.leftJoin(ApplyChange.class,ApplyChange::getId,TaxDetial::getApplyChangeId);
            queryWrapper.in(TaxDetial::getType,Constants.ZERO,Constants.ONE);
            queryWrapper.eq(TaxDetial::getTaxId,id);
            List<TaxDetial> detialList =  taxDetailJoinMapper.selectJoinList(TaxDetial.class,queryWrapper.orderByAsc(TaxDetial::getType));
            return detialList;
    }
 
    @Override
    public Taxes findOne(Taxes taxes) {
        QueryWrapper<Taxes> wrapper = new QueryWrapper<>(taxes);
        return taxesMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Taxes> findList(Taxes taxes) {
        QueryWrapper<Taxes> wrapper = new QueryWrapper<>(taxes);
        return taxesMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<Taxes> findPage(PageWrap<Taxes> pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        IPage<Taxes> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Taxes> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Taxes.class);
        queryWrapper.selectAs(SystemUser::getRealname,Taxes::getCreatorName);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Taxes::getCreator);
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.eq(Taxes::getIsdeleted, Constants.ZERO);
        if(user.getCompanyIdList() == null || user.getCompanyIdList().size() == 0){
            queryWrapper.eq(Taxes::getCompanyId,-1);//设置无效访问
        }else{
            queryWrapper.in(Taxes::getCompanyId,user.getCompanyIdList());
            queryWrapper.ge(pageWrap.getModel().getStartDate() != null,Taxes::getCreateDate, pageWrap.getModel().getStartDate());
            queryWrapper.le(pageWrap.getModel().getEndDate() != null,Taxes::getCreateDate, pageWrap.getModel().getEndDate());
            queryWrapper.eq(pageWrap.getModel().getTaxBank() != null,Taxes::getTaxBank, pageWrap.getModel().getTaxBank());
            queryWrapper.eq(pageWrap.getModel().getTaxBank() != null,Taxes::getTaxBank, pageWrap.getModel().getTaxBank());
            queryWrapper.eq(pageWrap.getModel().getRemark() != null,Taxes::getRemark, pageWrap.getModel().getRemark());
            queryWrapper.eq(pageWrap.getModel().getStatus() != null,Taxes::getStatus, pageWrap.getModel().getStatus());
            queryWrapper.eq(pageWrap.getModel().getCompanyId() != null,Taxes::getCompanyId, pageWrap.getModel().getCompanyId());
            queryWrapper.eq(pageWrap.getModel().getType() != null,Taxes::getType, pageWrap.getModel().getType());
            queryWrapper.eq(pageWrap.getModel().getTaxCode() != null,Taxes::getTaxCode, pageWrap.getModel().getTaxCode());
            queryWrapper.eq(pageWrap.getModel().getTaxAccount() != null,Taxes::getTaxAccount, pageWrap.getModel().getTaxAccount());
            queryWrapper.eq(pageWrap.getModel().getTaxAddr() != null,Taxes::getTaxAddr, pageWrap.getModel().getTaxAddr());
            queryWrapper.eq(pageWrap.getModel().getAddr() !=null,Taxes::getAddr, pageWrap.getModel().getAddr());
            queryWrapper.like(pageWrap.getModel().getCompanyName() != null,Taxes::getCompanyName, pageWrap.getModel().getCompanyName());
            queryWrapper.eq(pageWrap.getModel().getApplyType() != null,Taxes::getApplyType, pageWrap.getModel().getApplyType());
            queryWrapper.orderByDesc(Taxes::getCreateDate);
        }
        PageData<Taxes>  result =PageData.from(taxesJoinMapper.selectJoinPage(page,Taxes.class, queryWrapper));
        if(result!=null && result.getRecords()!=null){
            String path =systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+systemDictDataBiz.queryByCode(Constants.OSS,Constants.TAXES_FILE).getCode();
            for(Taxes t : result.getRecords()){
                if(StringUtils.isNotBlank(t.getImgurl())){
                    t.setImgurlFull(path + t.getImgurl());
                }
            }
        }
        return result;
    }
 
    @Override
    public long count(Taxes taxes) {
        QueryWrapper<Taxes> wrapper = new QueryWrapper<>(taxes);
        return taxesMapper.selectCount(wrapper);
    }
 
 
    @Override
    public PageData<Taxes> findPageForCompany(PageWrap<Taxes> pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        IPage<Taxes> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Taxes> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Taxes.class);
        queryWrapper.selectAs(SystemUser::getRealname,Taxes::getCreatorName);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Taxes::getCreator);
 
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.eq(Taxes::getIsdeleted, Constants.ZERO);
        Taxes queryModel = pageWrap.getModel();
        if(!user.getType().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业用户无法进行该业务查询");
        }else{
            queryWrapper.eq(Taxes::getCompanyId,user.getCompanyId());
        }
        if(!Objects.isNull(queryModel)){
            queryWrapper.ge(pageWrap.getModel().getStartDate() != null,Taxes::getCreateDate, (pageWrap.getModel().getStartDate()));
            queryWrapper.le(pageWrap.getModel().getEndDate() != null,Taxes::getCreateDate,  (pageWrap.getModel().getEndDate()));
            queryWrapper.eq(!Objects.isNull(queryModel.getInsuranceApplyId()),Taxes::getInsuranceApplyId,queryModel.getInsuranceApplyId());
            queryWrapper.eq(!Objects.isNull(queryModel.getStatus()),Taxes::getStatus,queryModel.getStatus());
        }
 
        queryWrapper.orderByAsc(Taxes::getCreateDate);
        PageData<Taxes>  result =PageData.from(taxesJoinMapper.selectJoinPage(page,Taxes.class, queryWrapper));
        if(result!=null && result.getRecords()!=null){
            String path =systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+systemDictDataBiz.queryByCode(Constants.OSS,Constants.TAXES_FILE).getCode();
            for(Taxes t : result.getRecords()){
                if(StringUtils.isNotBlank(t.getImgurl())){
                    t.setImgurlFull(path + t.getImgurl());
                }
            }
        }
        return result;
    }
 
 
 
    @Override
    public List<Taxes> findListForCompany(Taxes queryModel) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        MPJLambdaWrapper<Taxes> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Taxes.class);
        queryWrapper.selectAs(SystemUser::getRealname,Taxes::getCreatorName);
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Taxes::getCreator);
 
        queryWrapper.eq(Taxes::getIsdeleted, Constants.ZERO);
        if(!user.getType().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业用户无法进行该业务查询");
        }
        if(!Objects.isNull(queryModel)){
            queryWrapper.ge(queryModel.getStartDate() != null,Taxes::getCreateDate, (queryModel.getStartDate()));
            queryWrapper.le(queryModel.getEndDate() != null,Taxes::getCreateDate,  (queryModel.getEndDate()));
            queryWrapper.eq(!Objects.isNull(queryModel.getInsuranceApplyId()),Taxes::getInsuranceApplyId,queryModel.getInsuranceApplyId());
            queryWrapper.eq(!Objects.isNull(queryModel.getStatus()),Taxes::getStatus,queryModel.getStatus());
            queryWrapper.exists(!Objects.isNull(queryModel.getApplyId())," select 1 from tax_detial td where td.insurance_apply_id = "+queryModel.getApplyId()+" and t.id = td.tax_id ");
        }
        queryWrapper.orderByAsc(Taxes::getCreateDate);
 
        List<Taxes> result = taxesJoinMapper.selectJoinList(Taxes.class, queryWrapper);
        String path =systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+systemDictDataBiz.queryByCode(Constants.OSS,Constants.TAXES_FILE).getCode();
        for(Taxes t : result){
            if(StringUtils.isNotBlank(t.getImgurl())){
                t.setImgurlFull(path + t.getImgurl());
            }
        }
        return result;
    }
 
 
 
 
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public void entrustInvoicing(EntrustInvoicingDTO entrustInvoicingDTO){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(entrustInvoicingDTO)
            || Objects.isNull(entrustInvoicingDTO.getInvoicingMoney())
            || Objects.isNull(entrustInvoicingDTO.getType())
            || Objects.isNull(entrustInvoicingDTO.getTaxDetialList())
            || StringUtils.isBlank(entrustInvoicingDTO.getAddress())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(!user.getType().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业用户无法进行该业务查询");
        }
        Company company = companyMapper.selectById(user.getCompanyId());
        if(Objects.isNull(company)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到企业信息");
        }
        Taxes taxes = new Taxes();
        taxes.setCreator(user.getId());
        taxes.setCreateDate(new Date());
        taxes.setIsdeleted(Constants.ZERO);
        taxes.setStatus(Constants.ZERO);
        taxes.setCompanyId(company.getId());
        taxes.setPrice(entrustInvoicingDTO.getInvoicingMoney());
        taxes.setType(entrustInvoicingDTO.getType());
        taxes.setTaxCode(company.getTaxCode());
        taxes.setTaxAccount(company.getTaxAccount());
        taxes.setTaxAddr(company.getTaxAddr());
        taxes.setAddr(entrustInvoicingDTO.getAddress());
        taxes.setCompanyName(company.getName());
        taxes.setApplyType(Constants.ZERO);
        taxes.setTaxBank(company.getTaxBank());
        taxesMapper.insert(taxes);
 
        List<TaxDetial> taxDetialList = entrustInvoicingDTO.getTaxDetialList();
        for (TaxDetial taxDetial:taxDetialList) {
            if(Objects.isNull(taxDetial)
                    || Objects.isNull(taxDetial.getInsuranceApplyId())
                    || Objects.isNull(taxDetial.getFee())
            ){
                throw new BusinessException(ResponseStatus.BAD_REQUEST);
            }
            //查询每个保单下可以报销的金额
            InsuranceApply insuranceApply = insuranceApplyJoinMapper.selectJoinOne(InsuranceApply.class,new MPJLambdaWrapper<InsuranceApply>()
                    .selectAll(InsuranceApply.class)
//                    .select(" ifnull(( select sum(td.fee) from taxes ts inner join tax_detial td on td.TAX_ID = ts.id  where ts.status  = 1 and td.INSURANCE_APPLY_ID = t.id ),0) as taxesMoney")
                    .select(" ifnull(( select sum(td.fee) from taxes ts inner join tax_detial td on td.TAX_ID = ts.id  where ts.status != 2 and td.INSURANCE_APPLY_ID = t.id ),0) as taxesMoney ")
                    .eq(InsuranceApply::getId,taxDetial.getInsuranceApplyId())
            );
            if(Objects.isNull(insuranceApply)){
                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"保单号【"+taxDetial.getApplyCode()+"】未查询到保单信息");
            }
            if(insuranceApply.getCurrentFee().subtract(insuranceApply.getTaxesMoney()).compareTo(taxDetial.getFee())!=Constants.ZERO){
                //throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"保单号【"+taxDetial.getApplyCode()+"】可报销金额错误【"+insuranceApply.getCurrentFee().subtract(insuranceApply.getTaxesMoney())+"】");
                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"保单号【"+taxDetial.getApplyCode()+"】不满足开票条件");
            }
            taxDetial.setCreator(user.getId());
            taxDetial.setCreateDate(new Date());
            taxDetial.setIsdeleted(Constants.ZERO);
            taxDetial.setTaxId(taxes.getId());
            taxDetial.setTotalFee(insuranceApply.getCurrentFee());
            taxDetial.setType(Constants.ZERO);
        }
        taxDetailJoinMapper.insertBatchSomeColumn(taxDetialList);
 
 
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.TAXES;
        //删除其他待办
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,taxes.getId()));
        Notices notices = new Notices(noticeObjectType,Constants.ZERO,taxes.getId(),taxes.getPrice().toString(),
                taxes.getCompanyId(), Constants.NoticeType.ZERO);
        noticesMapper.insert(notices);
 
 
    }
 
 
 
 
    @Override
    public List<TaxesInvoicingVO> getDirectTaxes(Integer id){
        InsuranceApply queryInsuranceApply = insuranceApplyJoinMapper.selectById(id);
        if(Objects.isNull(queryInsuranceApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        Solutions solutions = solutionsMapper.selectById(queryInsuranceApply.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到方案信息");
        }
        if(!solutions.getType().equals(Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非直保方案保单,无法进行该操作");
        }
        List<TaxesInvoicingVO>  taxesInvoicingVOList = new ArrayList<>();
        this.dealTaxesData(id,taxesInvoicingVOList);
        List<Taxes> taxesList = taxesJoinMapper.selectJoinList(Taxes.class,new MPJLambdaWrapper<Taxes>()
                .selectAll(Taxes.class)
                .selectAs(Solutions::getName,Taxes::getSolutionName)
                .selectAs(InsuranceApply::getCode,Taxes::getInsuranceCode)
                .leftJoin(Solutions.class,Solutions::getId,Taxes::getSolutionId)
                .leftJoin(InsuranceApply.class,InsuranceApply::getId,Taxes::getInsuranceApplyId)
                .eq(Taxes::getStatus,Constants.ONE)
                .eq(Taxes::getInsuranceApplyId,id)
        );
        if(CollectionUtils.isNotEmpty(taxesList)){
            for (Taxes taxes:taxesList) {
                TaxesInvoicingVO taxesInvoicingVO = taxes.getTaxesInvoicingVO();
                taxesInvoicingVOList.add(taxesInvoicingVO);
            }
        }
        return taxesInvoicingVOList;
 
    }
 
 
    public void dealTaxesData(Integer id,List<TaxesInvoicingVO>  taxesInvoicingVOList){
        InsuranceApply insuranceApply =  insuranceApplyJoinMapper.selectJoinOne(InsuranceApply.class,
                new MPJLambdaWrapper<InsuranceApply>()
                        .selectAll(InsuranceApply.class)
                        .selectAs(Solutions::getName,InsuranceApply::getSolutionsName)
                        .select(" ( select count(1) from apply_detail ad  where ad.apply_id = t.id ) as insureNum")
                        .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId)
                        .notExists(" select 1 from taxes t1 inner join tax_detial t2 on t1.id  = t2.tax_id where t2.type = 0 and t1.status != 2 and t.id = t2.INSURANCE_APPLY_ID  ")
                        .eq(InsuranceApply::getStatus,Constants.InsuranceApplyStatus.UPLOAD_INSURANCE.getKey())
                        .eq(InsuranceApply::getId,id)
        );
 
 
        List<ApplyChange> applyChangeList = applyChangeJoinMapper.selectJoinList(ApplyChange.class,new MPJLambdaWrapper<ApplyChange>()
                .selectAll(ApplyChange.class)
                .selectAs(InsuranceApply::getCode,ApplyChange::getApplyCode)
                .selectAs(Solutions::getName,ApplyChange::getSolutionsName)
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 0  ) as addNum ")
                .select("( select count(1) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID and ad.TYPE = 1  ) as delNum ")
                .select("( select ifnull(sum(ad.FEE),0) from apply_chagne_detail ad where t.id = ad.APPLY_CHANGE_ID  ) as changeMoney" )
                .notExists(" select 1 from taxes t1 inner join tax_detial t2 on t1.id  = t2.tax_id where t2.type = 1 and t1.status != 2 and t.id = t2.APPLY_CHANGE_ID  ")
                .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyChange::getApplyId)
                .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId)
                .eq(ApplyChange::getStatus,Constants.ApplyChangeStatus.APPROVE.getKey())
                .eq(ApplyChange::getType,Constants.ZERO)
                .eq(InsuranceApply::getId,id)
        );
 
        if(CollectionUtils.isNotEmpty(applyChangeList)){
            for (ApplyChange applyChange:applyChangeList) {
                if(!Objects.isNull(insuranceApply)){
                    insuranceApply.setFee(insuranceApply.getFee().subtract(applyChange.getFee()));
                }
                TaxesInvoicingVO taxesInvoicingVO = applyChange.toTaxesInvoicingVO();
//                if(!Objects.isNull(insuranceApply)){
//                    taxesInvoicingVO.setAmount(BigDecimal.ZERO);
//                }
                taxesInvoicingVOList.add(taxesInvoicingVO);
            }
        }
        if(!Objects.isNull(insuranceApply)){
            TaxesInvoicingVO taxesInvoicingVO = insuranceApply.toTaxesInvoicingVO();
            taxesInvoicingVOList.add(taxesInvoicingVO);
        }
    }
 
 
    /**
     * 直保数据提交
     * @param directInvoicingDTO
     */
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public void directInvoicing(DirectInvoicingDTO directInvoicingDTO){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Objects.isNull(directInvoicingDTO)
                || Objects.isNull(directInvoicingDTO.getInvoicingMoney())
                || Objects.isNull(directInvoicingDTO.getType())
                || Objects.isNull(directInvoicingDTO.getInvoicingDetailDTOList())
                || StringUtils.isBlank(directInvoicingDTO.getAddress())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(!user.getType().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业用户无法进行该业务查询");
        }
        Company company = companyMapper.selectById(user.getCompanyId());
        if(Objects.isNull(company)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到企业信息");
        }
        InsuranceApply queryInsuranceApply = insuranceApplyJoinMapper.selectById(directInvoicingDTO.getId());
        if(Objects.isNull(queryInsuranceApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        Solutions solutions = solutionsMapper.selectById(queryInsuranceApply.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到方案信息");
        }
        if(!solutions.getType().equals(Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非直保方案保单,无法进行该操作");
        }
 
        Taxes taxes = new Taxes();
        taxes.setCreator(user.getId());
        taxes.setCreateDate(new Date());
        taxes.setIsdeleted(Constants.ZERO);
        taxes.setStatus(Constants.ZERO);
        taxes.setCompanyId(company.getId());
        taxes.setPrice(directInvoicingDTO.getInvoicingMoney());
        taxes.setType(directInvoicingDTO.getType());
        taxes.setTaxCode(company.getTaxCode());
        taxes.setTaxAccount(company.getTaxAccount());
        taxes.setTaxBank(company.getTaxBank());
        taxes.setTaxAddr(company.getTaxAddr());
        taxes.setAddr(directInvoicingDTO.getAddress());
        taxes.setCompanyName(company.getName());
        taxes.setApplyType(Constants.ONE);
        taxes.setSolutionId(solutions.getId());
        taxes.setInsuranceApplyId(queryInsuranceApply.getId());
        taxesMapper.insert(taxes);
 
        List<TaxesInvoicingVO>  taxesInvoicingVOList = new ArrayList<>();
        this.dealTaxesData(queryInsuranceApply.getId(),taxesInvoicingVOList);
        List<TaxDetial> taxDetailList = new ArrayList<>();
        for (TaxesInvoicingVO taxesInvoicingVO:taxesInvoicingVOList) {
            if(Objects.isNull(taxesInvoicingVO)
                    || Objects.isNull(taxesInvoicingVO.getAmount())
                    || Objects.isNull(taxesInvoicingVO.getId())
                    || Objects.isNull(taxesInvoicingVO.getType())
            ){
                throw new BusinessException(ResponseStatus.BAD_REQUEST);
            }
            TaxDetial taxDetial = new TaxDetial();
            taxDetial.setCreator(user.getId());
            taxDetial.setCreateDate(new Date());
            taxDetial.setIsdeleted(Constants.ZERO);
            taxDetial.setTaxId(taxes.getId());
            taxDetial.setTotalFee(taxesInvoicingVO.getAmount());
            taxDetial.setFee(taxesInvoicingVO.getAmount());
            taxDetial.setInsuranceApplyId(taxes.getInsuranceApplyId());
            if(taxesInvoicingVO.getType().equals(Constants.ZERO)){
                taxDetial.setType(taxesInvoicingVO.getType());
            }else if(taxesInvoicingVO.getType().equals(Constants.ONE)){
                taxDetial.setApplyChangeId(taxesInvoicingVO.getId());
                taxDetial.setType(taxesInvoicingVO.getType());
            }
            taxDetailList.add(taxDetial);
        }
        List<Integer> chIdList = directInvoicingDTO.getInvoicingDetailDTOList();
        if(CollectionUtils.isNotEmpty(chIdList)){
            for (Integer id:chIdList) {
                Taxes chTaxes = taxesMapper.selectById(id);
                if(Objects.isNull(chTaxes)){
                    throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到选择冲红数据");
                }
                if(!(chTaxes.getStatus().equals(Constants.ZERO)||chTaxes.getStatus().equals(Constants.ONE))){
                    throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"冲红数据状态错误");
                }
                TaxDetial taxDetial = new TaxDetial();
                taxDetial.setCreator(user.getId());
                taxDetial.setCreateDate(new Date());
                taxDetial.setIsdeleted(Constants.ZERO);
                taxDetial.setTaxId(taxes.getId());
                taxDetial.setTotalFee(chTaxes.getPrice());
                taxDetial.setFee(chTaxes.getPrice());
                taxDetial.setDelTaxId(chTaxes.getId());
                taxDetial.setInsuranceApplyId(taxes.getInsuranceApplyId());
                taxDetial.setType(Constants.TWO);
                taxDetailList.add(taxDetial);
            }
        }
        if(CollectionUtils.isNotEmpty(taxDetailList)){
            taxDetailJoinMapper.insertBatchSomeColumn(taxDetailList);
        }else{
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"无可开票数据,操作失败");
        }
 
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.TAXES;
        //删除其他待办
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,taxes.getId()));
        Notices notices = new Notices(noticeObjectType,Constants.ZERO,taxes.getId(),taxes.getPrice().toString(),
                taxes.getCompanyId(), Constants.NoticeType.ZERO);
        noticesMapper.insert(notices);
 
    }
 
 
}