WebsiteService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package space.apps.moudules.website.services;
  2. import java.io.IOException;
  3. import java.net.URLDecoder;
  4. import java.sql.SQLException;
  5. import java.sql.Timestamp;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.apache.logging.log4j.LogManager;
  9. import org.apache.logging.log4j.Logger;
  10. import apps.pojo.Website;
  11. import apps.pojo.WebsiteNavigation;
  12. import apps.pojo.WebsiteVisitLog;
  13. import space.core.apps.common.BaseTransaction;
  14. import space.core.apps.listener.ModuleInitListener;
  15. import space.core.apps.util.CloneUtil;
  16. import space.core.common.utils.DateTimeUtil;
  17. import space.core.common.utils.PojoUtil;
  18. import space.core.common.utils.StringUtil;
  19. import space.core.db.jdbc.GeneralDBAccess;
  20. import space.core.db.jdbc.SqlCommandImpl;
  21. /**
  22. * 网站服务类
  23. *
  24. * @author SPACE
  25. * @create by SPACE on 2020年1月19日
  26. */
  27. public class WebsiteService extends BaseTransaction {
  28. private static final long serialVersionUID = 1L;
  29. private static final String INITIAL_TITLE_SUFFIX = "【初始】";
  30. private static final String PORTAL_INITIAL_ARTICLE_SOURCE = "非繁模板测试数据";
  31. private static final String SCREEN_INITIAL_ARTICLE_SOURCE = "全屏风模板示例数据";
  32. private static final String STUDY_INITIAL_ARTICLE_SOURCE = "招生咨询示例数据";
  33. private static final String COLLEGE_BLUE_INITIAL_ARTICLE_SOURCE = "非繁二级蓝色系模板示例数据";
  34. private static final String TOP_LINK_INITIAL_ARTICLE_SOURCE = "全屏风顶部自定义链接";
  35. static Logger log = LogManager.getLogger(WebsiteService.class);
  36. public String add() throws Exception {
  37. Website website = (Website) PojoUtil.formatRequest(getRequest(), Website.class);
  38. GeneralDBAccess gdba = getGdbaInstance();
  39. website.setCatalogUri("/");
  40. gdba.insertByBean(website);
  41. initWtNavigationList(gdba, website.getWtid(), website.getWebsiteid(), getUnit().getUnitid(),
  42. getAppUser().getUserId());
  43. gdba.commit();
  44. new ModuleInitListener().initWebsiteAll();
  45. setResponsePackage(getResponseSuccessMsgPackage());
  46. return DEFAULT_FORWARD;
  47. }
  48. public String change() throws Exception {
  49. Website website = (Website) PojoUtil.formatRequest(getRequest(), Website.class);
  50. GeneralDBAccess gdba = getGdbaInstance();
  51. gdba.updateById(website, getRequest());
  52. gdba.commit();
  53. new ModuleInitListener().initWebsiteAll();
  54. setResponsePackage(getResponseSuccessMsgPackage());
  55. return DEFAULT_FORWARD;
  56. }
  57. private void initWtNavigationList(GeneralDBAccess gdba, Long wtId, Long websiteId, Long unitId, Long appUserId)
  58. throws Exception {
  59. String templateConfig = getWebsiteTemplateConfig(gdba, wtId);
  60. boolean ffkjPortalTemplate = isFfkjPortalTemplate(templateConfig);
  61. String sql = " Select * from website_navigation where wnccode = 10010 and wtid = :wtId ";
  62. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  63. sqlCmd.setString("wtId", wtId);
  64. List<WebsiteNavigation> navigationList = gdba.selectAsList(sqlCmd, WebsiteNavigation.class);
  65. List<WebsiteNavigation> cloneNavigationList = (List<WebsiteNavigation>) CloneUtil.cloneList(navigationList);
  66. Timestamp nowTimestamp = DateTimeUtil.getNowTimestamp();
  67. // 复制所有栏目
  68. for (WebsiteNavigation wn : navigationList) {
  69. wn.setWnccode(10000L);
  70. wn.setWtid(null);
  71. wn.setWebsiteid(websiteId);
  72. wn.setUnitid(unitId);
  73. wn.setCreateTime(nowTimestamp);
  74. wn.setCreatePid(appUserId);
  75. wn.setNavigationid(null);
  76. if (ffkjPortalTemplate) {
  77. wn.setName(appendInitialSuffix(wn.getName()));
  78. wn.setTitle(appendInitialSuffix(wn.getTitle()));
  79. }
  80. gdba.insertByBean(wn);
  81. }
  82. // 增加栏目关联关系
  83. for (WebsiteNavigation wn : navigationList) {
  84. if (StringUtil.isNull(wn.getParentNid())) {
  85. continue;
  86. }
  87. wn.setParentNid(getParentNidBySerialNumber(navigationList,
  88. getNavigationSerialNumber(cloneNavigationList, wn.getParentNid())));
  89. gdba.updateById(wn);
  90. }
  91. if (ffkjPortalTemplate) {
  92. initFfkjPortalInitialArticleList(gdba, wtId, websiteId, unitId, appUserId);
  93. }
  94. }
  95. private String getWebsiteTemplateConfig(GeneralDBAccess gdba, Long wtId) throws Exception {
  96. if (StringUtil.isNull(wtId)) {
  97. return null;
  98. }
  99. String sql = " select config from website_template where wtid = :wtId ";
  100. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  101. sqlCmd.setString("wtId", wtId);
  102. Map<String, Object> templateMap = gdba.selectAsMap(sqlCmd);
  103. if (templateMap == null || templateMap.get("config") == null) {
  104. return null;
  105. }
  106. return templateMap.get("config").toString();
  107. }
  108. private boolean isFfkjPortalTemplate(String templateConfig) {
  109. return "ffkjschool01".equals(templateConfig) || "ffkjcollege1".equals(templateConfig)
  110. || "ffkjscreen01".equals(templateConfig) || "ffkjscreen02".equals(templateConfig)
  111. || "ffkjcollegeblue1".equals(templateConfig) || "ffkjcollegered1".equals(templateConfig);
  112. }
  113. private String appendInitialSuffix(String value) {
  114. if (StringUtil.isNull(value) || value.endsWith(INITIAL_TITLE_SUFFIX)) {
  115. return value;
  116. }
  117. int maxBaseLength = 252;
  118. if (value.length() > maxBaseLength) {
  119. value = value.substring(0, maxBaseLength);
  120. }
  121. return value + INITIAL_TITLE_SUFFIX;
  122. }
  123. private void initFfkjPortalInitialArticleList(GeneralDBAccess gdba, Long wtId, Long websiteId, Long unitId,
  124. Long appUserId) throws Exception {
  125. Long referenceWebsiteId = getFfkjPortalReferenceWebsiteId(gdba, wtId, websiteId);
  126. if (StringUtil.isNull(referenceWebsiteId)) {
  127. log.warn("未找到非繁 portal 模板文章参考站点,跳过新站初始文章复制。websiteId=" + websiteId + ", wtId=" + wtId);
  128. return;
  129. }
  130. String sql = " insert into article ( "
  131. + " dcode, websiteid, unitid, ascode, navigationid, title, ajtcode, `desc`, source, thumbnail, "
  132. + " sticktop, sticktop_end_time, sticktop_schedulesid, `release`, release_time, release_task_schedulesid, "
  133. + " content, edit_content, url, read_count, create_time, last_change_time, create_pid, last_change_pid "
  134. + " ) "
  135. + " select 61, :websiteId1, :unitId, a.ascode, target_nav.navigationid, "
  136. + " 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), "
  137. + " a.ajtcode, replace(a.`desc`, reference_website.name, target_website.name), a.source, a.thumbnail, "
  138. + " a.sticktop, a.sticktop_end_time, a.sticktop_schedulesid, a.`release`, a.release_time, a.release_task_schedulesid, "
  139. + " a.content, a.edit_content, a.url, a.read_count, now(), now(), :appUserId1, :appUserId2 "
  140. + " from article a "
  141. + " join website reference_website on reference_website.websiteid = :referenceWebsiteId1 "
  142. + " join website target_website on target_website.websiteid = :websiteId2 "
  143. + " join website_navigation source_nav on source_nav.navigationid = a.navigationid "
  144. + " join website_navigation target_nav on target_nav.websiteid = :websiteId3 "
  145. + " and target_nav.wnccode = 10000 "
  146. + " and target_nav.idcard = source_nav.idcard "
  147. + " where a.websiteid = :referenceWebsiteId2 "
  148. + " and a.source in (:source1, :source2, :source3, :source4, :source5) "
  149. + " and not exists ( "
  150. + " select 1 from article target_article "
  151. + " where target_article.websiteid = :websiteId4 "
  152. + " and target_article.navigationid = target_nav.navigationid "
  153. + " 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) "
  154. + " ) "
  155. + " order by a.articleid ";
  156. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  157. sqlCmd.setString("websiteId1", websiteId).setString("websiteId2", websiteId).setString("websiteId3", websiteId)
  158. .setString("websiteId4", websiteId)
  159. .setString("unitId", unitId).setString("appUserId1", appUserId).setString("appUserId2", appUserId)
  160. .setString("referenceWebsiteId1", referenceWebsiteId).setString("referenceWebsiteId2", referenceWebsiteId)
  161. .setString("source1", PORTAL_INITIAL_ARTICLE_SOURCE)
  162. .setString("source2", SCREEN_INITIAL_ARTICLE_SOURCE)
  163. .setString("source3", STUDY_INITIAL_ARTICLE_SOURCE)
  164. .setString("source4", COLLEGE_BLUE_INITIAL_ARTICLE_SOURCE)
  165. .setString("source5", TOP_LINK_INITIAL_ARTICLE_SOURCE)
  166. .setString("topLinkSource1", TOP_LINK_INITIAL_ARTICLE_SOURCE)
  167. .setString("topLinkSource2", TOP_LINK_INITIAL_ARTICLE_SOURCE)
  168. .setString("initialSuffix", INITIAL_TITLE_SUFFIX).setString("initialSuffix2", INITIAL_TITLE_SUFFIX)
  169. .setString("initialTitlePattern", "%" + INITIAL_TITLE_SUFFIX)
  170. .setString("initialTitlePattern2", "%" + INITIAL_TITLE_SUFFIX);
  171. gdba.modify(sqlCmd);
  172. }
  173. private Long getFfkjPortalReferenceWebsiteId(GeneralDBAccess gdba, Long wtId, Long targetWebsiteId)
  174. throws Exception {
  175. String sql = " select w.websiteid "
  176. + " from website w "
  177. + " where w.dcode = 61 "
  178. + " and w.wtid = :wtId "
  179. + " and w.websiteid != :targetWebsiteId "
  180. + " and exists ( "
  181. + " select 1 from article a "
  182. + " where a.websiteid = w.websiteid "
  183. + " and a.source in (:source1, :source2, :source3, :source4, :source5) "
  184. + " ) "
  185. + " order by case when w.id_code in ('local_ffkj_school_site', 'local_ffkj_college_site') then 0 else 1 end, w.websiteid "
  186. + " limit 1 ";
  187. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  188. sqlCmd.setString("wtId", wtId).setString("targetWebsiteId", targetWebsiteId)
  189. .setString("source1", PORTAL_INITIAL_ARTICLE_SOURCE)
  190. .setString("source2", SCREEN_INITIAL_ARTICLE_SOURCE)
  191. .setString("source3", STUDY_INITIAL_ARTICLE_SOURCE)
  192. .setString("source4", COLLEGE_BLUE_INITIAL_ARTICLE_SOURCE)
  193. .setString("source5", TOP_LINK_INITIAL_ARTICLE_SOURCE);
  194. Map<String, Object> websiteMap = gdba.selectAsMap(sqlCmd);
  195. if (websiteMap == null || websiteMap.get("websiteid") == null) {
  196. return null;
  197. }
  198. return Long.valueOf(websiteMap.get("websiteid").toString());
  199. }
  200. private String getNavigationSerialNumber(List<WebsiteNavigation> navigationList, Long navigationId) {
  201. for (WebsiteNavigation websiteNavigation : navigationList) {
  202. if (websiteNavigation.getNavigationid().equals(navigationId)) {
  203. return websiteNavigation.getIdcard();
  204. }
  205. }
  206. return null;
  207. }
  208. private Long getParentNidBySerialNumber(List<WebsiteNavigation> navigationList, String serialNumber)
  209. throws Exception {
  210. for (WebsiteNavigation websiteNavigation : navigationList) {
  211. if (websiteNavigation.getIdcard().equals(serialNumber)) {
  212. return websiteNavigation.getNavigationid();
  213. }
  214. }
  215. return null;
  216. }
  217. /**
  218. * 获取有层级关系的网站列表
  219. *
  220. * @author SPACE
  221. * @throws Exception
  222. * @throws SQLException
  223. * @date 2020年1月19日
  224. */
  225. public void getWebsiteListApi() throws SQLException, Exception {
  226. Long unitId = getUnit().getUnitid();
  227. GeneralDBAccess gdba = getGdbaInstance();
  228. String sql = " select websiteid, name from website where unitid = :unitid and dcode = 61 and parent_wid is null ";
  229. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  230. sqlCmd.setString("unitid", unitId);
  231. List<Map<String, Object>> websiteList = gdba.selectAsMapList(sqlCmd);
  232. for (Map<String, Object> map : websiteList) {
  233. getWebsiteListApi(gdba, map, unitId, Long.valueOf(map.get("websiteid").toString()));
  234. }
  235. write(getResponseSuccessDataPackage(websiteList).toJson());
  236. }
  237. void getWebsiteListApi(GeneralDBAccess gdba, Map<String, Object> websiteMap, Long unitId, Long parWebsiteId)
  238. throws SQLException, Exception {
  239. String sql = " select websiteid, name from website where unitid = :unitid and dcode = 61 and parent_wid = :parWebsiteId ";
  240. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  241. sqlCmd.setString("unitid", unitId).setString("parWebsiteId", parWebsiteId);
  242. List<Map<String, Object>> websiteList = gdba.selectAsMapList(sqlCmd);
  243. if (websiteList.size() == 0) {
  244. return;
  245. }
  246. websiteMap.put("children", websiteList);
  247. for (Map<String, Object> map : websiteList) {
  248. getWebsiteListApi(gdba, map, unitId, Long.valueOf(map.get("websiteid").toString()));
  249. }
  250. }
  251. /**
  252. * 增加网站访问量
  253. *
  254. * @author SPACE
  255. * @throws Exception
  256. * @throws IOException
  257. * @log Sep 26, 2020 8:49:26 PM
  258. */
  259. public void addWebsiteVisit() throws Exception {
  260. try {
  261. String address = getParameter("address");
  262. if (StringUtil.notNull(address)) {
  263. address = URLDecoder.decode(address, "utf-8");
  264. } else {
  265. address = " Address is null ";
  266. }
  267. WebsiteVisitLog wvl = new WebsiteVisitLog();
  268. wvl.setWebsiteid(getWebsite().getWebsiteid());
  269. wvl.setWstcode(10000L);
  270. wvl.setIp(getRequest().getLocalAddr());
  271. wvl.setUrl(address);
  272. wvl.setMsg(getParameter("msg"));
  273. GeneralDBAccess gdba = getGdbaInstance();
  274. gdba.insertByBean(wvl);
  275. // 增加文章阅读量
  276. addArticleRead(gdba, getParameter("pathname"));
  277. gdba.commit();
  278. } catch (Exception e) {
  279. log.error("增加网站访问记录时失败,URL=" + getRequest().getRequestURL().toString(), e);
  280. }
  281. write("");
  282. }
  283. /**
  284. * 根据文章的URL增加一次文章阅读量
  285. *
  286. * @param gdba 数据库
  287. * @param uri URI
  288. * @throws Exception
  289. * @author SPACE
  290. * @log Sep 26, 2020 10:11:00 PM
  291. */
  292. private void addArticleRead(GeneralDBAccess gdba, String uri) throws Exception {
  293. if (StringUtil.isNull(uri) || "/".equals(uri)) {
  294. return;
  295. }
  296. String sql = " update article set read_count = (read_count + 1) where url = :url ";
  297. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  298. sqlCmd.setString("url", uri);
  299. int result = gdba.modify(sqlCmd);
  300. if (result == 1) {
  301. return;
  302. }
  303. // 处理单页的情况
  304. String sql2 = " update article set read_count = (read_count + 1) where articleid ="
  305. + " (Select articleid from (Select a.articleid from article a, website_navigation wn where a.navigationid = wn.navigationid "
  306. + "and wn.wntcode = 10060 and a.dcode = 61 and wn.url = :url order by a.create_time desc limit 0,1) as tempArticle )";
  307. SqlCommandImpl sqlCmd2 = new SqlCommandImpl(sql2);
  308. sqlCmd2.setString("url", uri);
  309. gdba.modify(sqlCmd2);
  310. }
  311. /**
  312. * 用于流程条件的接口
  313. *
  314. * @throws SQLException
  315. * @throws Exception
  316. *
  317. * @author SPACE
  318. * @log Create on 2021年1月23日 下午9:39:52
  319. */
  320. public void getAllWebsiteApi() throws SQLException, Exception {
  321. GeneralDBAccess gdba = getGdbaInstance();
  322. String sql = " select websiteid as 'id', name, logo as 'icon_path' from website where dcode = 61 order by websiteid asc ";
  323. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  324. List<Map<String, Object>> websiteList = gdba.selectAsMapList(sqlCmd);
  325. write(getResponseSuccessDataPackage(websiteList).toJson());
  326. }
  327. }