From 9a24f2802a3d6fe3e82c05e52f2880d65ad04dcf Mon Sep 17 00:00:00 2001
From: yujian <yujian>
Date: 星期一, 29 四月 2019 08:59:47 +0800
Subject: [PATCH] Merge branch 'div_develop' into master_develop

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java |  262 ++++++++++++++++------------------------------------
 1 files changed, 82 insertions(+), 180 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java
index 5300256..93cef48 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java
@@ -1,6 +1,5 @@
 package com.yeshi.fanli.service.impl.hongbao;
 
-import java.io.Serializable;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -9,18 +8,11 @@
 
 import javax.annotation.Resource;
 
-import org.hibernate.HibernateException;
-import org.hibernate.Query;
-import org.hibernate.SQLQuery;
-import org.hibernate.Session;
-import org.springframework.orm.hibernate4.HibernateCallback;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.yeshi.utils.NumberUtil;
 
 import com.yeshi.fanli.dao.mybatis.ThreeSaleMapper;
 import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
-import com.yeshi.fanli.dao.user.ThreeSaleDao;
 import com.yeshi.fanli.entity.bus.user.ThreeSale;
 import com.yeshi.fanli.entity.bus.user.ThreeSaleExtraInfo;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
@@ -32,16 +24,13 @@
 import com.yeshi.fanli.service.inter.user.UserInfoService;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.StringUtil;
-import com.yeshi.fanli.util.ThreeSaleCMQManager;
+import com.yeshi.fanli.util.cmq.ThreeSaleCMQManager;
 
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 
 @Service
 public class ThreeSaleSerivceImpl implements ThreeSaleSerivce {
-
-	@Resource
-	private ThreeSaleDao dao;
 
 	@Resource
 	private HongBaoManageService hongBaoManageService;
@@ -62,35 +51,7 @@
 	private MsgInviteDetailService msgInviteDetailService;
 
 	public UserInfo getBoss(final long uid) {
-		return (UserInfo) dao.excute(new HibernateCallback<UserInfo>() {
-			public UserInfo doInHibernate(Session session) throws HibernateException {
-				Query query = session.createQuery("select ts.boss FROM ThreeSale ts where ts.worker.id = ?");
-				query.setParameter(0, uid);
-				List<UserInfo> list = query.list();
-				if (list.size() > 0) {
-					return list.get(0);
-				}
-				return null;
-			}
-		});
-	}
-
-	public int findInviteCount(final long id) {
-
-		return (Integer) dao.excute(new HibernateCallback<Integer>() {
-
-			public Integer doInHibernate(Session session) throws HibernateException {
-				SQLQuery query = session.createSQLQuery(
-						"SELECT COUNT(*) from `yeshi_ec_threesale` t1  RIGHT JOIN (SELECT t2.`worker_id` AS worker_id FROM `yeshi_ec_threesale` t2  WHERE t2.`boss_id`=? ) t3 ON t1.`boss_id`=t3.worker_id WHERE t1.`boss_id`=t3.worker_id ");
-				query.setParameter(0, id);
-				SQLQuery query2 = session
-						.createSQLQuery("SELECT COUNT(*) from `yeshi_ec_threesale` t4 WHERE t4.`boss_id`=?");
-				query2.setParameter(0, id);
-				int int1 = Integer.parseInt(query.uniqueResult() + "");
-				int int2 = Integer.parseInt(query2.uniqueResult() + "");
-				return int1 + int2;
-			}
-		});
+		return threeSaleMapper.selectBoss(uid);
 	}
 
 	@Transactional
@@ -254,134 +215,14 @@
 		}
 	}
 
