luo
2023-12-25 23c4967b4cb8dbce8277f830f7152d315c5a4a57
guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/HousingDemandServiceImpl.java
@@ -3,14 +3,17 @@
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.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.file.OSSService;
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.util.WxAppletTools;
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;
@@ -19,14 +22,23 @@
import com.stylefeng.guns.modular.system.warpper.res.SearchHousingDemandRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @author zhibing.pu
@@ -46,8 +58,12 @@
    @Autowired
    private ICollectionHousingDemandService collectionHousingDemandService;
    @Autowired
    private WxAppletTools wxAppletTools;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OSSService ossService;
    /**
     * 添加房源需求
@@ -59,8 +75,14 @@
        if(null == appUser){
            return ResultUtil.tokenErr();
        }
        String s=null;
        if (StringUtils.hasLength(req.getDistrict())){
            s = req.getDistrict().replaceAll("\"", "");
        }
        HousingDemand housingDemand = new HousingDemand();
        housingDemand.setType(req.getType());
        BeanUtils.copyProperties(req, housingDemand);
        housingDemand.setDistrict(s);
        try {
            housingDemand.setCode(UUIDUtil.getRandomCode(8));
        } catch (Exception e) {
@@ -69,7 +91,68 @@
        housingDemand.setIsDelete(0);
        housingDemand.setInsertTime(new Date());
        housingDemand.setInsertUserId(appUser.getId());
        this.insert(housingDemand);
        housingDemand.setAppUserId(appUser.getId());
        housingDemand.setStatus(1);
        if (req.getType()==2){
            InputStream inputStream = null;
            OutputStream outputStream = null;
            String accessToken = wxAppletTools.getAccessToken();
            try {
                String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + accessToken;
                Map<String, Object> param = new HashMap<>();
                param.put("path", "pages/home/home");
//            param.put("page", "pages/index/index"); // 路径 如果没有默认跳转到首页面微信小程序发布后才可以使用不能添加参数
                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
                ossService.uploadFile2OSS(inputStream, objectName);
                //获取文件的URl地址
                String imgUrl = ossService.getImgUrl(objectName);
                housingDemand.setQrCode(imgUrl);
                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;
                }
            }
            this.insert(housingDemand);
        }else{
            this.updateById(housingDemand);
        }
        List<HousingDemandDistrict> list = new ArrayList<>();
        if(ToolUtil.isNotEmpty(req.getDistrict())){
            JSONArray jsonArray = JSON.parseArray(req.getDistrict());
@@ -102,31 +185,37 @@
     */
    @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;
        req.setPageNum((req.getPageNum() - 1) * req.getPageSize());
        //区域一级
        List<Integer> districtIds = new ArrayList<>();
        // 二级
        List<Integer> areaIds = new ArrayList<>();
        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) {
                    areaIds.add(Integer.valueOf(s));
                }
                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]);
            if (req.getSaleAmount().contains("以上")){
                saleAmountEnd =1000000.0;
                saleAmountStart = 2000.0;
            }else if (req.getSaleAmount().contains("以下")){
                saleAmountEnd =500.0;
                saleAmountStart = 0.0;
            }else{
                String[] split = req.getSaleAmount().split("-");
                saleAmountStart = Double.valueOf(split[0]);
                saleAmountEnd = Double.valueOf(split[1]);
            }
        }
        //户型
        List<String> houseModels = null;
