lin
2024-03-02 861e9b1eb145094dff6d1caf3dba30d7ce0e11d7
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
/*
 *      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.logging.log4j.util.Strings;
import org.springblade.common.cache.SysCache;
import org.springblade.common.param.CommonParamSet;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.grid.service.IGridService;
import org.springblade.modules.house.entity.HouseRentalEntity;
import org.springblade.modules.house.entity.HouseholdEntity;
import org.springblade.modules.house.excel.HouseRentalExcel;
import org.springblade.modules.house.mapper.HouseRentalMapper;
import org.springblade.modules.house.service.IHouseRentalService;
import org.springblade.modules.house.service.IHouseholdService;
import org.springblade.modules.house.vo.HouseRentalStatistics;
import org.springblade.modules.house.vo.HouseRentalTenantVO;
import org.springblade.modules.house.vo.HouseRentalVO;
import org.springblade.modules.house.vo.HouseholdVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 出租屋 服务实现类
 *
 * @author BladeX
 * @since 2023-10-28
 */
@Service
public class HouseRentalServiceImpl extends ServiceImpl<HouseRentalMapper, HouseRentalEntity> implements IHouseRentalService {
 
    @Autowired
    private IHouseholdService iHouseholdService;
 
 
    @Autowired
    private IGridService gridService;
 
    /**
     * 自定义分页查询
     *
     * @param page
     * @param houseRental
     * @return
     */
    @Override
    public IPage<HouseRentalTenantVO> selectHouseRentalPage(IPage<HouseRentalTenantVO> page, HouseRentalTenantVO houseRental) {
        if (null != houseRental.getAuditStatus()) {
            if (houseRental.getAuditStatus() == 0) {
                houseRental.setAuditStatus(2);
            }
        }
        CommonParamSet<Object> commonParamSet = new CommonParamSet<>().invoke(HouseRentalTenantVO.class, houseRental);
        List<HouseRentalTenantVO> houseRentalTenantVOS = baseMapper.selectHouseRentalPage(page, houseRental,
            commonParamSet.getGridCodeList(),
            commonParamSet.getRegionChildCodesList(),
            commonParamSet.getIsAdministrator());
        for (HouseRentalTenantVO houseRentalTenantVO : houseRentalTenantVOS) {
            if (houseRentalTenantVO.getStatus().equals(1)) {
                houseRentalTenantVO.setStatus(30);
            }
            if (houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getAuditStatus().equals(0)) {
                houseRentalTenantVO.setStatus(0);
            }
            if (houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getAuditStatus().equals(1)) {
                houseRentalTenantVO.setStatus(1);
            }
            if (houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getDldType().equals(3)) {
                houseRentalTenantVO.setStatus(20);
            }
            if (houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getDldType().equals(2)) {
                houseRentalTenantVO.setStatus(10);
            }
        }
        return page.setRecords(houseRentalTenantVOS);
    }
 
    /**
     * 查询房屋出租情况
     *
     * @param code
     * @return
     */
    @Override
    public List<HouseRentalVO> getHouseRentalListByCode(String code) {
        List<HouseRentalVO> houseRentalVOS = baseMapper.getHouseRentalListByCode(code);
        // 返回
        return houseRentalVOS;
    }
 
    /**
     * 自定义房屋出租新增
     *
     * @param houseRentalVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(HouseRentalVO houseRentalVO) {
        boolean flag = false;
        houseRentalVO.setCreateUser(AuthUtil.getUserId());
        houseRentalVO.setCreateTime(new Date());
        houseRentalVO.setUpdateUser(AuthUtil.getUserId());
        houseRentalVO.setUpdateTime(new Date());
        // 网格员新增默认是审核通过
        houseRentalVO.setAuditStatus(1);
        // 获取请求头中的角色别名
        String roleName = SpringUtils.getRequestParam("roleName");
        // 居民
        if (!Strings.isBlank(roleName) && roleName.equals("inhabitant")) {
            // 待审核
            houseRentalVO.setAuditStatus(0);
        }
        //保存自身
        flag = save(houseRentalVO);
        //保存到住户
        flag = saveHousehold(houseRentalVO, flag);
        return flag;
    }
 
    /**
     * 保存租户信息
     *
     * @param houseRentalVO
     * @param flag
     * @return
     */
    public boolean saveHousehold(HouseRentalVO houseRentalVO, boolean flag) {
        if (flag && houseRentalVO.getHouseholdVOList().size() > 0) {
            List<String> phoneList = new ArrayList<>();
            List<HouseholdEntity> householdEntityList = new ArrayList<>();
            houseRentalVO.getHouseholdVOList().forEach(e -> {
                e.setHouseCode(houseRentalVO.getHouseCode());
                e.setHousingRentalId(houseRentalVO.getId());
                e.setRelationship(18);
                e.setResidentialStatus(1);
                e.setRoleType(2);
                householdEntityList.add(e);
                phoneList.add(e.getPhoneNumber());
            });
            try {
                // 查询租户是否网格员身份的,给网格员设置居民角色
                IUserService bean = SpringUtils.getBean(IUserService.class);
                String str = "1717429261910528001";
                List<User> list = bean.list(Wrappers.<User>lambdaQuery()
                    .in(User::getPhone, phoneList)
                    .like(User::getRoleId, str));
                for (User user : list) {
                    boolean contains = user.getRoleId().contains("1717429059648606209");
                    if (contains) {
                        continue;
                    }
                    user.setRoleId(user.getRoleId() + ",1717429059648606209");
                }
                bean.updateBatchById(list);
            } catch (Exception e) {
                log.error("保存用户角色为居民:", e);
            }
            flag = iHouseholdService.saveBatch(householdEntityList);
        }
        return flag;
    }
 
