InfoService.java 13 KB

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