@@ -144,8 +233,58 @@
            }
        }
        SearchHousingDemandRes searchHouseResource = new SearchHousingDemandRes();
        List<SearchHousingDemandListRes> searchHouseResourceListRes = this.baseMapper.searchHousingDemand(req, district, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
        List<SearchHousingDemandListRes> searchHouseResourceListRes = this.baseMapper.searchHousingDemand
                (req, districtIds,areaIds, 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);
        }
        List<SearchHousingDemandListRes> collect = new ArrayList<>();
        if (req.getRentalDuration()!=null){
            if (req.getRentalDuration().equals("一年以上")){
                collect = searchHouseResourceListRes.stream().filter(t -> t.getRentalDuration() >= 12)
                        .collect(Collectors.toList());
            }else if (req.getRentalDuration().equals("一年以下")){
                collect = searchHouseResourceListRes.stream().filter(t -> t.getRentalDuration() < 12)
                        .collect(Collectors.toList());
            }
            searchHouseResource.setList(collect);
            searchHouseResource.setTotal(collect.size());
        }else{
            searchHouseResource.setList(searchHouseResourceListRes);
            searchHouseResource.setTotal(searchHouseResourceListRes.size());
        }
        Integer integer = this.baseMapper.searchHousingDemandCount(req, districtIds,areaIds, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
        return searchHouseResource;
    }
    /**
     * 搜索求房源列表数据
     * @return
     */
    @Override
    public SearchHousingDemandRes searchHousingDemand1(Integer id) {
        SearchHousingDemandRes searchHouseResource = new SearchHousingDemandRes();
        List<SearchHousingDemandListRes> searchHouseResourceListRes = this.baseMapper.searchHousingDemand1(id);
        for (SearchHousingDemandListRes searchHouseResourceListRe : searchHouseResourceListRes) {
            List<HousingDemandDistrict> list = housingDemandDistrictService.selectList(new EntityWrapper<HousingDemandDistrict>()
@@ -161,14 +300,35 @@
            searchHouseResourceListRe.setAddress(dis.size() == 0 ? null : dis);
        }
        searchHouseResource.setList(searchHouseResourceListRes);
        Integer integer = this.baseMapper.searchHousingDemandCount(req, district, saleAmountStart, saleAmountEnd, houseModels, houseTypeIds);
        searchHouseResource.setTotal(integer);
        searchHouseResource.setTotal(searchHouseResourceListRes.size());
        return searchHouseResource;
    }
    @Override
    public SearchHousingDemandRes searchHousingDemand2(List<Integer> id) {
        SearchHousingDemandRes searchHouseResource = new SearchHousingDemandRes();
        List<SearchHousingDemandListRes> searchHouseResourceListRes = this.baseMapper.searchHousingDemand2(id);
        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);
        searchHouseResource.setTotal(searchHouseResourceListRes.size());
        return searchHouseResource;
    }
    /**
     * 获取求房源详情
     * 不带分页的求房源列表
     * @param id
     * @return
     */
@@ -179,18 +339,22 @@
        HousingDemandInfoRes housingDemandInfoRes = new HousingDemandInfoRes();
        AppUser appUser = appUserService.getAppUser();
        housingDemandInfoRes.setId(id);
        housingDemandInfoRes.setDataType(housingDemand.getDataType());
        housingDemandInfoRes.setQrCode(housingDemand.getQrCode());
        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() + " / ";
        if (StringUtils.hasLength(housingDemand.getHouseTypeId())){
            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.setHouseType(houseType.substring(0, houseType.lastIndexOf("/")));
        housingDemandInfoRes.setFloor(housingDemand.getFloor());
        housingDemandInfoRes.setElevator(housingDemand.getElevator());
        housingDemandInfoRes.setDryingArea(housingDemand.getDryingArea());
@@ -213,13 +377,20 @@
        housingDemandInfoRes.setProfilePhoto(appUser1.getProfilePhoto());
        housingDemandInfoRes.setNickname(appUser1.getNickname());
        housingDemandInfoRes.setInsertTime(sdf.format(housingDemand.getInsertTime()));
        housingDemandInfoRes.setUpdateTime(sdf.format(housingDemand.getUpdateTime()));
        if (housingDemand.getUpdateTime()==null){
            housingDemandInfoRes.setUpdateTime(sdf.format(housingDemand.getInsertTime()));
        }else{
            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()));
            int collection = collectionHousingDemandService
                    .selectCount(new EntityWrapper<CollectionHousingDemand>()
                            .eq("housing_demand_id", id)
                            .eq("app_user_id", appUser.getId()));
            housingDemandInfoRes.setCollection(0 == collection ? 0 : 1);
        }
        //添加访问次数记录
@@ -250,6 +421,7 @@
        ContactInformationRes contactInformationRes = new ContactInformationRes();
        contactInformationRes.setWhatsApp(appUser.getWatchApp());
        contactInformationRes.setPhone(appUser.getPhone());
        contactInformationRes.setWechatQrCode(appUser.getWechatQRCode());
        return contactInformationRes;
    }