From 5f13dcc8eaac7cd26c0ec41bd1b3efbb70a08180 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 16 六月 2020 17:45:09 +0800
Subject: [PATCH] 云发单金额修改,增加获取电话号码接口

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java |  515 +++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 395 insertions(+), 120 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 f9085ea..50762a2 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
@@ -11,21 +11,28 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.yeshi.utils.BigDecimalUtil;
-import org.yeshi.utils.entity.FileUploadResult;
 
-import com.yeshi.fanli.controller.client.v2.ShareControllerV2;
+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;
+import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
+import com.yeshi.fanli.dto.mq.user.body.UserCloudMQMsg;
 import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
+import com.yeshi.fanli.dto.suning.SuningGoodsImg;
+import com.yeshi.fanli.dto.suning.SuningGoodsInfo;
+import com.yeshi.fanli.dto.vip.VIPConvertResultDTO;
+import com.yeshi.fanli.dto.vip.goods.VIPGoodsInfo;
 import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
 import com.yeshi.fanli.entity.bus.user.cloud.CloudOrderMenuEnum;
 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;
@@ -41,6 +48,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;
@@ -54,6 +62,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;
@@ -62,6 +71,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;
@@ -71,7 +81,13 @@
 import com.yeshi.fanli.util.jd.JDUtil;
 import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
 import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil;
+import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
+import com.yeshi.fanli.util.rocketmq.MQTopicName;
+import com.yeshi.fanli.util.suning.SuningApiUtil;
+import com.yeshi.fanli.util.suning.SuningUtil;
 import com.yeshi.fanli.util.taobao.TaoBaoUtil;
+import com.yeshi.fanli.util.vipshop.VipShopApiUtil;
+import com.yeshi.fanli.util.vipshop.VipShopUtil;
 import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
 @Service
@@ -137,6 +153,11 @@
 	@Resource
 	private UserCloudSendContentService userCloudSendContentService;
 	
+	@Resource
+	private UserCloudManageService userCloudManageService;
+	
+	@Resource(name = "producer")
+	private Producer producer;
 	
 	
 	@Override
@@ -152,6 +173,20 @@
 	@Override
 	public long countByUid(Long uid) {
 		Long count = userCloudMapper.countByUid(uid);
+		return count;
+	}
+	
+	
+	@Override
+	public List<UserCloud> query(int page, int count,String key, Integer state) {
+		return userCloudMapper.query((page-1)* count, count, key, state);
+	}
+	
+	@Override
+	public long count(String key, Integer state) {
+		Long count = userCloudMapper.count(key, state);
+		if (count == null)
+			count = 0L;
 		return count;
 	}
 
@@ -182,7 +217,6 @@
 	}
 
 	@Override
-	@Transactional(rollbackFor = Exception.class)
 	public void openCloud(Long uid, Long orderId, CloudOrderMenuEnum menuEnum) throws UserCloudException {
 		UserCloud existCloud = userCloudMapper.getByOrderId(orderId);
 		if (existCloud != null) {
@@ -196,8 +230,8 @@
 			if (userCloud.getEndTime().getTime() > java.lang.System.currentTimeMillis())
 				renew = true;
 			// 楠岃瘉濂楅鏄惁鐩稿悓
-			if (renew && userCloud.getRobotType() != menuEnum.getType()) {
-				LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]宸叉湁鍏朵粬浜戝彂鍗曞椁愯繕鏈粨鏉�");
+			if (renew && userCloud.getRobotType() != menuEnum.getRobotType()) {
+				LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]宸叉湁鍏朵粬浜戝彂鍗曞椁愯繕鏈粨鏉�");
 				throw new UserCloudException(1, "宸叉湁鍏朵粬浜戝彂鍗曞椁愯繕鏈粨鏉�");
 			}
 		}
