package space.apps.moudules.website.services; import java.io.IOException; import java.net.URLDecoder; import java.sql.SQLException; import java.sql.Timestamp; import java.util.List; import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import apps.pojo.Website; import apps.pojo.WebsiteNavigation; import apps.pojo.WebsiteVisitLog; import space.core.apps.common.BaseTransaction; import space.core.apps.listener.ModuleInitListener; import space.core.apps.util.CloneUtil; import space.core.common.utils.DateTimeUtil; import space.core.common.utils.PojoUtil; import space.core.common.utils.StringUtil; import space.core.db.jdbc.GeneralDBAccess; import space.core.db.jdbc.SqlCommandImpl; /** * 网站服务类 * * @author SPACE * @create by SPACE on 2020年1月19日 */ public class WebsiteService extends BaseTransaction { private static final long serialVersionUID = 1L; private static final String INITIAL_TITLE_SUFFIX = "【初始】"; private static final String PORTAL_INITIAL_ARTICLE_SOURCE = "非繁模板测试数据"; private static final String SCREEN_INITIAL_ARTICLE_SOURCE = "全屏风模板示例数据"; private static final String STUDY_INITIAL_ARTICLE_SOURCE = "招生咨询示例数据"; private static final String COLLEGE_BLUE_INITIAL_ARTICLE_SOURCE = "非繁二级蓝色系模板示例数据"; private static final String TOP_LINK_INITIAL_ARTICLE_SOURCE = "全屏风顶部自定义链接"; static Logger log = LogManager.getLogger(WebsiteService.class); public String add() throws Exception { Website website = (Website) PojoUtil.formatRequest(getRequest(), Website.class); GeneralDBAccess gdba = getGdbaInstance(); website.setCatalogUri("/"); gdba.insertByBean(website); initWtNavigationList(gdba, website.getWtid(), website.getWebsiteid(), getUnit().getUnitid(), getAppUser().getUserId()); gdba.commit(); new ModuleInitListener().initWebsiteAll(); setResponsePackage(getResponseSuccessMsgPackage()); return DEFAULT_FORWARD; } public String change() throws Exception { Website website = (Website) PojoUtil.formatRequest(getRequest(), Website.class); GeneralDBAccess gdba = getGdbaInstance(); gdba.updateById(website, getRequest()); gdba.commit(); new ModuleInitListener().initWebsiteAll(); setResponsePackage(getResponseSuccessMsgPackage()); return DEFAULT_FORWARD; } private void initWtNavigationList(GeneralDBAccess gdba, Long wtId, Long websiteId, Long unitId, Long appUserId) throws Exception { String templateConfig = getWebsiteTemplateConfig(gdba, wtId); boolean ffkjPortalTemplate = isFfkjPortalTemplate(templateConfig); String sql = " Select * from website_navigation where wnccode = 10010 and wtid = :wtId "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("wtId", wtId); List navigationList = gdba.selectAsList(sqlCmd, WebsiteNavigation.class); List cloneNavigationList = (List) CloneUtil.cloneList(navigationList); Timestamp nowTimestamp = DateTimeUtil.getNowTimestamp(); // 复制所有栏目 for (WebsiteNavigation wn : navigationList) { wn.setWnccode(10000L); wn.setWtid(null); wn.setWebsiteid(websiteId); wn.setUnitid(unitId); wn.setCreateTime(nowTimestamp); wn.setCreatePid(appUserId); wn.setNavigationid(null); if (ffkjPortalTemplate) { wn.setName(appendInitialSuffix(wn.getName())); wn.setTitle(appendInitialSuffix(wn.getTitle())); } gdba.insertByBean(wn); } // 增加栏目关联关系 for (WebsiteNavigation wn : navigationList) { if (StringUtil.isNull(wn.getParentNid())) { continue; } wn.setParentNid(getParentNidBySerialNumber(navigationList, getNavigationSerialNumber(cloneNavigationList, wn.getParentNid()))); gdba.updateById(wn); } if (ffkjPortalTemplate) { initFfkjPortalInitialArticleList(gdba, wtId, websiteId, unitId, appUserId); } } private String getWebsiteTemplateConfig(GeneralDBAccess gdba, Long wtId) throws Exception { if (StringUtil.isNull(wtId)) { return null; } String sql = " select config from website_template where wtid = :wtId "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("wtId", wtId); Map templateMap = gdba.selectAsMap(sqlCmd); if (templateMap == null || templateMap.get("config") == null) { return null; } return templateMap.get("config").toString(); } private boolean isFfkjPortalTemplate(String templateConfig) { return "ffkjschool01".equals(templateConfig) || "ffkjcollege1".equals(templateConfig) || "ffkjscreen01".equals(templateConfig) || "ffkjscreen02".equals(templateConfig) || "ffkjcollegeblue1".equals(templateConfig) || "ffkjcollegered1".equals(templateConfig); } private String appendInitialSuffix(String value) { if (StringUtil.isNull(value) || value.endsWith(INITIAL_TITLE_SUFFIX)) { return value; } int maxBaseLength = 252; if (value.length() > maxBaseLength) { value = value.substring(0, maxBaseLength); } return value + INITIAL_TITLE_SUFFIX; } private void initFfkjPortalInitialArticleList(GeneralDBAccess gdba, Long wtId, Long websiteId, Long unitId, Long appUserId) throws Exception { Long referenceWebsiteId = getFfkjPortalReferenceWebsiteId(gdba, wtId, websiteId); if (StringUtil.isNull(referenceWebsiteId)) { log.warn("未找到非繁 portal 模板文章参考站点,跳过新站初始文章复制。websiteId=" + websiteId + ", wtId=" + wtId); return; } String sql = " insert into article ( " + " dcode, websiteid, unitid, ascode, navigationid, title, ajtcode, `desc`, source, thumbnail, " + " sticktop, sticktop_end_time, sticktop_schedulesid, `release`, release_time, release_task_schedulesid, " + " content, edit_content, url, read_count, create_time, last_change_time, create_pid, last_change_pid " + " ) " + " select 61, :websiteId1, :unitId, a.ascode, target_nav.navigationid, " + " replace(case when a.source = :topLinkSource1 then a.title when a.title like :initialTitlePattern then a.title else concat(substring(a.title, 1, 252), :initialSuffix) end, reference_website.name, target_website.name), " + " a.ajtcode, replace(a.`desc`, reference_website.name, target_website.name), a.source, a.thumbnail, " + " a.sticktop, a.sticktop_end_time, a.sticktop_schedulesid, a.`release`, a.release_time, a.release_task_schedulesid, " + " a.content, a.edit_content, a.url, a.read_count, now(), now(), :appUserId1, :appUserId2 " + " from article a " + " join website reference_website on reference_website.websiteid = :referenceWebsiteId1 " + " join website target_website on target_website.websiteid = :websiteId2 " + " join website_navigation source_nav on source_nav.navigationid = a.navigationid " + " join website_navigation target_nav on target_nav.websiteid = :websiteId3 " + " and target_nav.wnccode = 10000 " + " and target_nav.idcard = source_nav.idcard " + " where a.websiteid = :referenceWebsiteId2 " + " and a.source in (:source1, :source2, :source3, :source4, :source5) " + " and not exists ( " + " select 1 from article target_article " + " where target_article.websiteid = :websiteId4 " + " and target_article.navigationid = target_nav.navigationid " + " and target_article.title = replace(case when a.source = :topLinkSource2 then a.title when a.title like :initialTitlePattern2 then a.title else concat(substring(a.title, 1, 252), :initialSuffix2) end, reference_website.name, target_website.name) " + " ) " + " order by a.articleid "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("websiteId1", websiteId).setString("websiteId2", websiteId).setString("websiteId3", websiteId) .setString("websiteId4", websiteId) .setString("unitId", unitId).setString("appUserId1", appUserId).setString("appUserId2", appUserId) .setString("referenceWebsiteId1", referenceWebsiteId).setString("referenceWebsiteId2", referenceWebsiteId) .setString("source1", PORTAL_INITIAL_ARTICLE_SOURCE) .setString("source2", SCREEN_INITIAL_ARTICLE_SOURCE) .setString("source3", STUDY_INITIAL_ARTICLE_SOURCE) .setString("source4", COLLEGE_BLUE_INITIAL_ARTICLE_SOURCE) .setString("source5", TOP_LINK_INITIAL_ARTICLE_SOURCE) .setString("topLinkSource1", TOP_LINK_INITIAL_ARTICLE_SOURCE) .setString("topLinkSource2", TOP_LINK_INITIAL_ARTICLE_SOURCE) .setString("initialSuffix", INITIAL_TITLE_SUFFIX).setString("initialSuffix2", INITIAL_TITLE_SUFFIX) .setString("initialTitlePattern", "%" + INITIAL_TITLE_SUFFIX) .setString("initialTitlePattern2", "%" + INITIAL_TITLE_SUFFIX); gdba.modify(sqlCmd); } private Long getFfkjPortalReferenceWebsiteId(GeneralDBAccess gdba, Long wtId, Long targetWebsiteId) throws Exception { String sql = " select w.websiteid " + " from website w " + " where w.dcode = 61 " + " and w.wtid = :wtId " + " and w.websiteid != :targetWebsiteId " + " and exists ( " + " select 1 from article a " + " where a.websiteid = w.websiteid " + " and a.source in (:source1, :source2, :source3, :source4, :source5) " + " ) " + " order by case when w.id_code in ('local_ffkj_school_site', 'local_ffkj_college_site') then 0 else 1 end, w.websiteid " + " limit 1 "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("wtId", wtId).setString("targetWebsiteId", targetWebsiteId) .setString("source1", PORTAL_INITIAL_ARTICLE_SOURCE) .setString("source2", SCREEN_INITIAL_ARTICLE_SOURCE) .setString("source3", STUDY_INITIAL_ARTICLE_SOURCE) .setString("source4", COLLEGE_BLUE_INITIAL_ARTICLE_SOURCE) .setString("source5", TOP_LINK_INITIAL_ARTICLE_SOURCE); Map websiteMap = gdba.selectAsMap(sqlCmd); if (websiteMap == null || websiteMap.get("websiteid") == null) { return null; } return Long.valueOf(websiteMap.get("websiteid").toString()); } private String getNavigationSerialNumber(List navigationList, Long navigationId) { for (WebsiteNavigation websiteNavigation : navigationList) { if (websiteNavigation.getNavigationid().equals(navigationId)) { return websiteNavigation.getIdcard(); } } return null; } private Long getParentNidBySerialNumber(List navigationList, String serialNumber) throws Exception { for (WebsiteNavigation websiteNavigation : navigationList) { if (websiteNavigation.getIdcard().equals(serialNumber)) { return websiteNavigation.getNavigationid(); } } return null; } /** * 获取有层级关系的网站列表 * * @author SPACE * @throws Exception * @throws SQLException * @date 2020年1月19日 */ public void getWebsiteListApi() throws SQLException, Exception { Long unitId = getUnit().getUnitid(); GeneralDBAccess gdba = getGdbaInstance(); String sql = " select websiteid, name from website where unitid = :unitid and dcode = 61 and parent_wid is null "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("unitid", unitId); List> websiteList = gdba.selectAsMapList(sqlCmd); for (Map map : websiteList) { getWebsiteListApi(gdba, map, unitId, Long.valueOf(map.get("websiteid").toString())); } write(getResponseSuccessDataPackage(websiteList).toJson()); } void getWebsiteListApi(GeneralDBAccess gdba, Map websiteMap, Long unitId, Long parWebsiteId) throws SQLException, Exception { String sql = " select websiteid, name from website where unitid = :unitid and dcode = 61 and parent_wid = :parWebsiteId "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("unitid", unitId).setString("parWebsiteId", parWebsiteId); List> websiteList = gdba.selectAsMapList(sqlCmd); if (websiteList.size() == 0) { return; } websiteMap.put("children", websiteList); for (Map map : websiteList) { getWebsiteListApi(gdba, map, unitId, Long.valueOf(map.get("websiteid").toString())); } } /** * 增加网站访问量 * * @author SPACE * @throws Exception * @throws IOException * @log Sep 26, 2020 8:49:26 PM */ public void addWebsiteVisit() throws Exception { try { String address = getParameter("address"); if (StringUtil.notNull(address)) { address = URLDecoder.decode(address, "utf-8"); } else { address = " Address is null "; } WebsiteVisitLog wvl = new WebsiteVisitLog(); wvl.setWebsiteid(getWebsite().getWebsiteid()); wvl.setWstcode(10000L); wvl.setIp(getRequest().getLocalAddr()); wvl.setUrl(address); wvl.setMsg(getParameter("msg")); GeneralDBAccess gdba = getGdbaInstance(); gdba.insertByBean(wvl); // 增加文章阅读量 addArticleRead(gdba, getParameter("pathname")); gdba.commit(); } catch (Exception e) { log.error("增加网站访问记录时失败,URL=" + getRequest().getRequestURL().toString(), e); } write(""); } /** * 根据文章的URL增加一次文章阅读量 * * @param gdba 数据库 * @param uri URI * @throws Exception * @author SPACE * @log Sep 26, 2020 10:11:00 PM */ private void addArticleRead(GeneralDBAccess gdba, String uri) throws Exception { if (StringUtil.isNull(uri) || "/".equals(uri)) { return; } String sql = " update article set read_count = (read_count + 1) where url = :url "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("url", uri); int result = gdba.modify(sqlCmd); if (result == 1) { return; } // 处理单页的情况 String sql2 = " update article set read_count = (read_count + 1) where articleid =" + " (Select articleid from (Select a.articleid from article a, website_navigation wn where a.navigationid = wn.navigationid " + "and wn.wntcode = 10060 and a.dcode = 61 and wn.url = :url order by a.create_time desc limit 0,1) as tempArticle )"; SqlCommandImpl sqlCmd2 = new SqlCommandImpl(sql2); sqlCmd2.setString("url", uri); gdba.modify(sqlCmd2); } /** * 用于流程条件的接口 * * @throws SQLException * @throws Exception * * @author SPACE * @log Create on 2021年1月23日 下午9:39:52 */ public void getAllWebsiteApi() throws SQLException, Exception { GeneralDBAccess gdba = getGdbaInstance(); String sql = " select websiteid as 'id', name, logo as 'icon_path' from website where dcode = 61 order by websiteid asc "; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); List> websiteList = gdba.selectAsMapList(sqlCmd); write(getResponseSuccessDataPackage(websiteList).toJson()); } }