package com.yeshi.fanli.service.impl.help;
|
|
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;
|
|
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.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;
|
|
@Service
|
public class HelpCenterServiceImpl implements HelpCenterService {
|
|
@Resource
|
private HelpCenterMapper helpCenterMapper;
|
|
@Resource
|
private MongoDBManager mongoDBManager;
|
|
@Resource
|
private AdActivityVersionControlService adActivityVersionControlService;
|
|
@Override
|
public int updateByPrimaryKey(HelpCenter record) {
|
return helpCenterMapper.updateByPrimaryKey(record);
|
}
|
|
@Override
|
public int updateByPrimaryKeySelective(HelpCenter record) {
|
return helpCenterMapper.updateByPrimaryKeySelective(record);
|
}
|
|
@Override
|
public HelpCenter selectByPrimaryKey(Long id) {
|
return helpCenterMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public HelpCenter selectByPrimaryKeyCache(Long id) {
|
return selectByPrimaryKey(id);
|
}
|
|
@Override
|
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());
|
helpCenterMapper.insert(helpCenter);
|
} else {
|
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) {
|
b[j] += 256;
|
}
|
}
|
|
String type = (String) map.get("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);
|
}
|
}
|
}
|
|
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) {
|
|
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
|
|
// 需要对比的字符串
|
String compareStr = "base64";
|
String compareStrStart = "data:image/";
|
// 字符串查找初始从0开始查找
|
int indexStart = 0;
|
int indexTypeStart = 0;
|
// 获取查找字符串的长度,这里有个规律:第二次查找出字符串的起始位置等于 第一次ab字符串出现的位置+ab的长度
|
int compareStrLength = compareStr.length();
|
|
while (true) {
|
Map<String, Object> place = new HashMap<String, Object>();
|
|
int tm = content.indexOf(compareStr, indexStart);
|
int typetm = content.indexOf(compareStrStart, indexTypeStart);
|
|
if (tm <= 0) {
|
// 直到没有匹配结果为止
|
break;
|
} else {
|
// 没查找一次就从新计算下次开始查找的位置
|
indexStart = tm + compareStrLength;
|
indexTypeStart = typetm + compareStrLength;
|
|
// 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;
|
place.put("header", header);
|
|
listMap.add(place);
|
}
|
}
|
|
return listMap;
|
}
|
|
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);
|
|
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 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, Long cid, Integer orderMode, Integer state) {
|
return helpCenterMapper.query(pageIndex, pageSize, key, cid, orderMode, state);
|
}
|
|
@Override
|
public long countQuery(String key, Long cid, Integer state) {
|
return helpCenterMapper.countQuery(key, cid, state);
|
}
|
|
@Override
|
@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) {
|
return helpCenterMapper.deleteBatchById(idList);
|
}
|
|
@Override
|
public HelpInfo getHelpInfo(Long id) {
|
return mongoDBManager.getHelpInfo(id);
|
}
|
|
@Override
|
@Cacheable(value = "helpCenterCache", key = "'getHelpInfoCache-'+#id")
|
public HelpInfo getHelpInfoCache(Long id) {
|
return getHelpInfo(id);
|
}
|
|
@Override
|
@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());
|
}
|
}
|
}
|
}
|