fix
Helius
2021-07-16 f50cfbe6eb4be4a36cda1f02a494cdcfb4259659
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
package com.xzx.gc.order.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.OrderEnum;
import com.xzx.gc.common.constant.PayEnum;
import com.xzx.gc.common.dto.SimplePage;
import com.xzx.gc.common.exception.BusinessException;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.*;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.admin.PayRequestInfoModel;
import com.xzx.gc.model.admin.PayStorageModel;
import com.xzx.gc.model.admin.StorageUserModel;
import com.xzx.gc.model.comon.account.AllAcountParamDto;
import com.xzx.gc.model.order.OrderInfoReq;
import com.xzx.gc.model.order.RkStatisticsStorageReceiverDto;
import com.xzx.gc.model.user.AccountVo;
import com.xzx.gc.order.dto.*;
import com.xzx.gc.order.mapper.*;
import com.xzx.gc.service.AccountContext;
import com.xzx.gc.util.DoubleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@Transactional
@Slf4j
public class StorageService {
 
 
    @Autowired
    private PayInfoMapper payInfoMapper;
 
    @Autowired
    private IdUtils idUtils;
 
    @Autowired
    private  OrderItemInfoMapper orderItemInfoMapper;
 
    @Autowired
    private OrderBatchInfoMapper orderBatchInfoMapper;
 
    @Autowired
    private OrderBatchMapper orderBatchMapper;
 
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private AddressMapper addressMapper;
 
    @Autowired
    private OrderStorageDetailMapper orderStorageDetailMapper;
 
 
    @Autowired
    private SysStorageService sysStorageService;
 
    @Autowired
    private OrderDetailService orderDetailService;
 
    @Autowired
    private OrderClockInMapper orderClockInMapper;
 
    @Autowired
    private OrderStorageInfoMapper orderStorageInfoMapper;
 
    @Autowired
    private OrderDetailMapper orderDetailMapper;
 
    @Autowired
    private  SysStorageMapper sysStorageMapper;
 
    @Autowired
    private OrderBatchInfoService orderBatchInfoService;
 
    @Autowired
    private OrderMapper orderMapper;
 
    @Autowired
    private PackageGoodsInfoService packageGoodsInfoService;
 
    @Autowired
    private PromotionRebateService promotionRebateService;
 
    @Autowired
    private OtherUserMapper otherUserMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private BusinessUtil businessUtil;
 
    @Autowired
    private OtherUserService otherUserService;
 
 
    @Autowired
    private SysEnvironmentalInfoService sysEnvironmentalInfoService;
 
    @Autowired
    private WeightItemPriceService weightItemPriceService;
 
    @Autowired
    private CityPartnerService cityPartnerService;
 
 
    @Autowired
    private PartnerGaodeService partnerGaodeService;
 
    @Autowired
    private UserVehicleInfoService userVehicleInfoService;
    @Autowired
    private OrderHomeApplianceService orderHomeApplianceService;
 
