动态使用countDownLaunch

/**
 * 根据用户和区域分析店铺
 * 
 * @param userId
 * @param regionId
 * @param order
 * @param date1
 * @param date2
 * @param date4
 * @param type
 * @return
 * @throws Exception
 * @author Harold
 * @date 2017年9月13日下午7:31:16
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Map<String, Object>> shopAnalyzeByUserAndRegion(long userId, Long regionId, String order, Date date1,
      Date date2, Date date3, Date date4, String dimension, String type) throws Exception {
   List<Long> sl = null;
   if(null == regionId){regionId = 0L;}
   if (0 != regionId) {// 查询区域
      sl = subjectShopService.getShopListByUserAndRegion(regionId, "");
   } else {// 查询用户所有店铺
      sl = subjectShopService.getShopListOfUser(userId);
   }
   if (null == sl || sl.size() == 0) {
      return null;
   }
   
      Map<Long, Long> statMap = getStatMap(sl, date1, date2);//客流
      Map<Long, ShopDealInfoDo> dealInfoMap = getDealInfoMap(sl, date1, date2);
      Date endDate = DateUtil.getDateOfEndMonth(date1);
      Map<Long, Long> targetMap = shopSalesTargetService.getTargetOfShopIds(
            CollectionUtil.transformListToArray(sl), date1, endDate);//目标
   
   List afterPageList = CollectionUtil.portListByQuantity(sl, 50);
   return shopAnalyze(statMap, targetMap, afterPageList, order, date1, date2, date3, date4, dimension, type);

}

public static List portListByQuantity(List list, int quantity) {
    if (list == null || list.size() == 0) {
        return list;
    }
    
    if (quantity <= 0) {
        new IllegalArgumentException("Wrong quantity.");
    }
    
    List wrapList = new ArrayList();
    int count = 0;
    while (count < list.size()) {
        wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity));
        count += quantity;
    }
    
    return wrapList;
}
 
ExecutorService newFixedThreadPool =  Executors.newFixedThreadPool(8);

public List<Map<String, Object>> shopAnalyze(final Map<Long, Long> statMap, final Map<Long, Long> targetMap, final List<List<Long>> shopList, String order, final Date date1, final Date date2, final Date date3,
      final Date date4, String dimension, final String type) throws Exception {
   //final List<Map<String, Object>> el = new ArrayList<Map<String, Object>>();//定义返回结果
   final Vector<Map<String, Object>> el = new Vector<Map<String, Object>>();
   if (!(null == shopList || shopList.size() < 1)) {
      int size = shopList.size();//定义线程数量
      final CountDownLatch countDownLatch = new CountDownLatch(size);//定义countDownLatch
      for(int i = 0; i < size; i++){
         final int j = i;
         newFixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
               try {
                  List<Map<String, Object>> divideResultList = getAnalyzeOfShopList(statMap, targetMap, shopList.get(j), date1, date2, date3, date4, type);
                  if (null != divideResultList && divideResultList.size() > 0) {
                     el.addAll(divideResultList);
                  }
               } catch (Exception e) {
                  logger.error("第{}个线程在计算门店分析时发生了错误", j);
               } finally {
                  countDownLatch.countDown();//每次减去一,避免死锁
               }
            }
         });
      }
      countDownLatch.await();          
   }
   if (order == null) {
      order = com.kiisoo.athena.common.constant.Constants.TYPE_SERVICE_ORDER_DESC;
   }

   return CommonUtil.orderList(el, order, dimension);
}
public static List<Map<String, Object>> orderList(List<Map<String, Object>> list, final String order, String dimension){
       Map<String, String> dimensionMap = new HashMap<String, String>();
       dimensionMap.put("0", "target");//目标销售额
       dimensionMap.put("1", "volAmount");//销售额
       dimensionMap.put("2", "lastlast");//比去年
       dimensionMap.put("3", "last");//比上周
       dimensionMap.put("4", "avgUnitSale");//件单价
       dimensionMap.put("5", "perSale");//客单价
       dimensionMap.put("6", "volRate");//连带率
       dimensionMap.put("7", "instoreFLow");//入店量
       dimensionMap.put("8", "dealRate");//成交率
       final String orderDimension = dimensionMap.get(dimension);
   Collections.sort(list, new Comparator<Map<String, Object>>(){
      @Override
           public int compare(Map<String, Object> o1, Map<String, Object> o2) {  
               Object value1Obj = o1.get(orderDimension);
               value1Obj = value1Obj == null ? 0 : value1Obj;
               Object value2Obj = o2.get(orderDimension);
               value2Obj = value2Obj == null ? 0 : value2Obj;
               Float value1 = Float.parseFloat(value1Obj.toString());
            Float value2= Float.parseFloat(value2Obj.toString());
            if (null != order && order.equals(com.kiisoo.athena.common.constant.Constants.TYPE_SERVICE_ORDER_DESC)) {
                return value2.compareTo(value1); 
         }else{
            return value1.compareTo(value2); 
         }    
           }  
        });
   return list;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值