    /**
     * 出租屋 自定义删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean removeHouseRental(Long id) {
        // 先删除出租屋信息
        boolean b = removeById(id);
        // 再删除租户信息
        boolean update = iHouseholdService.update(Wrappers.<HouseholdEntity>lambdaUpdate()
            .set(HouseholdEntity::getIsDeleted, 1)
            .eq(HouseholdEntity::getHousingRentalId, id));
        // 返回
        return b;
    }
 
    /**
     * 出租屋 自定义修改
     *
     * @param houseRental
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateHouseRental(HouseRentalVO houseRental) {
        boolean addFlag = true;
        boolean updateFlag = true;
        boolean removeFlag = true;
        houseRental.setUpdateUser(AuthUtil.getUserId());
        houseRental.setUpdateTime(new Date());
        //更新自身
        boolean update = updateById(houseRental);
        // 查询对应已存在的租户
        QueryWrapper<HouseholdEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("housing_rental_id", houseRental.getId());
        List<HouseholdEntity> oldList = iHouseholdService.list(wrapper);
        List<HouseholdVO> list = houseRental.getHouseholdVOList();
        // 申明新增,修改,删除集合
        List<HouseholdEntity> newList = new ArrayList<>();
        List<HouseholdEntity> addList = new ArrayList<>();
        List<HouseholdEntity> updateList = new ArrayList<>();
        List<HouseholdEntity> removeList = new ArrayList<>();
        // 找出需要新增的,否则组成新集合进行比对
        List<String> phoneList = new ArrayList<>();
 
        for (HouseholdEntity householdEntity : list) {
            if (null == householdEntity.getId()) {
                // 新增
                householdEntity.setHouseCode(houseRental.getHouseCode());
                householdEntity.setHousingRentalId(houseRental.getId());
                householdEntity.setRelationship(18);
                householdEntity.setResidentialStatus(1);
                householdEntity.setRoleType(2);
                addList.add(householdEntity);
                phoneList.add(householdEntity.getPhoneNumber());
            } else {
                newList.add(householdEntity);
            }
        }
        // 遍历去差集,判断是新增还是删除还是更新
        // 取旧数据和新提交数据差集--删除
        removeList = oldList.stream().filter(vo -> !newList.stream().map(e ->
            e.getId()).collect(Collectors.toList()).contains(vo.getId())).collect(Collectors.toList());
        // 取旧数据和新提交数据交集--更新
        updateList = newList.stream().filter(vo -> oldList.stream().map(e ->
            e.getId()).collect(Collectors.toList()).contains(vo.getId())).collect(Collectors.toList());
 
        // 批量新增
        if (addList.size() > 0) {
            addFlag = iHouseholdService.saveBatch(addList);
        }
        // 批量修改
        if (updateList.size() > 0) {
            updateFlag = iHouseholdService.updateBatchById(updateList);
            for (HouseholdEntity householdEntity : updateList) {
                phoneList.add(householdEntity.getPhoneNumber());
            }
        }
        // 批量删除
        if (removeList.size() > 0) {
            removeFlag = iHouseholdService.removeBatchByIds(removeList);
        }
        try {
            // 查询租户是否网格员身份的,给网格员设置居民角色
            IUserService bean = SpringUtils.getBean(IUserService.class);
            String str = "1717429261910528001";
            List<User> list2 = bean.list(Wrappers.<User>lambdaQuery()
                .in(User::getPhone, phoneList)
                .like(User::getRoleId, str));
            for (User user : list2) {
                boolean contains = user.getRoleId().contains("1717429059648606209");
                if (contains) {
                    continue;
                }
                user.setRoleId(user.getRoleId() + ",1717429059648606209");
            }
            bean.updateBatchById(list2);
        } catch (Exception e) {
            log.error("保存用户角色为居民:", e);
        }
        // 返回
        return update && addFlag && updateFlag && removeFlag;
    }
 
    /**
     * 获取统计数据
     *
     * @return
     */
    @Override
    public Object getStatistics(HouseRentalTenantVO houseRental) {
        List<String> list = new ArrayList<>();
        if (null != houseRental.getRoleName() && !houseRental.getRoleName().equals("")) {
            if (houseRental.getRoleName().equals("网格员")) {
                // 查询对应的房屋地址code
                list = gridService.getAddressCodeListByUserId(AuthUtil.getUserId());
            }
        }
        // 查询
        List<HouseRentalStatistics> statistics = baseMapper.getStatistics(houseRental, list);
        // 返回
        return statistics;
    }
 
    /**
     * 出租屋 确认
     *
     * @param houseRental
     * @return
     */
    @Override
    public Boolean confirmHouseRental(HouseRentalVO houseRental) {
        // 修改状态
        houseRental.setUpdateTime(new Date());
        // 修改
        return updateById(houseRental);
    }
 
    /**
     * 导出租赁信息
     *
     * @param houseRentalVO
     * @return
     */
    @Override
    public List<HouseRentalExcel> export(HouseRentalTenantVO houseRentalVO) {
        List<HouseRentalExcel> houseRentalExcels = baseMapper.export(houseRentalVO);
        return houseRentalExcels;
    }
 
    @Override
    public Integer getStatisticsCount(HouseRentalTenantVO houseRental) {
        CommonParamSet<Object> commonParamSet = new CommonParamSet<>().invoke(HouseRentalTenantVO.class, houseRental);
        return baseMapper.getStatisticsCount(houseRental, commonParamSet.getGridCodeList(),
            commonParamSet.getRegionChildCodesList(), commonParamSet.getIsAdministrator());
    }
}