package space.apps.moudules.website.template.ffkjcollegered1; import apps.pojo.Article; import apps.pojo.Website; import apps.pojo.WebsiteNavigation; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import space.apps.moudules.basic.util.SortOrderUtil; import space.apps.moudules.website.template.common.WebsiteArticleManager; import space.apps.moudules.website.template.common.WebsiteCommonService; import space.apps.moudules.website.template.common.WebsiteNavManager; 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.WebsiteTemplateArticleUtil; import space.apps.moudules.website.template.util.WebsiteTemplateNavUtil; import space.core.apps.common.BaseTransaction; import space.core.db.bean.base.PageBean; import space.core.db.jdbc.GeneralDBAccess; import space.core.db.jdbc.SqlCommandImpl; /** * 非繁二级网站模板-红色服务。 */ public class WebSiteArticleService extends BaseTransaction { private static final long serialVersionUID = 1L; private static final String SCREEN_RED_THEME_CLASS = "screen-red-theme"; private static final String HOME_GROUP_NAV_IDCARD = "FFKJ_COLLEGE_HOME_GROUP"; private static final String UTILITY_LINK_NAV_IDCARD = "FFKJ_COLLEGE_SCHOOL_HOME"; private static final String FOOTER_INFO_NAV_IDCARD = "FFKJ_COLLEGE_FOOTER_INFO"; private static final String FOOTER_LINK_NAV_IDCARD = "FFKJ_COLLEGE_FOOTER_LINKS"; private static final String INITIAL_TITLE_SUFFIX = "【初始】"; private static final String DEFAULT_SCHOOL_HOME_NAME = "学校首页"; private static final String DEFAULT_SCHOOL_HOME_URL = "https://hxcs.feifan.net/"; private static final String DEFAULT_SITE_HOME_NAME = "本站首页"; private static final String DEFAULT_SITE_HOME_URL = "/"; private static final String DEFAULT_FOOTER_ADDRESS = "中国 广东省广州市从化城鳌大道东772号"; private static final String DEFAULT_FOOTER_OFFICE_PHONE = "校办电话:020-87868999"; private static final String DEFAULT_FOOTER_ADMISSION_PHONE = "招生电话:020-87865222"; private static final String DEFAULT_FOOTER_EMPLOYMENT_PHONE = "就业办:020-87868935"; private static final String DEFAULT_FOOTER_COPYRIGHT_ENGLISH = "Copyright © Guangzhou Huaxia Vocational College All Rights Reserved"; private static final String DEFAULT_FOOTER_BEIAN_TEXT = "粤ICP备12042111号"; private static final String DEFAULT_FOOTER_BEIAN_URL = "https://beian.miit.gov.cn/"; public String controller() throws Exception { GeneralDBAccess gdba = getGdbaInstance(); Website website = getWebsite(); setThemeAttributes(gdba, website); WebsiteNavigation nav = WebsiteNavManager.getNavigationByURI(gdba, website, getRequest().getRequestURI()); if (nav != null && navigationType.CATALOG.getCode().equals(nav.getWntcode())) { setCatalogAttributes(gdba, website, nav); return "toCatalog"; } return WebsiteCommonService.controller(gdba, website, 10, getRequest()); } public String details() throws Exception { GeneralDBAccess gdba = getGdbaInstance(); Website website = getWebsite(); setThemeAttributes(gdba, website); String forward = WebsiteCommonService.details(gdba, website, getRequest()); setArticleExtras(gdba, website); return forward; } public String index() throws Exception { GeneralDBAccess gdba = getGdbaInstance(); Website website = getWebsite(); WebsiteCommonService.selectHeadNavigation(gdba, website, getRequest()); setRequestAttribute("website", website); setThemeAttributes(gdba, website); setNavView(gdba, website, "collegeHeroNmv", "FFKJ_COLLEGE_HOME_HERO", 3, true); setNavView(gdba, website, "collegeNewsNmv", "FFKJ_COLLEGE_NEWS", 5, false); setNavView(gdba, website, "collegeNoticeNmv", "FFKJ_COLLEGE_NOTICE", 4, false); setNavView(gdba, website, "collegeMajorNmv", "FFKJ_COLLEGE_MAJOR", 4, false); setHomeSectionRows(gdba, website); return DEFAULT_FORWARD; } public String search() throws Exception { GeneralDBAccess gdba = getGdbaInstance(); Website website = getWebsite(); String forward = WebsiteCommonService.search(gdba, website, getRequest(), 10); setThemeAttributes(gdba, website); return forward; } private void setThemeAttributes(GeneralDBAccess gdba, Website website) { setRequestAttribute("portalThemeClass", SCREEN_RED_THEME_CLASS); setRequestAttribute("portalThemeStyle", buildPortalThemeStyle(website)); setUtilityLinks(gdba, website); setFooterInfo(gdba, website); setFooterLinks(gdba, website); } private String buildPortalThemeStyle(Website website) { String themeColor = getThemeColor(website); if (themeColor.length() == 0) { return ""; } int[] rgb = parseHexColor(themeColor); int[] darkRgb = blendColor(rgb, new int[] { 0, 0, 0 }, 0.2); int[] brightRgb = blendColor(rgb, new int[] { 255, 255, 255 }, 0.14); String darkColor = toHexColor(darkRgb); String brightColor = toHexColor(brightRgb); return "body.screen-red-theme,.screen-red-theme{--hx-red:" + themeColor + ";--hx-red-dark:" + darkColor + ";--hx-red-bright:" + brightColor + ";--hx-red-rgb:" + toRgbText(rgb) + ";--hx-red-dark-rgb:" + toRgbText(darkRgb) + ";--hx-red-bright-rgb:" + toRgbText(brightRgb) + ";}"; } private String getThemeColor(Website website) { if (website == null || website.getWhiteLogoFile() == null) { return ""; } String value = website.getWhiteLogoFile().trim(); if (!value.matches("#[0-9a-fA-F]{6}")) { return ""; } return value.toLowerCase(); } private int[] parseHexColor(String hexColor) { return new int[] { Integer.parseInt(hexColor.substring(1, 3), 16), Integer.parseInt(hexColor.substring(3, 5), 16), Integer.parseInt(hexColor.substring(5, 7), 16) }; } private int[] blendColor(int[] sourceRgb, int[] targetRgb, double ratio) { int[] result = new int[3]; for (int index = 0; index < result.length; index++) { result[index] = clampColor((int) Math.round(sourceRgb[index] * (1 - ratio) + targetRgb[index] * ratio)); } return result; } private int clampColor(int value) { if (value < 0) { return 0; } if (value > 255) { return 255; } return value; } private String toHexColor(int[] rgb) { return "#" + toHexPart(rgb[0]) + toHexPart(rgb[1]) + toHexPart(rgb[2]); } private String toHexPart(int value) { String hex = Integer.toHexString(clampColor(value)); return hex.length() == 1 ? "0" + hex : hex; } private String toRgbText(int[] rgb) { return rgb[0] + ", " + rgb[1] + ", " + rgb[2]; } private void setUtilityLinks(GeneralDBAccess gdba, Website website) { String schoolHomeName = DEFAULT_SCHOOL_HOME_NAME; String schoolHomeUrl = DEFAULT_SCHOOL_HOME_URL; String siteHomeName = DEFAULT_SITE_HOME_NAME; String siteHomeUrl = DEFAULT_SITE_HOME_URL; try { setRequestAttribute("utilityLinksNmv", WebsiteNavManager.getArticleListByNavIDCard(gdba, website, UTILITY_LINK_NAV_IDCARD, 8)); Map schoolHome = getUtilityLinkArticle(gdba, website, DEFAULT_SCHOOL_HOME_NAME); schoolHomeName = firstNotBlank(schoolHome.get("title"), schoolHomeName); schoolHomeUrl = firstNotBlank(schoolHome.get("url"), schoolHomeUrl); Map siteHome = getUtilityLinkArticle(gdba, website, DEFAULT_SITE_HOME_NAME); siteHomeName = firstNotBlank(siteHome.get("title"), siteHomeName); siteHomeUrl = firstNotBlank(siteHome.get("url"), siteHomeUrl); } catch (Exception e) { // 缺少隐藏栏目或文章时保持默认入口,不影响页面打开。 setRequestAttribute("utilityLinksNmv", new NavigationMiniView()); } setRequestAttribute("schoolHomeName", schoolHomeName); setRequestAttribute("schoolHomeUrl", schoolHomeUrl); setRequestAttribute("siteHomeName", siteHomeName); setRequestAttribute("siteHomeUrl", siteHomeUrl); } private void setFooterInfo(GeneralDBAccess gdba, Website website) { String websiteName = website == null ? "" : firstNotBlank(website.getName(), ""); String footerAddress = DEFAULT_FOOTER_ADDRESS; String footerOfficePhone = DEFAULT_FOOTER_OFFICE_PHONE; String footerAdmissionPhone = DEFAULT_FOOTER_ADMISSION_PHONE; String footerEmploymentPhone = DEFAULT_FOOTER_EMPLOYMENT_PHONE; String footerOwner = "版权所有:" + websiteName; String footerLogo = ""; String footerLogoAlt = websiteName; String footerCopyright = DEFAULT_FOOTER_COPYRIGHT_ENGLISH; String footerBeianText = DEFAULT_FOOTER_BEIAN_TEXT; String footerBeianUrl = DEFAULT_FOOTER_BEIAN_URL; try { NavigationMiniView footerInfoNmv = WebsiteNavManager.getArticleListByNavIDCard(gdba, website, FOOTER_INFO_NAV_IDCARD, 16); setRequestAttribute("footerInfoNmv", footerInfoNmv); Map> footerInfoArticleMap = buildFooterInfoArticleMap(footerInfoNmv); footerAddress = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "地址"), footerAddress); footerOfficePhone = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "校办电话"), footerOfficePhone); footerAdmissionPhone = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "招生电话"), footerAdmissionPhone); footerEmploymentPhone = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "就业办"), footerEmploymentPhone); footerOwner = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "版权所有"), footerOwner); footerLogo = firstNotBlank(getFooterInfoThumbnail(footerInfoArticleMap, "底部LOGO"), footerLogo); footerLogoAlt = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "底部LOGO"), footerLogoAlt); footerCopyright = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "版权英文"), footerCopyright); footerBeianText = firstNotBlank(getFooterInfoDesc(footerInfoArticleMap, "备案信息"), footerBeianText); footerBeianUrl = firstNotBlank(getFooterInfoUrl(footerInfoArticleMap, "备案信息"), footerBeianUrl); } catch (Exception e) { setRequestAttribute("footerInfoNmv", new NavigationMiniView()); } setRequestAttribute("footerAddressText", footerAddress); setRequestAttribute("footerOfficePhoneText", footerOfficePhone); setRequestAttribute("footerAdmissionPhoneText", footerAdmissionPhone); setRequestAttribute("footerEmploymentPhoneText", footerEmploymentPhone); setRequestAttribute("footerOwnerText", footerOwner); setRequestAttribute("footerLogoUrl", footerLogo); setRequestAttribute("footerLogoAlt", footerLogoAlt); setRequestAttribute("footerCopyrightText", footerCopyright); setRequestAttribute("footerBeianText", footerBeianText); setRequestAttribute("footerBeianUrl", footerBeianUrl); } private Map> buildFooterInfoArticleMap(NavigationMiniView footerInfoNmv) { Map> footerInfoArticleMap = new HashMap>(); if (footerInfoNmv == null || footerInfoNmv.getArticleList() == null) { return footerInfoArticleMap; } for (Map articleMap : footerInfoNmv.getArticleList()) { if (articleMap == null || articleMap.get("title") == null) { continue; } String title = normalizeFooterInfoTitle(articleMap.get("title").toString()); if (title.length() > 0 && !footerInfoArticleMap.containsKey(title)) { footerInfoArticleMap.put(title, articleMap); } } return footerInfoArticleMap; } private String normalizeFooterInfoTitle(String title) { if (title == null) { return ""; } String normalizedTitle = title.trim(); while (normalizedTitle.endsWith(INITIAL_TITLE_SUFFIX)) { normalizedTitle = normalizedTitle.substring(0, normalizedTitle.length() - INITIAL_TITLE_SUFFIX.length()) .trim(); } return normalizedTitle; } private Object getFooterInfoDesc(Map> footerInfoArticleMap, String title) { return getFooterInfoField(footerInfoArticleMap, title, "desc"); } private Object getFooterInfoThumbnail(Map> footerInfoArticleMap, String title) { return getFooterInfoField(footerInfoArticleMap, title, "thumbnail"); } private Object getFooterInfoUrl(Map> footerInfoArticleMap, String title) { return getFooterInfoField(footerInfoArticleMap, title, "url"); } private Object getFooterInfoField(Map> footerInfoArticleMap, String title, String field) { if (footerInfoArticleMap == null || !footerInfoArticleMap.containsKey(title)) { return null; } return footerInfoArticleMap.get(title).get(field); } private void setFooterLinks(GeneralDBAccess gdba, Website website) { try { setRequestAttribute("footerLinksNmv", WebsiteNavManager.getArticleListByNavIDCard(gdba, website, FOOTER_LINK_NAV_IDCARD, 8)); } catch (Exception e) { setRequestAttribute("footerLinksNmv", new NavigationMiniView()); } } private Map getUtilityLinkArticle(GeneralDBAccess gdba, Website website, String title) throws Exception { String sql = "SELECT a.title, a.url FROM article a " + "JOIN website_navigation wn ON wn.navigationid = a.navigationid " + "WHERE a.dcode = 61 AND a.ascode = '10010' AND a.`release` = 1 " + "AND wn.dcode = 61 AND wn.wnccode = 10000 " + "AND wn.websiteid = :websiteid AND wn.idcard = :idcard " + "AND a.title = :title " + "ORDER BY " + SortOrderUtil.articleSort("a") + " LIMIT 1"; SqlCommandImpl sqlCmd = new SqlCommandImpl(sql); sqlCmd.setString("websiteid", website.getWebsiteid()).setString("idcard", UTILITY_LINK_NAV_IDCARD) .setString("title", title); Map article = gdba.selectAsMap(sqlCmd); return article == null ? new java.util.HashMap() : article; } private String firstNotBlank(Object value, String fallback) { if (value == null) { return fallback; } String text = value.toString().trim(); return text.length() == 0 ? fallback : text; } private void setNavView(GeneralDBAccess gdba, Website website, String attrName, String idcard, int size, boolean thumbnailOnly) throws Exception { try { NavigationMiniView nmv = thumbnailOnly ? WebsiteNavManager.getArticleThumbnailListByNavIDCard(gdba, website, idcard, size) : WebsiteNavManager.getArticleListByNavIDCard(gdba, website, idcard, size); setRequestAttribute(attrName, nmv); } catch (Exception e) { setRequestAttribute(attrName, new NavigationMiniView()); } } private void setHomeSectionRows(GeneralDBAccess gdba, Website website) throws Exception { List sectionList = new ArrayList(); List> homeChildNavList = getHomeChildNavigation(gdba, website); if (homeChildNavList != null) { for (Map navMap : getHomeSectionNavigationList(homeChildNavList)) { if (!isDynamicHomeSection(navMap)) { continue; } try { NavigationMiniView section = buildNavigationMiniView(gdba, navMap, 5); sectionList.add(section); } catch (Exception e) { // 单个自定义栏目数据异常时跳过,避免影响首页其他栏目。 } } } setRequestAttribute("collegeHomeSectionRows", groupSections(sectionList, 2)); } private List> getHomeSectionNavigationList(List> homeChildNavList) { List> sectionNavList = new ArrayList>(); boolean topBoundaryFound = false; for (Map navMap : homeChildNavList) { if (hasIDCard(navMap, UTILITY_LINK_NAV_IDCARD)) { topBoundaryFound = true; continue; } if (hasIDCard(navMap, FOOTER_LINK_NAV_IDCARD)) { if (topBoundaryFound) { break; } continue; } if (topBoundaryFound) { sectionNavList.add(navMap); } } if (topBoundaryFound) { return sectionNavList; } return homeChildNavList; } private boolean hasIDCard(Map navMap, String idcard) { return navMap != null && navMap.get("idcard") != null && idcard.equals(navMap.get("idcard").toString()); } private List> getHomeChildNavigation(GeneralDBAccess gdba, Website website) throws Exception { Map homeGroupMap = WebsiteTemplateNavUtil.getNavigationByIDCardAsMap(gdba, website, HOME_GROUP_NAV_IDCARD); if (homeGroupMap == null || homeGroupMap.get("navigationid") == null) { return null; } return WebsiteTemplateNavUtil.getWebsiteALLConcealTerminalNav(gdba, website, Long.valueOf(homeGroupMap.get("navigationid").toString())); } private boolean isDynamicHomeSection(Map navMap) { if (navMap == null || navMap.get("idcard") == null || navMap.get("wntcode") == null) { return false; } String idcard = navMap.get("idcard").toString(); if ("FFKJ_COLLEGE_HOME_HERO".equals(idcard) || "FFKJ_COLLEGE_NEWS".equals(idcard) || "FFKJ_COLLEGE_NOTICE".equals(idcard) || "FFKJ_COLLEGE_MAJOR".equals(idcard) || UTILITY_LINK_NAV_IDCARD.equals(idcard) || FOOTER_INFO_NAV_IDCARD.equals(idcard) || FOOTER_LINK_NAV_IDCARD.equals(idcard)) { return false; } return navigationType.LIST.getCode().toString().equals(navMap.get("wntcode").toString()); } private NavigationMiniView buildNavigationMiniView(GeneralDBAccess gdba, Map navMap, int size) throws Exception { normalizeNavigationMap(gdba, navMap); PageBean pb = new PageBean(); pb.setRowCountPerPage(size); List> articleList = WebsiteTemplateArticleUtil.getArticleList(gdba, Long.valueOf(navMap.get("navigationid").toString()), pb); return new NavigationMiniView(navMap, articleList); } private void normalizeNavigationMap(GeneralDBAccess gdba, Map navMap) throws Exception { if (navMap == null) { return; } if (navMap.get("href") == null) { navMap.put("href", WebsiteTemplateNavUtil.getNavigationHref(gdba, navMap)); } if (navMap.get("target") == null && navMap.get("wnjtcode") != null) { navMap.put("target", WebsiteTemplateNavUtil.jumpTypeCodeToTargetValue(navMap.get("wnjtcode").toString())); } } private List> groupSections(List sectionList, int groupSize) { List> rows = new ArrayList>(); if (sectionList == null || sectionList.size() == 0) { return rows; } for (int index = 0; index < sectionList.size(); index += groupSize) { List row = new ArrayList(); for (int offset = 0; offset < groupSize && index + offset < sectionList.size(); offset++) { row.add(sectionList.get(index + offset)); } rows.add(row); } return rows; } private void setArticleExtras(GeneralDBAccess gdba, Website website) throws Exception { Article article = WebsiteArticleManager.getArticleByRequest(gdba, website, getRequest()); if (article != null) { setRequestAttribute("articleThumbnail", article.getThumbnail()); setRequestAttribute("articleReadCount", article.getReadCount()); } } private void setCatalogAttributes(GeneralDBAccess gdba, Website website, WebsiteNavigation nav) throws Exception { WebsiteCommonService.selectHeadNavigation(gdba, website, getRequest()); setRequestAttribute("website", website); setRequestAttribute("navigation", nav); List crumbs = WebsiteNavManager.getNavParentList(gdba, website, nav.getNavigationid()); setRequestAttribute("crumbsNavList", crumbs); setRequestAttribute("crumbsNavListSize", crumbs == null ? 0 : crumbs.size() - 1); setRequestAttribute("childrenNavList", WebsiteNavManager.getChildrenNavigation(gdba, website, nav.getNavigationid())); setRequestAttribute("navMiniViewList", WebsiteNavManager.getChildrenNavigationMiniView(gdba, website, nav.getNavigationid())); if (nav.getParentNid() != null) { WebsiteNavigation parNav = WebsiteNavManager.getNavigationByID(gdba, website, nav.getParentNid()); setRequestAttribute("parNavigation", parNav); setRequestAttribute("brotherNavList", WebsiteNavManager.getChildrenNavigation(gdba, website, nav.getParentNid())); } } }