admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/count/UserInfoCountServiceImpl.java
@@ -1,289 +1,1002 @@
package com.yeshi.fanli.service.impl.count;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.yeshi.utils.DateUtil;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.service.inter.count.UserInfoCountService;
@Service
public class UserInfoCountServiceImpl implements UserInfoCountService {
   @Resource
   private UserInfoMapper userInfoMapper;
   @Override
   public long countNewUser(Integer isToday, Integer isMonth) {
      return userInfoMapper.countNewUser(isToday, isMonth);
   }
   @Override
   public long countRank(Integer rank) {
      return userInfoMapper.countRank(rank);
   }
   @Override
   public BigDecimal countAllMoney(Double rank) {
      return userInfoMapper.countAllMoney(rank);
   }
   @Override
   public long countLoseUser(int daysNum) {
      return userInfoMapper.countLoseUser(daysNum);
   }
   @Override
   public long countHasOrderUser() {
      return userInfoMapper.countHasOrderUser();
   }
   @Override
   public List<Map<String, Object>> countNewUserByDate(String channel,Integer type,String years, String startTime,
         String endTime) throws Exception {
      return userInfoMapper.countNewUserByDate(channel, type, years, startTime, endTime);
   }
   @Override
   public List<Map<String, Object>> getTodayBuyRate(String channel, Integer dateType,String years, String startTime,
         String endTime) throws Exception {
      List<Map<String, Object>> result_list = null;
      List<Map<String, Object>> listToday =
            userInfoMapper.getTodayHasOrder(channel, dateType, years, startTime, endTime);
      if (listToday == null || listToday.size() == 0) {
         return result_list;
      }
      List<Map<String, Object>> listTotal =
            userInfoMapper.countNewUserByDate(channel, dateType, years, startTime, endTime);
      if (listTotal == null || listTotal.size() == 0) {
         return result_list;
      }
      switch (dateType){
         case 1: // 按天处理
            result_list = dayFactory(startTime, endTime, listTotal);
            break;
         case 2: // 按月处理
            result_list = monthFactory(listTotal);
            break;
         case 3:
            result_list =  yearFactory(listTotal);
            break;
         default:
            break;
      }
      DecimalFormat df = new DecimalFormat("#.00");
      for (int i = 0; i < result_list.size(); i++) {
         double proportion = 0;
         Map<String, Object> resultMap = result_list.get(i);
         Object showDate = resultMap.get("showDate");
         for (int j = 0; j < listToday.size(); j++) {
            Map<String, Object> innerMap = listToday.get(j);
            Object innerDate = innerMap.get("showDate");
            if (innerDate != null && innerDate.toString().equals(showDate.toString())) {
               Object innerValue = innerMap.get("showValue");
               long innerData = Long.parseLong(innerValue.toString());
               Object showValue = resultMap.get("showValue");
               long showData = Long.parseLong(showValue.toString());
               if (showData > 0) {
                  proportion =  innerData/(double)showData;
               }
               break; // 结束内部循环
            }
         }
         resultMap.put("showValue", Double.parseDouble(df.format(proportion *100)));
      }
      return result_list;
   }
   @Override
   public List<Object> getWeekBuyRate(String channel, String startTime, String endTime,
         Integer orderNum, List<String> dateList)
         throws Exception {
      List<Object> resultList = new ArrayList<Object>();
      if (dateList == null || dateList.size() == 0) {
         return resultList;
      }
      List<Map<String, Object>> totalList = userInfoMapper.countNewUserByDate(channel, 1, null, startTime, endTime);
      if (totalList == null || totalList.size() == 0) {
         return resultList;
      }
      DecimalFormat df = new DecimalFormat("#.00");
      for (int i = 0; i < dateList.size(); i++) {
         double proportion = 0;
         String showDate = dateList.get(i);
         String plusDay = DateUtil.plusDay(7, showDate);
         Map<String, Object> map = userInfoMapper.getWeekHasOrder(channel, showDate, plusDay, orderNum);
         Object showValue = null;
         if (map != null) {
            showValue = map.get("showValue");
            if (showValue != null) {
               for (int j = 0; j < totalList.size(); j++) {
                  Map<String, Object> mapTotal = totalList.get(j);
                  String reslutTime = mapTotal.get("showDate").toString();
                  if (showDate.equalsIgnoreCase(reslutTime)) {
                     Object totalObject = mapTotal.get("showValue");
                     long hasCount = Long.parseLong(showValue.toString());
                     long total = Long.parseLong(totalObject.toString());
                     if (total > 0) {
                        proportion = hasCount / (double) total;
                     }
                     break;
                  }
               }
            }
         }
         resultList.add(Double.parseDouble(df.format(proportion * 100)));
      }
      return resultList;
   }
   public List<Map<String, Object>> dayFactory(String startTime, String endTime, List<Map<String, Object>> list) throws Exception {
      List<Map<String, Object>> listObject = new ArrayList<Map<String, Object>>();
      if (startTime.equals(endTime)) {
         Map<String, Object> map = list.get(0);
         Object total = map.get("showValue");
         if (total == null) {
            map.put("showValue", 0);
         }
         listObject.add(map);
         return listObject;
      }
      String plusDay = "";
      for (int i = 0; i < 1000; i++) {
         if (i == 0) {
            plusDay = startTime;
         } else {
            plusDay = DateUtil.plusDay(i, startTime);
         }
         Map<String, Object> mapObject = new HashMap<String, Object>();
         Object total = null;
         for (int j = 0; j < list.size(); j++) {
            Map<String, Object> map = list.get(j);
            Object createDate = map.get("showDate");
            String month = createDate.toString();
            if (plusDay.equalsIgnoreCase(month)) {
               total = map.get("showValue");
               break;
            }
         }
         if (total == null) {
            total = 0;
         }
         mapObject.put("showValue", total);
         mapObject.put("showDate", plusDay);
         listObject.add(mapObject);
         if (plusDay.equals(endTime)) {
            break; // 时间结束
         }
      }
      return listObject;
   }
   public List<Map<String, Object>> monthFactory(List<Map<String, Object>> list) {
      List<Map<String, Object>> listObject = new ArrayList<Map<String, Object>>();
      // 12 个月处理
      for (int i = 1; i <= 12; i++) {
         Map<String, Object> mapObject = new HashMap<String, Object>();
         Object total = null;
         for (int j = 0; j < list.size(); j++) {
            Map<String, Object> map = list.get(j);
            Object createDate = map.get("showDate");
            String month = createDate.toString();
            if ((i + "").equalsIgnoreCase(month) || i == Integer.parseInt(month)) {
               total = map.get("showValue");
               break;
            }
         }
         if (total == null) {
            total = 0;
         }
         mapObject.put("showValue", total);
         if (i <10) {
            mapObject.put("showDate", "0"+ i);
         } else {
            mapObject.put("showDate", i);
         }
         listObject.add(mapObject);
      }
      return listObject;
   }
   public List<Map<String, Object>> yearFactory(List<Map<String, Object>> list) {
      List<Map<String, Object>> listObject = new ArrayList<Map<String, Object>>();
      for (int i = 0; i < list.size(); i++) {
         Map<String, Object> map = list.get(i);
         Object total = map.get("showValue");
         if (total == null) {
            total = 0;
         }
         map.put("showValue", total);
         listObject.add(map);
      }
      return listObject;
   }
}
package com.yeshi.fanli.service.impl.count;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.yeshi.utils.DateUtil;
import com.yeshi.fanli.dao.mybatis.UserInfoCountMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dao.user.count.CountOrderInfoDao;
import com.yeshi.fanli.dao.user.count.CountUserInfoDao;
import com.yeshi.fanli.dto.ChartTDO;
import com.yeshi.fanli.dto.order.CountOrderDTO;
import com.yeshi.fanli.entity.admin.count.CountOrderInfo;
import com.yeshi.fanli.entity.admin.count.CountOrderInfo.CountOrderEnum;
import com.yeshi.fanli.entity.admin.count.CountUserInfo;
import com.yeshi.fanli.entity.admin.count.CountUserInfo.CountUserEnum;
import com.yeshi.fanli.entity.bus.user.UserInfoRegister;
import com.yeshi.fanli.entity.system.ChannelEnum;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.count.UserInfoCountService;
import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
import com.yeshi.fanli.service.inter.order.LostOrderService;
import com.yeshi.fanli.service.inter.order.UserOrderWeiQuanRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoRegisterService;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import org.yeshi.utils.TimeUtil;
import com.yeshi.fanli.vo.user.UserGoldCoinVO;
@Service
public class UserInfoCountServiceImpl implements UserInfoCountService {
   @Resource
   private UserInfoMapper userInfoMapper;
   @Resource
   private UserInfoCountMapper userInfoCountMapper;
   @Resource
   private CountUserInfoDao countUserInfoDao;
   @Resource
   private CountOrderInfoDao countOrderInfoDao;
   @Resource
   private CommonOrderCountService commonOrderCountService;
   @Resource
   private HongBaoV2CountService hongBaoV2CountService;
   @Resource
   private UserOrderWeiQuanRecordService userOrderWeiQuanRecordService;
   @Resource
   private LostOrderService lostOrderService;
   @Resource
   private UserInfoRegisterService userInfoRegisterService;
   @Override
   public long countNewUser(Integer isToday, Integer isMonth) {
      return userInfoMapper.countNewUser(isToday, isMonth);
   }
   @Override
   public long countRank(Integer rank) {
      return userInfoMapper.countRank(rank);
   }
   @Override
   public BigDecimal countAllMoney(Double rank) {
      return userInfoMapper.countAllMoney(rank);
   }
   @Override
   public long countLoseUser(int daysNum) {
      return userInfoMapper.countLoseUser(daysNum);
   }
   @Override
   public long countHasOrderUser() {
      return userInfoMapper.countHasOrderUser();
   }
   @Override
   public List<ChartTDO> countNewUserByDate(String channel, Integer type, String years, String startTime,
         String endTime) throws Exception {
      return userInfoMapper.countNewUserByDate(channel, type, years, startTime, endTime);
   }
   @Override
   public List<ChartTDO> getTodayBuyRate(String channel, Integer dateType, String years, String startTime,
         String endTime) throws Exception {
      List<ChartTDO> result_list = null;
      List<ChartTDO> listToday = userInfoMapper.getTodayHasOrder(channel, dateType, years, startTime, endTime);
      if (listToday == null || listToday.size() == 0) {
         return result_list;
      }
      List<ChartTDO> listTotal = userInfoMapper.countNewUserByDate(channel, dateType, years, startTime, endTime);
      if (listTotal == null || listTotal.size() == 0) {
         return result_list;
      }
      switch (dateType) {
      case 1: // 按天处理
         result_list = dayFactory(startTime, endTime, listTotal);
         break;
      case 2: // 按月处理
         result_list = monthFactory(listTotal);
         break;
      case 3:
         result_list = yearFactory(listTotal);
         break;
      default:
         break;
      }
      DecimalFormat df = new DecimalFormat("#.00");
      for (int i = 0; i < result_list.size(); i++) {
         double proportion = 0;
         ChartTDO chartTDO = result_list.get(i);
         String showDate = chartTDO.getShowDate();
         for (int j = 0; j < listToday.size(); j++) {
            ChartTDO innerTDO = listToday.get(j);
            String innerDate = innerTDO.getShowDate();
            if (innerDate != null && innerDate.equals(showDate)) {
               String innerValue = innerTDO.getShowValue();
               long innerData = Long.parseLong(innerValue);
               String showValue = chartTDO.getShowValue();
               long showData = Long.parseLong(showValue);
               if (showData > 0) {
                  proportion = innerData / (double) showData;
               }
               break; // 结束内部循环
            }
         }
         chartTDO.setShowValue(Double.parseDouble(df.format(proportion * 100)) + "");
      }
      return result_list;
   }
   @Override
   public List<Object> getWeekBuyRate(String channel, String startTime, String endTime, Integer orderNum,
         List<String> dateList) throws Exception {
      List<Object> resultList = new ArrayList<Object>();
      if (dateList == null || dateList.size() == 0) {
         return resultList;
      }
      List<ChartTDO> totalList = userInfoMapper.countNewUserByDate(channel, 1, null, startTime, endTime);
      if (totalList == null || totalList.size() == 0) {
         return resultList;
      }
      DecimalFormat df = new DecimalFormat("#.00");
      for (int i = 0; i < dateList.size(); i++) {
         double proportion = 0;
         String showDate = dateList.get(i);
         String plusDay = DateUtil.plusDay(7, showDate);
         ChartTDO chartTDO = userInfoMapper.getWeekHasOrder(channel, showDate, plusDay, orderNum);
         String showValue = null;
         if (chartTDO != null) {
            showValue = chartTDO.getShowValue();
            if (showValue != null) {
               for (int j = 0; j < totalList.size(); j++) {
                  ChartTDO chart = totalList.get(j);
                  String reslutTime = chart.getShowDate();
                  if (showDate.equalsIgnoreCase(reslutTime)) {
                     String totalObject = chart.getShowValue();
                     long hasCount = Long.parseLong(showValue);
                     long total = Long.parseLong(totalObject);
                     if (total > 0) {
                        proportion = hasCount / (double) total;
                     }
                     break;
                  }
               }
            }
         }
         resultList.add(Double.parseDouble(df.format(proportion * 100)));
      }
      return resultList;
   }
   public List<ChartTDO> dayFactory(String startTime, String endTime, List<ChartTDO> list) throws Exception {
      List<ChartTDO> listObject = new ArrayList<ChartTDO>();
      if (startTime.equals(endTime)) {
         ChartTDO chartTDO = list.get(0);
         String showValue = chartTDO.getShowValue();
         if (showValue == null) {
            chartTDO.setShowValue("0");
         }
         listObject.add(chartTDO);
         return listObject;
      }
      String plusDay = "";
      for (int i = 0; i < 1000; i++) {
         if (i == 0) {
            plusDay = startTime;
         } else {
            plusDay = DateUtil.plusDay(i, startTime);
         }
         String total = null;
         for (int j = 0; j < list.size(); j++) {
            ChartTDO chartTDO = list.get(j);
            String month = chartTDO.getShowDate();
            if (plusDay.equalsIgnoreCase(month)) {
               total = chartTDO.getShowValue();
               break;
            }
         }
         if (total == null) {
            total = "0";
         }
         ChartTDO chartTDO = new ChartTDO();
         chartTDO.setShowValue(total);
         chartTDO.setShowDate(plusDay);
         listObject.add(chartTDO);
         if (plusDay.equals(endTime)) {
            break; // 时间结束
         }
      }
      return listObject;
   }
   public List<ChartTDO> monthFactory(List<ChartTDO> list) {
      List<ChartTDO> listObject = new ArrayList<ChartTDO>();
      // 12 个月处理
      for (int i = 1; i <= 12; i++) {
         String total = null;
         for (int j = 0; j < list.size(); j++) {
            ChartTDO chartTDO = list.get(j);
            String month = chartTDO.getShowDate();
            if ((i + "").equalsIgnoreCase(month) || i == Integer.parseInt(month)) {
               total = chartTDO.getShowValue();
               break;
            }
         }
         if (total == null) {
            total = "0";
         }
         ChartTDO chartTDO = new ChartTDO();
         chartTDO.setShowValue(total);
         if (i < 10) {
            chartTDO.setShowDate("0" + i);
         } else {
            chartTDO.setShowDate("" + i);
         }
         listObject.add(chartTDO);
      }
      return listObject;
   }
   public List<ChartTDO> yearFactory(List<ChartTDO> list) {
      List<ChartTDO> listObject = new ArrayList<ChartTDO>();
      for (int i = 0; i < list.size(); i++) {
         ChartTDO chartTDO = list.get(i);
         String total = chartTDO.getShowValue();
         if (total == null) {
            total = "0";
         }
         chartTDO.setShowValue(total);
         listObject.add(chartTDO);
      }
      return listObject;
   }
   @Override
   public List<UserGoldCoinVO> listByUserGoldCoin(long start, int count, int type, String key) {
      return userInfoCountMapper.listByUserGoldCoin(start, count, type, key);
   }
   @Override
   public long countByUserGoldCoin(int type, String key) {
      return userInfoCountMapper.countByUserGoldCoin(type, key);
   }
   @Override
   public List<UserGoldCoinVO> listByHasGoldCoin(long start, int count, String key) {
      return userInfoCountMapper.listByHasGoldCoin(start, count, key);
   }
   @Override
   public long countByHasGoldCoin(String key) {
      return userInfoCountMapper.countByHasGoldCoin(key);
   }
   @Override
   public List<CountUserInfo> getNewUserData(Date startTime, Date endTime, String channel) {
      // 重新查询统计今日以及空缺
      return countUserInfoDao.query(CountUserEnum.newUser, startTime, endTime, channel);
   }
   @Override
   public List<CountOrderInfo> countOderByChannel(String channel, Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initOrderCount();
      return countOrderInfoDao.query(CountOrderEnum.channelTotal, startTime, endTime, channel);
   }
   // 初始化统计
   private void initOrderCount() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.channelTotal);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2018-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordOrderCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordOrderCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordOrderCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordOrderCount(String preDay) throws Exception {
      ChannelEnum[] channels = ChannelEnum.values();
      for (int i = 0; i < channels.length; i++) {
         int count = 0;
         String channelVlaue = channels[i].getVlaue();
         List<CountOrderDTO> listOrder = commonOrderCountService.countValidOrderByDay(preDay);
         if (listOrder != null && listOrder.size() > 0) {
            List<Long> listUid = new ArrayList<>();
            for (CountOrderDTO countOrderDTO: listOrder) {
               listUid.add(countOrderDTO.getUid());
            }
            List<UserInfoRegister> listRegister = userInfoRegisterService.listByMultipleUids(listUid);
            if (listRegister != null && listRegister.size() > 0) {
               for (int m = 0; m <listRegister.size(); m ++) {
                  boolean reduce = false;
                  Long uid = listRegister.get(m).getId();
                  String vlaue = listRegister.get(m).getChannel();
                  for (int n = 0; n <listOrder.size(); n ++) {
                     if (channelVlaue.equalsIgnoreCase(vlaue) && uid.longValue() == listOrder.get(n).getUid()) {
                        Integer totalOrder = listOrder.get(n).getTotalOrder();
                        if (totalOrder != null)
                           count += totalOrder;
                        listOrder.remove(n);
                        n--;
                        reduce = true;
                     }
                  }
                  if (reduce) {
                     listRegister.remove(m);
                     m--;
                  }
               }
            }
         }
         CountOrderInfo record = new CountOrderInfo();
         record.setNum(count);
         record.setChannel(channels[i].name());
         record.setDay(TimeUtil.parse(preDay));
         record.setType(CountOrderEnum.channelTotal.name());
         record.setId(StringUtil.Md5(preDay + channels[i].name() + CountOrderEnum.channelTotal.name()));
         countOrderInfoDao.save(record);
      }
   }
   @Override
   public List<CountOrderInfo> count24HOderByChannel(String channel, Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      init24HOrderCount();
      return countOrderInfoDao.query(CountOrderEnum.oder24H, startTime, endTime, channel);
   }
   // 初始化统计
   private void init24HOrderCount() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.oder24H);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2018-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecord24HOrderCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecord24HOrderCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecord24HOrderCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecord24HOrderCount(String preDay) throws Exception {
      ChannelEnum[] channels = ChannelEnum.values();
      for (int i = 0; i < channels.length; i++) {
         int count = 0;
         List<UserInfoRegister> list = userInfoRegisterService.listByChannelAndDay(channels[i].getVlaue(), preDay);
         if (list != null && list.size() > 0) {
            // 统计24小时产生订单数量
            for (UserInfoRegister user : list) {
               Date limitDay = DateUtil.plusDayDate(1, user.getCreateTime());
               Integer total = commonOrderCountService.countOderByUidAndDate(limitDay, user.getId());
               if (total != null)
                  count += total;
            }
         }
         CountOrderInfo record = new CountOrderInfo();
         record.setNum(count);
         record.setChannel(channels[i].name());
         record.setDay(TimeUtil.parse(preDay));
         record.setType(CountOrderEnum.oder24H.name());
         record.setId(StringUtil.Md5(preDay + channels[i].name() + CountOrderEnum.oder24H.name()));
         countOrderInfoDao.save(record);
      }
   }
   @Override
   public List<CountOrderInfo> countHongBaoByChannel(String channel, Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initHongBaoCount();
      return countOrderInfoDao.query(CountOrderEnum.channelCommission, startTime, endTime, channel);
   }
   // 初始化统计
   private void initHongBaoCount() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.channelCommission);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2018-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordHongBaoCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordHongBaoCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordHongBaoCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordHongBaoCount(String preDay) throws Exception {
      ChannelEnum[] channels = ChannelEnum.values();
      for (int i = 0; i < channels.length; i++) {
         BigDecimal money = new BigDecimal(0);
         String channelVlaue = channels[i].getVlaue();
         List<CountOrderDTO> listOrder = commonOrderCountService.countCommissionByDay(preDay);
         if (listOrder != null && listOrder.size() > 0) {
            List<Long> listUid = new ArrayList<>();
            for (CountOrderDTO countOrderDTO: listOrder) {
               listUid.add(countOrderDTO.getUid());
            }
            List<UserInfoRegister> listRegister = userInfoRegisterService.listByMultipleUids(listUid);
            if (listRegister != null && listRegister.size() > 0) {
               for (int m = 0; m <listRegister.size(); m ++) {
                  boolean reduce = false;
                  Long uid = listRegister.get(m).getId();
                  String vlaue = listRegister.get(m).getChannel();
                  for (int n = 0; n <listOrder.size(); n ++) {
                     if (channelVlaue.equalsIgnoreCase(vlaue) && uid.longValue() == listOrder.get(n).getUid()) {
                        BigDecimal commission = listOrder.get(n).getCommission();
                        if (commission != null)
                           MoneyBigDecimalUtil.add(money, commission);
                        listOrder.remove(n);
                        n--;
                        reduce = true;
                     }
                  }
                  if (reduce) {
                     listRegister.remove(m);
                     m--;
                  }
               }
            }
         }
         CountOrderInfo record = new CountOrderInfo();
         record.setMoney(money);
         record.setChannel(channels[i].name());
         record.setDay(TimeUtil.parse(preDay));
         record.setType(CountOrderEnum.channelCommission.name());
         record.setId(StringUtil.Md5(preDay + channels[i].name() + CountOrderEnum.channelCommission.name()));
         countOrderInfoDao.save(record);
      }
   }
   @Override
   public List<CountOrderInfo> countWeiQuanOrder(Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initWeiQuanCount();
      return countOrderInfoDao.query(CountOrderEnum.weiQuanNum, startTime, endTime);
   }
   // 初始化统计
   private void initWeiQuanCount() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.weiQuanNum);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2019-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordWeiQuanCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordWeiQuanCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordWeiQuanCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordWeiQuanCount(String preDay) throws Exception {
      Integer count = userOrderWeiQuanRecordService.countWeiQaunOrderNumberByDate(preDay);
      if (count == null)
         count = 0;
      CountOrderInfo record = new CountOrderInfo();
      record.setNum(count);
      record.setDay(TimeUtil.parse(preDay));
      record.setType(CountOrderEnum.weiQuanNum.name());
      record.setId(StringUtil.Md5(preDay + CountOrderEnum.weiQuanNum.name()));
      countOrderInfoDao.save(record);
   }
   @Override
   public List<CountOrderInfo> countWeiQuanOrderMoney(Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initWeiQuanMoneyCount();
      return countOrderInfoDao.query(CountOrderEnum.weiQuanMoney, startTime, endTime);
   }
   // 初始化统计
   private void initWeiQuanMoneyCount() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.weiQuanMoney);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2019-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordWeiQuanMoneyCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordWeiQuanMoneyCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordWeiQuanMoneyCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordWeiQuanMoneyCount(String preDay) throws Exception {
      BigDecimal money = userOrderWeiQuanRecordService.countWeiQaunOrderMoneyByDate(preDay);
      if (money == null)
         money = new BigDecimal(0);
      CountOrderInfo record = new CountOrderInfo();
      record.setMoney(money);
      record.setDay(TimeUtil.parse(preDay));
      record.setType(CountOrderEnum.weiQuanMoney.name());
      record.setId(StringUtil.Md5(preDay + CountOrderEnum.weiQuanMoney.name()));
      countOrderInfoDao.save(record);
   }
   @Override
   public List<CountOrderInfo> counOrderTotalNum(Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initOrderTotalNum();
      return countOrderInfoDao.query(CountOrderEnum.totalNum, startTime, endTime);
   }
   // 初始化统计
   private void initOrderTotalNum() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.totalNum);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2019-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordOrderTotalNum(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordOrderTotalNum(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordOrderTotalNum(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordOrderTotalNum(String preDay) throws Exception {
      Integer num = commonOrderCountService.countOderByDate(preDay);
      if (num == null)
         num = 0;
      CountOrderInfo record = new CountOrderInfo();
      record.setNum(num);
      record.setDay(TimeUtil.parse(preDay));
      record.setType(CountOrderEnum.totalNum.name());
      record.setId(StringUtil.Md5(preDay + CountOrderEnum.totalNum.name()));
      countOrderInfoDao.save(record);
   }
   @Override
   public List<CountOrderInfo> counOrderTotalCommission(Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initTotalCommission();
      return countOrderInfoDao.query(CountOrderEnum.totalCommission, startTime, endTime);
   }
   // 初始化统计
   private void initTotalCommission() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.totalCommission);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2019-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordTotalCommission(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordTotalCommission(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordTotalCommission(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordTotalCommission(String preDay) throws Exception {
      BigDecimal money = commonOrderCountService.countCommissionByDate(preDay);
      if (money == null)
         money = new BigDecimal("0");
      CountOrderInfo record = new CountOrderInfo();
      record.setMoney(money);
      record.setDay(TimeUtil.parse(preDay));
      record.setType(CountOrderEnum.totalCommission.name());
      record.setId(StringUtil.Md5(preDay + CountOrderEnum.totalCommission.name()));
      countOrderInfoDao.save(record);
   }
   @Override
   public List<CountOrderInfo> counOrderLastNum(Date startTime, Date endTime) {
      return countOrderInfoDao.query(CountOrderEnum.lastNum, startTime, endTime);
   }
   @Override
   public List<CountOrderInfo> counOrderLastMoney(Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initOrderLastMoney();
      return countOrderInfoDao.query(CountOrderEnum.lastMoney, startTime, endTime);
   }
   // 初始化统计
   private void initOrderLastMoney() {
      try {
         CountOrderInfo lastRecord = countOrderInfoDao.getMaxDate(CountOrderEnum.lastMoney);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2019-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordOrderLastMoney(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordOrderLastMoney(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordOrderLastMoney(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordOrderLastMoney(String preDay) throws Exception {
      BigDecimal money = lostOrderService.countAppealMoney(preDay);
      if (money == null)
         money = new BigDecimal("0");
      CountOrderInfo record = new CountOrderInfo();
      record.setMoney(money);
      record.setDay(TimeUtil.parse(preDay));
      record.setType(CountOrderEnum.lastMoney.name());
      record.setId(StringUtil.Md5(preDay + CountOrderEnum.lastMoney.name()));
      countOrderInfoDao.save(record);
   }
   @Override
   public List<CountUserInfo> countUserDownOrderByChannelAndToday(String channel, Date startTime, Date endTime) {
      // 重新查询统计今日以及空缺
      initTodayDownOrderCount();
      return countUserInfoDao.query(CountUserEnum.todayOrder, startTime, endTime, channel);
   }
   // 初始化统计
   private void initTodayDownOrderCount() {
      try {
         CountUserInfo lastRecord = countUserInfoDao.getMaxDate(CountUserEnum.todayOrder);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2018-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordTodayDownOrderCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordTodayDownOrderCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordTodayDownOrderCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordTodayDownOrderCount(String preDay) throws Exception {
      ChannelEnum[] channels = ChannelEnum.values();
      for (int i = 0; i < channels.length; i++) {
         int count = 0;
         String channelVlaue = channels[i].getVlaue();
         List<CountOrderDTO> listOrder = commonOrderCountService.countValidOrderByDay(preDay);
         if (listOrder != null && listOrder.size() > 0) {
            List<Long> listUid = new ArrayList<>();
            for (CountOrderDTO countOrderDTO: listOrder) {
               listUid.add(countOrderDTO.getUid());
            }
            List<UserInfoRegister> listRegister = userInfoRegisterService.listByMultipleUidAndDay(listUid, preDay);
            if (listRegister != null && listRegister.size() > 0) {
               for (int m = 0; m <listRegister.size(); m ++) {
                  boolean reduce = false;
                  Long uid = listRegister.get(m).getId();
                  String vlaue = listRegister.get(m).getChannel();
                  for (int n = 0; n <listOrder.size(); n ++) {
                     if (channelVlaue.equalsIgnoreCase(vlaue) && uid.longValue() == listOrder.get(n).getUid()) {
                        Integer totalOrder = listOrder.get(n).getTotalOrder();
                        if (totalOrder != null)
                           count += totalOrder;
                        listOrder.remove(n);
                        n--;
                        reduce = true;
                     }
                  }
                  if (reduce) {
                     listRegister.remove(m);
                     m--;
                  }
               }
            }
         }
         CountUserInfo record = new CountUserInfo();
         record.setNum(count);
         record.setChannel(channels[i].name());
         record.setDay(TimeUtil.parse(preDay));
         record.setType(CountUserEnum.todayOrder);
         record.setId(StringUtil.Md5(preDay + channels[i].name() + CountUserEnum.todayOrder.name()));
         countUserInfoDao.save(record);
      }
   }
   @Override
   public List<CountUserInfo> countUseByChannelAndWeekOrder(String channel, Date startTime, Date endTime) {
      return countUserInfoDao.query(CountUserEnum.weekOrder, startTime, endTime, channel);
   }
   @Override
   public List<CountUserInfo> countUseByChannelAndWeekThreeOrder(String channel, Date startTime, Date endTime) {
      return countUserInfoDao.query(CountUserEnum.weekThreeOrder, startTime, endTime, channel);
   }
   // 初始化统计
   @Override
   public void initCountUserWeekOrder() {
      try {
         CountUserInfo lastRecord = countUserInfoDao.getMaxDate(CountUserEnum.weekOrder);
         Date lastDay = null;
         if (lastRecord != null && lastRecord.getDay() != null) {
            lastDay = lastRecord.getDay();
         }
         if (lastDay == null) {
            lastDay = TimeUtil.parse("2018-01-01");
         }
         Date today = new Date();
         int betweenDays = DateUtil.daysBetween2(lastDay, today);
         if (betweenDays > 0) {
            for (int i = 0; i <= betweenDays; i++) {
               addRecordWeekOrderCount(DateUtil.plusDay(i, lastDay));
            }
         } else {
            // 重新统计昨日
            addRecordWeekOrderCount(DateUtil.reduceDay2(1, lastDay));
            // 重新统计今日
            addRecordWeekOrderCount(TimeUtil.getGernalTime(today.getTime()));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void addRecordWeekOrderCount(String preDay) throws Exception {
      ChannelEnum[] channels = ChannelEnum.values();
      for (int i = 0; i < channels.length; i++) {
         int count = 0;
         int count3 = 0;
         List<UserInfoRegister> list = userInfoRegisterService.listByChannelAndDay(channels[i].getVlaue(), preDay);
         if (list != null && list.size() > 0) {
            Date startDay = new Date(TimeUtil.convertDateToTemp2(preDay + " 23:59:59"));
            Date limitDay = DateUtil.plusDayDate(6, startDay);
            for (UserInfoRegister user : list) {
               Integer total = commonOrderCountService.countOderByUidAndDate(limitDay, user.getId());
               if (total != null && total > 0) {
                  count++;
                  if (total >= 3) {
                     count3++;
                  }
               }
            }
         }
         // 7天内产生有效订单
         CountUserInfo record = new CountUserInfo();
         record.setNum(count);
         record.setChannel(channels[i].name());
         record.setDay(TimeUtil.parse(preDay));
         record.setType(CountUserEnum.weekOrder);
         record.setId(StringUtil.Md5(preDay + channels[i].name() + CountUserEnum.weekOrder.name()));
         countUserInfoDao.save(record);
         // 7天内产生3单以上有效订单
         CountUserInfo record3 = new CountUserInfo();
         record3.setNum(count3);
         record3.setChannel(channels[i].name());
         record3.setDay(TimeUtil.parse(preDay));
         record3.setType(CountUserEnum.weekThreeOrder);
         record3.setId(StringUtil.Md5(preDay + channels[i].name() + CountUserEnum.weekThreeOrder.name()));
         countUserInfoDao.save(record);
      }
   }
}