From 28cf328a098334b51a3e9d2d56f983fb8c862211 Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期六, 23 五月 2020 09:54:38 +0800
Subject: [PATCH] 足迹、收藏订单兼容新需求

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinNewbiesServiceImpl.java |  237 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 227 insertions(+), 10 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinNewbiesServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinNewbiesServiceImpl.java
index f3955eb..efb2880 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinNewbiesServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinNewbiesServiceImpl.java
@@ -1,32 +1,54 @@
 package com.yeshi.fanli.service.impl.tlj;
 
 import java.math.BigDecimal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.List;
 
 import javax.annotation.Resource;
 
+import org.springframework.core.task.TaskExecutor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.yeshi.utils.DateUtil;
 
 import com.yeshi.fanli.dao.mybatis.tlj.UserTaoLiJinNewbiesMapper;
+import com.yeshi.fanli.dto.taobao.TaoLiJinDTO;
+import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinDetail;
+import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinDetail.TaoLiJinDetailTypeEnum;
 import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinNewbies;
+import com.yeshi.fanli.entity.bus.user.UserInfo;
 import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
 import com.yeshi.fanli.entity.push.DeviceActive;
+import com.yeshi.fanli.entity.taobao.TLJFreeBuyGoods;
+import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
+import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
+import com.yeshi.fanli.exception.taobao.TaoKeApiException;
+import com.yeshi.fanli.exception.tlj.TaoLiJinCreateException;
+import com.yeshi.fanli.exception.tlj.UserTaoLiJinNewbiesException;
 import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
 import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
 import com.yeshi.fanli.service.inter.push.DeviceActiveService;
 import com.yeshi.fanli.service.inter.redpack.UserTaoLiJinNewbiesService;
+import com.yeshi.fanli.service.inter.taobao.TLJFreeBuyGoodsService;
+import com.yeshi.fanli.service.inter.taobao.TaoBaoBuyRelationMapService;
 import com.yeshi.fanli.service.inter.tlj.ConfigTaoLiJinService;
+import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinDetailService;
 import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
+import com.yeshi.fanli.service.inter.user.UserInfoService;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.MoneyBigDecimalUtil;
 import com.yeshi.fanli.util.StringUtil;
+import com.yeshi.fanli.util.TaoBaoConstant;
+import com.yeshi.fanli.util.TimeUtil;
 import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService;
+import com.yeshi.fanli.util.taobao.TaoBaoUtil;
+import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 import com.yeshi.fanli.vo.redpack.TaoLiJinWinDetailVO;
+
+import net.sf.json.JSONObject;
 
 @Service
 public class UserTaoLiJinNewbiesServiceImpl implements UserTaoLiJinNewbiesService {
@@ -36,6 +58,9 @@
 	
 	@Resource
 	private ConfigTaoLiJinService configTaoLiJinService;
+	
+	@Resource	
+	private UserInfoService userInfoService;
 	
 	@Resource	
 	private UserInfoExtraService userInfoExtraService;
@@ -48,6 +73,18 @@
 	
 	@Resource
 	private JumpDetailV2Service jumpDetailV2Service;
+	
+	@Resource(name = "taskExecutor")
+	private TaskExecutor executor;
+	
+	@Resource
+	private TLJFreeBuyGoodsService tljFreeBuyGoodsService;
+	
+	@Resource
+	private UserTaoLiJinDetailService userTaoLiJinDetailService;
+	
+	@Resource
+	private TaoBaoBuyRelationMapService taoBaoBuyRelationMapService;
 	
 	
 	@Override
@@ -110,6 +147,10 @@
 		if (uid == null || uid <= 0) 
 			return null; // 鏈櫥褰曞脊鍑�
 		
+		UserInfo userInfo = userInfoService.selectByPKey(uid);
+		if (userInfo == null)
+			return null;
+		
 		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
 		if (userInfoExtra == null || userInfoExtra.getFirstLoginTime() == null) 
 			return null;
@@ -152,17 +193,18 @@
 			BigDecimal rate = new BigDecimal(randomNum/100).setScale(2, BigDecimal.ROUND_DOWN);
 			money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.sub(Constant.TLJ_NEW_USER_MONEY,redPackWin.getMoney()),rate);
 		}