-	@Transactional
-	public void validateTwentyFour() {
-		dao.excute(new HibernateCallback() {
-			@Override
-			public Object doInHibernate(Session session) throws HibernateException {
-				long currentTimeMillis = System.currentTimeMillis();
-				Query query = session
-						.createQuery("delete from ThreeSale ts where ts.state = ? and (?-ts.createTime) > 86400000");
-				query.setParameter(0, false);
-				query.setParameter(1, currentTimeMillis);
-				query.executeUpdate();
-				return null;
-			}
-		});
-	}
-
-	@Transactional
-	public void validate365() {
-		dao.excute(new HibernateCallback() {
-			@Override
-			public Object doInHibernate(Session session) throws HibernateException {
-				long currentTimeMillis = System.currentTimeMillis();
-				Query query = session.createQuery("delete from ThreeSale ts where (?-ts.createTime) > 31536000000");
-				query.setParameter(0, currentTimeMillis);
-				query.executeUpdate();
-				return null;
-			}
-		});
-	}
-
-	@SuppressWarnings("unchecked")
-	@Override
-	public List<UserInfo> getFirstUsers(Long uid, int page) {
-		return (List<UserInfo>) dao.excute(new HibernateCallback<List<UserInfo>>() {
-
-			@Override
-			public List<UserInfo> doInHibernate(Session session) throws HibernateException {
-				Query query = session
-						.createSQLQuery(
-								"SELECT u.* FROM `yeshi_ec_threesale` t LEFT JOIN `yeshi_ec_user` u ON u.`id`=t.`worker_id` WHERE  t.`boss_id`=? ORDER BY t.`createtime` DESC")
-						.addEntity(UserInfo.class);
-				query.setParameter(0, uid);
-				query.setFirstResult((page - 1) * Constant.PAGE_SIZE).setMaxResults(Constant.PAGE_SIZE);
-				return query.list();
-			}
-		});
-
-	}
-
-	@SuppressWarnings("unchecked")
-	@Override
-	public List<UserInfo> getSecondUsers(Long uid, int page) {
-		return (List<UserInfo>) dao.excute(new HibernateCallback<List<UserInfo>>() {
-
-			@Override
-			public List<UserInfo> doInHibernate(Session session) throws HibernateException {
-				Query query = session
-						.createSQLQuery(
-								"SELECT u.* FROM `yeshi_ec_threesale` t LEFT JOIN yeshi_ec_threesale t1 ON t1.`boss_id`=t.`worker_id` LEFT JOIN `yeshi_ec_user` u ON u.`id`=t1.`worker_id` WHERE  t.`boss_id`=? AND u.`id` IS NOT NULL GROUP BY u.`id` ORDER BY t.`createtime` DESC")
-						.addEntity(UserInfo.class);
-				query.setParameter(0, uid);
-				query.setFirstResult((page - 1) * Constant.PAGE_SIZE).setMaxResults(Constant.PAGE_SIZE);
-				return query.list();
-			}
-		});
-	}
-
-	@Override
-	public List<ThreeSale> getLastestThreeSaleList(int page, String key) {
-		if (NumberUtil.isNumeric(key)) {
-			Long uid = Long.parseLong(key);
-			int start = (page - 1) * Constant.PAGE_SIZE;
-			List<ThreeSale> list = dao.list(
-					"from ThreeSale ts where ts.boss.nickName like ? or ts.boss.id = ? or ts.worker.nickName like ? or ts.worker.id = ? order by ts.createTime desc",
-					start, Constant.PAGE_SIZE, new Serializable[] { "%" + key + "%", uid, "%" + key + "%", uid });
-			return list;
-		}
-		int start = (page - 1) * Constant.PAGE_SIZE;
-		List<ThreeSale> list = dao.list(
-				"from ThreeSale ts where ts.boss.nickName like ? or ts.worker.nickName like ? order by ts.createTime desc",
-				start, Constant.PAGE_SIZE, new Serializable[] { "%" + key + "%", "%" + key + "%" });
-		return list;
-
-	}
-
-	@Override
-	public int getLastestThreeSaleCount(String key) {
-		if (NumberUtil.isNumeric(key)) {
-			long lk = Long.parseLong(key);
-			return (int) dao.getCount(
-					"select count(*) from ThreeSale ts where ts.boss.id = ? or ts.worker.id = ? or ts.boss.nickName like ? or ts.worker.nickName like ?",
-					new Serializable[] { lk, lk, "%" + key + "%", "%" + key + "%" });
-		}
-		Long lcount = dao.getCount(
-				"select count(*) from ThreeSale ts where ts.boss.nickName like ? or ts.worker.nickName like ?",
-				new Serializable[] { "%" + key + "%", "%" + key + "%" });
-		return lcount.intValue();
-
-	}
-
 	@Override
 	public long getFirstUsersCount(Long uid) {
-		return (Long) dao.excute(new HibernateCallback<Long>() {
-
-			@Override
-			public Long doInHibernate(Session session) throws HibernateException {
-				Query query = session.createSQLQuery(
-						"SELECT count(u.id) FROM `yeshi_ec_threesale` t LEFT JOIN `yeshi_ec_user` u ON u.`id`=t.`worker_id` WHERE  t.`boss_id`=?");
-				query.setParameter(0, uid);
-				Object count = query.uniqueResult();
-				return Long.parseLong(count + "");
-			}
-		});
+		return threeSaleMapper.countFirstTeam(uid, null);
 	}
 
 	@Override
 	public long getSecondUsersCount(Long uid) {
-		return (Long) dao.excute(new HibernateCallback<Long>() {
-
-			@Override
-			public Long doInHibernate(Session session) throws HibernateException {
-				Query query = session.createSQLQuery(
-						"SELECT count(1) FROM `yeshi_ec_threesale` t LEFT JOIN yeshi_ec_threesale t1 ON t1.`boss_id`=t.`worker_id` LEFT JOIN `yeshi_ec_user` u ON u.`id`=t1.`worker_id` WHERE  t.`boss_id`=? AND u.`id` IS NOT NULL");
-				query.setParameter(0, uid);
-				Object count = query.uniqueResult();
-				return Long.parseLong(count + "");
-			}
-		});
+		return threeSaleMapper.countSecondTeam(uid, null);
 	}
 
 	@Override
