| | |
| | | package com.yeshi.fanli.service.impl.order;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.util.ArrayList;
|
| | | 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.Order;
|
| | | import com.yeshi.fanli.entity.order.HongBaoOrder;
|
| | | import com.yeshi.fanli.entity.order.InviteOrderSubsidy;
|
| | | import com.yeshi.fanli.exception.order.InviteOrderSubsidyException;
|
| | | 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.OrderService;
|
| | | import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
|
| | | import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | |
|
| | | @Service
|
| | | public class InviteOrderSubsidyServiceImpl implements InviteOrderSubsidyService {
|
| | |
|
| | | @Resource
|
| | | private InviteOrderSubsidyMapper inviteOrderSubsidyMapper;
|
| | |
|
| | | @Resource
|
| | | private HongBaoV2Service hongBaoV2Service;
|
| | |
|
| | | @Resource
|
| | | private HongBaoOrderService hongBaoOrderService;
|
| | |
|
| | | @Resource
|
| | | private OrderHongBaoMapService orderHongBaoMapService;
|
| | |
|
| | | @Resource
|
| | | private OrderService orderService;
|
| | |
|
| | | @Resource
|
| | | private HongBaoManageService hongBaoManageService;
|
| | |
|
| | | @Resource
|
| | | private UserVIPInfoService userVIPInfoService;
|
| | |
|
| | | @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);
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | private void addOrderSubsidy(InviteOrderSubsidy orderSubsidy) throws InviteOrderSubsidyException {
|
| | | // TODO 确定生效日期 机制2020年生效
|
| | | if (System.currentTimeMillis() < TimeUtil.convertToTimeTemp("2020-01-01", "yyyy-MM-dd")&&!Constant.IS_TEST) {
|
| | | return;
|
| | | }
|
| | |
|
| | | 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
|
| | | @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_ZIGOU && parent.getUrank() != 100) {// 不是会员的自购才补贴
|
| | | Order order = orderService.findOrderByOrderIdAndType(orderId, sourceType);
|
| | | 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);
|
| | | int state = InviteOrderSubsidy.STATE_UNKNOWN;
|
| | | // 确定是否使用了奖励券
|
| | | if (orderHongBaoMapService.selectByOrderIdAndSourceType(orderId, sourceType) != null) {
|
| | | state = InviteOrderSubsidy.STATE_INVALID;
|
| | | } else {
|
| | | // 确定是否返利到账5天后
|
| | | if (recieveMoneyMoreThan(hbList, 5)) {
|
| | | state = InviteOrderSubsidy.STATE_VALID;
|
| | | } else {
|
| | | state = InviteOrderSubsidy.STATE_UNKNOWN;
|
| | | }
|
| | | }
|
| | |
|
| | | BigDecimal totalMoney = new BigDecimal(0);
|
| | | // 计算总返利
|
| | | for (HongBaoV2 v2 : hbList) {
|
| | | if (v2.getState() == HongBaoV2.STATE_BUKELINGQU || v2.getState() == HongBaoV2.STATE_KELINGQU
|
| | | || v2.getState() == HongBaoV2.STATE_YILINGQU)
|
| | | totalMoney = totalMoney.add(v2.getMoney());
|
| | | }
|
| | |
|
| | | BigDecimal firstRate = hongBaoManageService
|
| | | .getInviteFirstLevelSubsidyRate(order.getThirdCreateTime().getTime());
|
| | | BigDecimal secondRate = hongBaoManageService
|
| | | .getInviteSecondLevelSubsidyRate(order.getThirdCreateTime().getTime());
|
| | |
|
| | | // 计算奖励金
|
| | | for (Iterator<Long> its = uidHongBaoMap.keySet().iterator(); its.hasNext();) {
|
| | | Long uid = its.next();
|
| | | int type = uidHongBaoMap.get(uid);
|
| | | if (type == HongBaoV2.TYPE_YIJI || type == HongBaoV2.TYPE_ERJI) {
|
| | | BigDecimal rate = null;
|
| | | if (type == HongBaoV2.TYPE_YIJI)
|
| | | rate = firstRate;
|
| | | else
|
| | | rate = secondRate;
|
| | | BigDecimal money = MoneyBigDecimalUtil.div(MoneyBigDecimalUtil.mul(totalMoney, rate),
|
| | | new BigDecimal(100));
|
| | | InviteOrderSubsidy orderSubsidy = new InviteOrderSubsidy();
|
| | | orderSubsidy.setMoney(money);
|
| | | orderSubsidy.setOriginalMoney(money);
|
| | | orderSubsidy.setUid(uid);
|
| | | orderSubsidy.setOrderNo(orderId);
|
| | | orderSubsidy.setSourceType(sourceType);
|
| | | orderSubsidy.setState(state);
|
| | | // VIP用户才能补贴
|
| | | if (userVIPInfoService.isVIP(uid))
|
| | | addOrderSubsidy(orderSubsidy);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | private boolean recieveMoneyMoreThan(List<HongBaoV2> hbList, int day) {
|
| | | // 是否全部是到账状态或失效状态
|
| | | int invalidCount = 0;// 失效个数
|
| | | int recieveCount = 0;// 到账的个数
|
| | | long maxGetTime = 0;
|
| | | for (HongBaoV2 v2 : hbList) {
|
| | | if (v2.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | invalidCount++;
|
| | | else if (v2.getState() == HongBaoV2.STATE_YILINGQU) {
|
| | | recieveCount++;
|
| | | if (v2.getGetTime() != null && v2.getGetTime().getTime() > maxGetTime)
|
| | | maxGetTime = v2.getGetTime().getTime();
|
| | |
|
| | | }
|
| | | }
|
| | | if (invalidCount + recieveCount == hbList.size() && recieveCount > 0 && maxGetTime > 0) {// 全部已到账
|
| | | // 查询到账时间距离当前时间是否超过指定天
|
| | | if (System.currentTimeMillis() - maxGetTime > 1000 * 60 * 60 * 24L * day)
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public long countByOrderIdAndSourceType(String orderId, int sourceType) {
|
| | | return inviteOrderSubsidyMapper.countByOrderNoAndType(orderId, sourceType);
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | @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());
|
| | | inviteOrderSubsidyMapper.updateByPrimaryKeySelective(update);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | @Override
|
| | | public void validByOrderIdAndSourceType(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) {
|
| | | InviteOrderSubsidy update = new InviteOrderSubsidy(subsidy.getId());
|
| | | update.setState(InviteOrderSubsidy.STATE_VALID);
|
| | | update.setUpdateTime(new Date());
|
| | | 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);
|
| | | }
|
| | |
|
| | | }
|