admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/elme/ElmeOrderProcessServiceImpl.java
@@ -1,150 +1,154 @@
package com.yeshi.fanli.service.impl.elme;
import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.elme.ElmeHongBaoOrderMap;
import com.yeshi.fanli.entity.elme.ElmeOrder;
import com.yeshi.fanli.exception.elme.ElmeHongBaoOrderMapException;
import com.yeshi.fanli.exception.elme.ElmeOrderException;
import com.yeshi.fanli.service.inter.elme.ElmeHongBaoOrderMapService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderProcessService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.order.msg.UserOrderMsgNotificationService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.elme.ElmeOrderUtil;
@Service
public class ElmeOrderProcessServiceImpl implements ElmeOrderProcessService {
   @Resource
   private ElmeOrderService elmeOrderService;
   @Resource
   private ElmeHongBaoOrderMapService elmeHongBaoOrderMapService;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private HongBaoV2Service hongBaoV2Service;
   @Resource
   private UserOrderMsgNotificationService userOrderMsgNotificationService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Transactional(rollbackFor=Exception.class)
   @Override
   public void processOrder(ElmeOrder elmeOrder) throws ElmeOrderException {
      try {
         elmeOrderService.addOrder(elmeOrder);
      } catch (ElmeOrderException e) {
         return;
      }
      if (elmeOrder.getId() == null)
         return;
      // 查询订单是否存在
      ElmeHongBaoOrderMap map = elmeHongBaoOrderMapService.selectByOrderId(elmeOrder.getId());
      if (map == null)// 订单不存在
      {
         Long uid = null;
         if (StringUtil.isNullOrEmpty(elmeOrder.getChannelId())) {//新版本
            UserExtraTaoBaoInfo extra = userExtraTaoBaoInfoService.getByRelationId(elmeOrder.getRid());
            if (extra != null)
               uid = extra.getUser().getId();
         } else {//老版本
            uid = Long.parseLong(elmeOrder.getRid());
         }
         if (uid == null)
            return;
         // 查询映射用户
         UserInfo user = userInfoService.selectByPKey(uid);
         if (user == null)
            return;
         // 制造hongbao
         HongBaoV2 hongBao = createHongBao(elmeOrder, user);
         hongBao.setUpdateTime(new Date());
         hongBaoV2Service.insertSelective(hongBao);
         // 添加红包映射
         ElmeHongBaoOrderMap newMap = new ElmeHongBaoOrderMap();
         newMap.setCreateTime(new Date());
         newMap.setElmeOrder(elmeOrder);
         newMap.setHongBao(hongBao);
         try {
            elmeHongBaoOrderMapService.addHongBaoOrderMap(newMap);
         } catch (ElmeHongBaoOrderMapException e) {
            throw new ElmeOrderException(e.getCode(), e.getMsg());
         }
      } else {// 订单存在
         HongBaoV2 oldHongBao = hongBaoV2Service.selectByPrimaryKey(map.getHongBao().getId());
         if (oldHongBao == null)
            return;
         // 失效与到账状态的红包不需要修改
         if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
            return;
         HongBaoV2 hongBao = createHongBao(elmeOrder, null);
         if (hongBao.getState() != oldHongBao.getState()) {// 是否需要修改红包状态
            HongBaoV2 update = new HongBaoV2(oldHongBao.getId());
            update.setState(hongBao.getState());
            update.setPreGetTime(hongBao.getPreGetTime());
            update.setUpdateTime(new Date());
            hongBaoV2Service.updateByPrimaryKeySelective(update);
         }
      }
   }
   private HongBaoV2 createHongBao(ElmeOrder elmeOrder, UserInfo userInfo) {
      BigDecimal fanliRate = hongBaoManageService.getFanLiRate(elmeOrder.getOrderDate().getTime());
      HongBaoV2 hongBao = new HongBaoV2();
      hongBao.setCreateTime(new Date());
      hongBao.setGetTime(null);
      hongBao.setMoney(MoneyBigDecimalUtil.div(
            MoneyBigDecimalUtil.mul(ElmeOrderUtil.getCommission(elmeOrder.getPayMoney()), fanliRate),
            new BigDecimal(100)));
      if (elmeOrder.getIsSettle() == true)
         hongBao.setPreGetTime(new Date(elmeOrder.getOrderDate().getTime() + 1000 * 60 * 60 * 24 * 15L));
      if (elmeOrder.getPayMoney().compareTo(new BigDecimal(0)) <= 0||(elmeOrder.getState()!=null&&elmeOrder.getState()==0))
         hongBao.setState(HongBaoV2.STATE_SHIXIAO);
      else {
         if (elmeOrder.getIsSettle() == true) {
            hongBao.setState(HongBaoV2.STATE_KELINGQU);
         } else {
            hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
         }
      }
      hongBao.setType(HongBaoV2.TYPE_ELME);
      if (userInfo != null)
         hongBao.setUrank(userInfo.getRank());
      hongBao.setUserInfo(userInfo);
      hongBao.setVersion(2);
      return hongBao;
   }
}
package com.yeshi.fanli.service.impl.elme;
import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Resource;
import com.yeshi.fanli.entity.SystemEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.entity.elme.ElmeHongBaoOrderMap;
import com.yeshi.fanli.entity.elme.ElmeOrder;
import com.yeshi.fanli.exception.elme.ElmeHongBaoOrderMapException;
import com.yeshi.fanli.exception.elme.ElmeOrderException;
import com.yeshi.fanli.service.inter.elme.ElmeHongBaoOrderMapService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderProcessService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.order.msg.UserOrderMsgNotificationService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.elme.ElmeOrderUtil;
@Service
public class ElmeOrderProcessServiceImpl implements ElmeOrderProcessService {
    @Resource
    private ElmeOrderService elmeOrderService;
    @Resource
    private ElmeHongBaoOrderMapService elmeHongBaoOrderMapService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private HongBaoManageService hongBaoManageService;
    @Resource
    private HongBaoV2Service hongBaoV2Service;
    @Resource
    private UserOrderMsgNotificationService userOrderMsgNotificationService;
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void processOrder(ElmeOrder elmeOrder) throws ElmeOrderException {
        try {
            elmeOrderService.addOrder(elmeOrder);
        } catch (ElmeOrderException e) {
            return;
        }
        if (elmeOrder.getId() == null)
            return;
        // 查询订单是否存在
        ElmeHongBaoOrderMap map = elmeHongBaoOrderMapService.selectByOrderId(elmeOrder.getId());
        if (map == null)// 订单不存在
        {
            Long uid = null;
            if (StringUtil.isNullOrEmpty(elmeOrder.getChannelId())) {// 新版本
                UserExtraTaoBaoInfo extra = userExtraTaoBaoInfoService.getByRelationId(elmeOrder.getRid(), null);
                if (extra != null)
                    uid = extra.getUser().getId();
            } else {// 老版本
                uid = Long.parseLong(elmeOrder.getRid());
            }
            if (uid == null)
                return;
            // 查询映射用户
            UserInfo user = userInfoService.selectByPKey(uid);
            if (user == null)
                return;
            SystemEnum system = userInfoService.getUserSystem(uid);
            // 制造hongbao
            HongBaoV2 hongBao = createHongBao(elmeOrder, user, system);
            hongBao.setUpdateTime(new Date());
            hongBaoV2Service.insertSelective(hongBao);
            // 添加红包映射
            ElmeHongBaoOrderMap newMap = new ElmeHongBaoOrderMap();
            newMap.setCreateTime(new Date());
            newMap.setElmeOrder(elmeOrder);
            newMap.setHongBao(hongBao);
            try {
                elmeHongBaoOrderMapService.addHongBaoOrderMap(newMap);
            } catch (ElmeHongBaoOrderMapException e) {
                throw new ElmeOrderException(e.getCode(), e.getMsg());
            }
        } else {// 订单存在
            HongBaoV2 oldHongBao = hongBaoV2Service.selectByPrimaryKey(map.getHongBao().getId());
            if (oldHongBao == null)
                return;
            // 失效与到账状态的红包不需要修改
            if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
                return;
            SystemEnum system = userInfoService.getUserSystem(oldHongBao.getUserInfo().getId());
            HongBaoV2 hongBao = createHongBao(elmeOrder, null, system);
            if (hongBao.getState() != oldHongBao.getState()) {// 是否需要修改红包状态
                HongBaoV2 update = new HongBaoV2(oldHongBao.getId());
                update.setState(hongBao.getState());
                update.setPreGetTime(hongBao.getPreGetTime());
                update.setUpdateTime(new Date());
                hongBaoV2Service.updateByPrimaryKeySelective(update);
            }
        }
    }
    private HongBaoV2 createHongBao(ElmeOrder elmeOrder, UserInfo userInfo, SystemEnum system) {
        BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen,
                elmeOrder.getOrderDate().getTime(), system);
        HongBaoV2 hongBao = new HongBaoV2();
        hongBao.setCreateTime(new Date());
        hongBao.setGetTime(null);
        hongBao.setMoney(MoneyBigDecimalUtil.div(
                MoneyBigDecimalUtil.mul(ElmeOrderUtil.getCommission(elmeOrder.getPayMoney()), fanliRate),
                new BigDecimal(100)));
        if (elmeOrder.getIsSettle() == true)
            hongBao.setPreGetTime(new Date(elmeOrder.getOrderDate().getTime() + 1000 * 60 * 60 * 24 * 15L));
        if (elmeOrder.getPayMoney().compareTo(new BigDecimal(0)) <= 0
                || (elmeOrder.getState() != null && elmeOrder.getState() == 0))
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
        else {
            if (elmeOrder.getIsSettle() == true) {
                hongBao.setState(HongBaoV2.STATE_KELINGQU);
            } else {
                hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
            }
        }
        hongBao.setType(HongBaoV2.TYPE_ELME);
        if (userInfo != null)
            hongBao.setUrank(userInfo.getRank());
        hongBao.setUserInfo(userInfo);
        hongBao.setVersion(2);
        return hongBao;
    }
}