From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderRepairServiceImpl.java | 2775 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 1,389 insertions(+), 1,386 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderRepairServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderRepairServiceImpl.java index 6f91cce..e83211a 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderRepairServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderRepairServiceImpl.java @@ -1,1386 +1,1389 @@ -package com.yeshi.fanli.service.impl.order; - -import java.io.File; -import java.io.FileNotFoundException; -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 java.util.Scanner; - -import javax.annotation.Resource; - -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; - -import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper; -import com.yeshi.fanli.dao.mybatis.money.ExtractCheckCompensateMapper; -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.dao.mybatis.order.OrderRepairHistoryMapper; -import com.yeshi.fanli.entity.bus.user.HongBaoV2; -import com.yeshi.fanli.entity.bus.user.UserInfo; -import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum; -import com.yeshi.fanli.entity.goods.CommonGoods; -import com.yeshi.fanli.entity.money.ExtractCheckCompensate; -import com.yeshi.fanli.entity.money.UserMoneyDetail; -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.push.PushInfo; -import com.yeshi.fanli.entity.push.PushInfo.PushTypeEnum; -import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; -import com.yeshi.fanli.entity.taobao.TaoBaoOrder; -import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder; -import com.yeshi.fanli.exception.money.UserMoneyDetailException; -import com.yeshi.fanli.exception.order.HongBaoException; -import com.yeshi.fanli.exception.push.PushException; -import com.yeshi.fanli.exception.push.PushInfoException; -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.money.UserMoneyService; -import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService; -import com.yeshi.fanli.service.inter.order.CommonOrderService; -import com.yeshi.fanli.service.inter.order.HongBaoOrderService; -import com.yeshi.fanli.service.inter.order.HongBaoV2Service; -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.order.config.HongBaoManageService; -import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService; -import com.yeshi.fanli.service.inter.order.tb.TaoBaoWeiQuanOrderService; -import com.yeshi.fanli.service.inter.push.PushInfoService; -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.StringUtil; -import com.yeshi.fanli.util.TimeUtil; -import com.yeshi.fanli.util.factory.CommonOrderGoodsFactory; -import com.yeshi.fanli.util.factory.UserMoneyDetailFactory; -import com.yeshi.fanli.util.factory.goods.CommonOrderFactory; -import com.yeshi.fanli.util.taobao.TaoBaoUtil; -import com.yeshi.fanli.util.taobao.TaoKeApiUtil; - -import net.sf.json.JSONObject; - -@Service -public class OrderRepairServiceImpl implements OrderRepairService { - - @Resource - private OrderRepairHistoryMapper orderRepairHistoryMapper; - - @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; - - @Resource - private DataSourceTransactionManager dataSourceTransactionManager; - - @Resource - private TaoBaoWeiQuanOrderService taoBaoWeiQuanOrderService; - - @Resource - private UserMoneyService userMoneyService; - - @Resource - private PushInfoService pushInfoService; - - @Resource - private UserMoneyMsgNotificationService userMoneyMsgNotificationService; - - @Resource - private ExtractCheckCompensateMapper extractCheckCompensateMapper; - - @Transactional(rollbackFor = Exception.class) - @Override - public void repairOrder(String orderId) throws Exception { - // 姣旇緝CommonOrder涓嶵aoBaoOrder鐨勯浼版敹鐩� - List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId); - // 缁熻 - BigDecimal sumMoney = new BigDecimal("0"); - for (TaoBaoOrder order : orderList) { - // if (order.getOrderState().equalsIgnoreCase("璁㈠崟浠樻") || - // order.getOrderState().equalsIgnoreCase("璁㈠崟鎴愬姛")) - // return; - if (order.getSubsidy() == null) - order.setSubsidy(new BigDecimal(0)); - if (order.geteIncome() == null) - order.seteIncome(new BigDecimal(0)); - - BigDecimal money = order.geteIncome().add(order.getSubsidy()); - sumMoney = sumMoney.add(money); - } - - List<CommonOrder> commonOrderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO, - orderId); - BigDecimal commonSumMoney = new BigDecimal("0"); - for (CommonOrder commonOrder : commonOrderList) { - commonSumMoney = commonSumMoney.add(commonOrder.geteIncome()); - } - - // 涓嶅鐞嗙淮鏉冭繃鐨勮鍗� - List<TaoBaoWeiQuanOrder> weiQuanOrderList = taoBaoWeiQuanOrderService.getWeiQuanSuccessOrders(orderId); - if (weiQuanOrderList != null && weiQuanOrderList.size() > 0) - return; - - processLessFanOrder(orderId); - - // if (sumMoney.compareTo(commonSumMoney) > 0) { - // // 灏戣繑浜� - // - // } else { - // - // } - - } - - @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) - private void processLessFanOrder(String orderId) throws Exception { - - List<CommonOrder> commonOrderList = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO, - orderId); - // 纭畾鏄嚜璐繕鏄垎浜禋 - if (commonOrderList.size() <= 0) - return; - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(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 = hongBaoOrderMapper.selectByCommonOrderId(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 = hongBaoV2Mapper.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())); - } - } - } - - // FileWriter fw = null; - // try { - // // 濡傛灉鏂囦欢瀛樺湪锛屽垯杩藉姞鍐呭锛涘鏋滄枃浠朵笉瀛樺湪锛屽垯鍒涘缓鏂囦欢 - // File f = new File("D:\\three_sale.txt"); - // fw = new FileWriter(f, true); - // } catch (IOException e) { - // e.printStackTrace(); - // } - // PrintWriter pw = new PrintWriter(fw); - // pw.println(orderId+"#"+firstUid+"-"+oldMoney.get(firstUid)+"#"+secondUid+"-"+(secondUid!=null? - // oldMoney.get(secondUid):"")); - // pw.flush(); - // try { - // fw.flush(); - // pw.close(); - // fw.close(); - // } catch (IOException e) { - // e.printStackTrace(); - // } - // - // if (1 > 0) - // return; - - 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 = CommonOrderFactory.create(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)// 鍒犻櫎澶氫綑鐨則radeId - // { - // 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()); - if (newHongBaoOrder == null) - continue; - 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())); - } else { - Long uid = hongBao.getUserInfo().getId(); - if (newMap.get(uid) == null) - newMap.put(uid, new BigDecimal(0)); - } - 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(rollbackFor = Exception.class) - private void updateOrderInfo(TaoBaoOrder taoBaoOrder, Long commonOrderId) throws Exception { - HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(commonOrderId); - CommonOrder newCommonOrder = CommonOrderFactory.create(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(UserLevelEnum.daRen, - 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.getSourceType(), - 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(),commonOrder.getSourceType()); - } 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(UserLevelEnum.daRen, - 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); - } - } - } - - @Transactional(rollbackFor = Exception.class) - 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 { - // 鎻掑叆鏄犲皠锛屼繚璇佷氦鏄揑D鐨勫畬鏁存�� - commonOrderTradeIdMapMapper.insertSelective(new CommonOrderTradeIdMap(commonOrder.getId(), - commonOrder.getTradeId(), new Date(), commonOrder.getSourceType())); - } catch (Exception e) { - - } - } - - // 娣诲姞绾㈠寘 - if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚� - BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen, - 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()),commonOrder.getSourceType())) - 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(),commonOrder.getSourceType()); - } 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.getUserByIdWithMybatis(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绾OSS瀛樺湪涓旂孩鍖呮湭澶辨晥,鍏嶅崟涓嶆敮鎸佸绾у垎閿� - - // 鎻掑叆涓�绾у瓙绾㈠寘 - 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) {// || - // hongBao.getState() - // == - // HongBaoV2.STATE_YILINGQU - 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) {// || - // hongBao.getState() - // == - // HongBaoV2.STATE_YILINGQU - 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(UserLevelEnum.daRen, - 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.getUserByIdWithMybatis(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(propagation = Propagation.REQUIRED) - private void deleteByCommonOrderId(Long commonOrderId) { - commonOrderMapper.deleteByPrimaryKey(commonOrderId); - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrderId); - if (hongBaoOrder == null || hongBaoOrder.getHongBaoV2() == null) - return; - List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getHongBaoV2().getId()); - if (children != null) - for (HongBaoV2 hongBao : children) { - hongBaoV2Mapper.deleteByPrimaryKey(hongBao.getId()); - } - hongBaoV2Mapper.deleteByPrimaryKey(hongBaoOrder.getHongBaoV2().getId()); - hongBaoOrderMapper.deleteByPrimaryKey(hongBaoOrder.getId()); - - } - - @Transactional - @Override - public void testTransaction() { - hongBaoV2Mapper.selectByPrimaryKey(1L); - } - - private static Map<String, OrderInfo> orderInfoMap; - - private OrderInfo getOrderInfo(String orderId) { - if (orderInfoMap == null) { - orderInfoMap = new HashMap<>(); - Scanner scanner = null; - try { - scanner = new Scanner(new File("C:\\Users\\Administrator\\Desktop\\璁㈠崟闆嗕腑鎺掓煡\\three_sale.txt")); - while (scanner.hasNext()) { - OrderInfo orderInfo = new OrderInfo(); - String info = scanner.next(); - if (StringUtil.isNullOrEmpty(info)) - continue; - - String[] sts = info.split("#"); - String orderid = sts[0]; - orderInfo.setOrderId(orderid); - String firstUid = sts[1].split("-")[0]; - String firstUidMoney = sts[1].split("-")[1]; - orderInfo.setFirstUid(Long.parseLong(firstUid)); - orderInfo.setFirstUidMoney(new BigDecimal(firstUidMoney)); - - String secondUid = sts[2].split("-")[0]; - String secondUidMoney = null; - if (sts[2].split("-").length > 1) - secondUidMoney = sts[2].split("-")[1]; - if (!StringUtil.isNullOrEmpty(secondUid)) - orderInfo.setSecondUid(Long.parseLong(secondUid)); - if (!StringUtil.isNullOrEmpty(secondUidMoney)) - orderInfo.setSecondUidMoney(new BigDecimal(secondUidMoney)); - orderInfoMap.put(orderInfo.getOrderId(), orderInfo); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - scanner.close(); - } - - return orderInfoMap.get(orderId); - } - - @Transactional(rollbackFor = Exception.class) - @Override - public void repireFirstAndSecondLevel(String orderId) throws Exception { - repireFirstShare(orderId); - if (1 > 0) - return; - OrderInfo info = getOrderInfo(orderId); - Map<Long, BigDecimal> oldMoneyMap = new HashMap<>(); - if (info.getFirstUid() != null) - oldMoneyMap.put(info.getFirstUid(), info.getFirstUidMoney()); - if (info.getSecondUid() != null) - oldMoneyMap.put(info.getSecondUid(), info.getSecondUidMoney()); - - if (info != null) { - List<CommonOrder> list = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO, - info.getOrderId()); - // 鏌ヨ鏄惁鏈夊瓙绾㈠寘 - for (CommonOrder commonOrder : list) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); - if (hongBaoOrder == null) - continue; - List<HongBaoV2> hongBaoList = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getHongBaoV2().getId()); - if (hongBaoList != null && hongBaoList.size() > 0)// 鏈夊瓙绾㈠寘灏变笉澶勭悊浜� - return; - } - - for (CommonOrder commonOrder : list) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); - if (hongBaoOrder == null) - continue; - HongBaoV2 hongBao = hongBaoOrder.getHongBaoV2(); - Long firstUid = info.getFirstUid(); - Long secondUid = info.getSecondUid(); - // 鍔犲叆瀛愮孩鍖� - UserInfo boss = null; - if (firstUid != null) - boss = userInfoService.selectByPKey(firstUid); - - if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1绾OSS瀛樺湪涓旂孩鍖呮湭澶辨晥,鍏嶅崟涓嶆敮鎸佸绾у垎閿� - // 鎻掑叆涓�绾у瓙绾㈠寘 - 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 - || hongBao.getState() == HongBaoV2.STATE_YILINGQU) { - 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) - continue; - - 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 - || hongBao.getState() == HongBaoV2.STATE_YILINGQU) { - 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) - continue; - hongBaoV2Mapper.insertSelective(secondHongbao); - } - - } - } - } - - // 缁熻淇敼鍚庣殑璧勯噾 - 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()); - if (newHongBaoOrder == null) - continue; - HongBaoV2 hongBao = newHongBaoOrder.getHongBaoV2(); - 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 = oldMoneyMap.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 void repireFirstShare(String orderId) throws Exception { - OrderInfo info = getOrderInfo(orderId); - Long firstUid = info.getFirstUid(); - Map<Long, BigDecimal> oldMoneyMap = new HashMap<>(); - if (info.getFirstUid() != null) - oldMoneyMap.put(info.getFirstUid(), info.getFirstUidMoney()); - if (info.getSecondUid() != null) - oldMoneyMap.put(info.getSecondUid(), info.getSecondUidMoney()); - - if (info != null) { - List<CommonOrder> list = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO, - info.getOrderId()); - // 鏌ヨ鏄惁鏈夊瓙绾㈠寘 - for (CommonOrder commonOrder : list) { - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); - if (hongBaoOrder == null) - continue; - List<HongBaoV2> hongBaoList = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getHongBaoV2().getId()); - if (hongBaoList != null && hongBaoList.size() > 0)// 鏈夊瓙绾㈠寘灏变笉澶勭悊浜� - return; - } - - for (CommonOrder commonOrder : list) { - if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ) - continue; - // 鍒嗕韩璧� - - HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); - if (hongBaoOrder == null) - continue; - - HongBaoV2 hongBao = hongBaoOrder.getHongBaoV2(); - // 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); - } - } - } - - } - - // 缁熻淇敼鍚庣殑璧勯噾 - 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()); - if (newHongBaoOrder == null) - continue; - HongBaoV2 hongBao = newHongBaoOrder.getHongBaoV2(); - 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 = oldMoneyMap.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); - } - - } - - class OrderInfo { - private String orderId; - private Long firstUid; - private BigDecimal firstUidMoney; - private Long secondUid; - private BigDecimal secondUidMoney; - - public String getOrderId() { - return orderId; - } - - public void setOrderId(String orderId) { - this.orderId = orderId; - } - - public Long getFirstUid() { - return firstUid; - } - - public void setFirstUid(Long firstUid) { - this.firstUid = firstUid; - } - - public BigDecimal getFirstUidMoney() { - return firstUidMoney; - } - - public void setFirstUidMoney(BigDecimal firstUidMoney) { - this.firstUidMoney = firstUidMoney; - } - - public Long getSecondUid() { - return secondUid; - } - - public void setSecondUid(Long secondUid) { - this.secondUid = secondUid; - } - - public BigDecimal getSecondUidMoney() { - return secondUidMoney; - } - - public void setSecondUidMoney(BigDecimal secondUidMoney) { - this.secondUidMoney = secondUidMoney; - } - } - - @Override - public List<OrderRepairHistory> listByUid(Long uid) { - - return orderRepairHistoryMapper.listByUid(uid); - } - - @Transactional - @Override - public void doMoney(Long uid) { - List<OrderRepairHistory> historyList = listByUid(uid); - for (int i = 0; i < historyList.size(); i++) { - if (historyList.get(i).getCreateTime().getTime() < TimeUtil.convertToTimeTemp("2019-07-15 17:00:38", - "yyyy-MM-dd HH:mm:ss")) { - historyList.remove(i); - i--; - } - - } - - BigDecimal money = new BigDecimal(0); - String orders = ""; - for (OrderRepairHistory history : historyList) { - money = money.add(history.getAfterGetMoney().subtract(history.getBeforeGetMoney())); - orders += history.getOrderId() + ","; - } - if (money.compareTo(new BigDecimal(0)) > 0)// 璧勯噾闇�瑕佸鍔� - { - try { - UserMoneyDetail detail = UserMoneyDetailFactory.createSystemEqualize("璁㈠崟缁熻寮傚父淇", money, - new UserInfo(uid)); - // 绯荤粺琛ラ綈 - // 鍔犺祫閲� - userMoneyService.addUserMoney(uid, money, detail); - pushMsg(uid, "鍏充簬杩戞湡璁㈠崟涓庤祫閲戝紓甯哥殑鎺掓煡涓庝慨澶嶇粨鏋滈�氱煡", - "灏婃暚鐨勭敤鎴凤紝缁忎粩缁嗘牳瀵规偍璐︽埛涓殑璁㈠崟锛屽彂鐜版偍璐︽埛涓湁鏈粺璁℃垨閿欒缁熻鐨勮鍗曪紝鎴戜滑宸茬粡涓烘偍琛ラ綈璧勯噾锛屽搴旇祫閲戝凡缁忓瓨鍏ユ偍鐨勮处鎴蜂綑棰濅腑锛屾暚璇锋煡鏀舵敞鏌ユ敹銆傛劅璋俊浠伙紝鏉挎牀蹇渷App缁堝皢鎴愪负鎮ㄦ渶淇′换鐨勮喘鐗╃渷閽卞姪鎵嬨��"); - userMoneyMsgNotificationService.systemEqualize(uid, "璁㈠崟缁熻寮傚父淇", money, userInfoService.getBalance(uid)); - } catch (UserMoneyDetailException e) { - e.printStackTrace(); - } - - } else {// 璧勯噾闇�瑕佸噺灏�,涓嶈鎵i挶 - pushMsg(uid, "鍏充簬杩戞湡璁㈠崟涓庤祫閲戝紓甯哥殑鎺掓煡涓庝慨澶嶇粨鏋滈�氱煡", - "灏婃暚鐨勭敤鎴凤紝缁忎粩缁嗘牳瀵规偍璐︽埛涓殑璁㈠崟锛屽彂鐜版偍璐︽埛涓湁閿欒缁熻鐨勮鍗曪紝鎴戜滑宸茬粡涓烘偍鎭㈠姝e父锛屾墍娑夊強璁㈠崟澶氱粺璁″嚭鏉ョ殑璧勯噾锛屼笉浼氳鎵i櫎锛屽嵆鎮ㄨ处鎴蜂腑鐨勮祫閲戝皢淇濇寔涓嶅彉锛屽凡鎻愮幇鐨勮祫閲戯紝涔熶笉浼氭敹鍥烇紝鏁鐭ユ檽銆傛劅璋俊浠伙紝鏉挎牀蹇渷App缁堝皢鎴愪负鎮ㄦ渶淇′换鐨勮喘鐗╃渷閽卞姪鎵嬨��"); - // userMoneyMsgNotificationService.systemEqualize(uid, "璁㈠崟缁熻寮傚父淇", - // money, userInfoService.getBalance(uid)); - // 鍔犲叆鎻愮幇瀹℃牳寮傚父淇濇姢涓� - ExtractCheckCompensate check = new ExtractCheckCompensate(); - check.setBeiZhu("7鏈堣鍗曞紓甯哥郴缁熷鐞�:" + orders); - check.setCreateTime(new Date()); - check.setMoney(money); - check.setUserInfo(new UserInfo(uid)); - check.setUpdateTime(new Date()); - extractCheckCompensateMapper.insertSelective(check); - } - } - - private void pushMsg(Long uid, String title, String content) { - - PushInfo pushInfo = new PushInfo(); - pushInfo.setTitle(title); - pushInfo.setContent(content); - pushInfo.setUids(uid + ""); - JSONObject jsonData = new JSONObject(); - jsonData.put("url", ""); - jsonData.put("ios", "鍏ㄦ帹"); - jsonData.put("android", "鍏ㄦ帹"); - pushInfo.setJsonData(jsonData.toString()); - pushInfo.setType(PushTypeEnum.ZNX); - pushInfo.setArrayAndroid("[鍏ㄦ帹]"); - pushInfo.setArrayIOS("[鍏ㄦ帹]"); - - pushInfo.setCreateTime(new Date()); - - try { - pushInfoService.save(pushInfo); - } catch (PushInfoException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - - try { - pushInfoService.handPush(pushInfo.getId()); - } catch (PushInfoException e) { - e.printStackTrace(); - } catch (PushException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - - } - -} +package com.yeshi.fanli.service.impl.order; + +import java.io.File; +import java.io.FileNotFoundException; +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 java.util.Scanner; + +import javax.annotation.Resource; + +import com.yeshi.fanli.entity.SystemEnum; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper; +import com.yeshi.fanli.dao.mybatis.money.ExtractCheckCompensateMapper; +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.dao.mybatis.order.OrderRepairHistoryMapper; +import com.yeshi.fanli.entity.bus.user.HongBaoV2; +import com.yeshi.fanli.entity.bus.user.UserInfo; +import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum; +import com.yeshi.fanli.entity.goods.CommonGoods; +import com.yeshi.fanli.entity.money.ExtractCheckCompensate; +import com.yeshi.fanli.entity.money.UserMoneyDetail; +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.push.PushInfo; +import com.yeshi.fanli.entity.push.PushInfo.PushTypeEnum; +import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief; +import com.yeshi.fanli.entity.taobao.TaoBaoOrder; +import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder; +import com.yeshi.fanli.exception.money.UserMoneyDetailException; +import com.yeshi.fanli.exception.order.HongBaoException; +import com.yeshi.fanli.exception.push.PushException; +import com.yeshi.fanli.exception.push.PushInfoException; +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.money.UserMoneyService; +import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService; +import com.yeshi.fanli.service.inter.order.CommonOrderService; +import com.yeshi.fanli.service.inter.order.HongBaoOrderService; +import com.yeshi.fanli.service.inter.order.HongBaoV2Service; +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.order.config.HongBaoManageService; +import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService; +import com.yeshi.fanli.service.inter.order.tb.TaoBaoWeiQuanOrderService; +import com.yeshi.fanli.service.inter.push.PushInfoService; +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 org.yeshi.utils.MoneyBigDecimalUtil; +import com.yeshi.fanli.util.StringUtil; +import org.yeshi.utils.TimeUtil; +import com.yeshi.fanli.util.factory.CommonOrderGoodsFactory; +import com.yeshi.fanli.util.factory.UserMoneyDetailFactory; +import com.yeshi.fanli.util.factory.goods.CommonOrderFactory; +import com.yeshi.fanli.util.taobao.TaoBaoUtil; +import com.yeshi.fanli.util.taobao.TaoKeApiUtil; + +import net.sf.json.JSONObject; + +@Service +public class OrderRepairServiceImpl implements OrderRepairService { + + @Resource + private OrderRepairHistoryMapper orderRepairHistoryMapper; + + @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; + + @Resource + private TaoBaoWeiQuanOrderService taoBaoWeiQuanOrderService; + + @Resource + private UserMoneyService userMoneyService; + + @Resource + private PushInfoService pushInfoService; + + @Resource + private UserMoneyMsgNotificationService userMoneyMsgNotificationService; + + @Resource + private ExtractCheckCompensateMapper extractCheckCompensateMapper; + + @Transactional(rollbackFor = Exception.class) + @Override + public void repairOrder(String orderId) throws Exception { + // 姣旇緝CommonOrder涓嶵aoBaoOrder鐨勯浼版敹鐩� + List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId); + // 缁熻 + BigDecimal sumMoney = new BigDecimal("0"); + for (TaoBaoOrder order : orderList) { + // if (order.getOrderState().equalsIgnoreCase("璁㈠崟浠樻") || + // order.getOrderState().equalsIgnoreCase("璁㈠崟鎴愬姛")) + // return; + if (order.getSubsidy() == null) + order.setSubsidy(new BigDecimal(0)); + if (order.geteIncome() == null) + order.seteIncome(new BigDecimal(0)); + + BigDecimal money = order.geteIncome().add(order.getSubsidy()); + sumMoney = sumMoney.add(money); + } + + List<CommonOrder> commonOrderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO, + orderId); + BigDecimal commonSumMoney = new BigDecimal("0"); + for (CommonOrder commonOrder : commonOrderList) { + commonSumMoney = commonSumMoney.add(commonOrder.geteIncome()); + } + + // 涓嶅鐞嗙淮鏉冭繃鐨勮鍗� + List<TaoBaoWeiQuanOrder> weiQuanOrderList = taoBaoWeiQuanOrderService.getWeiQuanSuccessOrders(orderId); + if (weiQuanOrderList != null && weiQuanOrderList.size() > 0) + return; + + processLessFanOrder(orderId); + + // if (sumMoney.compareTo(commonSumMoney) > 0) { + // // 灏戣繑浜� + // + // } else { + // + // } + + } + + private void processLessFanOrder(String orderId) throws Exception { + + List<CommonOrder> commonOrderList = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO, + orderId); + // 纭畾鏄嚜璐繕鏄垎浜禋 + if (commonOrderList.size() <= 0) + return; + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(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 = hongBaoOrderMapper.selectByCommonOrderId(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 = hongBaoV2Mapper.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())); + } + } + } + + // FileWriter fw = null; + // try { + // // 濡傛灉鏂囦欢瀛樺湪锛屽垯杩藉姞鍐呭锛涘鏋滄枃浠朵笉瀛樺湪锛屽垯鍒涘缓鏂囦欢 + // File f = new File("D:\\three_sale.txt"); + // fw = new FileWriter(f, true); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // PrintWriter pw = new PrintWriter(fw); + // pw.println(orderId+"#"+firstUid+"-"+oldMoney.get(firstUid)+"#"+secondUid+"-"+(secondUid!=null? + // oldMoney.get(secondUid):"")); + // pw.flush(); + // try { + // fw.flush(); + // pw.close(); + // fw.close(); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // + // if (1 > 0) + // return; + + 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 = CommonOrderFactory.create(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)// 鍒犻櫎澶氫綑鐨則radeId + // { + // 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()); + if (newHongBaoOrder == null) + continue; + 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())); + } else { + Long uid = hongBao.getUserInfo().getId(); + if (newMap.get(uid) == null) + newMap.put(uid, new BigDecimal(0)); + } + 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(String 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); + + } + + // 淇敼璁㈠崟淇℃伅 + private void updateOrderInfo(TaoBaoOrder taoBaoOrder, Long commonOrderId) throws Exception { + HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(commonOrderId); + CommonOrder newCommonOrder = CommonOrderFactory.create(taoBaoOrder); + + SystemEnum system=userInfoService.getUserSystem(newCommonOrder.getUserInfo().getId()); + + 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(UserLevelEnum.daRen, + commonOrder.getCreateTime().getTime(),system); + // 鍏嶅崟澶勭悊 + 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.getSourceType(), + 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(),commonOrder.getSourceType()); + } 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(),system); + } else if (child.getType() == HongBaoV2.TYPE_ERJI) {// 浜岀骇鍒嗕韩璧� + rate = hongBaoManageService.getSecondInviteRate(child.getUrank(), + commonOrder.getCreateTime().getTime(),system); + } + + // 浠ュ疄闄呮敹鍏ヤ负鍑嗚绠楅浼版敹鐩� + 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(UserLevelEnum.daRen, + commonOrder.getCreateTime().getTime(),system); + 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(),system); + } else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 浜岀骇鍒嗕韩璧� + rate = hongBaoManageService.getSecondShareRate(1, commonOrder.getCreateTime().getTime(),system); + } + + // 浠ュ疄闄呮敹鍏ヤ负鍑嗚绠楅浼版敹鐩� + 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); + } + } + } + + @Transactional(rollbackFor = Exception.class) + public void addOrder(CommonOrder commonOrder, int type, Long firstUid, Long secondUid) throws HongBaoException { + // 澧炲姞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); + } + + SystemEnum system=userInfoService.getUserSystem(commonOrder.getUserInfo().getId()); + + CommonOrder oldCommonOrder = commonOrderMapper.selectBySourceTypeAndTradeId(commonOrder.getSourceType(), + commonOrder.getTradeId()); + if (oldCommonOrder == null)// 鏂板 + { + commonOrder.setUpdateTime(new Date()); + commonOrderMapper.insertSelective(commonOrder); + try { + // 鎻掑叆鏄犲皠锛屼繚璇佷氦鏄揑D鐨勫畬鏁存�� + commonOrderTradeIdMapMapper.insertSelective(new CommonOrderTradeIdMap(commonOrder.getId(), + commonOrder.getTradeId(), new Date(), commonOrder.getSourceType())); + } catch (Exception e) { + + } + } + + // 娣诲姞绾㈠寘 + if (type == HongBaoV2.TYPE_ZIGOU) {// 鑾峰彇鑷喘鐨勮繑鍒╂瘮渚� + BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen, + commonOrder.getCreateTime().getTime(),system); + // 鏌ヨ鏄惁鏈夊厤鍗曡鍒� + 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, goods.getGoodsId(),commonOrder.getSourceType())) + 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(),commonOrder.getSourceType()); + } 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(HongBaoException.CODE_ORDER_WEIQUAN, "缁存潈璁㈠崟涓嶈兘鍒涘缓绾㈠寘"); + } + + if (mianDanMoney != null) + hongBao.setMoney(mianDanMoney); + + UserInfo user = userInfoService.getUserByIdWithMybatis(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绾OSS瀛樺湪涓旂孩鍖呮湭澶辨晥,鍏嶅崟涓嶆敮鎸佸绾у垎閿� + + // 鎻掑叆涓�绾у瓙绾㈠寘 + BigDecimal firstRate = hongBaoManageService + .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank(),system); + 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) {// || + // hongBao.getState() + // == + // HongBaoV2.STATE_YILINGQU + 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(),system); + 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) {// || + // hongBao.getState() + // == + // HongBaoV2.STATE_YILINGQU + 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(UserLevelEnum.daRen, + commonOrder.getCreateTime().getTime(),system); + + 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.getUserByIdWithMybatis(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(),system); + 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(HongBaoException.CODE_TYPE_ERROR, "type閿欒"); + + // 娣诲姞 + + } + + private void deleteByCommonOrderId(Long commonOrderId) { + commonOrderMapper.deleteByPrimaryKey(commonOrderId); + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrderId); + if (hongBaoOrder == null || hongBaoOrder.getHongBaoV2() == null) + return; + List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getHongBaoV2().getId()); + if (children != null) + for (HongBaoV2 hongBao : children) { + hongBaoV2Mapper.deleteByPrimaryKey(hongBao.getId()); + } + hongBaoV2Mapper.deleteByPrimaryKey(hongBaoOrder.getHongBaoV2().getId()); + hongBaoOrderMapper.deleteByPrimaryKey(hongBaoOrder.getId()); + + } + + @Transactional + @Override + public void testTransaction() { + hongBaoV2Mapper.selectByPrimaryKey(1L); + } + + private static Map<String, OrderInfo> orderInfoMap; + + private OrderInfo getOrderInfo(String orderId) { + if (orderInfoMap == null) { + orderInfoMap = new HashMap<>(); + Scanner scanner = null; + try { + scanner = new Scanner(new File("C:\\Users\\Administrator\\Desktop\\璁㈠崟闆嗕腑鎺掓煡\\three_sale.txt")); + while (scanner.hasNext()) { + OrderInfo orderInfo = new OrderInfo(); + String info = scanner.next(); + if (StringUtil.isNullOrEmpty(info)) + continue; + + String[] sts = info.split("#"); + String orderid = sts[0]; + orderInfo.setOrderId(orderid); + String firstUid = sts[1].split("-")[0]; + String firstUidMoney = sts[1].split("-")[1]; + orderInfo.setFirstUid(Long.parseLong(firstUid)); + orderInfo.setFirstUidMoney(new BigDecimal(firstUidMoney)); + + String secondUid = sts[2].split("-")[0]; + String secondUidMoney = null; + if (sts[2].split("-").length > 1) + secondUidMoney = sts[2].split("-")[1]; + if (!StringUtil.isNullOrEmpty(secondUid)) + orderInfo.setSecondUid(Long.parseLong(secondUid)); + if (!StringUtil.isNullOrEmpty(secondUidMoney)) + orderInfo.setSecondUidMoney(new BigDecimal(secondUidMoney)); + orderInfoMap.put(orderInfo.getOrderId(), orderInfo); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + scanner.close(); + } + + return orderInfoMap.get(orderId); + } + + @Transactional(rollbackFor = Exception.class) + @Override + public void repireFirstAndSecondLevel(String orderId) throws Exception { + repireFirstShare(orderId); + if (1 > 0) + return; + OrderInfo info = getOrderInfo(orderId); + Map<Long, BigDecimal> oldMoneyMap = new HashMap<>(); + if (info.getFirstUid() != null) + oldMoneyMap.put(info.getFirstUid(), info.getFirstUidMoney()); + if (info.getSecondUid() != null) + oldMoneyMap.put(info.getSecondUid(), info.getSecondUidMoney()); + + if (info != null) { + List<CommonOrder> list = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO, + info.getOrderId()); + // 鏌ヨ鏄惁鏈夊瓙绾㈠寘 + for (CommonOrder commonOrder : list) { + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); + if (hongBaoOrder == null) + continue; + List<HongBaoV2> hongBaoList = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getHongBaoV2().getId()); + if (hongBaoList != null && hongBaoList.size() > 0)// 鏈夊瓙绾㈠寘灏变笉澶勭悊浜� + return; + } + + + + for (CommonOrder commonOrder : list) { + SystemEnum system=userInfoService.getUserSystem(commonOrder.getUserInfo().getId()); + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); + if (hongBaoOrder == null) + continue; + HongBaoV2 hongBao = hongBaoOrder.getHongBaoV2(); + Long firstUid = info.getFirstUid(); + Long secondUid = info.getSecondUid(); + // 鍔犲叆瀛愮孩鍖� + UserInfo boss = null; + if (firstUid != null) + boss = userInfoService.selectByPKey(firstUid); + + if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1绾OSS瀛樺湪涓旂孩鍖呮湭澶辨晥,鍏嶅崟涓嶆敮鎸佸绾у垎閿� + // 鎻掑叆涓�绾у瓙绾㈠寘 + BigDecimal firstRate = hongBaoManageService + .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank(),system); + 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 + || hongBao.getState() == HongBaoV2.STATE_YILINGQU) { + 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) + continue; + + 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(),system); + 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 + || hongBao.getState() == HongBaoV2.STATE_YILINGQU) { + 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) + continue; + hongBaoV2Mapper.insertSelective(secondHongbao); + } + + } + } + } + + // 缁熻淇敼鍚庣殑璧勯噾 + 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()); + if (newHongBaoOrder == null) + continue; + HongBaoV2 hongBao = newHongBaoOrder.getHongBaoV2(); + 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 = oldMoneyMap.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 void repireFirstShare(String orderId) throws Exception { + OrderInfo info = getOrderInfo(orderId); + Long firstUid = info.getFirstUid(); + Map<Long, BigDecimal> oldMoneyMap = new HashMap<>(); + if (info.getFirstUid() != null) + oldMoneyMap.put(info.getFirstUid(), info.getFirstUidMoney()); + if (info.getSecondUid() != null) + oldMoneyMap.put(info.getSecondUid(), info.getSecondUidMoney()); + + if (info != null) { + List<CommonOrder> list = commonOrderMapper.listBySourceTypeAndOrderNo(Constant.SOURCE_TYPE_TAOBAO, + info.getOrderId()); + // 鏌ヨ鏄惁鏈夊瓙绾㈠寘 + for (CommonOrder commonOrder : list) { + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); + if (hongBaoOrder == null) + continue; + List<HongBaoV2> hongBaoList = hongBaoV2Mapper.listChildrenById(hongBaoOrder.getHongBaoV2().getId()); + if (hongBaoList != null && hongBaoList.size() > 0)// 鏈夊瓙绾㈠寘灏变笉澶勭悊浜� + return; + } + + for (CommonOrder commonOrder : list) { + if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ) + continue; + // 鍒嗕韩璧� + + HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId()); + if (hongBaoOrder == null) + continue; + + SystemEnum system=userInfoService.getUserSystem(commonOrder.getUserInfo().getId()); + + HongBaoV2 hongBao = hongBaoOrder.getHongBaoV2(); + // 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(),system); + 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); + } + } + } + + } + + // 缁熻淇敼鍚庣殑璧勯噾 + 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()); + if (newHongBaoOrder == null) + continue; + HongBaoV2 hongBao = newHongBaoOrder.getHongBaoV2(); + 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 = oldMoneyMap.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); + } + + } + + class OrderInfo { + private String orderId; + private Long firstUid; + private BigDecimal firstUidMoney; + private Long secondUid; + private BigDecimal secondUidMoney; + + public String getOrderId() { + return orderId; + } + + public void setOrderId(String orderId) { + this.orderId = orderId; + } + + public Long getFirstUid() { + return firstUid; + } + + public void setFirstUid(Long firstUid) { + this.firstUid = firstUid; + } + + public BigDecimal getFirstUidMoney() { + return firstUidMoney; + } + + public void setFirstUidMoney(BigDecimal firstUidMoney) { + this.firstUidMoney = firstUidMoney; + } + + public Long getSecondUid() { + return secondUid; + } + + public void setSecondUid(Long secondUid) { + this.secondUid = secondUid; + } + + public BigDecimal getSecondUidMoney() { + return secondUidMoney; + } + + public void setSecondUidMoney(BigDecimal secondUidMoney) { + this.secondUidMoney = secondUidMoney; + } + } + + @Override + public List<OrderRepairHistory> listByUid(Long uid) { + + return orderRepairHistoryMapper.listByUid(uid); + } + + @Transactional + @Override + public void doMoney(Long uid) { + List<OrderRepairHistory> historyList = listByUid(uid); + for (int i = 0; i < historyList.size(); i++) { + if (historyList.get(i).getCreateTime().getTime() < TimeUtil.convertToTimeTemp("2019-07-15 17:00:38", + "yyyy-MM-dd HH:mm:ss")) { + historyList.remove(i); + i--; + } + + } + + BigDecimal money = new BigDecimal(0); + String orders = ""; + for (OrderRepairHistory history : historyList) { + money = money.add(history.getAfterGetMoney().subtract(history.getBeforeGetMoney())); + orders += history.getOrderId() + ","; + } + if (money.compareTo(new BigDecimal(0)) > 0)// 璧勯噾闇�瑕佸鍔� + { + try { + UserMoneyDetail detail = UserMoneyDetailFactory.createSystemEqualize("璁㈠崟缁熻寮傚父淇", money, + new UserInfo(uid)); + // 绯荤粺琛ラ綈 + // 鍔犺祫閲� + userMoneyService.addUserMoney(uid, money, detail); + pushMsg(uid, "鍏充簬杩戞湡璁㈠崟涓庤祫閲戝紓甯哥殑鎺掓煡涓庝慨澶嶇粨鏋滈�氱煡", + "灏婃暚鐨勭敤鎴凤紝缁忎粩缁嗘牳瀵规偍璐︽埛涓殑璁㈠崟锛屽彂鐜版偍璐︽埛涓湁鏈粺璁℃垨閿欒缁熻鐨勮鍗曪紝鎴戜滑宸茬粡涓烘偍琛ラ綈璧勯噾锛屽搴旇祫閲戝凡缁忓瓨鍏ユ偍鐨勮处鎴蜂綑棰濅腑锛屾暚璇锋煡鏀舵敞鏌ユ敹銆傛劅璋俊浠伙紝鏉挎牀蹇渷App缁堝皢鎴愪负鎮ㄦ渶淇′换鐨勮喘鐗╃渷閽卞姪鎵嬨��"); + userMoneyMsgNotificationService.systemEqualize(uid, "璁㈠崟缁熻寮傚父淇", money, userInfoService.getBalance(uid)); + } catch (UserMoneyDetailException e) { + e.printStackTrace(); + } + + } else {// 璧勯噾闇�瑕佸噺灏�,涓嶈鎵i挶 + pushMsg(uid, "鍏充簬杩戞湡璁㈠崟涓庤祫閲戝紓甯哥殑鎺掓煡涓庝慨澶嶇粨鏋滈�氱煡", + "灏婃暚鐨勭敤鎴凤紝缁忎粩缁嗘牳瀵规偍璐︽埛涓殑璁㈠崟锛屽彂鐜版偍璐︽埛涓湁閿欒缁熻鐨勮鍗曪紝鎴戜滑宸茬粡涓烘偍鎭㈠姝e父锛屾墍娑夊強璁㈠崟澶氱粺璁″嚭鏉ョ殑璧勯噾锛屼笉浼氳鎵i櫎锛屽嵆鎮ㄨ处鎴蜂腑鐨勮祫閲戝皢淇濇寔涓嶅彉锛屽凡鎻愮幇鐨勮祫閲戯紝涔熶笉浼氭敹鍥烇紝鏁鐭ユ檽銆傛劅璋俊浠伙紝鏉挎牀蹇渷App缁堝皢鎴愪负鎮ㄦ渶淇′换鐨勮喘鐗╃渷閽卞姪鎵嬨��"); + // userMoneyMsgNotificationService.systemEqualize(uid, "璁㈠崟缁熻寮傚父淇", + // money, userInfoService.getBalance(uid)); + // 鍔犲叆鎻愮幇瀹℃牳寮傚父淇濇姢涓� + ExtractCheckCompensate check = new ExtractCheckCompensate(); + check.setBeiZhu("7鏈堣鍗曞紓甯哥郴缁熷鐞�:" + orders); + check.setCreateTime(new Date()); + check.setMoney(money); + check.setUserInfo(new UserInfo(uid)); + check.setUpdateTime(new Date()); + extractCheckCompensateMapper.insertSelective(check); + } + } + + private void pushMsg(Long uid, String title, String content) { + + PushInfo pushInfo = new PushInfo(); + pushInfo.setTitle(title); + pushInfo.setContent(content); + pushInfo.setUids(uid + ""); + JSONObject jsonData = new JSONObject(); + jsonData.put("url", ""); + jsonData.put("ios", "鍏ㄦ帹"); + jsonData.put("android", "鍏ㄦ帹"); + pushInfo.setJsonData(jsonData.toString()); + pushInfo.setType(PushTypeEnum.ZNX); + pushInfo.setArrayAndroid("[鍏ㄦ帹]"); + pushInfo.setArrayIOS("[鍏ㄦ帹]"); + + pushInfo.setCreateTime(new Date()); + + try { + pushInfoService.save(pushInfo); + } catch (PushInfoException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + try { + pushInfoService.handPush(pushInfo.getId()); + } catch (PushInfoException e) { + e.printStackTrace(); + } catch (PushException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} -- Gitblit v1.8.0