-
Notifications
You must be signed in to change notification settings - Fork 97
/
GoodsServiceImpl.java
248 lines (215 loc) · 8.55 KB
/
GoodsServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cn.tycoding.service.impl;
import cn.tycoding.entity.Goods;
import cn.tycoding.entity.PageBean;
import cn.tycoding.mapper.GoodsMapper;
import cn.tycoding.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @auther TyCoding
* @date 2018/9/19
*/
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private SolrTemplate solrTemplate;
@Autowired
private RedisTemplate redisTemplate;
/**
* 模拟演示redis缓存的效果
*
* @return
*/
public List<Goods> findAll() {
List<Goods> contentList = (List<Goods>) redisTemplate.boundHashOps("goods").get("all");
if (contentList == null) {
//说明缓存中没有数据
System.out.println("从数据库中读取数据放入redis...");
contentList = goodsMapper.findAll();
redisTemplate.boundHashOps("goods").put("all", contentList); //存入redis中
} else {
System.out.println("从缓存中读取数据...");
}
return contentList;
// return goodsMapper.findAll();
}
@Override
public List<Goods> findById(Long id) {
return goodsMapper.findById(id);
}
@Override
public void create(Goods goods) {
goodsMapper.create(goods);
Long id = goodsMapper.maxId(); //获取根据主键自增插入的最新一条记录的ID值
goods.setId(id);
//更新索引库
solrTemplate.saveBean(goods);
solrTemplate.commit();
}
@Override
public void update(Goods goods) {
goodsMapper.update(goods);
//更新索引库
solrTemplate.deleteById(String.valueOf(goods.getId()));
solrTemplate.commit();
List<Goods> list = new ArrayList<Goods>();
list.add(goods);
solrTemplate.saveBeans(list);
solrTemplate.commit();
}
@Override
public void delete(Long... ids) {
for (Long id : ids) {
goodsMapper.delete(id);
//更新索引库
solrTemplate.deleteById(String.valueOf(id));
solrTemplate.commit();
}
}
/**
* 分页查询-条件查询方法
*
* @param goods 查询条件
* @param pageCode 当前页
* @param pageSize 每页的记录数
* @return
*/
public PageBean findByPage(Goods goods, int pageCode, int pageSize) {
//使用Mybatis分页插件
PageHelper.startPage(pageCode, pageSize);
//调用分页查询方法,其实就是查询所有数据,mybatis自动帮我们进行分页计算
Page<Goods> page = goodsMapper.findByPage(goods);
return new PageBean(page.getTotal(), page.getResult());
}
/**
* 搜索 --- 从Solr索引库中
*
* @param searchMap
* @return
*/
@Override
public Map<String, Object> search(Map searchMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(searchList(searchMap));
return map;
}
//搜索列表
private Map searchList(Map searchMap) {
Map map = new HashMap();
//高亮配置
HighlightQuery query = new SimpleHighlightQuery();
HighlightOptions highlightOptions = new HighlightOptions().addField("item_title"); //设置高亮的域
highlightOptions.setSimplePrefix("<em style='color: red'>"); //设置高亮前缀
highlightOptions.setSimplePostfix("</em>"); //设置高亮后缀
query.setHighlightOptions(highlightOptions); //设置高亮选项
//关键字空格处理
String keywords = (String) searchMap.get("keywords");
searchMap.put("keywords", keywords.replace(" ", ""));
Criteria criteria = new Criteria("item_keywords");
//按照关键字查询
if (searchMap.get("keywords") != null) {
if (!searchMap.get("keywords").equals("")) {
criteria.is(searchMap.get("keywords"));
}
}
query.addCriteria(criteria);
//按商品分类过滤
if (searchMap.get("category") != null) {
if (!searchMap.get("category").equals("")) {
System.out.println("执行了category");
FilterQuery filterQuery = new SimpleFilterQuery();
Criteria filterCriteria = new Criteria("item_category").is(searchMap.get("category"));
filterQuery.addCriteria(filterCriteria);
query.addFilterQuery(filterQuery);
}
}
//按品牌过滤
if (searchMap.get("brand") != null) {
if (!searchMap.get("brand").equals("")) {
System.out.println("执行了brand...");
FilterQuery filterQuery = new SimpleFilterQuery();
Criteria filterCriteria = new Criteria("item_brand").is(searchMap.get("brand"));
filterQuery.addCriteria(filterCriteria);
query.addFilterQuery(filterQuery);
}
}
//按价格区间查询
if (searchMap.get("price") != null) {
if (!searchMap.get("price").equals("")) {
String[] price = ((String) searchMap.get("price")).split("-");
if (!price[0].equals("0")) {
//如果起点区间不等于0
Criteria filterCriteria = new Criteria("item_price").greaterThanEqual(price[0]);
FilterQuery filterQuery = new SimpleFilterQuery(filterCriteria);
query.addFilterQuery(filterQuery);
}
if (!price[1].equals("*")) {
//如果区间重点不等于*
Criteria filterCriteria = new Criteria("item_price").lessThanEqual(price[1]);
FilterQuery filterQuery = new SimpleFilterQuery(filterCriteria);
query.addFilterQuery(filterQuery);
}
}
}
//按价格的升降序查询
if (searchMap.get("sort") != null) {
if (!searchMap.get("sort").equals("")) {
String sortValue = (String) searchMap.get("sort");
String sortField = (String) searchMap.get("field");
if (sortValue != null && !sortValue.equals("")) {
if (sortValue.equals("asc")) {
Sort sort = new Sort(Sort.Direction.ASC, "item_" + sortField);
query.addSort(sort);
}
if (sortValue.equals("desc")) {
Sort sort = new Sort(Sort.Direction.DESC, "item_" + sortField);
query.addSort(sort);
}
}
}
}
/**
* 分页查询
*
* @Param pageCode 当前页
*
* @Param pageSize 每页显示的记录数
*/
Integer pageCode = (Integer) searchMap.get("pageCode");
if (pageCode == null) {
pageCode = 1; //默认第一页
}
Integer pageSize = (Integer) searchMap.get("pageSize");
if (pageSize == null) {
pageSize = 18; //默认18
}
query.setOffset((pageCode - 1) * pageSize); //从第几条记录开始查询:= 当前页 * 每页的记录数
query.setRows(pageSize);
HighlightPage<Goods> page = solrTemplate.queryForHighlightPage(query, Goods.class);
//循环高亮入口集合
for (HighlightEntry<Goods> h : page.getHighlighted()) {
Goods goods = h.getEntity(); //获取原实体类
if (h.getHighlights().size() > 0 && h.getHighlights().get(0).getSnipplets().size() > 0) {
goods.setTitle(h.getHighlights().get(0).getSnipplets().get(0)); //设置高亮的结果
}
}
map.put("rows", page.getContent()); //返回查询到的数据
map.put("totalPage", page.getTotalPages()); //返回总页数
map.put("total", page.getTotalElements()); //返回总记录数
return map;
}
}