@@ -205,39 +239,34 @@
 		RobotInfoDTO dto = null;
 		if (renew) { // 缁垂
 			dto = AitaokerApiUtil.robotRenewals(userCloud.getRobotId(), menuEnum.getMonth());
-		} else {
-			dto = AitaokerApiUtil.robotCreate(menuEnum.getMonth(), menuEnum.getType(), "wechatrobot", null);
+		} else { // 鍒涘缓鏈哄櫒浜�
+			dto = AitaokerApiUtil.robotCreate(menuEnum.getMonth(), menuEnum.getRobotType(), "wechatrobot", null);
 		}
 
 		if (dto == null) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖绌哄��");
-			throw new UserCloudException(1, "鏈哄櫒浜鸿幏鍙栧け璐�");
+			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 鏈哄櫒浜哄垱寤哄け璐�");
 		}
 
+		Integer robotId = dto.getId();
+		if (robotId == null) {
+			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 鏈哄櫒浜篒D杩斿洖涓虹┖");
+		}
+		
 		String endTimeStr = dto.getEndTime();
 		if (StringUtil.isNullOrEmpty(endTimeStr)) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿涓虹┖");
-			throw new UserCloudException(1, "鏈哄櫒浜鸿繑鍥炴椂闂翠负绌�");
+			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿涓虹┖");
 		}
 
 		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杩斿洖涓虹┖");
+			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 杩斿洖鏃堕棿鏍煎紡涓嶆纭�");
 		}
 
 		Integer groupNum = dto.getGroupNum();
 		if (groupNum == null) {
-			LogHelper.error("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 groupNum杩斿洖涓虹┖");
-			throw new UserCloudException(1, "groupNum杩斿洖涓虹┖");
+			LogHelper.cloudInfo("鏂规硶openCloud: [uid=" + uid + "][璁㈠崟ID=" + orderId + "]鏈哄櫒浜哄け璐ワ細 groupNum杩斿洖涓虹┖");
 		}
 
 		UserCloud newCloud = new UserCloud();
@@ -245,11 +274,21 @@
 		newCloud.setOrderId(orderId);
 		newCloud.setGroupNum(groupNum);
 		newCloud.setRobotId(robotId);
-		newCloud.setRobotType(menuEnum.getType());
-		newCloud.setStartTime(new Date());
+		newCloud.setRobotType(menuEnum.getRobotType());
+		if (renew) {
+			newCloud.setWxId(userCloud.getWxId());
+			newCloud.setWxName(userCloud.getWxName());
+			newCloud.setWxPortrait(userCloud.getPortrait());
+			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
@@ -265,12 +304,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);
@@ -278,7 +332,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) {
@@ -298,7 +363,7 @@
 		
 		String inviteCode = userInfoExtraService.getInviteCodeByUid(uid);
 		if (StringUtil.isNullOrEmpty(inviteCode))
-			throw new UserCloudException(1, "閭�璇风爜鏈縺娲�");
+			throw new UserCloudException(3, "閭�璇风爜鏈縺娲�");
 
 		UserExtraTaoBaoInfo taoBaoInfo = userExtraTaoBaoInfoService.getByUid(uid);
 		String relationId = null;
@@ -313,20 +378,24 @@
 		// 楠岃瘉鏄惁寮�閫�
 		UserCloud userCloud = userCloudMapper.getValidByUid(uid);
 		if (userCloud == null)
-			throw new UserCloudException(1, "浜戝彂鍗曞凡杩囨湡");
+			throw new UserCloudException(1001, "浜戝彂鍗曞凡杩囨湡");
 
 		Integer robotId = userCloud.getRobotId();
 		if (robotId == null)
-			throw new UserCloudException(1, "浜戝彂鍗曟満鍣ㄤ汉涓嶅瓨鍦�");
+			throw new UserCloudException(1002, "浜戝彂鍗曟満鍣ㄤ汉涓嶅瓨鍦�");
 
 		String wxId = userCloud.getWxId();
 		if (StringUtil.isNullOrEmpty(wxId)) 
-			throw new UserCloudException(1, "寰俊鍙风己澶�");
+			throw new UserCloudException(1003, "寰俊鍙蜂笉瀛樺湪锛岃鍏堝井淇$櫥褰�");
+		
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "寰俊宸叉帀绾匡紝璇峰厛閲嶆柊鐧诲綍寰俊");
+		}
 		
 		// 楠岃瘉寮�鍚姸鎬�
 		List<UserCloudGroup> listGroup = userCloudGroupService.listByUid(uid);
 		if (listGroup == null || listGroup.size() == 0)
