PageRenderTime 3349ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/penuel/mythopoet/controllers/SearchController.java

https://gitlab.com/tycoon/mythopoet
Java | 142 lines | 113 code | 18 blank | 11 comment | 15 complexity | 2fa887bb8d39a168cd6a76ffb3ab2a99 MD5 | raw file
  1. package com.penuel.mythopoet.controllers;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.penuel.mythopoet.model.Designer;
  4. import com.penuel.mythopoet.model.Item;
  5. import com.penuel.mythopoet.model.Tag;
  6. import com.penuel.mythopoet.modelView.ItemView;
  7. import com.penuel.mythopoet.modelView.Page;
  8. import com.penuel.mythopoet.service.DesignerService;
  9. import com.penuel.mythopoet.service.ItemBrandService;
  10. import com.penuel.mythopoet.service.ItemService;
  11. import com.penuel.mythopoet.service.TagService;
  12. import com.penuel.mythopoet.utils.ResponseUtil;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.ui.Model;
  19. import org.springframework.util.CollectionUtils;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestMethod;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.ResponseBody;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import javax.servlet.http.HttpServletRequest;
  27. /**
  28. * SearchController Created with mythopoet.
  29. * User: penuel (penuel.leo@gmail.com)
  30. * Date: 15/4/29 下午11:57
  31. * Desc:
  32. */
  33. @Controller @RequestMapping( "/search" )
  34. public class SearchController {
  35. @Autowired
  36. private ItemService itemService;
  37. @Autowired
  38. private DesignerService designerService;
  39. @Autowired
  40. private TagService tagService;
  41. @Autowired
  42. private ItemBrandService itemBrandService;
  43. private static final Logger LOGGER = LoggerFactory.getLogger(CartController.class);
  44. @RequestMapping( value = "", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  45. public String search(Model model, @RequestParam( value = "key", defaultValue = "" ) String key,
  46. @RequestParam( value = "pageNum", defaultValue = "1" ) int pageNum, @RequestParam( value = "pageSize", defaultValue = "20" ) int pageSize) {
  47. List<ItemView> itemViewList = new ArrayList<ItemView>();
  48. Page page = new Page(pageNum,pageSize);
  49. try {
  50. //List<Item> itemList = searchByKey(key,page);
  51. List<Item> itemList = itemService.searchByName(key, pageNum, pageSize);
  52. if ( !CollectionUtils.isEmpty(itemList) ){
  53. ItemView itemView;
  54. for ( Item item : itemList ){
  55. itemView = new ItemView();
  56. BeanUtils.copyProperties(item,itemView);
  57. itemBrandService.fillBrandInfo(itemView);
  58. itemViewList.add(itemView);
  59. }
  60. }
  61. } catch ( Exception e ) {
  62. LOGGER.error("SearchController.search Error:key=" + key + ",pageNum=" + pageNum + ",pageSize=" + pageSize, e);
  63. return ResponseUtil.result(1, "搜索失败", null);
  64. }
  65. return ResponseUtil.result(0, "OK", itemViewList);
  66. }
  67. @RequestMapping( value = "index" )
  68. public String index(Model model, HttpServletRequest request, @RequestParam( value = "key", defaultValue = "" ) String key) {
  69. request.setAttribute("key", key);
  70. return "search/list";
  71. }
  72. private List<Item> searchByKey(String key,Page page){
  73. /**煞笔产品 搜索非要把设计师的商品都搜出来,又不出分页规则,只能全部取出来,等数据量大了这是个大坑。*/
  74. List<Item> itemList = itemService.searchByName(key, 0, Integer.MAX_VALUE);
  75. if ( CollectionUtils.isEmpty(itemList) ) {
  76. itemList = new ArrayList<Item>();
  77. }
  78. List<Designer> designerList = designerService.searchByName(key);
  79. if ( !CollectionUtils.isEmpty(designerList) ) {
  80. /** 煞笔产品 搜索非要把设计师的商品都搜出来,又不出分页规则,只能全部取出来,再limit,也懒得做批量优化了。*/
  81. for ( Designer designer : designerList ) {
  82. List<Item> designerItemList = itemService.getAllByDesignerId(designer.getId());
  83. itemList.addAll(designerItemList);
  84. }
  85. }
  86. /**搜索tag*/
  87. List<Tag> tagList = tagService.searchByKey(key);
  88. if ( !CollectionUtils.isEmpty(tagList) ) {
  89. for ( Tag tag : tagList ) {
  90. List<Item> tagItemList = itemService.getAllByTagId(tag.getId());
  91. itemList.addAll(tagItemList);
  92. }
  93. }
  94. itemList = uniqItem(itemList);
  95. page.setTotalCount(itemList.size());
  96. /** 取消分页,全量加载,产品的要求,需要把搜索结果全部展现出来 */
  97. int offset = (page.getCurPageNum() - 1) * page.getPageSize();
  98. int maxCount = page.getCurPageNum() * page.getPageSize();
  99. LOGGER.info("SearchController.search Info:key=" + key + ",page=" + JSONObject.toJSONString(page) + ",but itemList.size=" + itemList.size());
  100. if ( itemList.size() > maxCount ) {
  101. itemList = itemList.subList(offset, maxCount);
  102. } else if ( itemList.size() <= offset ) {
  103. itemList = new ArrayList<Item>();
  104. } else {
  105. itemList = itemList.subList(offset, itemList.size());
  106. }
  107. return itemList;
  108. }
  109. private List<Item> uniqItem(List<Item> itemList) {
  110. if ( CollectionUtils.isEmpty(itemList) ) {
  111. return itemList;
  112. }
  113. List<Long> itemIdList = new ArrayList<Long>();
  114. for ( int i = itemList.size() - 1; i >= 0; i-- ) {
  115. Item item = itemList.get(i);
  116. if ( itemIdList.contains(item.getId()) ) {
  117. itemList.remove(i);
  118. } else {
  119. itemIdList.add(item.getId());
  120. }
  121. }
  122. return itemList;
  123. }
  124. }