123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- 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.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 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<InfoDao, Info> {
- @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<Info> findList(Info info) {
- return super.findList(info);
- }
- public Page<Info> findPage(Page<Info> page, Info info) {
- if (null != info.getPublishSource() && 2 == info.getPublishSource()) {
- page.setOrderBy("a.createDate desc");
- }
- Page<Info> infoPage = super.findPage(page, info);
- List<Info> 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;
- }
- @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<InfoLabel> 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, "<body", "<div");
- infoContent = StringUtils.replace(infoContent, "</body>", "</div>");
- //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<InsideMessage> 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 = "<div style=\"page-break-after: always;\">\r\n\t<span style=\"display: none;\"> </span></div>";//分页标记
- String[] sp = StringUtils.splitByWholeSeparator(infoContent, page);
- infoDao.deleteInfoPageByInfoId(info.getId());
- infoDao.insertInfoPage(info.getId(), sp);
- }
- private void sendExamine(MessageModel<InsideMessage> model) {
- StringBuilder map = new StringBuilder("{ \"code\": \"WEB_INSIDE_MESSAGE\"," +
- "\"mqInfo\": {\"topic\": \"MessageLine\",\"delay\": 3,\"async\":0}," +
- "\"info\": ");
- map.append("{ \"content\": \"" + model.info().content() +
- "\",\"shopId\": " + model.info().shopId() +
- ",\"userType\":" + model.info().userType() +
- ",\"messageType\":" + model.info().messageType());
- if (null != model.info().shopMessType()) {
- map.append(",\"shopMessType\":" + model.info().shopMessType());
- } else {
- map.append(",\"reasonContent\":\"" + model.info().reasonContent() + "\"");
- }
- map.append("}}");
- try {
- System.out.println(RequestUtil.httpPost("http://localhost:18002/tools/message/send", String.valueOf(map)));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @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<Info> 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<Info> 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);
- }
- }
|