From 2442c923fd35ef864be1cfe07e039eebfaf24ffc Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期二, 03 十二月 2019 11:48:21 +0800
Subject: [PATCH] 新人红包功能

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinNewbiesServiceImpl.java |  186 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 182 insertions(+), 4 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..694c0fd 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,31 +1,48 @@
 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 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.UserInfoExtra;
 import com.yeshi.fanli.entity.push.DeviceActive;
+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.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.annotation.RequestSerializableByKeyService;
+import com.yeshi.fanli.util.taobao.TaoBaoUtil;
+import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 import com.yeshi.fanli.vo.redpack.TaoLiJinWinDetailVO;
 
 @Service
@@ -48,6 +65,18 @@
 	
 	@Resource
 	private JumpDetailV2Service jumpDetailV2Service;
+	
+	@Resource(name = "taskExecutor")
+	private TaskExecutor executor;
+	
+	@Resource
+	private TLJFreeBuyGoodsService tljFreeBuyGoodsService;
+	
+	@Resource
+	private UserTaoLiJinDetailService userTaoLiJinDetailService;
+	
+	@Resource
+	private TaoBaoBuyRelationMapService taoBaoBuyRelationMapService;
 	
 	
 	@Override
@@ -152,17 +181,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);
@@ -177,6 +207,16 @@
 			num = redPackWin.getNum() + 1;
 			balance = MoneyBigDecimalUtil.add(money, redPackWin.getMoney());
 		}
+		
+		// 鏄庣粏
+		UserTaoLiJinDetail detail = new UserTaoLiJinDetail();
+		detail.setUid(uid);
+		detail.setMoney(money);
+		detail.setTitle(TaoLiJinDetailTypeEnum.add.getDesc());
+		detail.setType(TaoLiJinDetailTypeEnum.add);
+		detail.setCreateTime(new Date());
+		userTaoLiJinDetailService.insertSelective(detail);
+		
 		
 		Map<String,String> map = new HashMap<String,String>();
 		map.put("url", configTaoLiJinService.getValueByKey("new_user_free_buy_list"));
@@ -194,10 +234,148 @@
 		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,"鍙傛暟淇℃伅缂哄け");
+		
+		if (goods == null || goods.getCouponAmount() == null || 
+				goods.getCouponAmount().compareTo(new BigDecimal(0)) == 0) {
+			executor.execute(new Runnable() {
+				@Override
+				public void run() {
+					tljFreeBuyGoodsService.deleteByGoodsId(goods.getId());
+				}
+			});
+			throw new UserTaoLiJinNewbiesException(1, "璇ュ晢鍝侀潪鍏嶅崟鍟嗗搧");
+		}
+		
+		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.getId());
+				}
+			});
+			throw new UserTaoLiJinNewbiesException(1, "璇ュ晢鍝侀潪鍏嶅崟鍟嗗搧");
+		}
+		
+		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
+		if (userInfoExtra == null || userInfoExtra.getFirstLoginTime() == null)
+			throw new UserTaoLiJinNewbiesException(1001, "浣犱笉鏄�滄柊鐢ㄦ埛鈥濇棤娉曚娇鐢ㄦ柊浜虹孩鍖�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());
+		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
+	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);
 	}
-	
 
 }

--
Gitblit v1.8.0