| | |
| | | package com.yeshi.fanli.service.impl.taobao;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.Iterator;
|
| | |
| | | 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.hongbao.HongBaoMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.order.CommonOrderMapper;
|
| | | 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.HongBao;
|
| | | import com.yeshi.fanli.entity.bus.user.HongBaoV2;
|
| | | import com.yeshi.fanli.entity.bus.user.UserInfo;
|
| | | 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.TaoBaoOrder;
|
| | | 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.UserMoneyDetailException;
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | 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.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;
|
| | |
|
| | | @Service
|
| | | public class TaoBaoWeiQuanDrawBackServiceImpl implements TaoBaoWeiQuanDrawBackService {
|
| | | @Resource
|
| | | private TaoBaoWeiQuanDrawBackMapper taoBaoWeiQuanDrawBackMapper;
|
| | |
|
| | | @Resource
|
| | | private HongBaoMapper hongBaoMapper;
|
| | |
|
| | | @Resource
|
| | | private TaoBaoWeiQuanOrderService taoBaoWeiQuanOrderService;
|
| | |
| | | private UserInfoMapper userInfoMapper;
|
| | |
|
| | | @Resource
|
| | | private UserMoneyDetailMapper userMoneyDetailMapper;
|
| | |
|
| | | @Resource
|
| | | private AccountDetailsMapper accountDetailsMapper;
|
| | |
|
| | | @Resource
|
| | | private UserNotificationService userNotificationService;
|
| | |
|
| | | @Resource
|
| | | private CommonOrderMapper commonOrderMapper;
|
| | |
|
| | | @Resource
|
| | | private HongBaoV2Mapper hongBaoV2Mapper;
|
| | |
|
| | | @Resource
|
| | | private HongBaoOrderMapper hongBaoOrderMapper;
|
| | |
|
| | | @Resource
|
| | | private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
|
| | |
|
| | | @Override
|
| | | public void addWeiQuanDrawBack(TaoBaoWeiQuanDrawBack taoBaoWeiQuanDrawBack) {
|
| | |
| | | sumFee = sumFee.add(order.geteIncome());
|
| | | // 获取和该订单号有关联的用户
|
| | | // 获取主红包(同一个订单号的单只会对应同一个用户)
|
| | | List<HongBao> mainHongBaoList = hongBaoMapper.selectByOrderIdWithoutChild(orderId);
|
| | | 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 = 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());
|
| | | }
|
| | | if (mainHongBaoList == null || mainHongBaoList.size() == 0)
|
| | | return;
|
| | | for (int i = 0; i < mainHongBaoList.size(); i++) {
|
| | | HongBaoV2 hb = hongBaoV2Mapper.selectByPrimaryKey(mainHongBaoList.get(i).getId());
|
| | | mainHongBaoList.set(i, hb);
|
| | | // 只处理返利订单
|
| | | if (mainHongBaoList.get(i).getType() != HongBao.TYPE_TAOBAO) {
|
| | | if (hb.getType() != HongBaoV2.TYPE_ZIGOU) {
|
| | | mainHongBaoList.remove(i);
|
| | | i--;
|
| | | }
|
| | |
| | | Map<Long, BigDecimal> fanMoneyMap = new HashMap<>();
|
| | |
|
| | | if (mainHongBaoList != null)
|
| | | for (HongBao hongBao : mainHongBaoList) {
|
| | | if (hongBao.getState() == HongBao.STATE_SHIXIAO)
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {
|
| | | if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | continue;
|
| | | // 累计主红包的金额
|
| | | if (fanMoneyMap.get(hongBao.getUserInfo().getId()) == null) {
|
| | |
| | | BigDecimal userGetMoney = fanMoneyMap.get(uid);
|
| | | BigDecimal fanMoney = new BigDecimal("0");
|
| | | // 统计用户在这个单中需要扣除的资金
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = null;
|
| | | for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper
|
| | | .selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(), uid);
|
| | | weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(),
|
| | | uid);
|
| | | if (weiQuanDrawBack != null)
|
| | | continue;
|
| | | // 退款的资金
|
| | |
| | | AccountDetails accountDetails = AccountDetailsFactory.create("-" + drawBackMoney,
|
| | | AccountDetailsFactory.SHARE_GOODS_DRAWBACK, null, null, new UserInfo(uid));
|
| | | accountDetailsMapper.insertSelective(accountDetails);
|
| | | // 新版资金记录
|
| | | try {
|
| | | UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createFanLiWeiQuan(uid, weiQuanDrawBack,
|
| | | drawBackMoney);
|
| | | userMoneyDetail.setId(accountDetails.getId());
|
| | | userMoneyDetailMapper.insert(userMoneyDetail);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | | } catch (Exception e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | // 新版通知
|
| | | userMoneyMsgNotificationService.fanliOrderWeiQuan(uid, orderId, drawBackMoney,
|
| | | userInfoMapper.selectByPrimaryKey(uid).getMyHongBao());
|
| | |
|
| | | userNotificationService.weiQuanFanli(uid, orderId, drawBackMoney);
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | @Override
|
| | | public void doWeiQuanShare(String orderId) throws TaoBaoWeiQuanException {
|
| | | if (StringUtil.isNullOrEmpty(orderId))
|
| | |
| | | sumFee = sumFee.add(order.geteIncome());
|
| | | // 获取和该订单号有关联的用户
|
| | | // 获取主红包(同一个订单号的单只会对应同一个用户)
|
| | | List<HongBao> mainHongBaoList = hongBaoMapper.selectByOrderIdWithoutChild(orderId);
|
| | |
|
| | | 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 = 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());
|
| | | }
|
| | |
|
| | | if (mainHongBaoList == null || mainHongBaoList.size() == 0)
|
| | | return;
|
| | |
|
| | |
| | | Map<Long, BigDecimal> fanMoneyMap = new HashMap<>();
|
| | |
|
| | | if (mainHongBaoList != null)
|
| | | for (HongBao hongBao : mainHongBaoList) {
|
| | | if (hongBao.getState() == HongBao.STATE_SHIXIAO)
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {
|
| | | hongBao = hongBaoV2Mapper.selectByPrimaryKey(hongBao.getId());
|
| | | if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
|
| | | continue;
|
| | |
|
| | | // 返利红包不计入
|
| | | if (hongBao.getType() != HongBao.TYPE_TAOBAO) {
|
| | | if (hongBao.getType() != HongBaoV2.TYPE_ZIGOU) {
|
| | | // 累计主红包的金额
|
| | | if (fanMoneyMap.get(hongBao.getUserInfo().getId()) == null) {
|
| | | fanMoneyMap.put(hongBao.getUserInfo().getId(), hongBao.getMoney());
|
| | |
| | | fanMoneyMap.get(hongBao.getUserInfo().getId()).add(hongBao.getMoney()));
|
| | | }
|
| | | // 累计子红包的金额
|
| | | List<HongBao> childHongBaoList = hongBaoMapper.selectChildHongBaoByPid(hongBao.getId());
|
| | | List<HongBaoV2> childHongBaoList = hongBaoV2Mapper.listChildrenById(hongBao.getId());
|
| | | if (childHongBaoList != null)
|
| | | for (HongBao child : childHongBaoList) {
|
| | | if (child.getState() == HongBao.STATE_SHIXIAO)
|
| | | 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());
|
| | |
| | | BigDecimal userGetMoney = fanMoneyMap.get(uid);
|
| | | BigDecimal fanMoney = new BigDecimal("0");
|
| | | // 统计用户在这个单中需要扣除的资金
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = null;
|
| | | for (TaoBaoWeiQuanOrder weiQuanOrder : list) {
|
| | | TaoBaoWeiQuanDrawBack weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper
|
| | | .selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(), uid);
|
| | | weiQuanDrawBack = taoBaoWeiQuanDrawBackMapper.selectByOrderItemIdAndUid(weiQuanOrder.getOrderItemId(),
|
| | | uid);
|
| | | if (weiQuanDrawBack != null)
|
| | | continue;
|
| | | // 退款的资金
|
| | |
| | | AccountDetails accountDetails = AccountDetailsFactory.create("-" + drawBackMoney,
|
| | | AccountDetailsFactory.SHARE_GOODS_DRAWBACK, null, null, new UserInfo(uid));
|
| | | accountDetailsMapper.insertSelective(accountDetails);
|
| | | // 新版资金记录
|
| | | try {
|
| | | UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShareWeiQuan(uid, weiQuanDrawBack,
|
| | | drawBackMoney);
|
| | | userMoneyDetail.setId(accountDetails.getId());
|
| | | userMoneyDetailMapper.insert(userMoneyDetail);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | | } catch (Exception e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | userMoneyMsgNotificationService.shareOrderWeiQuan(uid, orderId, drawBackMoney,
|
| | | userInfoMapper.selectByPrimaryKey(uid).getMyHongBao());
|
| | |
|
| | | userNotificationService.weiQuanTiCheng(uid, orderId, drawBackMoney);
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | @Override
|
| | | public void doWeiQuanInvite(String orderId) throws TaoBaoWeiQuanException {
|
| | | if (StringUtil.isNullOrEmpty(orderId))
|
| | | throw new TaoBaoWeiQuanException(1, "订单号为空值");
|
| | | // 查询是否为维权订单
|
| | | 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);
|
| | | 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());
|
| | | }
|
| | | if (mainHongBaoList == null || mainHongBaoList.size() == 0)
|
| | | return;
|
| | |
|
| | | // 主用户ID
|
| | | Map<Long, BigDecimal> fanMoneyMap = new HashMap<>();
|
| | |
|
| | | if (mainHongBaoList != null)
|
| | | for (HongBaoV2 hongBao : mainHongBaoList) {
|
| | | HongBaoV2 hb = hongBaoV2Mapper.selectByPrimaryKey(hongBao.getId());
|
| | | hongBao = hb;
|
| | |
|
| | | 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)
|
| | | 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");
|
| | | // 统计用户在这个单中需要扣除的资金
|
| | | 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();
|
| | | }
|
| | | }
|
| | |
|
| | | // 新版通知
|
| | | userMoneyMsgNotificationService.inviteOrderWeiQuan(uid, orderId, drawBackMoney,
|
| | | userInfoMapper.selectByPrimaryKey(uid).getMyHongBao());
|
| | |
|
| | | userNotificationService.weiQuanTiCheng(uid, orderId, drawBackMoney);
|
| | | }
|