From 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64 Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期六, 09 五月 2020 21:41:27 +0800
Subject: [PATCH] 2.1需求

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/help/HelpCenterServiceImpl.java |  246 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 161 insertions(+), 85 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/help/HelpCenterServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/help/HelpCenterServiceImpl.java
index b5add51..1ff39dd 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/help/HelpCenterServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/help/HelpCenterServiceImpl.java
@@ -3,8 +3,10 @@
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 
 import javax.annotation.Resource;
@@ -12,79 +14,95 @@
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.yeshi.utils.tencentcloud.COSManager;
 
 import com.yeshi.fanli.dao.mybatis.help.HelpCenterMapper;
+import com.yeshi.fanli.entity.AppVersionInfo;
 import com.yeshi.fanli.entity.bus.help.HelpCenter;
 import com.yeshi.fanli.entity.bus.help.HelpInfo;
-import com.yeshi.fanli.exception.HelpCenterException;
+import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
+import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar;
+import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
+import com.yeshi.fanli.exception.config.HelpCenterException;
+import com.yeshi.fanli.exception.homemodule.HomeNavbarException;
 import com.yeshi.fanli.service.inter.help.HelpCenterService;
+import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
+import com.yeshi.fanli.util.FilePathEnum;
 import com.yeshi.fanli.util.StringUtil;
 import com.yeshi.fanli.util.db.MongoDBManager;
