linwe
2023-11-16 4ffde981d9f5a643888e85b9fde6515a9e29b020
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
/*
 *      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.extension.service.impl.ServiceImpl;
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.HouseTenantEntity;
import org.springblade.modules.house.service.IHouseTenantService;
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.mapper.HouseRentalMapper;
import org.springblade.modules.house.service.IHouseRentalService;
import org.springblade.modules.house.vo.HouseTenantVO;
import org.springblade.modules.system.excel.HouseExcel;
import org.springblade.modules.system.excel.HouseRentalExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 出租屋 服务实现类
 *
 * @author BladeX
 * @since 2023-10-28
 */
@Service
public class HouseRentalServiceImpl extends ServiceImpl<HouseRentalMapper, HouseRentalEntity> implements IHouseRentalService {
 
    @Autowired
    private IHouseTenantService houseTenantService;
 
    @Autowired
    private IGridService gridService;
 
    /**
     * 自定义分页查询
     * @param page
     * @param houseRental
     * @return
     */
    @Override
    public IPage<HouseRentalTenantVO> selectHouseRentalPage(IPage<HouseRentalTenantVO> page, HouseRentalTenantVO houseRental) {
        List<String> list = new ArrayList<>();
        if (null!=houseRental.getRoleName() && !houseRental.getRoleName().equals("")){
            if (houseRental.getRoleName().equals("网格员")){
                // 查询对应的房屋地址code
                list = gridService.getAddressCodeListByUserId(AuthUtil.getUserId());
            }
        }
        if (null!=houseRental.getAuditStatus()){
            if (houseRental.getAuditStatus()==0){
                houseRental.setAuditStatus(2);
            }
        }
        return page.setRecords(baseMapper.selectHouseRentalPage(page, houseRental,list));
    }
 
    /**
     * 查询房屋出租情况
     * @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) {
        houseRentalVO.setCreateUser(AuthUtil.getUserId());
        houseRentalVO.setCreateTime(new Date());
        houseRentalVO.setUpdateUser(AuthUtil.getUserId());
        houseRentalVO.setUpdateTime(new Date());
        //保存自身
        boolean save = save(houseRentalVO);
        List<HouseTenantEntity> houseTenantEntities = new ArrayList<>();
        houseRentalVO.getHouseTenantVOList().forEach(e->{
            HouseTenantEntity houseTenant= new HouseTenantEntity();
 
            houseTenant.setHousingRentalId(houseRentalVO.getId());
            houseTenant.setName(e.getName());
            houseTenant.setPhone(e.getPhone());
            houseTenant.setIdCard(e.getIdCard());
            houseTenant.setDomicile(e.getDomicile());
            houseTenant.setWorkUnit(e.getWorkUnit());
            houseTenantEntities.add(houseTenant);
 
        });
        boolean saveBatch = houseTenantService.saveBatch(houseTenantEntities);
 
        return save&&saveBatch;
    }
 
    /**
     * 出租屋 自定义删除
     * @param id
     * @return
     */
    @Override
    public Boolean removeHouseRental(Long id) {
        // 先删除出租屋信息
        boolean b = removeById(id);
        // 再删除租户信息
        int i = houseTenantService.removeByHousingRentalId(id);
        if (i>0){
            return true && b;
        }
        return false;
    }
 
    /**
     * 出租屋 自定义修改
     * @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<HouseTenantEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("housing_rental_id",houseRental.getId());
        List<HouseTenantEntity> oldList = houseTenantService.list(wrapper);
        List<HouseTenantVO> list = houseRental.getHouseTenantVOList();
        // 申明新增,修改,删除集合
        List<HouseTenantEntity> newList = new ArrayList<>();
        List<HouseTenantEntity> addList = new ArrayList<>();
        List<HouseTenantEntity> updateList = new ArrayList<>();
        List<HouseTenantEntity> removeList = new ArrayList<>();
        // 找出需要新增的,否则组成新集合进行比对
        for (HouseTenantVO houseTenantVO : list) {
            houseTenantVO.setHousingRentalId(houseRental.getId());
            if (null==houseTenantVO.getId()){
                // 新增
                HouseTenantEntity houseTenant= new HouseTenantEntity();
 
                houseTenant.setHousingRentalId(houseRental.getId());
                houseTenant.setName(houseTenantVO.getName());
                houseTenant.setPhone(houseTenantVO.getPhone());
                houseTenant.setIdCard(houseTenantVO.getIdCard());
                houseTenant.setDomicile(houseTenantVO.getDomicile());
                houseTenant.setWorkUnit(houseTenantVO.getWorkUnit());
                addList.add(houseTenant);
            }else {
                newList.add(houseTenantVO);
            }
        }
        // 遍历去差集,判断是新增还是删除还是更新
        // 取旧数据和新提交数据差集--删除
        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 = houseTenantService.saveBatch(addList);
        }
        // 批量修改
        if (updateList.size()>0) {
            updateFlag = houseTenantService.updateBatchById(updateList);
        }
        // 批量删除
        if (removeList.size()>0) {
            removeFlag = houseTenantService.removeBatchByIds(removeList);
        }
        // 返回
        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);
    }
 
    @Override
    public List<HouseRentalExcel> export(HouseRentalVO houseRentalVO) {
        List<HouseRentalExcel> houseRentalExcels = baseMapper.export(houseRentalVO);
        return houseRentalExcels;
    }
}