From 20bb4f95184453d4addfd27e40a78f3c63bcf6c3 Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期四, 11 六月 2020 18:15:53 +0800
Subject: [PATCH] 用户统计

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java |  246 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 179 insertions(+), 67 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java
index b18deb6..0cfcf16 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java
@@ -15,7 +15,6 @@
 import com.aliyun.openservices.ons.api.Message;
 import com.aliyun.openservices.ons.api.Producer;
 import com.yeshi.fanli.dao.mybatis.user.cloud.UserCloudMapper;
-import com.yeshi.fanli.dto.aitaoker.RobotInfoDTO;
 import com.yeshi.fanli.dto.aitaoker.WeiXinGroupDTO;
 import com.yeshi.fanli.dto.jd.JDCouponInfo;
 import com.yeshi.fanli.dto.jd.JDPingouInfo;
@@ -32,6 +31,7 @@
 import com.yeshi.fanli.entity.bus.user.cloud.UserCloud;
 import com.yeshi.fanli.entity.bus.user.cloud.UserCloudGoods;
 import com.yeshi.fanli.entity.bus.user.cloud.UserCloudGroup;
+import com.yeshi.fanli.entity.bus.user.cloud.UserCloudManage;
 import com.yeshi.fanli.entity.bus.user.cloud.UserCloudSendContent;
 import com.yeshi.fanli.entity.bus.user.cloud.UserCloudSendRecord;
 import com.yeshi.fanli.entity.dynamic.CommentInfo;
@@ -47,6 +47,7 @@
 import com.yeshi.fanli.exception.goods.ConvertLinkExceptionException;
 import com.yeshi.fanli.exception.share.ShareGoodsException;
 import com.yeshi.fanli.exception.user.cloud.UserCloudException;
+import com.yeshi.fanli.exception.user.cloud.UserCloudGoodsException;
 import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.fanli.service.inter.config.ConfigService;
 import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService;
@@ -60,6 +61,7 @@
 import com.yeshi.fanli.service.inter.user.UserInfoService;
 import com.yeshi.fanli.service.inter.user.cloud.UserCloudGoodsService;
 import com.yeshi.fanli.service.inter.user.cloud.UserCloudGroupService;
+import com.yeshi.fanli.service.inter.user.cloud.UserCloudManageService;
 import com.yeshi.fanli.service.inter.user.cloud.UserCloudSendContentService;
 import com.yeshi.fanli.service.inter.user.cloud.UserCloudSendRecordService;
 import com.yeshi.fanli.service.inter.user.cloud.UserCloudService;
@@ -68,6 +70,7 @@
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.ImageToBase64;
 import com.yeshi.fanli.util.MoneyBigDecimalUtil;
+import com.yeshi.fanli.util.RedisKeyEnum;
 import com.yeshi.fanli.util.RedisManager;
 import com.yeshi.fanli.util.StringUtil;
 import com.yeshi.fanli.util.aitaoker.AitaokerApiUtil;
@@ -149,6 +152,9 @@
 	@Resource
 	private UserCloudSendContentService userCloudSendContentService;
 	
+	@Resource
+	private UserCloudManageService userCloudManageService;
+	
 	@Resource(name = "producer")
 	private Producer producer;
 	
@@ -182,9 +188,6 @@
 			count = 0L;
 		return count;
 	}
-	
-	
-	
 
 	@Override
 	@Transactional(rollbackFor = Exception.class)
@@ -228,59 +231,66 @@
 				renew = true;
 			// 楠岃瘉濂楅鏄惁鐩稿悓
 			if (renew && userCloud.getRobotType() != menuEnum.getType()) {
-				LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]宸叉湁鍏朵粬浜戝彂鍗曞椁愯繕鏈粨鏉�");
+				LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]宸叉湁鍏朵粬浜戝彂鍗曞椁愯繕鏈粨鏉�");
 				throw new UserCloudException(1, "宸叉湁鍏朵粬浜戝彂鍗曞椁愯繕鏈粨鏉�");
 			}
 		}
 
