| | |
| | | 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.HouseResourceMapper; |
| | | import com.stylefeng.guns.modular.system.model.*; |
| | | import com.stylefeng.guns.modular.system.service.*; |
| | | import com.stylefeng.guns.modular.system.warpper.PointLocation; |
| | | import com.stylefeng.guns.modular.system.warpper.req.SearchHouseResourceReq; |
| | | import com.stylefeng.guns.modular.system.warpper.res.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | 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; |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取房源列表 |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @Override |
| | | public SearchHouseResourceRes searchHouseResource(SearchHouseResourceReq req) { |
| | | req.setPageNum((req.getPageNum() - 1) * req.getPageSize()); |
| | | //租房处理推荐和记录历史搜索数据 |
| | | if(req.getType() == 1){ |
| | | fillSearchHistory(req); |
| | | } |
| | | |
| | | //区域 |
| | | List<Integer> districtIds = null; |
| | | List<Integer> cityIds = null; |
| | | 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())){ |
| | | 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); |
| | | searchHouseResource.setList(searchHouseResourceListRes); |
| | | Integer integer = this.baseMapper.searchHouseResourceCount(req, cityIds, districtIds, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds); |
| | | searchHouseResource.setTotal(integer); |
| | | 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:ss"); |
| | | AppUser appUser = appUserService.getAppUser(); |
| | | HouseResource houseResource = this.selectById(id); |
| | | HouseResourceInfoRes houseResourceInfoRes = new HouseResourceInfoRes(); |
| | | houseResourceInfoRes.setId(id); |
| | | houseResourceInfoRes.setTitle(houseResource.getTitle()); |
| | | houseResourceInfoRes.setSaleAmount(houseResource.getSaleAmount()); |
| | | houseResourceInfoRes.setHouseModel(houseResource.getHouseModel()); |
| | | houseResourceInfoRes.setRentalDuration(houseResource.getRentalDuration()); |
| | | houseResourceInfoRes.setSaleDate(sdf.format(houseResource.getSaleDate())); |
| | | houseResourceInfoRes.setHousePhoto(houseResource.getHousePhoto()); |
| | | HouseType houseType = houseTypeService.selectById(houseResource.getHouseTypeId()); |
| | | houseResourceInfoRes.setHouseType(houseType.getName()); |
| | | houseResourceInfoRes.setFloor(houseResource.getFloor()); |
| | | houseResourceInfoRes.setElevator(houseResource.getElevator()); |
| | | houseResourceInfoRes.setDryingArea(houseResource.getDryingArea()); |
| | | houseResourceInfoRes.setHouseArea(houseResource.getHouseArea()); |
| | | houseResourceInfoRes.setKeepPet(houseResource.getKeepPet()); |
| | | Region region = regionService.selectById(houseResource.getDistrictId()); |
| | | Region region1 = regionService.selectById(houseResource.getCityId()); |
| | | 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())); |
| | | 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范围内的房源数据 |
| | | 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()); |
| | | List<HouseResource> houseResources = this.selectBatchIds(ids); |
| | | List<SearchHouseResourceListRes> list = new ArrayList<>(); |
| | | //遍历解析出返回数据 |
| | | for (HouseResource resource : houseResources) { |
| | | SearchHouseResourceListRes searchHouseResourceListRes = new SearchHouseResourceListRes(); |
| | | searchHouseResourceListRes.setId(resource.getId()); |
| | | AppUser appUser = appUserService.selectById(resource.getAppUserId()); |
| | | searchHouseResourceListRes.setHouseResource(appUser.getUserType()); |
| | | 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()); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取联系方式 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ContactInformationRes getContactInformation(Integer id) { |
| | | HouseResource houseResource = this.selectById(id); |
| | | AppUser appUser = appUserService.selectById(houseResource.getAppUserId()); |
| | | ContactInformationRes contactInformationRes = new ContactInformationRes(); |
| | | contactInformationRes.setWhatsApp(appUser.getWatchApp()); |
| | | contactInformationRes.setPhone(appUser.getPhone()); |
| | | return contactInformationRes; |
| | | } |
| | | |
| | | |
| | | |
| | | } |