package com.caimei.modules.info.service; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import com.caimei.modules.basesetting.dao.KeywordDao; import com.caimei.modules.hehe.entity.cmHeHeUserActivity; import com.caimei.modules.live.entity.NewPageLive; import com.caimei.modules.sys.utils.UploadImageUtils; import com.caimei.modules.utils.RequestUtil; import com.caimei.modules.utils.message.InsideMessage; import com.caimei.modules.utils.message.MessageModel; import com.caimei.modules.utils.message.MqInfo; import com.caimei.modules.utils.message.enums.MessageType; import com.caimei.utils.StringUtil; import org.apache.commons.collections.CollectionUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.caimei.dfs.image.beens.ImageUploadInfo; import com.caimei.modules.brand.utils.ImagePathUtils; import com.caimei.modules.info.dao.InfoDao; import com.caimei.modules.info.dao.InfoLabelDao; import com.caimei.modules.info.entity.Info; import com.caimei.modules.info.entity.InfoLabel; import com.thinkgem.jeesite.common.config.Global; import com.thinkgem.jeesite.common.persistence.Page; import com.thinkgem.jeesite.common.service.CrudService; import com.thinkgem.jeesite.common.utils.Encodes; import com.thinkgem.jeesite.common.utils.StringUtils; /** * 信息列表Service * * @author LG * @version 2016-06-27 */ @Service @Transactional(readOnly = true) public class InfoService extends CrudService { @Autowired private InfoDao infoDao; @Autowired private InfoLabelDao infoLabelDao; @Autowired private KeywordDao keywordDao; public Info get(String id) { Info info = super.get(id); if (null != info) { String admin2Server = Global.getConfig("admin2Server");//获取admin2文件服务器地址 String image = info.getGuidanceImage(); if (StringUtils.isNotBlank(image) && !image.contains("http:") && !image.contains("https:") && !image.contains("https:")) {//不包含http的 info.setGuidanceImage(admin2Server + "/uploadFile/infoImage/" + image); } } return info; } public List findList(Info info) { return super.findList(info); } public Page findPage(Page page, Info info) { if (null != info.getPublishSource() && 2 == info.getPublishSource()) { page.setOrderBy("a.createDate desc"); } Page infoPage = super.findPage(page, info); List infoList = infoPage.getList(); infoList.forEach(infoItem -> { //是否置顶 if (StringUtils.isNotEmpty(infoItem.getTopPosition())) { if ("1".equals(infoItem.getTopPosition())) { infoItem.setTopPosition("第一位"); } else if ("2".equals(infoItem.getTopPosition())) { infoItem.setTopPosition("第二位"); } else if ("3".equals(infoItem.getTopPosition())) { infoItem.setTopPosition("第三位"); } } }); return infoPage; } /** * 通过对象查询信息基础列表 * * @return 信息基础 */ public List getInfoList(Info info) { return infoDao.getInfoList(info); } /** * 通过对象查询信息基础 * * @param info 信息基础 * @return 信息基础 */ public Info getByInfo(Info info) { return infoDao.getByInfo(info); } /** * 修改信息基础 * * @param info 信息基础 * @return 结果 */ @Transactional(readOnly = false) public int updateInfo(Info info) { info.setUpdateDate(new Date()); return infoDao.updateInfo(info); } @Transactional(readOnly = false) public void save(Info info, HttpServletRequest request) { //替换中文逗号 String label = StringUtils.replace(info.getLabel(), ",", ","); info.setLabel(label); String[] split = StringUtils.split(label, ","); for (String lab : split) { InfoLabel label2 = new InfoLabel(null, lab); List list = infoLabelDao.findByLabel(label2); if (CollectionUtils.isEmpty(list)) { label2.preInsert(); infoLabelDao.insert(label2); } } String photoServer = Global.getConfig("photoServer");//获取文件服务器地址 // 标签 String[] labelIds = StringUtils.split(info.getLabelIds(), ","); String labelStr = ""; if (null != labelIds && labelIds.length > 0) { for (int i = 0; i < labelIds.length; i++) { labelStr += keywordDao.findKeyWordId(labelIds[i]); if (i != 5) { labelStr += "##"; } } } info.setRelatedLabels(labelStr); info.setLabelIds(info.getLabelIds()); /** * 富文本内容处理+内置图片上传 */ String infoContent = Encodes.unescapeHtml(info.getInfoContent()); infoContent = StringUtils.replace(infoContent, "", ""); //infoContent=Encodes.urlDecode(infoContent); Document doc = Jsoup.parse(infoContent); Elements links = doc.getElementsByTag("img"); for (Element link : links) { String linkSrc = link.attr("src"); String linkSrcOld = link.attr("src"); if (StringUtils.isNotBlank(linkSrc) && !linkSrc.contains("http:") && !linkSrc.contains("https:") && !linkSrc.contains("uploadFile/ueditor")) {//不包含http开头的 // String realPath = Encodes.urlDecode(request.getSession().getServletContext().getRealPath(linkSrc)); linkSrc = Encodes.urlDecode(linkSrc); String realPath = UploadImageUtils.getAbsolutePath(linkSrc); int pointerIndex = realPath.lastIndexOf("."); ImageUploadInfo saveImageSerivce = new ImageUploadInfo(); try { saveImageSerivce = ImagePathUtils.saveImageSerivce(realPath, pointerIndex, realPath); String src = photoServer + saveImageSerivce.getSource(); infoContent = StringUtils.replace(infoContent, linkSrcOld, src); } catch (Exception e) { logger.error("图片上传错误:" + e.toString(), e); } } } info.setInfoContent(infoContent); //引导图 String images = info.getGuidanceImage(); if (StringUtils.isNotBlank(images) && !images.contains("http:") && !images.contains("https:")) { images = Encodes.urlDecode(images); String realPath = UploadImageUtils.getAbsolutePath(images); int pointerIndex = realPath.lastIndexOf("."); ImageUploadInfo saveImageSerivce = new ImageUploadInfo(); try { saveImageSerivce = ImagePathUtils.saveImageSerivce(realPath, pointerIndex, realPath); info.setGuidanceImage(photoServer + saveImageSerivce.getSource()); } catch (Exception e) { logger.error("图片上传错误:" + e.toString(), e); } } //商城首页图 String homePageImage = info.getHomePageImage(); if (StringUtils.isNotBlank(homePageImage) && !homePageImage.contains("http:") && !homePageImage.contains("https:")) { homePageImage = Encodes.urlDecode(homePageImage); String realPath = UploadImageUtils.getAbsolutePath(homePageImage); int pointerIndex = realPath.lastIndexOf("."); ImageUploadInfo saveImageSerivce = new ImageUploadInfo(); try { saveImageSerivce = ImagePathUtils.saveImageSerivce(realPath, pointerIndex, realPath); info.setHomePageImage(photoServer + saveImageSerivce.getSource()); } catch (Exception e) { logger.error("图片上传错误:" + e.toString(), e); } } boolean flg = false; if (StringUtils.isBlank(info.getId())) { flg = true; } // 管理员审核文章 if (null != info.getAuditFlag() && 1 == info.getAuditFlag()) { //站内信 MessageModel insideMessageMessageModel = new MessageModel<>(); insideMessageMessageModel.code(MessageType.WEB_INSIDE_MESSAGE) .mqInfo(new MqInfo().topic("MessageLine").delay(1).async(0)); if (null != info.getAuditStatus() && 2 == info.getAuditStatus()) { // 审核通过 info.setPubdate(new Date()); info.setOnlineStatus(2); insideMessageMessageModel .info(new InsideMessage() .shopId(info.getShopId()) .userType(2) .messageType(2) .shopMessType(8) .productId(Integer.valueOf(info.getId())) .content(info.getTitle())); } else { // 审核不通过 info.setOnlineStatus(3); insideMessageMessageModel .info(new InsideMessage() .shopId(info.getShopId()) .userType(2) .messageType(2) .shopMessType(9) .content(info.getTitle()) .reasonContent(info.getFailReason())); } sendExamine(insideMessageMessageModel); } super.save(info); if (flg) { infoDao.insertInfoPraise(info.getId()); } //分段保存信息内容 String page = "
\r\n\t 
";//分页标记 String[] sp = StringUtils.splitByWholeSeparator(infoContent, page); infoDao.deleteInfoPageByInfoId(info.getId()); infoDao.insertInfoPage(info.getId(), sp); } private void sendExamine(MessageModel model) { StringBuilder map = new StringBuilder("{ \"code\": \"WEB_INSIDE_MESSAGE\"," + "\"mqInfo\": {\"topic\": \"MessageLine\",\"delay\": 3,\"async\":0}," + "\"info\": "); map.append("{ \"content\": \"" + model.info().content() + "\"" + ",\"shopMessType\":" + model.info().shopMessType() + ",\"userType\":" + model.info().userType() + ",\"messageType\":" + model.info().messageType()); map.append(",\"shopId\": " + model.info().shopId()); if (8 != model.info().shopMessType()) { map.append(",\"reasonContent\":\"" + model.info().reasonContent() + "\""); } map.append("}}"); try { String post = RequestUtil.httpPost(Global.getConfig("caimei.core") + "/tools/message/send", String.valueOf(map)); if (null==post){ throw new Exception(); } } catch (Exception e) { e.printStackTrace(); logger.error("审批消息推送失败>>>"+e); } } @Transactional(readOnly = false) public void delete(Info info) { // super.delete(info); infoDao.deleteInfo(info.getId()); //infoDao.deleteInfoPraise(info.getId());//删除点赞 //infoDao.deleteInfoPageByInfoId(info.getId());//删除信息分页 } @Transactional(readOnly = false) public void updateEnabledStatusByIds(String enabledStatus, String[] ids) { infoDao.updateEnabledStatusByIds(enabledStatus, ids); } @Transactional(readOnly = false) public void updateRecommendStatusByIds(String recommendStatus, String[] ids) { infoDao.updateRecommendStatusByIds(recommendStatus, ids); } @Transactional(readOnly = false) public void deleteByTypeId(String typeId) { infoDao.deleteByTypeId(typeId); } /** * 替换标签 * * @param label 原标签值 * @param reLabel 要替换的标签值 * @author LG * @date 2016年8月15日 * @version 1.0 */ @Transactional(readOnly = false) public void updateReplaceByLabel(String label, String reLabel) { infoDao.updateReplaceByLabel(label, reLabel); } @Transactional(readOnly = false) public void update(Info info) { infoDao.update(info); } /** * 置顶文章列表 * * @return */ public List findTopList() { Info info = new Info(); info.setTopFlag("1"); return infoDao.findList(info); } /** * 清除置顶,将后面置顶位的数据往前移 * * @param oldInfo */ @Transactional(readOnly = false) public void moveTopList(Info oldInfo) { Integer oldPosition = Integer.parseInt(oldInfo.getTopPosition()); List topInfoList = findTopList(); topInfoList.forEach(topInfo -> { Integer topPosition = Integer.parseInt(topInfo.getTopPosition()); if (Integer.parseInt(topInfo.getTopPosition()) > oldPosition) { topInfo.setTopPosition((--topPosition).toString()); update(topInfo); } }); } @Transactional(readOnly = false) public void auditInfo(Info info) { Date pubdate = null; if (info.getAuditStatus() == 2) { pubdate = new Date(); } infoDao.auditInfo(info.getId(), info.getAuditStatus(), info.getFailReason(), pubdate); } @Transactional(readOnly = false) public void offlineInfo(Integer id) { infoDao.offlineInfo(id); } }