From 972d3bc71115ec977dccf835fbcd148da3bcc86c Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期二, 03 九月 2019 16:41:49 +0800
Subject: [PATCH] 统计可用的免单券数量-countUsableFreeCouponForBuy

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralTaskRecordServiceImpl.java |  376 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 312 insertions(+), 64 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralTaskRecordServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralTaskRecordServiceImpl.java
index 074b27b..540e7a3 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralTaskRecordServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralTaskRecordServiceImpl.java
@@ -1,7 +1,8 @@
 package com.yeshi.fanli.service.impl.integral;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Set;
@@ -13,33 +14,63 @@
 import org.yeshi.utils.DateUtil;
 
 import com.yeshi.fanli.dao.mybatis.integral.IntegralTaskRecordMapper;
+import com.yeshi.fanli.dto.msg.MsgOtherRewardIntegralDTO;
 import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
+import com.yeshi.fanli.entity.integral.IntegralDetail;
+import com.yeshi.fanli.entity.integral.IntegralTask;
+import com.yeshi.fanli.entity.integral.IntegralTask.FrequencyEnum;
+import com.yeshi.fanli.entity.integral.IntegralTaskClass;
+import com.yeshi.fanli.entity.integral.IntegralTaskClass.UniqueKeyEnum;
 import com.yeshi.fanli.entity.integral.IntegralTaskRecord;
 import com.yeshi.fanli.exception.integral.IntegralTaskRecordException;
 import com.yeshi.fanli.exception.user.UserInfoExtraException;
 import com.yeshi.fanli.log.LogHelper;
+import com.yeshi.fanli.service.inter.config.ConfigService;
+import com.yeshi.fanli.service.inter.integral.IntegralDetailService;
+import com.yeshi.fanli.service.inter.integral.IntegralTaskClassService;
 import com.yeshi.fanli.service.inter.integral.IntegralTaskRecordService;
+import com.yeshi.fanli.service.inter.integral.IntegralTaskService;
+import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
 import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
