|
@@ -3,6 +3,7 @@ package shop.alien.store.controller;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -18,12 +19,18 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import shop.alien.entity.result.R;
|
|
import shop.alien.entity.result.R;
|
|
|
|
|
+import shop.alien.entity.store.StoreUser;
|
|
|
import shop.alien.entity.store.vo.StoreInfoVo;
|
|
import shop.alien.entity.store.vo.StoreInfoVo;
|
|
|
|
|
+import shop.alien.mapper.StoreUserMapper;
|
|
|
|
|
+import shop.alien.store.service.StoreImgService;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+import java.util.Comparator;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -35,10 +42,12 @@ import java.util.Map;
|
|
|
@RefreshScope
|
|
@RefreshScope
|
|
|
public class AiSearchController {
|
|
public class AiSearchController {
|
|
|
|
|
|
|
|
|
|
+ private final StoreUserMapper storeUserMapper;
|
|
|
@Value("${third-party-ai-search.base-url:http://124.93.18.180:7870/api/v1/search}")
|
|
@Value("${third-party-ai-search.base-url:http://124.93.18.180:7870/api/v1/search}")
|
|
|
private String aiSearchUrl;
|
|
private String aiSearchUrl;
|
|
|
|
|
|
|
|
private final RestTemplate restTemplate;
|
|
private final RestTemplate restTemplate;
|
|
|
|
|
+ private final StoreImgService storeImgService;
|
|
|
|
|
|
|
|
@RequestMapping("/search")
|
|
@RequestMapping("/search")
|
|
|
public R search(@RequestBody Map<String,String> map) {
|
|
public R search(@RequestBody Map<String,String> map) {
|
|
@@ -51,6 +60,8 @@ public class AiSearchController {
|
|
|
requestBody.put("user_lat", map.get("lat"));
|
|
requestBody.put("user_lat", map.get("lat"));
|
|
|
requestBody.put("user_lng", map.get("lon"));
|
|
requestBody.put("user_lng", map.get("lon"));
|
|
|
requestBody.put("category", map.get("category"));
|
|
requestBody.put("category", map.get("category"));
|
|
|
|
|
+ requestBody.put("page", map.get("pageNum"));
|
|
|
|
|
+ requestBody.put("sort_by", map.get("sortBy"));
|
|
|
HttpHeaders aiHeaders = new HttpHeaders();
|
|
HttpHeaders aiHeaders = new HttpHeaders();
|
|
|
aiHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
aiHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
// aiHeaders.set("Authorization", "Bearer " + accessToken);
|
|
// aiHeaders.set("Authorization", "Bearer " + accessToken);
|
|
@@ -62,14 +73,53 @@ public class AiSearchController {
|
|
|
String body = stringResponseEntity.getBody();
|
|
String body = stringResponseEntity.getBody();
|
|
|
JSONObject jsonObject = JSONObject.parseObject(body);
|
|
JSONObject jsonObject = JSONObject.parseObject(body);
|
|
|
JSONObject jsonObject1 = new JSONObject();
|
|
JSONObject jsonObject1 = new JSONObject();
|
|
|
- if ("生活服务".equals(requestBody.get("category")) || "休闲娱乐".equals(requestBody.get("category"))) {
|
|
|
|
|
|
|
+// if ("生活服务".equals(requestBody.get("category")) || "休闲娱乐".equals(requestBody.get("category"))) {
|
|
|
// 生活服务类别:转换为StoreInfoVo,确保返回的字段名按照StoreInfoVo定义
|
|
// 生活服务类别:转换为StoreInfoVo,确保返回的字段名按照StoreInfoVo定义
|
|
|
List<StoreInfoVo> result = convertToStoreInfoList(jsonObject.getJSONArray("results"));
|
|
List<StoreInfoVo> result = convertToStoreInfoList(jsonObject.getJSONArray("results"));
|
|
|
|
|
+
|
|
|
|
|
+ // 从结果中提取所有的id,放到集合中
|
|
|
|
|
+ List<Integer> storeIdList = result.stream()
|
|
|
|
|
+ .map(StoreInfoVo::getId)
|
|
|
|
|
+ .filter(id -> id != null)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ // 使用storeImgService批量查询,imgType为10
|
|
|
|
|
+ List<StoreUser> storeImgList = new ArrayList<>();
|
|
|
|
|
+ if (!storeIdList.isEmpty()) {
|
|
|
|
|
+ QueryWrapper<StoreUser> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
+ queryWrapper.in("store_id", storeIdList);
|
|
|
|
|
+ storeImgList = storeUserMapper.selectList(queryWrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 将storeImgList按照storeId分组,并关联到对应的result中
|
|
|
|
|
+ Map<Integer, List<StoreUser>> storeImgMap = storeImgList.stream()
|
|
|
|
|
+ .collect(Collectors.groupingBy(StoreUser::getStoreId));
|
|
|
|
|
+
|
|
|
|
|
+ // 将图片列表设置到对应的StoreInfoVo中
|
|
|
|
|
+ for (StoreInfoVo storeInfo : result) {
|
|
|
|
|
+ if (storeInfo.getId() != null) {
|
|
|
|
|
+ List<StoreUser> imgs = storeImgMap.get(storeInfo.getId());
|
|
|
|
|
+ if (imgs != null && !imgs.isEmpty()) {
|
|
|
|
|
+ // 将图片列表转换为URL列表,按照imgSort排序
|
|
|
|
|
+ List<String> imgUrlList = imgs.stream()
|
|
|
|
|
+ .sorted(Comparator.comparing(StoreUser::getStoreId))
|
|
|
|
|
+ .map(StoreUser::getHeadImg)
|
|
|
|
|
+ .filter(url -> url != null)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ storeInfo.setStoreAlbumUrlList(imgUrlList);
|
|
|
|
|
+ // 如果第一个图片存在,也可以设置到imgUrl字段作为头像
|
|
|
|
|
+ if (!imgUrlList.isEmpty()) {
|
|
|
|
|
+ storeInfo.setImgUrl(imgUrlList.get(0));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
jsonObject1.put("records", result);
|
|
jsonObject1.put("records", result);
|
|
|
- } else {
|
|
|
|
|
- // 其他类别:直接返回原始结果
|
|
|
|
|
- jsonObject1.put("records", jsonObject.get("results"));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// } else {
|
|
|
|
|
+// // 其他类别:直接返回原始结果
|
|
|
|
|
+// jsonObject1.put("records", jsonObject.get("results"));
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
jsonObject1.put("total", jsonObject.get("total"));
|
|
jsonObject1.put("total", jsonObject.get("total"));
|
|
|
jsonObject1.put("size", map.get("pageSize"));
|
|
jsonObject1.put("size", map.get("pageSize"));
|