    /**
     * 入库详情
     * @param storageId
     * @return
     */
    public StorageDetailBatchDto detail(String storageId){
 
        StorageDetailBatchDto storageDetailBatchDto=new StorageDetailBatchDto();
        //物品清单
        OrderStorageDetailInfo orderStorageDetailInfo=new OrderStorageDetailInfo();
        orderStorageDetailInfo.setStorageId(storageId);
        List<OrderStorageDetailInfo> select = orderStorageDetailMapper.select(orderStorageDetailInfo);
 
        Example example=new Example(OrderStorageInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storageId",storageId);
        OrderStorageInfo orderStorageInfo = orderStorageInfoMapper.selectOneByExample(example);
        Long sysStorageId = orderStorageInfo.getSysStorageId();
        Short sysStorageType = orderStorageInfo.getSysStorageType();
        if(CollUtil.isNotEmpty(select)){
 
 
            Map<String,Dict> map=new HashMap<>();
 
            for (OrderStorageDetailInfo storageDetailInfo : select) {
                String flag = storageDetailInfo.getFlag();
                BigDecimal weight = storageDetailInfo.getWeight();
                String amount = storageDetailInfo.getAmount();
                String money = storageDetailInfo.getMoney();
                String itemType = storageDetailInfo.getItemType();
                String price=storageDetailInfo.getPrice();
 
                if(OrderEnum.入库型.getValue().equals(flag)&&weight!=null&&weight.compareTo(BigDecimal.ZERO)!=0){
 
                    if(map.containsKey(itemType)){
                        Dict dict = map.get(itemType);
                        BigDecimal storageWeight1 = dict.getBigDecimal("storageWeight");
                        dict.set("storageWeight",NumberUtil.add(storageWeight1,weight));
                        BigDecimal storageMoney1 = dict.getBigDecimal("storageMoney");
                        dict.set("storageMoney",NumberUtil.add(storageMoney1,Convert.toBigDecimal(money)));
                        map.put(itemType,dict);
                    }else{
                        Dict dict=Dict.create();
                        dict.set("storageWeight",weight);
                        dict.set("storageMoney",new BigDecimal(money));
                        dict.set("price",price);
                        map.put(itemType,dict);
                    }
                }
 
                if(OrderEnum.入库型.getValue().equals(flag)&&amount!=null&&Convert.toBigDecimal(amount,BigDecimal.ZERO).compareTo(BigDecimal.ZERO)!=0){
 
                    if(map.containsKey(itemType)){
                        Dict dict = map.get(itemType);
                        BigDecimal storageAmount1 = dict.getBigDecimal("storageAmount");
                        dict.set("storageAmount",NumberUtil.add(storageAmount1,Convert.toBigDecimal(amount)));
                        BigDecimal storageMoney1 = dict.getBigDecimal("storageMoney");
                        dict.set("storageMoney",NumberUtil.add(storageMoney1,Convert.toBigDecimal(money)));
                        map.put(itemType,dict);
                    }else{
                        Dict dict=Dict.create();
                        dict.set("storageAmount",Convert.toBigDecimal(amount));
                        dict.set("storageMoney",new BigDecimal(money));
                        dict.set("price",price);
                        map.put(itemType,dict);
                    }
                }
 
 
                if(OrderEnum.回收型.getValue().equals(flag)&&weight!=null&&weight.compareTo(BigDecimal.ZERO)!=0){
 
                    if(map.containsKey(itemType)){
                        Dict dict = map.get(itemType);
                        BigDecimal recyleweight1 = dict.getBigDecimal("recyleweight");
                        dict.set("recyleweight",NumberUtil.add(recyleweight1,weight));
                        BigDecimal recyleMoney1 = dict.getBigDecimal("recyleMoney");
                        dict.set("recyleMoney",NumberUtil.add(recyleMoney1,Convert.toBigDecimal(money)));
                        map.put(itemType,dict);
                    }else{
                        Dict dict=Dict.create();
                        dict.set("recyleweight",weight);
                        dict.set("recyleMoney",new BigDecimal(money));
                        dict.set("price",price);
                        map.put(itemType,dict);
                    }
                }
 
                if(OrderEnum.回收型.getValue().equals(flag)&&amount!=null&&Convert.toBigDecimal(amount,BigDecimal.ZERO).compareTo(BigDecimal.ZERO)!=0){
 
                    if(map.containsKey(itemType)){
                        Dict dict = map.get(itemType);
                        BigDecimal recyleAmount1 = dict.getBigDecimal("recyleAmount");
                        dict.set("recyleAmount",NumberUtil.add(recyleAmount1,Convert.toBigDecimal(amount)));
                        BigDecimal recyleMoney1 = dict.getBigDecimal("recyleMoney");
                        dict.set("recyleMoney",NumberUtil.add(recyleMoney1,Convert.toBigDecimal(money)));
                        map.put(itemType,dict);
                    }else{
                        Dict dict=Dict.create();
                        dict.set("recyleAmount",Convert.toBigDecimal(amount));
                        dict.set("recyleMoney",new BigDecimal(money));
                        dict.set("price",price);
                        map.put(itemType,dict);
                    }
                }
            }
 
 
            if(MapUtil.isNotEmpty(map)){
                List<OrderStorageDetailInfo> list=new ArrayList<>();
 
 
                for (String s : map.keySet()) {
                    OrderStorageDetailInfo orderStorageDetailInfo1=new OrderStorageDetailInfo();
 
                    //选取仓库价格
                    SysEnvironmentalInfo sysEnvironmentalInfo1=new SysEnvironmentalInfo();
                    if(Convert.toShort(OrderEnum.打包站.getValue()).equals(sysStorageType)){
                        PackageGoodsInfo byPackageId = packageGoodsInfoService.findByPackageIdAndItemType(sysStorageId.toString(),s);
                        BeanUtil.copyProperties(byPackageId,sysEnvironmentalInfo1);
                    }else if(Convert.toShort(OrderEnum.仓库.getValue()).equals(sysStorageType)){
                        SysStorage byId1 = sysStorageService.findById(Convert.toInt(sysStorageId));
                        Integer areaId = Convert.toInt(byId1.getArea());
                        sysEnvironmentalInfo1 = sysEnvironmentalInfoService.findByItemTypeAndArea(s, areaId);
                    }
 
                    Dict dict = map.get(s);
                    String price = dict.getStr("price");
                    //设置分类价格等
                    orderStorageDetailInfo1.setItemTypeName(sysEnvironmentalInfo1.getTitle());
                    if(StrUtil.isNotBlank(price)){
                        orderStorageDetailInfo1.setItemTypePrice(price);
                    }else {
                        orderStorageDetailInfo1.setItemTypePrice(sysEnvironmentalInfo1.getPrice());
                    }
                    orderStorageDetailInfo1.setItemTypePicture(sysEnvironmentalInfo1.getPicture());
                    //上门价格
                    orderStorageDetailInfo1.setItemTypePrice2(sysEnvironmentalInfo1.getSecondPrice());
 
                    BigDecimal recyleweight = dict.getBigDecimal("recyleweight");
                    BigDecimal recyleMoney = dict.getBigDecimal("recyleMoney");
                    BigDecimal recyleAmount = dict.getBigDecimal("recyleAmount");
                    BigDecimal storageWeight = dict.getBigDecimal("storageWeight");
                    BigDecimal storageMoney = dict.getBigDecimal("storageMoney");
                    BigDecimal storageAmount= dict.getBigDecimal("storageAmount");
 
                    if(recyleweight!=null&&recyleweight.compareTo(BigDecimal.ZERO)!=0){
                        orderStorageDetailInfo1.setWeight(recyleweight);
                    }
 
                    if(recyleMoney!=null&&recyleMoney.compareTo(BigDecimal.ZERO)!=0){
                        orderStorageDetailInfo1.setMoney(recyleMoney.toString());
                    }
 
                    if(recyleAmount!=null&&recyleAmount.compareTo(BigDecimal.ZERO)!=0){
                        orderStorageDetailInfo1.setAmount(Convert.toStr(recyleAmount));
                    }
 
                    if(storageWeight!=null&&storageWeight.compareTo(BigDecimal.ZERO)!=0){
                        orderStorageDetailInfo1.setStorageWeight(storageWeight);
                    }
 
                    if(storageMoney!=null&&storageMoney.compareTo(BigDecimal.ZERO)!=0){
                        orderStorageDetailInfo1.setStorageMoney(storageMoney.toString());
                    }
 
                    if(storageAmount!=null&&storageAmount.compareTo(BigDecimal.ZERO)!=0){
                        orderStorageDetailInfo1.setStorageAmount(storageAmount);
                    }
 
                    list.add(orderStorageDetailInfo1);
                }
 
                storageDetailBatchDto.setList(list);
            }
        }
 
        //获取地称的
        OrderBatchInfo batchInfo=new OrderBatchInfo();
      
        String orderId = orderStorageInfo.getOrderId();
        Example example1=new Example(OrderBatchInfo.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("orderId",orderId);
        criteria1.andEqualTo("status",OrderEnum.已入库.getValue());
        OrderBatchInfo orderBatchInfo = orderBatchInfoMapper.selectOneByExample(example1);
 
        if(orderBatchInfo!=null){
            String weight = orderBatchInfo.getWeight();
            batchInfo.setWeight(weight);
 
            OrderClockIn today = orderClockInMapper.findToday(orderStorageInfo.getReceiver(), StrUtil.split(orderBatchInfo.getCreateTime(),"")[0]);
            if(today!=null){
                batchInfo.setEmptyWeight(today.getWeight());
            }
        }
 
        //获取仓库名称
        if(Convert.toShort(OrderEnum.打包站.getValue()).equals(sysStorageType)){
            CityPartner sysStorage = cityPartnerService.findById(Convert.toInt(sysStorageId));
            if(sysStorage!=null){
                orderStorageInfo.setSysStorageName(sysStorage.getPartnerName());
            }
        }else if(Convert.toShort(OrderEnum.仓库.getValue()).equals(sysStorageType)){
            SysStorage sysStorage = sysStorageMapper.selectByPrimaryKey(sysStorageId);
            if(sysStorage!=null){
                orderStorageInfo.setSysStorageName(sysStorage.getStorageName());
            }
        }
 
        storageDetailBatchDto.setOrderStorageInfo(orderStorageInfo);
 
        //设置地称信息
        if(StrUtil.isBlank(batchInfo.getWeight())) {
            batchInfo.setWeight(Convert.toStr(Constants.WEIGHT_INIT));
        }
        if(StrUtil.isBlank(batchInfo.getEmptyWeight())) {
            batchInfo.setEmptyWeight(Convert.toStr(Constants.WEIGHT_INIT));
        }
        storageDetailBatchDto.setOrderBatchInfo(batchInfo);
        return storageDetailBatchDto;
    }
 
 
    /**
     * 查看回收订单
     * @param storageId
     * @return
     */
    public List<OrderDetailInfo> recyledDetail(String storageId) {
 
        OrderStorageInfo orderStorageInfo = orderStorageInfoMapper.selectByPrimaryKey(storageId);
 
        if(orderStorageInfo==null){
            throw new RestException(-1,"入库订单不存在");
        }
 
        String orderId = orderStorageInfo.getOrderId();
 
 
        String[] split = orderId.split(",");
 
 
        List<OrderDetailInfo> list=new ArrayList<>();
        for (String s : split) {
            OrderDetailInfo orderDetailInfo = orderDetailMapper.selectByPrimaryKey(s);
            //地址
            Long addressId = orderDetailInfo.getAddressId();
            AddressInfo addressInfo = addressMapper.selectByPrimaryKey(addressId);
            orderDetailInfo.setAddress(StrUtil.nullToEmpty(addressInfo.getAddressArea())+addressInfo.getDetailAddress());
 
            //重新赋值回收员真实名称  源于小程序这边存入的是昵称
            String receiverPhone = orderDetailInfo.getReceiverPhone();
            if(StrUtil.isNotBlank(receiverPhone)) {
                OtherUserInfo otherUserInfo1 = otherUserService.findByMobileAndUserType(receiverPhone,CommonEnum.回收员.getValue());
                if(otherUserInfo1!=null) {
                    orderDetailInfo.setReceiverName(otherUserInfo1.getName());
                }
            }
 
            //回收的物品
            OrderItemInfo orderItemInfo=new OrderItemInfo();
            orderItemInfo.setOrderId(s);
            List<OrderItemInfo> orderItemInfos = orderItemInfoMapper.select(orderItemInfo);
 
            List<String> itemTypeNames=new ArrayList<>();
            if(CollUtil.isNotEmpty(orderItemInfos)){
                List<OrderItemInfo> collect = orderItemInfos.stream().filter(x -> (StrUtil.isNotBlank(x.getWeight())&&Convert.toBigDecimal(x.getWeight()).compareTo(BigDecimal.ZERO)!=0)||(StrUtil.isNotBlank(x.getAmount())&&Convert.toBigDecimal(x.getAmount()).compareTo(BigDecimal.ZERO)!=0)).collect(Collectors.toList());
 
                if(CollUtil.isNotEmpty(collect)){
 
                    for (OrderItemInfo itemInfo : collect) {
                        String itemType = itemInfo.getItemType();
                        SysEnvironmentalInfo sysEnvironmentalInfo1 =  sysEnvironmentalInfoService.findByItemTypeAndArea(itemType,Convert.toInt(Constants.TIANXIN_CODE));
                        if(sysEnvironmentalInfo1!=null) {
                            itemTypeNames.add(sysEnvironmentalInfo1.getTitle());
                        }
                    }
 
 
                    orderDetailInfo.setItemTypeName(CollUtil.join(itemTypeNames,"、"));
                }
            }
 
 
            list.add(orderDetailInfo);
        }
 
 
        return list;
 
 
    }
 
 
    /**
     * 统计
     * @param userId
     * @param userType
     * @return
     */
    public  RkStatisticsDto statistics(String userId,String userType,String data,String receiver){
        RkStatisticsDto rkStatisticsDto=new RkStatisticsDto();
        RkStatisticsStorageDto rkStatisticsStorageDto=new RkStatisticsStorageDto();
        RkStatisticsRecyleDto rkStatisticsRecyleDto=new RkStatisticsRecyleDto();
        Example example=new Example(OrderStorageInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storageStatus",OrderEnum.已入库.getValue());
        if(CommonEnum.回收员.getValue().equals(userType)){
            criteria.andEqualTo("receiver",userId);
        }else if(CommonEnum.入库员.getValue().equals(userType)||CommonEnum.打包员.getValue().equals(userType)){
            criteria.andEqualTo("storageUserId",userId);
        }else if(CommonEnum.打包站运营员.getValue().equals(userType)){
            criteria.andEqualTo("sysStorageType",OrderEnum.打包站.getValue());
            String partnerId = otherUserService.findById(userId).getPartnerId();
            criteria.andEqualTo("sysStorageId",partnerId);
        }else{
            throw new RestException(-1,"用户角色不正确");
        }
 
 
        /**********入库员*************/
        example.setOrderByClause("storage_time desc");
        List<OrderStorageInfo> orderStorageInfos = orderStorageInfoMapper.selectByExample(example);
        List<OrderStorageInfo> orderStorageInfosToday=new ArrayList<>();
        //按时间筛选
        List<OrderStorageInfo> orderStorageInfosTime=new ArrayList<>();
        if(CollUtil.isNotEmpty(orderStorageInfos)){
            rkStatisticsStorageDto.setNumSum(orderStorageInfos.size());
            BigDecimal weight=Constants.WEIGHT_INIT;
            BigDecimal money=Constants.MONEY_INIT;
            for (OrderStorageInfo orderStorageInfo : orderStorageInfos) {
                if(StrUtil.isNotBlank(orderStorageInfo.getStorageTime())&&DateUtil.format(new Date(),DateUtils.DATE_FORMAT_YMD).equals(orderStorageInfo.getStorageTime().split(" ")[0])){
                    orderStorageInfosToday.add(orderStorageInfo);
                }
                if(StrUtil.isNotBlank(data)&&StrUtil.isNotBlank(orderStorageInfo.getStorageTime())&&DateUtil.formatDate(DateUtil.parseDate(data)).equals(orderStorageInfo.getStorageTime().split(" ")[0])){
                    orderStorageInfosTime.add(orderStorageInfo);
                }
                weight=NumberUtil.add(weight,Convert.toBigDecimal(orderStorageInfo.getStorageWeight()));
                money=NumberUtil.add(money,Convert.toBigDecimal(orderStorageInfo.getStorageMoney()));
            }
            rkStatisticsStorageDto.setStorageMoneySum(money);
            rkStatisticsStorageDto.setStorageWeightSum(weight);
 
            //当天入库
            if(CollUtil.isNotEmpty(orderStorageInfosToday)){
                rkStatisticsStorageDto.setNum(orderStorageInfosToday.size());
                BigDecimal weightToday=Constants.WEIGHT_INIT;
                BigDecimal moneyToday=Constants.MONEY_INIT;
                for (OrderStorageInfo orderStorageInfo : orderStorageInfosToday) {
                    weightToday=NumberUtil.add(weightToday,Convert.toBigDecimal(orderStorageInfo.getStorageWeight()));
                    moneyToday=NumberUtil.add(moneyToday,Convert.toBigDecimal(orderStorageInfo.getStorageMoney()));
                }
                rkStatisticsStorageDto.setStorageMoney(moneyToday);
                rkStatisticsStorageDto.setStorageWeight(weightToday);
            }
 
 
            //按时间入库
            if(CollUtil.isNotEmpty(orderStorageInfosTime)){
 
                List<RkStatisticsStorageReceiverDto> receiverList=new ArrayList<>();
                if(StrUtil.isNotBlank(receiver)){
                    BigDecimal weightToday=Constants.WEIGHT_INIT;
                    BigDecimal moneyToday=Constants.MONEY_INIT;
                    //根据回收员筛选
                    List<OrderStorageInfo> collect = orderStorageInfosTime.stream().filter(x -> receiver.equals(x.getReceiver())).collect(Collectors.toList());
                    for (OrderStorageInfo orderStorageInfo : collect) {
                        weightToday=NumberUtil.add(weightToday,Convert.toBigDecimal(orderStorageInfo.getStorageWeight()));
                        moneyToday=NumberUtil.add(moneyToday,Convert.toBigDecimal(orderStorageInfo.getStorageMoney()));
                    }
                    RkStatisticsStorageReceiverDto dto=new RkStatisticsStorageReceiverDto();
                    OtherUserInfo byId = otherUserService.findById(receiver);
                    dto.setUserId(byId.getUserId());
                    dto.setAvatar(byId.getAvatar());
                    dto.setName(byId.getName());
                    dto.setMoney(businessUtil.changeMoney(moneyToday));
                    dto.setWeight(businessUtil.changeWeight(weightToday.toString()));
                    dto.setTime(data);
                    receiverList.add(dto);
                }else {
                    //按回收员分组
                    Map<String, List<OrderStorageInfo>> collect = orderStorageInfosTime.stream().collect(Collectors.groupingBy(x -> x.getReceiver()));
                    for (String s : collect.keySet()) {
                        BigDecimal weightToday=Constants.WEIGHT_INIT;
                        BigDecimal moneyToday=Constants.MONEY_INIT;
                        RkStatisticsStorageReceiverDto dto=new RkStatisticsStorageReceiverDto();
                        OtherUserInfo byId = otherUserService.findById(s);
                        dto.setUserId(byId.getUserId());
                        dto.setAvatar(byId.getAvatar());
                        dto.setName(byId.getName());
                        for (OrderStorageInfo orderStorageInfo : collect.get(s)) {
                            weightToday=NumberUtil.add(weightToday,Convert.toBigDecimal(orderStorageInfo.getStorageWeight()));
                            moneyToday=NumberUtil.add(moneyToday,Convert.toBigDecimal(orderStorageInfo.getStorageMoney()));
                        }
                        dto.setMoney(businessUtil.changeMoney(moneyToday));
                        dto.setWeight(businessUtil.changeWeight(weightToday.toString()));
                        dto.setTime(data);
                        receiverList.add(dto);
                    }
                }
                rkStatisticsStorageDto.setReceiverList(receiverList);
            }
 
 
            //回收员赋值入库信息
            if(CommonEnum.回收员.getValue().equals(userType)) {
                BeanUtils.copyProperties(rkStatisticsStorageDto, rkStatisticsRecyleDto);
                rkStatisticsRecyleDto.setNum(0);
                rkStatisticsRecyleDto.setNumSum(0);
            }
        }
 
        /**********回收员*************/
        if(CommonEnum.回收员.getValue().equals(userType)){
            List<OrderDetailInfo> orderRecyleInfosToday=new ArrayList<>();
            List<OrderDetailInfo> orderInfos = orderDetailService.findByReceiver(userId);
            if(CollUtil.isNotEmpty(orderInfos)){
                rkStatisticsRecyleDto.setNumSum(orderInfos.size());
                BigDecimal weight=Constants.WEIGHT_INIT;
                BigDecimal money=Constants.MONEY_INIT;
                for (OrderDetailInfo orderInfo : orderInfos) {
                    if(StrUtil.isNotBlank(orderInfo.getCompleteTime())&&DateUtil.format(new Date(),DateUtils.DATE_FORMAT_YMD).equals(orderInfo.getCompleteTime().split(" ")[0])){
                        orderRecyleInfosToday.add(orderInfo);
                    }
                    weight=NumberUtil.add(weight,Convert.toBigDecimal(orderInfo.getWeight()));
                    money=NumberUtil.add(money,Convert.toBigDecimal(orderInfo.getMoney()));
                }
                rkStatisticsRecyleDto.setRecycleMoneySum(money);
                rkStatisticsRecyleDto.setRecycleWeightSum(weight);
 
                if(CollUtil.isNotEmpty(orderRecyleInfosToday)){
                    rkStatisticsRecyleDto.setNum(orderRecyleInfosToday.size());
                    BigDecimal weightToday=Constants.WEIGHT_INIT;
                    BigDecimal moneyToday=Constants.MONEY_INIT;
                    for (OrderDetailInfo orderStorageInfo : orderRecyleInfosToday) {
                        weightToday=NumberUtil.add(weightToday,Convert.toBigDecimal(orderStorageInfo.getWeight()));
                        moneyToday=NumberUtil.add(moneyToday,Convert.toBigDecimal(orderStorageInfo.getMoney()));
                    }
                    rkStatisticsRecyleDto.setRecycleMoney(moneyToday);
                    rkStatisticsRecyleDto.setRecycleWeight(weightToday);
                }
            }
        }
 
        if(CommonEnum.回收员.getValue().equals(userType)){
            rkStatisticsDto.setRkStatisticsRecyleDto(rkStatisticsRecyleDto);
        }else{
            rkStatisticsDto.setStatisticsStorageDto(rkStatisticsStorageDto);
        }
        return  rkStatisticsDto;
    }
 
    /**
     * 入库
     * @param rk
     */
    public  void rk(RkDto rk){
        boolean lock=false;
        try {
            if(redisUtil.setnx(Constants.REDIS_ORDER_KEY+"rk:"+rk.getStorageId(),rk.getStorageId())) {
                lock=true;
 
 
                String now = DateUtil.now();
                OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(rk.getStorageUserId());
                //修改入库信息
                Example example = new Example(OrderStorageInfo.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("storageId", rk.getStorageId());
                OrderStorageInfo orderStorageInfo = orderStorageInfoMapper.selectOneByExample(example);
                if (orderStorageInfo == null) {
                    throw new RestException(-1, "入库单不存在");
                }
 
                Map map = new HashMap();
                map.put("userId", orderStorageInfo.getReceiver());
                List<AccountVo> receiverAccounts = accountMapper.queryMyMoney(map);
                if(CollUtil.isEmpty(receiverAccounts)){
                    throw new RestException(-1, "回收员账户已被禁用");
                }
 
                //检验是否已入库
                if (orderStorageInfo.getStorageStatus().toString().equals(OrderEnum.已入库.getValue())) {
                    throw new RestException(-1, "订单已入库");
                }
                orderStorageInfo.setStorageUserId(rk.getStorageUserId());
                orderStorageInfo.setStorageUserName(otherUserInfo.getName());
                orderStorageInfo.setStorageUserPhone(otherUserInfo.getMobilePhone());
                orderStorageInfo.setStorageTime(now);
                orderStorageInfo.setStorageWeight(rk.getStorageWeight());
                orderStorageInfo.setStorageMoney(Convert.toBigDecimal(rk.getStorageMoney()));
 
                int usertype=0;
                String packageId=null;
                if(otherUserInfo.getUserType().equals(CommonEnum.打包员.getValue())||otherUserInfo.getUserType().equals(CommonEnum.打包站运营员.getValue())) {
                    usertype = 2;
                    packageId = otherUserInfo.getPartnerId();
                }else if(otherUserInfo.getUserType().equals(CommonEnum.入库员.getValue())){
                    usertype=1;
                }
                Map map2 = new HashMap();
                map2.put("userId", rk.getStorageUserId());
                List<AccountVo> storageAccount = accountMapper.queryMyMoney(map2);
                if(CollUtil.isEmpty(storageAccount)){
                        throw new RestException(-1, "账户已被禁用");
                }
                AccountVo accountVo = storageAccount.get(0);
                String overdraftLimit = accountVo.getOverdraftLimit();
                BigDecimal limit = Convert.toBigDecimal(overdraftLimit, Constants.MONEY_INIT);
                BigDecimal moneyDecimal=Convert.toBigDecimal(rk.getStorageMoney());
                BigDecimal moneyDB = Convert.toBigDecimal(accountVo.getMoney(), Constants.MONEY_INIT);
                if(usertype==2) {
                    if (NumberUtil.isLess(NumberUtil.add(limit, moneyDB), moneyDecimal)) {
                        throw new RestException(-1, "额度不足");
                    }
                }else if(usertype==1){
                    //判断仓管员额度是否足够
                    if (NumberUtil.isGreater(moneyDecimal, limit)) {
                        throw new RestException(-1, "额度不足");
                    }
                }
 
                orderStorageInfo.setStorageStatus(Convert.toShort(OrderEnum.已入库.getValue()));
 
                if(usertype==1){
                    //仓库ID  地磅必须在入库才知道入库id
                    OtherUserInfo byId = otherUserService.findById(rk.getStorageUserId());
                    orderStorageInfo.setSysStorageId(Convert.toLong(byId.getStorageId()));
                }
 
                //查询废纸分类的单价  合伙人存在多个区域则按最低的单价算
                String paperBasic=null;
                String partnerId = otherUserService.findById(orderStorageInfo.getReceiver()).getPartnerId();
                List<PartnerGaode> byPartnerId = partnerGaodeService.findByPartnerId(partnerId);
                List<String> areaIds=byPartnerId.stream().map(x->x.getTownId()).collect(Collectors.toList());
                if(usertype==2) {
                    paperBasic = sysEnvironmentalInfoService.findByMinItemPriceTypeAndAreaIds(areaIds);
                    if (paperBasic==null) {
                        throw new RestException(-1,"请设置纸类的基准价格");
                    }
                }
 
 
                orderStorageInfoMapper.updateByPrimaryKeySelective(orderStorageInfo);
                OrderBatchInfo orderBatchInfo = new OrderBatchInfo();
                orderBatchInfo.setStatus(Convert.toShort(OrderEnum.已入库.getValue()));
                Example example1 = new Example(OrderBatchInfo.class);
                example1.createCriteria().andEqualTo("orderId", orderStorageInfo.getOrderId());
                //修改批次信息
                orderBatchInfoMapper.updateByExampleSelective(orderBatchInfo, example1);
                //修改物品分类信息
                List<WeightItemPrice> list=new ArrayList<>();
 
                BigDecimal paperWeight=BigDecimal.ZERO;
                //纸类的所有小类集合
                List<String> paperChildType=CollUtil.newArrayList();
                if(usertype==2) {
                    paperChildType = packageGoodsInfoService.findChildItemTypeByType(packageId, Constants.PAPER_ITEM_TYPE);
                }
                for (OrderItemDto orderItemReq : rk.getStorageItemList()) {
                    //修改入库的金额 和数量 重量
                    OrderStorageDetailInfo detailInfo = new OrderStorageDetailInfo();
                    detailInfo.setWeight(Convert.toBigDecimal(orderItemReq.getWeight(),Constants.WEIGHT_INIT));
                    detailInfo.setMoney(orderItemReq.getMoney());
                    detailInfo.setAmount(orderItemReq.getAmount());
                    detailInfo.setPrice(orderItemReq.getItemTypePrice());
 
                    Example example2 = new Example(OrderStorageDetailInfo.class);
                    Example.Criteria criteria2 = example2.createCriteria();
                    criteria2.andEqualTo("storageId", rk.getStorageId());
                    criteria2.andEqualTo("flag", OrderEnum.入库型.getValue());
                    criteria2.andEqualTo("itemType", orderItemReq.getItemType());
                    orderStorageDetailMapper.updateByExampleSelective(detailInfo, example2);
 
                    if(usertype==2) {
 
                        if(orderItemReq.getWeight().compareTo(BigDecimal.ZERO)>0){
                            //计算出所有纸类的重量
                            if(paperChildType.contains(orderItemReq.getItemType())){
                                paperWeight=NumberUtil.add(paperWeight,orderItemReq.getWeight());
                            }
                        }
 
                        //提成明细
                        if (detailInfo.getWeight().compareTo(BigDecimal.ZERO) > 0) {
                            WeightItemPrice weightItemPrice = new WeightItemPrice();
                            weightItemPrice.setStorageId(rk.getStorageId());
                            weightItemPrice.setStorageTime(now);
                            weightItemPrice.setPackageId(orderStorageInfo.getSysStorageId().toString());
                            weightItemPrice.setWeight(detailInfo.getWeight().toString());
                            weightItemPrice.setReceiver(orderStorageInfo.getReceiver());
                            weightItemPrice.setProductType(orderItemReq.getItemType());
                            weightItemPrice.setPackagePrice(orderItemReq.getItemTypePrice());
                            weightItemPrice.setGoodsPrice(paperBasic);
                            //1:未审核,2:已审核 3:异常
                            weightItemPrice.setStatus("1");
                            list.add(weightItemPrice);
                        }
                    }
 
                }
 
                //添加提成明细
                if(CollUtil.isNotEmpty(list)){
                    weightItemPriceService.addBatch(list);
                }
 
                String payType="";
                //修改订单相关
                int i=0;
 
                List<OrderInfo> listOrder=new ArrayList<>();
                for (String orderId : orderStorageInfo.getOrderId().split(",")) {
 
                    //修改订单信息
                    OrderInfo orderInfo = new OrderInfo();
                    orderInfo.setOrderId(orderId);
                    orderInfo.setOrderStatus(OrderEnum.完成.getValue());
                    listOrder.add(orderInfo);
//                    orderMapper.updateByPrimaryKeySelective(orderInfo);
 
                    if(i==0) {
                        PayInfo payInfo = new PayInfo();
                        payInfo.setOrderId(orderId);
                        payInfo.setCreateUserId(orderStorageInfo.getReceiver());
                        List<PayInfo> select = payInfoMapper.select(payInfo);
                        if (CollUtil.isNotEmpty(select)) {
                            String payType1 = select.get(0).getPayType();
                            if (PayEnum.现金支出.getValue().equals(payType1) || PayEnum.现金收入.getValue().equals(payType1)) {
                                payType = PayEnum.现金支出.getValue();
                            } else {
                                payType = PayEnum.环保金支出.getValue();
                            }
                        }
                    }
                    //修改订单详情信息   冗余字段暂时废弃
//                    OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
//                    orderDetailInfo.setStorageMoney(Convert.toStr(rk.getStorageMoney()));
//                    orderDetailInfo.setStorageUserId(orderStorageInfo.getStorageUserId());
//                    orderDetailInfo.setStorageUserName(orderStorageInfo.getStorageUserName());
//                    orderDetailInfo.setStorageUserPhone(orderStorageInfo.getStorageUserPhone());
//                    Example example2 = new Example(OrderDetailInfo.class);
//                    example2.createCriteria().andEqualTo("orderId", orderId);
//                    orderDetailMapper.updateByExampleSelective(orderDetailInfo, example2);
                    i++;
                }
 
                //批量修改订单状态
                if(CollUtil.isNotEmpty(listOrder)){
                    orderBatchMapper.updateBatchByPrimaryKeySelective(listOrder);
                }
 
                OrderInfoReq orderInfoReq = new OrderInfoReq();
                orderInfoReq.setNewVersion(true);
                orderInfoReq.setReceiver(orderStorageInfo.getReceiver());
                orderInfoReq.setStorageUserId(orderStorageInfo.getStorageUserId());
                orderInfoReq.setPayType(payType);
                orderInfoReq.setStorageMoney(Convert.toStr(rk.getStorageMoney()));
                orderInfoReq.setOrderId(orderStorageInfo.getOrderId());
                orderInfoReq.setStorageId(orderStorageInfo.getStorageId());
                orderInfoReq.setVersion(rk.getVersion());
                if(paperBasic!=null){
                    orderInfoReq.setPaperMoney(NumberUtil.mul(Convert.toBigDecimal(paperBasic),paperWeight));
                }
                //录入支付记录
                AccountVo receiverAccount=receiverAccounts.get(0);
//                orderService.storageOperInfo(orderInfoReq, orderStorageInfo.getStorageId(), now, accountId);
                //更改账户信息
                orderService.storageAccount(orderInfoReq, receiverAccount,accountVo,usertype,partnerId);
 
                //推广返利
                AllAcountParamDto allAcountParamDto=AllAcountParamDto.builder().
                        type(6).flowNo(orderStorageInfo.getOrderId()).userId(orderInfoReq.getStorageUserId())
                        .build();
//                accountFactory.getService(6).account(allAcountParamDto);
                new AccountContext(promotionRebateService).account(allAcountParamDto);
 
                //入库消息
                orderService.storageMessage(orderInfoReq, now);
 
                //删除回收员的入库错误次数
                redisUtil.hdel(Constants.REDIS_ORDER_KEY + "storage:errcount", orderStorageInfo.getReceiver());
            }
        } catch (RestException e){
            throw  new BusinessException(-1,e.getMsg(),ExceptionUtil.getMessage(e));
        }catch (Exception e) {
            ExceptionUtils.err("入库失败",e);
        }finally {
            if(lock) {
                redisUtil.del(Constants.REDIS_ORDER_KEY + "rk:" + rk.getStorageId());
            }
        }
 
 
    }
 
 
    /**
     * 已完成订单列表查询
     * @param simplePage
     * @param userId
     * @return
     */
    public   List<OrderStorageInfo>  findCompleteOrder(SimplePage simplePage,String userId){
        Example example=new Example(OrderStorageInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storageStatus",Convert.toShort(0));
        //rk代表入库员已完成列表查询接口
        if(StrUtil.isBlank(simplePage.getExtra())){
            criteria.andEqualTo("receiver",userId);
        }else{
            criteria.andEqualTo("storageUserId",userId);
        }
        example.setOrderByClause("storage_time desc");
        PageHelper.startPage(simplePage.getPageNo(),simplePage.getPageSize());
        List<OrderStorageInfo> select = orderStorageInfoMapper.selectByExample(example);
        //查看是否是定点下单 不可能混合 否则会拆分成2个入库单
        if(CollUtil.isNotEmpty(select)){
            for (OrderStorageInfo orderStorageInfo : select) {
//                String orderId = orderStorageInfo.getOrderId();
//                String s = orderId.split(",")[0];
//                OrderInfo orderInfo = orderMapper.selectByPrimaryKey(s);
//                if(orderInfo!=null) {
//                    orderStorageInfo.setStoreId(orderInfo.getStoreId());
//                }
                String weight = orderStorageInfo.getStorageWeight();
                String recycleWeight = orderStorageInfo.getRecycleWeight();
                if(StrUtil.isBlank(weight)){
                    orderStorageInfo.setStorageWeight(Constants.WEIGHT_INIT.toString());
                }
                if(StrUtil.isBlank(recycleWeight)){
                    orderStorageInfo.setRecycleWeight(Constants.WEIGHT_INIT.toString());
                }
 
            }
        }
        //如果是入库员 需要显示回收员名字和其所属的车辆编号
        if(StrUtil.isNotBlank(simplePage.getExtra())) {
            for (OrderStorageInfo orderStorageInfo : select) {
                String receiver = orderStorageInfo.getReceiver();
                orderStorageInfo.setReceiverName(otherUserService.findById(receiver).getName());
                VehicleInfo byUserId = userVehicleInfoService.findByUserId(receiver);
                if (byUserId != null) {
                    orderStorageInfo.setVehicleNo(byUserId.getVehicleNo());
                }
            }
        }else {
            //回收员
            for (OrderStorageInfo orderStorageInfo : select) {
                String orderId = orderStorageInfo.getOrderId();
                String[] split = orderId.split(",");
                if(ArrayUtil.isNotEmpty(split)&&split.length==1){
                    orderId=split[0];
                    OrderInfo byId = orderService.findById(orderId);
                    //设置订单状态
                    orderStorageInfo.setOrderType(byId.getOrderType());
 
                    if(OrderEnum.到家服务.getValue().equals(byId.getOrderType())){
                        orderStorageInfo.setRelaPhone(byId.getRelaPhone());
                        orderStorageInfo.setCreateUserId(byId.getCreateUserId());
                        orderStorageInfo.setOrderId(byId.getOrderId());
                    }else if(OrderEnum.家电回收.getValue().equals(byId.getOrderType())){
                        OrderDetailInfo byId1 = orderDetailService.findById(orderId);
                        Long homeApplianceId = byId1.getHomeApplianceId();
                        //家电下单的家电信息
                        if(homeApplianceId!=null) {
                            OrderHomeAppliance byOrderId = orderHomeApplianceService.findById(homeApplianceId);
                            orderStorageInfo.setOrderHomeAppliance(byOrderId);
                        }
                    }
 
 
                }
            }
        }
        return select;
    }
 
    /**
     * 已完成订单列表个数
     * @return
     */
    public   int  countCompleteOrder(String userId){
        Example example=new Example(OrderStorageInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storageStatus",Convert.toShort(OrderEnum.已入库.getValue()));
        criteria.andEqualTo("storageUserId",userId);
        int select = orderStorageInfoMapper.selectCountByExample(example);
        return select;
    }
 
    /**
     * 新任务订单列表个数
     * @return
     */
    public   int  countStorageOrder(String userId){
        Example example=new Example(OrderStorageInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storageStatus",Convert.toShort(OrderEnum.入库检验中.getValue()));
        criteria.andEqualTo("storageUserId",userId);
        int select = orderStorageInfoMapper.selectCountByExample(example);
        return select;
    }
 
 
 
    /**
     * 手动扫码入库新任务详情
     *
     * @param request
     * @param userId
     * @param usertype 1入库 2打包
     * @return
     */
    public Result<RkDto> rkDetail(HttpServletRequest request, String userId, int usertype, String packageId) {
        Result<RkDto> result = new Result();
        RkDto rk = new RkDto();
 
        String key=Constants.REDIS_ORDER_KEY+"rkscan:"+request.getHeader("userId");
        if(redisUtil.setnx(key,"0")) {
 
            try {
                //获取用户所有待入库订单
                List<OrderInfo> orderInfos = orderService.findDetailByReceiverAndStatus(userId, OrderEnum.待入库.getValue());
                if (CollUtil.isEmpty(orderInfos)) {
                    return Result.error(-1, "没有待入库的订单");
                }
 
 
                //定点回收
                List<String> collect1 = orderInfos.stream().filter(x -> x.getStoreId() != null).map(OrderInfo::getOrderId).collect(Collectors.toList());
 
                //上门回收
                List<String> collect2 = orderInfos.stream().filter(x -> x.getStoreId() == null).map(OrderInfo::getOrderId).collect(Collectors.toList());
 
                //1为上门 2为定点
                int type = 0;
 
                Dict dict = null;
                if (CollUtil.isNotEmpty(collect1)) {
                    type = 2;
                    String storageId = idUtils.generate("RK", 0);
                    dict = orderBatchInfoService.addDB(userId, collect1, storageId, usertype, packageId);
                }
 
                if (CollUtil.isNotEmpty(collect2)) {
                    type = 1;
                    String storageId = idUtils.generate("RK", 2);
                    dict = orderBatchInfoService.addDB(userId, collect2, storageId, usertype, packageId);
                }
 
 
                //混合订单直接返回到列表
                if (CollUtil.isNotEmpty(collect1) && CollUtil.isNotEmpty(collect2)) {
                    rk.setMixOrder(true);
                    result.setData(rk);
                    return result;
                }
 
                List<OrderItemDto> orderItemDtos = new ArrayList<>();
                List<OrderStorageDetailInfo> select = (List<OrderStorageDetailInfo>) dict.get("detail");
                //设置分类
                setItem(rk, orderItemDtos, select, usertype, packageId);
                rk.setStorageUserId(request.getHeader("userId"));
                rk.setStorageId(dict.getStr("storageId"));
                OrderInfoReq orderInfoReq = new OrderInfoReq();
                orderInfoReq.setReceiver(userId);
                List<String> strings = orderMapper.fwOrderListByUserId(orderInfoReq);
                if (CollUtil.isNotEmpty(strings)) {
                    rk.setFwOrderNum(strings.size());
                }
                //回收金额
                String recycleMoney = dict.getStr("recycleMoney");
                rk.setMoney(recycleMoney);
                result.setData(rk);
            } catch (Exception e) {
                ExceptionUtils.err("扫码入库失败",e);
            } finally {
                redisUtil.del(key);
            }
            return result;
        }else {
            throw new RestException(-1,"二维码已被扫码");
        }
 
    }
 
 
    /**
     * 设置分类信息
     * @param rk
     * @param orderItemDtos
     * @param select
     */
    public void setItem(RkDto rk, List<OrderItemDto> orderItemDtos, List<OrderStorageDetailInfo> select,  int usertype, String packageId) {
        //入库根据仓库所属地查看价格
        Long parentId = null;
 
        if (usertype == 1) {
            SysStorage byId1 = sysStorageService.findById(Convert.toInt(packageId));
            Integer areaId = Convert.toInt(byId1.getArea());
            SysEnvironmentalInfo sysEnvironmentalInfo2 = sysEnvironmentalInfoService.findByItemTypeAndArea(Constants.HOUSE_HOLD_ITEM_TYPE,areaId);
            if (sysEnvironmentalInfo2 != null) {
                parentId = sysEnvironmentalInfo2.getId();
            }
            if (CollUtil.isNotEmpty(select)) {
                for (OrderStorageDetailInfo detailInfo : select) {
                    OrderItemDto orderItemDto = new OrderItemDto();
                    BeanUtils.copyProperties(detailInfo, orderItemDto);
 
                    //设置分类名称和图片
                    String itemType = detailInfo.getItemType();
                    SysEnvironmentalInfo sysEnvironmentalInfo1 = null;
                    sysEnvironmentalInfo1 = sysEnvironmentalInfoService.findByItemTypeAndArea(itemType, areaId);
 
                    if (sysEnvironmentalInfo1 != null) {
 
 
                        Long parentId1 = sysEnvironmentalInfo1.getParentId();
                        if (parentId != null && parentId.intValue() == (parentId1.intValue())) {
                            orderItemDto.setHouseHold(true);
                            BigDecimal mul = NumberUtil.mul(Convert.toBigDecimal(sysEnvironmentalInfo1.getPutStoragePrice()), Convert.toBigDecimal(orderItemDto.getAmount(), Constants.MONEY_INIT));
                            orderItemDto.setMoney(businessUtil.changeMoney(mul));
                        }else {
                            BigDecimal mul = NumberUtil.mul(Convert.toBigDecimal(sysEnvironmentalInfo1.getPutStoragePrice()), Convert.toBigDecimal(orderItemDto.getWeight(), Constants.MONEY_INIT));
                            orderItemDto.setMoney(businessUtil.changeMoney(mul));
                        }
                        orderItemDto.setItemPic(sysEnvironmentalInfo1.getPicture());
                        orderItemDto.setItemName(sysEnvironmentalInfo1.getTitle());
                        orderItemDto.setItemTypePrice(sysEnvironmentalInfo1.getPutStoragePrice().toString());
//                        if (type == 1) {
//                            orderItemDto.setItemTypePrice(sysEnvironmentalInfo1.getPutStoragePrice().toString());
//                        } else if (type == 2) {
//                            orderItemDto.setItemTypePrice(sysEnvironmentalInfo1.getSecondPrice());
//                        }
 
                        //查找父类的itemType
                        SysEnvironmentalInfo byId = null;
                        byId = sysEnvironmentalInfoService.findById(parentId1);
                        if (byId != null) {
                            orderItemDto.setParentItemType(byId.getItemType());
                        }
                        orderItemDtos.add(orderItemDto);
                    }
                }
                //按重量降序排序
                List<OrderItemDto> collect = orderItemDtos.stream().sorted(Comparator.comparing(OrderItemDto::getWeight).reversed()).collect(Collectors.toList());
                rk.setStorageItemList(collect);
            }
        } else if (usertype == 2) {
            List<PackageGoodsInfo> byPackageId = packageGoodsInfoService.findByPackageId(packageId);
            if (CollUtil.isNotEmpty(byPackageId)) {
                for (PackageGoodsInfo packageGoodsInfo : byPackageId) {
                    OrderItemDto orderItemDto = new OrderItemDto();
                    orderItemDto.setAmount("0");
                    orderItemDto.setWeight(Constants.WEIGHT_INIT);
                    orderItemDto.setMoney(Constants.MONEY_INIT.toString());
                    orderItemDto.setItemType(packageGoodsInfo.getItemType());
                    //设置分类名称和图片
                    Long parentId1 = packageGoodsInfo.getParentId();
                    if (parentId != null && parentId.intValue() == (parentId1.intValue())) {
                        orderItemDto.setHouseHold(true);
                    }
                    orderItemDto.setItemPic(packageGoodsInfo.getPicture());
                    orderItemDto.setItemName(packageGoodsInfo.getTitle());
 
                    orderItemDto.setItemTypePrice(packageGoodsInfo.getPutStoragePrice().toString());
//                    if (type == 1) {
//                        orderItemDto.setItemTypePrice(packageGoodsInfo.getPrice());
//                    } else if (type == 2) {
//                        orderItemDto.setItemTypePrice(packageGoodsInfo.getSecondPrice());
//                    }
 
                    //查找父类的itemType
                    SysEnvironmentalInfo byId = null;
                    packageGoodsInfo = packageGoodsInfoService.findById(parentId1);
                    if (packageGoodsInfo != null) {
                        byId = new SysEnvironmentalInfo();
                        BeanUtil.copyProperties(packageGoodsInfo, byId);
                    }
                    if (byId != null) {
                        orderItemDto.setParentItemType(byId.getItemType());
                    }
                    orderItemDtos.add(orderItemDto);
                }
                rk.setStorageItemList(orderItemDtos);
            }
 
        }
 
    }
 
 
}