+import com.yeshi.fanli.util.StringUtil;
+import com.yeshi.fanli.util.TimeUtil;
 
 @Service
 public class IntegralTaskRecordServiceImpl implements IntegralTaskRecordService {
 
 	@Resource
 	private IntegralTaskRecordMapper integralTaskRecordMapper;
-	
+
 	@Resource
 	private UserInfoExtraService userInfoExtraService;
+
+	@Resource
+	private IntegralDetailService integralDetailService;
+
+	@Resource
+	private IntegralTaskService integralTaskService;
+
+	@Resource
+	private IntegralTaskClassService integralTaskClassService;
 	
+	@Resource
+	private UserOtherMsgNotificationService userOtherMsgNotificationService;
+	
+	@Resource
+	private ConfigService configService;
+	
+	
+
 	@Override
 	public Integer getTotalGoldCoin(long uid, Long cid, Integer dateType) {
 		return integralTaskRecordMapper.getTotalGoldCoin(uid, cid, dateType);
 	}
-	
+
 	@Override
 	public Integer countFinished(long uid, Long cid, Integer dateType) {
 		return integralTaskRecordMapper.countFinished(uid, cid, dateType);
 	}
-	
+
 	@Override
 	public void finishedTask(long uid, long cid, long taskId, int goldCoin) {
 		IntegralTaskRecord taskRecord = new IntegralTaskRecord();
@@ -52,16 +83,16 @@
 		taskRecord.setUpdateTime(new Date());
 		integralTaskRecordMapper.insertSelective(taskRecord);
 	}
-	
+
 	@Override
 	public boolean isToDaySign(long uid, Long cid) {
-		 Integer countFinished = integralTaskRecordMapper.countFinished(uid, cid, 1);
-		 if (countFinished != null && countFinished > 0) {
-			 return true;
-		 }
-		 return false;
+		Integer countFinished = integralTaskRecordMapper.countFinished(uid, cid, 1);
+		if (countFinished != null && countFinished > 0) {
+			return true;
+		}
+		return false;
 	}
-	
+
 	@Override
 	public Integer getNowdaySignNum(long uid, Long cid) {
 		int num = 1;
@@ -69,96 +100,86 @@
 			// 绛惧埌鏃堕棿鍊掑簭鏌ヨ杩�7澶╂暟鎹�
 			List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
 			if (list != null && list.size() > 0) {
-				Date today =new Date();
-				for (IntegralTaskRecord record: list) {
-					if (DateUtil.daysBetween2(record.getCreateTime(),today) != 1) {
+				Date today = new Date();
+				for (IntegralTaskRecord record : list) {
+					if (DateUtil.daysBetween2(record.getCreateTime(), today) == 0) {
+						continue;
+					}
+
+					if (DateUtil.daysBetween2(record.getCreateTime(), today) != 1) {
 						break; // 澶╂暟鏈繛缁�
 					}
-					num ++ ;
+					today = record.getCreateTime();
+					num++;
 				}
 			}
 		} catch (Exception e) {
 			LogHelper.errorDetailInfo(e);
 		}
-		
+
 		if (num > 7) {
-			num = 1;
+			num = 7;
 		}
 		return num;
 	}
-	
-	
+
 	@Override
-	public List<Date> getSignDays(long uid, Long cid) {
-		List<Date> listDate = new ArrayList<Date>();
+	public List<IntegralTaskRecord> getSignDaysRecord(long uid, Long cid) {
+		List<IntegralTaskRecord> listRecord = new ArrayList<IntegralTaskRecord>();
 		try {
 			// 绛惧埌鏃堕棿姝e簭搴忔煡璇㈣繎7澶╂暟鎹�
 			List<IntegralTaskRecord> list = integralTaskRecordMapper.listDaySignRecord(uid, cid);
 			if (list != null && list.size() > 0) {
 				Date date = new Date();
-				for (IntegralTaskRecord record: list) {
+				for (IntegralTaskRecord record : list) {
 					Date createTime = record.getCreateTime();
-					if (DateUtil.daysBetween2(createTime,date) > 1) {
+					if (DateUtil.daysBetween2(createTime, date) > 1) {
 						break; // 澶╂暟鏈繛缁�
 					}
 					date = createTime;
-					listDate.add(record.getCreateTime());
+					listRecord.add(record);
 				}
 			}
 		} catch (Exception e) {
 			LogHelper.errorDetailInfo(e);
 		}
-		
-		// 鍊掑簭
-	    Collections.reverse(listDate);
-		return listDate;
+		return listRecord;
 	}
-	
+
 	@Override
 	public List<IntegralTaskRecord> listNotReceived(int count, long uid) {
 		return integralTaskRecordMapper.listNotReceived(count, uid);
 	}
-	
-	
+
+	@Override
+	public List<IntegralTaskRecord> listNotReceivedExcludeId(int count, long uid, Set<Long> idList) {
+		if (idList != null && idList.size() == 0) {
+			idList = null;
+		}
+		return integralTaskRecordMapper.listNotReceivedExcludeId(count, uid, idList);
+	}
+
 	@Override
 	@Transactional
-	public Integer receiveGoldCoin(Long uid, Set<Long> idList) throws IntegralTaskRecordException	{
-		if(idList == null || idList.size() == 0) {
-			throw new IntegralTaskRecordException(1, "id涓虹┖");
-		}
-		
+	public Integer receiveGoldCoinALL(Long uid) throws IntegralTaskRecordException {
 		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
-		if (userInfoExtra == null) {
+		if (userInfoExtra == null)
 			throw new IntegralTaskRecordException(1, "鐢ㄦ埛淇℃伅涓嶅叏");
-		}
-		
+
+		List<IntegralTaskRecord> list = integralTaskRecordMapper.listNotReceived(Integer.MAX_VALUE, uid);
+		if (list == null || list.size() == 0)
+			throw new IntegralTaskRecordException(1, "鏆傛棤鍙鍙�");
+
 		Integer totalGoldCoin = 0;
-		for (Long id: idList) {
-			IntegralTaskRecord record = integralTaskRecordMapper.selectByPrimaryKey(id);
-			if (record == null) 
-				continue;
-			
-			Long uid2 = record.getUid();
-			if (uid2 == null || uid.longValue() != uid2.longValue())
-				continue; // 鐢ㄦ埛id涓嶇鍚�
-			
-			if (record.getState() != null && record.getState().intValue() == 1) {
-				continue; // 宸查鍙�
-			}
-			
-			IntegralTaskRecord taskRecord = new IntegralTaskRecord();
-			taskRecord.setId(record.getId());
-			taskRecord.setState(1); // 宸查鍙�
-			taskRecord.setUpdateTime(new Date());
-			integralTaskRecordMapper.updateByPrimaryKeySelective(taskRecord);
-			
-			totalGoldCoin += record.getGoldCoin();
+		for (IntegralTaskRecord record : list) {
+			if (addDetail(uid, record))
+				totalGoldCoin += record.getGoldCoin();
 		}
-		
+
 		Integer goldCoin = userInfoExtra.getGoldCoin();
-		if (goldCoin == null) 
+		if (goldCoin == null)
 			goldCoin = 0;
-		
+
 		UserInfoExtra extra = new UserInfoExtra();
 		extra.setId(userInfoExtra.getId());
 		extra.setGoldCoin(goldCoin + totalGoldCoin);
@@ -167,8 +188,235 @@
 		} catch (UserInfoExtraException e) {
 			throw new IntegralTaskRecordException(1, "鐢ㄦ埛淇℃伅涓嶅叏");
 		}
-		
+
 		return extra.getGoldCoin();
 	}
-}
 
+	@Override
+	@Transactional
+	public Integer receiveGoldCoin(Long uid, Set<Long> idList) throws IntegralTaskRecordException {
+		if (idList == null || idList.size() == 0) {
+			throw new IntegralTaskRecordException(1, "id涓虹┖");
+		}
+
+		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
+		if (userInfoExtra == null) {
+			throw new IntegralTaskRecordException(1, "鐢ㄦ埛淇℃伅涓嶅叏");
+		}
+ 
+		Integer totalGoldCoin = 0;
+		for (Long id : idList) {
+			IntegralTaskRecord record = integralTaskRecordMapper.selectByPrimaryKey(id);
+			if (record == null)
+				continue;
+			
+			// 宸查鍙�
+			Integer state = record.getState();
+			if (state != null && state == 1) 
+				continue;
+			
+			if (addDetail(uid, record))
+				totalGoldCoin += record.getGoldCoin();
+		}
+
+		Integer goldCoin = userInfoExtra.getGoldCoin();
+		if (goldCoin == null)
+			goldCoin = 0;
+
+		UserInfoExtra extra = new UserInfoExtra();
+		extra.setId(userInfoExtra.getId());
+		extra.setGoldCoin(goldCoin + totalGoldCoin);
+		try {
+			userInfoExtraService.saveUserInfoExtra(extra);
+		} catch (UserInfoExtraException e) {
+			throw new IntegralTaskRecordException(1, "鐢ㄦ埛淇℃伅涓嶅叏");
+		}
+
+		return extra.getGoldCoin();
+	}
+
+	public boolean addDetail(Long uid, IntegralTaskRecord record) throws IntegralTaskRecordException {
+
+		Long uid2 = record.getUid();
+		if (uid2 == null || uid.longValue() != uid2.longValue())
+			return false; // 鐢ㄦ埛id涓嶇鍚�
+
+		if (record.getState() != null && record.getState().intValue() == 1) {
+			return false; // 宸查鍙�
+		}
+
+		IntegralTaskRecord taskRecord = new IntegralTaskRecord();
+		taskRecord.setId(record.getId());
+		taskRecord.setState(1); // 宸查鍙�
+		taskRecord.setUpdateTime(new Date());
+		integralTaskRecordMapper.updateByPrimaryKeySelective(taskRecord);
+
+		Long cid = record.getCid();
+		if (cid == null)
+			return false;
+
+		IntegralTaskClass taskClass = integralTaskClassService.selectByPrimaryKey(cid);
+		if (taskClass == null)
+			return false;
+
+		Long taskId = record.getTaskId();
+		if (taskId == null)
+			return false;
+
+		IntegralTask integralTask = integralTaskService.selectByPrimaryKey(taskId);
+		if (integralTask == null)
+			return false;
+
+		// 鍔犲叆鏄庣粏
+		IntegralDetail detail = new IntegralDetail();
+		if (UniqueKeyEnum.dailySign == taskClass.getUniqueKey()) {
+			SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
+			Date recordTime = record.getCreateTime();
+			List<IntegralTaskRecord> list = integralTaskRecordMapper.listSignRecordByDateTime(uid, cid,
+					format.format(recordTime));
+			int num = 1;
+			if (list != null && list.size() > 0) {
+				try {
+					Date nextDate = null;
+					for (IntegralTaskRecord tntegralTaskRecord : list) {
+						if (nextDate == null) {
+							nextDate = tntegralTaskRecord.getCreateTime();
+							continue;
+						}
+
+						if (DateUtil.daysBetween2(tntegralTaskRecord.getCreateTime(), nextDate) != 1) {
+							break; // 澶╂暟鏈繛缁�
+						}
+						nextDate = tntegralTaskRecord.getCreateTime();
+						num++;
+					}
+				} catch (ParseException e) {
+					e.printStackTrace();
+				}
+			}
+			detail.setTitle(taskClass.getName() + "-绗�" + num + "澶�");
+		} else {
+			FrequencyEnum frequency = integralTask.getFrequency();
+			if (frequency == FrequencyEnum.everyday) {
+				int num = integralTaskRecordMapper.countByTaskIdTodayNum(uid, taskId,
+						TimeUtil.getWholeTime(record.getCreateTime().getTime()));
+				if (num <= 0)
+					num = 1;
+				String title = taskClass.getName() + "-" + integralTask.getName() + "-绗�" + num + "娆�";
+				detail.setTitle(title);
+			} else if (frequency == FrequencyEnum.onlyOne) {
+				String title = taskClass.getName() + "-" + integralTask.getName();
+				detail.setTitle(title);
+			}
+		}
+
+		detail.setUid(uid);
+		detail.setMoney(record.getGoldCoin());
+		detail.setCreateTime(record.getCreateTime());
+		integralDetailService.insertSelective(detail);
+
+		return true;
+	}
+
+	@Override
+	public int countGetCountByTaskIdAndDay(Long taskId, Long uid, Date day) {
+		Date minTime = null;
+		Date maxTime = null;
+		if (day == null) {
+			minTime = new Date(TimeUtil.convertToTimeTemp("1970-01-01", "yyyy-MM-dd"));
+			maxTime = new Date(TimeUtil.convertToTimeTemp("2100-01-01", "yyyy-MM-dd"));
+		} else {
+			minTime = new Date(
+					TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(day.getTime(), "yyyy-MM-dd"), "yyyy-MM-dd"));
+			maxTime = new Date(minTime.getTime() + 1000 * 60 * 60 * 24L);
+		}
+		return integralTaskRecordMapper.countGetCountByTaskIdAndDay(taskId, uid, minTime, maxTime);
+	}
+
+	@Override
+	public IntegralTaskRecord addRecord(IntegralTaskRecord record) throws IntegralTaskRecordException {
+		if (record == null)
+			throw new IntegralTaskRecordException(1, "璁板綍涓虹┖");
+
+		if (record.getCid() == null || record.getTaskId() == null || record.getGoldCoin() == null
+				|| record.getState() == null || record.getUid() == null)
+			throw new IntegralTaskRecordException(2, "鏁版嵁涓嶅畬鎴�");
+
+		if (record.getCreateTime() == null)
+			record.setCreateTime(new Date());
+
+		if (record.getUpdateTime() == null)
+			record.setUpdateTime(new Date());
+		integralTaskRecordMapper.insertSelective(record);
+		return record;
+	}
+
+	@Override
+	public List<IntegralTaskRecord> listByCidAndUidAndCreateTime(Long cid, Long uid, Date minTime, Date maxTime,
+			int page, int count) {
+		return integralTaskRecordMapper.listByCidAndUidAndCreateTime(cid, uid, minTime, maxTime, (page - 1) * count,
+				count);
+	}
+
+	@Override
+	public long countByCidAndUidAndCreateTime(Long cid, Long uid, Date minTime, Date maxTime) {
+		return integralTaskRecordMapper.countByCidAndUidAndCreateTime(cid, uid, minTime, maxTime);
+	}
+
+	@Override
+	public List<IntegralTaskRecord> listByUidAndTaskId(Long uid, Long taskId, int page, int count) {
+		return integralTaskRecordMapper.listByUidAndTaskId(uid, taskId, (page - 1) * count, count);
+	}
+	
+	/**
+	 * 琚個璇蜂汉涓嬩簡棣栧崟锛堢涓�绗旇繑鍒╄鍗曪級 鐩存帴缁欓個璇蜂汉涓�绗旈噾甯�
+	 * @param uid
+	 */
+	@Override
+	public void firstOrderRewardInviter(Long uid, String beizu) {
+		UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
+		if (userInfoExtra == null) 
+			return;
+		
+		String reward = configService.get("first_order_reward_boss");
+		if (StringUtil.isNullOrEmpty(reward)) 
+			return;
+		
+		Integer goldCoin = userInfoExtra.getGoldCoin();
+		if (goldCoin == null)
+			goldCoin = 0;
+		
+		int addGoldCoin = Integer.parseInt(reward);
+		goldCoin = goldCoin + addGoldCoin;
+		
+		try {
+			// 澧炲姞閲戝竵
+			UserInfoExtra extra = new UserInfoExtra();
+			extra.setId(userInfoExtra.getId());
+			extra.setGoldCoin(goldCoin);
+			userInfoExtraService.saveUserInfoExtra(extra);
+		} catch (UserInfoExtraException e) {
+			e.printStackTrace();
+			return;
+		}
+
+		// 閲戝竵璇︽儏
+		IntegralDetail detail = new IntegralDetail();
+		detail.setTitle("闃熷憳棣栫瑪濂栧姳");
+		detail.setUid(uid);
+		detail.setMoney(addGoldCoin);
+		detail.setRemark(beizu);
+		detail.setCreateTime(new Date());
+		integralDetailService.insertSelective(detail);
+		
+		// 娑堟伅
+		MsgOtherRewardIntegralDTO msgOther = new MsgOtherRewardIntegralDTO();
+		msgOther.setTitle("閲戝竵濂栧姳");
+		msgOther.setSource("涓�绾ч槦鍛橀绗斾笅鍗曪紝绯荤粺濂栧姳閭�璇蜂汉閲戝竵");
+		msgOther.setNum(addGoldCoin + "閲戝竵");
+		msgOther.setTotal(goldCoin + "閲戝竵");
+		msgOther.setExplain("浠呬竴绾ч槦鍛橀绗斾笅鍗曞鍔�");
+		userOtherMsgNotificationService.firstOrderRewardMsg(uid, "璁㈠崟宸插埌璐﹀悗绯荤粺浼氳嚜鍔ㄥ鍔�", msgOther);
+	}
+	
+}

--
Gitblit v1.8.0