SearchAction.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 net.mingsoft.basic.action.BaseAction;
  36. import net.mingsoft.basic.biz.ICategoryBiz;
  37. import net.mingsoft.basic.biz.IColumnBiz;
  38. import net.mingsoft.basic.biz.IModelBiz;
  39. import net.mingsoft.basic.entity.BaseEntity;
  40. import net.mingsoft.basic.entity.ColumnEntity;
  41. import net.mingsoft.cms.biz.IArticleBiz;
  42. import net.mingsoft.cms.util.CmsParserUtil;
  43. import net.mingsoft.mdiy.biz.IContentModelBiz;
  44. import net.mingsoft.mdiy.biz.IContentModelFieldBiz;
  45. import net.mingsoft.mdiy.biz.ISearchBiz;
  46. import net.mingsoft.mdiy.entity.ContentModelEntity;
  47. import net.mingsoft.mdiy.entity.ContentModelFieldEntity;
  48. import net.mingsoft.mdiy.entity.SearchEntity;
  49. import cn.hutool.core.map.MapUtil;
  50. import cn.hutool.core.text.StrSpliter;
  51. import cn.hutool.core.util.ObjectUtil;
  52. import cn.hutool.core.util.PageUtil;
  53. import freemarker.core.ParseException;
  54. import freemarker.template.MalformedTemplateNameException;
  55. import freemarker.template.TemplateNotFoundException;
  56. import net.mingsoft.base.constant.Const;
  57. import net.mingsoft.basic.util.BasicUtil;
  58. import net.mingsoft.basic.util.StringUtil;
  59. import net.mingsoft.mdiy.util.ParserUtil;
  60. /**
  61. * 根据搜索结果动态解析模版内容
  62. * @author 铭飞开源团队-Administrator
  63. * @date 2018年12月18日
  64. */
  65. @Controller(value = "webSearchAction")
  66. @RequestMapping("/cms")
  67. public class SearchAction extends BaseAction {
  68. /**
  69. * 搜索标签;
  70. */
  71. public static final String SEARCH = "search";
  72. /**
  73. * 搜索的标题;
  74. */
  75. public static final String BASIC_TITLE = "basic_title";
  76. /**
  77. * 注入文章业务层
  78. */
  79. @Autowired
  80. private IArticleBiz articleBiz;
  81. /**
  82. * 注入搜索业务层
  83. */
  84. @Autowired
  85. private ISearchBiz searchBiz;
  86. /**
  87. * 注入栏目业务层
  88. */
  89. @Autowired
  90. private IColumnBiz columnBiz;
  91. /**
  92. * 内容模型业务层
  93. */
  94. @Autowired
  95. private IContentModelBiz contentModelBiz;
  96. /**
  97. * 内容字段业务层
  98. */
  99. @Autowired
  100. private IContentModelFieldBiz fieldBiz;
  101. /**
  102. * 注入分类业务层
  103. */
  104. @Autowired
  105. private ICategoryBiz categoryBiz;
  106. /**
  107. * 模块管理biz
  108. */
  109. @Autowired
  110. private IModelBiz modelBiz;
  111. /**
  112. * 实现前端页面的文章搜索
  113. *
  114. * @param request
  115. * @param searchId
  116. * 搜索id
  117. * @param response
  118. */
  119. @RequestMapping(value = "/{searchId}/search")
  120. @ResponseBody
  121. public void search(HttpServletRequest request, @PathVariable int searchId, HttpServletResponse response) {
  122. SearchEntity _search = new SearchEntity();
  123. _search.setAppId(BasicUtil.getAppId());
  124. _search.setSearchId(searchId);
  125. // 获取对应搜索模型
  126. SearchEntity search = (SearchEntity) searchBiz.getEntity(_search);
  127. //判断当前搜索是否有模板文件
  128. if (ObjectUtil.isNull(search)) {
  129. this.outJson(response, false);
  130. }
  131. Map<String, Object> map = BasicUtil.assemblyRequestMap();
  132. // 读取请求字段
  133. Map<String, String[]> field = request.getParameterMap();
  134. Map<String, String> basicField = getMapByProperties(net.mingsoft.mdiy.constant.Const.BASIC_FIELD);
  135. // 文章字段集合
  136. Map<String, Object> articleFieldName = new HashMap<String, Object>();
  137. // 自定义字段集合
  138. Map<String, String> diyFieldName = new HashMap<String, String>();
  139. ColumnEntity column = null; // 当前栏目
  140. ContentModelEntity contentModel = null; // 栏目对应模型
  141. List<ContentModelFieldEntity> fieldList = new ArrayList<ContentModelFieldEntity>(); // 栏目对应字段
  142. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>(); // 栏目对应字段的值
  143. int typeId = BasicUtil.getInt("categoryId",0);
  144. //记录自定义模型字段名
  145. List filedStr = new ArrayList<>();
  146. //根据栏目确定模版
  147. if(typeId>0){
  148. column = (ColumnEntity) columnBiz.getEntity(Integer.parseInt(typeId+""));
  149. // 获取表单类型的id
  150. if (column != null) {
  151. contentModel = (ContentModelEntity) contentModelBiz.getEntity(column.getColumnContentModelId());
  152. if (contentModel != null) {
  153. fieldList = fieldBiz.queryListByCmid(contentModel.getCmId());
  154. for (ContentModelFieldEntity cmField : fieldList) {
  155. filedStr.add(cmField.getFieldFieldName());
  156. }
  157. map.put(ParserUtil.TABLE_NAME, contentModel.getCmTableName());
  158. }
  159. }
  160. map.put(ParserUtil.COLUMN, column);
  161. //设置栏目编号
  162. map.put(ParserUtil.TYPE_ID, typeId);
  163. }
  164. // 遍历取字段集合
  165. if (field != null) {
  166. for (Entry<String, String[]> entry : field.entrySet()) {
  167. if (entry != null) {
  168. String value = entry.getValue()[0]; // 处理由get方法请求中文乱码问题
  169. if (ObjectUtil.isNull(value)) {
  170. continue;
  171. }
  172. if (request.getMethod().equals(RequestMethod.GET)) { // 如果是get方法需要将请求地址参数转吗
  173. try {
  174. value = new String(value.getBytes("ISO-8859-1"),Const.UTF8);
  175. } catch (UnsupportedEncodingException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. // 若为文章字段,则保存至文章字段集合;否则保存至自定义字段集合
  180. if (ObjectUtil.isNotNull(basicField.get(entry.getKey())) && ObjectUtil.isNotNull(value)) {
  181. articleFieldName.put(entry.getKey(), value);
  182. } else {
  183. if (!StringUtil.isBlank(value)) {
  184. diyFieldName.put(entry.getKey(), value);
  185. //判断请求中的是否是自定义模型中的字段
  186. if(filedStr.contains(entry.getKey())){
  187. //设置自定义模型字段和值
  188. DiyModelMap diyMap = new DiyModelMap();
  189. diyMap.setKey(entry.getKey());
  190. diyMap.setValue(value);
  191. fieldValueList.add(diyMap);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. //添加自定义模型的字段和值
  199. if(fieldValueList.size()>0){
  200. map.put("diyModel", fieldValueList);
  201. }
  202. //组织where查询条件
  203. Map whereMap = this.searchMap(articleFieldName, diyFieldName, fieldList);
  204. // 获取符合条件的文章总数
  205. int count = articleBiz.getSearchCount(contentModel, whereMap, BasicUtil.getAppId(), null);
  206. int size = BasicUtil.getInt(ParserUtil.SIZE,10);
  207. int total = PageUtil.totalPage(count, size);
  208. //获取总数
  209. map.put(ParserUtil.TOTAL, total);
  210. //设置页面显示数量
  211. map.put(ParserUtil.RCOUNT, size);
  212. map.put(ParserUtil.SIZE, size);
  213. //设置列表当前页
  214. map.put(ParserUtil.PAGE_NO, BasicUtil.getInt(ParserUtil.PAGE_NO,1));
  215. int pageNo = (int) map.get(ParserUtil.PAGE_NO);
  216. int next ,pre;
  217. if(StringUtil.isBlank(pageNo) || pageNo==1){
  218. //如果总页数等于1,下一页就是第一页,不等于就有第二页
  219. next = 1==total ? total : 2;
  220. pre = 1;
  221. }else{
  222. next = pageNo==total ? total : pageNo +1;
  223. pre = pageNo-1==0 ? 1 : pageNo-1;
  224. }
  225. String str = ParserUtil.PAGE_NO+",";
  226. //设置分页的统一链接
  227. String url = BasicUtil.getUrl() + request.getServletPath() +"?" + BasicUtil.assemblyRequestUrlParams(str.split(","));
  228. String pageNoStr = "&"+ParserUtil.PAGE_NO+"=";
  229. //下一页
  230. String nextUrl = url + pageNoStr+next;
  231. //首页
  232. String indexUrl = url + pageNoStr + 1;
  233. //尾页
  234. String lastUrl = url + pageNoStr + total;
  235. //上一页
  236. String preUrl = url + pageNoStr + pre;
  237. map.put(ParserUtil.INDEX_URL, indexUrl);
  238. map.put(ParserUtil.NEXT_URL, nextUrl);
  239. map.put(ParserUtil.PRE_URL, preUrl);
  240. map.put(ParserUtil.LAST_URL, lastUrl);
  241. map.put(ParserUtil.URL, BasicUtil.getUrl());
  242. Map<Object, Object> searchMap = new HashMap<>();
  243. searchMap.put(BASIC_TITLE, BasicUtil.getString(BASIC_TITLE));
  244. searchMap.put(ParserUtil.PAGE_NO, BasicUtil.getInt(ParserUtil.PAGE_NO,1));
  245. map.put(SEARCH, searchMap);
  246. //动态解析
  247. map.put(ParserUtil.IS_DO,false);
  248. //设置动态请求的模块路径
  249. map.put(ParserUtil.MODEL_NAME, "mcms");
  250. //解析后的内容
  251. String content = "";
  252. try {
  253. //根据模板路径,参数生成
  254. content = CmsParserUtil.generate(search.getSearchTemplets(),map, isMobileDevice(request));
  255. } catch (TemplateNotFoundException e) {
  256. e.printStackTrace();
  257. } catch (MalformedTemplateNameException e) {
  258. e.printStackTrace();
  259. } catch (ParseException e) {
  260. e.printStackTrace();
  261. } catch (IOException e) {
  262. e.printStackTrace();
  263. }
  264. this.outString(response, content);
  265. }
  266. /**
  267. * 动态组织查询where条件 获取查询条件的Map key:字段名 value:List 字段的各种判断值 list[0]:是否为自定义字段
  268. * list[1]:是否为整形 list[2]:是否是等值查询 list[3]:字段的值
  269. *
  270. * @param articleField
  271. * 文章字段
  272. * @param diyFieldName
  273. * 动态字段
  274. * @param fields
  275. * 模型对应的字段类型
  276. * @return
  277. */
  278. @SuppressWarnings({ "rawtypes", "unchecked" })
  279. private Map<String, List> searchMap(Map<String, Object> articleField, Map<String, String> diyFieldName,
  280. List fields) {
  281. Map<String, List> map = new HashMap<String, List>();
  282. // 遍历文章中的字段
  283. for (Iterator iter = articleField.keySet().iterator(); iter.hasNext();) {
  284. String key = iter.next().toString();
  285. String fieldValue = articleField.get(key).toString();
  286. List list = new ArrayList();
  287. List listValue = new ArrayList();
  288. // 是否为自定义字段
  289. list.add(false);
  290. // 是否是数字类型,true:不是
  291. list.add(true);
  292. // 是否是模糊查询3
  293. list.add(true);
  294. // 字段值
  295. listValue.add(articleField.get(key));
  296. list.add(listValue);
  297. map.put(key, list);
  298. }
  299. // 遍历字段自定义字段
  300. for (Iterator iter = diyFieldName.keySet().iterator(); iter.hasNext();) {
  301. String key = iter.next().toString();
  302. String fieldValue = diyFieldName.get(key);
  303. // 获取字段实体
  304. ContentModelFieldEntity field = this.get(key, fields);
  305. if (field != null) {
  306. List list = new ArrayList();
  307. // 是否为自定义字段0
  308. list.add(0, true);
  309. List listValue = new ArrayList();
  310. // 字段的值
  311. if (field.getFieldType() == IContentModelFieldBiz.INT || field.getFieldType() == IContentModelFieldBiz.FLOAT) {
  312. // 判断是否为区间查询
  313. if (diyFieldName.get(key).toString().indexOf("-") > 0) {
  314. String[] values = fieldValue.toString().split("-");
  315. // 是否是数字类型,false:是
  316. list.add(false);
  317. // 是否是区间比较 false:是
  318. list.add(false);
  319. // 字段值1
  320. listValue.add(values[0]);
  321. listValue.add(values[1]);
  322. } else {
  323. // 是否是数字类型,false:是2
  324. list.add(false);
  325. // 是否是区间比较 true:不是3
  326. list.add(true);
  327. // 字段值 1
  328. listValue.add(fieldValue);
  329. }
  330. } else {
  331. // 是否是数字类型,true:不是2
  332. list.add(true);
  333. list.add(false);
  334. // 字段值 1
  335. listValue.add(fieldValue);
  336. }
  337. list.add(listValue);
  338. map.put(key, list);
  339. }
  340. }
  341. return map;
  342. }
  343. /**
  344. * 根据字段名称获取字段类型
  345. *
  346. * @param columnName
  347. * 字段名称
  348. * @return 字段实体
  349. */
  350. private ContentModelFieldEntity get(String columnName, List<ContentModelFieldEntity> fields) {
  351. if (fields == null) {
  352. return null;
  353. }
  354. for (ContentModelFieldEntity field : fields) {
  355. if (field.getFieldFieldName().equals(columnName)) {
  356. return field;
  357. }
  358. }
  359. return null;
  360. }
  361. /**
  362. * 存储自定义模型字段和接口参数
  363. * @author 铭飞开源团队
  364. * @date 2019年3月5日
  365. */
  366. public class DiyModelMap {
  367. String key;
  368. Object value;
  369. public String getKey() {
  370. return key;
  371. }
  372. public void setKey(String key) {
  373. this.key = key;
  374. }
  375. public Object getValue() {
  376. return value;
  377. }
  378. public void setValue(Object value) {
  379. this.value = value;
  380. }
  381. }
  382. }