-import org.yeshi.utils.tencentcloud.COSManager;
 
 @Service
 public class HelpCenterServiceImpl implements HelpCenterService {
 
 	@Resource
 	private HelpCenterMapper helpCenterMapper;
-	
+
 	@Resource
 	private MongoDBManager mongoDBManager;
-	
+
+	@Resource
+	private AdActivityVersionControlService adActivityVersionControlService;
 
 	@Override
-	public int insertSelective(HelpCenter record) throws HelpCenterException {
-		return helpCenterMapper.insertSelective(record);
-	}
-
-	@Override
-	public int updateByPrimaryKey(HelpCenter record) throws HelpCenterException {
+	public int updateByPrimaryKey(HelpCenter record) {
 		return helpCenterMapper.updateByPrimaryKey(record);
 	}
 
 	@Override
-	public int updateByPrimaryKeySelective(HelpCenter record) throws HelpCenterException {
+	public int updateByPrimaryKeySelective(HelpCenter record) {
 		return helpCenterMapper.updateByPrimaryKeySelective(record);
 	}
 
 	@Override
-	public HelpCenter selectByPrimaryKey(Long id) throws HelpCenterException {
+	public HelpCenter selectByPrimaryKey(Long id) {
 		return helpCenterMapper.selectByPrimaryKey(id);
 	}
-	
+
 	@Override
-	public HelpCenter selectByPrimaryKeyCache(Long id) throws HelpCenterException {
+	public HelpCenter selectByPrimaryKeyCache(Long id) {
 		return selectByPrimaryKey(id);
 	}
 
 	@Override
-	public String save(HelpCenter helpCenter, String content, String html) throws Exception {
+	public String save(HelpCenter helpCenter, String content, String html) throws HelpCenterException, Exception {
+		if (StringUtil.isNullOrEmpty(helpCenter.getTitle()))
+			throw new HelpCenterException(1, "鏍囬涓嶈兘涓虹┖");
 
+		if (helpCenter.getHelpClass() == null || helpCenter.getHelpClass().getId() <= 0)
+			throw new HelpCenterException(1, "鍒嗙被棰樹笉鑳戒负绌�");
+
+		Integer state = helpCenter.getState();
+		if (state == null) {
+			helpCenter.setState(0);
+		}
+
+		Integer weight = helpCenter.getWeight();
+		if (weight == null) {
+			helpCenter.setWeight(0);
+		}
+
+		helpCenter.setUpdatetime(new Date());
 		Long id = helpCenter.getId();
 		if (id == null) {
 			helpCenter.setCreatetime(new Date());
-			helpCenter.setUpdatetime(new Date());
 			helpCenterMapper.insert(helpCenter);
-			
 		} else {
-			helpCenter.setUpdatetime(new Date());
-			helpCenterMapper.updateByPrimaryKeySelective(helpCenter);
-			
+			HelpCenter result = helpCenterMapper.selectByPrimaryKey(id);
+			if (result == null)
+				throw new HelpCenterException(1, "姝ょ被鍐呭宸蹭笉瀛樺湪锛岃鍒锋柊");
+
+			helpCenter.setCreatetime(result.getCreatetime());
+			helpCenterMapper.updateByPrimaryKey(helpCenter);
 		}
-		
-		
+
 		List<Map<String, Object>> positions = getPosition(html);
-
 		if (positions != null && positions.size() > 0) {
-
 			@SuppressWarnings("restriction")
 			sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
 
 			for (int i = 0; i < positions.size(); i++) {
 				Map<String, Object> map = positions.get(i);
-
 				String base64Img = (String) map.get("base64Img");
-
 				byte[] b = decoder.decodeBuffer(base64Img);
 				for (int j = 0; j < b.length; ++j) {
 					if (b[j] < 0) {
@@ -93,34 +111,51 @@
 				}
 
 				String type = (String) map.get("type");
-				
 				// 涓婁紶鏂囦欢鐩稿浣嶇疆
-				String fileUrl = "helpInfo/" + UUID.randomUUID().toString().replace("-", "") + "." +type;
+				String fileUrl = FilePathEnum.helpCenterContent + UUID.randomUUID().toString().replace("-", "") + "."
+						+ type;
 				/* 涓婁紶鏂板浘鐗� */
 				String uploadFilePath = COSManager.getInstance().uploadFileByte(b, fileUrl).getUrl();
 
 				if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
 					String header = (String) map.get("header");
 					html = html.replace(header, uploadFilePath);
-					//System.out.println("uploadFilePath: " + uploadFilePath);
-					//content = content.replace(header, uploadFilePath);
 				}
-				
 			}
 		}
-	
-		
+
 		String replaceHtml = replaceIframe(html);
-		
 		Long backId = helpCenter.getId();
-		
 		HelpInfo helpInfo = new HelpInfo(backId, content, replaceHtml);
-		
 		mongoDBManager.saveHelpInfo(helpInfo);
-
 		return html;
-
 	}
+	
+	
+	@Override
+	public void switchState(Long id) throws HelpCenterException {
+		if (id == null) {
+			throw new HelpCenterException(1, "璇蜂紶閫掓纭弬鏁�");
+		}
+		
+		HelpCenter resultObj = helpCenterMapper.selectByPrimaryKey(id);
+		if (resultObj == null) {
+			throw new HelpCenterException(1, "姝ゅ唴瀹瑰凡涓嶅瓨鍦�");
+		}
+		
+		Integer state = resultObj.getState();
+		if (state == null || state == 0) {
+			state = 1;
+		} else {
+			state = 0;
+		}
+		
+		HelpCenter updateObj = new HelpCenter();
+		updateObj.setId(id);
+		updateObj.setState(state);
+		helpCenterMapper.updateByPrimaryKeySelective(updateObj);
+	}
+	
 
 	public List<Map<String, Object>> getPosition(String content) {
 
@@ -149,29 +184,26 @@
 				indexStart = tm + compareStrLength;
 				indexTypeStart = typetm + compareStrLength;
 
-				//System.out.println(indexStart);
-				//System.out.println(indexTypeStart);
-				
+				// System.out.println(indexStart);
+				// System.out.println(indexTypeStart);
+
 				place.put("start", tm + 7);
 
 				String suffix = content.substring(typetm + 11, tm - 1);
 				place.put("type", suffix);
-				
+
 				String newContent = content.substring(tm + 7, content.length());
-				
-				
+
 				char qmark = '"';
 				int end = newContent.indexOf(qmark);
 				place.put("end", end);
 
 				String base64Img = newContent.substring(0, end);
 				place.put("base64Img", base64Img);
-				
-				
-				String header = compareStrStart + suffix+ ";" + compareStr +"," +base64Img;
+
+				String header = compareStrStart + suffix + ";" + compareStr + "," + base64Img;
 				place.put("header", header);
-				
-				
+
 				listMap.add(place);
 			}
 		}
@@ -180,95 +212,139 @@
 	}
 
 	public String replaceIframe(String html) {
-	
+
 		String result = html;
-		
+
 		// 瀛楃涓叉煡鎵惧垵濮嬩粠0寮�濮嬫煡鎵�
 		int indexStart = 0;
 		// 闇�瑕佸姣旂殑瀛楃涓�
 		String compareStr = "<iframe";
-		
+
 		int compareStrLength = compareStr.length();
-	
+
 		while (true) {
-	
+
 			int tm = html.indexOf(compareStr, indexStart);
-	
+
 			if (tm <= 0) {
 				// 鐩村埌娌℃湁鍖归厤缁撴灉涓烘
 				break;
 			} else {
 				// 璧峰浣嶇疆
 				indexStart = tm + compareStrLength;
-	
+
 				// 缁撴潫浣嶇疆
 				String newContent = html.substring(tm, html.length());
 				int end = newContent.indexOf("</iframe>");
-	
-				// 鏇挎崲鎸囧畾瀛楁  <iframe src=""></iframe>
-				String replaceStr = html.substring(tm,  tm +  end + 9);
-				
+
+				// 鏇挎崲鎸囧畾瀛楁 <iframe src=""></iframe>
+				String replaceStr = html.substring(tm, tm + end + 9);
+
 				char qmark = '"';
 				// src 璧峰浣嶇疆
 				int srcindex = replaceStr.indexOf("src=", 0);
-				
+
 				String replaceSrc = replaceStr.substring(srcindex + 5, replaceStr.length());
 				// src "缁撴潫浣嶇疆
 				int endsrc = replaceSrc.indexOf(qmark, 0);
-				
-				String src = replaceSrc.substring(0, endsrc +1);
-				
+
+				String src = replaceSrc.substring(0, endsrc + 1);
+
 				String voide = "<video src=\"" + src + " controls=\"controls\"> 鎮ㄧ殑娴忚鍣ㄤ笉鏀寔video鏍囩锛岃鏇存柊娴忚鍣�</video>";
-				
+
 				result = html.replace(replaceStr, voide);
 			}
 		}
-	
+
 		return result;
 	}
 
 	@Override
-	public List<HelpCenter> query(int pageIndex, int pageSize, String key, Integer orderMode) throws HelpCenterException {
-		return helpCenterMapper.query(pageIndex, pageSize, key, orderMode);
+	public List<HelpCenter> query(int pageIndex, int pageSize, String key, Long cid, Integer orderMode, Integer state) {
+		return helpCenterMapper.query(pageIndex, pageSize, key, cid, orderMode, state);
 	}
 
 	@Override
-	public long countQuery(String key) throws HelpCenterException {
-		return helpCenterMapper.countQuery(key);
+	public long countQuery(String key, Long cid, Integer state) {
+		return helpCenterMapper.countQuery(key, cid, state);
 	}
 
-	
 	@Override
-	@Cacheable(value = "helpCenterCache",key="'queryIdAndTitle-'+#pageIndex+'-'+#key")
-	public List<HelpCenter> queryIdAndTitle(int pageIndex, int pageSize, String key) throws HelpCenterException {
-		return helpCenterMapper.queryIdAndTitle(pageIndex, pageSize, key);
+	@Cacheable(value = "helpCenterCache", key = "'queryIdAndTitle-'+#pageId+'-'+#key+'-'+#cid")
+	public List<HelpCenter> listValid(long pageId, int pageSize, String key, Long cid) {
+		return helpCenterMapper.listValid(pageId, pageSize, key, cid);
 	}
-	
+
 	@Override
-	public int deleteBatchById(List<String> idList) throws HelpCenterException {
+	public int deleteBatchById(List<String> idList) {
 		return helpCenterMapper.deleteBatchById(idList);
 	}
 
 	@Override
-	public HelpInfo getHelpInfo(Long id) throws HelpCenterException {
+	public HelpInfo getHelpInfo(Long id) {
 		return mongoDBManager.getHelpInfo(id);
 	}
-	
+
 	@Override
-	@Cacheable(value = "helpCenterCache",key="'getHelpInfoCache-'+#id")
-	public HelpInfo getHelpInfoCache(Long id) throws HelpCenterException {
+	@Cacheable(value = "helpCenterCache", key = "'getHelpInfoCache-'+#id")
+	public HelpInfo getHelpInfoCache(Long id) {
 		return getHelpInfo(id);
 	}
-	
-	
-	
+
 	@Override
-	@Transactional
-	public void deleteInfoBatchById(List<String> idList) throws HelpCenterException {
+	@Transactional(rollbackFor = Exception.class)
+	public void deleteInfoBatchById(List<String> idList) {
 		if (idList != null) {
 			for (String id : idList) {
 				mongoDBManager.removeHelpInfo(Long.parseLong(id));
 			}
 		}
 	}
+
+	@Transactional(rollbackFor = Exception.class)
+	@Override
+	public void setVersions(Long id, List<Long> versions) throws HelpCenterException {
+		HelpCenter helpCenter = helpCenterMapper.selectByPrimaryKey(id);
+		if (helpCenter == null) {
+			throw new HelpCenterException(1, "姝や俊鎭笉瀛樺湪锛岃鍒锋柊閲嶈瘯");
+		}
+
+		Set<Long> oldSet = new HashSet<>();
+		List<AdActivityVersionControl> versionList = adActivityVersionControlService
+				.listByTypeAndSourceId(AdActivityType.helpCenter, id);
+		if (versionList != null) {
+			for (AdActivityVersionControl control : versionList)
+				oldSet.add(control.getVersion().getId());
+		}
+
+		Set<Long> newSet = new HashSet<>();
+		for (Long version : versions) {
+			newSet.add(version);
+		}
+
+		Set<Long> delSet = new HashSet<>();
+		delSet.addAll(oldSet);
+		delSet.removeAll(newSet);
+		for (Long versionId : delSet) {
+			adActivityVersionControlService.deleteBySourceAndVersion(id, AdActivityType.helpCenter, versionId);
+		}
+
+		Set<Long> addSet = new HashSet<>();
+		addSet.addAll(newSet);
+		addSet.removeAll(oldSet);
+
+		// 娣诲姞鏄犲皠
+		for (Long versionId : addSet) {
+			AdActivityVersionControl control = new AdActivityVersionControl();
+			control.setCreateTime(new Date());
+			control.setSourceId(id);
+			control.setType(AdActivityType.helpCenter);
+			control.setVersion(new AppVersionInfo(versionId));
+			try {
+				adActivityVersionControlService.addVersionControl(control);
+			} catch (Exception e) {
+				throw new HelpCenterException(2, e.getMessage());
+			}
+		}
+	}
 }

--
Gitblit v1.8.0