-			throw new UserCloudException(1, "璇峰厛娣诲姞浜戝彂鍗曠兢淇℃伅");
+			throw new UserCloudException(1004, "璇峰厛寰俊鐧诲綍");
 
 		List<UserCloudGroup> listOpen = new ArrayList<>();
 		for (UserCloudGroup cloudGrou : listGroup) {
@@ -335,7 +404,8 @@
 			}
 		}
 		if (listOpen.size() == 0)
-			throw new UserCloudException(1, "璇峰厛寮�鍚簯鍙戝崟缇ゅ姛鑳�");
+			throw new UserCloudException(1005, "璇峰厛寮�鍚簯鍙戝崟缇ゅ姛鑳�");
+		
 		
 		// 楠岃瘉鍙戝湀鏄惁鍙
 		GoodsEvaluate evaluate = goodsEvaluateService.getById(id);
@@ -350,6 +420,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<>();
@@ -391,6 +466,7 @@
 		
 
 		String qrCode = null;
+		String cloudPic = null;
 		ImgInfo imgVideo = null;
 		List<String> listImg = new ArrayList<>();
 		
@@ -401,15 +477,26 @@
 					imgVideo = imgInfo;
 					continue;
 				}
-				GoodsDetailVO goodsVO = imgInfo.getGoodsVO();
-				if (goodsVO == null) {
-					listImg.add(imgInfo.getUrl());
-				} else {
-					String jumpLink = getJumpLink(goodsVO, user, relationId, inviteCode, imgInfo.getUrl());
-					if (!StringUtil.isNullOrEmpty(jumpLink)) {
-						qrCode = jumpLink;
-					}
+				
+				if (imgInfo.getType() == ImgEnum.goods) {
+					cloudPic = imgInfo.getUrl();
 				}
+				listImg.add(imgInfo.getUrl());
+				
+				// 鍙栨秷浜岀淮鐮佸浘
+//				GoodsDetailVO goodsVO = imgInfo.getGoodsVO();
+//				if (goodsVO == null) {
+//					listImg.add(imgInfo.getUrl());
+//				} else {
+//					String jumpLink = getJumpLink(goodsVO, user, relationId, inviteCode, imgInfo.getUrl());
+//					if (!StringUtil.isNullOrEmpty(jumpLink)) {
+//						qrCode = jumpLink;
+//					}
+//				}
+			}
+			
+			if (cloudPic == null) {
+				cloudPic = imgs.get(0).getUrl();
 			}
 		}
 		
@@ -422,7 +509,7 @@
 		sendRecord.setWxId(wxId);
 		sendRecord.setRobotId(robotId);
 		sendRecord.setSendTime(new Date());
-		sendRecord.setSendOrigin(UserCloudSendRecord.ORIGIN_STORE);
+		sendRecord.setSendOrigin(UserCloudSendRecord.ORIGIN_EVALUATE);
 		UserCloudSendRecord result = userCloudSendRecordService.save(sendRecord);
 		String pid = result.getId();
 		
@@ -446,9 +533,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);
 					// 鍙戦�佸浘鏂�
@@ -483,17 +573,15 @@
 				}
 				
 				// 鍙戦�佸浘鐗�
-				String picurl = null;
-				if (!StringUtil.isNullOrEmpty(qrCode)) {
-					picurl = qrCode;
-				} else if (listImg.size() > 0) {
-					picurl = listImg.get(0);
-				}
-				if (!StringUtil.isNullOrEmpty(picurl)) {
-					String imgBase64 = ImageToBase64.NetImageToBase64(picurl);
-					boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
-					if (macsend)
-						sendContent.setPicUrl(picurl);
+				if (!StringUtil.isNullOrEmpty(cloudPic)) {
+					try {
+						String imgBase64 = ImageToBase64.NetImageToBase64(cloudPic);
+						boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
+						if (macsend)
+							sendContent.setPicUrl(cloudPic);
+					} catch (Exception e) {
+						LogHelper.errorDetailInfo(e);
+					}
 				}
 				
 				// 璇勮鏂囨湰
@@ -511,53 +599,27 @@
 		}
 	}
 	