-		RobotInfoDTO dto = null;
-		if (renew) { // 缁垂
-			dto = AitaokerApiUtil.robotRenewals(userCloud.getRobotId(), menuEnum.getMonth());
-		} else {
-			dto = AitaokerApiUtil.robotCreate(menuEnum.getMonth(), menuEnum.getType(), "wechatrobot", null);
-		}
-
-		if (dto == null) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖绌哄��");
-			throw new UserCloudException(1, "鏈哄櫒浜鸿幏鍙栧け璐�");
-		}
-
-		String endTimeStr = dto.getEndTime();
-		if (StringUtil.isNullOrEmpty(endTimeStr)) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿涓虹┖");
-			throw new UserCloudException(1, "鏈哄櫒浜鸿繑鍥炴椂闂翠负绌�");
-		}
-
-		long endTime = 0;
-		try {
-			endTime = Long.parseLong(endTimeStr);
-		} catch (Exception e) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿鏍煎紡涓嶆纭�");
-			throw new UserCloudException(1, "鏈哄櫒浜鸿繑鍥炴椂闂存牸寮忎笉姝g‘");
-		}
-
-		Integer robotId = dto.getId();
-		if (robotId == null) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 鏈哄櫒浜篒D杩斿洖涓虹┖");
-			throw new UserCloudException(1, "鏈哄櫒浜篒D杩斿洖涓虹┖");
-		}
-
-		Integer groupNum = dto.getGroupNum();
-		if (groupNum == null) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 groupNum杩斿洖涓虹┖");
-			throw new UserCloudException(1, "groupNum杩斿洖涓虹┖");
-		}
+//		RobotInfoDTO dto = null;
+//		if (renew) { // 缁垂
+//			dto = AitaokerApiUtil.robotRenewals(userCloud.getRobotId(), menuEnum.getMonth());
+//		} else { // 鍒涘缓鏈哄櫒浜�
+//			dto = AitaokerApiUtil.robotCreate(menuEnum.getMonth(), menuEnum.getType(), "wechatrobot", null);
+//		}
+//
+//		if (dto == null) {
+//			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖绌哄��");
+//			throw new UserCloudException(1, "鏈哄櫒浜鸿幏鍙栧け璐�");
+//		}
+//
+//		String endTimeStr = dto.getEndTime();
+//		if (StringUtil.isNullOrEmpty(endTimeStr)) {
+//			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿涓虹┖");
+//			throw new UserCloudException(1, "鏈哄櫒浜鸿繑鍥炴椂闂翠负绌�");
+//		}
+//
+//		long endTime = 0;
+//		try {
+//			endTime = Long.parseLong(endTimeStr);
+//		} catch (Exception e) {
+//			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿鏍煎紡涓嶆纭�");
+//			throw new UserCloudException(1, "鏈哄櫒浜鸿繑鍥炴椂闂存牸寮忎笉姝g‘");
+//		}
+//
+//		Integer robotId = dto.getId();
+//		if (robotId == null) {
+//			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 鏈哄櫒浜篒D杩斿洖涓虹┖");
+//			throw new UserCloudException(1, "鏈哄櫒浜篒D杩斿洖涓虹┖");
+//		}
+//
+//		Integer groupNum = dto.getGroupNum();
+//		if (groupNum == null) {
+//			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 groupNum杩斿洖涓虹┖");
+//			throw new UserCloudException(1, "groupNum杩斿洖涓虹┖");
+//		}
 
 		UserCloud newCloud = new UserCloud();
 		newCloud.setUid(uid);
 		newCloud.setOrderId(orderId);
-		newCloud.setGroupNum(groupNum);
-		newCloud.setRobotId(robotId);
+//		newCloud.setGroupNum(groupNum);
+//		newCloud.setRobotId(robotId);
 		newCloud.setRobotType(menuEnum.getType());
-		newCloud.setStartTime(new Date());
-		newCloud.setEndTime(new Date(endTime * 1000)); // Unix 杞崲 鏅�氭椂闂�
+//		if (renew) {
+//			newCloud.setStartTime(userCloud.getStartTime());
+//		} else {
+//			newCloud.setStartTime(new Date());
+//		}
+//		newCloud.setEndTime(new Date(endTime * 1000)); // Unix 杞崲 鏅�氭椂闂�
 		newCloud.setCreateTime(new Date());
 		userCloudMapper.insertSelective(newCloud);
+		
+		// 寮�鍚彂鍦堝姛鑳�
+		userCloudManageService.save(uid, null, null);
 	}
 
 	@Override
