package space.apps.moudules.website.template.common; import java.util.ArrayList; import java.util.List; import java.util.Map; import apps.pojo.Website; import apps.pojo.WebsiteNavigation; import space.apps.moudules.website.template.common.WebsiteTmplateConstvars.navigationType; import space.apps.moudules.website.template.common.bean.NavigationMiniView; import space.apps.moudules.website.template.util.WebsiteDataCacheUtil; import space.apps.moudules.website.template.util.WebsiteTemplateArticleUtil; import space.apps.moudules.website.template.util.WebsiteTemplateNavUtil; import space.apps.moudules.website.template.util.WebsiteUtil; import space.common.exception.SpaceNullPointerException; import space.core.apps.jedis.JedisUtil; import space.core.db.bean.base.PageBean; import space.core.db.jdbc.GeneralDBAccess; /** * 网站栏目管理 * * @author SPACE * @log Oct 6, 2020 7:22:19 PM */ public class WebsiteNavManager { /** * 根据网站获取所有栏目 * * @param gdba 数据库 * @param website 网站对象 * @return 栏目列表 * @throws Exception * @author SPACE * @log Oct 6, 2020 7:21:53 PM */ @SuppressWarnings("unchecked") public static List> getNavigationByWebsite(GeneralDBAccess gdba, Website website) throws Exception { return getNavigationByWebsite(gdba, website, false); } @SuppressWarnings("unchecked") public static List> getNavigationByWebsite(GeneralDBAccess gdba, Website website, boolean promoteHiddenCatalogChildren) throws Exception { String key = promoteHiddenCatalogChildren ? WebsiteDataCacheUtil.generateWebsitePromotedHeadNavAllCacheKey(website.getWebsiteid()) : WebsiteDataCacheUtil.generateWebsiteHeadNavAllCacheKey(website.getWebsiteid()); if (JedisUtil.exists(key)) { return (List>) JedisUtil.obj.get(key); } List> navList = promoteHiddenCatalogChildren ? WebsiteTemplateNavUtil.getWebsitePromotedAllTerminalNav(gdba, website) : WebsiteTemplateNavUtil.getWebsiteAllTerminalNav(gdba, website); WebsiteTemplateNavUtil.clearNavOtherField(navList); JedisUtil.obj.setMinute10(key, navList); return navList; } @SuppressWarnings("unchecked") public static List> getPCNavigationByWebsite(GeneralDBAccess gdba, Website website) throws Exception { return getPCNavigationByWebsite(gdba, website, false); } @SuppressWarnings("unchecked") public static List> getPCNavigationByWebsite(GeneralDBAccess gdba, Website website, boolean promoteHiddenCatalogChildren) throws Exception { String key = promoteHiddenCatalogChildren ? WebsiteDataCacheUtil.generateWebsitePromotedHeadNavPCCacheKey(website.getWebsiteid()) : WebsiteDataCacheUtil.generateWebsiteHeadNavPCCacheKey(website.getWebsiteid()); if (JedisUtil.exists(key)) { return (List>) JedisUtil.obj.get(key); } List> navList = promoteHiddenCatalogChildren ? WebsiteTemplateNavUtil.getWebsitePromotedPCTerminalNav(gdba, website) : WebsiteTemplateNavUtil.getWebsitePCTerminalNav(gdba, website); WebsiteTemplateNavUtil.clearNavOtherField(navList); JedisUtil.obj.setMinute10(key, navList); return navList; } @SuppressWarnings("unchecked") public static List> getMobileNavigationByWebsite(GeneralDBAccess gdba, Website website) throws Exception { return getMobileNavigationByWebsite(gdba, website, false); } @SuppressWarnings("unchecked") public static List> getMobileNavigationByWebsite(GeneralDBAccess gdba, Website website, boolean promoteHiddenCatalogChildren) throws Exception { String key = promoteHiddenCatalogChildren ? WebsiteDataCacheUtil.generateWebsitePromotedHeadNavMobileCacheKey(website.getWebsiteid()) : WebsiteDataCacheUtil.generateWebsiteHeadNavMobileCacheKey(website.getWebsiteid()); if (JedisUtil.exists(key)) { return (List>) JedisUtil.obj.get(key); } List> navList = promoteHiddenCatalogChildren ? WebsiteTemplateNavUtil.getWebsitePromotedMobileTerminalNav(gdba, website) : WebsiteTemplateNavUtil.getWebsiteMobileTerminalNav(gdba, website); WebsiteTemplateNavUtil.clearNavOtherField(navList); JedisUtil.obj.setMinute10(key, navList); return navList; } /** * 获取下一级子栏目 * * @param gdba 数据库 * @param website 文章 * @param navID 栏目ID * @author SPACE * @throws Exception * @log Oct 7, 2020 6:24:18 PM */ @SuppressWarnings("unchecked") public static List> getChildrenNavigation(GeneralDBAccess gdba, Website website, Long navID) throws Exception { String key = WebsiteDataCacheUtil.generateNavChildrenCacheKey(website.getWebsiteid(), navID); if (JedisUtil.exists(key)) { Object object = JedisUtil.obj.get(key); if (object instanceof String) { return null; } else { return (List>) object; } } List> childrenNavList = WebsiteTemplateNavUtil.getChildrenNav(gdba, website, navID); if (childrenNavList == null) { JedisUtil.obj.setSecond50(key, WebsiteDataCacheUtil.DATA_NULL); return null; } WebsiteTemplateNavUtil.clearNavOtherField(childrenNavList); JedisUtil.obj.setSecond50(key, childrenNavList); return childrenNavList; } /** * 获取子栏目的 mini 视图列表 * * @param gdba 数据库 * @param website 网站 * @param navID 栏目ID * @author SPACE * @throws Exception * @log Oct 8, 2020 4:47:21 AM */ @SuppressWarnings("unchecked") public static List getChildrenNavigationMiniView(GeneralDBAccess gdba, Website website, Long navID) throws Exception { return getChildrenNavigationMiniView2(gdba, website, navID, Integer.valueOf(3)); } /** * 获取子栏目的 mini 视图列表,并指定每个列表栏目返回的文章数。 * 保留该方法用于兼容旧生产自定义模板编译产物。 */ @SuppressWarnings("unchecked") public static List getChildrenNavigationMiniView2(GeneralDBAccess gdba, Website website, Long navID, Integer dataSize) throws Exception { String key = WebsiteDataCacheUtil.generateNavMiniViewCacheKey(website.getWebsiteid(), navID); if (JedisUtil.exists(key)) { return (List) JedisUtil.obj.get(key); } List> childrenNavigation = getChildrenNavigation(gdba, website, navID); if (childrenNavigation == null || childrenNavigation.size() == 0) { return null; } List navMiniList = new ArrayList(); for (Map navMap : childrenNavigation) { PageBean pb = new PageBean(); Long wntCode = Long.valueOf(navMap.get("wntcode").toString()); Long navigationID = Long.valueOf(navMap.get("navigationid").toString()); // 列表 if (navigationType.LIST.getCode().equals(wntCode)) { pb.setRowCountPerPage(dataSize == null ? 15 : dataSize.intValue()); } // 单页 else if (navigationType.NAV_PAGE.getCode().equals(wntCode)) { continue; // pb.setRowCountPerPage(1); } // 其他类别的跳出 else { continue; } // 查询文章列表 List> articleList = WebsiteTemplateArticleUtil.getArticleList(gdba, navigationID, pb); if (articleList.size() == 0) { continue; } NavigationMiniView nmv = new NavigationMiniView(navMap, articleList); navMiniList.add(nmv); } JedisUtil.obj.setSecond50(key, navMiniList); return navMiniList; } /** * 根据栏目身份证获取文章列表 * * @param gdba 数据库 * @param website 网站 * @param idCard 栏目身份证 * @param dataSize 数据条数 * @return 文章列表 * @throws Exception * @author SPACE * @log Oct 9, 2020 5:23:07 AM */ public static NavigationMiniView getArticleListByNavIDCard(GeneralDBAccess gdba, Website website, String idCard, int dataSize) throws Exception { return getArticleListByNavIDCard(gdba, website, idCard, dataSize, false); } /** * 根据栏目身份证获取只有缩略图的文章列表 * * @param gdba 数据库 * @param website 网站 * @param idCard 栏目身份证 * @param dataSize 数据条数 * @return 文章列表 * @throws Exception * @author SPACE * @log Oct 9, 2020 5:24:45 AM */ public static NavigationMiniView getArticleThumbnailListByNavIDCard(GeneralDBAccess gdba, Website website, String idCard, int dataSize) throws Exception { return getArticleListByNavIDCard(gdba, website, idCard, dataSize, true); } /** * 根据栏目身份证获取文章列表 * * @param gdba 数据库 * @param website 网站 * @param idCard 栏目身份证 * @param dataSize 数据条数 * @param thumbnailArticleFlag 是否只查询带有缩略图的文章,为真则查询,否则混合查询 * @return 文章列表 * @author SPACE * @throws Exception * @log Oct 8, 2020 7:57:47 AM */ private static NavigationMiniView getArticleListByNavIDCard(GeneralDBAccess gdba, Website website, String idCard, int dataSize, boolean thumbnailArticleFlag) throws Exception { String key = WebsiteDataCacheUtil.generateNavMiniViewByIDCardCacheKey(website.getWebsiteid(), idCard, thumbnailArticleFlag); if (JedisUtil.exists(key)) { return (NavigationMiniView) JedisUtil.obj.get(key); } Map navMap = WebsiteTemplateNavUtil.getNavigationByIDCardAsMap(gdba, website, idCard); if (navMap == null) { throw new SpaceNullPointerException("栏目 IDCard“" + idCard + "”不存在"); } PageBean pb = new PageBean(); pb.setRowCountPerPage(dataSize); List> articleList = WebsiteTemplateArticleUtil.getArticleList(gdba, Long.valueOf(navMap.get("navigationid").toString()), thumbnailArticleFlag, pb); NavigationMiniView navMiniView = new NavigationMiniView(navMap, articleList); JedisUtil.obj.setSecond50(key, navMiniView); return navMiniView; } /** * 获取子栏目的文章列表栏目视图 * * @param gdba 数据库 * @param website 文章 * @param idCard 栏目身份证 * @param dataSize 数据条数 * @return 栏目mini视图对象 * @throws Exception * @author SPACE * @log Oct 9, 2020 11:36:00 PM */ public static NavigationMiniView getNavChildrenArticleListByNavIDCard(GeneralDBAccess gdba, Website website, String idCard, int dataSize) throws Exception { return getNavChildrenArticleListByNavIDCard(gdba, website, idCard, dataSize, false); } /** * 获取子栏目的文章列表栏目视图(查询带缩略图的) * * @param gdba 数据库 * @param website 文章 * @param idCard 栏目身份证 * @param dataSize 数据条数 * @return 栏目mini视图对象 * @throws Exception * @author SPACE * @log Oct 9, 2020 11:36:00 PM */ public static NavigationMiniView getNavChildrenArticleListByNavIDCardThumbnailArticleFlag(GeneralDBAccess gdba, Website website, String idCard, int dataSize, boolean thumbnailArticleFlag) throws Exception { return getNavChildrenArticleListByNavIDCard(gdba, website, idCard, dataSize, thumbnailArticleFlag); } /** * 获取子栏目的文章列表栏目视图 * * @param gdba 数据库 * @param website 网站 * @param idCard 栏目身份证 * @param dataSize 数据条数 * @param thumbnailArticleFlag 是否只查询带有缩略图的文章,为真则查询,否则混合查询 * @return 栏目视图 * @throws Exception * @author SPACE * @log Oct 9, 2020 9:09:23 AM */ public static NavigationMiniView getNavChildrenArticleListByNavIDCard(GeneralDBAccess gdba, Website website, String idCard, int dataSize, boolean thumbnailArticleFlag) throws Exception { String key = WebsiteDataCacheUtil.generateNavChildrenMiniViewByIDCardCacheKey(website.getWebsiteid(), idCard, thumbnailArticleFlag); if (JedisUtil.exists(key)) { return (NavigationMiniView) JedisUtil.obj.get(key); } Map navMap = WebsiteTemplateNavUtil.getNavigationByIDCardAsMap(gdba, website, idCard); if (navMap == null) { throw new SpaceNullPointerException("栏目 IDCard“" + idCard + "”不存在"); } PageBean pb = new PageBean(); pb.setRowCountPerPage(dataSize); List> articleList = WebsiteTemplateArticleUtil.getAllChildrenArticleList(gdba, Long.valueOf(navMap.get("navigationid").toString()), false, pb); NavigationMiniView navMiniView = new NavigationMiniView(navMap, articleList); JedisUtil.obj.setSecond50(key, navMiniView); return navMiniView; } /** * 获取栏目对象 * * @param gdba 数据库 * @param website 网站 * @param navID 栏目ID * @return 栏目对象 * @author SPACE * @throws Exception * @log Oct 8, 2020 8:37:17 PM */ public static WebsiteNavigation getNavigationByID(GeneralDBAccess gdba, Website website, Long navID) throws Exception { String key = WebsiteDataCacheUtil.generateNavByIDCacheKey(website.getWebsiteid(), navID); if (JedisUtil.exists(key)) { Object object = JedisUtil.obj.get(key); if (object instanceof String) { return null; } else { return (WebsiteNavigation) object; } } WebsiteNavigation nav = null; try { nav = WebsiteTemplateNavUtil.getNavigationByID(gdba, navID); JedisUtil.obj.setSecond50(key, nav); return nav; } catch (Exception e) { JedisUtil.obj.setSecond50(key, WebsiteDataCacheUtil.DATA_NULL); throw e; } } /** * 获取栏目对象 * * @param gdba 数据库 * @param website 网站 * @param uri URL * @return 栏目对象 * @author SPACE * @throws Exception * @log Oct 8, 2020 8:59:11 PM */ public static WebsiteNavigation getNavigationByURI(GeneralDBAccess gdba, Website website, String uri) throws Exception { String key = WebsiteDataCacheUtil.generateNavByURICacheKey(website.getWebsiteid(), uri); if (JedisUtil.exists(key)) { Object object = JedisUtil.obj.get(key); if (object instanceof String) { return null; } else { return (WebsiteNavigation) object; } } WebsiteNavigation nav = null; try { nav = WebsiteTemplateNavUtil.getNavigationByURI(gdba, website, uri); JedisUtil.obj.setSecond50(key, nav); return nav; } catch (Exception e) { JedisUtil.obj.setSecond50(key, WebsiteDataCacheUtil.DATA_NULL); throw e; } } /** * 获取最顶级的父级栏目对象 * * @param gdba 数据库 * @param website 网站对象 * @param navID 栏目ID * @return 栏目对象 * @throws Exception * * @author SPACE * @log Create on 2021年1月9日 下午10:13:24 */ public static WebsiteNavigation getTopParent(GeneralDBAccess gdba, Website website, Long navID) throws Exception { List navParentList = getNavParentList(gdba, website, navID); if (navParentList == null || navParentList.size() == 0) { return null; } return navParentList.get(0); } /** * 获取上级栏目链(包含自己) * * @param gdba 数据库 * @param navID 栏目ID * @return 栏目列表 * @throws Exception * @author SPACE * @log Oct 8, 2020 9:05:33 PM */ @SuppressWarnings("unchecked") public static List getNavParentList(GeneralDBAccess gdba, Website website, Long navID) throws Exception { String key = WebsiteDataCacheUtil.generateNavParentListCacheKey(website.getWebsiteid(), navID); if (JedisUtil.exists(key)) { Object object = JedisUtil.obj.get(key); if (object instanceof String) { return null; } else { return (List) object; } } List navParentList = null; try { navParentList = WebsiteTemplateNavUtil.getNavParentList(gdba, navID); JedisUtil.obj.setSecond50(key, navParentList); return navParentList; } catch (Exception e) { JedisUtil.obj.setSecond50(key, WebsiteDataCacheUtil.DATA_NULL); throw e; } } /** * 增加带协议和端口的完整栏目链接到栏目对象中,替换键“url” * * @param website 网站 * @param navigationMap 栏目对象 * @author SPACE * @log Oct 9, 2020 11:19:39 PM */ public static void addWebsiteDomainFullURLToNavObj(Website website, Map navigationMap) { String domain = WebsiteUtil.disposeWebsiteDomain(website); String navURL = navigationMap.get("url").toString(); if (navURL.startsWith("/")) { navURL = navURL.substring(1, navURL.length()); } navigationMap.put("url", domain + navURL); } }