| | |
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import com.yeshi.fanli.dao.mybatis.AccountDetailsMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper;
|
| | | import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper;
|
| | |
| | | import com.yeshi.fanli.dao.mybatis.order.HongBaoOrderMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoOrderMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanDrawBackMapper;
|
| | | import com.yeshi.fanli.entity.bus.user.AccountDetails;
|
| | | import com.yeshi.fanli.entity.bus.user.HongBaoV2;
|
| | | import com.yeshi.fanli.entity.bus.user.UserInfo;
|
| | | import com.yeshi.fanli.entity.money.UserMoneyDebt;
|
| | | import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum;
|
| | | import com.yeshi.fanli.entity.money.UserMoneyDetail;
|
| | | import com.yeshi.fanli.entity.order.CommonOrder;
|
| | | import com.yeshi.fanli.entity.order.HongBaoOrder;
|
| | |
| | | import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack;
|
| | | import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
|
| | | import com.yeshi.fanli.exception.TaoBaoWeiQuanException;
|
| | | import com.yeshi.fanli.exception.money.UserMoneyDebtException;
|
| | | import com.yeshi.fanli.exception.money.UserMoneyDetailException;
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
|
| | | import com.yeshi.fanli.service.inter.msg.UserMoneyMsgNotificationService;
|
| | | import com.yeshi.fanli.service.inter.taobao.TaoBaoWeiQuanDrawBackService;
|
| | | import com.yeshi.fanli.service.inter.taobao.TaoBaoWeiQuanOrderService;
|
| | | import com.yeshi.fanli.service.inter.user.UserNotificationService;
|
| | | import com.yeshi.fanli.service.inter.user.UserMoneyService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.factory.AccountDetailsFactory;
|
| | | import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
|
| | | import com.yeshi.fanli.vo.order.CommonOrderVO;
|
| | |
|
| | |
| | | private UserMoneyDetailMapper userMoneyDetailMapper;
|
| | |
|
| | | @Resource
|
| | | private AccountDetailsMapper accountDetailsMapper;
|
| | |
|
| | | @Resource
|
| | | private UserNotificationService userNotificationService;
|
| | |
|
| | | @Resource
|
| | | private CommonOrderMapper commonOrderMapper;
|
| | |
|
| | | @Resource
|
| | |
| | | @Resource
|
| | | private HongBaoOrderMapper hongBaoOrderMapper;
|
| | |
|
| | | @Resource
|
| | | private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
|
| | |
|
| | | @Resource
|
| | | private UserMoneyDebtService userMoneyDebtService;
|
| | |
|
| | | @Resource
|
| | | private UserMoneyService userMoneyService;
|
| | |
|
| | | @Override
|
| | | public void addWeiQuanDrawBack(TaoBaoWeiQuanDrawBack taoBaoWeiQuanDrawBack) {
|
| | | taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(taoBaoWeiQuanDrawBack.getOrderItemId(),
|
| | | taoBaoWeiQuanDrawBack.getUser().getId());
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | private void addDebt(Long uid, Long hbId, BigDecimal money) throws UserMoneyDebtException {
|
| | | UserMoneyDebt debt = new UserMoneyDebt();
|
| | | debt.setBeiZhu(null);
|
| | | debt.setCreateTime(new Date());
|
| | | debt.setLeftMoney(money);
|
| | | debt.setOriginMoney(money);
|
| | | debt.setSourceId(hbId);
|
| | | debt.setType(UserMoneyDebtTypeEnum.order);
|
| | | debt.setUid(uid);
|
| | | userMoneyDebtService.addUserMoneyDebt(debt);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 计算退款金额
|
| | | * |
| | | * @param settlement
|
| | | * @param wqMoney
|
| | | * @param hongBaoMoney
|
| | | * @return
|
| | | */
|
| | | private BigDecimal computeDrawBackMoney(BigDecimal settlement, BigDecimal wqMoney, BigDecimal hongBaoMoney) {
|
| | | BigDecimal money = (hongBaoMoney.multiply(wqMoney)).divide(settlement, 2, BigDecimal.ROUND_UP);
|
| | | if (money.compareTo(hongBaoMoney) > 0)
|
| | | money = hongBaoMoney;
|
| | | return money;
|
| | | }
|
| | |
|
| | | @Transactional
|
| | |
| | | List<TaoBaoWeiQuanOrder> list = taoBaoWeiQuanOrderService.getWeiQuanSuccessOrders(orderId);
|
| | | if (list == null || list.size() == 0)
|
| | | return;
|
| | | // 查询订单库
|
| | | List<TaoBaoOrder> orderList = taoBaoOrderMapper.selectTaoBaoOrderByOrderId(orderId);
|
| | | // 计算淘宝联盟返给平台的资金
|
| | | BigDecimal sumFee = new BigDecimal("0");
|
| | | for (TaoBaoOrder order : orderList)
|
| | | if (order.getOrderState().equalsIgnoreCase("订单结算"))
|
| | | sumFee = sumFee.add(order.geteIncome());
|
| | | // 获取和该订单号有关联的用户
|
| | | // 获取主红包(同一个订单号的单只会对应同一个用户)
|
| | | List<CommonOrderVO> typeList = new ArrayList<>();
|
| | |
| | | commonOrderMapper.updateByPrimaryKeySelective(co);
|
| | |
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(vo.getId());
|
| | | mainHongBaoList.add(hongBaoOrder.getHongBaoV2());
|
| | | if (hongBaoOrder.getCommonOrder() != null
|
| | | && !StringUtil.isNullOrEmpty(hongBaoOrder.getCommonOrder().getTradeId()))
|
| | | // 查询是否已经维权
|
| | | {
|
| | | TaoBaoWeiQuanDrawBack drawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(
|
| | | hongBaoOrder.getCommonOrder().getTradeId(),
|
| | | hongBaoOrder.getHongBaoV2().getUserInfo().getId());
|
| | | if (drawBack == null)// 添加还未扣款的子订单
|
| | | mainHongBaoList.add(hongBaoOrder.getHongBaoV2());
|
| | | }
|
| | | }
|
| | | if (mainHongBaoList == null || mainHongBaoList.size() == 0)
|
| | | return;
|
| | |
| | |
|
| | | if (mainHongBaoList != null)
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {
|
| | | if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | if (hongBao.getState() != HongBaoV2.STATE_YILINGQU)
|
| | | continue;
|
| | | // 累计主红包的金额
|
| | | if (fanMoneyMap.get(hongBao.getUserInfo().getId()) == null) {
|
| | |
| | | while (its.hasNext()) {
|
| | | Long uid = its.next();
|
| | | BigDecimal userGetMoney = fanMoneyMap.get(uid);
|
| | | BigDecimal fanMoney = new BigDecimal("0");
|
| | | BigDecimal wqMoney = new BigDecimal("0");
|
| | | // 统计用户在这个单中需要扣除的资金
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = null;
|
| | | List<TaoBaoWeiQuanDrawBack> newWeiQuanDrawBackList = new ArrayList<>();
|
| | | BigDecimal settleMent = new BigDecimal(0);
|
| | | for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
|
| | | weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(),
|
| | | uid);
|
| | | if (weiQuanDrawBack != null)
|
| | | continue;
|
| | | // 计算结算金额
|
| | | TaoBaoOrder taoBaoOrder = taoBaoOrderMapper.selectTaoBaoOrderByTradeId(weiQuanOrder.getOrderItemId());
|
| | | if (taoBaoOrder == null)
|
| | | throw new TaoBaoWeiQuanException(101, "未找到子订单相关数据");
|
| | |
|
| | | if (taoBaoOrder.getSettlement() == null)
|
| | | throw new TaoBaoWeiQuanException(102, "淘宝结算金额为空");
|
| | |
|
| | | settleMent = settleMent.add(taoBaoOrder.getSettlement());
|
| | |
|
| | | // 退款的资金
|
| | | fanMoney = fanMoney.add(weiQuanOrder.getFanMoney());
|
| | | // 插入记录
|
| | | wqMoney = wqMoney.add(weiQuanOrder.getMoney());
|
| | | weiQuanDrawBack = new TaoBaoWeiQuanDrawBack();
|
| | | weiQuanDrawBack.setCreateTime(new Date());
|
| | | // 暂时设为0
|
| | | weiQuanDrawBack.setDrawBackMoney(new BigDecimal(0));
|
| | |
|
| | | CommonOrder commonOrder = commonOrderMapper.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
|
| | | weiQuanOrder.getOrderItemId());
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
|
| | | // 计算退款的资金
|
| | | BigDecimal drawBack = null;
|
| | | if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null)
|
| | | drawBack = computeDrawBackMoney(commonOrder.getSettlement(), weiQuanOrder.getMoney(),
|
| | | hongBaoOrder.getHongBaoV2().getMoney());
|
| | |
|
| | | weiQuanDrawBack.setDrawBackMoney(drawBack);
|
| | | weiQuanDrawBack.setOrderId(weiQuanOrder.getOrderId());
|
| | | weiQuanDrawBack.setOrderItemId(weiQuanOrder.getOrderItemId());
|
| | | weiQuanDrawBack.setUser(new UserInfo(uid));
|
| | | taoBaoWeiQuanDrawBackMapper.insertSelective(weiQuanDrawBack);
|
| | | newWeiQuanDrawBackList.add(weiQuanDrawBack);
|
| | | }
|
| | |
|
| | | if (fanMoney.compareTo(new BigDecimal("0")) <= 0)
|
| | | if (wqMoney.compareTo(new BigDecimal("0")) <= 0)
|
| | | continue;
|
| | |
|
| | | // 插入记录
|
| | | for (TaoBaoWeiQuanDrawBack drawBack : newWeiQuanDrawBackList) {
|
| | | taoBaoWeiQuanDrawBackMapper.insertSelective(drawBack);
|
| | | }
|
| | |
|
| | | // 退款金额
|
| | | BigDecimal drawBackMoney = MoneyBigDecimalUtil.div(userGetMoney.multiply(fanMoney), sumFee);
|
| | | userInfoMapper.subHongBaoByUid(uid, drawBackMoney);
|
| | | // 添加资金记录
|
| | | AccountDetails accountDetails = AccountDetailsFactory.create("-" + drawBackMoney,
|
| | | AccountDetailsFactory.SHARE_GOODS_DRAWBACK, null, null, new UserInfo(uid));
|
| | | accountDetailsMapper.insertSelective(accountDetails);
|
| | | BigDecimal drawBackMoney = computeDrawBackMoney(settleMent, wqMoney, userGetMoney);
|
| | |
|
| | | // 计算资金是否充足
|
| | | UserInfo user = userInfoMapper.selectByPKey(uid);
|
| | | if (user != null && user.getMyHongBao().compareTo(drawBackMoney) < 0)// 资金不足
|
| | | {
|
| | | // 添加债务
|
| | | for (TaoBaoWeiQuanDrawBack drawBack : newWeiQuanDrawBackList) {
|
| | | TaoBaoOrder taoBaoOrder = taoBaoOrderMapper.selectTaoBaoOrderByTradeId(drawBack.getOrderItemId());
|
| | | if (taoBaoOrder != null) {
|
| | | TaoBaoWeiQuanOrder weiQuanOrder = taoBaoWeiQuanOrderService
|
| | | .selectByTradeId(drawBack.getOrderItemId());
|
| | | CommonOrder co = commonOrderMapper.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
|
| | | drawBack.getOrderItemId());
|
| | | if (co != null) {
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(co.getId());
|
| | | if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null) {
|
| | | BigDecimal drawback = computeDrawBackMoney(taoBaoOrder.getSettlement(),
|
| | | weiQuanOrder.getMoney(), hongBaoOrder.getHongBaoV2().getMoney());
|
| | | try {
|
| | | addDebt(uid, hongBaoOrder.getHongBaoV2().getId(), drawback);
|
| | | } catch (UserMoneyDebtException e) {
|
| | | throw new TaoBaoWeiQuanException(101, "资金借贷异常");
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | continue;
|
| | | }
|
| | |
|
| | | UserMoneyDetail userMoneyDetail = null;
|
| | | // 新版资金记录
|
| | | try {
|
| | | UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createFanLiWeiQuan(uid, weiQuanDrawBack,
|
| | | drawBackMoney);
|
| | | userMoneyDetail.setId(accountDetails.getId());
|
| | | userMoneyDetailMapper.insert(userMoneyDetail);
|
| | | userMoneyDetail = UserMoneyDetailFactory.createFanLiWeiQuan(uid, weiQuanDrawBack, drawBackMoney);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | |
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | | userMoneyService.subUserMoney(uid, drawBackMoney, userMoneyDetail);
|
| | |
|
| | | userNotificationService.weiQuanFanli(uid, orderId, drawBackMoney);
|
| | | // 新版通知
|
| | | userMoneyMsgNotificationService.fanliOrderWeiQuan(uid, orderId, Constant.SOURCE_TYPE_TAOBAO, drawBackMoney,
|
| | | userInfoMapper.selectByPKey(uid).getMyHongBao());
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | List<TaoBaoWeiQuanOrder> list = taoBaoWeiQuanOrderService.getWeiQuanSuccessOrders(orderId);
|
| | | if (list == null || list.size() == 0)
|
| | | return;
|
| | | // 查询订单库
|
| | | List<TaoBaoOrder> orderList = taoBaoOrderMapper.selectTaoBaoOrderByOrderId(orderId);
|
| | | // 计算淘宝联盟返给平台的资金
|
| | | BigDecimal sumFee = new BigDecimal("0");
|
| | | for (TaoBaoOrder order : orderList)
|
| | | if (order.getOrderState().equalsIgnoreCase("订单结算"))
|
| | | sumFee = sumFee.add(order.geteIncome());
|
| | | // 获取和该订单号有关联的用户
|
| | | // 获取主红包(同一个订单号的单只会对应同一个用户)
|
| | |
|
| | | List<CommonOrderVO> typeList = new ArrayList<>();
|
| | | CommonOrderVO cv = new CommonOrderVO();
|
| | | cv.setSourceType(Constant.SOURCE_TYPE_TAOBAO);
|
| | |
| | | co.setState(CommonOrder.STATE_WQ);
|
| | | co.setUpdateTime(new Date());
|
| | | commonOrderMapper.updateByPrimaryKeySelective(co);
|
| | |
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(vo.getId());
|
| | | mainHongBaoList.add(hongBaoOrder.getHongBaoV2());
|
| | | if (hongBaoOrder.getCommonOrder() != null
|
| | | && !StringUtil.isNullOrEmpty(hongBaoOrder.getCommonOrder().getTradeId()))
|
| | | // 查询是否已经维权
|
| | | {
|
| | | TaoBaoWeiQuanDrawBack drawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(
|
| | | hongBaoOrder.getCommonOrder().getTradeId(),
|
| | | hongBaoOrder.getHongBaoV2().getUserInfo().getId());
|
| | | if (drawBack == null)// 添加还未扣款的子订单
|
| | | mainHongBaoList.add(hongBaoOrder.getHongBaoV2());
|
| | | }
|
| | | }
|
| | |
|
| | | if (mainHongBaoList == null || mainHongBaoList.size() == 0)
|
| | |
| | | Map<Long, BigDecimal> fanMoneyMap = new HashMap<>();
|
| | |
|
| | | if (mainHongBaoList != null)
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {// 统计订单下的所有主红包
|
| | | hongBao = hongBaoV2Mapper.selectByPrimaryKey(hongBao.getId());
|
| | | if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | if (hongBao.getState() != HongBaoV2.STATE_YILINGQU)
|
| | | continue;
|
| | |
|
| | | // 返利红包不计入
|
| | | if (hongBao.getType() != HongBaoV2.TYPE_ZIGOU) {
|
| | | // 只计入分享赚的红包
|
| | | if (hongBao.getType() == HongBaoV2.TYPE_SHARE_GOODS) {
|
| | | // 累计主红包的金额
|
| | | if (fanMoneyMap.get(hongBao.getUserInfo().getId()) == null) {
|
| | | fanMoneyMap.put(hongBao.getUserInfo().getId(), hongBao.getMoney());
|
| | |
| | | fanMoneyMap.put(hongBao.getUserInfo().getId(),
|
| | | fanMoneyMap.get(hongBao.getUserInfo().getId()).add(hongBao.getMoney()));
|
| | | }
|
| | | // 累计子红包的金额
|
| | | List<HongBaoV2> childHongBaoList = hongBaoV2Mapper.listChildrenById(hongBao.getId());
|
| | | if (childHongBaoList != null)
|
| | | for (HongBaoV2 child : childHongBaoList) {
|
| | | if (child.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | continue;
|
| | | if (fanMoneyMap.get(child.getUserInfo().getId()) == null) {
|
| | | fanMoneyMap.put(child.getUserInfo().getId(), child.getMoney());
|
| | | } else
|
| | | fanMoneyMap.put(child.getUserInfo().getId(),
|
| | | fanMoneyMap.get(child.getUserInfo().getId()).add(child.getMoney()));
|
| | | }
|
| | | }
|
| | |
|
| | | // 计算每个用户的返利比例
|
| | | Iterator<Long> its = fanMoneyMap.keySet().iterator();
|
| | | // 查询和这个订单有关的用户(主订单)
|
| | | while (its.hasNext()) {
|
| | | Long uid = its.next();
|
| | | BigDecimal userGetMoney = fanMoneyMap.get(uid);
|
| | | BigDecimal fanMoney = new BigDecimal("0");
|
| | | BigDecimal wqMoney = new BigDecimal("0");
|
| | | BigDecimal settleMent = new BigDecimal("0");
|
| | | // 统计用户在这个单中需要扣除的资金
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = null;
|
| | |
|
| | | List<TaoBaoWeiQuanDrawBack> newWeiQuanDrawBackList = new ArrayList<>();
|
| | | for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
|
| | | weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(),
|
| | | uid);
|
| | | if (weiQuanDrawBack != null)
|
| | | continue;
|
| | | // 计算结算金额
|
| | | TaoBaoOrder taoBaoOrder = taoBaoOrderMapper.selectTaoBaoOrderByTradeId(weiQuanOrder.getOrderItemId());
|
| | | if (taoBaoOrder == null)
|
| | | throw new TaoBaoWeiQuanException(101, "未找到子订单相关数据");
|
| | |
|
| | | if (taoBaoOrder.getSettlement() == null)
|
| | | throw new TaoBaoWeiQuanException(102, "淘宝结算金额为空");
|
| | |
|
| | | settleMent = settleMent.add(taoBaoOrder.getSettlement());
|
| | |
|
| | | // 退款的资金
|
| | | fanMoney = fanMoney.add(weiQuanOrder.getFanMoney());
|
| | | wqMoney = wqMoney.add(weiQuanOrder.getMoney());
|
| | | // 插入记录
|
| | | weiQuanDrawBack = new TaoBaoWeiQuanDrawBack();
|
| | | weiQuanDrawBack.setCreateTime(new Date());
|
| | | // TODO 暂时设为0
|
| | | weiQuanDrawBack.setDrawBackMoney(new BigDecimal(0));
|
| | | // 计算退款资金
|
| | | CommonOrder commonOrder = commonOrderMapper.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
|
| | | weiQuanOrder.getOrderItemId());
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
|
| | | // 计算退款的资金
|
| | | BigDecimal drawBack = null;
|
| | | if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null)
|
| | | drawBack = computeDrawBackMoney(commonOrder.getSettlement(), weiQuanOrder.getMoney(),
|
| | | hongBaoOrder.getHongBaoV2().getMoney());
|
| | |
|
| | | weiQuanDrawBack.setDrawBackMoney(drawBack);
|
| | |
|
| | | weiQuanDrawBack.setOrderId(weiQuanOrder.getOrderId());
|
| | | weiQuanDrawBack.setOrderItemId(weiQuanOrder.getOrderItemId());
|
| | | weiQuanDrawBack.setUser(new UserInfo(uid));
|
| | | taoBaoWeiQuanDrawBackMapper.insertSelective(weiQuanDrawBack);
|
| | | newWeiQuanDrawBackList.add(weiQuanDrawBack);
|
| | | }
|
| | |
|
| | | if (fanMoney.compareTo(new BigDecimal("0")) <= 0)
|
| | | if (wqMoney.compareTo(new BigDecimal("0")) <= 0)
|
| | | continue;
|
| | |
|
| | | // 退款金额
|
| | | BigDecimal drawBackMoney = MoneyBigDecimalUtil.div(userGetMoney.multiply(fanMoney), sumFee);
|
| | | userInfoMapper.subHongBaoByUid(uid, drawBackMoney);
|
| | | // 添加资金记录
|
| | | AccountDetails accountDetails = AccountDetailsFactory.create("-" + drawBackMoney,
|
| | | AccountDetailsFactory.SHARE_GOODS_DRAWBACK, null, null, new UserInfo(uid));
|
| | | accountDetailsMapper.insertSelective(accountDetails);
|
| | | BigDecimal drawBackMoney = computeDrawBackMoney(settleMent, wqMoney, userGetMoney);
|
| | | if (drawBackMoney.compareTo(userGetMoney) > 0)
|
| | | drawBackMoney = userGetMoney;
|
| | |
|
| | | for (TaoBaoWeiQuanDrawBack drawBack : newWeiQuanDrawBackList)
|
| | | taoBaoWeiQuanDrawBackMapper.insertSelective(drawBack);
|
| | | // 计算资金是否充足
|
| | | UserInfo user = userInfoMapper.selectByPKey(uid);
|
| | | if (user != null && user.getMyHongBao().compareTo(drawBackMoney) < 0)// 资金不足
|
| | | {
|
| | | // 添加债务
|
| | | for (TaoBaoWeiQuanDrawBack drawBack : newWeiQuanDrawBackList) {
|
| | | TaoBaoOrder taoBaoOrder = taoBaoOrderMapper.selectTaoBaoOrderByTradeId(drawBack.getOrderItemId());
|
| | | if (taoBaoOrder != null) {
|
| | | TaoBaoWeiQuanOrder weiQuanOrder = taoBaoWeiQuanOrderService
|
| | | .selectByTradeId(drawBack.getOrderItemId());
|
| | | BigDecimal settleMentMoney = taoBaoOrder.getSettlement();
|
| | | CommonOrder co = commonOrderMapper.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
|
| | | weiQuanOrder.getOrderItemId());
|
| | | if (co != null) {
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(co.getId());
|
| | | BigDecimal drawback = computeDrawBackMoney(settleMentMoney, weiQuanOrder.getMoney(),
|
| | | hongBaoOrder.getHongBaoV2().getMoney());
|
| | | try {
|
| | | addDebt(uid, hongBaoOrder.getHongBaoV2().getId(), drawback);
|
| | | } catch (UserMoneyDebtException e) {
|
| | | throw new TaoBaoWeiQuanException(101, "资金借贷异常");
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | continue;
|
| | | }
|
| | |
|
| | | UserMoneyDetail userMoneyDetail = null;
|
| | | // 新版资金记录
|
| | | try {
|
| | | UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShareWeiQuan(uid, weiQuanDrawBack,
|
| | | drawBackMoney);
|
| | | userMoneyDetail.setId(accountDetails.getId());
|
| | | userMoneyDetailMapper.insert(userMoneyDetail);
|
| | | userMoneyDetail = UserMoneyDetailFactory.createShareWeiQuan(uid, weiQuanDrawBack, drawBackMoney);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | |
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | | userMoneyService.subUserMoney(uid, drawBackMoney, userMoneyDetail);
|
| | | userMoneyMsgNotificationService.shareOrderWeiQuan(uid, orderId, Constant.SOURCE_TYPE_TAOBAO, drawBackMoney,
|
| | |
|
| | | userNotificationService.weiQuanTiCheng(uid, orderId, drawBackMoney);
|
| | | userInfoMapper.selectByPrimaryKey(uid).getMyHongBao());
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | @Transactional
|
| | |
| | | if (list == null || list.size() == 0)
|
| | | return;
|
| | | // 查询订单库
|
| | | List<TaoBaoOrder> orderList = taoBaoOrderMapper.selectTaoBaoOrderByOrderId(orderId);
|
| | | // 计算淘宝联盟返给平台的资金
|
| | | BigDecimal sumFee = new BigDecimal("0");
|
| | | for (TaoBaoOrder order : orderList)
|
| | | if (order.getOrderState().equalsIgnoreCase("订单结算"))
|
| | | sumFee = sumFee.add(order.geteIncome());
|
| | | // 获取和该订单号有关联的用户
|
| | | // 获取主红包(同一个订单号的单只会对应同一个用户)
|
| | | List<CommonOrderVO> typeList = new ArrayList<>();
|
| | | CommonOrderVO cv = new CommonOrderVO();
|
| | | cv.setSourceType(Constant.SOURCE_TYPE_TAOBAO);
|
| | | cv.setOrderNo(orderId);
|
| | | typeList.add(cv);
|
| | |
|
| | | List<CommonOrderVO> commonOrderList = commonOrderMapper.listOrderGoodsInfo(typeList);
|
| | | List<HongBaoV2> mainHongBaoList = new ArrayList<>();
|
| | | if (commonOrderList != null)
|
| | | for (CommonOrderVO vo : commonOrderList) {
|
| | | CommonOrder co = commonOrderMapper.selectByPrimaryKey(vo.getId());
|
| | | if (co.getState() != CommonOrder.STATE_WQ) {
|
| | | co = new CommonOrder(vo.getId());
|
| | | co.setState(CommonOrder.STATE_WQ);
|
| | | co.setUpdateTime(new Date());
|
| | | commonOrderMapper.updateByPrimaryKeySelective(co);
|
| | | }
|
| | |
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(vo.getId());
|
| | | mainHongBaoList.add(hongBaoOrder.getHongBaoV2());
|
| | | for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
|
| | | TaoBaoOrder taoBaoOrder = taoBaoOrderMapper.selectTaoBaoOrderByTradeId(weiQuanOrder.getOrderItemId());
|
| | | BigDecimal wqMoney = weiQuanOrder.getMoney();
|
| | | if (taoBaoOrder == null)
|
| | | throw new TaoBaoWeiQuanException(1, "来源交易ID有误");
|
| | | BigDecimal settleMent = new BigDecimal("0");
|
| | | if (taoBaoOrder.getOrderState().equalsIgnoreCase("订单结算")) {
|
| | | settleMent = settleMent.add(taoBaoOrder.getSettlement());
|
| | | }
|
| | | if (mainHongBaoList == null || mainHongBaoList.size() == 0)
|
| | | return;
|
| | |
|
| | | // 主用户ID
|
| | | Map<Long, BigDecimal> fanMoneyMap = new HashMap<>();
|
| | | // 查询下级子用户
|
| | | CommonOrder commonOrder = commonOrderMapper.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
|
| | | weiQuanOrder.getOrderItemId());
|
| | | if (commonOrder == null)
|
| | | throw new TaoBaoWeiQuanException(2, "订单有误");
|
| | |
|
| | | if (mainHongBaoList != null)
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {
|
| | | HongBaoV2 hb = hongBaoV2Mapper.selectByPrimaryKey(hongBao.getId());
|
| | | hongBao = hb;
|
| | | HongBaoOrder hongbaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
|
| | |
|
| | | if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | continue;
|
| | | // 返利红包不计入
|
| | | if (hongBao.getType() != HongBaoV2.TYPE_ZIGOU) {
|
| | | // 累计主红包的金额
|
| | | if (fanMoneyMap.get(hongBao.getUserInfo().getId()) == null) {
|
| | | fanMoneyMap.put(hongBao.getUserInfo().getId(), hongBao.getMoney());
|
| | | } else
|
| | | fanMoneyMap.put(hongBao.getUserInfo().getId(),
|
| | | fanMoneyMap.get(hongBao.getUserInfo().getId()).add(hongBao.getMoney()));
|
| | | }
|
| | | // 累计子红包的金额
|
| | | List<HongBaoV2> childHongBaoList = hongBaoV2Mapper.listChildrenById(hongBao.getId());
|
| | | if (childHongBaoList != null)
|
| | | for (HongBaoV2 child : childHongBaoList) {
|
| | | if (child.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | if (hongbaoOrder == null || hongbaoOrder.getHongBaoV2() == null)
|
| | | throw new TaoBaoWeiQuanException(3, "红包有误");
|
| | |
|
| | | List<HongBaoV2> childList = hongBaoV2Mapper.listChildrenById(hongbaoOrder.getHongBaoV2().getId());
|
| | |
|
| | | if (childList != null)
|
| | | for (HongBaoV2 child : childList) {
|
| | | Long uid = child.getUserInfo().getId();
|
| | | if (child.getState() != HongBaoV2.STATE_YILINGQU)
|
| | | continue;
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper
|
| | | .selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(), child.getUserInfo().getId());
|
| | | if (weiQuanDrawBack != null)
|
| | | continue;
|
| | | BigDecimal drawBackMoney = computeDrawBackMoney(settleMent, wqMoney, child.getMoney());
|
| | | weiQuanDrawBack = new TaoBaoWeiQuanDrawBack();
|
| | | weiQuanDrawBack.setCreateTime(new Date());
|
| | | weiQuanDrawBack.setDrawBackMoney(drawBackMoney);
|
| | | weiQuanDrawBack.setOrderId(weiQuanOrder.getOrderId());
|
| | | weiQuanDrawBack.setOrderItemId(weiQuanOrder.getOrderItemId());
|
| | | weiQuanDrawBack.setUser(child.getUserInfo());
|
| | | taoBaoWeiQuanDrawBackMapper.insertSelective(weiQuanDrawBack);// 加入返还记录
|
| | |
|
| | | // 如果资金大于0才扣除
|
| | |
|
| | | if (drawBackMoney != null && drawBackMoney.compareTo(new BigDecimal(0)) > 0) {
|
| | |
|
| | | // 判断资金是否足够扣款
|
| | | UserInfo user = userInfoMapper.selectByPKey(uid);
|
| | | if (user != null && user.getMyHongBao().compareTo(drawBackMoney) < 0)// 资金不足
|
| | | {
|
| | | try {
|
| | | addDebt(uid, child.getId(), drawBackMoney);
|
| | | } catch (UserMoneyDebtException e) {
|
| | | throw new TaoBaoWeiQuanException(101, "资金借贷异常");
|
| | | }
|
| | | continue;
|
| | | if (fanMoneyMap.get(child.getUserInfo().getId()) == null) {
|
| | | fanMoneyMap.put(child.getUserInfo().getId(), child.getMoney());
|
| | | } else
|
| | | fanMoneyMap.put(child.getUserInfo().getId(),
|
| | | fanMoneyMap.get(child.getUserInfo().getId()).add(child.getMoney()));
|
| | | }
|
| | |
|
| | | UserMoneyDetail userMoneyDetail = null;
|
| | | // 新版资金记录
|
| | | try {
|
| | | userMoneyDetail = UserMoneyDetailFactory.createInviteWeiQuan(uid, weiQuanDrawBack,
|
| | | drawBackMoney);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | | } catch (Exception e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | userMoneyService.subUserMoney(uid, drawBackMoney, userMoneyDetail);
|
| | |
|
| | | // 新版通知
|
| | | userMoneyMsgNotificationService.inviteOrderWeiQuan(uid, orderId, Constant.SOURCE_TYPE_TAOBAO,
|
| | | drawBackMoney, userInfoMapper.selectByPrimaryKey(uid).getMyHongBao());
|
| | | }
|
| | | }
|
| | |
|
| | | // 计算每个用户的返利比例
|
| | | Iterator<Long> its = fanMoneyMap.keySet().iterator();
|
| | | while (its.hasNext()) {
|
| | | Long uid = its.next();
|
| | | BigDecimal userGetMoney = fanMoneyMap.get(uid);
|
| | | BigDecimal fanMoney = new BigDecimal("0");
|
| | | // 统计用户在这个单中需要扣除的资金
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = null;
|
| | | for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
|
| | | weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(),
|
| | | uid);
|
| | | if (weiQuanDrawBack != null)
|
| | | continue;
|
| | | // 退款的资金
|
| | | fanMoney = fanMoney.add(weiQuanOrder.getFanMoney());
|
| | | // 插入记录
|
| | | weiQuanDrawBack = new TaoBaoWeiQuanDrawBack();
|
| | | weiQuanDrawBack.setCreateTime(new Date());
|
| | | // TODO 暂时设为0
|
| | | weiQuanDrawBack.setDrawBackMoney(new BigDecimal(0));
|
| | | weiQuanDrawBack.setOrderId(weiQuanOrder.getOrderId());
|
| | | weiQuanDrawBack.setOrderItemId(weiQuanOrder.getOrderItemId());
|
| | | weiQuanDrawBack.setUser(new UserInfo(uid));
|
| | | taoBaoWeiQuanDrawBackMapper.insertSelective(weiQuanDrawBack);
|
| | | }
|
| | |
|
| | | if (fanMoney.compareTo(new BigDecimal("0")) <= 0)
|
| | | continue;
|
| | |
|
| | | // 退款金额
|
| | | BigDecimal drawBackMoney = MoneyBigDecimalUtil.div(userGetMoney.multiply(fanMoney), sumFee);
|
| | | userInfoMapper.subHongBaoByUid(uid, drawBackMoney);
|
| | | // 添加资金记录
|
| | | AccountDetails accountDetails = AccountDetailsFactory.create("-" + drawBackMoney,
|
| | | AccountDetailsFactory.SHARE_GOODS_DRAWBACK, null, null, new UserInfo(uid));
|
| | | accountDetailsMapper.insertSelective(accountDetails);
|
| | |
|
| | | // 新版资金记录
|
| | | try {
|
| | | UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInviteWeiQuan(uid, weiQuanDrawBack,
|
| | | drawBackMoney);
|
| | | userMoneyDetail.setId(accountDetails.getId());
|
| | | userMoneyDetailMapper.insert(userMoneyDetail);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | | } catch (Exception e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | //
|
| | |
|
| | | userNotificationService.weiQuanTiCheng(uid, orderId, drawBackMoney);
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public TaoBaoWeiQuanDrawBack selectByOrderItemId(String orderItemId) {
|
| | | List<TaoBaoWeiQuanDrawBack> list = taoBaoWeiQuanDrawBackMapper.selectByOrderItemId(orderItemId);
|
| | | if (list != null && list.size() > 0)
|
| | | return list.get(0);
|
| | | else
|
| | | return null;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public TaoBaoWeiQuanDrawBack selectByHongBaoId(Long hbId) {
|
| | | Long uid = null;
|
| | | HongBaoV2 hb = hongBaoV2Mapper.selectByPrimaryKey(hbId);
|
| | | uid = hb.getUserInfo().getId();
|
| | | if (hb.getParent() != null)
|
| | | hb = hb.getParent();
|
| | | HongBaoOrder hbo = hongBaoOrderMapper.selectByHongBaoId(hb.getId());
|
| | | if (hbo != null && hbo.getCommonOrder() != null) {
|
| | | return taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(hbo.getCommonOrder().getTradeId(), uid);
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | }
|