-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductServiceImpl.java
More file actions
366 lines (304 loc) · 10.1 KB
/
Copy pathProductServiceImpl.java
File metadata and controls
366 lines (304 loc) · 10.1 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package cn.snnu.mm.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import cn.snnu.mm.bean.Brand;
import cn.snnu.mm.bean.Carrier;
import cn.snnu.mm.bean.Category;
import cn.snnu.mm.bean.Consignee;
import cn.snnu.mm.bean.Order;
import cn.snnu.mm.bean.OrderItem;
import cn.snnu.mm.bean.PageBean;
import cn.snnu.mm.bean.Product;
import cn.snnu.mm.dao.ProductDao;
import cn.snnu.mm.service.ProductService;
@Service
public class ProductServiceImpl implements ProductService {
@Resource
private ProductDao productDao;
@Override
public List<Product> findByHot() throws SQLException {
return productDao.findByHot();
}
@Override
public List<Product> findByNew() throws SQLException {
return productDao.findByNew();
}
@Override
public Product findById(String pid) throws SQLException {
return productDao.findById(pid);
}
@Override
public void submitOrder(Order order) throws SQLException {
//调用dao存储order表数据的方法
productDao.addOrders(order);
//调用dao存储orderitem表数据的方法
productDao.addOrderItem(order);
}
@Override
public List<Consignee> findInfoById(String id) throws SQLException {
return productDao.findInfoById(id);
}
@Override
public void delConsignee(String cid) throws SQLException {
productDao.delConsignee(cid);
}
@Override
public void saveConsignee(String uuid, String location, String name, String address, String telephone,
String email, String uid) throws SQLException {
productDao.saveConsignee(uuid,location,name,address,telephone,email,uid);
}
@Override
public void editConsignee(String flag, String location, String name, String address, String telephone,
String email, String uid) throws SQLException {
productDao.editConsignee(flag,location,name,address,telephone,email,uid);
}
@Override
public void updateOrder(Order order) throws SQLException {
productDao.updateOrder(order);
}
@Override
public PageBean findAllOrders(String id, int currentPage, int currentCount, String time, String word) throws SQLException {
//封装一个PageBean 返回web层
PageBean<Order> pageBean = new PageBean<Order>();
//1、封装当前页
pageBean.setCurrentPage(currentPage);
//2、封装每页显示的条数
pageBean.setCurrentCount(currentCount);
//3、封装总条数
int totalCount = 0;
try {
totalCount = productDao.getCount(id);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setTotalCount(totalCount);
//4、封装总页数
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、当前页显示的数据
// select * from product where cid=? limit ?,?
// 当前页与起始索引index的关系
int index = (currentPage-1)*currentCount;
List<Order> orderList = null;
/**
* 独创
*
* @author zhangwz
* 2018年5月12日 上午01:14:37
*/
try {
/**
* 查询该用户的所有的订单信息(单表查询orders表)
* 集合中的每一个Order对象的数据是不完整的 缺少List<OrderItem> orderItems数据
*/
orderList = productDao.findAllOrders(id,index,currentCount);
//循环所有的订单 为每个订单填充订单项集合信息
if(orderList!=null){
for(Order order : orderList){
//获得每一个订单的oid
String oid = order.getOid();
//查询该订单的所有的订单项---mapList封装的是多个订单项和该订单项中的商品的信息
List<Map<String, Object>> mapList = productDao.findAllOrderItemByOid(oid,time,word);
//将mapList转换成List<OrderItem> orderItems
for(Map<String,Object> map : mapList){
try {
//从map中取出count subtotal 封装到OrderItem中
OrderItem item = new OrderItem();
//item.setCount(Integer.parseInt(map.get("count").toString()));
BeanUtils.populate(item, map);
//从map中取出pimage pname shop_price 封装到Product中
Product product = new Product();
BeanUtils.populate(product, map);
//将product封装到OrderItem
item.setProduct(product);
//将orderitem封装到order中的orderItemList中
order.getOrderItems().add(item);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
pageBean.setList(orderList);
System.out.println("pageBean = " + JSON.toJSONString(pageBean));
return pageBean;
}
@Override
public List<Map<String, Object>> findAllOrderItemByOid(String oid, String time, String word) throws SQLException{
List<Map<String, Object>> mapList = null;
try {
mapList = productDao.findAllOrderItemByOid(oid,time,word);
} catch (Exception e) {
e.printStackTrace();
}
return mapList;
}
@Override
public List<Map<String, Object>> findAllOrderItemByPid(String[] pidArray, Order order) throws SQLException {
List<Map<String, Object>> mapList = null;
try {
mapList = productDao.findAllOrderItemByPid(pidArray, order);
} catch (Exception e) {
e.printStackTrace();
}
return mapList;
}
@Override
public List<Brand> finBrand() throws SQLException {
return productDao.finBrand();
}
@Override
public PageBean findBrandListByBid(String flag, String bid, int currentPage, int currentCount) throws SQLException {
//封装一个PageBean 返回web层
PageBean<Product> pageBean = new PageBean<Product>();
//1、封装当前页
pageBean.setCurrentPage(currentPage);
//2、封装每页显示的条数
pageBean.setCurrentCount(currentCount);
//3、封装总条数
int totalCount = 0;
try {
totalCount = productDao.getBrandCount(flag,bid);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setTotalCount(totalCount);
//4、封装总页数
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、当前页显示的数据
// select * from product where cid=? limit ?,?
// 当前页与起始索引index的关系
int index = (currentPage-1)*currentCount;
List<Product> list = null;
try {
list = productDao.findBrandProductByPage(flag,bid,index,currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setList(list);
return pageBean;
}
@Override
public PageBean findProductListByName(String name, int currentPage, int currentCount) throws SQLException {
//封装一个PageBean 返回web层
PageBean<Product> pageBean = new PageBean<Product>();
//1、封装当前页
pageBean.setCurrentPage(currentPage);
//2、封装每页显示的条数
pageBean.setCurrentCount(currentCount);
//3、封装总条数
int totalCount = 0;
try {
totalCount = productDao.getProductCount(name);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setTotalCount(totalCount);
//4、封装总页数
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、当前页显示的数据
// select * from product where cid=? limit ?,?
// 当前页与起始索引index的关系
int index = (currentPage-1)*currentCount;
List<Product> list = null;
try {
list = productDao.findProductByPage(name,index,currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setList(list);
return pageBean;
}
@Override
public PageBean findBrandFromResult(String flag, String bid, String word, int currentPage, int currentCount) {
//封装一个PageBean 返回web层
PageBean<Product> pageBean = new PageBean<Product>();
//1、封装当前页
pageBean.setCurrentPage(currentPage);
//2、封装每页显示的条数
pageBean.setCurrentCount(currentCount);
//3、封装总条数
int totalCount = 0;
try {
totalCount = productDao.getBrandCount(flag,bid);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setTotalCount(totalCount);
//4、封装总页数
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、当前页显示的数据
// select * from product where cid=? limit ?,?
// 当前页与起始索引index的关系
int index = (currentPage-1)*currentCount;
List<Product> list = null;
try {
list = productDao.findProductResultByPage(flag,bid,word,index,currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setList(list);
return pageBean;
}
@Override
public PageBean findProductFromResult(String name, String word, int currentPage, int currentCount)
throws SQLException {
//封装一个PageBean 返回web层
PageBean<Product> pageBean = new PageBean<Product>();
//1、封装当前页
pageBean.setCurrentPage(currentPage);
//2、封装每页显示的条数
pageBean.setCurrentCount(currentCount);
//3、封装总条数
int totalCount = 0;
try {
totalCount = productDao.getProductCount(name);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setTotalCount(totalCount);
//4、封装总页数
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、当前页显示的数据
// select * from product where cid=? limit ?,?
// 当前页与起始索引index的关系
int index = (currentPage-1)*currentCount;
List<Product> list = null;
try {
list = productDao.findProductResult(name,word,index,currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
pageBean.setList(list);
return pageBean;
}
@Override
public List<Product> findHotBrand() throws SQLException {
return productDao.findHotBrand();
}
@Override
public List<Category> findCategory() throws SQLException {
return productDao.findCategory();
}
@Override
public List<Carrier> finCarrier() throws SQLException {
return productDao.finCarrier();
}
@Override
public List<Product> findAccessories() throws SQLException {
return productDao.findAccessories();
}
}