-	private String getJumpLink(GoodsDetailVO goodsVO, UserInfo user, String relationId, String inviteCode,
-			String mainPic) {
-		String jumpLink = null;
-		if (goodsVO.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) {
-			TaoBaoLink taoBaoLink = null;
-			try {
-				taoBaoLink = shareGoodsService.getTaoBaoLinkForShare(user.getId(), goodsVO.getGoodsId(), relationId);
-			} catch (ShareGoodsException e) {
-				e.printStackTrace();
-			}
-			jumpLink = ShareControllerV2.getERCodeContentNew(
-					configService.get(ConfigKeyEnum.taobaoShareQrcodeText.getKey()), taoBaoLink.getGoods(),
-					TaoBaoUtil.filterTaoToken(taoBaoLink.getTaoToken()));
-		} else if (goodsVO.getGoodsType() == Constant.SOURCE_TYPE_JD) {
-			JDGoods jdGoods = jdGoodsCacheUtil.getGoodsInfo(goodsVO.getGoodsId());
-			if (jdGoods == null) {
-				return null;
-			}
-
-			String couponUrl = null;
-			JDCouponInfo couponInfo = JDUtil.getShowCouponInfo(jdGoods);
-			if (couponInfo != null) {
-				couponUrl = couponInfo.getLink();
-			}
-			String materialId = "https://item.jd.com/" + goodsVO.getGoodsId() + ".html";
-			jumpLink = JDApiUtil.convertLinkWithSubUnionId(materialId, couponUrl, JDApiUtil.POSITION_SHARE + "",
-					user.getId() + "");
-		} else if (goodsVO.getGoodsType() == Constant.SOURCE_TYPE_PDD) {
-			jumpLink = PinDuoDuoApiUtil.getPromotionUrl(goodsVO.getGoodsId(), PinDuoDuoApiUtil.PID_SHARE + "",
-					user.getId() + "");
-		}
-
-		FileUploadResult uploadResult = qrCodeService.drawDynamicGoodsPoster(jumpLink, user.getPortrait(), inviteCode, mainPic, goodsVO);
-			
-		if (uploadResult != null) {
-			return uploadResult.getUrl();
-		}
-		return null;
-	}
 	
 	@Override
-	public void sendCustomGoods(Long uid, Long goodsId, Integer goodsType) throws UserCloudException{
-		sendCircleByGoods(uid, goodsId, goodsType, null);
+	@Transactional(rollbackFor = Exception.class)
+	public void sendCustomGoods(Long uid, Long goodsId, Integer goodsType, Long sellerId) throws UserCloudException{
+		sendCircleByGoods(uid, goodsId, goodsType, null, sellerId,  UserCloudSendRecord.SEND_WAY_MANUAL);
 	}
 
 	@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;
@@ -568,19 +630,14 @@
 		
 		try {
 			// 鍙戦�佸晢鍝�
-			sendCircleByGoods(uid, cgoods.getGoodsId(), cgoods.getGoodsType(), id);
-			
-			UserCloudGoods record = new UserCloudGoods();
-			record.setId(id);
-			record.setState(UserCloudGoods.STATE_SHARED);
-			record.setUpdateTime(new Date());
-			userCloudGoodsService.updateByPrimaryKeySelective(record);
+			sendCircleByGoods(uid, cgoods.getGoodsId(), cgoods.getGoodsType(), id, cgoods.getSellerId(), UserCloudSendRecord.SEND_WAY_AUTO);
 		} catch (UserCloudException e) {
 			LogHelper.cloudInfo("autoSendCustomGoods - [uid:" +uid + " 搴搃d:"+  id +  "]鍘熷洜锛�"+ e.getMsg());
 		}
 	}
 