+		
+		if (money.compareTo(new BigDecimal(0)) <= 0)
+			return null;
+		
 		money = money.setScale(2);
 		
-		// 鎻掑叆棰嗗彇璁板綍
 		int num = 1;
 		BigDecimal balance = null;
 		if (redPackWin == null) {
 			balance = money;
-			
 			// 娉ㄥ唽寮�濮� 鏈夋晥鏈�
 			Date endDate = DateUtil.plusDayDate(Constant.TLJ_NEW_USER_LIMIT, firstLoginTime);
-			
 			redPackWin = new UserTaoLiJinNewbies();
 			redPackWin.setId(uid);
 			redPackWin.setNum(1);
@@ -178,8 +220,18 @@
 			balance = MoneyBigDecimalUtil.add(money, redPackWin.getMoney());
 		}
 		
-		Map<String,String> map = new HashMap<String,String>();
-		map.put("url", configTaoLiJinService.getValueByKey("new_user_free_buy_list"));
+		// 鏄庣粏
+		UserTaoLiJinDetail detail = new UserTaoLiJinDetail();
+		detail.setUid(uid);
+		detail.setMoney(money);
+		detail.setTitle(TaoLiJinDetailTypeEnum.add.getDesc());
+		detail.setType(TaoLiJinDetailTypeEnum.add);
+		detail.setCreateTime(new Date());
+		detail.setRemark("鏂颁汉绾㈠寘");
+		userTaoLiJinDetailService.insertSelective(detail);
+		
+		JSONObject json = new JSONObject();
+		json.put("url", configTaoLiJinService.getValueByKey("new_user_free_buy_list"));
 		
 		TaoLiJinWinDetailVO winDetailVO = new TaoLiJinWinDetailVO();
 		winDetailVO.setTitle("鏂颁汉绾㈠寘");
@@ -187,17 +239,182 @@
 		winDetailVO.setMoney(money.toString());
 		winDetailVO.setDesc("宸查鍙栵細" + balance + "鍏�");
 		winDetailVO.setTips(configTaoLiJinService.getValueByKey("new_user_win_tips"));
-		winDetailVO.setParams(map.toString());
+		winDetailVO.setParams(json.toString());
 		winDetailVO.setJumpDetail(jumpDetailV2Service.getByTypeCache("web"));
 		winDetailVO.setLogo(Constant.systemCommonConfig.getDefaultPortrait());
-		winDetailVO.setLogoLight(Constant.systemCommonConfig.getDefaultPortrait());
+		winDetailVO.setLogoLight(userInfo.getPortrait());
 		return winDetailVO;
 	}
