yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
fanli/src/main/java/com/yeshi/fanli/controller/client/v2/BrandControllerV2.java
@@ -15,6 +15,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.brand.TaoBaoShopHistory;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetail;
@@ -25,8 +26,10 @@
import com.yeshi.fanli.service.inter.brand.TaoBaoShopHistoryService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoShopService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
@@ -56,15 +59,14 @@
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
   @Resource
   private DaTaoKeGoodsService daTaoKeGoodsService;
   @Resource
   private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
   /**
    * 店铺列表
@@ -74,16 +76,91 @@
    */
   @RequestMapping(value = "getShopList", method = RequestMethod.POST)
   public void getShopList(AcceptData acceptData, Integer page, Long cid, PrintWriter out) {
      try {
         if (page == null || page < 1) {
            page = 1;
         }
         JSONObject data = brandClassShopService.listEffectiveCacheV2(page, cid);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadFalseResult("查询失败"));
      if (page == null || page < 1) {
         page = 1;
      }
      JSONObject data = brandClassShopService.listEffectiveCacheV2(page, cid);
      out.print(JsonUtil.loadTrueResult(data));
   }
   /**
    * 店铺足迹
    *
    * @param acceptData
    * @param type
    *            精选1, 足迹列表2
    * @param out
    */
   @RequestMapping(value = "getHistory", method = RequestMethod.POST)
   public void getHistory(AcceptData acceptData, Integer page, Long uid, Integer type, PrintWriter out) {
      if (type == null) {
         out.print(JsonUtil.loadFalseResult("类型不正确"));
         return;
      }
      if (page == null || page < 1) {
         page = 1;
      }
      int pageSize;
      if (type == 1) {
         pageSize = 4;
      } else {
         pageSize = Constant.PAGE_SIZE;
      }
      long count = 0;
      List<TaoBaoShopHistory> listHistory = null;
      if (uid == null || uid == 0) {
         String device = acceptData.getDevice();
         count = taoBaoShopHistoryService.countByDevice(device);
         if (count > 0) {
            listHistory = taoBaoShopHistoryService.listByDevice((page - 1) * pageSize, pageSize, device);
         }
      } else {
         count = taoBaoShopHistoryService.countByUid(uid);
         if (count > 0) {
            listHistory = taoBaoShopHistoryService.listByUid((page - 1) * pageSize, pageSize, uid);
         }
      }
      List<TaoBaoShop> list = new ArrayList<TaoBaoShop>();
      if (listHistory != null && listHistory.size() > 0) {
         for (TaoBaoShopHistory history : listHistory) {
            TaoBaoShop taoBaoShop = history.getTaoBaoShop();
            if (taoBaoShop != null) {
               Integer userType = taoBaoShop.getUserType();
               if (userType == null || userType == 0) {
                  taoBaoShop.setUserType(10);
               } else {
                  taoBaoShop.setUserType(11);
               }
               String shopLink = taoBaoShop.getShopLink();
               if (StringUtil.isNullOrEmpty(shopLink)) {
                  taoBaoShop.setShopLink(TaoBaoUtil.getShopLink(taoBaoShop.getId()));
               }
               String shopNameCustom = taoBaoShop.getShopNameCustom();
               if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
                  taoBaoShop.setShopName(shopNameCustom);
               }
               String shopIconCustom = taoBaoShop.getShopIconCustom();
               if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
                  taoBaoShop.setShopIcon(shopIconCustom);
               }
               list.add(taoBaoShop);
            }
         }
      }
      JSONObject data = new JSONObject();
      data.put("count", count);
      data.put("list", JsonUtil.getApiCommonGson().toJson(list));
      out.print(JsonUtil.loadTrueResult(data));
   }
   /**
@@ -94,138 +171,143 @@
    */
   @RequestMapping(value = "getShopInfo", method = RequestMethod.POST)
   public void getShopInfo(AcceptData acceptData, Integer page, Long sid, Long uid, PrintWriter out) {
      try {
         if (sid == null) {
            out.print(JsonUtil.loadFalseResult("缺失店铺id"));
      if (sid == null) {
         out.print(JsonUtil.loadFalseResult("缺失店铺id"));
         return;
      }
      JSONObject data = new JSONObject();
      if (page == 1) {
         TaoBaoShop taoBaoShop = taoBaoShopService.selectByPrimaryKey(sid);
         if (taoBaoShop == null) {
            out.print(JsonUtil.loadFalseResult("店铺信息获取失败"));
            return;
         }
         JSONObject data = new JSONObject();
         if (page == 1) {
            TaoBaoShop taoBaoShop = taoBaoShopService.selectByPrimaryKey(sid);
            if (taoBaoShop == null) {
               out.print(JsonUtil.loadFalseResult("店铺信息获取失败"));
               return;
            }
            String shopLink = taoBaoShop.getShopLink();
            if (StringUtil.isNullOrEmpty(shopLink)) {
               taoBaoShop.setShopLink(TaoBaoUtil.getShopLink(taoBaoShop.getId()));
            }
            String shopNameCustom = taoBaoShop.getShopNameCustom();
            if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
               taoBaoShop.setShopName(shopNameCustom);
            }
            String shopIconCustom = taoBaoShop.getShopIconCustom();
            if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
               taoBaoShop.setShopIcon(shopIconCustom);
            }
            data.put("shop", JsonUtil.getApiCommonGson().toJson(taoBaoShop));
            ThreadUtil.run(new Runnable() {
               public void run() {
                  // 添加足迹
                  taoBaoShopHistoryService.addHistory(sid, uid, acceptData.getDevice());
               }
            });
         String shopLink = taoBaoShop.getShopLink();
         if (StringUtil.isNullOrEmpty(shopLink)) {
            taoBaoShop.setShopLink(TaoBaoUtil.getShopLink(taoBaoShop.getId()));
         }
         // 商品列表信息
         int pageSize = 50;
         List<TaoBaoGoodsBrief> listGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
         // 先查询大淘客数据
         List<DaTaoKeDetail> listDaTaoKe = daTaoKeGoodsService.listBySellerId(0, pageSize, sid);
         if (listDaTaoKe != null && listDaTaoKe.size() > 0) {
            for (DaTaoKeDetail daTaoKe: listDaTaoKe) {
               listGoodsBrief.add(TaoBaoUtil.convert(daTaoKe));
            }
         String shopNameCustom = taoBaoShop.getShopNameCustom();
         if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
            taoBaoShop.setShopName(shopNameCustom);
         }
         // 不足50条数据、查询精选库
         if (listGoodsBrief.size() < pageSize) {
            int needCount = pageSize- listGoodsBrief.size();
            List<TaoBaoGoodsBrief> listGoods = taoBaoGoodsBriefService.listByShopId(0, needCount, sid);
            if (listGoods != null && listGoods.size() > 0) {
               if (listGoodsBrief.size() > 0) {
                  for (int i = 0; i< listGoods.size(); i ++) {
                     long id = listGoods.get(i).getAuctionId();
                     boolean isRepeat = false;
                     for (TaoBaoGoodsBrief goodsBrief: listGoodsBrief) {
                        long auctionId = goodsBrief.getAuctionId();
                        if (auctionId == id) {
                           isRepeat = true;
                           break;
                        }
                     }
                     if(isRepeat) {
                        listGoods.remove(i);
                        i--;
                     }
                  }
               }
               listGoodsBrief.addAll(listGoods);
            }
         String shopIconCustom = taoBaoShop.getShopIconCustom();
         if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
            taoBaoShop.setShopIcon(shopIconCustom);
         }
         JSONArray array = new JSONArray();
         if (listGoodsBrief.size() > 0) {
            List<Long> listGid = new ArrayList<Long>();
            for (TaoBaoGoodsBrief taoBaoGoodsBrief : listGoodsBrief) {
               listGid.add(taoBaoGoodsBrief.getAuctionId());
            }
            // API网络接口验证是否在售
            List<TaoBaoGoodsBrief> listTaoKeGoods = null;
            try {
               listTaoKeGoods = TaoKeApiUtil.getBatchGoodsInfo(listGid);
            } catch (TaoKeApiException e) {
               e.printStackTrace();
            } catch (TaobaoGoodsDownException e) {
               e.printStackTrace();
            } catch (Exception e) {
               e.printStackTrace();
            }
         Integer userType = taoBaoShop.getUserType();
         if (userType == null || userType == 0) {
            taoBaoShop.setUserType(10);
         } else {
            taoBaoShop.setUserType(11);
         }
            Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
                  .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
            BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
            BigDecimal shareRate = hongBaoManageService.getShareRate();
            for (TaoBaoGoodsBrief taoBaoGoodsBrief : listGoodsBrief) {
               if (listTaoKeGoods != null && listTaoKeGoods.size() > 0) {
                  boolean stateSale = false; // 默认停售
                  Long goodsId = taoBaoGoodsBrief.getAuctionId();
                  for (TaoBaoGoodsBrief taoKeGoods : listTaoKeGoods) {
                     Long auctionId = taoKeGoods.getAuctionId();
                     if (goodsId == auctionId || goodsId.equals(auctionId)) {
                        stateSale = true; // 在售
         data.put("shop", JsonUtil.getApiCommonGson().toJson(taoBaoShop));
         ThreadUtil.run(new Runnable() {
            public void run() {
               // 添加足迹
               taoBaoShopHistoryService.addHistory(sid, uid, acceptData.getDevice());
            }
         });
      }
      // 商品列表信息
      int pageSize = 50;
      List<TaoBaoGoodsBrief> listGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
      // 先查询大淘客数据
      List<DaTaoKeDetail> listDaTaoKe = daTaoKeGoodsService.listBySellerId(0, pageSize, sid);
      if (listDaTaoKe != null && listDaTaoKe.size() > 0) {
         for (DaTaoKeDetail daTaoKe : listDaTaoKe) {
            listGoodsBrief.add(TaoBaoUtil.convert(daTaoKe));
         }
      }
      // 不足50条数据、查询精选库
      if (listGoodsBrief.size() < pageSize) {
         int needCount = pageSize - listGoodsBrief.size();
         List<TaoBaoGoodsBrief> listGoods = taoBaoGoodsBriefService.listByShopId(0, needCount, sid);
         if (listGoods != null && listGoods.size() > 0) {
            if (listGoodsBrief.size() > 0) {
               for (int i = 0; i < listGoods.size(); i++) {
                  long id = listGoods.get(i).getAuctionId();
                  boolean isRepeat = false;
                  for (TaoBaoGoodsBrief goodsBrief : listGoodsBrief) {
                     long auctionId = goodsBrief.getAuctionId();
                     if (auctionId == id) {
                        isRepeat = true;
                        break;
                     }
                  }
                  if (!stateSale) {
                     continue;
                  if (isRepeat) {
                     listGoods.remove(i);
                     i--;
                  }
               }
               GoodsDetailVO goodsDetailVO = GoodsDetailVOFactory.convertTaoBao(taoBaoGoodsBrief, null, fanLiRate, shareRate);
               array.add(gson.toJson(goodsDetailVO));
            }
            listGoodsBrief.addAll(listGoods);
         }
         data.put("count", array.size());
         data.put("list", array);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadFalseResult("查询失败"));
      }
      JSONArray array = new JSONArray();
      if (listGoodsBrief.size() > 0) {
         List<Long> listGid = new ArrayList<Long>();
         for (TaoBaoGoodsBrief taoBaoGoodsBrief : listGoodsBrief) {
            listGid.add(taoBaoGoodsBrief.getAuctionId());
         }
         // API网络接口验证是否在售
         List<TaoBaoGoodsBrief> listTaoKeGoods = null;
         try {
            listTaoKeGoods = TaoKeApiUtil.getBatchGoodsInfo(listGid);
         } catch (TaoKeApiException e) {
            e.printStackTrace();
         } catch (TaobaoGoodsDownException e) {
            e.printStackTrace();
         } catch (Exception e) {
            e.printStackTrace();
         }
         Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
               .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
         BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
         BigDecimal shareRate = hongBaoManageService.getShareRate();
         // 商品信息过滤
         listGoodsBrief = taoBaoGoodsUpdateService.filterImportantTaoBaoGoods(listGoodsBrief);
         for (TaoBaoGoodsBrief taoBaoGoodsBrief : listGoodsBrief) {
            if (listTaoKeGoods != null && listTaoKeGoods.size() > 0) {
               boolean stateSale = false; // 默认停售
               Long goodsId = taoBaoGoodsBrief.getAuctionId();
               for (TaoBaoGoodsBrief taoKeGoods : listTaoKeGoods) {
                  Long auctionId = taoKeGoods.getAuctionId();
                  if (goodsId == auctionId || goodsId.equals(auctionId)) {
                     stateSale = true; // 在售
                     break;
                  }
               }
               if (!stateSale) {
                  continue;
               }
            }
            GoodsDetailVO goodsDetailVO = GoodsDetailVOFactory.convertTaoBao(taoBaoGoodsBrief, null, fanLiRate,
                  shareRate);
            array.add(gson.toJson(goodsDetailVO));
         }
      }
      data.put("count", array.size());
      data.put("list", array);
      out.print(JsonUtil.loadTrueResult(data));
      // 添加到更新列表
      taoBaoGoodsUpdateService.addUpdateQueueAsync(listGoodsBrief);
   }
}