| | |
| | | package com.yeshi.fanli.service.impl.hongbao;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.util.Calendar;
|
| | | import java.util.Collections;
|
| | | import java.util.Comparator;
|
| | | import java.util.Date;
|
| | |
| | | import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoOrderMapper;
|
| | | 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.order.CommonOrder;
|
| | | import com.yeshi.fanli.entity.order.CommonOrderGoods;
|
| | | import com.yeshi.fanli.entity.order.HongBaoOrder;
|
| | |
| | | import com.yeshi.fanli.exception.HongBaoException;
|
| | | 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.user.UserInfoService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | | import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
|
| | |
|
| | | @Service
|
| | |
| | |
|
| | | @Resource
|
| | | private CommonOrderService commonOrderService;
|
| | |
|
| | | @Resource
|
| | | private UserInfoService userInfoService;
|
| | |
|
| | | @Override
|
| | | public int insert(HongBaoV2 record) {
|
| | |
| | | @Resource
|
| | | private HongBaoManageService hongBaoManageService;
|
| | |
|
| | | @Resource
|
| | | private ThreeSaleSerivce threeSaleSerivce;
|
| | |
|
| | | @Transactional
|
| | | @Override
|
| | | public void addHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
|
| | | if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
|
| | | throw new HongBaoException(1, "订单信息不完整");
|
| | | HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
|
| | | if (hongBaoOrder == null) {
|
| | | if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
|
| | | BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
|
| | |
|
| | | } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
|
| | |
|
| | | } else
|
| | | throw new HongBaoException(2, "type错误");
|
| | |
|
| | | } else {
|
| | |
|
| | |
| | |
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | private void updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type) throws HongBaoException {
|
| | | if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
|
| | | BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
|
| | | HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
|
| | | hongBao.setUpdateTime(new Date());
|
| | | if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
| | | hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
|
| | | 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));
|
| | | } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
|
| | | hongBao.setState(HongBaoV2.STATE_SHIXIAO);
|
| | | }
|
| | | hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
|
| | | // 获取子红包
|
| | | List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBaoOrder.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_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_KELINGQU) {
|
| | | 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")));
|
| | | } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
|
| | | childUpdate.setMoney(
|
| | | MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), rate.divide(new BigDecimal(100))));
|
| | | }
|
| | | hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
|
| | | }
|
| | |
|
| | | } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
|
| | | // TODO 分享赚的二级更新
|
| | | // 分享赚
|
| | | BigDecimal shareRate = hongBaoManageService.getShareRate(commonOrder.getCreateTime().getTime());
|
| | | HongBaoV2 hongBao = new HongBaoV2();
|
| | | hongBao.setCreateTime(new Date());
|
| | | hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
|
| | | hongBao.setVersion(2);
|
| | | if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
| | | hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
|
| | | 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")));
|
| | | }
|
| | | 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);
|
| | | } else
|
| | | throw new HongBaoException(2, "type错误");
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | private void saveHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
|
| | | if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
|
| | | BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
|
| | | HongBaoV2 hongBao = new HongBaoV2();
|
| | | hongBao.setCreateTime(new Date());
|
| | | hongBao.setType(HongBaoV2.TYPE_ZIGOU);
|
| | | hongBao.setVersion(2);
|
| | | if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
| | | hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
|
| | | 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));
|
| | | } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
|
| | | hongBao.setState(HongBaoV2.STATE_SHIXIAO);
|
| | | hongBao.setMoney(new BigDecimal(0));
|
| | | } else {
|
| | | throw new HongBaoException(3, "维权订单不能创建红包");
|
| | | }
|
| | | 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 = threeSaleSerivce.getBoss(user.getId());
|
| | | if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1级BOSS存在且红包未失效
|
| | |
|
| | | // 插入一级子红包
|
| | | BigDecimal firstRate = hongBaoManageService
|
| | | .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank());
|
| | | HongBaoV2 firstHongbao = new HongBaoV2();
|
| | | 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(commonOrder.geteIncome(), 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")));
|
| | | } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
|
| | | firstHongbao.setMoney(
|
| | | MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), firstRate.divide(new BigDecimal(100))));
|
| | | }
|
| | | hongBaoV2Mapper.insertSelective(firstHongbao);
|
| | |
|
| | | // 插入二级子红包
|
| | | boss = threeSaleSerivce.getBoss(boss.getId());
|
| | | if (boss != null) {// 二级BOSS存在
|
| | | BigDecimal secondRate = hongBaoManageService
|
| | | .getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank());
|
| | | HongBaoV2 secondHongbao = new HongBaoV2();
|
| | | 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) {
|
| | | firstHongbao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
|
| | | 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")));
|
| | | } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
|
| | | secondHongbao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
|
| | | secondRate.divide(new BigDecimal(100))));
|
| | | }
|
| | | 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.setCreateTime(new Date());
|
| | | hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
|
| | | hongBao.setVersion(2);
|
| | | if (commonOrder.getState() == CommonOrder.STATE_FK) {
|
| | | hongBao.setState(HongBaoV2.STATE_BUFENSHIXIAO);
|
| | | 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")));
|
| | | }
|
| | | 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);
|
| | | } else
|
| | | throw new HongBaoException(2, "type错误");
|
| | | }
|
| | | }
|