44323
2024-01-17 cef87e5c90c5551df2f98047e113530d79270e9f
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
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.alipay.api.internal.util.codec.Base64;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mongodb.client.result.UpdateResult;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.file.OSSService;
import com.stylefeng.guns.modular.system.dao.HouseResourceMapper;
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.WxAppletTools;
import com.stylefeng.guns.modular.system.warpper.PointLocation;
import com.stylefeng.guns.modular.system.warpper.req.AddHouseReq;
import com.stylefeng.guns.modular.system.warpper.req.HouseQuery;
import com.stylefeng.guns.modular.system.warpper.req.SearchHouseResourceReq;
import com.stylefeng.guns.modular.system.warpper.req.UserInfoQuery;
import com.stylefeng.guns.modular.system.warpper.res.*;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.http.entity.ContentType;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.*;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author zhibing.pu
 * @Date 2023/11/8 15:10
 */
@Service
public class HouseResourceService extends ServiceImpl<HouseResourceMapper, HouseResource> implements IHouseResourceService {
 
    @Autowired
    private ISearchHistoryConditionService searchHistoryConditionService;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IHouseTypeService houseTypeService;
    @Autowired
    private ICollectionHouseResourceService collectionHouseResourceService;
    @Resource
    private MongoTemplate mongoTemplate;
    @Autowired
    private IHousingDemandService housingDemandService;
    @Autowired
    private WxAppletTools wxAppletTools;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private OSSService ossService;
    @Value("${wx.appletsAppid}")
    private String wxAppletsAppid;
    @Value("${wx.appletsAppSecret}")
    private String wxAppletsAppSecret;
    /**
     * 获取房源列表
     * @param req
     * @return
     */
    @Override
    public SearchHouseResourceRes searchHouseResource(SearchHouseResourceReq req) {
        req.setPageNum((req.getPageNum() - 1) * req.getPageSize());
        //租房处理推荐和记录历史搜索数据
        fillSearchHistory(req);
        //区域
        List<Integer> districtIds = new ArrayList<>();
        List<Integer> cityIds = new ArrayList<>();
        if (req.getDataType()!=null && req.getDataType()==1 && req.getUserType()==5 && req.getDistrict()==null && req.getArea()==null){
            // 如果用户有历史 并且在首页列表的时候 看他有没有历史搜索
            if (appUserService.getAppUser()!=null){
                SearchHistoryCondition app_user_id = searchHistoryConditionService.selectOne(new EntityWrapper<SearchHistoryCondition>()
                        .eq("app_user_id", appUserService.getAppUser().getId()));
                if (app_user_id!=null){
                    if(StringUtils.hasLength(app_user_id.getDistrict()) ){
                        districtIds.add(Integer.valueOf(app_user_id.getDistrict()));
                    }
                }
            }
        }
        if (req.getDistrict() != null &&(!req.getDistrict().equals("")) ){
            // 一级id
            Integer integer = Integer.valueOf(req.getDistrict());
            districtIds.add(integer);
            if (req.getArea()!=null && (!req.getArea().equals(""))){
                String[] split = req.getArea().split(",");
                for (String s : split) {
                    cityIds.add(Integer.valueOf(s));
                }
            }
        }else if (StringUtils.hasLength(req.getArea())){
            String[] split = req.getArea().split(",");
            for (String s : split) {
                cityIds.add(Integer.valueOf(s));
            }
            // 根据用户历史搜索 推荐对应的区域
        }
//        if(StringUtils.hasLength(req.getDistrict())){
//            cityIds = new ArrayList<>();
//            districtIds = 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(0 == districtId || null == districtId){
//                    List<Region> regions = regionService.selectList(new EntityWrapper<Region>().eq("parent_id", cityId));
//                    districtIds.addAll(regions.stream().map(Region::getId).collect(Collectors.toList()));
//                }
//                cityIds.add(cityId);
//                districtIds.add(districtId);
//            }
//        }
 
        //价格范围
        Double saleAmountStart = null;
        Double saleAmountEnd = null;
        if(StringUtils.hasLength(req.getSaleAmount())){
            if (req.getSaleAmount().contains("以上")){
                saleAmountStart = Double.valueOf(req.getSaleAmount().substring(0, req.getSaleAmount().length() - 2));
                saleAmountEnd = 10000000000000000000000.0;
            }else if (req.getSaleAmount().contains("以下")){
                saleAmountStart = 0.0;
                saleAmountEnd = Double.valueOf(req.getSaleAmount().substring(0, req.getSaleAmount().length() - 2));
            }else{
                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));
            }
        }
        SearchHouseResourceRes searchHouseResource = new SearchHouseResourceRes();
        List<SearchHouseResourceListRes> searchHouseResourceListRes = this.baseMapper.searchHouseResource(req, cityIds, districtIds, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
 
        // 如果有根据用户历史来搜索必看 如果没有搜索出数据 那么查询所有必看
        if (req.getDataType()!=null&&req.getDataType()==1 && (!StringUtils.hasLength(req.getDistrict())) && searchHouseResourceListRes!=null&&searchHouseResourceListRes.size()==0){
            cityIds=null;
            districtIds=null;
            searchHouseResourceListRes = this.baseMapper.searchHouseResource(req, cityIds, districtIds, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
        }
        for (SearchHouseResourceListRes t : searchHouseResourceListRes) {
            if (t.getIsManage()!=null && t.getIsManage() == 1){
                // 后台添加设置为中介房源
                t.setHouseResource(3);
            }
        }
        searchHouseResource.setList(searchHouseResourceListRes);
        List<SearchHouseResourceListRes> searchHouseResourceListRes1 = this.baseMapper.searchHouseResource1(req, cityIds, districtIds, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
        searchHouseResource.setTotal(searchHouseResourceListRes1.size());
        return searchHouseResource;
    }
 
    /**
     * 填装历史搜索记录和修改历史搜索记录
     * @param req
     */
    private void fillSearchHistory(SearchHouseResourceReq req){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser){
            //获取历史搜索数据
            SearchHistoryCondition searchHistoryCondition = searchHistoryConditionService
                    .selectOne(new EntityWrapper<SearchHistoryCondition>().eq("app_user_id", appUser.getId()));
            if(null == searchHistoryCondition){
                //没有历史记录,将现有记录添加进去
                searchHistoryCondition = new SearchHistoryCondition();
                searchHistoryCondition.setAppUserId(appUser.getId());
                searchHistoryCondition.setDistrict(req.getDistrict());
                searchHistoryCondition.setPrice(req.getSaleAmount());
                searchHistoryCondition.setHouseModels(req.getHouseModel());
                searchHistoryCondition.setElevator(req.getElevator());
                searchHistoryCondition.setHouseTypeIds(req.getHouseTypeId());
                searchHistoryConditionService.insert(searchHistoryCondition);
            }else{
                //没有进行搜索的情况,将历史记录填充进搜素条件。有搜索条件则不填充,然后更新历史搜索条件
                if(ToolUtil.isEmpty(req.getDistrict()) && ToolUtil.isEmpty(req.getSaleAmount()) &&
                        ToolUtil.isEmpty(req.getHouseModel()) && ToolUtil.isEmpty(req.getElevator()) &&
                        ToolUtil.isEmpty(req.getHouseTypeId())){
//                    req.setDistrict(searchHistoryCondition.getDistrict());
//                    req.setSaleAmount(searchHistoryCondition.getPrice());
//                    req.setHouseModel(searchHistoryCondition.getHouseModels());
//                    req.setElevator(searchHistoryCondition.getElevator());
//                    req.setHouseTypeId(searchHistoryCondition.getHouseTypeIds());
                }else{
                    //更新历史搜索记录
                    searchHistoryCondition.setDistrict(req.getDistrict());
                    searchHistoryCondition.setPrice(req.getSaleAmount());
                    searchHistoryCondition.setHouseModels(req.getHouseModel());
                    searchHistoryCondition.setElevator(req.getElevator());
                    searchHistoryCondition.setHouseTypeIds(req.getHouseTypeId());
                    searchHistoryConditionService.updateAllColumnById(searchHistoryCondition);
                }
            }
        }
    }
 
 
    /**
     * 获取区域房源数量
     * @return
     */
    @Override
    public List<DistrictHouseResourceNumberRes> getDistrictHouseResourceNumber(Integer userType, Integer dataType) {
        List<DistrictHouseResourceNumberRes> districtHouseResourceNumber = this.baseMapper.getDistrictHouseResourceNumber(userType, dataType);
        return districtHouseResourceNumber;
    }
 
 
    /**
     * 获取房源详情
     * @param id
     * @return
     */
    @Override
    public HouseResourceInfoRes getHouseResourceInfo(Integer id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        AppUser appUser = appUserService.getAppUser();
        HouseResource houseResource = this.selectById(id);
        SearchHouseResourceReq req = new SearchHouseResourceReq();
        if (houseResource.getDistrictId()!=null)req.setDistrict(houseResource.getCityId().toString());
        if (houseResource.getSaleAmount()!=null)req.setSaleAmount(houseResource.getSaleAmount().toString());
        req.setHouseModel(houseResource.getHouseModel());
        req.setElevator(houseResource.getElevator());
        if (houseResource.getHouseTypeId()!=null)req.setHouseTypeId(houseResource.getHouseTypeId().toString());
        fillSearchHistory(req);
        HouseResourceInfoRes houseResourceInfoRes = new HouseResourceInfoRes();
        houseResourceInfoRes.setDataType(houseResource.getDataType());
        houseResourceInfoRes.setId(id);
        houseResourceInfoRes.setBuildingOrientation(houseResource.getBuildingOrientation());
        houseResourceInfoRes.setVideoPhoto(houseResource.getVideoPhoto());
        houseResourceInfoRes.setQrCode(houseResource.getQrCode());
        houseResourceInfoRes.setTitle(houseResource.getTitle());
        houseResourceInfoRes.setSaleAmount(houseResource.getSaleAmount());
        houseResourceInfoRes.setHouseModel(houseResource.getHouseModel());
        houseResourceInfoRes.setRentalDuration(houseResource.getRentalDuration());
        houseResourceInfoRes.setSaleDate(houseResource.getStartTime());
        houseResourceInfoRes.setHousePhoto(houseResource.getHousePhoto());
        HouseType houseType = houseTypeService.selectById(houseResource.getHouseTypeId());
        if (houseType!=null)houseResourceInfoRes.setHouseType(houseType.getName());
        houseResourceInfoRes.setFloor(houseResource.getFloor());
        houseResourceInfoRes.setElevator(houseResource.getElevator());
        if (houseResource.getBalcony()!=null&&houseResource.getBalcony()==1)houseResourceInfoRes.setDryingAreas("阳台");
        if (houseResource.getDryingArea()!=null&&houseResource.getDryingArea()==1)houseResourceInfoRes.setDryingAreas("天台");
        if (houseResource.getAir()!=null&&houseResource.getAir()==1)houseResourceInfoRes.setDryingAreas("无");
        houseResourceInfoRes.setHouseArea(houseResource.getHouseArea());
        houseResourceInfoRes.setKeepPet(houseResource.getKeepPet());
        Region region = regionService.selectById(houseResource.getDistrictId());
        Region region1 = regionService.selectById(houseResource.getCityId());
        if (region != null&&region1 != null){
            if (houseResource.getHouseAddress()==null){
                houseResourceInfoRes.setAddress(region1.getName() + " > " + region.getName());
            }else{
                houseResourceInfoRes.setAddress(region1.getName() + " > " + region.getName() + "/" +houseResource.getHouseAddress() );
            }
        }
        houseResourceInfoRes.setLongitude(houseResource.getLongitude());
        houseResourceInfoRes.setLatitude(houseResource.getLatitude());
        houseResourceInfoRes.setMoreIntroduction(houseResource.getMoreIntroduction());
        houseResourceInfoRes.setViewsNumber(houseResource.getViewsNumber());
        AppUser appUser1 = appUserService.selectById(houseResource.getAppUserId());
        houseResourceInfoRes.setProfilePhoto(appUser1.getProfilePhoto());
        houseResourceInfoRes.setNickname(appUser1.getNickname());
        houseResourceInfoRes.setUserType(appUser1.getUserType());
        houseResourceInfoRes.setInsertTime(sdf.format(houseResource.getInsertTime()));
        // 后台添加的
        if (houseResource.getIsManage()!=null && houseResource.getIsManage()==1){
            User user = userService.selectById(houseResource.getInsertUserId());
            if (user!=null){
                houseResourceInfoRes.setNickname(user.getName());
                houseResourceInfoRes.setProfilePhoto(user.getAvatar());
                houseResourceInfoRes.setUserType(3);
            }
        }
        if (houseResource.getUpdateTime()!=null){
            houseResourceInfoRes.setUpdateTime(sdf.format(houseResource.getUpdateTime()));
        }
        houseResourceInfoRes.setCode(houseResource.getCode());
        int collectionTimes = collectionHouseResourceService.selectCount(new EntityWrapper<CollectionHouseResource>().eq("house_resource_id", id));
        houseResourceInfoRes.setCollectionTimes(collectionTimes);
        houseResourceInfoRes.setCollection(0);
        if(null != appUser){
            int collection = collectionHouseResourceService.selectCount(new EntityWrapper<CollectionHouseResource>().eq("house_resource_id", id).eq("app_user_id", appUser.getId()));
            houseResourceInfoRes.setCollection(0 == collection ? 0 : 1);
        }
        //添加访问次数记录
        addViewsNumber(houseResource);
        return houseResourceInfoRes;
    }
 
    /**
     * 添加访问次数记录
     * @param houseResource
     */
    private void addViewsNumber(HouseResource houseResource){
        houseResource.setViewsNumber((null == houseResource.getViewsNumber() ? 0 : houseResource.getViewsNumber()) + 1);
        this.updateById(houseResource);
    }
 
 
    /**
     * 获取附近房源数据
     * @param id
     * @return
     */
    @Override
    public List<SearchHouseResourceListRes> getNearbyHouseResource(Integer id) {
        HouseResource houseResource = this.selectById(id);
        //获取中心半径5KM范围内的房源数据
        if (houseResource.getLongitude()!=null && houseResource.getLatitude()!=null){
        Double x = Double.valueOf(houseResource.getLongitude());
        Double y = Double.valueOf(houseResource.getLatitude());
        Point point = new Point(x, y);
        Circle circle = new Circle(point, new Distance(5, Metrics.KILOMETERS));
        Query query = Query.query(Criteria.where("geoJsonPoint").withinSphere(circle));
        List<PointLocation> pointLocations = mongoTemplate.find(query, PointLocation.class);
        List<Integer> ids = pointLocations.stream().map(PointLocation::getHouseId).collect(Collectors.toList());
        if (ids.size()!= 0){
        List<HouseResource> houseResources = this.selectBatchIds(ids);
            List<HouseResource> collect = houseResources.stream()
                    .filter(t -> !t.getId().equals(id)
                            && t.getAuthStatus() == 2 && t.getType() == 2
                    && t.getStatus() == 1 && t.getIsDelete() == 0 && t.getDataType() == houseResource.getDataType())
                    .collect(Collectors.toList());
            List<SearchHouseResourceListRes> list = new ArrayList<>();
        //遍历解析出返回数据
        for (HouseResource resource : collect) {
            SearchHouseResourceListRes searchHouseResourceListRes = new SearchHouseResourceListRes();
            searchHouseResourceListRes.setId(resource.getId());
            AppUser appUser = appUserService.selectById(resource.getAppUserId());
            searchHouseResourceListRes.setHouseResource(appUser.getUserType());
            if(StringUtils.hasLength(resource.getHousePhoto())){
                searchHouseResourceListRes.setImgUrl(resource.getHousePhoto().split(",")[0]);
            }
            searchHouseResourceListRes.setTitle(resource.getTitle());
            searchHouseResourceListRes.setHouseArea(resource.getHouseArea());
            searchHouseResourceListRes.setHouseModel(resource.getHouseModel());
            Region region = regionService.selectById(houseResource.getDistrictId());
            Region region1 = regionService.selectById(houseResource.getCityId());
            if (region!=null && region1!=null){
                searchHouseResourceListRes.setAddress(region1.getName() + " > " + region.getName() + "/" + houseResource.getHouseAddress());
            }
            searchHouseResourceListRes.setSaleAmount(resource.getSaleAmount().doubleValue());
            searchHouseResourceListRes.setElevator(resource.getElevator());
            searchHouseResourceListRes.setDryingArea(resource.getDryingArea());
            searchHouseResourceListRes.setGarden(resource.getGarden());
            searchHouseResourceListRes.setCarport(resource.getCarport());
            searchHouseResourceListRes.setBalcony(resource.getBalcony());
            searchHouseResourceListRes.setKeepPet(resource.getKeepPet());
            list.add(searchHouseResourceListRes);
        }
            return list;
        }
 
        }
        List<SearchHouseResourceListRes> list = new ArrayList<>();
        return list;
    }
 
 
    /**
     * 获取联系方式
     * @param id
     * @return
     */
    @Override
    public ContactInformationRes getContactInformation(Integer id) {
        HouseResource houseResource = this.selectById(id);
        ContactInformationRes contactInformationRes = new ContactInformationRes();
        contactInformationRes.setWatchApp(houseResource.getWatchApp());
        contactInformationRes.setPhone(houseResource.getPhone());
        contactInformationRes.setWechatQrCode(houseResource.getWechatQRCode());
        return contactInformationRes;
    }
 
    @Override
    public SearchHouseResourceRes listHouse(HouseQuery query) {
        SearchHouseResourceRes res = new SearchHouseResourceRes();
        res.setList(this.baseMapper.listHouse(query));
        res.setTotal(res.getList().size());
        return res;
    }
    @Override
    public ResultUtil addHouse(AddHouseReq req) {
        Integer appUserId = appUserService.getAppUser().getId();
        AppUser appUser = appUserService.selectOne(new EntityWrapper<AppUser>()
                .eq("id", appUserId)
                .eq("status", 1));
        if (appUser!=null){
            if (req.getId()==null){
                if (req.getType()==2){
                    if (StringUtils.hasLength(req.getWechatQRCode()))appUser.setWechatQrCode1(req.getWechatQRCode());
                    if (StringUtils.hasLength(req.getWatchApp()))appUser.setWatchApp1(req.getWatchApp());
                    if (StringUtils.hasLength(req.getPhone()))appUser.setPhone1(req.getPhone());
                    appUserService.updateById(appUser);
                if (appUser.getAuth()!=2){
                    // 未认证 只能能发布三条房源信息
                    List<HouseResource> houseResources = this.selectList(new EntityWrapper<HouseResource>()
                            .eq("app_user_id", appUserId)
                            .eq("is_delete", 0)
                            .eq("type", 2)
                    );
                    if (houseResources.size()>=3){
                        return ResultUtil.error("未认证中介或认证未通过,只能同时发布3条房源信息");
                    }
                }
                }
            }
            HouseResource houseResource = new HouseResource();
            houseResource.setPlatform(req.getPlatform());
            BeanUtils.copyProperties(req,houseResource);
            houseResource.setType(req.getType());
            if (StringUtils.hasLength(req.getHousePhoto())){
                houseResource.setHousePhoto(req.getHousePhoto());
            }else{
                houseResource.setHousePhoto("");
            }
            houseResource.setInsertTime(new Date());
            houseResource.setGoodHouse(0);
            houseResource.setIsDelete(0);
            houseResource.setInsertUserId(appUserId);
            houseResource.setViewsNumber(0);
            houseResource.setUpdateTime(new Date());
            // todo 修改回审核和上下架状态
            houseResource.setStatus(1);
            houseResource.setDataType(req.getDataType());
            houseResource.setAuthStatus(1);
            houseResource.setLeaseTime(req.getTime());
            houseResource.setAppUserId(appUserId);
            houseResource.setFirmHouse(req.getFirmHouse());
            if (StringUtils.hasLength(req.getTime())){
                if (req.getTime().contains("年")){
                    houseResource.setRentalDuration(2);
                }else{
                    houseResource.setRentalDuration(1);
                }
            }
            if (req.getType()==2){
                if (req.getId()==null){
                    houseResource.setAuthStatus(2);
                    this.baseMapper.insert(houseResource);
                }else{
                    houseResource.setAuthStatus(2);
                    this.baseMapper.updateById(houseResource);
                }
            }else{
                if (req.getId()==null){
                    houseResource.setAuthStatus(2);
                    this.baseMapper.insert(houseResource);
                }else{
                    houseResource.setAuthStatus(2);
                    this.baseMapper.updateById(houseResource);
                }
            }
            if (req.getType()==2 && req.getId()==null){
                PointLocation pointLocation = new PointLocation();
                GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.valueOf(req.getLongitude()),Double.valueOf(req.getLatitude()));
                pointLocation.setGeoJsonPoint(geoJsonPoint);
                pointLocation.setHouseId(houseResource.getId());
                mongoTemplate.insert(pointLocation);
                houseResource.setCode(pointLocation.getId());
                houseResource.setAuthStatus(2);
                this.baseMapper.updateById(houseResource);
                InputStream inputStream = null;
                OutputStream outputStream = null;
                String accessToken = wxAppletTools.getAccessToken();
                try {
                    String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
                    Map<String, Object> param = new HashMap<>();
                    param.put("scene","id="+houseResource.getId());
                    //pageA/houseDetail
                    param.put("page", "pageA/houseDetail");
                    param.put("check_path", false);
                    param.put("env_version", "trial");
                    param.put("width", 200); //二维码尺寸
                    param.put("is_hyaline", true); // 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 参数仅对小程序码生效
                    param.put("auto_color", true); // 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 参数仅对小程序码生效
                    Map<String, Object> line_color = new HashMap<>();
                    line_color.put("r", 0);
                    line_color.put("g", 0);
                    line_color.put("b", 0);
                    param.put("line_color", line_color);
                    System.err.println("调用生成微信URL接口传参:" + param);
                    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
                    HttpEntity requestEntity = new HttpEntity(param, headers);
                    ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
                    System.err.println("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
                    byte[] result = entity.getBody();
                    System.err.println(Base64.encodeBase64String(result));
                    inputStream = new ByteArrayInputStream(result);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    // 最后上传生成的文件名
                    String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + ".jpg";
                    // oss中的文件夹名
                    String objectName = sdf.format(new Date()) + "/" + finalFileName;
                    // 上传oss
                    String s = ossService.uploadFile2OSS(inputStream, objectName);
                    //获取文件的URl地址
                    String imgUrl = ossService.getImgUrl(objectName);
                    houseResource.setQrCode(imgUrl);
                    this.baseMapper.updateById(houseResource);
                    System.err.println("看看文件路径" + imgUrl);
                } catch (Exception e) {
                    System.err.println("调用小程序生成微信永久小程序码URL接口异常" + e);
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                }
            }
            if (req.getType()==2 && req.getId()!=null){
                HouseResource houseResource1 = this.baseMapper.selectById(req.getId());
                if (!StringUtils.hasLength(houseResource1.getCode())){
                    PointLocation pointLocation = new PointLocation();
                    GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.valueOf(req.getLongitude()),Double.valueOf(req.getLatitude()));
                    pointLocation.setGeoJsonPoint(geoJsonPoint);
                    pointLocation.setHouseId(req.getId());
                    mongoTemplate.insert(pointLocation);
                    houseResource1.setCode(pointLocation.getId());
                    houseResource1.setAuthStatus(2);
                    this.baseMapper.updateById(houseResource1);
                }
                Query query = Query.query(Criteria.where("_id").is(houseResource1.getCode()));
                PointLocation pointLocation = new PointLocation();
                pointLocation.setId(houseResource1.getCode());
                pointLocation.setHouseId(houseResource.getId());
                GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.valueOf(req.getLongitude()),Double.valueOf(req.getLatitude()));
                pointLocation.setGeoJsonPoint(geoJsonPoint);
                org.bson.Document document = (org.bson.Document)mongoTemplate.getConverter() .convertToMongoType(pointLocation);
                Update update = Update.fromDocument (document);
                UpdateResult updateResult = mongoTemplate.updateFirst(query, update, PointLocation.class);
                System.err.println(updateResult.getModifiedCount());
            }
            return ResultUtil.success();
        }
        return ResultUtil.success();
    }
 
    @Override
    public ResultUtil confirm(Integer userType) {
        AppUser appUser = appUserService.getAppUser();
        if (userType==2 && appUser.getUserType()==2){
 
        }else{
            appUser.setChangeTime(new Date());
        }
        appUser.setUserType(userType);
        appUserService.updateById(appUser);
 
        return ResultUtil.success();
    }
 
    @Override
    public List<CollectListRes> collect(Integer id) {
      return this.baseMapper.collect(id);
    }
 
    @Override
    public ResultUtil<Integer> getSurplusPushNumber1() {
        AppUser appUser = appUserService.getAppUser();
        // 判断这个人是什么身份
        if (appUser.getUserType()==2){
            // 最多只能发布三条房源信息
            int size = this.baseMapper.selectList(new EntityWrapper<HouseResource>()
                    .eq("app_user_id", appUser.getId())
                    .eq("is_delete", 0)
                    .eq("type",2)
            ).size();
            if (3-size<0){
                return ResultUtil.success(0);
            }
            return ResultUtil.success(3-size);
        }
        if (appUser.getUserType()==3){
            // 如果是中介 判断他是否认证成功
            if (appUser.getAuth()==2){
                return ResultUtil.success(99999);
            }else{
                // 如果是中介但是还未认证 最多只能上架三条
                int size = this.baseMapper.selectList(new EntityWrapper<HouseResource>()
                        .eq("app_user_id", appUser.getId())
                        .eq("is_delete", 0)
                        .eq("type",2)
                ).size();
                if (3-size<0){
                    return ResultUtil.success(0);
                }
                return ResultUtil.success(3-size);
            }
        }
        return ResultUtil.success(0);
    }
 
 
    public List<CollectListRes> release(UserInfoQuery query, List<Integer> collect) {
        return this.baseMapper.release(query,collect);
    }
}