|
@@ -0,0 +1,496 @@
|
|
|
+package com.qs.mp.user.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qs.mp.admin.domain.TicketBox;
|
|
|
+import com.qs.mp.admin.domain.vo.*;
|
|
|
+import com.qs.mp.admin.service.*;
|
|
|
+import com.qs.mp.channel.domain.*;
|
|
|
+import com.qs.mp.channel.domain.param.ChannelCommParam;
|
|
|
+import com.qs.mp.channel.service.*;
|
|
|
+import com.qs.mp.common.core.domain.AjaxResult;
|
|
|
+import com.qs.mp.common.core.redis.DistributedLocker;
|
|
|
+import com.qs.mp.common.core.redis.RedisCache;
|
|
|
+import com.qs.mp.common.enums.*;
|
|
|
+import com.qs.mp.common.exception.ServiceException;
|
|
|
+import com.qs.mp.common.utils.DateUtils;
|
|
|
+import com.qs.mp.common.utils.LogUtil;
|
|
|
+import com.qs.mp.framework.redis.RedisLockKey;
|
|
|
+import com.qs.mp.framework.service.IAsyncTaskService;
|
|
|
+import com.qs.mp.pay.domain.PayOrder;
|
|
|
+import com.qs.mp.system.domain.SysUser;
|
|
|
+import com.qs.mp.system.service.ISysUserService;
|
|
|
+import com.qs.mp.system.service.id.BizIdGenerator;
|
|
|
+import com.qs.mp.user.domain.*;
|
|
|
+import com.qs.mp.user.domain.vo.*;
|
|
|
+import com.qs.mp.user.mapper.GroupBuyingOrderMapper;
|
|
|
+import com.qs.mp.user.service.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.aop.framework.AopContext;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 用户盲票订单 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author quanshu
|
|
|
+ * @since 2022-03-07
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class UserGroupOrderServiceImpl extends
|
|
|
+ ServiceImpl<GroupBuyingOrderMapper, GroupBuyingOrder> implements IUserGroupOrderService{
|
|
|
+
|
|
|
+ protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${bind.channelId}")
|
|
|
+ private Long bindChannelId;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IChannelUserRelService channelUserRelService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IChannelService channelService;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BizIdGenerator bizIdGenerator;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IAsyncTaskService asyncTaskService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DistributedLocker distributedLocker;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IGroupBuyingService groupBuyingService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IGroupBuyingGroupService groupBuyingGroupService;
|
|
|
+ @Autowired
|
|
|
+ private IUserAddrService userAddrService;
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService userService;
|
|
|
+ @Autowired
|
|
|
+ private IChannelMoneyLogService channelMoneyLogService;
|
|
|
+ @Autowired
|
|
|
+ private IChannelCommissionService channelCommissionService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String submitOrder(Long userId, GroupOrderSettleVO orderSettleVO,
|
|
|
+ UserShareVO userShareVO) {
|
|
|
+ UserGroupOrderServiceImpl proxy = (UserGroupOrderServiceImpl) AopContext.currentProxy();
|
|
|
+ GroupBuying groupBuying = groupBuyingService.getById(orderSettleVO.getGroupbuyingId());
|
|
|
+ GroupBuyingGroup groupBuyingGroup = groupBuyingGroupService.getById(orderSettleVO.getGroupId());
|
|
|
+ //再次校验并锁次数
|
|
|
+ checkGroup(orderSettleVO,groupBuying,groupBuyingGroup,true);
|
|
|
+ return proxy.createOnlineOrder(userId, orderSettleVO, userShareVO,groupBuying,groupBuyingGroup);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean checkGroup(GroupOrderSettleVO orderSettleVO,GroupBuying groupBuying,GroupBuyingGroup groupBuyingGroup,boolean updateGroupNumber) {
|
|
|
+ boolean flag =false;
|
|
|
+ //发起拼团再次校验
|
|
|
+ String key = RedisLockKey.build(RedisLockKey.USER_INITIATE_GROUP_LOCK,orderSettleVO.getGroupbuyingId(),orderSettleVO.getUserId());
|
|
|
+ String groupUserKey = RedisLockKey.build(RedisLockKey.USER_JOIN_GROUP_LOCK,orderSettleVO.getGroupbuyingId(),orderSettleVO.getUserId());
|
|
|
+ if (!distributedLocker.tryLock(key, 3, -1, TimeUnit.SECONDS)) {
|
|
|
+ throw new ServiceException("拼团活动太火爆了,请稍后重试!");
|
|
|
+ }
|
|
|
+ if (!distributedLocker.tryLock(groupUserKey, 3, -1, TimeUnit.SECONDS)) {
|
|
|
+ throw new ServiceException("拼团活动太火爆了,请稍后重试!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(groupBuyingGroup.getGroupStatus().equals(GroupingStatusEnum.END.getValue())){
|
|
|
+ throw new ServiceException("拼团以开奖!");
|
|
|
+ }
|
|
|
+ if(groupBuyingGroup.getGroupStatus().equals(GroupingStatusEnum.CLOSE.getValue())){
|
|
|
+ throw new ServiceException("拼团以结束!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!checkBuying(groupBuying)) {
|
|
|
+ throw new ServiceException("活动已经过期,下次早点来哦!");
|
|
|
+ }
|
|
|
+ if(groupBuying.getIsPutaway() == 1){
|
|
|
+ throw new ServiceException("活动已下架!");
|
|
|
+ }
|
|
|
+ if(orderSettleVO.getGroupType() == 1){
|
|
|
+ if(ObjectUtil.isNotEmpty(redisCache.getCacheObject(key))){
|
|
|
+ int count = redisCache.getCacheObject(key);
|
|
|
+ if (count <= 0){
|
|
|
+ throw new ServiceException("您的拼团次数已经达到上限啦!");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //设置当前用户发起拼团的最大次数
|
|
|
+ redisCache.setCacheObject(key,groupBuying.getStartGroupTimes());
|
|
|
+ }
|
|
|
+ //如果用户参团次数缓存为空,并且系统设置了每个用户的允许参团的次数
|
|
|
+ if(ObjectUtil.isEmpty(redisCache.getCacheList(groupUserKey)) && groupBuying.getPartinGroupTimes() != null){
|
|
|
+ redisCache.setCacheObject(groupUserKey,groupBuying.getPartinGroupTimes());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(orderSettleVO.getGroupType() == 2){
|
|
|
+ //当前团参团人数小于开团人数
|
|
|
+ if(groupBuyingGroup.getPartinNum() < groupBuying.getGroupSize()){
|
|
|
+ //判断用户是否还有参团名额
|
|
|
+ int groupCount = -1;
|
|
|
+ //设置了用户参团次数
|
|
|
+ if(Objects.nonNull(redisCache.getCacheObject(groupUserKey))){
|
|
|
+ groupCount = redisCache.getCacheObject(groupUserKey);
|
|
|
+ }
|
|
|
+ if (groupCount == 0){
|
|
|
+ throw new ServiceException("您的参团次数已经达到上限啦!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //锁定名额
|
|
|
+ if(updateGroupNumber){
|
|
|
+ updateGroupNumber(orderSettleVO,groupBuying,groupBuyingGroup);
|
|
|
+ }
|
|
|
+ }finally {
|
|
|
+ distributedLocker.unlock(key);
|
|
|
+ distributedLocker.unlock(groupUserKey);
|
|
|
+ }
|
|
|
+ flag = true;
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean updateGroupNumber(GroupOrderSettleVO orderSettleVO, GroupBuying groupBuying, GroupBuyingGroup groupBuyingGroup) {
|
|
|
+ String key = RedisLockKey.build(RedisLockKey.USER_INITIATE_GROUP_LOCK,orderSettleVO.getGroupbuyingId(),orderSettleVO.getUserId());
|
|
|
+ String groupUserKey = RedisLockKey.build(RedisLockKey.USER_JOIN_GROUP_LOCK,orderSettleVO.getGroupbuyingId(),orderSettleVO.getUserId());
|
|
|
+ //发起拼团,更新用户剩余拼团次数
|
|
|
+ if(orderSettleVO.getGroupType() == 1){
|
|
|
+ redisCache.decrement(key,1);
|
|
|
+ redisCache.decrement(groupUserKey,1);
|
|
|
+ }
|
|
|
+ //参与拼团,更新用户参团次数
|
|
|
+ if(orderSettleVO.getGroupType() == 2){
|
|
|
+ redisCache.decrement(groupUserKey,1);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验活动是否过期
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean checkBuying(GroupBuying groupBuying){
|
|
|
+ Date now = new Date();
|
|
|
+ Date start = groupBuying.getStartTime();
|
|
|
+ Date end = groupBuying.getEndTime();
|
|
|
+ if(now.compareTo(start) >= 0 || now.compareTo(end) <= 0){
|
|
|
+ return true;
|
|
|
+ }else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean batchCancelOrder(String boxId, List<String> orderIds) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public String createOnlineOrder(Long userId, GroupOrderSettleVO orderSettleVO,
|
|
|
+ UserShareVO userShareVO,GroupBuying groupBuying,GroupBuyingGroup groupBuyingGroup) {
|
|
|
+
|
|
|
+
|
|
|
+ // 创建订单
|
|
|
+ SysUser sysUser = userService.getById(userId);
|
|
|
+ UserAddr userAddr = userAddrService.getDefaultChannelAddr(userId);
|
|
|
+ if(null != orderSettleVO.getUserAddrId()){
|
|
|
+ userAddr = userAddrService.getById(orderSettleVO.getUserAddrId());
|
|
|
+ }
|
|
|
+ GroupBuyingOrder groupBuyingOrder = new GroupBuyingOrder();
|
|
|
+ groupBuyingOrder.setOrderId(bizIdGenerator.newIdWithUidSharding(String.valueOf(userId)));
|
|
|
+ groupBuyingOrder.setUserId(userId);
|
|
|
+ groupBuyingOrder.setGroupbuyingTitle(groupBuying.getTitle());
|
|
|
+ groupBuyingOrder.setUserId(userId);
|
|
|
+ groupBuyingOrder.setAddress(userAddr.getAddr());
|
|
|
+ groupBuyingOrder.setProvince(userAddr.getProvince());
|
|
|
+ groupBuyingOrder.setCity(userAddr.getCity());
|
|
|
+ groupBuyingOrder.setArea(userAddr.getArea());
|
|
|
+ groupBuyingOrder.setCommStatus(CommStatusEnum.NO.getValue());
|
|
|
+ groupBuyingOrder.setGroupbuyingId(groupBuying.getId());
|
|
|
+ groupBuyingOrder.setGroupId(groupBuyingGroup.getId());
|
|
|
+ groupBuyingOrder.setOrderAmt(orderSettleVO.getOrderAmt());
|
|
|
+ groupBuyingOrder.setPayType(orderSettleVO.getPayType());
|
|
|
+ groupBuyingOrder.setAppId(orderSettleVO.getAppId());
|
|
|
+ groupBuyingOrder.setPhonenumber(sysUser.getPhonenumber());
|
|
|
+ groupBuyingOrder.setPrizeTitle(orderSettleVO.getPrizeTitle());
|
|
|
+ groupBuyingOrder.setPrizeQuantity(orderSettleVO.getPrizeQuantity());
|
|
|
+ groupBuyingOrder.setPrizeType(orderSettleVO.getPrizeType());
|
|
|
+ groupBuyingOrder.setPayAmt(orderSettleVO.getOrderAmt());
|
|
|
+ groupBuyingOrder.setGroupType(orderSettleVO.getGroupType());
|
|
|
+
|
|
|
+ if (orderSettleVO.getOrderAmt() == 0) {
|
|
|
+ groupBuyingOrder.setCommStatus(CommStatusEnum.YES.getValue()); // 无需结佣,直接置为已结佣
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询用户的所属经销商,线上票直接查询
|
|
|
+ ChannelUserRel channelUserRel = channelUserRelService.getOne(
|
|
|
+ new LambdaQueryWrapper<ChannelUserRel>()
|
|
|
+ .eq(ChannelUserRel::getUserId, userId));
|
|
|
+ if (null != channelUserRel) {
|
|
|
+ groupBuyingOrder.setChannelId(channelUserRel.getChannelId());
|
|
|
+ } else {
|
|
|
+ // 没被绑定,看有没有分享者
|
|
|
+ if (null != userShareVO.getSuid() && 0 != userShareVO.getSuid()) {
|
|
|
+ if (UserShareVO.SHARE_TYPE_USER.equals(userShareVO.getType())) {
|
|
|
+ ChannelUserRel channelShareUserRel = channelUserRelService.getOne(
|
|
|
+ new LambdaQueryWrapper<ChannelUserRel>()
|
|
|
+ .eq(ChannelUserRel::getUserId, userShareVO.getSuid()));
|
|
|
+ if (null != channelShareUserRel) {
|
|
|
+ groupBuyingOrder.setChannelId(channelShareUserRel.getChannelId());
|
|
|
+ groupBuyingOrder.setShareInfo(JSONObject.toJSONString(userShareVO));
|
|
|
+ }
|
|
|
+ } else if (UserShareVO.SHARE_TYPE_SITE.equals(userShareVO.getType())) {
|
|
|
+ Channel channel = channelService.getOne(
|
|
|
+ new LambdaQueryWrapper<Channel>()
|
|
|
+ .eq(Channel::getUserId, userShareVO.getSuid()));
|
|
|
+ if (null != channel) {
|
|
|
+ groupBuyingOrder.setChannelId(channel.getChannelId());
|
|
|
+ groupBuyingOrder.setShareInfo(JSONObject.toJSONString(userShareVO));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 野生用户绑定固定的经销商
|
|
|
+ if (groupBuyingOrder.getChannelId() == null || groupBuyingOrder.getChannelId() == 0) {
|
|
|
+ UserShareVO tempUserShareVO = new UserShareVO();
|
|
|
+ tempUserShareVO.setSuid(bindChannelId);
|
|
|
+ tempUserShareVO.setType(UserShareVO.SHARE_TYPE_SITE);
|
|
|
+ groupBuyingOrder.setChannelId(bindChannelId);
|
|
|
+ groupBuyingOrder.setShareInfo(JSONObject.toJSONString(tempUserShareVO));
|
|
|
+ }
|
|
|
+ save(groupBuyingOrder);
|
|
|
+
|
|
|
+
|
|
|
+// if (orderSettleVO.getPayAmt() == 0) {
|
|
|
+// // 无需支付的,直接置为成功
|
|
|
+// processTicketOrder(userTicketOrder);
|
|
|
+// // 插入付款异步任务
|
|
|
+// Assert.isTrue(asyncTaskService.insertAsyncTask(AsyncTaskTypeEnum.TICKET_PAY, userTicketOrder.getOrderId()),
|
|
|
+// "盲票支付,创建异步任务失败:" + userTicketOrder.getOrderId());
|
|
|
+// }
|
|
|
+
|
|
|
+ //锁拼团次数
|
|
|
+ return groupBuyingOrder.getOrderId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean cancelOrder(String orderId) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean paySuccess(PayOrder payOrder) {
|
|
|
+ GroupBuyingOrder buyingOrder = getOne(new LambdaQueryWrapper<GroupBuyingOrder>()
|
|
|
+ .eq(GroupBuyingOrder::getOrderId,payOrder.getBizId())
|
|
|
+ );
|
|
|
+ if (null == buyingOrder || buyingOrder.getStatus() != UserTicketOrderStatusEnum.NOT_PAY.getValue()) {
|
|
|
+ LogUtil.error(logger, "支付回调用户购票订单处理,订单状态异常,不是待支付。payOrder:{0}, ticketOrder:{1}",
|
|
|
+ JSONObject.toJSONString(payOrder), JSONObject.toJSONString(buyingOrder));
|
|
|
+ //回退次数
|
|
|
+ backGroup(buyingOrder);
|
|
|
+ throw new ServiceException("支付回调用户购票订单处理,订单状态异常,不是待支付。orderId:" + payOrder.getBizId());
|
|
|
+
|
|
|
+ }
|
|
|
+ buyingOrder.setPayTime(DateUtils.parseDate(payOrder.getCompleteDate()));
|
|
|
+ buyingOrder.setPayType(payOrder.getPayResource());
|
|
|
+
|
|
|
+ // 插入异步任务
|
|
|
+ Assert.isTrue(asyncTaskService.insertAsyncTask(AsyncTaskTypeEnum.TICKET_PAY, payOrder.getBizId()),
|
|
|
+ "创建支付成功异步任务失败. orderId:" + payOrder.getOrderId());
|
|
|
+ //todo 处理开团发奖逻辑
|
|
|
+ //return processTicketOrder(ticketOrder);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付回调失败,回退锁定次数
|
|
|
+ */
|
|
|
+ public boolean backGroup(GroupBuyingOrder buyingOrder){
|
|
|
+ String key = RedisLockKey.build(RedisLockKey.USER_INITIATE_GROUP_LOCK,buyingOrder.getGroupbuyingId(),buyingOrder.getUserId());
|
|
|
+ String groupUserKey = RedisLockKey.build(RedisLockKey.USER_JOIN_GROUP_LOCK,buyingOrder.getGroupbuyingId(),buyingOrder.getUserId());
|
|
|
+ if (!distributedLocker.tryLock(key, 3, -1, TimeUnit.SECONDS)) {
|
|
|
+ throw new ServiceException("拼团活动太火爆了,请稍后重试!");
|
|
|
+ }
|
|
|
+ if (!distributedLocker.tryLock(groupUserKey, 3, -1, TimeUnit.SECONDS)) {
|
|
|
+ throw new ServiceException("拼团活动太火爆了,请稍后重试!");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //发起拼团,更新用户剩余拼团次数
|
|
|
+ if(buyingOrder.getGroupType() == 1){
|
|
|
+ redisCache.increment(key,1);
|
|
|
+ redisCache.increment(groupUserKey,1);
|
|
|
+ }
|
|
|
+ //参与拼团,更新用户参团次数
|
|
|
+ if(buyingOrder.getGroupType() == 2){
|
|
|
+ redisCache.increment(groupUserKey,1);
|
|
|
+ }
|
|
|
+ }finally {
|
|
|
+ distributedLocker.unlock(key);
|
|
|
+ distributedLocker.unlock(groupUserKey);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean commToChannel(String orderId, Integer resource) {
|
|
|
+ ChannelCommParam channelCommParam = new ChannelCommParam();
|
|
|
+ GroupBuyingOrder buyingOrder = getOne(new LambdaQueryWrapper<GroupBuyingOrder>()
|
|
|
+ .eq(GroupBuyingOrder::getOrderId,orderId)
|
|
|
+ );
|
|
|
+ if (ChannelCommissionResourceEnum.USER.getValue().equals(resource)) {
|
|
|
+
|
|
|
+ BeanUtils.copyProperties(buyingOrder, channelCommParam);
|
|
|
+ Assert.isTrue(channelCommParam.getCommStatus() != CommStatusEnum.YES,
|
|
|
+ "结佣处理,结佣状态异常,不是结佣中或未结佣。orderId=" + orderId);
|
|
|
+
|
|
|
+ // 判断线上票判断渠道商是否打标,打标则不分佣
|
|
|
+ if (TicketTypeEnum.ONLINE.equals(buyingOrder.getResource())) {
|
|
|
+ if (buyingOrder.getChannelId() != null) {
|
|
|
+ Channel channel = channelService.getById(buyingOrder.getChannelId());
|
|
|
+ if (channel.getCommFlag() == 1) {
|
|
|
+ // 不分佣,更新订单状态
|
|
|
+ boolean rst = update(
|
|
|
+ new LambdaUpdateWrapper<GroupBuyingOrder>().set(GroupBuyingOrder::getCommStatus,
|
|
|
+ CommStatusEnum.NO_COMM)
|
|
|
+ .eq(GroupBuyingOrder::getOrderId, orderId)
|
|
|
+ .in(GroupBuyingOrder::getCommStatus, CommStatusEnum.DOING, CommStatusEnum.NO));
|
|
|
+ Assert.isTrue(rst, "定制渠道不结佣,更新订单状态失败。orderId:" + orderId);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (channelCommParam.getPayAmt() == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (null != channelCommParam.getChannelId()) {
|
|
|
+ // 推广员的分佣
|
|
|
+ int promoterCommAmt = 0;
|
|
|
+ // 如果有推广员则先给推广员分佣
|
|
|
+ if (channelCommParam.getPromoterId() != null && channelCommParam.getPromoterId() > 0) {
|
|
|
+ Channel promoter = channelService.getById(channelCommParam.getPromoterId());
|
|
|
+ if (promoter != null) {
|
|
|
+ ChannelCommission siteCommission = saveSiteCommAmt(channelCommParam, buyingOrder, promoter, promoterCommAmt);
|
|
|
+ promoterCommAmt = siteCommission.getCommAmt();
|
|
|
+ // 推广员账户余额增加
|
|
|
+ changeMoney(promoter, siteCommission);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 给门店分佣
|
|
|
+ Channel channel = channelService.getById(channelCommParam.getChannelId());
|
|
|
+ ChannelCommission siteCommission = saveSiteCommAmt(channelCommParam, buyingOrder, channel, promoterCommAmt);
|
|
|
+ // 经销商账户余额增加
|
|
|
+ changeMoney(channel, siteCommission);
|
|
|
+
|
|
|
+ int sumCommAmt = siteCommission.getCommAmt() + promoterCommAmt;
|
|
|
+ // 渠道分润
|
|
|
+ while (channel.getParentId() > 0) {
|
|
|
+ channel = channelService.getById(channel.getParentId());
|
|
|
+ ChannelCommission channelCommission = new ChannelCommission();
|
|
|
+ channelCommission.setChannelId(channel.getChannelId());
|
|
|
+ channelCommission.setOrderId(siteCommission.getOrderId());
|
|
|
+ channelCommission.setResource(resource);
|
|
|
+ channelCommission.setBoxId(siteCommission.getBoxId());
|
|
|
+ channelCommission.setSaleAmt(siteCommission.getSaleAmt());
|
|
|
+ channelCommission.setSaleCommRate(siteCommission.getSaleCommRate());
|
|
|
+ channelCommission.setSaleCommAmt(siteCommission.getSaleCommAmt());
|
|
|
+ channelCommission.setCommRate(channel.getCommRate());
|
|
|
+ channelCommission.setCommAmt(new BigDecimal(channelCommission.getSaleCommAmt()).multiply(
|
|
|
+ channelCommission.getCommRate()).divide(new BigDecimal(100), 0, RoundingMode.DOWN)
|
|
|
+ .intValue() - sumCommAmt);
|
|
|
+ channelCommissionService.save(channelCommission);
|
|
|
+ sumCommAmt += channelCommission.getCommAmt();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ LogUtil.info(logger, "渠道结佣时,订单对应的渠道不存在,无需结佣。orderId:{0}",
|
|
|
+ channelCommParam.getOrderId());
|
|
|
+ }
|
|
|
+ if (ChannelCommissionResourceEnum.USER.getValue().equals(resource)) {
|
|
|
+ boolean rst = update(
|
|
|
+ new LambdaUpdateWrapper<GroupBuyingOrder>().set(GroupBuyingOrder::getCommStatus,
|
|
|
+ CommStatusEnum.YES)
|
|
|
+ .eq(GroupBuyingOrder::getOrderId, orderId)
|
|
|
+ .in(GroupBuyingOrder::getCommStatus, CommStatusEnum.DOING, CommStatusEnum.NO));
|
|
|
+ Assert.isTrue(rst, "渠道结佣完成,更新订单状态失败。orderId:" + orderId);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void changeMoney(Channel channel, ChannelCommission siteCommission) {
|
|
|
+ ChannelMoneyLog moneyLog = new ChannelMoneyLog();
|
|
|
+ moneyLog.setChannelId(channel.getChannelId());
|
|
|
+ moneyLog.setType(ChannelMoneyEnum.COMMISSION);
|
|
|
+ moneyLog.setLogMoney(siteCommission.getCommAmt());
|
|
|
+ moneyLog.setBizTime(new Date());
|
|
|
+ moneyLog.setRefId(String.valueOf(siteCommission.getId()));
|
|
|
+ channelMoneyLogService.changeMoney(moneyLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ChannelCommission saveSiteCommAmt(ChannelCommParam channelCommParam, TicketBox ticketBox,
|
|
|
+ Channel channel, int sumCommAmt) {
|
|
|
+ ChannelCommission siteCommission = new ChannelCommission();
|
|
|
+ siteCommission.setChannelId(channel.getChannelId());
|
|
|
+ siteCommission.setOrderId(channelCommParam.getOrderId());
|
|
|
+ siteCommission.setBoxId(channelCommParam.getBoxId());
|
|
|
+ siteCommission.setSaleAmt(channelCommParam.getPayAmt());
|
|
|
+ siteCommission.setSaleCommRate(ticketBox.getSaleCommRate());
|
|
|
+ siteCommission.setSaleCommAmt(new BigDecimal(siteCommission.getSaleAmt()).multiply(
|
|
|
+ siteCommission.getSaleCommRate()).divide(new BigDecimal(100), 0, RoundingMode.DOWN)
|
|
|
+ .intValue());
|
|
|
+ siteCommission.setCommRate(channel.getCommRate());
|
|
|
+ siteCommission.setCommAmt(new BigDecimal(siteCommission.getSaleCommAmt()).multiply(
|
|
|
+ siteCommission.getCommRate()).divide(new BigDecimal(100), 0, RoundingMode.DOWN).intValue() - sumCommAmt);
|
|
|
+ channelCommissionService.save(siteCommission);
|
|
|
+ return siteCommission;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ChannelCommission saveSiteCommAmt(ChannelCommParam channelCommParam, GroupBuyingOrder groupBuyingOrder,
|
|
|
+ Channel channel, int sumCommAmt) {
|
|
|
+ ChannelCommission siteCommission = new ChannelCommission();
|
|
|
+ siteCommission.setChannelId(channel.getChannelId());
|
|
|
+ siteCommission.setOrderId(channelCommParam.getOrderId());
|
|
|
+ siteCommission.setBoxId(channelCommParam.getBoxId());
|
|
|
+ siteCommission.setSaleAmt(channelCommParam.getPayAmt());
|
|
|
+ siteCommission.setSaleCommRate(groupBuyingOrder.getSaleCommRate());
|
|
|
+ siteCommission.setSaleCommAmt(new BigDecimal(siteCommission.getSaleAmt()).multiply(
|
|
|
+ siteCommission.getSaleCommRate()).divide(new BigDecimal(100), 0, RoundingMode.DOWN)
|
|
|
+ .intValue());
|
|
|
+ siteCommission.setCommRate(channel.getCommRate());
|
|
|
+ siteCommission.setCommAmt(new BigDecimal(siteCommission.getSaleCommAmt()).multiply(
|
|
|
+ siteCommission.getCommRate()).divide(new BigDecimal(100), 0, RoundingMode.DOWN).intValue() - sumCommAmt);
|
|
|
+ channelCommissionService.save(siteCommission);
|
|
|
+ return siteCommission;
|
|
|
+ }
|
|
|
+}
|