-	private void sendCircleByGoods(Long uid, Long goodsId, Integer goodsType, Long storeId) throws UserCloudException {
+	private void sendCircleByGoods(Long uid, Long goodsId, Integer goodsType, Long storeId,
+			Long sellerId, int way) throws UserCloudException {
 		// 楠岃瘉鏄惁鎺堟潈
 		UserInfo user = userInfoService.getUserByIdWithMybatis(uid);
 		if (user == null)
@@ -589,6 +646,10 @@
 		if (user != null && user.getState() != UserInfo.STATE_NORMAL) {
 			throw new UserCloudException(Constant.CODE_FORBIDDEN_USER, Constant.FORBIDDEN_USER_REASON_DESC);
 		}
+		
+		String inviteCode = userInfoExtraService.getInviteCodeByUid(uid);
+		if (StringUtil.isNullOrEmpty(inviteCode))
+			throw new UserCloudException(3, "閭�璇风爜鏈縺娲�");
 
 		UserExtraTaoBaoInfo taoBaoInfo = userExtraTaoBaoInfoService.getByUid(uid);
 		String relationId = null;
@@ -602,20 +663,49 @@
 		// 楠岃瘉鏄惁寮�閫�
 		UserCloud userCloud = userCloudMapper.getValidByUid(uid);
 		if (userCloud == null)
-			throw new UserCloudException(1, "浜戝彂鍗曞凡杩囨湡");
+			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) {
+			return;
+		}
+				
 		Integer robotId = userCloud.getRobotId();
 		if (robotId == null)
 			throw new UserCloudException(1, "浜戝彂鍗曟満鍣ㄤ汉涓嶅瓨鍦�");
 
 		String wxId = userCloud.getWxId();
 		if (StringUtil.isNullOrEmpty(wxId)) 
-			throw new UserCloudException(1, "寰俊鍙风己澶�");
+			throw new UserCloudException(1002, "璇峰厛鐧诲綍寰俊");
+		
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "寰俊宸叉帀绾匡紝璇峰厛閲嶆柊鐧诲綍寰俊");
+		}
+		
 		
 		// 楠岃瘉寮�鍚姸鎬�
 		List<UserCloudGroup> listGroup = userCloudGroupService.listByUid(uid);
 		if (listGroup == null || listGroup.size() == 0)
-			throw new UserCloudException(1, "璇峰厛娣诲姞浜戝彂鍗曠兢淇℃伅");
+			throw new UserCloudException(1003, "璇峰厛鐧诲綍寰俊");
 
 		List<UserCloudGroup> listOpen = new ArrayList<>();
 		for (UserCloudGroup cloudGrou : listGroup) {
@@ -625,7 +715,14 @@
 		}
 		
 		if (listOpen.size() == 0)
-			throw new UserCloudException(1, "璇峰厛寮�鍚簯鍙戝崟缇ゅ姛鑳�");
+			throw new UserCloudException(1004, "璇峰厛寮�鍚簯鍙戝崟缇ゅ姛鑳�");
+		
+		
+		// 妫�娴嬪井淇℃槸鍚︾櫥褰曠姸鎬�
+		if (!AitaokerApiUtil.onlineCheck(robotId)) {
+			throw new UserCloudException(1003, "璇烽噸鏂扮櫥褰曞井淇�");
+		}
+		
 		
 		// 淇濆瓨鍙戦�佽褰�
 		UserCloudSendRecord sendRecord = new UserCloudSendRecord();
@@ -644,17 +741,43 @@
 		}
 		UserCloudSendRecord result = userCloudSendRecordService.save(sendRecord);
 		