@@ -296,12 +306,27 @@
 		List<WeiXinGroupDTO> list = AitaokerApiUtil.getContract(robotId);
 		if (list == null || list.size() == 0)
 			throw new UserCloudException(1, "鏈绱㈠埌瀵瑰簲缇�");
-
+		
+		String wxId = userCloud.getWxId();
 		for (WeiXinGroupDTO dto : list) {
-			userCloudGroupService.addGroup(uid, dto.getGroupId(), dto.getGroupName(), userCloud.getGroupNum());
+			String groupId = dto.getGroupId();
+			// 妫�娴嬫槸鍚﹀尮閰嶇兢
+			String key =  RedisKeyEnum.cloudMatchGroup.getKey() + StringUtil.Md5(wxId + "_" + groupId);
+			String result = redisManager.getCommonString(key);
+			 if (!StringUtil.isNullOrEmpty(result)) {
+				userCloudGroupService.addGroup(uid, dto.getGroupId(), dto.getGroupName(), userCloud.getGroupNum());
+			}
 		}
 	}
 	
+	@Override
+	public void cacheMatchGroup(String wxId, String groupId) {
+		 if (StringUtil.isNullOrEmpty(wxId) || StringUtil.isNullOrEmpty(groupId))
+			return;
+		String key =  RedisKeyEnum.cloudMatchGroup.getKey() + StringUtil.Md5(wxId + "_" + groupId);
+		redisManager.cacheCommonString(key , "true" , 60*20);
+	}
+		
 	@Override
 	public void sendByDynamic(Long uid, String id) throws UserCloudException{
 		sendCircleByDynamic(uid, id, UserCloudSendRecord.SEND_WAY_MANUAL);
@@ -309,7 +334,18 @@
 	
 	
 	@Override
+	@Transactional(rollbackFor = Exception.class)
 	public void autoSendByDynamic(Long uid, String id) {
+		boolean official = false;
+		UserCloudManage cloudManage = userCloudManageService.selectByPrimaryKey(uid);
+		if (cloudManage != null) {
+			if (cloudManage.getOfficial() != null)
+				official = cloudManage.getOfficial();
+		}
+		
+		if (!official)
+			return;
+		
 		try {
 			sendCircleByDynamic(uid, id, UserCloudSendRecord.SEND_WAY_AUTO);
 		} catch (UserCloudException e) {
@@ -354,6 +390,10 @@
 		if (StringUtil.isNullOrEmpty(wxId)) 
 			throw new UserCloudException(1003, "寰俊鍙蜂笉瀛樺湪锛岃鍏堝井淇$櫥褰�");
 		
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "寰俊宸叉帀绾匡紝璇峰厛閲嶆柊鐧诲綍寰俊");
+		}
+		
 		// 楠岃瘉寮�鍚姸鎬�
 		List<UserCloudGroup> listGroup = userCloudGroupService.listByUid(uid);
 		if (listGroup == null || listGroup.size() == 0)
@@ -382,6 +422,11 @@
 		if (comments == null || comments.size() == 0)
 			throw new UserCloudException(1, "璇ュ唴瀹逛笉鑳借浆閾�");
 
+		// 妫�娴嬪井淇℃槸鍚︾櫥褰曠姸鎬�
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "璇烽噸鏂扮櫥褰曞井淇�");
+		}
+		
 		
 		boolean hasToken = false;
 		List<String> listComment = new ArrayList<>();
@@ -490,9 +535,12 @@
 						
 					if (listImg.size() > 0) {
 						for (String img: listImg) {
-							picUrl += "," + img;
+							picUrl +=  img + ";" ;
 						}
 					}
+					
+					if (picUrl.endsWith(";")) 
+						picUrl = picUrl.substring(0, picUrl.length()-1);
 					sendContent.setTitle(title);
 					sendContent.setPicUrl(picUrl);
 					// 鍙戦�佸浘鏂�
@@ -554,14 +602,26 @@
 	}
 	
 	
-	
 	@Override
+	@Transactional(rollbackFor = Exception.class)
 	public void sendCustomGoods(Long uid, Long goodsId, Integer goodsType, Long sellerId) throws UserCloudException{
 		sendCircleByGoods(uid, goodsId, goodsType, null, sellerId);
 	}
 
 	@Override
