puzhibing
2023-11-28 7b726d5ff439e7b8bb66369ecc5881e370286bc8
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.HousingDemandMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.UUIDUtil;
import com.stylefeng.guns.modular.system.warpper.req.HousingDemandReq;
import com.stylefeng.guns.modular.system.warpper.req.SearchHousingDemandReq;
import com.stylefeng.guns.modular.system.warpper.res.ContactInformationRes;
import com.stylefeng.guns.modular.system.warpper.res.HousingDemandInfoRes;
import com.stylefeng.guns.modular.system.warpper.res.SearchHousingDemandListRes;
import com.stylefeng.guns.modular.system.warpper.res.SearchHousingDemandRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * @author zhibing.pu
 * @Date 2023/11/20 16:59
 */
@Service
public class HousingDemandServiceImpl extends ServiceImpl<HousingDemandMapper, HousingDemand> implements IHousingDemandService {
 
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IHousingDemandDistrictService housingDemandDistrictService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IHouseTypeService houseTypeService;
    @Autowired
    private ICollectionHousingDemandService collectionHousingDemandService;
 
 
 
 
    /**
     * 添加房源需求
     * @param req
     */
    @Override
    public ResultUtil addHousingDemand(HousingDemandReq req) {
        AppUser appUser = appUserService.getAppUser();
        if(null == appUser){
            return ResultUtil.tokenErr();
        }
        HousingDemand housingDemand = new HousingDemand();
        BeanUtils.copyProperties(req, housingDemand);
        try {
            housingDemand.setCode(UUIDUtil.getRandomCode(8));
        } catch (Exception e) {
            e.printStackTrace();
        }
        housingDemand.setIsDelete(0);
        housingDemand.setInsertTime(new Date());
        housingDemand.setInsertUserId(appUser.getId());
        this.insert(housingDemand);
        List<HousingDemandDistrict> list = new ArrayList<>();
        if(ToolUtil.isNotEmpty(req.getDistrict())){
            JSONArray jsonArray = JSON.parseArray(req.getDistrict());
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer cityId = jsonObject.getInteger("cityId");
                Integer districtId = jsonObject.getInteger("districtId");
                //不限区域
                if(null == districtId){
                    districtId = 0;
                }
                HousingDemandDistrict housingDemandDistrict = new HousingDemandDistrict();
                housingDemandDistrict.setCityId(cityId);
                housingDemandDistrict.setDistrictId(districtId);
                housingDemandDistrict.setHousingDemandId(housingDemand.getId());
                list.add(housingDemandDistrict);
            }
        }
        if(list.size() > 0){
            housingDemandDistrictService.insertBatch(list);
        }
        return ResultUtil.success();
    }
 
 
    /**
     * 搜索求房源列表数据
     * @param req
     * @return
     */
    @Override
    public SearchHousingDemandRes searchHousingDemand(SearchHousingDemandReq req) {
        req.setPageNum(req.getPageNum() - 1 * req.getPageSize());
        //区域
        List<String> district = null;
        if(StringUtils.hasLength(req.getDistrict())){
            district = new ArrayList<>();
            JSONArray jsonArray = JSON.parseArray(req.getDistrict());
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer cityId = jsonObject.getInteger("cityId");
                Integer districtId = jsonObject.getInteger("districtId");
                //不限区域
                if(null == districtId){
                    districtId = 0;
                }
                district.add("cityId:" + cityId + ",districtId:" + districtId);
            }
        }
 
        //价格范围
        Double saleAmountStart = null;
        Double saleAmountEnd = null;
        if(StringUtils.hasLength(req.getSaleAmount())){
            String[] split = req.getSaleAmount().split("-");
            saleAmountStart = Double.valueOf(split[0]);
            saleAmountEnd = Double.valueOf(split[1]);
        }
        //户型
        List<String> houseModels = null;
        if(StringUtils.hasLength(req.getHouseModel())){
            String[] split = req.getHouseModel().split(",");
            houseModels = Arrays.asList(split);
        }
        //房型
        List<Integer> houseTypeIds = null;
        if(StringUtils.hasLength(req.getHouseTypeId())){
            houseTypeIds = new ArrayList<>();
            String[] split = req.getHouseTypeId().split(",");
            for (String s : split) {
                houseTypeIds.add(Integer.valueOf(s));
            }
        }
 
        SearchHousingDemandRes searchHouseResource = new SearchHousingDemandRes();
        List<SearchHousingDemandListRes> searchHouseResourceListRes = this.baseMapper.searchHousingDemand(req, district, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
 
        for (SearchHousingDemandListRes searchHouseResourceListRe : searchHouseResourceListRes) {
            List<HousingDemandDistrict> list = housingDemandDistrictService.selectList(new EntityWrapper<HousingDemandDistrict>()
                    .eq("housing_demand_id", searchHouseResourceListRe.getId()));
            List<String> dis = new ArrayList<>();
            for (HousingDemandDistrict housingDemandDistrict : list) {
                Region region = regionService.selectById(housingDemandDistrict.getCityId());
                Region region1 = regionService.selectById(housingDemandDistrict.getDistrictId());
                dis.add(region.getName() + ">" + (null == region1 ? "不限" : region1.getName()));
 
            }
            //城市不限用空判断
            searchHouseResourceListRe.setAddress(dis.size() == 0 ? null : dis);
        }
        searchHouseResource.setList(searchHouseResourceListRes);
        Integer integer = this.baseMapper.searchHousingDemandCount(req, district, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
        searchHouseResource.setTotal(integer);
        return searchHouseResource;
    }
 
 
    /**
     * 获取求房源详情
     * @param id
     * @return
     */
    @Override
    public HousingDemandInfoRes housingDemandInfo(Integer id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HousingDemand housingDemand = this.selectById(id);
        HousingDemandInfoRes housingDemandInfoRes = new HousingDemandInfoRes();
        AppUser appUser = appUserService.getAppUser();
        housingDemandInfoRes.setId(id);
        housingDemandInfoRes.setTitle(housingDemand.getTitle());
        housingDemandInfoRes.setSaleAmount(housingDemand.getSaleAmount());
        housingDemandInfoRes.setHouseModel(housingDemand.getHouseModel());
        housingDemandInfoRes.setRentalDuration(housingDemand.getRentalDuration());
        housingDemandInfoRes.setSaleDate(housingDemand.getSaleDate());
        String[] split = housingDemand.getHouseTypeId().split(",");
        String houseType = "";
        for (String s : split) {
            HouseType htype = houseTypeService.selectById(s);
            houseType += htype.getName() + " / ";
        }
        housingDemandInfoRes.setHouseType(houseType.substring(0, houseType.lastIndexOf("/")));
        housingDemandInfoRes.setFloor(housingDemand.getFloor());
        housingDemandInfoRes.setElevator(housingDemand.getElevator());
        housingDemandInfoRes.setDryingArea(housingDemand.getDryingArea());
        housingDemandInfoRes.setHouseArea(housingDemand.getHouseArea());
        housingDemandInfoRes.setKeepPet(housingDemand.getKeepPet());
        List<HousingDemandDistrict> list = housingDemandDistrictService.selectList(new EntityWrapper<HousingDemandDistrict>()
                .eq("housing_demand_id", id));
        List<String> dis = new ArrayList<>();
        for (HousingDemandDistrict housingDemandDistrict : list) {
            Region region = regionService.selectById(housingDemandDistrict.getCityId());
            Region region1 = regionService.selectById(housingDemandDistrict.getDistrictId());
            dis.add(region.getName() + ">" + (null == region1 ? "不限" : region1.getName()));
 
        }
        //城市不限用空判断
        housingDemandInfoRes.setAddress(dis.size() == 0 ? null : dis);
        housingDemandInfoRes.setMoreIntroduction(housingDemand.getMoreIntroduction());
        housingDemandInfoRes.setViewsNumber(housingDemand.getViewsNumber());
        AppUser appUser1 = appUserService.selectById(housingDemand.getAppUserId());
        housingDemandInfoRes.setProfilePhoto(appUser1.getProfilePhoto());
        housingDemandInfoRes.setNickname(appUser1.getNickname());
        housingDemandInfoRes.setInsertTime(sdf.format(housingDemand.getInsertTime()));
        housingDemandInfoRes.setUpdateTime(sdf.format(housingDemand.getUpdateTime()));
        housingDemandInfoRes.setCode(housingDemand.getCode());
        int collectionTimes = collectionHousingDemandService.selectCount(new EntityWrapper<CollectionHousingDemand>().eq("housing_demand_id", id));
        housingDemandInfoRes.setCollectionTimes(collectionTimes);
        housingDemandInfoRes.setCollection(0);
        if(null != appUser){
            int collection = collectionHousingDemandService.selectCount(new EntityWrapper<CollectionHousingDemand>().eq("housing_demand_id", id).eq("app_user_id", appUser.getId()));
            housingDemandInfoRes.setCollection(0 == collection ? 0 : 1);
        }
        //添加访问次数记录
        addViewsNumber(housingDemand);
        return housingDemandInfoRes;
    }
 
 
    /**
     * 添加访问次数记录
     * @param housingDemand
     */
    private void addViewsNumber(HousingDemand housingDemand){
        housingDemand.setViewsNumber((null == housingDemand.getViewsNumber() ? 0 : housingDemand.getViewsNumber()) + 1);
        this.updateById(housingDemand);
    }
 
 
    /**
     * 获取联系方式
     * @param id
     * @return
     */
    @Override
    public ContactInformationRes getContactInformation(Integer id) {
        HousingDemand houseResource = this.selectById(id);
        AppUser appUser = appUserService.selectById(houseResource.getAppUserId());
        ContactInformationRes contactInformationRes = new ContactInformationRes();
        contactInformationRes.setWhatsApp(appUser.getWatchApp());
        contactInformationRes.setPhone(appUser.getPhone());
        return contactInformationRes;
    }
 
    /**
     * 获取剩余发布数量
     * @return
     */
    @Override
    public ResultUtil<Integer> getSurplusPushNumber() {
        AppUser appUser = appUserService.getAppUser();
        if(null == appUser){
            return ResultUtil.tokenErr();
        }
        int num = this.selectCount(new EntityWrapper<HousingDemand>().eq("is_delete", 0)
                .eq("app_user_id", appUser.getId())
                .eq("status", 1)
        );
        return ResultUtil.success(appUser.getUserType() != 3 ? (3 - num) : -1);
    }
}