+	
+	
+	@RequestSerializableByKeyService(key = "#uid")
+	@Transactional(rollbackFor = Exception.class)
+	@Override
+	public String createTaoLiJinForBuy(TaoBaoGoodsBrief goods, Long uid) throws UserTaoLiJinNewbiesException {
+		if (uid == null || uid <= 0 || goods == null) 
+			throw new UserTaoLiJinNewbiesException(1,"鍙傛暟淇℃伅缂哄け");
+		
+		// 楠岃瘉鍟嗗搧鏄惁鍦ㄥ厤鍗曞晢鍝佸簱
+		TLJFreeBuyGoods tljFreeBuyGoods = tljFreeBuyGoodsService.selectByAuctionIdAndDay(goods.getAuctionId(),
+				TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd"));
+		if (tljFreeBuyGoods == null) 
+			throw new UserTaoLiJinNewbiesException(1, "璇ュ晢鍝侀潪鍏嶅崟鍟嗗搧");
+		
+		// 楠岃瘉鏄惁鏈夊埜
+		if (goods == null || goods.getCouponAmount() == null || 
+				goods.getCouponAmount().compareTo(new BigDecimal(0)) <= 0) {
+			executor.execute(new Runnable() {
+				@Override
+				public void run() {
+					tljFreeBuyGoodsService.deleteByGoodsId(goods.getAuctionId());
+				}
+			});
+			throw new UserTaoLiJinNewbiesException(1, "璇ュ晢鍝侀潪鍏嶅崟鍟嗗搧");
+		}
+		
+		// 楠岃瘉鍒稿悗浠� 鏄惁鍦� 1-2鍏�
+		BigDecimal money = TaoBaoUtil.getAfterUseCouplePrice(goods);
+		if (money.compareTo(new BigDecimal(1.0)) < 0 || money.compareTo(new BigDecimal(2.0)) > 0) {
+			executor.execute(new Runnable() {
+				@Override
+				public void run() {
+					tljFreeBuyGoodsService.deleteByGoodsId(goods.getAuctionId());
+				}
+			});
+			throw new UserTaoLiJinNewbiesException(1, "璇ュ晢鍝侀潪鍏嶅崟鍟嗗搧");
+		}
+		
+		// 楠岃瘉鐢ㄦ埛淇℃伅
+		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
+		if (userInfoExtra == null || userInfoExtra.getFirstLoginTime() == null)
+			throw new UserTaoLiJinNewbiesException(1001, "浣犱笉鏄�滄柊鐢ㄦ埛鈥濇棤娉曚娇鐢ㄦ柊浜虹孩鍖�0鍏冭喘涔般��");
+		
+		// 楠岃瘉鏄惁婵�娲�
+		if (StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode()))
+			throw new UserTaoLiJinNewbiesException(1002, "閭�璇风爜鏈縺娲伙紝鏃犳硶浣跨敤鏂颁汉绾㈠寘0鍏冭喘涔般��");
+		
+		// 楠岃瘉鏄惁宸茶秴鍑烘柊鐢ㄦ埛瀹氫箟
+		try {
+			int days = DateUtil.daysBetween2(userInfoExtra.getFirstLoginTime(), new Date());
+			if (days > Constant.TLJ_NEW_USER_DEFINE)
+				throw new UserTaoLiJinNewbiesException(1001, "浣犱笉鏄�滄柊鐢ㄦ埛鈥濇棤娉曚娇鐢ㄦ柊浜虹孩鍖�0鍏冭喘涔般��");
+		} catch (ParseException e1) {
+			throw new UserTaoLiJinNewbiesException(1, "鏃ユ湡淇℃伅璁$畻澶辫触");
+		}
+		
+		// 楠岃瘉浣欓
+		UserTaoLiJinNewbies taoLiJinNewbies = userTaoLiJinNewbiesMapper.selectForUpdate(uid);
+		if (taoLiJinNewbies == null || taoLiJinNewbies.getMoney() == null ||
+				taoLiJinNewbies.getMoney().compareTo(money)	< 0) 
+			throw new UserTaoLiJinNewbiesException(1001, "鏂颁汉绾㈠寘涓嶈冻锛岃鎹釜鍟嗗搧鍐嶈瘯鎴栫户缁鍙栧埌瓒抽鏂颁汉绾㈠寘銆�");
+		
+		// 楠岃瘉鏄惁宸茶繃鏈�
+		if (taoLiJinNewbies.getEndTime() == null 
+				|| taoLiJinNewbies.getEndTime().getTime() < java.lang.System.currentTimeMillis())
+			throw new UserTaoLiJinNewbiesException(1, "鏂颁汉绾㈠寘宸茶繃鏈�");
+		
+		// 绾㈠寘鍑忓皯
+		userTaoLiJinNewbiesMapper.subMoney(uid, money);
+		
+		// 鍔犲叆鏄庣粏
+		UserTaoLiJinDetail detail = new UserTaoLiJinDetail();
+		detail.setUid(uid);
+		detail.setMoney(new BigDecimal("-" + money));
+		detail.setTitle(TaoLiJinDetailTypeEnum.reduce.getDesc());
+		detail.setType(TaoLiJinDetailTypeEnum.reduce);
+		detail.setCreateTime(new Date());
+		detail.setRemark("鏂颁汉鍏嶅崟浣跨敤绾㈠寘");
+		userTaoLiJinDetailService.insertSelective(detail);
+		
+		// 鍒涘缓娣樼ぜ閲戦摼鎺�
+		if (TaoBaoUtil.isSpecialGoods(goods.getMaterialLibType())) {
+			return createTaoLiJin(goods.getAuctionId(), money, 1, TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT);
+		} else { // 闈炶繑鍒╁簱鍟嗗搧
+			String relationId = null;
+			try {
+				relationId = taoBaoBuyRelationMapService.getRelationId(uid);
+			} catch (Exception e) {
+				LogHelper.errorDetailInfo(e, "uid:" + uid, null);
+			}
+
+			if (StringUtil.isNullOrEmpty(relationId))
+				throw new UserTaoLiJinNewbiesException(1, "娓犻亾鏈妗�");
+			
+			return createTaoLiJin(goods.getAuctionId(), money, 1, TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID)
+					+ "&relationId=" + relationId;
+		}
+	}
+	
+	
+	@Transactional(rollbackFor=Exception.class)
+	private String createTaoLiJin(Long auctionId, BigDecimal perface, int totalNum, String pid) throws UserTaoLiJinNewbiesException {
+		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+		SimpleDateFormat formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+		
+		String name = "鏂颁汉鍏嶅崟璐�";
+		Date date = new Date();
+		String date_str = format.format(date);
+		String sendEndTime_str = date_str + " 22:00:00";
+		String useEndTime_str = date_str + " 23:59:59";
+		
+		// 鍙戞斁鏃堕棿
+		Date sendStartTime = date;
+		Date sendEndTime = null;
+		
+		// 浣跨敤鏃堕棿
+		Date useStartTime = null;
+		Date useEndTime = null;
+		try {
+			sendEndTime = formatStr.parse(sendEndTime_str);
+			useEndTime = formatStr.parse(useEndTime_str);
+		} catch (ParseException e2) {
+			e2.printStackTrace();
+		}
+	
+		// 鍒涘缓娣樼ぜ閲戠孩鍖�
+		TaoLiJinDTO taoLiJinDTO = null;
+		TaoKeAppInfo taoKeAppInfo = new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET, pid);
+		try {
+			taoLiJinDTO = TaoKeApiUtil.createTaoLiJin(auctionId, name, perface, totalNum, sendStartTime,
+					sendEndTime, useStartTime, useEndTime, taoKeAppInfo);
+		} catch (TaoLiJinCreateException e) {
+			LogHelper.errorDetailInfo(e);
+			executor.execute(new Runnable() {
+				@Override
+				public void run() {
+					if (e.getCode() == TaoLiJinCreateException.CODE_TLJ_FORBIDDEN) {
+						// 璇ュ晢鍝佷笉鏀寔鍒涘缓娣樼ぜ閲戠孩鍖�
+						tljFreeBuyGoodsService.deleteByGoodsId(auctionId);
+					} else if (e.getCode() == TaoLiJinCreateException.CODE_TLJ_NO_MONEY) {
+						// 瀹樻柟鐜╂硶閽卞寘浣欓涓嶈冻 TODO
+					}
+				}
+			});
+		} catch (TaoKeApiException e) {
+			LogHelper.errorDetailInfo(e);
+		}
+
+		if (taoLiJinDTO == null || StringUtil.isNullOrEmpty(taoLiJinDTO.getSendUrl()))
+			throw new UserTaoLiJinNewbiesException(1, "娣樺疂API绾㈠寘鍒涘缓澶辫触");
+		
+		return taoLiJinDTO.getSendUrl();
+	}
+
+	
 	
 	@Override
 	public UserTaoLiJinNewbies selectByPrimaryKey(Long uid) {
 		return userTaoLiJinNewbiesMapper.selectByPrimaryKey(uid);
 	}
-	
 
+	
+	@Override
+	public long count(String key) {
+		return userTaoLiJinNewbiesMapper.count(key);
+	}
+	
+	@Override
+	public List<UserTaoLiJinNewbies> query(int page, int count, String key,Integer order) {
+		return userTaoLiJinNewbiesMapper.query((page -1)* count, count, key, order);
+	}
 }

--
Gitblit v1.8.0