+	@Transactional(rollbackFor = Exception.class)
 	public void autoSendCustomGoods(Long uid, Long id) {
+		boolean custom = false;
+		UserCloudManage cloudManage = userCloudManageService.selectByPrimaryKey(uid);
+		if (cloudManage != null) {
+			if (cloudManage.getCustom() != null)
+				custom = cloudManage.getCustom();
+		}
+		
+		if (!custom) {
+			return;
+		}
+		
 		UserCloudGoods cloudGoods = userCloudGoodsService.selectByPrimaryKey(id);
 		if (cloudGoods == null) {
 			return;
@@ -573,12 +633,6 @@
 		try {
 			// 鍙戦�佸晢鍝�
 			sendCircleByGoods(uid, cgoods.getGoodsId(), cgoods.getGoodsType(), id, cgoods.getSellerId());
-			
-			UserCloudGoods record = new UserCloudGoods();
-			record.setId(id);
-			record.setState(UserCloudGoods.STATE_SHARED);
-			record.setUpdateTime(new Date());
-			userCloudGoodsService.updateByPrimaryKeySelective(record);
 		} catch (UserCloudException e) {
 			LogHelper.cloudInfo("autoSendCustomGoods - [uid:" +uid + " 搴搃d:"+  id +  "]鍘熷洜锛�"+ e.getMsg());
 		}
@@ -612,6 +666,30 @@
 		if (userCloud == null)
 			throw new UserCloudException(1001, "浜戝彂鍗曞凡杩囨湡");
 
+		
+		// 鍔犲叆鎴戠殑澶囬�夊簱
+		if (storeId == null) {
+			try {
+				UserCloudGoods cloudGoods = userCloudGoodsService.addGoods(uid, goodsId, goodsType, sellerId);
+				if (cloudGoods != null)
+					storeId =cloudGoods.getId();
+			} catch (UserCloudGoodsException e) {
+				LogHelper.cloudInfo("sendCustomGoods - [uid:" +uid + "goodsId:"+  goodsId +  "goodsType" + goodsType +"]鍘熷洜锛�"+ e.getMsg());
+				throw new UserCloudException(1, "鍔犲叆浜戝彂鍗曞閫夊簱澶辫触");
+			}
+		}
+					
+		// 鑷�夊簱鍟嗗搧鏄惁鎵撳紑
+		boolean custom = false;
+		UserCloudManage cloudManage = userCloudManageService.selectByPrimaryKey(uid);
+		if (cloudManage != null) {
+			if (cloudManage.getCustom() != null)
+				custom = cloudManage.getCustom();
+		}
+		if (!custom) {
+			throw new UserCloudException(0, "鎴愬姛鍔犲叆浜戝彂鍗曞簱");
+		}
+				
 		Integer robotId = userCloud.getRobotId();
 		if (robotId == null)
 			throw new UserCloudException(1, "浜戝彂鍗曟満鍣ㄤ汉涓嶅瓨鍦�");
@@ -619,6 +697,11 @@
 		String wxId = userCloud.getWxId();
 		if (StringUtil.isNullOrEmpty(wxId)) 
 			throw new UserCloudException(1002, "璇峰厛鐧诲綍寰俊");
+		
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "寰俊宸叉帀绾匡紝璇峰厛閲嶆柊鐧诲綍寰俊");
+		}
+		
 		
 		// 楠岃瘉寮�鍚姸鎬�
 		List<UserCloudGroup> listGroup = userCloudGroupService.listByUid(uid);
@@ -634,6 +717,13 @@
 		
 		if (listOpen.size() == 0)
 			throw new UserCloudException(1004, "璇峰厛寮�鍚簯鍙戝崟缇ゅ姛鑳�");
+		
+		
+		// 妫�娴嬪井淇℃槸鍚︾櫥褰曠姸鎬�
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "璇烽噸鏂扮櫥褰曞井淇�");
+		}
+		
 		
 		// 淇濆瓨鍙戦�佽褰�
 		UserCloudSendRecord sendRecord = new UserCloudSendRecord();
