admin
2018-12-25 4cb15e222cd7d099d533ccbeb7f9a8cd99bf180c
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java
@@ -23,6 +23,7 @@
import com.yeshi.fanli.dao.mybatis.ThreeSaleGiftMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dao.mybatis.hongbao.HongBaoMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper;
import com.yeshi.fanli.dao.mybatis.order.OrderItemMapper;
import com.yeshi.fanli.dao.mybatis.order.OrderMapper;
import com.yeshi.fanli.dao.mybatis.share.PidUserMapper;
@@ -34,6 +35,7 @@
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.OrderItem;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.entity.taobao.PidOrder;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
@@ -44,7 +46,10 @@
import com.yeshi.fanli.exception.ObjectStateException;
import com.yeshi.fanli.exception.OrderItemException;
import com.yeshi.fanli.exception.TaoBaoWeiQuanException;
import com.yeshi.fanli.exception.money.UserMoneyDetailException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.AccountDetailsHongBaoMapService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoService;
import com.yeshi.fanli.service.inter.order.LostOrderService;
import com.yeshi.fanli.service.inter.order.OrderItemServcie;
@@ -62,6 +67,7 @@
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.factory.AccountDetailsFactory;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
@Service
@@ -135,6 +141,15 @@
   @Resource
   private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private AccountDetailsHongBaoMapService accountDetailsHongBaoMapService;
   @Resource
   private UserMoneyDetailMapper userMoneyDetailMapper;
   @Override
   public void processOrder(Map<String, List<TaoBaoOrder>> orders) {
@@ -417,7 +432,7 @@
         orderMapper.updateByPrimaryKeySelective(updateOrder);
         Order order = oldOrder;
         // 订单返利比例
         BigDecimal rate = new BigDecimal(hongBaoManageMapper.selectByKey("hongbao_goods_proportion").getValue());
         BigDecimal rate = hongBaoManageService.getFanLiRate();
         // 查找是否存在红包
         List<HongBao> hongBaoList = hongBaoMapper.selectByOid(order.getId());
@@ -501,6 +516,23 @@
                  AccountDetailsFactory.FANLI, orderItem, null, hb.getUserInfo());
            accountDetailsMapper.insertSelective(accountDetails);
            // 插入新版资金明细
            try {
               UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createFanLi(hb.getUserInfo().getId(),
                     hb.getOrderId(), 1,hb.getId(), hb.getMoney());
               userMoneyDetail.setId(accountDetails.getId());
               userMoneyDetailMapper.insert(userMoneyDetail);
            } catch (UserMoneyDetailException e1) {
               try {
                  LogHelper.errorDetailInfo(e1);
               } catch (Exception e2) {
                  e2.printStackTrace();
               }
            }
            // 添加资金明细与红包的映射关系
            accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hb.getId(), accountDetails.getId());
            // 维权扣款(2018-08-05后开始实行)
            if (hb.getBalanceTime() != null
                  && hb.getBalanceTime().getTime() >= TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) {
@@ -573,25 +605,49 @@
      }
   }
   private HongBao filterWeiQuanINGHongBao(HongBao hongBao) {
      List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList = taoBaoWeiQuanOrderMapper
            .selectListByOrderIdAndState(hongBao.getOrderId(), "维权创建");
      List<TaoBaoWeiQuanOrder> taoBaoWeiQuanList1 = taoBaoWeiQuanOrderMapper
            .selectListByOrderIdAndState(hongBao.getOrderId(), "等待处理");
      if ((taoBaoWeiQuanList != null && taoBaoWeiQuanList.size() > 0)
            || (taoBaoWeiQuanList1 != null && taoBaoWeiQuanList1.size() > 0)) {
         return null;
      }
      return hongBao;
   }
   @Override
   @Transactional
   public void fanliInvaiteAndShare(Long uid) throws TaoBaoWeiQuanException {
      /**
       * 处理邀请赚订单
       */
      // 查询UID的邀请赚订单
      List<HongBao> hongBaoList = hongBaoMapper.selectCanBalanceHongBaoByChildHongBaoAndUid(uid);
      BigDecimal money = new BigDecimal(0);
      List<Long> hbIdList = new ArrayList<>();
      BigDecimal invitemoney = new BigDecimal(0);
      // 需要判断退款的订单号
      Set<String> drawBackOrders = new HashSet<String>();
      for (HongBao hongBao : hongBaoList) {
         hongBao = filterWeiQuanINGHongBao(hongBao);
         if (hongBao == null)
            continue;
         hongBao = hongBaoMapper.selectByPrimaryKeyForUpdate(hongBao.getId());
         if (hongBao.getState() == HongBao.STATE_BUKELINGQU || hongBao.getState() == HongBao.STATE_KELINGQU) {
            money = money.add(hongBao.getMoney());
            invitemoney = invitemoney.add(hongBao.getMoney());
            HongBao updateHongBao = new HongBao();
            updateHongBao.setId(hongBao.getId());
            updateHongBao.setGetTime(System.currentTimeMillis());
            updateHongBao.setState(HongBao.STATE_YILINGQU);
            hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
            // 添加到红包返利记录集合
            hbIdList.add(hongBao.getId());
            // 2018-08-05 过后的订单才处理维权
            Date balanceTime = null;
            if (hongBao.getParent() != null)
@@ -607,18 +663,106 @@
            }
         }
      }
      /**
       * 处理一级二级分享赚(属于邀请赚类型)
       */
      List<HongBao> totalHongBaoList = new ArrayList<>();
      // 查询UID的二级或者三级分享赚订单
      // TODO 暂时查询10000条数据,后面再做分页
      List<HongBao> hbList = hongBaoMapper.selectCanBalanceHongBaoByTypeAndUid(HongBao.TYPE_SHARE_YIJI, uid, 10000);
      if (hbList != null && hbList.size() > 0)
         totalHongBaoList.addAll(hbList);
      hbList = hongBaoMapper.selectCanBalanceHongBaoByTypeAndUid(HongBao.TYPE_SHARE_ERJI, uid, 10000);
      if (hbList != null && hbList.size() > 0)
         totalHongBaoList.addAll(hbList);
      for (HongBao hb : totalHongBaoList) {
         if (hb.getState() == HongBao.STATE_BUKELINGQU || hb.getState() == HongBao.STATE_KELINGQU) {
            hb = filterWeiQuanINGHongBao(hb);
            if (hb == null)
               continue;
            invitemoney = invitemoney.add(hb.getMoney());
            HongBao updateHongBao = new HongBao();
            updateHongBao.setId(hb.getId());
            updateHongBao.setGetTime(System.currentTimeMillis());
            updateHongBao.setState(HongBao.STATE_YILINGQU);
            hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
            // 添加到红包返利记录集合
            hbIdList.add(hb.getId());
            if (!StringUtil.isNullOrEmpty(hb.getOrderId()))
               drawBackOrders.add(hb.getOrderId());
         }
      }
      // 邀请赚到账
      if (invitemoney.compareTo(new BigDecimal(0)) > 0) {
         userInfoMapper.addHongBaoByUid(uid, invitemoney);
         // 添加记录
         AccountDetails accountDetails = AccountDetailsFactory.create("+" + invitemoney,
               AccountDetailsFactory.TICHENG, null, null, new UserInfo(uid));
         accountDetailsMapper.insertSelective(accountDetails);
         // 添加新版详情记录
         try {
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInvite(uid, 0, 0, 0, invitemoney,
                  new Date());
            userMoneyDetail.setId(accountDetails.getId());
            userMoneyDetailMapper.insert(userMoneyDetail);
         } catch (UserMoneyDetailException e) {
            try {
               LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
               e1.printStackTrace();
            }
         }
         // 记录返利红包与资金详情的对应关系
         // 添加到红包返利记录集合
         accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, accountDetails.getId());
         // 发送推送
         try {
            // 提成到账消息通知
            userNotificationService.tiChengInviteRecieved(uid, invitemoney);
         } catch (Exception e) {
            e.printStackTrace();
         }
         for (String orderId : drawBackOrders)
            taoBaoWeiQuanDrawBackService.doWeiQuanInvite(orderId);
      }
      hbIdList.clear();
      drawBackOrders.clear();
      /**
       * 处理分享赚
       */
      BigDecimal sharemoney = new BigDecimal(0);
      // 查询UID的分享赚订单
      List<PidOrder> pidOrderList = pidOrderMapper.getCanBalanceListByUid(uid);
      for (PidOrder pidOrder : pidOrderList) {
         HongBao hongBao = hongBaoMapper.selectByPrimaryKeyForUpdate(pidOrder.getHongBao().getId());
         hongBao = filterWeiQuanINGHongBao(hongBao);
         if (hongBao == null)
            continue;
         if (hongBao.getState() == HongBao.STATE_BUKELINGQU || hongBao.getState() == HongBao.STATE_KELINGQU) {
            money = money.add(hongBao.getMoney());
            sharemoney = sharemoney.add(hongBao.getMoney());
            HongBao updateHongBao = new HongBao();
            updateHongBao.setId(hongBao.getId());
            updateHongBao.setGetTime(System.currentTimeMillis());
            updateHongBao.setState(HongBao.STATE_YILINGQU);
            hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
            // 添加到红包返利记录集合
            hbIdList.add(hongBao.getId());
            // 2018-08-05 过后的订单才处理维权
            Date balanceTime = null;
            if (hongBao.getParent() != null)
@@ -632,47 +776,48 @@
         }
      }
      List<HongBao> totalHongBaoList = new ArrayList<>();
      // 查询UID的二级或者三级分享赚订单
      // TODO 暂时查询10000条数据,后面再做分页
      List<HongBao> hbList = hongBaoMapper.selectCanBalanceHongBaoByTypeAndUid(HongBao.TYPE_SHARE_YIJI, uid, 10000);
      if (hbList != null && hbList.size() > 0)
         totalHongBaoList.addAll(hbList);
      hbList = hongBaoMapper.selectCanBalanceHongBaoByTypeAndUid(HongBao.TYPE_SHARE_ERJI, uid, 10000);
      if (hbList != null && hbList.size() > 0)
         totalHongBaoList.addAll(hbList);
      /**
       * 分享赚到账
       */
      for (HongBao hb : totalHongBaoList) {
         if (hb.getState() == HongBao.STATE_BUKELINGQU || hb.getState() == HongBao.STATE_KELINGQU) {
            money = money.add(hb.getMoney());
            HongBao updateHongBao = new HongBao();
            updateHongBao.setId(hb.getId());
            updateHongBao.setGetTime(System.currentTimeMillis());
            updateHongBao.setState(HongBao.STATE_YILINGQU);
            hongBaoMapper.updateByPrimaryKeySelective(updateHongBao);
            if (!StringUtil.isNullOrEmpty(hb.getOrderId()))
               drawBackOrders.add(hb.getOrderId());
         }
      }
      if (money.compareTo(new BigDecimal(0)) > 0) {
         userInfoMapper.addHongBaoByUid(uid, money);
      if (sharemoney.compareTo(new BigDecimal(0)) > 0) {
         userInfoMapper.addHongBaoByUid(uid, sharemoney);
         // 添加记录
         AccountDetails accountDetails = AccountDetailsFactory.create("+" + money, AccountDetailsFactory.TICHENG,
               null, null, new UserInfo(uid));
         AccountDetails accountDetails = AccountDetailsFactory.create("+" + sharemoney,
               AccountDetailsFactory.SHARE_GOODS, null, null, new UserInfo(uid));
         accountDetailsMapper.insertSelective(accountDetails);
         // 添加新版详情记录
         try {
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, 0, 0, 0, sharemoney,
                  new Date());
            userMoneyDetail.setId(accountDetails.getId());
            userMoneyDetailMapper.insert(userMoneyDetail);
         } catch (UserMoneyDetailException e) {
            try {
               LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
               e1.printStackTrace();
            }
         }
         // 记录返利红包与资金详情的对应关系
         // 添加到红包返利记录集合
         accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(hbIdList, accountDetails.getId());
         // 发送推送
         try {
            // 提成到账消息通知
            userNotificationService.tiChengRecieved(uid, money);
            userNotificationService.tiChengShareRecieved(uid, sharemoney);
         } catch (Exception e) {
            e.printStackTrace();
         }
         for (String orderId : drawBackOrders)
            taoBaoWeiQuanDrawBackService.doWeiQuanShare(orderId);
      }
   }
}