@@ -456,20 +297,12 @@
 		return threeSaleMapper.countSuperiorQuery(state, uid);
 	}
 
-	@Override
-	public List<ThreeSale> listFirstTeam(long start, int count, Long uid) {
-		return threeSaleMapper.listFirstTeam(start, count, uid);
-	}
 
 	@Override
 	public long countFirstTeam(Long uid, Integer state) {
 		return threeSaleMapper.countFirstTeam(uid, state);
 	}
 
-	@Override
-	public List<ThreeSale> listSecondTeam(long start, int count, Long uid) {
-		return threeSaleMapper.listSecondTeam(start, count, uid);
-	}
 
 	@Override
 	public long countSecondTeam(Long uid, Integer state) {
@@ -492,26 +325,26 @@
 	}
 
 	@Override
-	public JSONObject getMyFirstTeam(long start, int count, Long uid) {
+	public JSONObject getMyFirstTeam(long start, int count, Long uid, Integer state) {
 
-		List<ThreeSale> list = threeSaleMapper.listFirstTeam(start, count, uid);
+		List<ThreeSale> list = threeSaleMapper.listFirstTeam(start, count, uid, state);
 		if (list == null) {
 			list = new ArrayList<ThreeSale>();
 		}
 
-		long countTotal = threeSaleMapper.countFirstTeam(uid, null);
+		long countTotal = threeSaleMapper.countFirstTeam(uid, state);
 
 		return organizeTeam(countTotal, list);
 	}
 
 	@Override
-	public JSONObject getMySecondTeam(long start, int count, Long uid) {
-		List<ThreeSale> list = threeSaleMapper.listSecondTeam(start, count, uid);
+	public JSONObject getMySecondTeam(long start, int count, Long uid, Integer state) {
+		List<ThreeSale> list = threeSaleMapper.listSecondTeam(start, count, uid, state);
 		if (list == null) {
 			list = new ArrayList<ThreeSale>();
 		}
 
-		long countTotal = threeSaleMapper.countSecondTeam(uid, null);
+		long countTotal = threeSaleMapper.countSecondTeam(uid, state);
 
 		return organizeTeam(countTotal, list);
 	}
@@ -645,9 +478,9 @@
 	@Transactional
 	public void bindRelationshipByInviteCode(UserInfo invitee, UserInfo inviter) throws ThreeSaleException {
 
-		if (invitee == null || inviter == null)
+		if (invitee == null || inviter == null) 
 			throw new ThreeSaleException(1, "鐢ㄦ埛淇℃伅涓虹┖");
-
+		
 		// 鑾峰彇鏈夋晥鐨勯個璇峰叧绯�
 		ThreeSale threeSale = threeSaleMapper.getRelationshipByBossIdAndWorkerId(inviter.getId(), invitee.getId());
 
@@ -669,7 +502,6 @@
 			threeSale.setCreateTime(java.lang.System.currentTimeMillis());
 			threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
 			threeSaleMapper.insertSelective(threeSale);
-
 		}
 
 		List<ThreeSale> listExpire = threeSaleMapper.getExpireRecord(inviter.getId(), invitee.getId());
@@ -690,8 +522,78 @@
 	}
 
 	@Override
+	@Transactional
+	public void bindRelationshipByWX(UserInfo invitee, Long inviterId, ThreeSale threeSaleOld) throws ThreeSaleException {
+
+		if (invitee == null || (inviterId == null && threeSaleOld == null))
+			throw new ThreeSaleException(1, "鐢ㄦ埛淇℃伅涓虹┖");
+		
+		ThreeSale threeSale = null;
+		if (inviterId != null ) {
+			// 鏂板缓绔嬬‘瀹氬叧绯�
+			threeSale  = new ThreeSale();
+			threeSale.setBoss(new UserInfo(inviterId));
+			threeSale.setWorker(invitee);
+			threeSale.setState(true);
+			threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
+			threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
+			threeSale.setCreateTime(java.lang.System.currentTimeMillis());
+			threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
+			threeSaleMapper.insertSelective(threeSale);
+			
+			// 閭�璇峰叧绯诲け鏁�  1.5.3 涔嬪墠澶辨晥
+			if (threeSaleOld != null) {
+				ThreeSale three  = new ThreeSale();
+				three.setId(threeSaleOld.getId());
+				three.setExpire(ThreeSale.EXPIRE_OUTOFDATE);
+				three.setUpdateTime(java.lang.System.currentTimeMillis());
+				threeSaleMapper.updateByPrimaryKeySelective(three);
+			}
+		} else if (threeSaleOld != null && inviterId == null) {
+			// 1.5.3 涔嬪墠鐢熸晥
+			threeSaleOld.setState(true);
+			threeSaleOld.setExpire(ThreeSale.EXPIRE_NORMAL);
+			threeSaleOld.setSucceedTime(java.lang.System.currentTimeMillis());
+			threeSaleOld.setUpdateTime(java.lang.System.currentTimeMillis());
+			threeSaleMapper.updateByPrimaryKeySelective(threeSaleOld);
+			
+			threeSale = threeSaleOld;
+		}
+
+		if (threeSale == null) {
+			throw new ThreeSaleException(1, "鍏崇郴缁戝畾澶辫触");
+		}
+		
+		UserInfo boss = userInfoService.selectByPKey(threeSale.getBoss().getId());
+		threeSale.setBoss(boss);
+		
+		
+		// 娣诲姞鍒伴槦鍒�
+		ThreeSaleCMQManager.getInstance().addThreeSaleMsg(threeSale);
+		// 閭�璇锋秷鎭�
+		userInviteMsgNotificationService.inviteSuccess(threeSale.getBoss().getId(), threeSale);
+	}
+	
+	/**
+	 * 鏍规嵁閭�璇疯�呮煡璇㈠叧绯�
+	 * 
+	 * @param workerId
+	 * @return
+	 */
+	@Override
+	public ThreeSale getByWorkerId(Long workerId) {
+		return threeSaleMapper.getByWorkerId(workerId);
+	}
+	
+	
+	@Override
 	public long countSuccessFirstTeam(Long uid) {
 		return threeSaleMapper.countFirstTeam(uid, 1);
 	}
 
+	@Override
+	public ThreeSale selectByWorkerId(Long workerId) {
+		return threeSaleMapper.selectSuccessByWorkerId(workerId);
+	}
+
 }

--
Gitblit v1.8.0