-		if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
-			sendTaoBaoGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
-		} else if (goodsType == Constant.SOURCE_TYPE_JD) {
-			sendJDGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
-		} else if (goodsType == Constant.SOURCE_TYPE_PDD) {
-			sendPDDGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
-		} else if (goodsType == Constant.SOURCE_TYPE_VIP) {
+		try {
+			if (goodsType == Constant.SOURCE_TYPE_TAOBAO) {
+				sendTaoBaoGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
+			} else if (goodsType == Constant.SOURCE_TYPE_JD) {
+				sendJDGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
+			} else if (goodsType == Constant.SOURCE_TYPE_PDD) {
+				sendPDDGoods(user, robotId, wxId, goodsId, relationId, listOpen, result.getId());
+			} else if (goodsType == Constant.SOURCE_TYPE_VIP) {
+				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);
+			}
 			
-		} else if (goodsType == Constant.SOURCE_TYPE_SUNING) {
-			
+		} catch (UserCloudException e) {
+			LogHelper.cloudInfo("autoSendCustomGoods - [uid:" +uid + " 搴搃d:"+  storeId +  "]鍘熷洜锛�"+ e.getMsg());
+			// 鏇存柊鍙戝崟璁板綍
+			if (storeId != null) {
+				UserCloudGoods record = new UserCloudGoods();
+				record.setId(storeId);
+				record.setState(UserCloudGoods.STATE_INVALID);
+				record.setSendTime(new Date());
+				record.setUpdateTime(new Date());
+				userCloudGoodsService.updateByPrimaryKeySelective(record);
+			}
 		}
