zhongrj
2023-12-22 ddef54b90b578d14e2cdad5d21a0fc2f612be46a
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
/*
 *      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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springblade.common.utils.IdUtils;
import org.springblade.common.utils.NodeTreeUtil;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
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.grid.vo.GridVO;
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.place.vo.PlaceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 房屋 服务实现类
 *
 * @author BladeX
 * @since 2023-10-28
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, HouseEntity> implements IHouseService {
 
 
    @Autowired
    private IGridService gridService;
 
    @Autowired
    private IHouseholdService householdService;
 
    @Autowired
    private IGridRangeService gridRangeService;
 
    @Override
    public IPage<HouseVO> selectHousePage(IPage<HouseVO> page, HouseVO house) {
        List<HouseVO> houseVOS = baseMapper.selectHousePage(page, house);
        // 遍历查询网格
        for (HouseVO houseVO : houseVOS) {
            // 设置对应的网格名称
            GridVO gridVO = gridService.getGridDetailByHouseCode(houseVO.getHouseCode());
            if (null!= gridVO){
                houseVO.setGridName(gridVO.getGridName());
            }
        }
        return page.setRecords(houseVOS);
    }
 
    /**
     * 房屋自定义详情查询
     * @param house
     * @return
     */
    @Override
    public HouseVO getHouseDetail(HouseVO house) {
        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());
            }
        }
        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) {
        // 查询库中是否已存在
        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());
            houseEntity.setHouseName(houseAndHoldExcel.getHouseName());
            houseEntity.setDistrictName(houseAndHoldExcel.getDistrictName());
            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());
            houseEntity.setServiceDue(houseAndHoldExcel.getServiceDue());
            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);
            }
            // 新增
            save(houseEntity);
        }
    }
 
    /**
     * 保存住户数据
     * @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("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.getRoleType())){
                householdEntity.setRoleType(Integer.parseInt(houseAndHoldExcel.getRoleType()));
            }
            if (!Strings.isBlank(houseAndHoldExcel.getRelationship())){
                householdEntity.setRelationship(Integer.parseInt(houseAndHoldExcel.getRelationship()));
            }
            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()));
            }
            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()));
            }
            householdEntity.setHukouRegistration(houseAndHoldExcel.getHukouRegistration());
            if (!Strings.isBlank(houseAndHoldExcel.getWorkStatus())){
                householdEntity.setWorkStatus(Integer.parseInt(houseAndHoldExcel.getWorkStatus()));
            }
            householdEntity.setEmployer(houseAndHoldExcel.getEmployer());
            if (!Strings.isBlank(houseAndHoldExcel.getMaritalStatus())){
                householdEntity.setMaritalStatus(Integer.parseInt(houseAndHoldExcel.getMaritalStatus()));
            }
            householdEntity.setCardNumber(houseAndHoldExcel.getCardNumber());
            householdEntity.setOtherContact(houseAndHoldExcel.getOtherContact());
            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) {
                String labelId = houseAndHoldExcel.getLabelId();
                if (StringUtils.isBlank(labelId)) {
                    return;
                }
                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) {
                        UserHouseLabelEntity userHouseLabelEntity = new UserHouseLabelEntity();
                        userHouseLabelEntity.setLabelId(BigDecimal.valueOf(one1.getId()).longValue());
                        userHouseLabelEntity.setHouseholdId(householdEntity.getId());
                        userHouseLabelEntity.setLableType(1);
                        userHouseLabelEntity.setLabelName(s);
                        userHouseLabelEntity.setHouseCode(houseAndHoldExcel.getHouseCode());
                        bean.save(userHouseLabelEntity);
                    }
                }
 
            }
        }
    }
 
    @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("1")) {
        //     result1 查询楼栋数  result2 查询房屋套数 result3 查询住户数  result4 查询单元数
            Integer result1 = baseMapper.getHouseStatisticsOne(code, AuthUtil.getUserId(),aoiCode,buildingCode,unitCode);
            Integer result2 = baseMapper.getHouseStatisticsTwo(code, AuthUtil.getUserId(),aoiCode,buildingCode,unitCode);
            Integer result3 = baseMapper.getHouseStatisticsThree(code, AuthUtil.getUserId(),aoiCode,buildingCode,unitCode);
            Integer result4 = baseMapper.getHouseStatisticsFour(code, AuthUtil.getUserId(),aoiCode,buildingCode,unitCode);
            objectObjectHashMap.put("result1", result1);
            objectObjectHashMap.put("result2", result2);
            objectObjectHashMap.put("result3", result3);
            objectObjectHashMap.put("result4", result4);
        } else {
            Integer result1 = baseMapper.getHouseStatisticsOne(code, null,aoiCode,buildingCode,unitCode);
            Integer result2 = baseMapper.getHouseStatisticsTwo(code, null,aoiCode,buildingCode,unitCode);
            Integer result3 = baseMapper.getHouseStatisticsThree(code, null,aoiCode,buildingCode,unitCode);
            Integer result4 = baseMapper.getHouseStatisticsFour(code, null,aoiCode,buildingCode,unitCode);
            objectObjectHashMap.put("result1", result1);
            objectObjectHashMap.put("result2", result2);
            objectObjectHashMap.put("result3", result3);
            objectObjectHashMap.put("result4", result4);
        }
        return objectObjectHashMap;
    }
}