From 573c491b4a1ba60e12a5678a01c1546c0077c1ee Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 30 七月 2019 09:07:42 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushCouponServiceImpl.java |  150 +++++++++++++++++++++++++++++++++++---------------
 1 files changed, 105 insertions(+), 45 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushCouponServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushCouponServiceImpl.java
index a0e3225..e27991e 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushCouponServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushCouponServiceImpl.java
@@ -1,21 +1,29 @@
 package com.yeshi.fanli.service.impl.push;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 
 import javax.annotation.Resource;
-import javax.transaction.Transactional;
 
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import com.yeshi.fanli.dao.mybatis.push.PushCouponMapper;
+import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
 import com.yeshi.fanli.entity.push.PushCoupon;
+import com.yeshi.fanli.entity.push.PushCouponRecord;
 import com.yeshi.fanli.exception.PushException;
 import com.yeshi.fanli.exception.push.PushCouponException;
 import com.yeshi.fanli.service.inter.config.ConfigService;
 import com.yeshi.fanli.service.inter.push.PushCouponRecordService;
 import com.yeshi.fanli.service.inter.push.PushCouponService;
 import com.yeshi.fanli.service.inter.push.PushService;
+import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
+import com.yeshi.fanli.util.StringUtil;
+
+import net.sf.json.JSONObject;
 
 @Service
 public class PushCouponServiceImpl implements PushCouponService {
@@ -31,6 +39,9 @@
 	
 	@Resource
 	private PushCouponRecordService pushCouponRecordService;
+	
+	@Resource
+	private UserSystemCouponService userSystemCouponService;
 	
 	@Override
 	public int deleteByPrimaryKey(Long id) {
@@ -75,54 +86,69 @@
 
 	@Override
 	@Transactional
-	public void save(PushCoupon record) throws PushCouponException {
+	public void save(PushCoupon record) throws PushCouponException,Exception {
 		if (record == null) {
 			throw new PushCouponException(1, "鍙傛暟涓嶆纭�");
 		}
 
-		String title = record.getTitle();
-		String content = record.getContent();
-		if (title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0) {
-			throw new PushCouponException(1, "鏍囬鎴栧唴瀹逛笉鑳戒负绌�");
+		String uids = record.getUids();
+		if (StringUtil.isNullOrEmpty(uids)) {
+			throw new PushCouponException(1, "鍙戞斁鐢ㄦ埛涓嶈兘涓虹┖");
 		}
-
-		Date startTime = record.getStartTime();
-		Date endTime = record.getEndTime();
-		if (startTime == null || endTime == null) {
-			throw new PushCouponException(1, "鏈夋晥鏈熸椂闂翠笉鑳戒负绌�");
-		}
-
+		
 		Integer amount = record.getAmount();
-		if (amount == null || amount < 1) {
-			throw new PushCouponException(1, "鍒告暟閲忎笉鑳藉皬浜�1");
+		if (amount == null) {
+			throw new PushCouponException(1, "鍒告暟閲忎笉鑳戒负绌�");
+		} else if (amount < 1 || amount > 5) {
+			throw new PushCouponException(1, "鍒告暟閲忚寖鍥存槸 1~5");
 		}
+		
 
 		Long id = record.getId();
 		if (id == null) {
 			// 鏂板
-			record.setPushed(false);
 			record.setCreateTime(new Date());
 			record.setUpdateTime(new Date());
-
 			// 鎻掑叆鏁版嵁
-			insert(record);
-		} else {
-			// 淇敼
-			PushCoupon current = selectByPrimaryKey(id);
-			if (current == null) {
-				throw new PushCouponException(1, "璇ヨ褰曞凡涓嶅瓨鍦�");
+			pushCouponMapper.insert(record);
+			
+			String[] uidArray = uids.split(",");
+			
+			if (uidArray != null) {
+				for (int i = 0; i < uidArray.length; i++) {
+
+					Long uid = Long.parseLong(uidArray[i]);
+					// 鎻掑叆璁板綍
+					PushCouponRecord pushCouponRecord = new PushCouponRecord();
+					pushCouponRecord.setPushCoupon(record);
+					pushCouponRecord.setUid(uid);
+					pushCouponRecord.setCreateTime(new Date());
+				
+					// 鎻掑叆棰嗗彇璁板綍
+					pushCouponRecordService.insertSelective(pushCouponRecord);
+					// 鐩存帴鍙戦�佸埜
+					userSystemCouponService.randomRewardCoupon(amount, uid, UserSystemCoupon.SOURCE_SYSTEM_PUSH);
+				}
 			}
-
-			current.setTitle(title);
-			current.setContent(content);
-			current.setAmount(amount);
-			current.setStartTime(startTime);
-			current.setEndTime(endTime);
-			current.setUids(record.getUids());
-			current.setRemark(record.getRemark());
-
-			// 鏇存柊鏁版嵁
-			updateByPrimaryKey(current);
+			
+			
+		} else {
+//			
+//			// 淇敼
+//			PushCoupon current = selectByPrimaryKey(id);
+//			if (current == null) {
+//				throw new PushCouponException(1, "璇ヨ褰曞凡涓嶅瓨鍦�");
+//			}
+//			
+//			if(current.isPushed()) {
+//				throw new PushCouponException(1, "宸叉帹閫佺殑淇℃伅涓嶈兘淇敼");
+//			}
+//			current.setAmount(amount);
+//			current.setUids(record.getUids());
+//			current.setRemark(record.getRemark());
+//			current.setUpdateTime(new Date());
+//			// 鏇存柊鏁版嵁
+//			updateByPrimaryKey(current);
 		}
 	}
 
@@ -134,6 +160,15 @@
 		if (pushCoupon == null) {
 			throw new PushCouponException(1, "鎺ㄩ�佷俊鎭凡涓嶅瓨鍦�");
 		}
+		
+		if(pushCoupon.isPushed()) {
+			throw new PushCouponException(1, "璇ヤ俊鎭笉鑳介噸澶嶆帹閫�");
+		}
+		String versions = pushCoupon.getVersions();
+		if (versions == null || versions.trim().length() == 0 ) {
+			throw new PushCouponException(1, "鎺ㄩ�佺増鏈笉鑳戒负绌�");
+		}
+		
 
 		String title = pushCoupon.getTitle();
 		String content = pushCoupon.getContent();
@@ -142,24 +177,48 @@
 		}
 		
 		
+		List<String> listuid = null;
 		String uids = pushCoupon.getUids();
-		if (uids == null || uids.trim().length() == 0) {
+		if (uids != null && uids.trim().length() > 0) {
+			listuid = Arrays.asList(uids.split(","));
+			if (listuid == null || listuid.size() == 0) {
+				throw new PushCouponException(1, "鐢ㄦ埛id鏍煎紡涓嶆纭�");
+			}
+		}
+		
+		List<String> listIOS = new ArrayList<String>();
+		JSONObject json = JSONObject.fromObject(versions);
+		String versionsIOS = json.getString("IOS");
+		if (versionsIOS != null && versionsIOS.trim().length() > 0) {
+			if (versionsIOS.contains("鍏ㄦ帹")) {
+				listIOS = null;
+			} else {
+				listIOS = Arrays.asList(versionsIOS.split(","));
+			}
+		}
+		
+		List<String> listAndroid = new ArrayList<String>();
+		String versionsAndroid  = json.getString("Android");
+		if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
+			if (versionsAndroid.contains("鍏ㄦ帹")) {
+				listAndroid = null;
+			} else {
+				listAndroid = Arrays.asList(versionsAndroid.split(","));
+			}
+		}
+		
+		
+		if (listuid == null) {
 			// 鍏ㄦ帹
-			pushService.pushWelfareCenter(null, title, content);
+			pushService.pushWelfareCenter(null, title, content, listIOS, listAndroid);
 			
 		} else {
 			// 鎸囧畾鐢ㄦ埛鎺ㄩ��
-			String[] uidArray = uids.split(",");
-			
-			if (uidArray != null) {
-				for (int i = 0; i < uidArray.length; i++) {
-					String str_uid = uidArray[i];
-					if (str_uid != null && str_uid.trim().length() > 0) {
-						pushService.pushWelfareCenter(Long.parseLong(str_uid), title, content);
-					}
+			for (String str_uid: listuid) {
+				if (str_uid != null && str_uid.trim().length() > 0) {
+					pushService.pushWelfareCenter(Long.parseLong(str_uid), title, content, listIOS, listAndroid);
 				}
 			}
-			
 		}
 		
 		// 宸叉帹閫�
@@ -182,6 +241,7 @@
 		
 		for (Long id: list) {
 			long receivedCount = pushCouponRecordService.countByPushId(id);
+			// 棰嗗彇鏁伴噺
 			if (receivedCount > 0) {
 				continue;
 			}

--
Gitblit v1.8.0