44323
2024-02-22 aa8ff2d61669d0779fdacdba76e26388587b435d
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
package com.stylefeng.guns.modular.api;
 
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.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.stylefeng.guns.modular.file.OSSService;
import com.stylefeng.guns.modular.system.model.AppUser;
import com.stylefeng.guns.modular.system.model.HouseResource;
import com.stylefeng.guns.modular.system.model.HouseType;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.HttpRequestUtil;
import com.stylefeng.guns.modular.system.util.HttpUtils;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.WxAppletTools;
import com.stylefeng.guns.modular.system.warpper.req.*;
import com.stylefeng.guns.modular.system.warpper.res.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author zhibing.pu
 * @Date 2023/11/8 15:10
 */
@RestController
@RequestMapping("")
public class HouseResourceController {
 
    @Autowired
    private IHouseResourceService houseResourceService;
    @Autowired
    private IHouseTypeService houseTypeService;
    @Autowired
    private ICollectionHouseResourceService collectionHouseResourceService;
    @Autowired
    private IReportHouseResourceService reportHouseResourceService;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private WxAppletTools wxAppletTools;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OSSService ossService;
    @Value("${wx.appletsAppid}")
    private String wxAppletsAppid;
 
    @Value("${wx.appletsAppSecret}")
    private String wxAppletsAppSecret;
 
 
    /**
     * 生成小程序码地址
     */
    public static final String CREATE_CODE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN";
    // 生成小程序码
    private String generateMiniProgramCode(String apiUrl, String pagePath, String outputFilePath) throws IOException {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 
        // 设置请求方法
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
 
        // 构建请求参数
        String params = "path=" + URLEncoder.encode(pagePath, "UTF-8");
 
        // 获取输出流
        try (OutputStream outputStream = connection.getOutputStream()) {
            outputStream.write(params.getBytes());
        }
 
        // 获取输入流
        try (InputStream inputStream = connection.getInputStream();
             FileOutputStream outputStream = new FileOutputStream(outputFilePath)) {
            // 将返回的图片数据写入文件
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
            MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), byteArrayInputStream);
            return ossService.uploadFile(file);
        }
    }
    @ResponseBody
    @GetMapping("/base/houseResource/qrCode/{id}")
    @ApiOperation(value = "海报二维码", tags = {"房源"})
    public ResultUtil<String> poster(@PathVariable("id") Integer id){
        HouseResource houseResource = houseResourceService.selectById(id);
        return ResultUtil.success(houseResource.getQrCode());
    }
    @ResponseBody
    @GetMapping("/base/houseResource/historyTitle")
    @ApiOperation(value = "选择之前填入过的标题", tags = {"房源"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil<List<String>> historyTitle(){
        Integer id = appUserService.getAppUser().getId();
        List<String> titles = houseResourceService.selectList(new EntityWrapper<HouseResource>()
                .eq("app_user_id", id)).stream().filter(t-> StringUtils.hasLength(t.getTitle())).map(HouseResource::getTitle)
                .collect(Collectors.toList());
        return ResultUtil.success(titles);
    }
    @ResponseBody
    @GetMapping("/base/houseResource/historyIntroduce")
    @ApiOperation(value = "选择之前填入过的更多介绍", tags = {"房源"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil<List<String>> historyIntroduce(){
        Integer id = appUserService.getAppUser().getId();
        List<String> titles = houseResourceService.selectList(new EntityWrapper<HouseResource>()
                .eq("app_user_id", id)).stream().filter(t-> StringUtils.hasLength(t.getMoreIntroduction())).map(HouseResource::getMoreIntroduction)
                .collect(Collectors.toList());
        return ResultUtil.success(titles);
    }
    @ResponseBody
    @GetMapping("/base/houseResource/getHouse")
    @ApiOperation(value = "根据选择的区域 获取附近小区", tags = {"房源"})
    public ResultUtil<List<String>> getHouse(String cityName){
        List<String> res = new ArrayList<>();
        String url = "https://apis.map.qq.com/ws/place/v1/search?boundary=region(香港,0)&keyword="+cityName+"&filter=category=住宅区,别墅,社区,宿舍&page_size=30&page_index=1&key=G52BZ-X5AKH-V2JDQ-WSLRK-7DSMZ-YWFXZ";
        String s = HttpUtils.sendGet(url);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(s, JsonObject.class);
        JsonArray dataArray = jsonObject.getAsJsonArray("data");
        List<String> titles = new ArrayList<>();
        for (JsonElement element : dataArray) {
            JsonObject dataObject = element.getAsJsonObject();
            String title = dataObject.get("title").getAsString();
            titles.add(title);
        }
        for (String title : titles) {
            res.add(title);
        }
        return ResultUtil.success(res);
    }
 
    public static void main(String[] args) {
        String url = "https://apis.map.qq.com/ws/place/v1/search?boundary=region(香港,0)&keyword=上环/中环/金钟&filter=category=住宅区,别墅,社区,宿舍&page_size=30&page_index=1&key=G52BZ-X5AKH-V2JDQ-WSLRK-7DSMZ-YWFXZ";
        String s = HttpUtils.sendGet(url);
        // 使用Gson库解析JSON
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(s, JsonObject.class);
        JsonArray dataArray = jsonObject.getAsJsonArray("data");
 
        // 遍历data数组,提取title字段值并放入集合
        List<String> titles = new ArrayList<>();
        for (JsonElement element : dataArray) {
            JsonObject dataObject = element.getAsJsonObject();
            String title = dataObject.get("title").getAsString();
            titles.add(title);
        }
 
        // 打印集合中的title字段值
        for (String title : titles) {
            System.out.println(title);
        }
    }
    // todo 放行
    @ResponseBody
    @PostMapping("/base/addHouse/confirm")
    @ApiOperation(value = "中介身份提示", tags = {"发布"})
    @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....",
            required = true, paramType = "header")
    public ResultUtil confirm(Integer userType){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser && (appUser.getStatus() == 2|| appUser.getStatus() == 3)){
            return ResultUtil.errorLogin("当前账号已被冻结或删除");
        }
        return houseResourceService.confirm(userType);
    }
 
    @ResponseBody
    @PostMapping("/base/addHouse/userInfo")
    @ApiOperation(value = "发布房源获取当前人的中介认证状态", tags = {"发布"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil userInfo(){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser && (appUser.getStatus() == 2|| appUser.getStatus() == 3)){
            return ResultUtil.errorLogin("当前账号已被冻结或删除");
        }
        Integer auth = appUserService.getAppUser().getAuth();
        return ResultUtil.success(appUser);
    }
 
    @ResponseBody
    @PostMapping("/base/addHouse/add")
    @ApiOperation(value = "发布房源", tags = {"发布"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil addHouse(@RequestBody AddHouseReq req){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser && (appUser.getStatus() == 2|| appUser.getStatus() == 3)){
            return ResultUtil.errorLogin("当前账号已被冻结或删除");
        }
        return houseResourceService.addHouse(req);
    }
 
    @ResponseBody
    @GetMapping("/base/houseResource/searchHouseResource")
    @ApiOperation(value = "获取房源列表", tags = {"首页", "地图房源"})
    public ResultUtil<SearchHouseResourceRes> searchHouseResource(SearchHouseResourceReq req){
        SearchHouseResourceRes searchHouseResource = houseResourceService.searchHouseResource(req);
        return ResultUtil.success(searchHouseResource);
    }
 
    @ResponseBody
    @GetMapping("/base/houseType/getHouseType")
    @ApiOperation(value = "获取房源类型", tags = {"首页"})
    public ResultUtil<List<HouseType>> getHouseType(){
        List<HouseType> houseTypes = houseTypeService.selectList(null);
        return ResultUtil.success(houseTypes);
    }
 
    @ResponseBody
    @GetMapping("/base/houseResource/getDistrictHouseResourceNumber")
    @ApiOperation(value = "获取行政区域房源数量", tags = {"地图房源"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userType", value = "2=个人,3=中介,4=公司盘", required = true),
            @ApiImplicitParam(name = "dataType", value = "1=租房,2=卖房", required = true)
    })
    public ResultUtil<List<DistrictHouseResourceNumberRes>> getDistrictHouseResourceNumber(Integer userType, Integer dataType){
        List<DistrictHouseResourceNumberRes> districtHouseResourceNumber = houseResourceService.getDistrictHouseResourceNumber(userType, dataType);
        return ResultUtil.success(districtHouseResourceNumber);
    }
 
    @ResponseBody
    @PostMapping("/base/houseResource/getHouseResourceInfo")
    @ApiOperation(value = "获取房源详情", tags = {"详情"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源id", required = true),
//            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = false, paramType = "header")
    })
    public ResultUtil<HouseResourceInfoRes> getHouseResourceInfo(Integer id){
        HouseResourceInfoRes houseResourceInfo = houseResourceService.getHouseResourceInfo(id);
        return ResultUtil.success(houseResourceInfo);
    }
 
    @ResponseBody
    @PostMapping("/base/houseResource/collectionHouseResource")
    @ApiOperation(value = "收藏/取消收藏房源操作", tags = {"详情"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源id", required = true),
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil collectionHouseResource(Integer id){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser && (appUser.getStatus() == 2|| appUser.getStatus() == 3)){
            return ResultUtil.errorLogin("当前账号已被冻结或删除");
        }
        return collectionHouseResourceService.collectionHouseResource(id);
    }
 
    @ResponseBody
    @GetMapping("/base/houseResource/getNearbyHouseResource")
    @ApiOperation(value = "获取房源详情中的附近房源", tags = {"详情"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源id", required = true),
    })
    public ResultUtil<List<SearchHouseResourceListRes>> getNearbyHouseResource(Integer id){
        List<SearchHouseResourceListRes> nearbyHouseResource = houseResourceService.getNearbyHouseResource(id);
        return ResultUtil.success(nearbyHouseResource);
    }
 
    @ResponseBody
    @GetMapping("/base/houseResource/getContactInformation/{id}")
    @ApiOperation(value = "获取联系方式", tags = {"详情"})
    public ResultUtil<ContactInformationRes> getContactInformation(@PathVariable("id") Integer id){
        ContactInformationRes contactInformation = houseResourceService.getContactInformation(id);
        return ResultUtil.success(contactInformation);
    }
 
    @ResponseBody
    @PostMapping("/base/houseResource/addReportHouseResource")
    @ApiOperation(value = "添加房源举报", tags = {"详情"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil addReportHouseResource(@RequestBody ReportHouseResourceReq req){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser && (appUser.getStatus() == 2|| appUser.getStatus() == 3)){
            return ResultUtil.errorLogin("当前账号已被冻结或删除");
        }
        return reportHouseResourceService.addReportHouseResource(req);
    }
 
    @ResponseBody
    @PostMapping("/base/intermediary/list")
    @ApiOperation(value = "找中介", tags = {"服务"})
    public ResultUtil<SearchIntermediaryRes> searchIntermediaryList(@RequestBody SearchIntermediaryReq req){
        SearchIntermediaryRes res= appUserService.searchIntermediaryList(req);
        return ResultUtil.success(res);
    }
 
    @ResponseBody
    @PostMapping("/base/intermediary/intermediaryInfo")
    @ApiOperation(value = "找中介-中介信息", tags = {"服务"})
    public ResultUtil<SearchIntermediaryListRes> searchIntermediaryInfo(Integer id){
        SearchIntermediaryListRes res= appUserService.searchIntermediaryInfo(id);
        if (res==null){
            SearchIntermediaryListRes searchIntermediaryListRes = new SearchIntermediaryListRes();
            AppUser appUser = appUserService.selectById(id);
            searchIntermediaryListRes.setId(id);
            searchIntermediaryListRes.setNickname(appUser.getNickname());
            searchIntermediaryListRes.setCompanyName(appUser.getCompanyName());
            searchIntermediaryListRes.setCompanyAddress(appUser.getCompanyAddress());
            searchIntermediaryListRes.setIntroduce(appUser.getIntroduce());
            searchIntermediaryListRes.setProfilePhoto(appUser.getProfilePhoto());
            searchIntermediaryListRes.setPhone(appUser.getPhone());
            searchIntermediaryListRes.setHouseCount(0);
            return ResultUtil.success(searchIntermediaryListRes);
        }
        return ResultUtil.success(res);
    }
 
    @ResponseBody
    @GetMapping("/base/intermediary/getContactInfo/{id}")
    @ApiOperation(value = "获取中介联系方式", tags = {"详情"})
    public ResultUtil<AppUser> getContactInfo(@PathVariable("id") Integer id){
        AppUser appUser = appUserService.selectById(id);
        return ResultUtil.success(appUser);
    }
    @ResponseBody
    @PostMapping("/base/intermediary/listHouse")
    @ApiOperation(value = "个人详情-我的房源", tags = {"服务"})
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil<SearchHouseResourceRes> listHouse(@RequestBody HouseQuery query){
        SearchHouseResourceRes res= houseResourceService.listHouse(query);
        return ResultUtil.success(res);
    }
 
    @ResponseBody
    @PostMapping("/base/housingDemand/getSurplusPushNumber1")
    @ApiOperation(value = "获取剩余发布数量", tags = {"房源"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header")
    })
    public ResultUtil getSurplusPushNumber1(){
        AppUser appUser = appUserService.getAppUser();
        if(null != appUser && (appUser.getStatus() == 2|| appUser.getStatus() == 3)){
            return ResultUtil.errorLogin("当前账号已被冻结或删除");
        }
        return houseResourceService.getSurplusPushNumber1();
    }
}