InfoService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package com.caimei.modules.info.service;
  2. import java.util.Date;
  3. import java.util.List;
  4. import javax.servlet.http.HttpServletRequest;
  5. import com.caimei.modules.basesetting.dao.KeywordDao;
  6. import com.caimei.modules.hehe.entity.cmHeHeUserActivity;
  7. import com.caimei.modules.live.entity.NewPageLive;
  8. import com.caimei.modules.sys.utils.UploadImageUtils;
  9. import com.caimei.modules.utils.MessageUtil;
  10. import com.caimei.modules.utils.RequestUtil;
  11. import com.caimei.modules.utils.message.InsideMessage;
  12. import com.caimei.modules.utils.message.MessageModel;
  13. import com.caimei.modules.utils.message.MqInfo;
  14. import com.caimei.modules.utils.message.enums.MessageType;
  15. import com.caimei.utils.StringUtil;
  16. import org.apache.commons.collections.CollectionUtils;
  17. import org.jsoup.Jsoup;
  18. import org.jsoup.nodes.Document;
  19. import org.jsoup.nodes.Element;
  20. import org.jsoup.select.Elements;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import com.caimei.dfs.image.beens.ImageUploadInfo;
  25. import com.caimei.modules.brand.utils.ImagePathUtils;
  26. import com.caimei.modules.info.dao.InfoDao;
  27. import com.caimei.modules.info.dao.InfoLabelDao;
  28. import com.caimei.modules.info.entity.Info;
  29. import com.caimei.modules.info.entity.InfoLabel;
  30. import com.thinkgem.jeesite.common.config.Global;
  31. import com.thinkgem.jeesite.common.persistence.Page;
  32. import com.thinkgem.jeesite.common.service.CrudService;
  33. import com.thinkgem.jeesite.common.utils.Encodes;
  34. import com.thinkgem.jeesite.common.utils.StringUtils;
  35. /**
  36. * 信息列表Service
  37. *
  38. * @author LG
  39. * @version 2016-06-27
  40. */
  41. @Service
  42. @Transactional(readOnly = true)
  43. public class InfoService extends CrudService<InfoDao, Info> {
  44. @Autowired
  45. private InfoDao infoDao;
  46. @Autowired
  47. private InfoLabelDao infoLabelDao;
  48. @Autowired
  49. private KeywordDao keywordDao;
  50. public Info get(String id) {
  51. Info info = super.get(id);
  52. if (null != info) {
  53. String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址
  54. String image = info.getGuidanceImage();
  55. if (StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:") && !image.contains("https:")) {//不包含http的
  56. info.setGuidanceImage(admin2Server + "/uploadFile/infoImage/" + image);
  57. }
  58. }
  59. return info;
  60. }
  61. public List<Info> findList(Info info) {
  62. return super.findList(info);
  63. }
  64. public Page<Info> findPage(Page<Info> page, Info info) {
  65. if (null != info.getPublishSource() && 2 == info.getPublishSource()) {
  66. page.setOrderBy("a.createDate desc");
  67. }
  68. Page<Info> infoPage = super.findPage(page, info);
  69. List<Info> infoList = infoPage.getList();
  70. infoList.forEach(infoItem -> {
  71. //是否置顶
  72. if (StringUtils.isNotEmpty(infoItem.getTopPosition())) {
  73. if ("1".equals(infoItem.getTopPosition())) {
  74. infoItem.setTopPosition("第一位");
  75. } else if ("2".equals(infoItem.getTopPosition())) {
  76. infoItem.setTopPosition("第二位");
  77. } else if ("3".equals(infoItem.getTopPosition())) {
  78. infoItem.setTopPosition("第三位");
  79. }
  80. }
  81. });
  82. return infoPage;
  83. }
  84. /**
  85. * 通过对象查询信息基础列表
  86. *
  87. * @return 信息基础
  88. */
  89. public List<Info> getInfoList(Info info)
  90. {
  91. return infoDao.getInfoList(info);
  92. }
  93. /**
  94. * 通过对象查询信息基础
  95. *
  96. * @param info 信息基础
  97. * @return 信息基础
  98. */
  99. public Info getByInfo(Info info)
  100. {
  101. return infoDao.getByInfo(info);
  102. }
  103. /**
  104. * 修改信息基础
  105. *
  106. * @param info 信息基础
  107. * @return 结果
  108. */
  109. @Transactional(readOnly = false)
  110. public int updateInfo(Info info)
  111. {
  112. info.setUpdateDate(new Date());
  113. return infoDao.updateInfo(info);
  114. }
  115. @Transactional(readOnly = false)
  116. public void save(Info info, HttpServletRequest request) {
  117. //替换中文逗号
  118. String label = StringUtils.replace(info.getLabel(), ",", ",");
  119. info.setLabel(label);
  120. String[] split = StringUtils.split(label, ",");
  121. for (String lab : split) {
  122. InfoLabel label2 = new InfoLabel(null, lab);
  123. List<InfoLabel> list = infoLabelDao.findByLabel(label2);
  124. if (CollectionUtils.isEmpty(list)) {
  125. label2.preInsert();
  126. infoLabelDao.insert(label2);
  127. }
  128. }
  129. String photoServer = Global.getConfig("photoServer");//获取文件服务器地址
  130. // 标签
  131. String[] labelIds = StringUtils.split(info.getLabelIds(), ",");
  132. String labelStr = "";
  133. if (null != labelIds && labelIds.length > 0) {
  134. for (int i = 0; i < labelIds.length; i++) {
  135. labelStr += keywordDao.findKeyWordId(labelIds[i]);
  136. if (i != 5) {
  137. labelStr += "##";
  138. }
  139. }
  140. }
  141. info.setRelatedLabels(labelStr);
  142. info.setLabelIds(info.getLabelIds());
  143. /**
  144. * 富文本内容处理+内置图片上传
  145. */
  146. String infoContent = Encodes.unescapeHtml(info.getInfoContent());
  147. infoContent = StringUtils.replace(infoContent, "<body", "<div");
  148. infoContent = StringUtils.replace(infoContent, "</body>", "</div>");
  149. //infoContent=Encodes.urlDecode(infoContent);
  150. Document doc = Jsoup.parse(infoContent);
  151. Elements links = doc.getElementsByTag("img");
  152. for (Element link : links) {
  153. String linkSrc = link.attr("src");
  154. String linkSrcOld = link.attr("src");
  155. if (StringUtils.isNotBlank(linkSrc) && !linkSrc.contains("http:") && !linkSrc.contains("https:") && !linkSrc.contains("uploadFile/ueditor")) {//不包含http开头的
  156. // String realPath = Encodes.urlDecode(request.getSession().getServletContext().getRealPath(linkSrc));
  157. linkSrc = Encodes.urlDecode(linkSrc);
  158. String realPath = UploadImageUtils.getAbsolutePath(linkSrc);
  159. int pointerIndex = realPath.lastIndexOf(".");
  160. ImageUploadInfo saveImageSerivce = new ImageUploadInfo();
  161. try {
  162. saveImageSerivce = ImagePathUtils.saveImageSerivce(realPath, pointerIndex, realPath);
  163. String src = photoServer + saveImageSerivce.getSource();
  164. infoContent = StringUtils.replace(infoContent, linkSrcOld, src);
  165. } catch (Exception e) {
  166. logger.error("图片上传错误:" + e.toString(), e);
  167. }
  168. }
  169. }
  170. info.setInfoContent(infoContent);
  171. //引导图
  172. String images = info.getGuidanceImage();
  173. if (StringUtils.isNotBlank(images) && !images.contains("http:") && !images.contains("https:")) {
  174. images = Encodes.urlDecode(images);
  175. String realPath = UploadImageUtils.getAbsolutePath(images);
  176. int pointerIndex = realPath.lastIndexOf(".");
  177. ImageUploadInfo saveImageSerivce = new ImageUploadInfo();
  178. try {
  179. saveImageSerivce = ImagePathUtils.saveImageSerivce(realPath, pointerIndex, realPath);
  180. info.setGuidanceImage(photoServer + saveImageSerivce.getSource());
  181. } catch (Exception e) {
  182. logger.error("图片上传错误:" + e.toString(), e);
  183. }
  184. }
  185. //商城首页图
  186. String homePageImage = info.getHomePageImage();
  187. if (StringUtils.isNotBlank(homePageImage) && !homePageImage.contains("http:") && !homePageImage.contains("https:")) {
  188. homePageImage = Encodes.urlDecode(homePageImage);
  189. String realPath = UploadImageUtils.getAbsolutePath(homePageImage);
  190. int pointerIndex = realPath.lastIndexOf(".");
  191. ImageUploadInfo saveImageSerivce = new ImageUploadInfo();
  192. try {
  193. saveImageSerivce = ImagePathUtils.saveImageSerivce(realPath, pointerIndex, realPath);
  194. info.setHomePageImage(photoServer + saveImageSerivce.getSource());
  195. } catch (Exception e) {
  196. logger.error("图片上传错误:" + e.toString(), e);
  197. }
  198. }
  199. boolean flg = false;
  200. if (StringUtils.isBlank(info.getId())) {
  201. flg = true;
  202. }
  203. // 管理员审核文章
  204. if (null != info.getAuditFlag() && 1 == info.getAuditFlag()) {
  205. //站内信
  206. MessageModel<InsideMessage> insideMessageMessageModel = new MessageModel<>();
  207. insideMessageMessageModel.code(MessageType.WEB_INSIDE_MESSAGE)
  208. .mqInfo(new MqInfo().topic("MessageLine").delay(1).async(0));
  209. if (null != info.getAuditStatus() && 2 == info.getAuditStatus()) {
  210. // 审核通过
  211. info.setPubdate(new Date());
  212. info.setOnlineStatus(2);
  213. insideMessageMessageModel
  214. .info(new InsideMessage()
  215. .shopId(info.getShopId())
  216. .userType(2)
  217. .messageType(2)
  218. .shopMessType(8)
  219. .productId(Integer.valueOf(info.getId()))
  220. .content(info.getTitle()));
  221. } else {
  222. // 审核不通过
  223. info.setOnlineStatus(3);
  224. insideMessageMessageModel
  225. .info(new InsideMessage()
  226. .shopId(info.getShopId())
  227. .userType(2)
  228. .messageType(2)
  229. .shopMessType(9)
  230. .content(info.getTitle())
  231. .reasonContent(info.getFailReason()));
  232. }
  233. MessageUtil.sendMessage(insideMessageMessageModel);
  234. }
  235. super.save(info);
  236. if (flg) {
  237. infoDao.insertInfoPraise(info.getId());
  238. }
  239. //分段保存信息内容
  240. String page = "<div style=\"page-break-after: always;\">\r\n\t<span style=\"display: none;\"> </span></div>";//分页标记
  241. String[] sp = StringUtils.splitByWholeSeparator(infoContent, page);
  242. infoDao.deleteInfoPageByInfoId(info.getId());
  243. infoDao.insertInfoPage(info.getId(), sp);
  244. }
  245. @Transactional(readOnly = false)
  246. public void delete(Info info) {
  247. // super.delete(info);
  248. infoDao.deleteInfo(info.getId());
  249. //infoDao.deleteInfoPraise(info.getId());//删除点赞
  250. //infoDao.deleteInfoPageByInfoId(info.getId());//删除信息分页
  251. }
  252. @Transactional(readOnly = false)
  253. public void updateEnabledStatusByIds(String enabledStatus, String[] ids) {
  254. infoDao.updateEnabledStatusByIds(enabledStatus, ids);
  255. }
  256. @Transactional(readOnly = false)
  257. public void updateRecommendStatusByIds(String recommendStatus, String[] ids) {
  258. infoDao.updateRecommendStatusByIds(recommendStatus, ids);
  259. }
  260. @Transactional(readOnly = false)
  261. public void deleteByTypeId(String typeId) {
  262. infoDao.deleteByTypeId(typeId);
  263. }
  264. /**
  265. * 替换标签
  266. *
  267. * @param label 原标签值
  268. * @param reLabel 要替换的标签值
  269. * @author LG
  270. * @date 2016年8月15日
  271. * @version 1.0
  272. */
  273. @Transactional(readOnly = false)
  274. public void updateReplaceByLabel(String label, String reLabel) {
  275. infoDao.updateReplaceByLabel(label, reLabel);
  276. }
  277. @Transactional(readOnly = false)
  278. public void update(Info info) {
  279. infoDao.update(info);
  280. }
  281. /**
  282. * 置顶文章列表
  283. *
  284. * @return
  285. */
  286. public List<Info> findTopList() {
  287. Info info = new Info();
  288. info.setTopFlag("1");
  289. return infoDao.findList(info);
  290. }
  291. /**
  292. * 清除置顶,将后面置顶位的数据往前移
  293. *
  294. * @param oldInfo
  295. */
  296. @Transactional(readOnly = false)
  297. public void moveTopList(Info oldInfo) {
  298. Integer oldPosition = Integer.parseInt(oldInfo.getTopPosition());
  299. List<Info> topInfoList = findTopList();
  300. topInfoList.forEach(topInfo -> {
  301. Integer topPosition = Integer.parseInt(topInfo.getTopPosition());
  302. if (Integer.parseInt(topInfo.getTopPosition()) > oldPosition) {
  303. topInfo.setTopPosition((--topPosition).toString());
  304. update(topInfo);
  305. }
  306. });
  307. }
  308. @Transactional(readOnly = false)
  309. public void auditInfo(Info info) {
  310. Date pubdate = null;
  311. if (info.getAuditStatus() == 2) {
  312. pubdate = new Date();
  313. }
  314. infoDao.auditInfo(info.getId(), info.getAuditStatus(), info.getFailReason(), pubdate);
  315. }
  316. @Transactional(readOnly = false)
  317. public void offlineInfo(Integer id) {
  318. infoDao.offlineInfo(id);
  319. }
  320. }