InfoService.java 14 KB

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