SearchAction.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. The MIT License (MIT) * Copyright (c) 2016 铭飞科技(mingsoft.net)
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. * this software and associated documentation files (the "Software"), to deal in
  5. * the Software without restriction, including without limitation the rights to
  6. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. * the Software, and to permit persons to whom the Software is furnished to do so,
  8. * subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in all
  10. * copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  13. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  15. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. package net.mingsoft.cms.action.web;
  19. import java.io.IOException;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Controller;
  31. import org.springframework.web.bind.annotation.PathVariable;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.RequestMethod;
  34. import org.springframework.web.bind.annotation.ResponseBody;
  35. import cn.hutool.core.util.ObjectUtil;
  36. import cn.hutool.core.util.PageUtil;
  37. import freemarker.core.ParseException;
  38. import freemarker.template.MalformedTemplateNameException;
  39. import freemarker.template.TemplateNotFoundException;
  40. import net.mingsoft.base.constant.Const;
  41. import net.mingsoft.basic.action.BaseAction;
  42. import net.mingsoft.basic.biz.ICategoryBiz;
  43. import net.mingsoft.basic.biz.IColumnBiz;
  44. import net.mingsoft.basic.biz.IModelBiz;
  45. import net.mingsoft.basic.entity.ColumnEntity;
  46. import net.mingsoft.basic.util.BasicUtil;
  47. import net.mingsoft.basic.util.StringUtil;
  48. import net.mingsoft.cms.biz.IArticleBiz;
  49. import net.mingsoft.cms.util.CmsParserUtil;
  50. import net.mingsoft.mdiy.bean.PageBean;
  51. import net.mingsoft.mdiy.biz.IContentModelBiz;
  52. import net.mingsoft.mdiy.biz.IContentModelFieldBiz;
  53. import net.mingsoft.mdiy.biz.ISearchBiz;
  54. import net.mingsoft.mdiy.entity.ContentModelEntity;
  55. import net.mingsoft.mdiy.entity.ContentModelFieldEntity;
  56. import net.mingsoft.mdiy.entity.SearchEntity;
  57. import net.mingsoft.mdiy.parser.TagParser;
  58. import net.mingsoft.mdiy.util.ParserUtil;
  59. /**
  60. * 根据搜索结果动态解析模版内容
  61. * @author 铭飞开源团队-Administrator
  62. * @date 2018年12月18日
  63. */
  64. @Controller(value = "webSearchAction")
  65. @RequestMapping("/cms")
  66. public class SearchAction extends BaseAction {
  67. /**
  68. * 搜索标签;
  69. */
  70. public static final String SEARCH = "search";
  71. /**
  72. * 搜索的标题;
  73. */
  74. public static final String BASIC_TITLE = "basic_title";
  75. /**
  76. * 注入文章业务层
  77. */
  78. @Autowired
  79. private IArticleBiz articleBiz;
  80. /**
  81. * 注入搜索业务层
  82. */
  83. @Autowired
  84. private ISearchBiz searchBiz;
  85. /**
  86. * 注入栏目业务层
  87. */
  88. @Autowired
  89. private IColumnBiz columnBiz;
  90. /**
  91. * 内容模型业务层
  92. */
  93. @Autowired
  94. private IContentModelBiz contentModelBiz;
  95. /**
  96. * 内容字段业务层
  97. */
  98. @Autowired
  99. private IContentModelFieldBiz fieldBiz;
  100. /**
  101. * 注入分类业务层
  102. */
  103. @Autowired
  104. private ICategoryBiz categoryBiz;
  105. /**
  106. * 模块管理biz
  107. */
  108. @Autowired
  109. private IModelBiz modelBiz;
  110. /**
  111. * 实现前端页面的文章搜索
  112. *
  113. * @param request
  114. * @param searchId
  115. * 搜索id
  116. * @param response
  117. */
  118. @RequestMapping(value = "/{searchId}/search")
  119. @ResponseBody
  120. public void search(HttpServletRequest request, @PathVariable int searchId, HttpServletResponse response) {
  121. SearchEntity _search = new SearchEntity();
  122. _search.setAppId(BasicUtil.getAppId());
  123. _search.setSearchId(searchId);
  124. // 获取对应搜索模型
  125. SearchEntity search = (SearchEntity) searchBiz.getEntity(_search);
  126. //判断当前搜索是否有模板文件
  127. if (ObjectUtil.isNull(search)) {
  128. this.outJson(response, false);
  129. }
  130. Map<String, Object> map = new HashMap<>();
  131. // 读取请求字段
  132. Map<String, String[]> field = request.getParameterMap();
  133. Map<String, String> basicField = getMapByProperties(net.mingsoft.mdiy.constant.Const.BASIC_FIELD);
  134. // 文章字段集合
  135. Map<String, Object> articleFieldName = new HashMap<String, Object>();
  136. // 自定义字段集合
  137. Map<String, String> diyFieldName = new HashMap<String, String>();
  138. ColumnEntity column = null; // 当前栏目
  139. ContentModelEntity contentModel = null; // 栏目对应模型
  140. List<ContentModelFieldEntity> fieldList = new ArrayList<ContentModelFieldEntity>(); // 栏目对应字段
  141. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>(); // 栏目对应字段的值
  142. int typeId = 0;
  143. String categoryIds = BasicUtil.getString("categoryId");
  144. //当传递了栏目编号,但不是栏目集合
  145. if(!StringUtil.isBlank(categoryIds) && !categoryIds.contains(",")){
  146. typeId = Integer.parseInt(categoryIds);
  147. }
  148. //记录自定义模型字段名
  149. List filedStr = new ArrayList<>();
  150. //根据栏目确定自定义模型
  151. if(typeId>0){
  152. column = (ColumnEntity) columnBiz.getEntity(Integer.parseInt(typeId+""));
  153. // 获取表单类型的id
  154. if (column != null) {
  155. contentModel = (ContentModelEntity) contentModelBiz.getEntity(column.getColumnContentModelId());
  156. if (contentModel != null) {
  157. fieldList = fieldBiz.queryListByCmid(contentModel.getCmId());
  158. for (ContentModelFieldEntity cmField : fieldList) {
  159. filedStr.add(cmField.getFieldFieldName());
  160. }
  161. map.put(ParserUtil.TABLE_NAME, contentModel.getCmTableName());
  162. }
  163. }
  164. map.put(ParserUtil.COLUMN, column);
  165. //设置栏目编号
  166. // map.put(ParserUtil.TYPE_ID, typeId);
  167. }
  168. // 遍历取字段集合
  169. if (field != null) {
  170. for (Entry<String, String[]> entry : field.entrySet()) {
  171. if (entry != null) {
  172. String value = entry.getValue()[0]; // 处理由get方法请求中文乱码问题
  173. if (ObjectUtil.isNull(value)) {
  174. continue;
  175. }
  176. if (request.getMethod().equals(RequestMethod.GET)) { // 如果是get方法需要将请求地址参数转吗
  177. try {
  178. value = new String(value.getBytes("ISO-8859-1"),Const.UTF8);
  179. } catch (UnsupportedEncodingException e) {
  180. e.printStackTrace();
  181. }
  182. }
  183. // 若为文章字段,则保存至文章字段集合;否则保存至自定义字段集合
  184. if (ObjectUtil.isNotNull(basicField.get(entry.getKey())) && ObjectUtil.isNotNull(value)) {
  185. articleFieldName.put(entry.getKey(), value);
  186. } else {
  187. if (!StringUtil.isBlank(value)) {
  188. diyFieldName.put(entry.getKey(), value);
  189. //判断请求中的是否是自定义模型中的字段
  190. if(filedStr.contains(entry.getKey())){
  191. //设置自定义模型字段和值
  192. DiyModelMap diyMap = new DiyModelMap();
  193. diyMap.setKey(entry.getKey());
  194. diyMap.setValue(value);
  195. fieldValueList.add(diyMap);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. //添加自定义模型的字段和值
  203. if(fieldValueList.size()>0){
  204. map.put("diyModel", fieldValueList);
  205. }
  206. //组织where查询条件
  207. Map whereMap = this.searchMap(articleFieldName, diyFieldName, fieldList);
  208. // 获取符合条件的文章总数
  209. int count = articleBiz.getSearchCount(contentModel, whereMap, BasicUtil.getAppId(), categoryIds);
  210. //设置分页类
  211. PageBean page = new PageBean();
  212. //读取模板的分页数量
  213. int size = BasicUtil.getInt(ParserUtil.SIZE,10);
  214. try {
  215. size = TagParser.getPageSize(ParserUtil.read(search.getSearchTemplets(),false ));
  216. } catch (TemplateNotFoundException e1) {
  217. e1.printStackTrace();
  218. } catch (MalformedTemplateNameException e1) {
  219. e1.printStackTrace();
  220. } catch (ParseException e1) {
  221. e1.printStackTrace();
  222. } catch (IOException e1) {
  223. e1.printStackTrace();
  224. }
  225. int total = PageUtil.totalPage(count, size);
  226. int pageNo = BasicUtil.getInt(ParserUtil.PAGE_NO, 1);
  227. if(pageNo >= total && total!=0) {
  228. pageNo = total;
  229. }
  230. //获取总数
  231. page.setTotal(total);
  232. //设置页面显示数量
  233. page.setSize(size);
  234. //设置列表当前页
  235. page.setPageNo(pageNo);
  236. String str = ParserUtil.PAGE_NO+","+ParserUtil.SIZE;
  237. //设置分页的统一链接
  238. String url = BasicUtil.getUrl()+request.getServletPath() +"?" + BasicUtil.assemblyRequestUrlParams(str.split(","));
  239. String pageNoStr = "&"+ParserUtil.SIZE+"="+size+"&"+ParserUtil.PAGE_NO+"=";
  240. //下一页
  241. String nextUrl = url + pageNoStr+((pageNo+1 > total)?total:pageNo+1);
  242. //首页
  243. String indexUrl = url + pageNoStr + 1;
  244. //尾页
  245. String lastUrl = url + pageNoStr + total;
  246. //上一页 当前页为1时,上一页就是1
  247. String preUrl = url + pageNoStr + ((pageNo==1) ? 1:pageNo-1);
  248. page.setIndexUrl(indexUrl);
  249. page.setNextUrl(nextUrl);
  250. page.setPreUrl(preUrl);
  251. page.setLastUrl(lastUrl);
  252. map.put(ParserUtil.URL, BasicUtil.getUrl());
  253. Map<String, Object> searchMap = BasicUtil.assemblyRequestMap();
  254. searchMap.put(ParserUtil.PAGE_NO, pageNo);
  255. map.put(SEARCH, searchMap);
  256. map.put(ParserUtil.PAGE, page);
  257. //动态解析
  258. map.put(ParserUtil.IS_DO,false);
  259. //设置动态请求的模块路径
  260. map.put(ParserUtil.MODEL_NAME, "mcms");
  261. //解析后的内容
  262. String content = "";
  263. try {
  264. //根据模板路径,参数生成
  265. content = CmsParserUtil.generate(search.getSearchTemplets(),map, isMobileDevice(request));
  266. } catch (TemplateNotFoundException e) {
  267. e.printStackTrace();
  268. } catch (MalformedTemplateNameException e) {
  269. e.printStackTrace();
  270. } catch (ParseException e) {
  271. e.printStackTrace();
  272. } catch (IOException e) {
  273. e.printStackTrace();
  274. }
  275. this.outString(response, content);
  276. }
  277. /**
  278. * 动态组织查询where条件 获取查询条件的Map key:字段名 value:List 字段的各种判断值 list[0]:是否为自定义字段
  279. * list[1]:是否为整形 list[2]:是否是等值查询 list[3]:字段的值
  280. *
  281. * @param articleField
  282. * 文章字段
  283. * @param diyFieldName
  284. * 动态字段
  285. * @param fields
  286. * 模型对应的字段类型
  287. * @return
  288. */
  289. @SuppressWarnings({ "rawtypes", "unchecked" })
  290. private Map<String, List> searchMap(Map<String, Object> articleField, Map<String, String> diyFieldName,
  291. List fields) {
  292. Map<String, List> map = new HashMap<String, List>();
  293. // 遍历文章中的字段
  294. for (Iterator iter = articleField.keySet().iterator(); iter.hasNext();) {
  295. String key = iter.next().toString();
  296. String fieldValue = articleField.get(key).toString();
  297. List list = new ArrayList();
  298. List listValue = new ArrayList();
  299. // 是否为自定义字段
  300. list.add(false);
  301. // 是否是数字类型,true:不是
  302. list.add(true);
  303. // 是否是模糊查询3
  304. list.add(true);
  305. // 字段值
  306. listValue.add(articleField.get(key));
  307. list.add(listValue);
  308. map.put(key, list);
  309. }
  310. // 遍历字段自定义字段
  311. for (Iterator iter = diyFieldName.keySet().iterator(); iter.hasNext();) {
  312. String key = iter.next().toString();
  313. String fieldValue = diyFieldName.get(key);
  314. // 获取字段实体
  315. ContentModelFieldEntity field = this.get(key, fields);
  316. if (field != null) {
  317. List list = new ArrayList();
  318. // 是否为自定义字段0
  319. list.add(0, true);
  320. List listValue = new ArrayList();
  321. // 字段的值
  322. if (field.getFieldType() == IContentModelFieldBiz.INT || field.getFieldType() == IContentModelFieldBiz.FLOAT) {
  323. // 判断是否为区间查询
  324. if (diyFieldName.get(key).toString().indexOf("-") > 0) {
  325. String[] values = fieldValue.toString().split("-");
  326. // 是否是数字类型,false:是
  327. list.add(false);
  328. // 是否是区间比较 false:是
  329. list.add(false);
  330. // 字段值1
  331. listValue.add(values[0]);
  332. listValue.add(values[1]);
  333. } else {
  334. // 是否是数字类型,false:是2
  335. list.add(false);
  336. // 是否是区间比较 true:不是3
  337. list.add(true);
  338. // 字段值 1
  339. listValue.add(fieldValue);
  340. }
  341. } else {
  342. // 是否是数字类型,true:不是2
  343. list.add(true);
  344. list.add(false);
  345. // 字段值 1
  346. listValue.add(fieldValue);
  347. }
  348. list.add(listValue);
  349. map.put(key, list);
  350. }
  351. }
  352. return map;
  353. }
  354. /**
  355. * 根据字段名称获取字段类型
  356. *
  357. * @param columnName
  358. * 字段名称
  359. * @return 字段实体
  360. */
  361. private ContentModelFieldEntity get(String columnName, List<ContentModelFieldEntity> fields) {
  362. if (fields == null) {
  363. return null;
  364. }
  365. for (ContentModelFieldEntity field : fields) {
  366. if (field.getFieldFieldName().equals(columnName)) {
  367. return field;
  368. }
  369. }
  370. return null;
  371. }
  372. /**
  373. * 存储自定义模型字段和接口参数
  374. * @author 铭飞开源团队
  375. * @date 2019年3月5日
  376. */
  377. public class DiyModelMap {
  378. String key;
  379. Object value;
  380. public String getKey() {
  381. return key;
  382. }
  383. public void setKey(String key) {
  384. this.key = key;
  385. }
  386. public Object getValue() {
  387. return value;
  388. }
  389. public void setValue(Object value) {
  390. this.value = value;
  391. }
  392. }
  393. }