+	
+		
+		
 	}
 	
 	/**
@@ -822,6 +945,89 @@
 	
 	
 	/**
+	 * 鍞搧浼氬晢鍝�
+	 * @param user
+	 * @param robotId
+	 * @param wxId
+	 * @param goodsId
+	 * @param relationId
+	 * @param listOpen
+	 * @param pid
+	 * @throws UserCloudException
+	 */
+	private void sendVIPGoods(UserInfo user, int robotId, String wxId, Long goodsId, String relationId, 
+			List<UserCloudGroup> listOpen, String pid) throws UserCloudException {
+		VIPGoodsInfo goods = VipShopApiUtil.getGoodsDetail(goodsId + "");
+		if (goods == null)
+			throw new UserCloudException(1, "璇ュ晢鍝佸凡涓嬫灦");
+
+		VIPConvertResultDTO resultDTO = VipShopApiUtil.convertLink(goodsId + "", VipShopUtil.getShareChanTag(user.getId()));
+		String jumpLink = resultDTO.getUrl();
+		
+		boolean coupon = false;
+		String quanPrice = "";
+		String couponAmount = "";
+		// 鑾峰彇鎺ㄨ崘璇�
+		String recommendText = shareGoodsTextTemplateService.getRecommendText(coupon, goods.getGoodsName(), null,
+				couponAmount, null);
+		// 鑾峰彇璇勮璇�
+		String commentText = shareGoodsTextTemplateService.getCommentTextByLink(coupon, jumpLink, goods.getMarketPrice(), 
+				quanPrice, ConfigKeyEnum.quickShareVIPCommentText);
+		
+		// 浜戝彂鍗�
+		sendGoods(robotId, wxId, listOpen, recommendText, commentText, goods.getGoodsDetailPictures(), user.getId(), pid);
+	}
+	
+	
+	/**
+	 * 浜笢鍟嗗搧浜戝彂鍗�
+	 * @param user
+	 * @param robotId
+	 * @param wxId
+	 * @param goodsId
+	 * @param relationId
+	 * @param listOpen
+	 * @throws UserCloudException
+	 */
+	private void sendSuNingGoods(UserInfo user, int robotId, String wxId, Long goodsId, Long sellerId, String relationId, 
+			List<UserCloudGroup> listOpen, String pid) throws UserCloudException {
+		SuningGoodsInfo goods = SuningApiUtil.getGoodsDetail(goodsId +"", sellerId +"");
+		if (goods == null)
+			throw new UserCloudException(1, "璇ュ晢鍝佸凡涓嬫灦");
+		
+		
+		String couponLink = goods.getCouponInfo().getCouponUrl();
+		String jumpLink = SuningApiUtil.convertLink(SuningUtil.getProductUrl(sellerId +"", goodsId +""),
+				StringUtil.isNullOrEmpty(couponLink) ? null : couponLink, SuningApiUtil.PID_SHARE, user.getId() + "");
+		
+		
+		boolean coupon = false;
+		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 template = configService.get(ConfigKeyEnum.quickShareSuNingCommentText.getKey());
+		String commentText = shareGoodsTextTemplateService.createQuickShareTextSuNing(template, goods, jumpLink);
+		
+		
+		List<String> imgList = new ArrayList<>();
+		for (SuningGoodsImg img : goods.getCommodityInfo().getPictureUrl()) {
+			imgList.add(img.getPicUrl());
+		}
+		
+		// 浜戝彂鍗�
+		sendGoods(robotId, wxId, listOpen, recommendText, commentText, imgList, user.getId(), pid);
+	}
+	
+	/**
 	 * 鍙戦�佸晢鍝�
 	 * @param robotId
 	 * @param title
@@ -845,9 +1051,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);
 				
@@ -875,10 +1084,15 @@
 				// 鍙戦�佸浘鐗�
 				String picurl = listImg.get(0);
 				if (!StringUtil.isNullOrEmpty(picurl)) {
-					String imgBase64 = ImageToBase64.NetImageToBase64(picurl);
-					boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
-					if (macsend)
-						sendContent.setPicUrl(picurl);
+					try {
+						String imgBase64 = ImageToBase64.NetImageToBase64(picurl);
+						boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
+						if (macsend)
+							sendContent.setPicUrl(picurl);
+					} catch (Exception e) {
+						LogHelper.errorDetailInfo(e);
+					}
+				
 				}
 				// 璇勮鏂囨湰
 				List<String> list = new ArrayList<>();
@@ -894,4 +1108,65 @@
 	}
 
 	
-}
+	@Override
+	public void fixedTimeSend(List<GoodsEvaluate> listActivity, List<GoodsEvaluate> listGoods) {
+		if (listGoods == null && listActivity == null) 
+			return;
+		// 涓�涓皬鏃朵箣鍓�
+		Date lastTime = new Date(java.lang.System.currentTimeMillis() - 1000 * 60 * 60);
+		for (int i = 0; i < 1000; i ++) {
+			// 鏌ヨ鍝簺鐢ㄦ埛寮�閫�
+			List<Long> listUser = userCloudMapper.listValidUid(i*1000, 1000);
+			if (listUser == null || listUser.size() == 0) {
+				break;
+			}
+			
+			// 閬嶅巻鐢ㄦ埛
+			for (Long uid: listUser) {
+				try {// 鍒ゆ柇鏄惁寮�閫氬畼鏂归噰闆�
+					UserCloudManage cloudManage = userCloudManageService.selectByPrimaryKey(uid);
+					if (cloudManage == null || cloudManage.getOfficial() == null || !cloudManage.getOfficial()) {
+						continue;
+					}
+					// 鏄惁鍦ㄤ竴涓皬鏃跺唴宸插彂閫佽繃
+					UserCloudSendRecord last = userCloudSendRecordService.getLastByUid(uid, lastTime);
+					if (last != null)
+						continue;
+					
+					String evaluateId = null;
+					// 娲诲姩
+					if (listActivity != null && listActivity.size() > 0) {
+						for (GoodsEvaluate evaluate: listActivity) {
+							UserCloudSendRecord record = userCloudSendRecordService.getByUidAndSendId(uid, evaluate.getId());
+							if (record != null) 
+								continue;
+							
+							evaluateId = evaluate.getId();
+							break;
+						}
+					}
+					// 鍗曞搧
+					if (StringUtil.isNullOrEmpty(evaluateId) && listGoods != null && listGoods.size() > 0) {
+						for (GoodsEvaluate evaluate: listGoods) {
+							UserCloudSendRecord record = userCloudSendRecordService.getByUidAndSendId(uid, evaluate.getId());
+							if (record != null) 
+								continue;
+							
+							evaluateId = evaluate.getId();
+							break;
+						}
+					}
+				
+					if(!StringUtil.isNullOrEmpty(evaluateId)) {
+						UserCloudMQMsg msg = new UserCloudMQMsg(uid, evaluateId, UserCloudMQMsg.TYPE_EVALUATE);
+						Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userCloud, msg);
+						producer.send(message);
+					}
+				} catch (Exception e) {
+					LogHelper.errorDetailInfo(e);
+				}
+			}
+		}
+		
+	}
+}
\ No newline at end of file

--
Gitblit v1.8.0