doum
7 天以前 074bcb8394fab66ce531c219e1e7de7c142ff2d5
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
package com.doumee.service.business.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.core.annotation.excel.ExcelExporter;
import com.doumee.core.conditoner.ConditionerUtil;
import com.doumee.core.conditoner.model.request.AddMoneyRequest;
import com.doumee.core.conditoner.model.request.CompanyGsManageRequest;
import com.doumee.core.conditoner.model.request.LogQueryRequest;
import com.doumee.core.conditoner.model.response.CompanyGsInfoResponse;
import com.doumee.core.conditoner.model.response.ConditionerBaseResponse;
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.*;
import com.doumee.dao.business.model.*;
import com.doumee.service.business.ConditionerBizService;
import com.doumee.service.business.YwCustomerRechargeBizService;
import com.doumee.service.business.YwElectricalBizService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class YwCustomerRechargeBizServiceImpl implements YwCustomerRechargeBizService {
 
    private static final String ONLINE_TEXT = "在线";
 
    @Autowired
    private YwCustomerMapper ywCustomerMapper;
    @Autowired
    private YwCustomerGsMapper ywCustomerGsMapper;
    @Autowired
    private YwCustomerElectricalMapper ywCustomerElectricalMapper;
    @Autowired
    private YwCustomerConditionerMapper ywCustomerConditionerMapper;
    @Autowired
    private YwElectricalMapper ywElectricalMapper;
    @Autowired
    private YwConditionerMapper ywConditionerMapper;
    @Autowired
    private YwElectricalChargeMapper ywElectricalChargeMapper;
    @Autowired
    private YwElectricalActionsMapper ywElectricalActionsMapper;
    @Autowired
    private YwElectricalBizService ywElectricalBizService;
    @Autowired
    private ConditionerBizService conditionerBizService;
 
    @Override
    public PageData<YwCustomerRechargeMerchantVO> findMerchantPage(PageWrap<YwCustomerRechargeQueryDTO> pageWrap) {
        if (pageWrap == null) {
            pageWrap = new PageWrap<>();
        }
        YwCustomerRechargeQueryDTO query = pageWrap.getModel() != null ? pageWrap.getModel() : new YwCustomerRechargeQueryDTO();
        boolean hasDeviceFilter = query.getElectricalStatusFilter() != null || query.getConditionerStatusFilter() != null;
 
        if (hasDeviceFilter) {
            List<YwCustomer> all = ywCustomerMapper.selectJoinList(YwCustomer.class, buildMerchantCustomerWrapper(query));
            List<YwCustomerRechargeMerchantVO> enriched = enrichMerchantList(all);
            List<YwCustomerRechargeMerchantVO> filtered = enriched.stream()
                    .filter(vo -> matchDeviceFilter(vo, query))
                    .collect(Collectors.toList());
            return manualPage(filtered, pageWrap.getPage(), pageWrap.getCapacity());
        }
 
        IPage<YwCustomer> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        IPage<YwCustomer> result = ywCustomerMapper.selectJoinPage(page, YwCustomer.class, buildMerchantCustomerWrapper(query));
        List<YwCustomerRechargeMerchantVO> list = enrichMerchantList(result.getRecords());
        PageData<YwCustomerRechargeMerchantVO> data = new PageData<>();
        data.setRecords(list);
        data.setTotal(result.getTotal());
        data.setPage(result.getCurrent());
        data.setCapacity(result.getSize());
        return data;
    }
 
    private MPJLambdaWrapper<YwCustomer> buildMerchantCustomerWrapper(YwCustomerRechargeQueryDTO query) {
        return new MPJLambdaWrapper<YwCustomer>()
                .selectAll(YwCustomer.class)
                .selectAs(Member::getName, YwCustomer::getMemberName)
                .selectAs(Member::getPhone, YwCustomer::getMemberPhone)
                .leftJoin(Member.class, Member::getId, YwCustomer::getMemberId)
                .eq(YwCustomer::getIsdeleted, Constants.ZERO)
                .like(StringUtils.isNotBlank(query.getNameKeyword()), YwCustomer::getName, query.getNameKeyword())
                .orderByDesc(YwCustomer::getCreateDate);
    }
 
    private PageData<YwCustomerRechargeMerchantVO> manualPage(List<YwCustomerRechargeMerchantVO> list, long page, long capacity) {
        int p = (int) Math.max(page, 1);
        int size = (int) Math.max(capacity, 1);
        int from = (p - 1) * size;
        int to = Math.min(from + size, list.size());
        PageData<YwCustomerRechargeMerchantVO> data = new PageData<>();
        data.setTotal(list.size());
        data.setPage(p);
        data.setCapacity(size);
        data.setRecords(from >= list.size() ? Collections.emptyList() : list.subList(from, to));
        return data;
    }
 
    private boolean matchDeviceFilter(YwCustomerRechargeMerchantVO vo, YwCustomerRechargeQueryDTO query) {
        if (query.getElectricalStatusFilter() != null && !matchStatusFilter(
                query.getElectricalStatusFilter(), vo.getElectricalCount(), vo.getElectricalOnlineAll(), vo.getElectricalHasOffline())) {
            return false;
        }
        if (query.getConditionerStatusFilter() != null && !matchStatusFilter(
                query.getConditionerStatusFilter(), vo.getConditionerCount(), vo.getConditionerOnlineAll(), vo.getConditionerHasOffline())) {
            return false;
        }
        return true;
    }
 
    private boolean matchStatusFilter(Integer filter, Integer count, Boolean allOnline, Boolean hasOffline) {
        int c = count != null ? count : 0;
        if (filter == 3) {
            return c == 0;
        }
        if (c == 0) {
            return false;
        }
        if (filter == 1) {
            return Boolean.TRUE.equals(allOnline);
        }
        if (filter == 2) {
            return Boolean.TRUE.equals(hasOffline);
        }
        return true;
    }
 
    private List<YwCustomerRechargeMerchantVO> enrichMerchantList(List<YwCustomer> customers) {
        if (CollectionUtils.isEmpty(customers)) {
            return Collections.emptyList();
        }
        List<Integer> customerIds = customers.stream().map(YwCustomer::getId).collect(Collectors.toList());
 
        Map<Integer, YwCustomerGs> gsMap = loadGsMap(customerIds);
 
        List<YwCustomerElectrical> relE = loadCustomerElectricalRels(customerIds);
        Map<Integer, List<Integer>> customerElectricalIds = relE.stream()
                .collect(Collectors.groupingBy(YwCustomerElectrical::getCustomerId,
                        Collectors.mapping(YwCustomerElectrical::getElectricalId, Collectors.toList())));
 
        List<YwCustomerConditioner> relC = loadCustomerConditionerRels(customerIds);
        Map<Integer, List<Integer>> customerConditionerIds = relC.stream()
                .collect(Collectors.groupingBy(YwCustomerConditioner::getCustomerId,
                        Collectors.mapping(YwCustomerConditioner::getConditionerId, Collectors.toList())));
 
        Set<Integer> allElectricalIds = relE.stream().map(YwCustomerElectrical::getElectricalId).collect(Collectors.toSet());
        Map<Integer, YwElectrical> electricalMap = loadElectricalMap(allElectricalIds);
 
        Set<Integer> allConditionerIds = relC.stream().map(YwCustomerConditioner::getConditionerId).collect(Collectors.toSet());
        Map<Integer, YwConditioner> conditionerMap = loadConditionerMap(allConditionerIds);
 
        List<YwCustomerRechargeMerchantVO> list = new ArrayList<>();
        for (YwCustomer c : customers) {
            YwCustomerRechargeMerchantVO vo = new YwCustomerRechargeMerchantVO();
            vo.setId(c.getId());
            vo.setType(c.getType());
            vo.setName(c.getName());
            vo.setPhone(c.getPhone());
            vo.setCreateDate(c.getCreateDate());
            vo.setMemberName(c.getMemberName());
            vo.setMemberPhone(StringUtils.defaultIfBlank(c.getMemberPhone(), c.getPhone()));
 
            List<Integer> eIds = customerElectricalIds.getOrDefault(c.getId(), Collections.emptyList());
            vo.setElectricalCount(eIds.size());
            if (eIds.isEmpty()) {
                vo.setElectricalBalances(Collections.emptyList());
                vo.setElectricalOnlineAll(false);
                vo.setElectricalHasOffline(false);
            } else {
                List<YwCustomerElectricalBalanceItem> balanceItems = new ArrayList<>();
                boolean allOnline = true;
                boolean hasOffline = false;
                int index = 0;
                for (Integer eid : eIds) {
                    YwElectrical e = electricalMap.get(eid);
                    if (e == null) continue;
                    YwCustomerElectricalBalanceItem item = new YwCustomerElectricalBalanceItem();
                    index++;
                    item.setName(StringUtils.isNotBlank(e.getName()) ? e.getName() : ("电表" + index));
                    item.setAddress(StringUtils.isNotBlank(e.getAddress()) ? e.getAddress() : "-");
                    item.setBalance(e.getBalance() != null ? e.getBalance() : BigDecimal.ZERO);
                    balanceItems.add(item);
                    if (!Objects.equals(e.getOnline(), Constants.ONE)) {
                        allOnline = false;
                        hasOffline = true;
                    }
                }
                vo.setElectricalBalances(balanceItems);
                vo.setElectricalOnlineAll(allOnline && !balanceItems.isEmpty());
                vo.setElectricalHasOffline(hasOffline);
            }
 
            List<Integer> cIds = customerConditionerIds.getOrDefault(c.getId(), Collections.emptyList());
            vo.setConditionerCount(cIds.size());
            if (cIds.isEmpty()) {
                vo.setConditionerOnlineAll(false);
                vo.setConditionerHasOffline(false);
            } else {
                boolean allOnline = true;
                boolean hasOffline = false;
                for (Integer cid : cIds) {
                    YwConditioner cond = conditionerMap.get(cid);
                    if (cond == null) continue;
                    if (!ONLINE_TEXT.equals(cond.getOnline())) {
                        allOnline = false;
                        hasOffline = true;
                    }
                }
                vo.setConditionerOnlineAll(allOnline);
                vo.setConditionerHasOffline(hasOffline);
            }
 
            YwCustomerGs gs = gsMap.get(c.getId());
            if (gs != null) {
                vo.setAcBalance(gs.getLeftMoney());
                vo.setAcBalanceSyncDate(gs.getSyncDate());
            }
            list.add(vo);
        }
        return list;
    }
 
    private Map<Integer, YwCustomerGs> loadGsMap(List<Integer> customerIds) {
        if (CollectionUtils.isEmpty(customerIds)) {
            return Collections.emptyMap();
        }
        try {
            return ywCustomerGsMapper.selectList(new QueryWrapper<YwCustomerGs>().lambda()
                            .select(YwCustomerGs::getId, YwCustomerGs::getCustomerId,
                                    YwCustomerGs::getLeftMoney, YwCustomerGs::getSyncDate, YwCustomerGs::getPlatformGsId)
                            .eq(YwCustomerGs::getIsdeleted, Constants.ZERO)
                            .in(YwCustomerGs::getCustomerId, customerIds))
                    .stream().collect(Collectors.toMap(YwCustomerGs::getCustomerId, g -> g, (a, b) -> a));
        } catch (Exception e) {
            log.warn("load yw_customer_gs failed, skip gs stats: {}", e.getMessage());
            return Collections.emptyMap();
        }
    }
 
    private List<YwCustomerElectrical> loadCustomerElectricalRels(List<Integer> customerIds) {
        if (CollectionUtils.isEmpty(customerIds)) {
            return Collections.emptyList();
        }
        try {
            return ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda()
                    .select(YwCustomerElectrical::getCustomerId, YwCustomerElectrical::getElectricalId)
                    .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO)
                    .in(YwCustomerElectrical::getCustomerId, customerIds));
        } catch (Exception e) {
            log.warn("load yw_customer_electrical failed, skip electrical stats: {}", e.getMessage());
            return Collections.emptyList();
        }
    }
 
    private List<YwCustomerConditioner> loadCustomerConditionerRels(List<Integer> customerIds) {
        if (CollectionUtils.isEmpty(customerIds)) {
            return Collections.emptyList();
        }
        try {
            return ywCustomerConditionerMapper.selectList(new QueryWrapper<YwCustomerConditioner>().lambda()
                    .select(YwCustomerConditioner::getCustomerId, YwCustomerConditioner::getConditionerId)
                    .eq(YwCustomerConditioner::getIsdeleted, Constants.ZERO)
                    .in(YwCustomerConditioner::getCustomerId, customerIds));
        } catch (Exception e) {
            log.warn("load yw_customer_conditioner failed, skip conditioner stats: {}", e.getMessage());
            return Collections.emptyList();
        }
    }
 
    private Map<Integer, YwElectrical> loadElectricalMap(Set<Integer> electricalIds) {
        if (CollectionUtils.isEmpty(electricalIds)) {
            return Collections.emptyMap();
        }
        try {
            return ywElectricalMapper.selectList(new QueryWrapper<YwElectrical>().lambda()
                            .select(YwElectrical::getId, YwElectrical::getName, YwElectrical::getAddress,
                                    YwElectrical::getBalance, YwElectrical::getOnline, YwElectrical::getIsdeleted)
                            .in(YwElectrical::getId, electricalIds)
                            .eq(YwElectrical::getIsdeleted, Constants.ZERO))
                    .stream().collect(Collectors.toMap(YwElectrical::getId, e -> e, (a, b) -> a));
        } catch (Exception e) {
            log.warn("load electrical for merchant page failed: {}", e.getMessage());
            return Collections.emptyMap();
        }
    }
 
    private Map<Integer, YwConditioner> loadConditionerMap(Set<Integer> conditionerIds) {
        if (CollectionUtils.isEmpty(conditionerIds)) {
            return Collections.emptyMap();
        }
        try {
            return ywConditionerMapper.selectList(new QueryWrapper<YwConditioner>().lambda()
                            .select(YwConditioner::getId, YwConditioner::getOnline, YwConditioner::getIsdeleted)
                            .in(YwConditioner::getId, conditionerIds)
                            .eq(YwConditioner::getIsdeleted, Constants.ZERO))
                    .stream().collect(Collectors.toMap(YwConditioner::getId, c -> c, (a, b) -> a));
        } catch (Exception e) {
            log.warn("load conditioner for merchant page failed: {}", e.getMessage());
            return Collections.emptyMap();
        }
    }
 
    @Override
    public YwCustomerRechargeDetailVO getDetail(Integer customerId) {
        YwCustomer customer = requireCustomer(customerId);
        YwCustomerRechargeDetailVO vo = new YwCustomerRechargeDetailVO();
        vo.setCustomerId(customer.getId());
        vo.setCustomerName(customer.getName());
        vo.setPhone(customer.getPhone());
        vo.setGsConfig(getCustomerGsConfig(customerId));
        vo.setElectricalList(loadCustomerElectricalList(customerId));
        vo.setConditionerList(loadCustomerConditionerList(customerId));
        return vo;
    }
 
    @Override
    public PageData<YwElectrical> listCustomerElectrical(PageWrap<YwElectrical> pageWrap, Integer customerId) {
        requireCustomer(customerId);
        List<Integer> ids = listBoundElectricalIds(customerId);
        if (ids.isEmpty()) {
            return emptyPage(pageWrap);
        }
        return pageElectricalByIds(pageWrap, ids);
    }
 
    @Override
    public PageData<YwElectrical> pageSelectableElectrical(PageWrap<YwElectrical> pageWrap, Integer customerId) {
        requireCustomer(customerId);
        Set<Integer> excluded = listAllBoundElectricalIds();
        IPage<YwElectrical> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        YwElectrical model = pageWrap.getModel();
        QueryWrapper<YwElectrical> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(YwElectrical::getIsdeleted, Constants.ZERO)
                .notIn(!excluded.isEmpty(), YwElectrical::getId, excluded)
                .and(model != null && StringUtils.isNotBlank(model.getName()), w -> w
                        .like(YwElectrical::getName, model.getName())
                        .or().like(YwElectrical::getAddress, model.getName()))
                .orderByDesc(YwElectrical::getCreateDate);
        IPage<YwElectrical> result = ywElectricalMapper.selectPage(page, qw);
        ywElectricalBizService.enrichList(result.getRecords());
        return PageData.from(result);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCustomerElectrical(YwCustomerElectricalSaveDTO dto, LoginUserInfo user) {
        requireCustomer(dto.getCustomerId());
        if (CollectionUtils.isEmpty(dto.getElectricalIds())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "请选择电表");
        }
        Set<Integer> excluded = listAllBoundElectricalIdsExcept(dto.getCustomerId());
        for (Integer eid : dto.getElectricalIds()) {
            if (excluded.contains(eid)) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "电表已被其他商户绑定:" + eid);
            }
            YwCustomerElectrical exist = ywCustomerElectricalMapper.selectOne(new QueryWrapper<YwCustomerElectrical>().lambda()
                    .eq(YwCustomerElectrical::getCustomerId, dto.getCustomerId())
                    .eq(YwCustomerElectrical::getElectricalId, eid)
                    .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO)
                    .last("limit 1"));
            if (exist != null) {
                continue;
            }
            YwCustomerElectrical rel = new YwCustomerElectrical();
            rel.setCreator(user.getId());
            rel.setCreateDate(new Date());
            rel.setEditor(user.getId());
            rel.setEditDate(new Date());
            rel.setIsdeleted(Constants.ZERO);
            rel.setCustomerId(dto.getCustomerId());
            rel.setElectricalId(eid);
            ywCustomerElectricalMapper.insert(rel);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCustomerElectrical(Integer customerId, Integer electricalId, LoginUserInfo user) {
        ywCustomerElectricalMapper.update(null, new UpdateWrapper<YwCustomerElectrical>().lambda()
                .set(YwCustomerElectrical::getIsdeleted, Constants.ONE)
                .set(YwCustomerElectrical::getEditDate, new Date())
                .set(YwCustomerElectrical::getEditor, user.getId())
                .eq(YwCustomerElectrical::getCustomerId, customerId)
                .eq(YwCustomerElectrical::getElectricalId, electricalId)
                .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO));
    }
 
    @Override
    public PageData<YwConditioner> listCustomerConditioner(PageWrap<YwConditioner> pageWrap, Integer customerId) {
        requireCustomer(customerId);
        List<YwConditioner> list = loadCustomerConditionerList(customerId);
        if (CollectionUtils.isEmpty(list)) {
            return emptyPage(pageWrap);
        }
        int p = (int) Math.max(pageWrap.getPage(), 1);
        int size = (int) Math.max(pageWrap.getCapacity(), 1);
        int from = (p - 1) * size;
        int to = Math.min(from + size, list.size());
        PageData<YwConditioner> data = new PageData<>();
        data.setTotal(list.size());
        data.setPage(p);
        data.setCapacity(size);
        data.setRecords(from >= list.size() ? Collections.emptyList() : list.subList(from, to));
        return data;
    }
 
    @Override
    public YwCustomerGs getCustomerGsConfig(Integer customerId) {
        try {
            return ywCustomerGsMapper.selectOne(new QueryWrapper<YwCustomerGs>().lambda()
                    .eq(YwCustomerGs::getCustomerId, customerId)
                    .eq(YwCustomerGs::getIsdeleted, Constants.ZERO)
                    .last("limit 1"));
        } catch (Exception e) {
            log.warn("load yw_customer_gs failed, retry without stop_money: {}", e.getMessage());
            return ywCustomerGsMapper.selectOne(new QueryWrapper<YwCustomerGs>().lambda()
                    .select(YwCustomerGs::getId, YwCustomerGs::getCustomerId, YwCustomerGs::getPlatformGsId,
                            YwCustomerGs::getIsPwr, YwCustomerGs::getIsRestStop, YwCustomerGs::getGsBz,
                            YwCustomerGs::getLeftMoney, YwCustomerGs::getSyncDate,
                            YwCustomerGs::getCreator, YwCustomerGs::getCreateDate,
                            YwCustomerGs::getEditor, YwCustomerGs::getEditDate, YwCustomerGs::getIsdeleted)
                    .eq(YwCustomerGs::getCustomerId, customerId)
                    .eq(YwCustomerGs::getIsdeleted, Constants.ZERO)
                    .last("limit 1"));
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCustomerGsConfig(YwCustomerGsConfigDTO dto, LoginUserInfo user) {
        YwCustomer customer = requireCustomer(dto.getCustomerId());
        validateGsConfigRequired(dto);
        conditionerBizService.ensureLogin();
 
        List<Integer> conditionerIds = dto.getConditioners().stream()
                .map(YwCustomerGsConfigDTO.ConditionerItem::getConditionerId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        List<YwConditioner> conditioners = ywConditionerMapper.selectBatchIds(conditionerIds);
        Map<Integer, YwConditioner> condMap = conditioners.stream()
                .filter(c -> !Objects.equals(c.getIsdeleted(), Constants.ONE))
                .collect(Collectors.toMap(YwConditioner::getId, c -> c, (a, b) -> a));
 
        List<Integer> liDev = new ArrayList<>();
        Map<String, Object> dDev = new LinkedHashMap<>();
        for (YwCustomerGsConfigDTO.ConditionerItem item : dto.getConditioners()) {
            YwConditioner cond = condMap.get(item.getConditionerId());
            if (cond == null || cond.getPlatformDevId() == null) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "内机缺少平台设备 ID");
            }
            liDev.add(cond.getPlatformDevId());
            int ratio = item.getDevRatio() != null ? item.getDevRatio() : 100;
            dDev.put(String.valueOf(cond.getPlatformDevId()), ratio);
        }
 
        YwCustomerGs gs = getCustomerGsConfig(dto.getCustomerId());
        if (gs == null) {
            gs = new YwCustomerGs();
            gs.setCreator(user.getId());
            gs.setCreateDate(new Date());
            gs.setIsdeleted(Constants.ZERO);
            gs.setCustomerId(dto.getCustomerId());
        }
        gs.setEditor(user.getId());
        gs.setEditDate(new Date());
        gs.setIsPwr(dto.getIsPwr());
        gs.setIsRestStop(dto.getIsRestStop());
        gs.setGsBz(StringUtils.defaultString(dto.getGsBz()));
        gs.setStopMoney(dto.getStopMoney());
 
        if (StringUtils.isBlank(customer.getName())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "客户名称不能为空");
        }
        if (liDev.isEmpty()) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "关联内机平台设备 ID 无效");
        }
 
        CompanyGsManageRequest companyReq = new CompanyGsManageRequest();
        companyReq.fillSessionDefaults();
        companyReq.setGs_name(customer.getName().trim());
        companyReq.setIs_pwr(gs.getIsPwr());
        companyReq.setIs_rest_stop(gs.getIsRestStop());
        companyReq.setGs_bz(gs.getGsBz());
        companyReq.setStop_money(gs.getStopMoney());
        companyReq.setLi_dev(liDev);
        companyReq.setD_dev(dDev);
 
        if (gs.getPlatformGsId() == null) {
            ConditionerBaseResponse<Object> addResp = ConditionerUtil.addGs(companyReq);
            if (addResp == null || !addResp.isSuccess()) {
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), apiMsg(addResp, "addGs 失败"));
            }
            gs.setPlatformGsId(parseAddGsId(addResp.getData()));
            if (gs.getPlatformGsId() == null) {
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "addGs 未返回公司 ID");
            }
        }
 
        CompanyGsManageRequest changeReq = new CompanyGsManageRequest();
        changeReq.fillSessionDefaults();
        changeReq.setId(gs.getPlatformGsId());
        changeReq.setGs_name(companyReq.getGs_name());
        changeReq.setIs_pwr(companyReq.getIs_pwr());
        changeReq.setIs_rest_stop(companyReq.getIs_rest_stop());
        changeReq.setGs_bz(companyReq.getGs_bz());
        changeReq.setStop_money(gs.getStopMoney());
        changeReq.setLi_dev(liDev);
        changeReq.setD_dev(dDev);
        if (gs.getLeftMoney() != null) {
            changeReq.setLeft_money(gs.getLeftMoney());
        }
        ConditionerBaseResponse<Object> changeResp = ConditionerUtil.changeGs(changeReq);
        if (changeResp == null || !changeResp.isSuccess()) {
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), apiMsg(changeResp, "changeGs 失败"));
        }
 
        refreshGsLeftMoney(gs);
        if (gs.getId() == null) {
            try {
                ywCustomerGsMapper.insert(gs);
            } catch (Exception e) {
                if (gs.getStopMoney() != null) {
                    log.warn("insert yw_customer_gs with stop_money failed, retry without stop_money: {}", e.getMessage());
                    gs.setStopMoney(null);
                    ywCustomerGsMapper.insert(gs);
                } else {
                    throw e;
                }
            }
        } else {
            try {
                ywCustomerGsMapper.updateById(gs);
            } catch (Exception e) {
                if (gs.getStopMoney() != null) {
                    log.warn("update yw_customer_gs with stop_money failed, retry without stop_money: {}", e.getMessage());
                    BigDecimal stopMoney = gs.getStopMoney();
                    gs.setStopMoney(null);
                    ywCustomerGsMapper.updateById(gs);
                    gs.setStopMoney(stopMoney);
                } else {
                    throw e;
                }
            }
        }
 
        saveCustomerConditionerRels(dto, user);
    }
 
    /**
     * 关联内机 upsert:表上有 uk(customer_id, conditioner_id),不能软删后重复 insert。
     */
    private void saveCustomerConditionerRels(YwCustomerGsConfigDTO dto, LoginUserInfo user) {
        List<YwCustomerConditioner> existingRels = ywCustomerConditionerMapper.selectList(
                new QueryWrapper<YwCustomerConditioner>().lambda()
                        .eq(YwCustomerConditioner::getCustomerId, dto.getCustomerId()));
        Map<Integer, YwCustomerConditioner> relByCondId = existingRels.stream()
                .collect(Collectors.toMap(YwCustomerConditioner::getConditionerId, r -> r, (a, b) -> a));
 
        Set<Integer> targetIds = dto.getConditioners().stream()
                .map(YwCustomerGsConfigDTO.ConditionerItem::getConditionerId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
 
        Date now = new Date();
        for (YwCustomerConditioner rel : existingRels) {
            if (!targetIds.contains(rel.getConditionerId())
                    && Objects.equals(rel.getIsdeleted(), Constants.ZERO)) {
                ywCustomerConditionerMapper.update(null, new UpdateWrapper<YwCustomerConditioner>().lambda()
                        .set(YwCustomerConditioner::getIsdeleted, Constants.ONE)
                        .set(YwCustomerConditioner::getEditDate, now)
                        .set(YwCustomerConditioner::getEditor, user.getId())
                        .eq(YwCustomerConditioner::getId, rel.getId()));
            }
        }
 
        for (YwCustomerGsConfigDTO.ConditionerItem item : dto.getConditioners()) {
            if (item.getConditionerId() == null) {
                continue;
            }
            int ratio = item.getDevRatio() != null ? item.getDevRatio() : 100;
            YwCustomerConditioner rel = relByCondId.get(item.getConditionerId());
            if (rel != null) {
                rel.setIsdeleted(Constants.ZERO);
                rel.setDevRatio(ratio);
                rel.setEditor(user.getId());
                rel.setEditDate(now);
                ywCustomerConditionerMapper.updateById(rel);
            } else {
                YwCustomerConditioner created = new YwCustomerConditioner();
                created.setCreator(user.getId());
                created.setCreateDate(now);
                created.setEditor(user.getId());
                created.setEditDate(now);
                created.setIsdeleted(Constants.ZERO);
                created.setCustomerId(dto.getCustomerId());
                created.setConditionerId(item.getConditionerId());
                created.setDevRatio(ratio);
                ywCustomerConditionerMapper.insert(created);
                relByCondId.put(item.getConditionerId(), created);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String rechargeElectrical(YwCustomerRechargeElectricalDTO dto, LoginUserInfo user) {
        requireCustomer(dto.getCustomerId());
        assertElectricalBound(dto.getCustomerId(), dto.getElectricalId());
        YwElectricalOperateDTO op = new YwElectricalOperateDTO();
        op.setElectricalId(dto.getElectricalId());
        op.setAction("recharge");
        op.setMoney(dto.getMoney());
        op.setRemark(dto.getRemark());
        op.setCustomerId(dto.getCustomerId());
        return ywElectricalBizService.operate(op, user);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String resetElectricalAccount(YwCustomerRechargeElectricalDTO dto, LoginUserInfo user) {
        requireCustomer(dto.getCustomerId());
        assertElectricalBound(dto.getCustomerId(), dto.getElectricalId());
        String action = StringUtils.defaultIfBlank(dto.getResetAction(), "resetPrepay");
        YwElectricalOperateDTO op = new YwElectricalOperateDTO();
        op.setElectricalId(dto.getElectricalId());
        op.setAction(action);
        op.setCustomerId(dto.getCustomerId());
        return ywElectricalBizService.operate(op, user);
    }
 
    @Override
    public Map<String, Object> getElectricalRemoteInfo(Integer electricalId) {
        return ywElectricalBizService.getRemoteInfo(electricalId);
    }
 
    @Override
    public Map<String, Object> readMeterAndRefresh(Integer customerId, Integer electricalId, LoginUserInfo user) {
        assertElectricalBound(customerId, electricalId);
        YwElectricalOperateDTO op = new YwElectricalOperateDTO();
        op.setElectricalId(electricalId);
        op.setAction("readMeter");
        op.setCustomerId(customerId);
        String msg = ywElectricalBizService.operate(op, user);
        Map<String, Object> info = ywElectricalBizService.getRemoteInfo(electricalId);
        info.put("message", msg);
        return info;
    }
 
    @Override
    public Map<String, Object> getConditionerRechargeInfo(Integer customerId) {
        YwCustomerGs gs = requireGsConfig(customerId);
        conditionerBizService.ensureLogin();
        refreshGsLeftMoney(gs);
        ywCustomerGsMapper.updateById(gs);
 
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("gsConfig", gs);
        map.put("customerName", requireCustomer(customerId).getName());
        CompanyGsManageRequest req = new CompanyGsManageRequest();
        req.fillSessionDefaults();
        req.setId(gs.getPlatformGsId());
        ConditionerBaseResponse<List<CompanyGsInfoResponse>> resp = ConditionerUtil.getGs(req);
        if (resp != null && resp.isSuccess() && !CollectionUtils.isEmpty(resp.getData())) {
            CompanyGsInfoResponse info = resp.getData().get(0);
            map.put("platformInfo", info);
        }
        return map;
    }
 
    @Override
    public String rechargeConditioner(YwCustomerRechargeConditionerDTO dto, LoginUserInfo user) {
        YwCustomerGs gs = requireGsConfig(dto.getCustomerId());
        if (dto.getMoney() == null || dto.getMoney().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "充值金额须大于 0");
        }
        conditionerBizService.ensureLogin();
        AddMoneyRequest req = new AddMoneyRequest();
        req.fillSessionDefaults();
        req.setId(gs.getPlatformGsId());
        req.setCz_money(dto.getMoney().toPlainString());
        ConditionerBaseResponse<Object> resp = ConditionerUtil.addMoney(req);
        YwElectricalCharge charge = buildConditionerCharge(dto, gs, user);
        if (resp != null && resp.isSuccess()) {
            refreshGsLeftMoney(gs);
            ywCustomerGsMapper.updateById(gs);
            charge.setStatus(Constants.ONE);
            charge.setStatusInfo("充值成功");
            charge.setBalanceAfter(gs.getLeftMoney());
            ywElectricalChargeMapper.insert(charge);
            return "充值成功";
        }
        charge.setStatus(Constants.TWO);
        charge.setStatusInfo(apiMsg(resp, "充值失败"));
        ywElectricalChargeMapper.insert(charge);
        throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), charge.getStatusInfo());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String cleanConditionerAccount(Integer customerId, LoginUserInfo user) {
        YwCustomerGs gs = requireGsConfig(customerId);
        conditionerBizService.ensureLogin();
        CompanyGsManageRequest req = new CompanyGsManageRequest();
        req.fillSessionDefaults();
        req.setId(gs.getPlatformGsId());
        ConditionerBaseResponse<Object> resp = ConditionerUtil.cleanMoney(req);
        if (resp == null || !resp.isSuccess()) {
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), apiMsg(resp, "清零失败"));
        }
        refreshGsLeftMoney(gs);
        gs.setEditor(user.getId());
        gs.setEditDate(new Date());
        ywCustomerGsMapper.updateById(gs);
        return "清零成功";
    }
 
    @Override
    public PageData<YwCustomerRechargeRecordVO> findRechargeRecordPage(PageWrap<YwCustomerRechargeRecordQueryDTO> pageWrap) {
        Utils.MP.blankToNull(pageWrap.getModel());
        YwCustomerRechargeRecordQueryDTO query = pageWrap.getModel() != null ? pageWrap.getModel() : new YwCustomerRechargeRecordQueryDTO();
        IPage<YwCustomerRechargeRecordVO> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<YwElectricalCharge> wrapper = new MPJLambdaWrapper<YwElectricalCharge>()
                .selectAll(YwElectricalCharge.class)
                .selectAs(YwCustomer::getName, YwCustomerRechargeRecordVO::getCustomerName)
                .leftJoin(YwCustomer.class, YwCustomer::getId, YwElectricalCharge::getCustomerId)
                .isNotNull(YwElectricalCharge::getCustomerId)
                .eq(YwElectricalCharge::getIsdeleted, Constants.ZERO)
                .eq(query.getType() != null, YwElectricalCharge::getType, query.getType())
                .eq(query.getStatus() != null, YwElectricalCharge::getStatus, query.getStatus())
                .like(StringUtils.isNotBlank(query.getCustomerName()), YwCustomer::getName, query.getCustomerName())
                .ge(query.getCreateTimeBegin() != null, YwElectricalCharge::getCreateDate, query.getCreateTimeBegin())
                .le(query.getCreateTimeEnd() != null, YwElectricalCharge::getCreateDate, query.getCreateTimeEnd())
                .orderByDesc(YwElectricalCharge::getCreateDate);
        IPage<YwCustomerRechargeRecordVO> result = ywElectricalChargeMapper.selectJoinPage(page, YwCustomerRechargeRecordVO.class, wrapper);
        result.getRecords().forEach(this::fillRecordText);
        return PageData.from(result);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String retryRecharge(Integer id, LoginUserInfo user) {
        YwElectricalCharge charge = requireCharge(id);
        if (!Objects.equals(charge.getStatus(), Constants.TWO)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "仅失败记录可再次提交");
        }
        if (Objects.equals(charge.getType(), Constants.ZERO)) {
            YwCustomerRechargeElectricalDTO dto = new YwCustomerRechargeElectricalDTO();
            dto.setCustomerId(charge.getCustomerId());
            dto.setElectricalId(charge.getObjId());
            dto.setMoney(charge.getMoney());
            dto.setRemark(charge.getRemark());
            return rechargeElectrical(dto, user);
        }
        if (Objects.equals(charge.getType(), Constants.ONE)) {
            YwCustomerRechargeConditionerDTO dto = new YwCustomerRechargeConditionerDTO();
            dto.setCustomerId(charge.getCustomerId());
            dto.setMoney(charge.getMoney());
            dto.setRemark(charge.getRemark());
            return rechargeConditioner(dto, user);
        }
        throw new BusinessException(ResponseStatus.BAD_REQUEST);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String syncRechargeStatus(Integer id, LoginUserInfo user) {
        YwElectricalCharge charge = requireCharge(id);
        if (!Objects.equals(charge.getStatus(), Constants.ZERO)) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "仅充值中记录可同步");
        }
        if (Objects.equals(charge.getType(), Constants.ZERO)) {
            return syncElectricalChargeStatus(charge, user);
        }
        if (Objects.equals(charge.getType(), Constants.ONE)) {
            return syncConditionerChargeStatus(charge, user);
        }
        throw new BusinessException(ResponseStatus.BAD_REQUEST);
    }
 
    @Override
    public void exportRechargeRecord(PageWrap<YwCustomerRechargeRecordQueryDTO> pageWrap, HttpServletResponse response) {
        pageWrap.setPage(1);
        pageWrap.setCapacity(Integer.MAX_VALUE);
        List<YwCustomerRechargeRecordVO> records = findRechargeRecordPage(pageWrap).getRecords();
        ExcelExporter.build(YwCustomerRechargeRecordVO.class).export(records, "商户充值记录", response);
    }
 
    private String syncElectricalChargeStatus(YwElectricalCharge charge, LoginUserInfo user) {
        return ywElectricalBizService.syncAsyncActionStatus(charge.getOprId());
    }
 
    private String syncConditionerChargeStatus(YwElectricalCharge charge, LoginUserInfo user) {
        YwCustomerGs gs = requireGsConfig(charge.getCustomerId());
        conditionerBizService.ensureLogin();
        LogQueryRequest req = new LogQueryRequest();
        req.fillSessionDefaults();
        req.setPage(1);
        req.setPageSize(50);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (charge.getCreateDate() != null) {
            String day = sdf.format(charge.getCreateDate());
            req.setStart_time(day);
            req.setEnd_time(day);
        }
        ConditionerBaseResponse<List<Object>> resp = ConditionerUtil.getCzLog(req);
        if (resp == null || !resp.isSuccess() || CollectionUtils.isEmpty(resp.getData())) {
            return "未匹配到平台充值日志,仍为充值中";
        }
        for (Object row : resp.getData()) {
            JSONObject o = (JSONObject) JSON.toJSON(row);
            if (!matchCzLog(o, gs.getPlatformGsId(), charge)) {
                continue;
            }
            refreshGsLeftMoney(gs);
            ywCustomerGsMapper.updateById(gs);
            updateChargeStatus(charge.getId(), Constants.ONE, "充值成功", gs.getLeftMoney());
            return "已同步为充值成功";
        }
        return "未匹配到平台充值日志,仍为充值中";
    }
 
    private boolean matchCzLog(JSONObject o, Integer gsId, YwElectricalCharge charge) {
        Integer logGsId = o.getInteger("gs_id");
        if (logGsId == null) {
            logGsId = o.getInteger("id");
        }
        if (!Objects.equals(logGsId, gsId)) {
            return false;
        }
        String money = o.getString("cz_money");
        if (money == null) {
            money = o.getString("money");
        }
        if (charge.getMoney() != null && money != null) {
            try {
                return charge.getMoney().compareTo(new BigDecimal(money)) == 0;
            } catch (NumberFormatException ignored) {
            }
        }
        return true;
    }
 
    private void updateChargeStatus(Integer id, int status, String statusInfo, BigDecimal balanceAfter) {
        UpdateWrapper<YwElectricalCharge> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(YwElectricalCharge::getStatus, status)
                .set(YwElectricalCharge::getStatusInfo, statusInfo)
                .set(YwElectricalCharge::getStatusTime, new Date())
                .set(YwElectricalCharge::getEditDate, new Date())
                .eq(YwElectricalCharge::getId, id);
        if (balanceAfter != null) {
            uw.lambda().set(YwElectricalCharge::getBalanceAfter, balanceAfter);
        }
        ywElectricalChargeMapper.update(null, uw);
    }
 
    private YwElectricalCharge buildConditionerCharge(YwCustomerRechargeConditionerDTO dto, YwCustomerGs gs, LoginUserInfo user) {
        YwCustomer customer = requireCustomer(dto.getCustomerId());
        YwElectricalCharge charge = new YwElectricalCharge();
        charge.setCreator(user.getId());
        charge.setCreateDate(new Date());
        charge.setEditor(user.getId());
        charge.setEditDate(new Date());
        charge.setIsdeleted(Constants.ZERO);
        charge.setType(Constants.ONE);
        charge.setCustomerId(dto.getCustomerId());
        charge.setObjId(gs.getId());
        charge.setName(customer.getName());
        charge.setMoney(dto.getMoney());
        charge.setRemark(dto.getRemark());
        charge.setBanlance(gs.getLeftMoney());
        charge.setStatus(Constants.ZERO);
        charge.setStatusTime(new Date());
        charge.setStatusInfo("充值中");
        charge.setDeviceInfo("GS-" + gs.getPlatformGsId() + " " + customer.getName());
        return charge;
    }
 
    private void fillRecordText(YwCustomerRechargeRecordVO vo) {
        if (StringUtils.isBlank(vo.getDeviceInfo())) {
            if (StringUtils.isNotBlank(vo.getAddress()) || StringUtils.isNotBlank(vo.getName())) {
                vo.setDeviceInfo(StringUtils.defaultString(vo.getAddress()) + " " + StringUtils.defaultString(vo.getName()));
            }
        }
        vo.setTypeText(Objects.equals(vo.getType(), Constants.ONE) ? "空调" : "电表");
        if (Objects.equals(vo.getStatus(), Constants.ZERO)) {
            vo.setStatusText("充值中");
        } else if (Objects.equals(vo.getStatus(), Constants.ONE)) {
            vo.setStatusText("充值成功");
        } else if (Objects.equals(vo.getStatus(), Constants.TWO)) {
            vo.setStatusText("充值失败");
        }
    }
 
    private YwElectricalCharge requireCharge(Integer id) {
        YwElectricalCharge charge = ywElectricalChargeMapper.selectById(id);
        if (charge == null || Objects.equals(charge.getIsdeleted(), Constants.ONE) || charge.getCustomerId() == null) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        return charge;
    }
 
    private void refreshGsLeftMoney(YwCustomerGs gs) {
        if (gs.getPlatformGsId() == null) {
            return;
        }
        CompanyGsManageRequest req = new CompanyGsManageRequest();
        req.fillSessionDefaults();
        req.setId(gs.getPlatformGsId());
        ConditionerBaseResponse<List<CompanyGsInfoResponse>> resp = ConditionerUtil.getGs(req);
        if (resp == null || !resp.isSuccess() || CollectionUtils.isEmpty(resp.getData())) {
            return;
        }
        CompanyGsInfoResponse info = resp.getData().stream()
                .filter(i -> Objects.equals(i.getId(), gs.getPlatformGsId()))
                .findFirst().orElse(resp.getData().get(0));
        gs.setLeftMoney(toBigDecimal(info.getLeft_money()));
        gs.setSyncDate(new Date());
    }
 
    private Integer parseAddGsId(Object data) {
        if (data == null) {
            return null;
        }
        if (data instanceof Number) {
            return ((Number) data).intValue();
        }
        JSONObject obj = (JSONObject) JSON.toJSON(data);
        if (obj.containsKey("id")) {
            return obj.getInteger("id");
        }
        return null;
    }
 
    private BigDecimal toBigDecimal(Object val) {
        if (val == null) {
            return null;
        }
        if (val instanceof BigDecimal) {
            return (BigDecimal) val;
        }
        if (val instanceof Number) {
            return BigDecimal.valueOf(((Number) val).doubleValue());
        }
        try {
            return new BigDecimal(String.valueOf(val));
        } catch (NumberFormatException e) {
            return null;
        }
    }
 
    private void validateGsConfigRequired(YwCustomerGsConfigDTO dto) {
        if (dto.getStopMoney() == null) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "欠费额度不能为空");
        }
        if (dto.getStopMoney().compareTo(BigDecimal.ZERO) < 0) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "欠费额度不能小于0");
        }
        if (dto.getIsPwr() == null) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "计费开关不能为空");
        }
        if (dto.getIsRestStop() == null) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "是否停机不能为空");
        }
        if (CollectionUtils.isEmpty(dto.getConditioners())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "请至少关联一台空调内机");
        }
    }
 
    private String apiMsg(ConditionerBaseResponse<?> resp, String def) {
        return resp != null && StringUtils.isNotBlank(resp.getMessage()) ? resp.getMessage() : def;
    }
 
    private YwCustomer requireCustomer(Integer customerId) {
        YwCustomer c = ywCustomerMapper.selectById(customerId);
        if (c == null || Objects.equals(c.getIsdeleted(), Constants.ONE)) {
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        return c;
    }
 
    private YwCustomerGs requireGsConfig(Integer customerId) {
        YwCustomerGs gs = getCustomerGsConfig(customerId);
        if (gs == null || gs.getPlatformGsId() == null) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "请先配置并保存空调关联");
        }
        return gs;
    }
 
    private void assertElectricalBound(Integer customerId, Integer electricalId) {
        Long cnt = ywCustomerElectricalMapper.selectCount(new QueryWrapper<YwCustomerElectrical>().lambda()
                .eq(YwCustomerElectrical::getCustomerId, customerId)
                .eq(YwCustomerElectrical::getElectricalId, electricalId)
                .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO));
        if (cnt == null || cnt == 0) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "电表未关联该商户");
        }
    }
 
    private List<Integer> listBoundElectricalIds(Integer customerId) {
        return ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda()
                        .eq(YwCustomerElectrical::getCustomerId, customerId)
                        .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO))
                .stream().map(YwCustomerElectrical::getElectricalId).collect(Collectors.toList());
    }
 
    private Set<Integer> listAllBoundElectricalIds() {
        return ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda()
                        .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO))
                .stream().map(YwCustomerElectrical::getElectricalId).collect(Collectors.toSet());
    }
 
    private Set<Integer> listAllBoundElectricalIdsExcept(Integer customerId) {
        return ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda()
                        .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO)
                        .ne(YwCustomerElectrical::getCustomerId, customerId))
                .stream().map(YwCustomerElectrical::getElectricalId).collect(Collectors.toSet());
    }
 
    private List<YwElectrical> loadCustomerElectricalList(Integer customerId) {
        List<Integer> ids = listBoundElectricalIds(customerId);
        if (ids.isEmpty()) {
            return Collections.emptyList();
        }
        List<YwElectrical> list = ywElectricalMapper.selectBatchIds(ids);
        list = list.stream().filter(e -> !Objects.equals(e.getIsdeleted(), Constants.ONE)).collect(Collectors.toList());
        ywElectricalBizService.enrichList(list);
        return list;
    }
 
    private List<YwConditioner> loadCustomerConditionerList(Integer customerId) {
        List<YwCustomerConditioner> rels = ywCustomerConditionerMapper.selectList(new QueryWrapper<YwCustomerConditioner>().lambda()
                .eq(YwCustomerConditioner::getCustomerId, customerId)
                .eq(YwCustomerConditioner::getIsdeleted, Constants.ZERO));
        if (rels.isEmpty()) {
            return Collections.emptyList();
        }
        Map<Integer, Integer> ratioMap = rels.stream()
                .collect(Collectors.toMap(YwCustomerConditioner::getConditionerId, YwCustomerConditioner::getDevRatio, (a, b) -> a));
        List<YwConditioner> list = ywConditionerMapper.selectBatchIds(ratioMap.keySet());
        list = list.stream().filter(c -> !Objects.equals(c.getIsdeleted(), Constants.ONE)).collect(Collectors.toList());
        for (YwConditioner c : list) {
            c.setDevRatio(ratioMap.get(c.getId()));
        }
        return list;
    }
 
    private PageData<YwElectrical> pageElectricalByIds(PageWrap<YwElectrical> pageWrap, List<Integer> ids) {
        IPage<YwElectrical> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        YwElectrical model = pageWrap.getModel();
        QueryWrapper<YwElectrical> qw = new QueryWrapper<>();
        qw.lambda()
                .in(YwElectrical::getId, ids)
                .eq(YwElectrical::getIsdeleted, Constants.ZERO)
                .and(model != null && StringUtils.isNotBlank(model.getName()), w -> w
                        .like(YwElectrical::getName, model.getName())
                        .or().like(YwElectrical::getAddress, model.getName()))
                .orderByDesc(YwElectrical::getCreateDate);
        IPage<YwElectrical> result = ywElectricalMapper.selectPage(page, qw);
        ywElectricalBizService.enrichList(result.getRecords());
        return PageData.from(result);
    }
 
    private <T> PageData<T> emptyPage(PageWrap<?> pageWrap) {
        PageData<T> data = new PageData<>();
        data.setRecords(Collections.emptyList());
        data.setTotal(0);
        data.setPage(pageWrap.getPage());
        data.setCapacity(pageWrap.getCapacity());
        return data;
    }
}