| | |
| | | package com.yeshi.fanli.service.impl.order;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Calendar;
|
| | | import java.util.Date;
|
| | | import java.util.Iterator;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.concurrent.ConcurrentHashMap;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import com.yeshi.fanli.dao.mybatis.order.InviteOrderSubsidyMapper;
|
| | | import com.yeshi.fanli.entity.bus.user.HongBaoV2;
|
| | | import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
|
| | | import com.yeshi.fanli.entity.order.CommonOrder;
|
| | | import com.yeshi.fanli.entity.order.HongBaoOrder;
|
| | | import com.yeshi.fanli.entity.order.InviteOrderSubsidy;
|
| | | import com.yeshi.fanli.exception.order.CommonOrderException;
|
| | | import com.yeshi.fanli.exception.order.InviteOrderSubsidyException;
|
| | | import com.yeshi.fanli.service.inter.order.CommonOrderService;
|
| | | import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
|
| | | import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
|
| | | import com.yeshi.fanli.service.inter.order.InviteOrderSubsidyService;
|
| | | import com.yeshi.fanli.service.inter.order.OrderHongBaoMapService;
|
| | | import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
|
| | | import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
|
| | | import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
|
| | | import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
|
| | | import com.yeshi.fanli.service.manger.user.UserLevelManager;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | | import com.yeshi.fanli.util.user.UserLevelUtil;
|
| | |
|
| | | @Service
|
| | | public class InviteOrderSubsidyServiceImpl implements InviteOrderSubsidyService {
|
| | | @Resource
|
| | | private InviteOrderSubsidyMapper inviteOrderSubsidyMapper;
|
| | |
|
| | | @Resource
|
| | | private HongBaoV2Service hongBaoV2Service;
|
| | |
|
| | | @Resource
|
| | | private HongBaoOrderService hongBaoOrderService;
|
| | |
|
| | | @Resource
|
| | | private OrderHongBaoMapService orderHongBaoMapService;
|
| | |
|
| | | @Resource
|
| | | private CommonOrderService commonOrderService;
|
| | |
|
| | | @Resource
|
| | | private HongBaoManageService hongBaoManageService;
|
| | |
|
| | | @Resource
|
| | | private UserVIPInfoService userVIPInfoService;
|
| | |
|
| | | @Resource
|
| | | private UserLevelManager userLevelManager;
|
| | |
|
| | | @Resource
|
| | | private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
|
| | |
|
| | | @Resource
|
| | | private ThreeSaleSerivce threeSaleSerivce;
|
| | |
|
| | | @Override
|
| | | public InviteOrderSubsidy getByOrderNoAndType(Long uid, String orderNo, Integer type) {
|
| | | return inviteOrderSubsidyMapper.getByOrderNoAndType(uid, orderNo, type);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public InviteOrderSubsidy getByOrderNoAndTypeForUpdate(Long uid, String orderNo, Integer type) {
|
| | | return inviteOrderSubsidyMapper.getByOrderNoAndTypeForUpdate(uid, orderNo, type);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<InviteOrderSubsidy> listByOrderNoAndType(String orderNo, Integer type) {
|
| | | return inviteOrderSubsidyMapper.listByOrderNoAndType(orderNo, type);
|
| | | }
|
| | |
|
| | | private void addOrderSubsidy(InviteOrderSubsidy orderSubsidy)
|
| | | throws InviteOrderSubsidyException, CommonOrderException {
|
| | | if (orderSubsidy.getMoney() == null || StringUtil.isNullOrEmpty(orderSubsidy.getOrderNo())
|
| | | || orderSubsidy.getSourceType() == null || orderSubsidy.getUid() == null)
|
| | | throw new InviteOrderSubsidyException(1, "数据不完整");
|
| | | InviteOrderSubsidy old = getByOrderNoAndType(orderSubsidy.getUid(), orderSubsidy.getOrderNo(),
|
| | | orderSubsidy.getSourceType());
|
| | | if (old != null) {// 修改
|
| | | if (old.getState() == InviteOrderSubsidy.STATE_INVALID
|
| | | || old.getState() == InviteOrderSubsidy.STATE_SUBSIDIZED)
|
| | | return;
|
| | |
|
| | | // 修改状态
|
| | | InviteOrderSubsidy update = new InviteOrderSubsidy(old.getId());
|
| | | update.setMoney(orderSubsidy.getMoney());
|
| | | if (old.getState() == InviteOrderSubsidy.STATE_UNKNOWN) {// 状态未确定之前才可以更改原始补贴资金
|
| | | update.setOriginalMoney(orderSubsidy.getOriginalMoney());
|
| | | }
|
| | | update.setState(orderSubsidy.getState());
|
| | | update.setUpdateTime(new Date());
|
| | | inviteOrderSubsidyMapper.updateByPrimaryKeySelective(update);
|
| | | } else {// 添加
|
| | | if (orderSubsidy.getCreateTime() == null)
|
| | | orderSubsidy.setCreateTime(new Date());
|
| | | inviteOrderSubsidyMapper.insertSelective(orderSubsidy);
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void addOrUpdateByOrder(String orderId, int sourceType) throws InviteOrderSubsidyException {
|
| | | List<HongBaoOrder> list = hongBaoOrderService.listByOrderIdAndSourceType(orderId, sourceType);
|
| | | if (list != null && list.size() > 0) {
|
| | | // 必须是自购订单才返利
|
| | | HongBaoV2 parent = hongBaoV2Service.selectByPrimaryKey(list.get(0).getHongBaoV2().getId());
|
| | | if (parent != null
|
| | | && (parent.getType() == HongBaoV2.TYPE_SHARE_GOODS || parent.getType() == HongBaoV2.TYPE_ZIGOU)) {//
|
| | |
|
| | | List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(sourceType, orderId);
|
| | | if (orderList == null || orderList.size() == 0)
|
| | | throw new InviteOrderSubsidyException(10, "订单为空");
|
| | | CommonOrder firstOrder = orderList.get(0);
|
| | | Date placeOrderTime = firstOrder.getThirdCreateTime();
|
| | |
|
| | | if (placeOrderTime.getTime() < Constant.NEW_ORDER_FANLI_RULE_TIME)
|
| | | return;
|
| | |
|
| | | int urank = firstOrder.getUrank();
|
| | | UserLevelEnum buyerUserLevel = UserLevelUtil.getByOrderRank(urank);
|
| | | if (buyerUserLevel == null)
|
| | | throw new InviteOrderSubsidyException(11, "用户等级为空");
|
| | |
|
| | | List<Long> idList = new ArrayList<>();
|
| | | for (HongBaoOrder ho : list)
|
| | | if (ho.getHongBaoV2() != null)
|
| | | idList.add(ho.getHongBaoV2().getId());
|
| | | // 查询该订单号下的所有子红包
|
| | | List<HongBaoV2> children = hongBaoV2Service.listChildrenByIds(idList);
|
| | | if (children != null && children.size() > 0) {
|
| | | Map<Long, Integer> uidHongBaoMap = new ConcurrentHashMap<>();
|
| | | for (HongBaoV2 v2 : children) {// 按用户ID统计红包
|
| | | Long uid = v2.getUserInfo().getId();
|
| | | if (v2.getState() == HongBaoV2.STATE_BUKELINGQU || v2.getState() == HongBaoV2.STATE_KELINGQU
|
| | | || v2.getState() == HongBaoV2.STATE_YILINGQU) {
|
| | | uidHongBaoMap.put(uid, v2.getType());
|
| | | }
|
| | | }
|
| | |
|
| | | List<HongBaoV2> hbList = hongBaoV2Service.listByIds(idList);
|
| | | Integer state = InviteOrderSubsidy.STATE_UNKNOWN;
|
| | |
|
| | | // 产生改订单的返利总金额
|
| | | BigDecimal totalBuyFanLiMoney = new BigDecimal(0);
|
| | | // 计算总返利
|
| | | for (HongBaoV2 v2 : hbList) {
|
| | | if (v2.getState() == HongBaoV2.STATE_BUKELINGQU || v2.getState() == HongBaoV2.STATE_KELINGQU
|
| | | || v2.getState() == HongBaoV2.STATE_YILINGQU)
|
| | | totalBuyFanLiMoney = totalBuyFanLiMoney.add(v2.getMoney());
|
| | | }
|
| | |
|
| | | // 计算奖励金
|
| | | for (Iterator<Long> its = uidHongBaoMap.keySet().iterator(); its.hasNext();) {
|
| | | Long uid = its.next();
|
| | | UserLevelEnum userLevel = userLevelManager.getUserLevel(uid, placeOrderTime);
|
| | |
|
| | | int type = uidHongBaoMap.get(uid);
|
| | | if (type == HongBaoV2.TYPE_YIJI || type == HongBaoV2.TYPE_ERJI
|
| | | || type == HongBaoV2.TYPE_SHARE_YIJI || type == HongBaoV2.TYPE_SHARE_ERJI) {
|
| | | BigDecimal money = null;
|
| | | if (type == HongBaoV2.TYPE_YIJI || type == HongBaoV2.TYPE_SHARE_YIJI)
|
| | | money = orderHongBaoMoneyComputeService.computeFirstTeamSubsidy(totalBuyFanLiMoney,
|
| | | placeOrderTime, buyerUserLevel, userLevel);
|
| | | else
|
| | | money = orderHongBaoMoneyComputeService.computeSecondTeamSubsidy(totalBuyFanLiMoney,
|
| | | placeOrderTime, buyerUserLevel, userLevel);
|
| | |
|
| | | if (money == null)
|
| | | continue;
|
| | |
|
| | | InviteOrderSubsidy orderSubsidy = new InviteOrderSubsidy();
|
| | | orderSubsidy.setMoney(money);
|
| | | orderSubsidy.setOriginalMoney(money);
|
| | | orderSubsidy.setUid(uid);
|
| | | orderSubsidy.setOrderNo(orderId);
|
| | | orderSubsidy.setSourceType(sourceType);
|
| | | orderSubsidy.setState(state);
|
| | | if (type == HongBaoV2.TYPE_YIJI || type == HongBaoV2.TYPE_SHARE_YIJI)
|
| | | orderSubsidy.setLevel(InviteOrderSubsidy.LEVEL_ONE);
|
| | | else
|
| | | orderSubsidy.setLevel(InviteOrderSubsidy.LEVEL_TWO);
|
| | |
|
| | | try {
|
| | | addOrderSubsidy(orderSubsidy);
|
| | | } catch (CommonOrderException e) {
|
| | | throw new InviteOrderSubsidyException(e.getCode(), e.getMsg());
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | boolean isSettle = false;
|
| | | if (orderList != null)
|
| | | for (CommonOrder commonOrder : orderList) {
|
| | | if (commonOrder.getState() == CommonOrder.STATE_JS
|
| | | || commonOrder.getState() == CommonOrder.STATE_WQ) {
|
| | | isSettle = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (isSettle) {
|
| | | validByOrderIdAndSourceType(orderId, sourceType);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | @Override
|
| | | public long countByOrderIdAndSourceType(String orderId, int sourceType) {
|
| | | return inviteOrderSubsidyMapper.countByOrderNoAndType(orderId, sourceType);
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void invalidByOrderIdAndSourceType(String orderId, int sourceType) {
|
| | | List<InviteOrderSubsidy> list = inviteOrderSubsidyMapper.listByOrderNoAndType(orderId, sourceType);
|
| | | if (list != null && list.size() > 0)
|
| | | for (InviteOrderSubsidy subsidy : list) {
|
| | | if (subsidy.getState() == InviteOrderSubsidy.STATE_UNKNOWN
|
| | | || subsidy.getState() == InviteOrderSubsidy.STATE_VALID) {
|
| | | InviteOrderSubsidy update = new InviteOrderSubsidy(subsidy.getId());
|
| | | update.setState(InviteOrderSubsidy.STATE_INVALID);
|
| | | update.setUpdateTime(new Date());
|
| | | update.setInValidTime(new Date());
|
| | | inviteOrderSubsidyMapper.updateByPrimaryKeySelective(update);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void validByOrderIdAndSourceType(String orderId, int sourceType) throws InviteOrderSubsidyException {
|
| | | // 查询预计到账时间
|
| | | List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(sourceType, orderId);
|
| | | if (orderList == null || orderList.size() == 0)
|
| | | throw new InviteOrderSubsidyException(31, "订单不存在");
|
| | |
|
| | | if (orderList.get(0).getSettleTime() == null)
|
| | | throw new InviteOrderSubsidyException(32, "订单尚未确认收货");
|
| | |
|
| | | // 确认收货时间的下月25号到账
|
| | | Calendar calendar = Calendar.getInstance();
|
| | | calendar.setTime(orderList.get(0).getSettleTime());
|
| | | calendar.add(Calendar.MONTH, 1);
|
| | | Date preGetTime = new Date(TimeUtil.convertToTimeTemp(
|
| | | calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd"));
|
| | |
|
| | | List<InviteOrderSubsidy> list = inviteOrderSubsidyMapper.listByOrderNoAndType(orderId, sourceType);
|
| | | if (list != null && list.size() > 0)
|
| | | for (InviteOrderSubsidy subsidy : list) {
|
| | | if (subsidy.getState() == InviteOrderSubsidy.STATE_UNKNOWN) {
|
| | | InviteOrderSubsidy update = new InviteOrderSubsidy(subsidy.getId());
|
| | | update.setState(InviteOrderSubsidy.STATE_VALID);
|
| | | update.setUpdateTime(new Date());
|
| | | update.setValidTime(new Date());
|
| | | update.setPreGetTime(preGetTime);
|
| | | inviteOrderSubsidyMapper.updateByPrimaryKeySelective(update);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void settleById(Long id, BigDecimal money) {
|
| | | InviteOrderSubsidy orderSubsidy = new InviteOrderSubsidy(id);
|
| | | orderSubsidy.setMoney(money);
|
| | | orderSubsidy.setState(InviteOrderSubsidy.STATE_SUBSIDIZED);
|
| | | orderSubsidy.setUpdateTime(new Date());
|
| | | inviteOrderSubsidyMapper.updateByPrimaryKeySelective(orderSubsidy);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public BigDecimal sumRecievedMoneyByUid(long uid, Integer level) {
|
| | | BigDecimal money = inviteOrderSubsidyMapper.sumRecievedMoneyByUid(uid, level);
|
| | | if (money == null) {
|
| | | money = new BigDecimal(0);
|
| | | }
|
| | | return money;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public BigDecimal sumValidMoneyByUidAndDate(long uid, Integer day, Integer level) {
|
| | | BigDecimal money = inviteOrderSubsidyMapper.sumValidMoneyByUidAndDate(uid, day, level);
|
| | | if (money == null) {
|
| | | money = new BigDecimal(0);
|
| | | }
|
| | | return money;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public BigDecimal sumMoneyByUidAndDateAndState(long uid, Integer day, Integer level, Integer state) {
|
| | | BigDecimal money = inviteOrderSubsidyMapper.sumMoneyByUidAndDateAndState(uid, day, level, state);
|
| | | if (money == null) {
|
| | | money = new BigDecimal(0);
|
| | | }
|
| | | return money;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<InviteOrderSubsidy> listByOrderNoAndTypeAndUid(String orderNo, Integer type, Long uid) {
|
| | | return inviteOrderSubsidyMapper.listByOrderNoAndTypeAndUid(orderNo, type, uid);
|
| | | }
|
| | |
|
| | | }
|