@@ -662,6 +752,16 @@
 			sendVIPGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
 		} else if (goodsType == Constant.SOURCE_TYPE_SUNING) {
 			sendSuNingGoods(user, robotId, wxId, goodsId, sellerId, relationId, listOpen, result.getId());
+		}
+		
+		// 鏇存柊鍙戝崟璁板綍
+		if (storeId != null) {
+			UserCloudGoods record = new UserCloudGoods();
+			record.setId(storeId);
+			record.setState(UserCloudGoods.STATE_SHARED);
+			record.setSendTime(new Date());
+			record.setUpdateTime(new Date());
+			userCloudGoodsService.updateByPrimaryKeySelective(record);
 		}
 	}
 	
@@ -857,7 +957,7 @@
 				couponAmount, null);
 		// 鑾峰彇璇勮璇�
 		String commentText = shareGoodsTextTemplateService.getCommentTextByLink(coupon, jumpLink, goods.getMarketPrice(), 
-				quanPrice, ConfigKeyEnum.quickShareJDCommentText);
+				quanPrice, ConfigKeyEnum.quickShareVIPCommentText);
 		
 		// 浜戝彂鍗�
 		sendGoods(robotId, wxId, listOpen, recommendText, commentText, goods.getGoodsDetailPictures(), user.getId(), pid);
@@ -887,17 +987,21 @@
 		
 		
 		boolean coupon = false;
-		String quanPrice = "";
 		String couponAmount = "";
 		String sales = null;
+		if (goods.getCouponInfo() != null && !StringUtil.isNullOrEmpty(goods.getCouponInfo().getCouponUrl())) {// 鏈夊埜
+			couponAmount = new BigDecimal(goods.getCouponInfo().getCouponValue()).toString();
+			coupon = true;
+		}  
+		
 		// 鑾峰彇鎺ㄨ崘璇�
 		String recommendText = shareGoodsTextTemplateService.getRecommendText(coupon, goods.getCommodityInfo().getCommodityName(), sales, 
 				couponAmount, null);
 		
-		// 鑾峰彇璇勮璇�
-		String zkPrice = goods.getCommodityInfo().getCommodityPrice().toString();
-		String commentText = shareGoodsTextTemplateService.getCommentTextByLink(coupon, jumpLink, zkPrice, quanPrice, 
-				ConfigKeyEnum.quickShareJDCommentText);
+		// 鐢熸垚蹇嵎鍒嗕韩鍐呭
+		String template = configService.get(ConfigKeyEnum.quickShareSuNingCommentText.getKey());
+		String commentText = shareGoodsTextTemplateService.createQuickShareTextSuNing(template, goods, jumpLink);
+		
 		
 		List<String> imgList = new ArrayList<>();
 		for (SuningGoodsImg img : goods.getCommodityInfo().getPictureUrl()) {
@@ -932,9 +1036,12 @@
 				String picUrl = "";
 				if (listImg.size() > 0) {
 					for (String img: listImg) {
-						picUrl += "," + img;
+						picUrl +=  img + ";" ;
 					}
 				}
+				
+				if (picUrl.endsWith(";")) 
+					picUrl = picUrl.substring(0, picUrl.length()-1);
 				sendContent.setTitle(title);
 				sendContent.setPicUrl(picUrl);
 				
@@ -991,10 +1098,8 @@
 			return;
 		
 		// 涓�涓皬鏃朵箣鍓�
-		long rand = 1000 * 60 * 60 ;
-		long time = java.lang.System.currentTimeMillis() - rand;
+		long time = java.lang.System.currentTimeMillis() -  1000 * 60 * 60 ;
 		Date lastTime =  new Date(time);
-		
 		
 		for (int i = 0; i < 1000; i ++) {
 			List<Long> list = userCloudMapper.listValidUid((i-1)*1000, 1000);
@@ -1012,7 +1117,14 @@
 					if (last != null)
 						continue;
 					
-					if (!Constant.IS_TEST) {
+					boolean official = false;
+					UserCloudManage cloudManage = userCloudManageService.selectByPrimaryKey(uid);
+					if (cloudManage != null) {
+						if (cloudManage.getOfficial() != null)
+							official = cloudManage.getOfficial();
+					}
+					
+					if (!Constant.IS_TEST && official) {
 						UserCloudMQMsg msg = new UserCloudMQMsg(uid, id, UserCloudMQMsg.TYPE_EVALUATE);
 						Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userCloud, msg);
 						producer.send(message);

--
Gitblit v1.8.0