package com.yeshi.fanli.service.impl.order;
|
|
import java.math.BigDecimal;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper;
|
import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
|
import com.yeshi.fanli.dao.mybatis.order.CommonOrderMapper;
|
import com.yeshi.fanli.dao.mybatis.order.CommonOrderTradeIdMapMapper;
|
import com.yeshi.fanli.dao.mybatis.order.HongBaoOrderMapper;
|
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
|
import com.yeshi.fanli.entity.bus.user.UserInfo;
|
import com.yeshi.fanli.entity.goods.CommonGoods;
|
import com.yeshi.fanli.entity.order.CommonOrder;
|
import com.yeshi.fanli.entity.order.CommonOrderGoods;
|
import com.yeshi.fanli.entity.order.CommonOrderTradeIdMap;
|
import com.yeshi.fanli.entity.order.HongBaoOrder;
|
import com.yeshi.fanli.entity.order.OrderRepairHistory;
|
import com.yeshi.fanli.entity.order.ShareGoodsActivityOrder;
|
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
|
import com.yeshi.fanli.exception.HongBaoException;
|
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
|
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
|
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
|
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
|
import com.yeshi.fanli.service.inter.order.CommonOrderService;
|
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
|
import com.yeshi.fanli.service.inter.order.OrderRepairHistoryService;
|
import com.yeshi.fanli.service.inter.order.OrderRepairService;
|
import com.yeshi.fanli.service.inter.order.ShareGoodsActivityOrderService;
|
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
|
import com.yeshi.fanli.service.inter.user.UserInfoService;
|
import com.yeshi.fanli.service.inter.user.UserSystemCouponRecordService;
|
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
import com.yeshi.fanli.util.TimeUtil;
|
import com.yeshi.fanli.util.factory.CommonOrderGoodsFactory;
|
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
|
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
|
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
|
|
@Service
|
public class OrderRepairServiceImpl implements OrderRepairService {
|
|
@Resource
|
private TaoBaoOrderService taoBaoOrderService;
|
|
@Resource
|
private CommonOrderService commonOrderService;
|
|
@Resource
|
private HongBaoOrderService hongBaoOrderService;
|
|
@Resource
|
private HongBaoV2Service hongBaoV2Service;
|
|
@Resource
|
private CommonOrderGoodsMapper commonOrderGoodsMapper;
|
|
@Resource
|
private CommonGoodsService commonGoodsService;
|
|
@Resource
|
private HongBaoManageService hongBaoManageService;
|
|
@Resource
|
private UserSystemCouponRecordService userSystemCouponRecordService;
|
|
@Resource
|
private HongBaoV2Mapper hongBaoV2Mapper;
|
|
@Resource
|
private UserSystemCouponService userSystemCouponService;
|
|
@Resource
|
private ShareGoodsActivityOrderService shareGoodsActivityOrderService;
|
|
@Resource
|
private CommonOrderMapper commonOrderMapper;
|
|
@Resource
|
private CommonOrderTradeIdMapMapper commonOrderTradeIdMapMapper;
|
|
@Resource
|
private UserInfoService userInfoService;
|
|
@Resource
|
private HongBaoOrderMapper hongBaoOrderMapper;
|
|
@Resource
|
private OrderRepairHistoryService orderRepairHistoryService;
|
|
@Override
|
public void repairOrder(String orderId) {
|
// 比较CommonOrder与TaoBaoOrder的预估收益
|
List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
|
// 统计
|
BigDecimal sumMoney = new BigDecimal("0");
|
for (TaoBaoOrder order : orderList) {
|
BigDecimal money = order.geteIncome().add(order.getSubsidy());
|
sumMoney = sumMoney.add(money);
|
if (order.getOrderState().equalsIgnoreCase("订单付款") || order.getOrderState().equalsIgnoreCase("订单成功"))
|
return;
|
}
|
|
|
List<CommonOrder> commonOrderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
|
orderId);
|
BigDecimal commonSumMoney = new BigDecimal("0");
|
for (CommonOrder commonOrder : commonOrderList) {
|
commonSumMoney = commonSumMoney.add(commonOrder.geteIncome());
|
}
|
|
try {
|
processLessFanOrder(orderId);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
// if (sumMoney.compareTo(commonSumMoney) > 0) {
|
// // 少返了
|
//
|
// } else {
|
//
|
// }
|
|
}
|
|
@Transactional
|
private void processLessFanOrder(String orderId) throws Exception {
|
List<CommonOrder> commonOrderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
|
orderId);
|
// 确定是自购还是分享赚
|
if (commonOrderList.size() <= 0)
|
return;
|
HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(commonOrderList.get(0).getId());
|
// 统计原来的上下级关系及返利的资金
|
Map<Long, BigDecimal> oldMoney = new HashMap<>();
|
Long firstUid = null;
|
Long secondUid = null;
|
Long mainUid = null;
|
// 统计返利的资金
|
for (CommonOrder co : commonOrderList) {
|
HongBaoOrder tempHongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(co.getId());
|
if (tempHongBaoOrder == null || tempHongBaoOrder.getHongBaoV2() == null)
|
continue;
|
hongBaoOrder = tempHongBaoOrder;
|
|
mainUid = hongBaoOrder.getHongBaoV2().getUserInfo().getId();
|
if (oldMoney.get(mainUid) == null)
|
oldMoney.put(mainUid, new BigDecimal(0));
|
if (hongBaoOrder.getHongBaoV2().getState() == HongBaoV2.STATE_YILINGQU)
|
oldMoney.put(mainUid, oldMoney.get(mainUid).add(hongBaoOrder.getHongBaoV2().getMoney()));
|
// 查询是否有下级
|
List<HongBaoV2> childrenList = hongBaoV2Service.listChildrenById(hongBaoOrder.getHongBaoV2().getId());
|
for (HongBaoV2 child : childrenList) {
|
if (child.getType() == HongBaoV2.TYPE_YIJI || child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {
|
firstUid = child.getUserInfo().getId();
|
if (oldMoney.get(firstUid) == null)
|
oldMoney.put(firstUid, new BigDecimal(0));
|
if (child.getState() == HongBaoV2.STATE_YILINGQU)
|
oldMoney.put(firstUid, oldMoney.get(firstUid).add(child.getMoney()));
|
|
} else if (child.getType() == HongBaoV2.TYPE_ERJI || child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {
|
secondUid = child.getUserInfo().getId();
|
if (oldMoney.get(secondUid) == null)
|
oldMoney.put(secondUid, new BigDecimal(0));
|
if (child.getState() == HongBaoV2.STATE_YILINGQU)
|
oldMoney.put(firstUid, oldMoney.get(secondUid).add(child.getMoney()));
|
}
|
}
|
}
|
|
List<TaoBaoOrder> taoBaoOrderList = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
|
|
// 根据交易ID查询
|
// 先删除所有信息
|
List<CommonOrder> commonOrderOldList = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO,
|
orderId);
|
if (commonOrderOldList != null)
|
for (CommonOrder co : commonOrderOldList) {
|
deleteByCommonOrderId(co.getId());
|
}
|
|
// 然后再增加
|
|
int invalidCount = 0;
|
for (TaoBaoOrder tb : taoBaoOrderList) {
|
if ("订单失效".equalsIgnoreCase(tb.getOrderState())) {
|
invalidCount++;
|
}
|
}
|
|
// 获取整体订单的状态
|
int wholeOrderState = 0;
|
if (taoBaoOrderList.size() == invalidCount)
|
wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_SHIXIAO;
|
else if (invalidCount == 0)
|
wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_YOUXIAO;
|
else
|
wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO;
|
|
for (TaoBaoOrder taoBaoOrder : taoBaoOrderList) {
|
CommonOrder commonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
|
commonOrder.setStateWholeOrder(wholeOrderState);
|
commonOrder.setCommonOrderGoods(getCommonOrderGoods(taoBaoOrder.getAuctionId()));
|
commonOrder.setUserInfo(new UserInfo(mainUid));
|
addOrder(commonOrder, hongBaoOrder.getHongBaoV2().getType(), firstUid, secondUid);
|
// } else {// 删除旧的,添加新的
|
// Long commonOrderId = oldCommonOrder.getId();
|
// List<CommonOrder> list =
|
// commonOrderService.listBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
|
// tradeId);
|
// if (list.size() > 1)// 删除多余的tradeId
|
// {
|
// for (CommonOrder co : list) {
|
// if (co.getId().longValue() != commonOrderId)
|
// deleteByCommonOrderId(co.getId());
|
// }
|
// }
|
// // 修改原来的信息
|
// updateOrderInfo(taoBaoOrder, commonOrderId);
|
// }
|
}
|
|
// 统计修改后的资金
|
List<CommonOrder> newCommonOrderList = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO,
|
orderId);
|
Map<Long, BigDecimal> newMap = new HashMap<>();
|
if (newCommonOrderList != null)
|
for (CommonOrder co : newCommonOrderList) {
|
HongBaoOrder newHongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(co.getId());
|
HongBaoV2 hongBao = newHongBaoOrder.getHongBaoV2();
|
if (hongBao.getState() == HongBaoV2.STATE_YILINGQU) {
|
Long uid = hongBao.getUserInfo().getId();
|
if (newMap.get(uid) == null)
|
newMap.put(uid, new BigDecimal(0));
|
newMap.put(uid, newMap.get(uid).add(hongBao.getMoney()));
|
}
|
List<HongBaoV2> children = hongBaoV2Service.listChildrenById(hongBao.getId());
|
if (children != null)
|
for (HongBaoV2 child : children) {
|
if (newMap.get(child.getUserInfo().getId()) == null)
|
newMap.put(child.getUserInfo().getId(), new BigDecimal(0));
|
if (child.getState() == HongBaoV2.STATE_YILINGQU)
|
newMap.put(child.getUserInfo().getId(),
|
newMap.get(child.getUserInfo().getId()).add(child.getMoney()));
|
}
|
}
|
|
for (Iterator<Long> its = newMap.keySet().iterator(); its.hasNext();) {
|
Long uid = its.next();
|
BigDecimal beforeGetMoney = oldMoney.get(uid);
|
BigDecimal afterGetMoney = newMap.get(uid);
|
OrderRepairHistory history = new OrderRepairHistory();
|
history.setAfterGetMoney(afterGetMoney);
|
history.setBeforeGetMoney(beforeGetMoney);
|
history.setOrderId(orderId);
|
history.setUid(uid);
|
orderRepairHistoryService.addOrderRepairHistory(history);
|
}
|
|
}
|
|
private CommonOrderGoods getCommonOrderGoods(Long auctionId) {
|
List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper.listByGoodsIdAndGoodsType(auctionId + "",
|
Constant.SOURCE_TYPE_TAOBAO);
|
CommonOrderGoods cog = null;
|
if (commonGoodsList.size() <= 0)// 不存在就插入商品
|
{
|
TaoBaoGoodsBrief taoBaoGoods = null;
|
try {
|
taoBaoGoods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId);
|
} catch (TaobaoGoodsDownException e) {
|
e.printStackTrace();
|
LogHelper.errorDetailInfo(e, "AUCTIONID:" + auctionId, "");
|
try {
|
taoBaoGoods = TaoBaoUtil.getSimpleGoodsBrief(auctionId);
|
} catch (Exception e1) {
|
CommonGoods commonGoods = commonGoodsService.getCommonGoodsByGoodsIdAndGoodsType(auctionId,
|
Constant.SOURCE_TYPE_TAOBAO);
|
if (commonGoods != null)
|
taoBaoGoods = TaoBaoUtil.convert(commonGoods);
|
}
|
}
|
if (taoBaoGoods != null) {
|
cog = CommonOrderGoodsFactory.create(taoBaoGoods);
|
}
|
cog.setCreateTime(new Date());
|
cog.setUpdateTime(new Date());
|
commonOrderGoodsMapper.insertSelective(cog);
|
|
return cog;
|
} else
|
return commonGoodsList.get(0);
|
|
}
|
|
// 修改订单信息
|
@Transactional
|
private void updateOrderInfo(TaoBaoOrder taoBaoOrder, Long commonOrderId) throws Exception {
|
HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(commonOrderId);
|
CommonOrder newCommonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
|
List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper
|
.listByGoodsIdAndGoodsType(taoBaoOrder.getAuctionId() + "", newCommonOrder.getSourceType());
|
CommonOrderGoods cog = null;
|
if (commonGoodsList.size() <= 0)// 不存在就插入商品
|
{
|
TaoBaoGoodsBrief taoBaoGoods = null;
|
try {
|
taoBaoGoods = TaoKeApiUtil.getSimpleGoodsInfo(taoBaoOrder.getAuctionId());
|
} catch (TaobaoGoodsDownException e) {
|
e.printStackTrace();
|
LogHelper.errorDetailInfo(e, "AUCTIONID:" + taoBaoOrder.getAuctionId(), "");
|
try {
|
taoBaoGoods = TaoBaoUtil.getSimpleGoodsBrief(taoBaoOrder.getAuctionId());
|
} catch (Exception e1) {
|
CommonGoods commonGoods = commonGoodsService.getCommonGoodsByGoodsIdAndGoodsType(
|
taoBaoOrder.getAuctionId(), Constant.SOURCE_TYPE_TAOBAO);
|
if (commonGoods != null)
|
taoBaoGoods = TaoBaoUtil.convert(commonGoods);
|
}
|
}
|
if (taoBaoGoods != null) {
|
cog = CommonOrderGoodsFactory.create(taoBaoGoods);
|
}
|
cog.setCreateTime(new Date());
|
cog.setUpdateTime(new Date());
|
commonOrderGoodsMapper.insertSelective(cog);
|
}
|
|
newCommonOrder.setCommonOrderGoods(cog);
|
newCommonOrder.setId(commonOrderId);
|
newCommonOrder.setCreateTime(null);
|
newCommonOrder.setUpdateTime(new Date());
|
commonOrderService.updateByPrimaryKeySelective(newCommonOrder);
|
// 更新主红包信息
|
CommonOrder commonOrder = commonOrderService.selectByPrimaryKey(newCommonOrder.getId());
|
|
if (hongBaoOrder.getHongBaoV2().getType() == HongBaoV2.TYPE_ZIGOU) {
|
BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
|
// 免单处理
|
boolean mianDan = false;
|
List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
|
commonOrder.getOrderNo());
|
if (orderList != null && orderList.size() == 1) {// 只有1个订单才参与免单
|
BigDecimal payMent = commonOrder.getPayment();
|
if (commonOrder.getState() == CommonOrder.STATE_JS)
|
payMent = commonOrder.getSettlement();
|
if (payMent.compareTo(new BigDecimal(10)) < 0) {
|
mianDan = userSystemCouponRecordService.isSuccessMianDan(commonOrder.getOrderNo());
|
}
|
}
|
|
HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
|
hongBao.setUpdateTime(new Date());
|
// 更改状态与资金
|
if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
|
if (mianDan)
|
hongBao.setMoney(commonOrder.getPayment());
|
} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
|
if (hongBaoOrder.getHongBaoV2().getPreGetTime().getTime() < System.currentTimeMillis())
|
hongBao.setState(HongBaoV2.STATE_YILINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
|
hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
|
if (mianDan)
|
hongBao.setMoney(commonOrder.getSettlement().compareTo(commonOrder.getPayment()) <= 0
|
? commonOrder.getSettlement() : commonOrder.getPayment());// 返利结算与付款较小的金额
|
} else if (commonOrder.getState() == CommonOrder.STATE_SX) {
|
hongBao.setState(HongBaoV2.STATE_SHIXIAO);
|
hongBao.setMoney(new BigDecimal(0));
|
if (mianDan) {
|
try {
|
userSystemCouponService.updateStateByDrawback(commonOrder.getOrderNo());
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new HongBaoException(12, "免单券退款出错");
|
}
|
}
|
}
|
hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
|
|
// 更新子红包信息
|
// 获取子红包
|
List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
|
if (children != null && children.size() > 0)
|
for (HongBaoV2 child : children) {
|
HongBaoV2 childUpdate = new HongBaoV2(child.getId());
|
childUpdate.setState(hongBao.getState());
|
childUpdate.setUpdateTime(new Date());
|
BigDecimal rate = null;
|
if (child.getType() == HongBaoV2.TYPE_YIJI) {// 一级分享赚
|
rate = hongBaoManageService.getFirstInviteRate(child.getUrank(),
|
commonOrder.getCreateTime().getTime());
|
} else if (child.getType() == HongBaoV2.TYPE_ERJI) {// 二级分享赚
|
rate = hongBaoManageService.getSecondInviteRate(child.getUrank(),
|
commonOrder.getCreateTime().getTime());
|
}
|
|
// 以实际收入为准计算预估收益
|
if (hongBao.getState() == HongBaoV2.STATE_YILINGQU) {
|
childUpdate.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
|
calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
|
"yyyy-M-dd")));
|
|
if (child.getPreGetTime().getTime() < System.currentTimeMillis()) {
|
childUpdate.setState(HongBaoV2.STATE_YILINGQU);
|
childUpdate.setGetTime(child.getGetTime());
|
}
|
|
} else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
|
childUpdate.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
|
}
|
hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
|
}
|
} else if (hongBaoOrder.getHongBaoV2().getType() == HongBaoV2.TYPE_SHARE_GOODS) {
|
|
BigDecimal fanliRate = hongBaoManageService.getShareRate(commonOrder.getCreateTime().getTime());
|
List<ShareGoodsActivityOrder> list = shareGoodsActivityOrderService
|
.listByOrderIdAndUid(commonOrder.getUserInfo().getId(), commonOrder.getOrderNo());
|
if (list != null && list.size() > 0) {
|
fanliRate = list.get(0).getShareRate();
|
}
|
|
HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
|
hongBao.setUpdateTime(new Date());
|
// 更改状态与资金
|
if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
|
} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
|
hongBao.setState(HongBaoV2.STATE_YILINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
if (hongBaoOrder.getHongBaoV2().getGetTime() == null) {
|
hongBao.setGetTime(hongBaoOrder.getHongBaoV2().getPreGetTime());
|
}
|
|
} else if (commonOrder.getState() == CommonOrder.STATE_SX) {
|
hongBao.setState(HongBaoV2.STATE_SHIXIAO);
|
hongBao.setMoney(new BigDecimal(0));
|
}
|
hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
|
|
// 获取子红包
|
List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
|
if (children != null)
|
for (HongBaoV2 child : children) {
|
HongBaoV2 childUpdate = new HongBaoV2(child.getId());
|
// 统一设置状态
|
childUpdate.setState(hongBao.getState());
|
childUpdate.setUpdateTime(new Date());
|
BigDecimal rate = null;
|
if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 一级分享赚
|
rate = hongBaoManageService.getFirstShareRate(1, commonOrder.getCreateTime().getTime());
|
} else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 二级分享赚
|
rate = hongBaoManageService.getSecondShareRate(1, commonOrder.getCreateTime().getTime());
|
}
|
|
// 以实际收入为准计算预估收益
|
if (CommonOrder.STATE_JS == commonOrder.getState()) {
|
childUpdate.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
|
if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-16",
|
"yyyy-MM-dd"))
|
childUpdate.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
|
rate.divide(new BigDecimal(100))));
|
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
|
calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
|
"yyyy-M-dd")));
|
if (child.getGetTime() == null) {
|
childUpdate.setGetTime(child.getPreGetTime());
|
}
|
|
} else if (CommonOrder.STATE_FK == commonOrder.getState()) {
|
if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-16",
|
"yyyy-MM-dd"))
|
childUpdate.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
|
rate.divide(new BigDecimal(100))));
|
else
|
childUpdate.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
|
}
|
hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
|
}
|
}
|
}
|
|
public void addOrder(CommonOrder commonOrder, int type, Long firstUid, Long secondUid) throws Exception {
|
// 增加commonOrder
|
commonOrder.setCreateTime(commonOrder.getThirdCreateTime());
|
// 不存在就插入,存在就不管
|
CommonOrderGoods goods = commonOrder.getCommonOrderGoods();
|
|
// 之前不存在于数据库
|
if (commonOrder.getCommonOrderGoods().getId() == null) {
|
List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper
|
.listByGoodsIdAndGoodsType(goods.getGoodsId(), goods.getGoodsType());
|
|
if (commonGoodsList == null || commonGoodsList.size() < 1) {// 不存在
|
commonOrderGoodsMapper.insertSelective(goods);
|
} else {// 存在
|
goods = commonGoodsList.get(0);
|
}
|
if (goods.getId() == null)
|
return;
|
commonOrder.setCommonOrderGoods(goods);
|
}
|
|
CommonOrder oldCommonOrder = commonOrderMapper.selectBySourceTypeAndTradeId(commonOrder.getSourceType(),
|
commonOrder.getTradeId());
|
if (oldCommonOrder == null)// 新增
|
{
|
commonOrder.setUpdateTime(new Date());
|
commonOrderMapper.insertSelective(commonOrder);
|
try {
|
// 插入映射,保证交易ID的完整性
|
commonOrderTradeIdMapMapper.insertSelective(new CommonOrderTradeIdMap(commonOrder.getId(),
|
commonOrder.getTradeId(), new Date(), commonOrder.getSourceType()));
|
} catch (Exception e) {
|
|
}
|
}
|
|
// 添加红包
|
if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
|
BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
|
// 查询是否有免单计划
|
BigDecimal mianDanMoney = null;
|
if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_FK) {
|
List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
|
commonOrder.getOrderNo());
|
if (orderList != null && orderList.size() == 1) {// 只有1个订单才参与免单
|
BigDecimal payMent = commonOrder.getPayment();
|
if (commonOrder.getState() == CommonOrder.STATE_JS)
|
payMent = commonOrder.getSettlement();
|
|
goods = commonOrderGoodsMapper.selectByPrimaryKey(commonOrder.getCommonOrderGoods().getId());
|
if (goods != null) {
|
|
try {
|
if (userSystemCouponService.updateCouponRecordUsed(commonOrder.getUserInfo().getId(),
|
commonOrder.getOrderNo(), payMent, Long.parseLong(goods.getGoodsId())))
|
mianDanMoney = payMent;
|
} catch (NumberFormatException e) {
|
throw new HongBaoException(10, "免单商品处理出错");
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new HongBaoException(11, "免单商品处理出错");
|
}
|
}
|
}
|
} else if (commonOrder.getState() == CommonOrder.STATE_SX) {// 设置免单券失效
|
try {
|
userSystemCouponService.updateStateByDrawback(commonOrder.getOrderNo());
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
HongBaoV2 hongBao = new HongBaoV2();
|
hongBao.setBeizhu("2019年7月批量修改");
|
hongBao.setUserInfo(commonOrder.getUserInfo());
|
hongBao.setCreateTime(new Date());
|
hongBao.setType(HongBaoV2.TYPE_ZIGOU);
|
hongBao.setVersion(2);
|
if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
|
} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
|
hongBao.setState(HongBaoV2.STATE_KELINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
|
hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
|
|
if (hongBao.getPreGetTime().getTime() < System.currentTimeMillis()) {
|
hongBao.setState(HongBaoV2.STATE_YILINGQU);
|
hongBao.setGetTime(hongBao.getPreGetTime());
|
}
|
} else if (commonOrder.getState() == CommonOrder.STATE_SX) {
|
hongBao.setState(HongBaoV2.STATE_SHIXIAO);
|
hongBao.setMoney(new BigDecimal(0));
|
} else {
|
throw new HongBaoException(3, "维权订单不能创建红包");
|
}
|
|
if (mianDanMoney != null)
|
hongBao.setMoney(mianDanMoney);
|
|
UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
|
hongBao.setUrank(user.getRank());
|
hongBaoV2Mapper.insertSelective(hongBao);
|
// 添加红包与订单的映射
|
HongBaoOrder hongBaoOrder = new HongBaoOrder();
|
hongBaoOrder.setCommonOrder(commonOrder);
|
hongBaoOrder.setCreateTime(new Date());
|
hongBaoOrder.setHongBaoV2(hongBao);
|
hongBaoOrderMapper.insertSelective(hongBaoOrder);
|
|
UserInfo boss = null;
|
if (firstUid != null)
|
boss = userInfoService.selectByPKey(firstUid);
|
|
if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO && mianDanMoney == null) {// 1级BOSS存在且红包未失效,免单不支持多级分销
|
|
// 插入一级子红包
|
BigDecimal firstRate = hongBaoManageService
|
.getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank());
|
if (firstRate.compareTo(new BigDecimal(0)) <= 0)
|
return;
|
HongBaoV2 firstHongbao = new HongBaoV2();
|
firstHongbao.setBeizhu("2019年7月批量修改");
|
firstHongbao.setUserInfo(boss);
|
firstHongbao.setUrank(boss.getRank());
|
firstHongbao.setParent(hongBao);
|
firstHongbao.setCreateTime(new Date());
|
firstHongbao.setType(HongBaoV2.TYPE_YIJI);
|
firstHongbao.setVersion(2);
|
firstHongbao.setState(hongBao.getState());
|
|
if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
|
firstHongbao.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
firstHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
|
calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
|
"yyyy-M-dd")));
|
if (firstHongbao.getPreGetTime().getTime() < System.currentTimeMillis()) {
|
firstHongbao.setGetTime(firstHongbao.getPreGetTime());
|
firstHongbao.setState(HongBaoV2.STATE_YILINGQU);
|
}
|
} else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
|
firstHongbao.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
|
}
|
// 返利为0的不通知
|
if (firstHongbao.getMoney() == null || firstHongbao.getMoney().compareTo(new BigDecimal(0)) <= 0)
|
return;
|
|
hongBaoV2Mapper.insertSelective(firstHongbao);
|
|
// 插入二级子红包
|
if (secondUid == null)
|
boss = null;
|
else
|
boss = userInfoService.selectByPKey(secondUid);
|
|
if (boss != null) {// 二级BOSS存在
|
BigDecimal secondRate = hongBaoManageService
|
.getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank());
|
if (secondRate.compareTo(new BigDecimal(0)) <= 0)
|
return;
|
HongBaoV2 secondHongbao = new HongBaoV2();
|
secondHongbao.setBeizhu("2019年7月批量修改");
|
secondHongbao.setUserInfo(boss);
|
secondHongbao.setUrank(boss.getRank());
|
secondHongbao.setParent(hongBao);
|
secondHongbao.setCreateTime(new Date());
|
secondHongbao.setType(HongBaoV2.TYPE_ERJI);
|
secondHongbao.setVersion(2);
|
secondHongbao.setState(hongBao.getState());
|
if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
|
secondHongbao.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
secondHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
|
calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
|
"yyyy-M-dd")));
|
if (secondHongbao.getPreGetTime().getTime() < System.currentTimeMillis()) {
|
secondHongbao.setGetTime(secondHongbao.getPreGetTime());
|
secondHongbao.setState(HongBaoV2.STATE_YILINGQU);
|
}
|
|
} else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
|
secondHongbao.setMoney(
|
MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
|
}
|
|
// 返利为0的不统计
|
if (secondHongbao.getMoney() == null || secondHongbao.getMoney().compareTo(new BigDecimal(0)) <= 0)
|
return;
|
hongBaoV2Mapper.insertSelective(secondHongbao);
|
}
|
|
}
|
|
} else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
|
// 分享赚不加入失效的订单
|
if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
|
return;
|
// 分享赚
|
BigDecimal shareRate = hongBaoManageService.getShareRate(commonOrder.getCreateTime().getTime());
|
|
HongBaoV2 hongBao = new HongBaoV2();
|
hongBao.setBeizhu("2019年7月批量修改");
|
hongBao.setCreateTime(new Date());
|
hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
|
hongBao.setVersion(2);
|
if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), shareRate.divide(new BigDecimal(100))));
|
} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
|
hongBao.setState(HongBaoV2.STATE_KELINGQU);
|
hongBao.setMoney(
|
MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), shareRate.divide(new BigDecimal(100))));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
|
calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
|
if (hongBao.getPreGetTime().getTime() < System.currentTimeMillis()) {
|
hongBao.setGetTime(hongBao.getPreGetTime());
|
hongBao.setState(HongBaoV2.STATE_YILINGQU);
|
}
|
}
|
UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
|
hongBao.setUrank(user.getRank());
|
hongBao.setUserInfo(user);
|
hongBaoV2Mapper.insertSelective(hongBao);
|
// 插入红包与订单映射
|
HongBaoOrder hongBaoOrder = new HongBaoOrder();
|
hongBaoOrder.setCommonOrder(commonOrder);
|
hongBaoOrder.setCreateTime(new Date());
|
hongBaoOrder.setHongBaoV2(hongBao);
|
hongBaoOrderMapper.insertSelective(hongBaoOrder);
|
|
// 4月17日后才有一级分享赚
|
if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
|
UserInfo boss = null;
|
if (firstUid != null)
|
boss = userInfoService.selectByPKey(firstUid);
|
if (boss != null) {
|
BigDecimal firstLevelRate = hongBaoManageService.getFirstShareRate(1,
|
commonOrder.getThirdCreateTime().getTime());
|
HongBaoV2 child = new HongBaoV2();
|
child.setBeizhu("2019年7月批量修改");
|
child.setParent(hongBao);
|
child.setType(HongBaoV2.TYPE_SHARE_YIJI);
|
if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
child.setState(HongBaoV2.STATE_BUKELINGQU);
|
child.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
|
firstLevelRate.divide(new BigDecimal(100))));
|
} else if (commonOrder.getState() == CommonOrder.STATE_JS) {
|
child.setState(HongBaoV2.STATE_KELINGQU);
|
child.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
|
firstLevelRate.divide(new BigDecimal(100))));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(commonOrder.getSettleTime());
|
calendar.add(Calendar.MONTH, 1);
|
child.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
|
calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
|
"yyyy-M-dd")));
|
|
if (child.getPreGetTime().getTime() < System.currentTimeMillis()) {
|
child.setGetTime(child.getPreGetTime());
|
child.setState(HongBaoV2.STATE_YILINGQU);
|
}
|
|
}
|
child.setUserInfo(boss);
|
child.setUrank(boss.getRank());
|
child.setVersion(2);
|
child.setCreateTime(new Date());
|
hongBaoV2Mapper.insertSelective(child);
|
}
|
}
|
|
} else
|
throw new HongBaoException(2, "type错误");
|
|
// 添加
|
|
}
|
|
@Transactional
|
private void deleteByCommonOrderId(Long commonOrderId) {
|
HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(commonOrderId);
|
if (hongBaoOrder == null || hongBaoOrder.getHongBaoV2() == null)
|
return;
|
List<HongBaoV2> children = hongBaoV2Service.listChildrenById(hongBaoOrder.getHongBaoV2().getId());
|
if (children != null)
|
for (HongBaoV2 hongBao : children) {
|
hongBaoV2Service.deleteByPrimaryKey(hongBao.getId());
|
}
|
hongBaoV2Service.deleteByPrimaryKey(hongBaoOrder.getHongBaoV2().getId());
|
hongBaoOrderService.deleteByPrimaryKey(hongBaoOrder.getId());
|
commonOrderService.deleteByPrimaryKey(commonOrderId);
|
}
|
|
}
|