|
|
@@ -0,0 +1,91 @@
|
|
|
+package com.poyee.service;
|
|
|
+
|
|
|
+import com.alibaba.excel.util.StringUtils;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.poyee.common.exception.ServiceException;
|
|
|
+import com.poyee.dto.ProductPromGroupInfoDto;
|
|
|
+import com.poyee.dto.task.PromotionTaskPageDto;
|
|
|
+import com.poyee.enums.HttpMethod;
|
|
|
+import com.poyee.enums.ProductTypeEnums;
|
|
|
+import com.poyee.util.http.HttpClientUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取商品信息
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ProductInfoService {
|
|
|
+
|
|
|
+ @Value("${uri.product:https://m2-dev.hobbystocks.cn/py-app/api/local/groupInfoByIds?ids=}")
|
|
|
+ private String groupUri;
|
|
|
+
|
|
|
+ private String shopUri;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据商品类型和商品/拼团id获取商品信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void getProductInfoByIds(List<PromotionTaskPageDto> rows) {
|
|
|
+ //获取数据
|
|
|
+ for(ProductTypeEnums type : ProductTypeEnums.NORMAL){
|
|
|
+ List<Integer> ids = getIdsByType(rows, type);
|
|
|
+ if(CollectionUtils.isEmpty(ids)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String url = "";
|
|
|
+ String idsStr = ids.stream()
|
|
|
+ .map(String::valueOf)
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+ switch (type){
|
|
|
+// case SHOP:
|
|
|
+// url = shopUri + idsStr;
|
|
|
+// break;
|
|
|
+ case GROUP:
|
|
|
+ url = groupUri + idsStr;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ log.error("商品类型【{}】不查询:", type);
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotBlank(url)) {
|
|
|
+ String execute = HttpClientUtil.create()
|
|
|
+ .uri(url, HttpMethod.GET)
|
|
|
+ .build()
|
|
|
+ .execute();
|
|
|
+ if(StringUtils.isNotBlank(execute)){
|
|
|
+ try {
|
|
|
+ JSONArray data = JSONObject.parseObject(execute).getJSONArray("data");
|
|
|
+ if(CollectionUtils.isNotEmpty(data)){
|
|
|
+ List<ProductPromGroupInfoDto> infos = JSONArray.parseArray(JSONObject.toJSONString(data), ProductPromGroupInfoDto.class);
|
|
|
+ for(ProductPromGroupInfoDto info : infos){
|
|
|
+ rows.stream()
|
|
|
+ .filter(dto -> Objects.equals(dto.getType(),type.getType()) && Objects.equals(dto.getProductId(),String.valueOf(info.getId())))
|
|
|
+ .forEach(dto -> dto.setProductInfo(info));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e) {
|
|
|
+ log.error("获取商品信息失败:", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<Integer> getIdsByType(List<PromotionTaskPageDto> rows, ProductTypeEnums typeEnums) {
|
|
|
+ return rows.stream()
|
|
|
+ .filter(dto -> Objects.equals(dto.getType(), typeEnums.getType()))
|
|
|
+ .map(PromotionTaskPageDto::getProductId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .map(Integer::valueOf)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|