Parcourir la source

优化:修复立即购买和推荐产品展示接口在商品数据少时(不是没数据),随机返回集为空情况。

lym il y a 4 ans
Parent
commit
3b7fa1c36d

+ 57 - 15
carbon-h5/carbon-h5-service/src/main/java/com/hcloud/microserver/h5/service/impl/GoodsManageServiceImpl.java

@@ -2,6 +2,7 @@ package com.hcloud.microserver.h5.service.impl;
 
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
+import com.hcloud.microserver.commoncore.enums.ResultEnum;
 import com.hcloud.microserver.commoncore.exception.GlobalException;
 import com.hcloud.microserver.h5.facade.carbon.forms.GoodsInfoForm;
 import com.hcloud.microserver.h5.bo.GoodsInfoBO;
@@ -37,6 +38,13 @@ public class GoodsManageServiceImpl implements GoodsManageService {
         Map<String,GoodsInfoForm> resultMap = new HashMap<>();
         if (helpMap != null && helpMap.size() > 0){
             resultList = getRandomPY(resultMap,helpMap,carbonNum,1);
+        }else { //商品少随机分配不能满足
+            PageHelper.startPage(1,20);
+            List<GoodsInfoForm> list = goodsManageMapper.getBuyGoodsInfo(null,"ORDER BY tmp.unsaledCarbonSkin DESC");
+            if (list == null || list.size() < 1){
+                throw new GlobalException(ResultEnum.FAILURE.getCode(),"没有可售产品!");
+            }
+            resultList = getRandom(resultMap,list,carbonNum,1);
         }
 
         return resultList;
@@ -121,7 +129,7 @@ public class GoodsManageServiceImpl implements GoodsManageService {
         for (List<GoodsInfoForm> valueList:values){ //获取随机主体集
             int number = new Random().nextInt(valueList.size());
             GoodsInfoForm info = valueList.get(number);
-            if (resultKeys != null && resultKeys.contains(info.getGuid())){
+            if (resultKeys != null && resultKeys.contains(info.getGuid())){ //随机抽取的对象已存在集合内
                 number += 1;
                 if (valueList.size() > 1 && number != valueList.size()){
                     info = valueList.get(valueList.size() -1);
@@ -166,7 +174,7 @@ public class GoodsManageServiceImpl implements GoodsManageService {
                 if (resultKeys.contains(info.getGuid())) continue; //主体已存在
                 int unsaledCarbonSkin = info.getUnsaledCarbonSkin().intValue();
                 if (unsaledCarbonSkin >= unBuyNum) { //大于购买量
-                    info.setCarbonSkin(BigDecimal.valueOf(Double.valueOf(buyNum)));
+                    info.setCarbonSkin(BigDecimal.valueOf(Double.valueOf(unBuyNum)));
                     total += unBuyNum;
                     unBuyNum = unBuyNum - unBuyNum;
                 } else {
@@ -180,7 +188,7 @@ public class GoodsManageServiceImpl implements GoodsManageService {
         }
 
         //列表循环完,分配不够,追加主体
-        if (total < buyNum){
+        if (total < buyNum && pageNo < 5){
             if (pageNo == 4){
                 throw new GlobalException(1,"购买量过大");
             }
@@ -188,8 +196,14 @@ public class GoodsManageServiceImpl implements GoodsManageService {
             Map<String,List<GoodsInfoForm>> farmList = selectGoodsInfo(pageNo,unBuyNum);
             if (farmList != null && farmList.size() > 0) {
                 getRandomPY(resultMap, farmList, unBuyNum, pageNo);
-            }else {
-                throw new GlobalException(1,"碳汇量不够购买量");
+            }else { //商品少随机分配不能满足,不再随机分配取未售碳汇量多的20条商品
+                Integer pageNum = pageNo - 1; //从第一页开始
+                PageHelper.startPage(pageNum,20);
+                List<GoodsInfoForm> GoodList = goodsManageMapper.getBuyGoodsInfo(null,"ORDER BY tmp.unsaledCarbonSkin DESC");
+                if (GoodList == null || GoodList.size() < 1){
+                    throw new GlobalException(1,"购买量过大");
+                }
+                getRandom(resultMap,GoodList,unBuyNum,pageNo);
             }
         }
 
@@ -205,6 +219,11 @@ public class GoodsManageServiceImpl implements GoodsManageService {
         Map<String,GoodsInfoForm> resultMap = new HashMap<>();
         if (helpMap != null && helpMap.size() > 0){
             resultList = getGoodsRandomPY(resultMap,helpMap,goodsManageQueryForm.getPageNo(),goodsManageQueryForm.getPageSize());
+        }else {//商品少随机分配不能满足
+            resultList = getBuyGoodsInfo(goodsManageQueryForm.getPageNo(),goodsManageQueryForm.getPageSize(),null);
+            if (resultList == null || resultList.size() < 1){
+                throw new GlobalException(ResultEnum.FAILURE.getCode(),"没有可售产品!");
+            }
         }
 
         PageInfo pageInfo = new PageInfo();
@@ -218,18 +237,10 @@ public class GoodsManageServiceImpl implements GoodsManageService {
      * @return
      */
     private Map<String,List<GoodsInfoForm>> getGoodsInfo(Integer pageNo, int pageSize){
-        String orderStr = null;
-        if (pageNo > 20){ //加载超过20页多的排前
-            orderStr = "ORDER BY tmp.unsaledCarbonSkin DESC";
-        }else {
-            orderStr = "ORDER BY tmp.saledCarbonSkin ASC,tmp.unsaledCarbonSkin DESC";
-        }
-
         List<String> RandomStrings = getRandomString(pageSize); //开发主体首字母
         Map<String,List<GoodsInfoForm>> helpMap = new HashMap();
         for (String namePy:RandomStrings){ //循环获取
-            PageHelper.startPage(pageNo,pageSize);
-            List<GoodsInfoForm> list = goodsManageMapper.getBuyGoodsInfo(namePy,orderStr);
+            List<GoodsInfoForm> list = getBuyGoodsInfo(pageNo,pageSize,namePy);
             if (list != null && list.size() > 0)
                 helpMap.put(namePy,list);
         }
@@ -266,11 +277,42 @@ public class GoodsManageServiceImpl implements GoodsManageService {
         if (list != null && list.size() < pageSize && pageNo < 5){ //未达返回数
             pageNo = pageNo + 1;
             helpMap = getGoodsInfo(pageNo,pageSize);
-            getGoodsRandomPY(resultMap,helpMap,pageNo,pageSize);
+            if (helpMap != null && helpMap.size() > 0){
+                getGoodsRandomPY(resultMap,helpMap,pageNo,pageSize);
+            }else { //商品少随机分配不能满足
+                List<GoodsInfoForm> goodlist = getBuyGoodsInfo(1,pageSize,null);
+                if (goodlist != null && goodlist.size() > 0){ //在返回集的数据
+                    for (GoodsInfoForm form:goodlist){
+                        if (!resultKeys.contains(form.getGuid())) //去重复
+                            resultMap.put(form.getGuid(),form);
+                    }
+                }
+            }
         }
 
         list = new ArrayList<>(resultMap.values());
         if (list.size() > pageSize) list = list.subList(0,pageSize);
         return list;
     }
+
+    /**
+     * 查询商品
+     * lym
+     * @param pageNo
+     * @param pageSize
+     * @param namePy
+     * @return
+     */
+    private List<GoodsInfoForm> getBuyGoodsInfo(Integer pageNo,Integer pageSize,String namePy){
+        String orderStr = null;
+        if (pageNo > 20){ //加载超过20页多的排前
+            orderStr = "ORDER BY tmp.unsaledCarbonSkin DESC";
+        }else {
+            orderStr = "ORDER BY tmp.saledCarbonSkin ASC,tmp.unsaledCarbonSkin DESC";
+        }
+        PageHelper.startPage(pageNo,pageSize);
+        List<GoodsInfoForm> list = goodsManageMapper.getBuyGoodsInfo(namePy,orderStr);
+        return list;
+    }
+
 }