lin
2024-04-23 2d39bcf8a8421012ed95062a711b8814114f7cf4
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.house.service.impl;
 
import cn.hutool.core.util.IdcardUtil;
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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.core.server.EmbedServer;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.cache.SysCache;
import org.springblade.common.param.CommonParamSet;
import org.springblade.common.utils.IdUtils;
import org.springblade.common.utils.NodeTreeUtil;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.doorplateAddress.entity.DoorplateAddressEntity;
import org.springblade.modules.doorplateAddress.service.IDoorplateAddressService;
import org.springblade.modules.grid.entity.GridEntity;
import org.springblade.modules.grid.entity.GridRangeEntity;
import org.springblade.modules.grid.service.IGridRangeService;
import org.springblade.modules.grid.service.IGridService;
import org.springblade.modules.house.entity.HouseEntity;
import org.springblade.modules.house.entity.HouseholdEntity;
import org.springblade.modules.house.entity.UserHouseLabelEntity;
import org.springblade.modules.house.excel.HouseAndHoldExcel;
import org.springblade.modules.house.excel.HouseExcel;
import org.springblade.modules.house.mapper.HouseMapper;
import org.springblade.modules.house.service.IHouseService;
import org.springblade.modules.house.service.IHouseholdService;
import org.springblade.modules.house.service.IUserHouseLabelService;
import org.springblade.modules.house.vo.HouseParam;
import org.springblade.modules.house.vo.HouseTree;
import org.springblade.modules.house.vo.HouseVO;
import org.springblade.modules.label.entity.LabelEntity;
import org.springblade.modules.label.service.ILabelService;
import org.springblade.modules.label.vo.LabelVO;
import org.springblade.modules.place.excel.PlaceExcel;
import org.springblade.modules.place.vo.PlaceVO;
import org.springblade.modules.police.entity.PoliceAffairsGridEntity;
import org.springblade.modules.police.service.IPoliceAffairsGridService;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IRegionService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 房屋 服务实现类
 *
 * @author BladeX
 * @since 2023-10-28
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, HouseEntity> implements IHouseService {
 
 
    private static final Logger logger = LoggerFactory.getLogger(HouseServiceImpl.class);
 
    @Autowired
    private IGridService gridService;
 
    @Autowired
    private IHouseholdService householdService;
 
    @Autowired
    private IGridRangeService gridRangeService;
 
    @Autowired
    private IUserService userService;
 
    @Autowired
    private IRegionService regionService;
 
    @Override
    public IPage<HouseVO> selectHousePage(IPage<HouseVO> page, HouseVO house) {
//        List<String> regionChildCodesList = SysCache.getRegionChildCodesByDeptId(AuthUtil.getDeptId());
//        Integer isAdministrator = AuthUtil.isAdministrator() == true ? 1 : 2;
        CommonParamSet commonParamSet = new CommonParamSet().invoke(HouseVO.class, house);
 
        List<HouseVO> houseVOS = baseMapper.selectHousePage(page, house,
            commonParamSet.getRegionChildCodesList(), commonParamSet.getGridCodeList(), commonParamSet.getIsAdministrator()
        );
        return page.setRecords(houseVOS);
    }
 
    /**
     * 房屋自定义详情查询
     *
     * @param house
     * @return
     */
    @Override
    public HouseVO getHouseDetail(HouseVO house) {
        if (ObjectUtils.isEmpty(house)) {
            logger.error("house是空值", house);
            return new HouseVO();
        }
        if (StringUtils.isBlank(house.getHouseCode()) && ObjectUtils.isEmpty(house.getId())) {
            logger.error("house.HouseCode是空值", house);
            return new HouseVO();
        }
        return baseMapper.getHouseDetail(house);
    }
 
    /**
     * 房屋自定义新增或修改
     *
     * @param house
     * @return
     */
    @Override
    public boolean saveOrUpdateHouse(HouseEntity house) {
        boolean flag = false;
        // 如果没有房屋编号,自己生成
        if (!Strings.isBlank(house.getHouseCode())) {
            // 查询是否已存在房屋数据
            QueryWrapper<HouseEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("house_code", house.getHouseCode());
            HouseEntity one = getOne(wrapper);
            if (null != one) {
                house.setId(one.getId());
                // 更新数据
                return updateById(house);
            }
        } else {
            //自己生成编号
            // 设置来源( 1:地址总表  2:国控采集)
            house.setSource(2);
            // 并生成36位的houseCode
            house.setHouseCode(IdUtils.getIdBy36());
        }
        //插入数据
        flag = save(house);
        // 设置网格绑定数据
        gridBind(house);
        // 返回
        return flag;
    }
 
    /**
     * 网格绑定
     *
     * @param house
     */
    public void gridBind(HouseEntity house) {
        if (null != house.getGridId()) {
            // 判断关联关系表是否存在
            QueryWrapper<GridRangeEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("grid_id", house.getGridId()).eq("house_code", house.getHouseCode());
            GridRangeEntity one = gridRangeService.getOne(wrapper);
            if (null == one) {
                // 新增
                GridRangeEntity gridRangeEntity = new GridRangeEntity();
                gridRangeEntity.setHouseCode(house.getHouseCode());
                gridRangeEntity.setGridId(house.getGridId());
                // 插入
                gridRangeService.save(gridRangeEntity);
            }
        }
    }
 
 
    /**
     * 导入房屋数据
     *
     * @param data
     * @param isCovered
     */
    @Override
    public void importUserHouse(List<HouseExcel> data, Boolean isCovered) {
        data.forEach(houseExcel -> {
            HouseEntity HouseEntity = Objects.requireNonNull(BeanUtil.copy(houseExcel, HouseEntity.class));
            this.save(HouseEntity);
        });
    }
 
    @Override
    public List<HouseExcel> export(HouseVO household) {
        List<HouseExcel> houseExcels = baseMapper.export(household);
        return houseExcels;
    }
 
    /**
     * 查询房屋树
     *
     * @param houseParam
     * @return
     */
    @Override
    public List<HouseTree> getHouseTree(HouseParam houseParam) {
        List<String> houseCodeList = getHouseCodeList(houseParam);
        return NodeTreeUtil.getHouseTree(baseMapper.getHouseTree(houseParam, houseCodeList));
    }
 
 
    /**
     * 根据角色获取地址编号集合
     *
     * @param houseParam
     * @return
     */
    private List<String> getHouseCodeList(HouseParam houseParam) {
        List<String> stringList = new ArrayList<>();
        if (null != houseParam.getRoleName() && !houseParam.getRoleName().equals("")) {
            if (houseParam.getRoleName().equals("网格员")) {
                // 查询对应的房屋地址code
                stringList = gridService.getAddressCodeListByUserId(AuthUtil.getUserId(), null);
            }
        }
        return stringList;
    }
 
    /**
     * 人房数据导入
     *
     * @param data
     * @param isCovered
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importHouseAndHold(List<HouseAndHoldExcel> data, Boolean isCovered) {
        for (HouseAndHoldExcel houseAndHoldExcel : data) {
            // System.out.println(houseAndHoldExcel);
            System.out.println("houseAndHoldExcel = " + houseAndHoldExcel);
            // 保存房屋数据--一个一个插入,防止一个表格中存在多个地址编号相同的数据
            saveHouseData(houseAndHoldExcel);
            // 保存住户数据(包含标签)--一个一个插入,防止一个表格中存在多个地址编号相同的数据
            saveHouseholdData(houseAndHoldExcel);
            // 保存租户数据
        }
    }
 
    /**
     * 保存房屋数据
     *
     * @param houseAndHoldExcel
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveHouseData(HouseAndHoldExcel houseAndHoldExcel) {
        IDoorplateAddressService doorplateAddressService = SpringUtils.getBean(IDoorplateAddressService.class);
        // 查询库中是否已存在
        QueryWrapper<HouseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("house_code", houseAndHoldExcel.getHouseCode())
            .eq("is_deleted", 0);
        HouseEntity one = getOne(wrapper);
        // 不存在则插入,存在则不操作
        if (null == one) {
            HouseEntity houseEntity = new HouseEntity();
            houseEntity.setHouseCode(houseAndHoldExcel.getHouseCode());
            if (Strings.isBlank(houseAndHoldExcel.getHouseName())) {
                // 查询地址总表对应的数据
                QueryWrapper<DoorplateAddressEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("address_code", houseAndHoldExcel.getHouseCode());
                DoorplateAddressEntity addressEntity = doorplateAddressService.getOne(queryWrapper);
                if (null != addressEntity) {
                    houseEntity.setHouseName(addressEntity.getAddressName());
                    houseEntity.setAddress(addressEntity.getAddressName());
                    houseEntity.setDistrictCode(addressEntity.getAoiCode());
                    houseEntity.setDistrictName(addressEntity.getAoiName());
                    houseEntity.setLng(addressEntity.getX());
                    houseEntity.setLat(addressEntity.getY());
                }
            } else {
                houseEntity.setHouseName(houseAndHoldExcel.getHouseName());
                houseEntity.setAddress(houseAndHoldExcel.getHouseName());
            }
            if (!Strings.isBlank(houseAndHoldExcel.getDistrictName())) {
                houseEntity.setDistrictName(houseAndHoldExcel.getDistrictName());
            }
            if (!Strings.isBlank(houseAndHoldExcel.getUnit())) {
                houseEntity.setUnit(houseAndHoldExcel.getUnit());
            }
            if (!Strings.isBlank(houseAndHoldExcel.getFloor())) {
                houseEntity.setFloor(houseAndHoldExcel.getFloor());
            }
            houseEntity.setRoom(houseAndHoldExcel.getRoom());
            houseEntity.setBuilding(houseAndHoldExcel.getBuilding());
            houseEntity.setArea(houseAndHoldExcel.getArea());
            houseEntity.setPropertyPrice(houseAndHoldExcel.getPropertyPrice());
            if (!Strings.isBlank(houseAndHoldExcel.getServiceDue())) {
                try {
                    Date date = new SimpleDateFormat("yyyy-MM-dd").parse(houseAndHoldExcel.getServiceDue());
                    houseEntity.setServiceDue(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            houseEntity.setRemark(houseAndHoldExcel.getRemark());
            houseEntity.setCreateTime(new Date());
            houseEntity.setCreateUser(AuthUtil.getUserId().toString());
            houseEntity.setUpdateTime(new Date());
            houseEntity.setUpdateUser(AuthUtil.getUserId().toString());
            if (!Strings.isBlank(houseAndHoldExcel.getHouseCode())) {
                houseEntity.setSource(1);
            } else {
                houseEntity.setHouseCode(IdUtils.getIdBy36());
                houseEntity.setSource(2);
            }
            // 网格处理
            importGridHandle(houseAndHoldExcel, houseEntity);
            // 新增
            save(houseEntity);
        } else {
            // 更新
            if (Strings.isBlank(houseAndHoldExcel.getHouseName())) {
                // 查询地址总表对应的数据
                QueryWrapper<DoorplateAddressEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("address_code", houseAndHoldExcel.getHouseCode());
                DoorplateAddressEntity addressEntity = doorplateAddressService.getOne(queryWrapper);
                if (null != addressEntity) {
                    one.setHouseName(addressEntity.getAddressName());
                    one.setAddress(addressEntity.getAddressName());
                    one.setDistrictCode(addressEntity.getAoiCode());
                    one.setDistrictName(addressEntity.getAoiName());
                    one.setLng(addressEntity.getX());
                    one.setLat(addressEntity.getY());
                }
            } else {
                one.setHouseName(houseAndHoldExcel.getHouseName());
                one.setAddress(houseAndHoldExcel.getHouseName());
            }
            if (!Strings.isBlank(houseAndHoldExcel.getDistrictName())) {
                one.setDistrictName(houseAndHoldExcel.getDistrictName());
            }
            if (!Strings.isBlank(houseAndHoldExcel.getUnit())) {
                one.setUnit(houseAndHoldExcel.getUnit());
            }
            if (!Strings.isBlank(houseAndHoldExcel.getFloor())) {
                one.setFloor(houseAndHoldExcel.getFloor());
            }
            one.setRoom(houseAndHoldExcel.getRoom());
            one.setBuilding(houseAndHoldExcel.getBuilding());
            one.setArea(houseAndHoldExcel.getArea());
            one.setPropertyPrice(houseAndHoldExcel.getPropertyPrice());
            // 网格处理
            importGridHandle(houseAndHoldExcel, one);
            // 更新
            updateById(one);
        }
    }
 
    /**
     * 保存住户数据
     *
     * @param houseAndHoldExcel
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveHouseholdData(HouseAndHoldExcel houseAndHoldExcel) {
        // 查询库中是否已存在
        QueryWrapper<HouseholdEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("house_code", houseAndHoldExcel.getHouseCode())
            .eq("is_deleted", 0)
            .eq("phone_number", houseAndHoldExcel.getPhoneNumber())
            .eq("name", houseAndHoldExcel.getName());
        HouseholdEntity one = householdService.getOne(wrapper);
        // 不存在则插入,存在则不操作
        if (null == one) {
            HouseholdEntity householdEntity = new HouseholdEntity();
            householdEntity.setHouseCode(houseAndHoldExcel.getHouseCode());
            householdEntity.setName(houseAndHoldExcel.getName());
            householdEntity.setPhoneNumber(houseAndHoldExcel.getPhoneNumber());
            // 与角色关系
            if (!Strings.isBlank(houseAndHoldExcel.getRelationship())) {
                householdEntity.setRelationship(Integer.parseInt(houseAndHoldExcel.getRelationship()));
                // 如果是业主,则需要往用户表插入用户
                if (householdEntity.getRelationship() == 1) {
                    saveOrUpdateUser(householdEntity);
                }
            }
            /// 是否主要联系人
            if (!Strings.isBlank(houseAndHoldExcel.getIsPrimaryContact())) {
                householdEntity.setIsPrimaryContact(Integer.parseInt(houseAndHoldExcel.getIsPrimaryContact()));
            }
            // 居住状态
            if (!Strings.isBlank(houseAndHoldExcel.getResidentialStatus())) {
                householdEntity.setResidentialStatus(Integer.parseInt(houseAndHoldExcel.getResidentialStatus()));
            }
            // 性别
            if (!Strings.isBlank(houseAndHoldExcel.getGender())) {
                householdEntity.setGender(Short.parseShort(houseAndHoldExcel.getGender()));
            }
            // 居民身份证
            if (!Strings.isBlank(houseAndHoldExcel.getIdCard())) {
                if (IdcardUtil.isValidCard(houseAndHoldExcel.getIdCard())) {
                    // 身份证类型为居民身份证
                    householdEntity.setCardType(111);
                    householdEntity.setIdCard(houseAndHoldExcel.getIdCard());
                } else {
                    householdEntity.setIdCard(houseAndHoldExcel.getIdCard());
                }
            }
            // 党员
            if (!Strings.isBlank(houseAndHoldExcel.getPartyEmber())) {
                householdEntity.setPartyEmber(Integer.parseInt(houseAndHoldExcel.getPartyEmber()));
            }
            // householdEntity.setHkmtPass(houseAndHoldExcel.getHkmtPass());
            // householdEntity.setPassport(houseAndHoldExcel.getPassport());
            // 民族
            if (!Strings.isBlank(houseAndHoldExcel.getEthnicity())) {
                householdEntity.setEthnicity(Integer.parseInt(houseAndHoldExcel.getEthnicity()));
            }
            // 学历
            if (!Strings.isBlank(houseAndHoldExcel.getEducation())) {
                householdEntity.setEducation(Integer.parseInt(houseAndHoldExcel.getEducation()));
            }
            // 户籍类型
            if (!Strings.isBlank(houseAndHoldExcel.getResidentType())) {
                householdEntity.setResidentType(Integer.parseInt(houseAndHoldExcel.getResidentType().trim()));
            }
            // 户籍地区县
            if (!Strings.isBlank(houseAndHoldExcel.getResidentAdcode())) {
                String adCode = shiftResidentResidentAdCode(houseAndHoldExcel.getResidentProvinceAdcode(),
                    houseAndHoldExcel.getResidentCityAdcode(),
                    houseAndHoldExcel.getResidentAdcode());
                // 转换行政区code
                houseAndHoldExcel.setResidentAdcode(adCode);
            }
            householdEntity.setHukouRegistration(houseAndHoldExcel.getHukouRegistration());
            // 籍贯地区县
            if (!Strings.isBlank(houseAndHoldExcel.getNativePlaceAdcode())) {
                String adCode = shiftResidentResidentAdCode(null,
                    null,
                    houseAndHoldExcel.getNativePlaceAdcode());
                // 转换行政区code
                houseAndHoldExcel.setNativePlaceAdcode(adCode);
            }
            // 健康状况
            if (!Strings.isBlank(houseAndHoldExcel.getHealthStatus())) {
                householdEntity.setHealthStatus(Integer.parseInt(houseAndHoldExcel.getHealthStatus()));
            }
            householdEntity.setDiseaseName(houseAndHoldExcel.getDiseaseName());
            householdEntity.setReligiousBelief(houseAndHoldExcel.getReligiousBelief());
            // 工作状态
            if (!Strings.isBlank(houseAndHoldExcel.getWorkStatus())) {
                householdEntity.setWorkStatus(Integer.parseInt(houseAndHoldExcel.getWorkStatus()));
            }
            householdEntity.setEmployer(houseAndHoldExcel.getEmployer());
            householdEntity.setOccupation(houseAndHoldExcel.getOccupation());
            householdEntity.setCmpyRegAddr(houseAndHoldExcel.getCmpyRegAddr());
            // 外出
            householdEntity.setGoOutReason(houseAndHoldExcel.getGoOutReason());
            if (!Strings.isBlank(houseAndHoldExcel.getGoOutTime())) {
                try {
                    Date date = new SimpleDateFormat("yyyy-MM-dd").parse(houseAndHoldExcel.getGoOutTime());
                    householdEntity.setGoOutTime(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            householdEntity.setGoOutWhere(houseAndHoldExcel.getGoOutWhere());
            householdEntity.setGoOutAddr(houseAndHoldExcel.getGoOutAddr());
            // 婚姻状态
            if (!Strings.isBlank(houseAndHoldExcel.getMaritalStatus())) {
                householdEntity.setMaritalStatus(Integer.parseInt(houseAndHoldExcel.getMaritalStatus()));
            }
            // 车牌号
            householdEntity.setCardNumber(houseAndHoldExcel.getCardNumber());
            // 其他联系方式
            householdEntity.setOtherContact(houseAndHoldExcel.getOtherContact());
            if (!Strings.isBlank(householdEntity.getHomeAdcode())) {
                // 暂时不处理,导入数据目前都有house_code
                String adCode = shiftResidentHomeAdcode(houseAndHoldExcel.getHomeAdcode());
                // 转换行政区code
                houseAndHoldExcel.setHomeAdcode(adCode);
            }
            householdEntity.setCurrentAddress(houseAndHoldExcel.getCurrentAddress());
            householdEntity.setDisabilityCert(houseAndHoldExcel.getDisabilityCert());
            householdEntity.setRemark(houseAndHoldExcel.getRemarks());
            householdEntity.setCreateTime(new Date());
            householdEntity.setCreateUser(AuthUtil.getUserId());
            householdEntity.setUpdateTime(new Date());
            householdEntity.setUpdateUser(AuthUtil.getUserId());
            // 新增
            boolean save = householdService.save(householdEntity);
            if (save) {
                householdLabelHandle(houseAndHoldExcel, householdEntity);
            }
        } else {
            // 更新
            one.setHouseCode(houseAndHoldExcel.getHouseCode());
            one.setName(houseAndHoldExcel.getName());
            one.setPhoneNumber(houseAndHoldExcel.getPhoneNumber());
            // 与角色关系
            if (!Strings.isBlank(houseAndHoldExcel.getRelationship())) {
                one.setRelationship(Integer.parseInt(houseAndHoldExcel.getRelationship()));
                // 如果是业主,则需要往用户表插入用户
                if (one.getRelationship() == 1) {
                    saveOrUpdateUser(one);
                }
            }
            /// 是否主要联系人
            if (!Strings.isBlank(houseAndHoldExcel.getIsPrimaryContact())) {
                one.setIsPrimaryContact(Integer.parseInt(houseAndHoldExcel.getIsPrimaryContact()));
            }
            // 居住状态
            if (!Strings.isBlank(houseAndHoldExcel.getResidentialStatus())) {
                one.setResidentialStatus(Integer.parseInt(houseAndHoldExcel.getResidentialStatus()));
            }
            // 性别
            if (!Strings.isBlank(houseAndHoldExcel.getGender())) {
                one.setGender(Short.parseShort(houseAndHoldExcel.getGender()));
            }
            // 居民身份证
            if (!Strings.isBlank(houseAndHoldExcel.getIdCard())) {
                if (IdcardUtil.isValidCard(houseAndHoldExcel.getIdCard())) {
                    // 身份证类型为居民身份证
                    one.setCardType(111);
                    one.setIdCard(houseAndHoldExcel.getIdCard());
                } else {
                    one.setIdCard(houseAndHoldExcel.getIdCard());
                }
            }
            // 党员
            if (!Strings.isBlank(houseAndHoldExcel.getPartyEmber())) {
                one.setPartyEmber(Integer.parseInt(houseAndHoldExcel.getPartyEmber()));
            }
            // 民族
            if (!Strings.isBlank(houseAndHoldExcel.getEthnicity())) {
                one.setEthnicity(Integer.parseInt(houseAndHoldExcel.getEthnicity()));
            }
            // 学历
            if (!Strings.isBlank(houseAndHoldExcel.getEducation())) {
                one.setEducation(Integer.parseInt(houseAndHoldExcel.getEducation()));
            }
            // 户籍类型
            if (!Strings.isBlank(houseAndHoldExcel.getResidentType())) {
                one.setResidentType(Integer.parseInt(houseAndHoldExcel.getResidentType().trim()));
            }
            // 户籍地区县
            if (!Strings.isBlank(houseAndHoldExcel.getResidentAdcode())) {
                String adCode = shiftResidentResidentAdCode(houseAndHoldExcel.getResidentProvinceAdcode(),
                    houseAndHoldExcel.getResidentCityAdcode(),
                    houseAndHoldExcel.getResidentAdcode());
                // 转换行政区code
                houseAndHoldExcel.setResidentAdcode(adCode);
            }
            one.setHukouRegistration(houseAndHoldExcel.getHukouRegistration());
            // 籍贯地区县
            if (!Strings.isBlank(houseAndHoldExcel.getNativePlaceAdcode())) {
                String adCode = shiftResidentResidentAdCode(null,
                    null,
                    houseAndHoldExcel.getNativePlaceAdcode());
                // 转换行政区code
                houseAndHoldExcel.setNativePlaceAdcode(adCode);
            }
            // 健康状况
            if (!Strings.isBlank(houseAndHoldExcel.getHealthStatus())) {
                one.setHealthStatus(Integer.parseInt(houseAndHoldExcel.getHealthStatus()));
            }
            one.setDiseaseName(houseAndHoldExcel.getDiseaseName());
            one.setReligiousBelief(houseAndHoldExcel.getReligiousBelief());
            // 工作状态
            if (!Strings.isBlank(houseAndHoldExcel.getWorkStatus())) {
                one.setWorkStatus(Integer.parseInt(houseAndHoldExcel.getWorkStatus()));
            }
            one.setEmployer(houseAndHoldExcel.getEmployer());
            one.setOccupation(houseAndHoldExcel.getOccupation());
            one.setCmpyRegAddr(houseAndHoldExcel.getCmpyRegAddr());
            one.setGoOutReason(houseAndHoldExcel.getGoOutReason());
            if (!Strings.isBlank(houseAndHoldExcel.getGoOutTime())) {
                try {
                    Date date = new SimpleDateFormat("yyyy-MM-dd").parse(houseAndHoldExcel.getGoOutTime());
                    one.setGoOutTime(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            one.setGoOutWhere(houseAndHoldExcel.getGoOutWhere());
            one.setGoOutAddr(houseAndHoldExcel.getGoOutAddr());
            // 婚姻状态
            if (!Strings.isBlank(houseAndHoldExcel.getMaritalStatus())) {
                one.setMaritalStatus(Integer.parseInt(houseAndHoldExcel.getMaritalStatus()));
            }
            one.setCardNumber(houseAndHoldExcel.getCardNumber());
            one.setOtherContact(houseAndHoldExcel.getOtherContact());
            if (!Strings.isBlank(one.getHomeAdcode())) {
                // 暂时不处理,导入数据目前都有house_code
                String adCode = shiftResidentHomeAdcode(houseAndHoldExcel.getHomeAdcode());
                // 转换行政区code
                houseAndHoldExcel.setHomeAdcode(adCode);
            }
            one.setCurrentAddress(houseAndHoldExcel.getCurrentAddress());
            one.setDisabilityCert(houseAndHoldExcel.getDisabilityCert());
            one.setRemark(houseAndHoldExcel.getRemarks());
            one.setUpdateTime(new Date());
            one.setUpdateUser(AuthUtil.getUserId());
            // 新增
            boolean update = householdService.updateById(one);
            if (update) {
                // 住户标签处理
                householdLabelHandle(houseAndHoldExcel, one);
            }
        }
    }
 
 
    /**
     * 导入是网格数据绑定处理
     *
     * @param houseAndHoldExcel
     * @param houseEntity
     */
    public void importGridHandle(HouseAndHoldExcel houseAndHoldExcel, HouseEntity houseEntity) {
        if (!Strings.isBlank(houseAndHoldExcel.getCommunityName()) &&
            !Strings.isBlank(houseAndHoldExcel.getGridName())) {
            // 查询对应的网格信息
            GridEntity gridEntity = gridService.getGridInfoByParam(houseAndHoldExcel.getCommunityName(), houseAndHoldExcel.getGridName());
            if (null != gridEntity) {
                // 设置场所范围
                houseEntity.setGridId(gridEntity.getId());
                houseEntity.setGridCode(gridEntity.getGridCode());
                // 更新网格范围绑定
                GridRangeEntity gridRangeEntity = new GridRangeEntity();
                gridRangeEntity.setGridId(gridEntity.getId());
                gridRangeEntity.setGridCode(gridEntity.getGridCode());
                gridRangeEntity.setHouseCode(houseEntity.getHouseCode());
                UpdateWrapper<GridRangeEntity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("house_code", houseEntity.getHouseCode());
                gridRangeService.update(gridRangeEntity, updateWrapper);
            }
        }
    }
 
    /**
     * 住户标签处理
     *
     * @param houseAndHoldExcel
     * @param householdEntity
     */
    public void householdLabelHandle(HouseAndHoldExcel houseAndHoldExcel, HouseholdEntity householdEntity) {
        String labelId = houseAndHoldExcel.getLabelId();
        if (!StringUtils.isBlank(labelId)) {
            String[] split = labelId.split(",");
            IUserHouseLabelService bean = SpringUtils.getBean(IUserHouseLabelService.class);
            ILabelService bean1 = SpringUtils.getBean(ILabelService.class);
            for (String s : split) {
                // 查询标签信息
                LabelEntity one1 = bean1.getOne(Wrappers.<LabelEntity>lambdaQuery().eq(LabelEntity::getLabelName, s));
                if (one1 != null) {
                    // 判断是否已存在关联关系,没有则新增
                    QueryWrapper<UserHouseLabelEntity> wrapper = new QueryWrapper<>();
                    wrapper.eq("label_id", one1.getId())
                        .eq("house_code", householdEntity.getHouseCode())
                        .eq("household_id", householdEntity.getId());
                    UserHouseLabelEntity houseLabelEntity = bean.getOne(wrapper);
                    if (null == houseLabelEntity) {
                        UserHouseLabelEntity userHouseLabelEntity = new UserHouseLabelEntity();
                        userHouseLabelEntity.setLabelId(BigDecimal.valueOf(one1.getId()).longValue());
                        userHouseLabelEntity.setHouseholdId(householdEntity.getId());
                        // 设置默认的绿色
                        userHouseLabelEntity.setColor("green");
                        userHouseLabelEntity.setLableType(1);
                        userHouseLabelEntity.setLabelName(s);
                        userHouseLabelEntity.setHouseCode(houseAndHoldExcel.getHouseCode());
                        bean.save(userHouseLabelEntity);
                    }
                }
            }
        }
    }
 
    /**
     * 住户业主信息处理,将业主人员插入到用户表
     *
     * @return
     */
    public void userHandle(HouseholdEntity householdEntity) {
        if (!Strings.isBlank(householdEntity.getPhoneNumber())) {
            // 根据手机号查询对应账号和手机号的用户信息
            List<User> userList = userService.getUserListByPhoneOrAccount(householdEntity.getPhoneNumber());
            if (userList.size() > 0) {
                User user = userList.get(0);
                householdEntity.setAssociatedUserId(user.getId());
                // 更新
                householdService.updateById(householdEntity);
                // 判断用户是否包含了居民角色,不包含则需更新
                if (!user.getRoleId().contains("1717429059648606209")) {
                    user.setRoleId(user.getRoleId() + ",1717429059648606209");
                    //更新
                    userService.updateById(user);
                }
            } else {
                // 插入用户信息
                //如果用户不存在,则新增一个用户
                User newUser = new User();
                newUser.setAccount(householdEntity.getPhoneNumber());
                newUser.setPhone(householdEntity.getPhoneNumber());
                newUser.setName(householdEntity.getName());
                newUser.setRealName(householdEntity.getName());
                // 社区群众部门
                newUser.setDeptId("1727979636479037441");
                // 目前暂定居民角色,
                newUser.setRoleId("1717429059648606209");
                //默认密码为 123456
                newUser.setPassword("123456");
                // 用户新增
                boolean submit = userService.submit(newUser);
                // 更新绑定用户信息
                householdEntity.setAssociatedUserId(newUser.getId());
                // 更新
                householdService.updateById(householdEntity);
            }
        }
    }
 
    /**
     * 现居住地街道转换
     *
     * @param homeAdcode
     * @return
     */
    public String shiftResidentHomeAdcode(String homeAdcode) {
        // 只根据区县名称查询
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("town_name", homeAdcode);
        List<Region> list = regionService.list(wrapper);
        if (list.size() == 1) {
            return list.get(0).getTownCode();
        }
        return "";
    }
 
    /**
     * 根据名称转成code
     *
     * @param residentProvinceAdcode 省名称
     * @param residentCityAdcode     市名称
     * @param residentAdcode         区县名称
     */
    public String shiftResidentResidentAdCode(String residentProvinceAdcode, String residentCityAdcode, String residentAdcode) {
        if (!Strings.isBlank(residentProvinceAdcode)
            && !Strings.isBlank(residentCityAdcode)) {
            // 根据省市县三级查询对应的区县code
            QueryWrapper<Region> wrapper = new QueryWrapper<>();
            wrapper.eq("province_name", residentProvinceAdcode)
                .eq("city_name", residentCityAdcode)
                .eq("district_name", residentAdcode);
            List<Region> list = regionService.list(wrapper);
            if (list.size() > 0) {
                return list.get(0).getDistrictCode();
            }
        } else {
            // 只根据区县名称查询
            QueryWrapper<Region> wrapper = new QueryWrapper<>();
            wrapper.eq("district_name", residentAdcode);
            List<Region> list = regionService.list(wrapper);
            if (list.size() == 1) {
                return list.get(0).getDistrictCode();
            }
        }
        return "";
    }
 
    /**
     * 保存或更新用户(业主)
     *
     * @param householdEntity
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateUser(HouseholdEntity householdEntity) {
        if (null != householdEntity.getPhoneNumber() && !householdEntity.getPhoneNumber().equals("")) {
            //根据手机号查询库里的数据
            List<User> list = userService.getUserListByPhoneOrAccount(householdEntity.getPhoneNumber());
            if (list.size() > 0) {
                User user = list.get(0);
                //如果用户存在,则该用户id绑定住户
                householdEntity.setAssociatedUserId(user.getId());
                // 判断用户是否包含了居民角色,不包含则需更新
                if (!user.getRoleId().contains("1717429059648606209")) {
                    user.setRoleId(user.getRoleId() + ",1717429059648606209");
                    //更新
                    userService.updateById(user);
                }
            } else {
                User newUser = new User();
                //如果用户不存在,则新增一个用户
                newUser.setAccount(householdEntity.getPhoneNumber());
                newUser.setPhone(householdEntity.getPhoneNumber());
                newUser.setName(householdEntity.getName());
                newUser.setRealName(householdEntity.getName());
                // 社区群众部门
                newUser.setDeptId("1727979636479037441");
                // 目前暂定居民角色,
                newUser.setRoleId("1717429059648606209");
                //默认密码为 123456
                newUser.setPassword("123456");
                // 设置机构
                // 用户新增
                boolean submit = userService.submit(newUser);
                //绑定id
                householdEntity.setAssociatedUserId(newUser.getId());
            }
        }
    }
 
    @Override
    public Map<String, Object> getHouseStatistics(String code, String roleType, String aoiCode, String buildingCode, String unitCode) {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        if (roleType.equals("2")) {
            //     result1 查询楼栋数  result2 查询房屋套数 result3 查询住户数  result4 查询单元数
            Integer result1 = baseMapper.getHouseStatisticsOne(code, null, aoiCode, buildingCode, unitCode, roleType);
            Integer result2 = baseMapper.getHouseStatisticsTwo(code, null, aoiCode, buildingCode, unitCode, roleType);
            Integer result3 = baseMapper.getHouseStatisticsThree(code, null, aoiCode, buildingCode, unitCode, roleType);
            Integer result4 = baseMapper.getHouseStatisticsFour(code, null, aoiCode, buildingCode, unitCode, roleType);
            objectObjectHashMap.put("result1", result1);
            objectObjectHashMap.put("result2", result2);
            objectObjectHashMap.put("result3", result3);
            objectObjectHashMap.put("result4", result4);
        } else {
 
            Integer result1 = baseMapper.getHouseStatisticsOne(code, AuthUtil.getUserId(), aoiCode, buildingCode, unitCode, roleType);
            Integer result2 = baseMapper.getHouseStatisticsTwo(code, AuthUtil.getUserId(), aoiCode, buildingCode, unitCode, roleType);
            Integer result3 = baseMapper.getHouseStatisticsThree(code, AuthUtil.getUserId(), aoiCode, buildingCode, unitCode, roleType);
            Integer result4 = baseMapper.getHouseStatisticsFour(code, AuthUtil.getUserId(), aoiCode, buildingCode, unitCode, roleType);
            objectObjectHashMap.put("result1", result1);
            objectObjectHashMap.put("result2", result2);
            objectObjectHashMap.put("result3", result3);
            objectObjectHashMap.put("result4", result4);
        }
        return objectObjectHashMap;
    }
 
    @Override
    public List<String> getHouseBuilding(String districtCode) {
        return baseMapper.getHouseBuilding(districtCode);
    }
 
    @Override
    public List<String> getHouseUnit(String districtCode, String building) {
        return baseMapper.getHouseUnit(districtCode, building);
    }
 
    @Override
    public List<Map<String, Object>> labelStatistics(HouseVO house) {
        CommonParamSet commonParamSet = new CommonParamSet<>().invoke(HouseVO.class, house);
        return baseMapper.labelStatistics(house, commonParamSet.getRegionChildCodesList(), commonParamSet.getIsAdministrator());
    }
 
    @Override
    public List<Map<String, Object>> labelCommunityStatistics(HouseVO house) {
        CommonParamSet commonParamSet = new CommonParamSet<>().invoke(HouseVO.class, house);
        List<Map<String, Object>> list = baseMapper.labelCommunityStatistics(house, commonParamSet.getRegionChildCodesList());
        for (Map<String, Object> map : list) {
            List<LabelVO> code = baseMapper.getlabelCount(house,
                commonParamSet.getRegionChildCodesList(),
                commonParamSet.getIsAdministrator(),
                map.get("code").toString());
            map.put("child", code);
        }
        return list;
    }
 
    /**
     * 房屋网格处理
     *
     * @return
     */
    @Override
    public Object houseGridHandle() {
        // 查询未处理的数据
        List<HouseEntity> houseEntityList = baseMapper.getNotBindGridOrJwGridList(1);
        // 遍历
        for (HouseEntity houseEntity : houseEntityList) {
            String point = "'POINT(" + houseEntity.getLng() + " " + houseEntity.getLat() + ")'";
            List<GridEntity> gridEntityList = SpringUtils.getBean(IGridService.class).spatialAnalysis(point);
            if (gridEntityList.size() > 0) {
                houseEntity.setGridCode(gridEntityList.get(0).getGridCode());
                houseEntity.setGridId(gridEntityList.get(0).getId());
                // 更新
                updateById(houseEntity);
            }
        }
 
        return null;
    }
 
    /**
     * 房屋警格处理
     *
     * @return
     */
    @Override
    public Object houseJwGridHandle() {
        // 查询未处理的数据
        List<HouseEntity> houseEntityList = baseMapper.getNotBindGridOrJwGridList(2);
        // 遍历
        for (HouseEntity houseEntity : houseEntityList) {
            String point = "'POINT(" + houseEntity.getLng() + " " + houseEntity.getLat() + ")'";
            List<PoliceAffairsGridEntity> policeAffairsGridEntityList = SpringUtils.getBean(IPoliceAffairsGridService.class).spatialAnalysis(point);
            if (policeAffairsGridEntityList.size() > 0) {
                houseEntity.setJwGridCode(policeAffairsGridEntityList.get(0).getJwGridCode());
                // 更新
                updateById(houseEntity);
            }
        }
        return null;
    }
 
    /**
     * 房屋画像统计-按房屋标签统计
     *
     * @param house
     * @return
     */
    @Override
    public List<Map<String, Object>> getHouseLabelStatistic(HouseVO house) {
        CommonParamSet commonParamSet = new CommonParamSet<>().invoke(HouseVO.class, house);
        // 按房屋标签统计
        List<Map<String, Object>> list = baseMapper.getHouseLabelStatistic(house,
            commonParamSet.getIsAdministrator(),
            commonParamSet.getRegionChildCodesList(),
            commonParamSet.getGridCodeList());
        // 返回
        return list;
    }
 
    /**
     * 查询对应的社区编号
     *
     * @param id
     * @return
     */
    @Override
    public String getCommunityCode(Long id) {
        return baseMapper.getCommunityCode(id);
    }
 
    /**
     * 查询所有房屋总数
     *
     * @return
     */
    @Override
    public int getAllListTotal() {
        return baseMapper.getAllListTotal();
    }
 
    /**
     * 查询所有的房屋
     *
     * @param i
     * @param size
     * @return
     */
    @Override
    public List<HouseVO> getAllList(int i, int size) {
        return baseMapper.getAllList(i, size);
    }
 
    /**
     * 房屋标签初始化处置-根据有租客的,初始成出租,有业主没租客的初始化成自主,业主都没的就是闲置
     *
     * @param house
     * @return
     */
    @Override
    public boolean initHouseLabelBind(HouseVO house) {
        IUserHouseLabelService userHouseLabelService = SpringUtils.getBean(IUserHouseLabelService.class);
        // 查询无房屋状态的房屋数量
        int total = baseMapper.getNotBindLabelHouseNum(house);
        int size = 1000;
        int num = total / size;
        for (int i = 1; i <= num + 1; i++) {
            // 查询无房屋状态的房屋列表集合
            List<HouseVO> houseVOList = baseMapper.getNotBindLabelHouseList((i - 1) * size, size);
            for (HouseVO houseEntity : houseVOList) {
                //处理状态
                UserHouseLabelEntity houseLabelEntity = new UserHouseLabelEntity();
                houseLabelEntity.setHouseCode(houseEntity.getHouseCode());
                houseLabelEntity.setLableType(2);
                houseLabelEntity.setCreateTime(new Date());
                if (houseEntity.getStatus() == 1) {
                    // 闲置
                    houseLabelEntity.setColor("green");
                    houseLabelEntity.setLabelName("闲置");
                    houseLabelEntity.setLabelId(1037L);
                }
                if (houseEntity.getStatus() == 2) {
                    // 自住
                    houseLabelEntity.setColor("blue");
                    houseLabelEntity.setLabelName("自住");
                    houseLabelEntity.setLabelId(1038L);
                }
                if (houseEntity.getStatus() == 3) {
                    // 出租
                    houseLabelEntity.setColor("yellow");
                    houseLabelEntity.setLabelName("出租");
                    houseLabelEntity.setLabelId(1039L);
                }
                // 查询当前房屋是否已有,有则更新,无则新增
                QueryWrapper<UserHouseLabelEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("house_code", houseEntity.getHouseCode())
                    .eq("lable_type", 2);
                UserHouseLabelEntity userHouseLabelEntity = userHouseLabelService.getOne(wrapper);
                if (null != userHouseLabelEntity) {
                    // 更新
                    houseLabelEntity.setId(userHouseLabelEntity.getId());
                    userHouseLabelService.updateById(houseLabelEntity);
                } else {
                    // 新增
                    userHouseLabelService.save(houseLabelEntity);
                }
            }
        }
        return true;
    }
}