admin
2019-01-03 5981b2cae7c20ec9021c8ccbe1a926f35f640210
fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
@@ -1,6 +1,7 @@
package com.yeshi.fanli.service.impl.hongbao;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@@ -19,17 +20,24 @@
import com.yeshi.fanli.dao.mybatis.order.OrderItemMapper;
import com.yeshi.fanli.dao.mybatis.order.OrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoOrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.entity.bus.user.HongBao;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.CommonOrderGoods;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.exception.HongBaoException;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
@Service
@@ -61,6 +69,12 @@
   @Resource
   private CommonOrderService commonOrderService;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
   @Override
   public int insert(HongBaoV2 record) {
@@ -132,7 +146,6 @@
         if (hb.getOrder() != null) {
            orderId = hb.getOrder().getOrderId();
            // 查询同一订单号的非子红包
         } else
            orderId = hb.getOrderId();
@@ -180,9 +193,15 @@
            if (list.size() <= 0)
               return;
            List<TaoBaoOrder> orderList = taoBaoOrderMapper.selectTaoBaoOrderByOrderId(orderId);
            if (orderList == null || orderList.size() < list.size()) {
               System.out.println("红包数与订单数不对应");
               return;
            }
            // 加入排序值
            for (int i = 0; i < orderList.size(); i++) {
               orderList.get(i).setOrderBy(i + 1);
            }
            // 按照auctionId,payMoney排序
@@ -228,6 +247,11 @@
               commonOrder.setCreateTime(new Date(list.get(i).getCreatetime()));
               // TODO 维权判断
               List<TaoBaoWeiQuanOrder> weiQuanList = taoBaoWeiQuanOrderMapper.selectListByOrderIdAndState(orderId,
                     "维权成功");
               if (weiQuanList != null && weiQuanList.size() > 0)
                  commonOrder.setState(CommonOrder.STATE_WQ);
               commonOrderMapper.insertSelective(commonOrder);
               // 插入红包
@@ -268,24 +292,284 @@
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Transactional
   @Override
   public void addHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
      if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
         throw new HongBaoException(1, "订单信息不完整");
      HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
      if (hongBaoOrder == null) {
         saveHongBao(commonOrder, type);
      } else {
         updateHongBao(hongBaoOrder, commonOrder, type);
      }
   }
   @Transactional
   private void updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type) throws HongBaoException {
      System.out.println(commonOrder.getOrderNo());
      HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKey(hongBaoOrder.getHongBaoV2().getId());
      if (oldHongBao == null)
         throw new HongBaoException(10, "红包对象不存在");
      // 已经失效或者已经领取的红包不做处理
      if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
         return;
         if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
            BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
         HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
         hongBao.setUpdateTime(new Date());
         // 更改状态与资金
         if (commonOrder.getState() == CommonOrder.STATE_FK) {
            hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
         } else if (commonOrder.getState() == CommonOrder.STATE_JS) {
            hongBao.setState(HongBaoV2.STATE_KELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
            hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
            hongBao.setMoney(new BigDecimal(0));
         }
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
         if (children != null)
            for (HongBaoV2 child : children) {
               HongBaoV2 childUpdate = new HongBaoV2(child.getId());
               childUpdate.setState(hongBao.getState());
               childUpdate.setUpdateTime(new Date());
               BigDecimal rate = null;
               if (child.getType() == HongBaoV2.TYPE_YIJI) {// 一级分享赚
                  rate = hongBaoManageService.getFirstInviteRate(child.getUrank(),
                        commonOrder.getCreateTime().getTime());
               } else if (child.getType() == HongBaoV2.TYPE_ERJI) {// 二级分享赚
                  rate = hongBaoManageService.getSecondInviteRate(child.getUrank(),
                        commonOrder.getCreateTime().getTime());
               }
               // 以实际收入为准计算预估收益
               if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), rate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), rate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
            }
         } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
         BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
         HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
         hongBao.setUpdateTime(new Date());
         // 更改状态与资金
         if (commonOrder.getState() == CommonOrder.STATE_FK) {
            hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
         } else if (commonOrder.getState() == CommonOrder.STATE_JS) {
            hongBao.setState(HongBaoV2.STATE_KELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(commonOrder.getSettleTime());
            calendar.add(Calendar.MONTH, 1);
            hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                  calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
            hongBao.setMoney(new BigDecimal(0));
         }
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
         if (children != null)
            for (HongBaoV2 child : children) {
               HongBaoV2 childUpdate = new HongBaoV2(child.getId());
               // 统一设置状态
               childUpdate.setState(hongBao.getState());
               childUpdate.setUpdateTime(new Date());
               BigDecimal rate = null;
               if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 一级分享赚
                  rate = hongBaoManageService.getFirstShareRate(child.getUrank(),
                        commonOrder.getCreateTime().getTime());
               } else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 二级分享赚
                  rate = hongBaoManageService.getSecondShareRate(child.getUrank(),
                        commonOrder.getCreateTime().getTime());
               }
               // 以实际收入为准计算预估收益
               if (CommonOrder.STATE_JS == commonOrder.getState()) {
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), rate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               } else if (CommonOrder.STATE_FK == commonOrder.getState()) {
                  if (commonOrder.getOrderNo().equalsIgnoreCase("314134177659155087"))
                     System.out.println("");
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), rate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
            }
         } else
            throw new HongBaoException(2, "type错误");
   }
   @Transactional
   private void saveHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
         HongBaoV2 hongBao = new HongBaoV2();
         hongBao.setUserInfo(commonOrder.getUserInfo());
         hongBao.setCreateTime(new Date());
         hongBao.setType(HongBaoV2.TYPE_ZIGOU);
         hongBao.setVersion(2);
         if (commonOrder.getState() == CommonOrder.STATE_FK) {
            hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
         } else if (commonOrder.getState() == CommonOrder.STATE_JS) {
            hongBao.setState(HongBaoV2.STATE_KELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
            hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
            hongBao.setMoney(new BigDecimal(0));
      } else {
            throw new HongBaoException(3, "维权订单不能创建红包");
         }
         UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
         hongBao.setUrank(user.getRank());
         hongBaoV2Mapper.insertSelective(hongBao);
         // 添加红包与订单的映射
         HongBaoOrder hongBaoOrder = new HongBaoOrder();
         hongBaoOrder.setCommonOrder(commonOrder);
         hongBaoOrder.setCreateTime(new Date());
         hongBaoOrder.setHongBaoV2(hongBao);
         hongBaoOrderMapper.insertSelective(hongBaoOrder);
         UserInfo boss = threeSaleSerivce.getBoss(user.getId());
         if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1级BOSS存在且红包未失效
            // 插入一级子红包
            BigDecimal firstRate = hongBaoManageService
                  .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank());
            if (firstRate.compareTo(new BigDecimal(0)) <= 0)
               return;
            HongBaoV2 firstHongbao = new HongBaoV2();
            firstHongbao.setUserInfo(boss);
            firstHongbao.setUrank(boss.getRank());
            firstHongbao.setParent(hongBao);
            firstHongbao.setCreateTime(new Date());
            firstHongbao.setType(HongBaoV2.TYPE_YIJI);
            firstHongbao.setVersion(2);
            firstHongbao.setState(hongBao.getState());
            if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
               firstHongbao.setMoney(
                     MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), firstRate.divide(new BigDecimal(100))));
               Calendar calendar = Calendar.getInstance();
               calendar.setTime(commonOrder.getSettleTime());
               calendar.add(Calendar.MONTH, 1);
               firstHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                     calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                     "yyyy-M-dd")));
            } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
               firstHongbao.setMoney(
                     MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), firstRate.divide(new BigDecimal(100))));
            }
            hongBaoV2Mapper.insertSelective(firstHongbao);
            // 插入二级子红包
            boss = threeSaleSerivce.getBoss(boss.getId());
            if (boss != null) {// 二级BOSS存在
               BigDecimal secondRate = hongBaoManageService
                     .getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank());
               if (secondRate.compareTo(new BigDecimal(0)) <= 0)
                  return;
               HongBaoV2 secondHongbao = new HongBaoV2();
               secondHongbao.setUserInfo(boss);
               secondHongbao.setUrank(boss.getRank());
               secondHongbao.setParent(hongBao);
               secondHongbao.setCreateTime(new Date());
               secondHongbao.setType(HongBaoV2.TYPE_ERJI);
               secondHongbao.setVersion(2);
               secondHongbao.setState(hongBao.getState());
               if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                  firstHongbao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
                        secondRate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  secondHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                  secondHongbao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
                        secondRate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.insertSelective(secondHongbao);
      }
   }
      } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
         // 分享赚不加入失效的订单
         if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
            return;
         // 分享赚
         BigDecimal shareRate = hongBaoManageService.getShareRate(commonOrder.getCreateTime().getTime());
         HongBaoV2 hongBao = new HongBaoV2();
         hongBao.setCreateTime(new Date());
         hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
         hongBao.setVersion(2);
         if (commonOrder.getState() == CommonOrder.STATE_FK) {
            hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), shareRate.divide(new BigDecimal(100))));
         } else if (commonOrder.getState() == CommonOrder.STATE_JS) {
            hongBao.setState(HongBaoV2.STATE_KELINGQU);
            hongBao.setMoney(
                  MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), shareRate.divide(new BigDecimal(100))));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(commonOrder.getSettleTime());
            calendar.add(Calendar.MONTH, 1);
            hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                  calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
         }
         UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
         hongBao.setUrank(user.getRank());
         hongBao.setUserInfo(user);
         hongBaoV2Mapper.insertSelective(hongBao);
         // 插入红包与订单映射
         HongBaoOrder hongBaoOrder = new HongBaoOrder();
         hongBaoOrder.setCommonOrder(commonOrder);
         hongBaoOrder.setCreateTime(new Date());
         hongBaoOrder.setHongBaoV2(hongBao);
         hongBaoOrderMapper.insertSelective(hongBaoOrder);
      } else
         throw new HongBaoException(2, "type错误");
   }
}