ProductController.java
4.65 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
package vion.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.Product;
import vion.model.Store;
import vion.service.IProductSerrvice;
import vion.service.IStoreSerrvice;
import vion.utils.ResultUtil;
import vion.vo.ProductVO;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class ProductController {
@Autowired
private IProductSerrvice productSerrvice;
@Autowired
private IStoreSerrvice storeSerrvice;
@GetMapping("/products")
@ResponseBody
public Object getProductList(@RequestParam(name = "accountId",required=false)Integer account_id,
@RequestParam(name = "storeId",required=false)Integer store_id,
@RequestParam(name = "name",required=false)String name,
@RequestParam(name = "brand",required=false)Integer brand,
@RequestParam(name = "functionary",required=false)Integer functionary,
@RequestParam(name = "pageNum")Integer pageNum,
@RequestParam(name = "pageSize")Integer pageSize){
//pageNum当前页,pageSize每页条数
Page page= PageHelper.startPage(pageNum,pageSize);
QueryWrapper<Product> wrapper=new QueryWrapper<>();
if(store_id!=null){
wrapper.eq("store_id",store_id);
}
if(account_id!=null)
wrapper.eq("account_id",account_id);
if(name!=null)
wrapper.eq("name",name);
if(brand!=null)
wrapper.eq("brand",brand);
if(functionary!=null)
wrapper.eq("functionary",functionary);
List<Product> productList=productSerrvice.list(wrapper);
List<Store> storeList=storeSerrvice.list();
List<ProductVO> productVOS=new ArrayList<>();
ProductVO productVO;
for(Product pro:productList){
productVO=new ProductVO();
productVO.setStoreName(storeList.stream().filter(store -> store.getId().equals(pro.getStoreId())).collect(Collectors.toList()).get(0).getName());
productVO.setId(pro.getId());
productVO.setStoreId(pro.getStoreId());
productVO.setMaterialNo(pro.getMaterialNo());
productVO.setName(pro.getName());
productVO.setFunctionary(pro.getFunctionary());
productVO.setBrand(pro.getBrand());
productVO.setProductCount(pro.getProductCount());
productVO.setProductPrice(pro.getProductPrice());
productVO.setProductTotalPrice(pro.getProductTotalPrice());
productVO.setProductSubtotal(pro.getProductSubtotal());
productVO.setCreateTime(pro.getCreateTime());
productVO.setModifyTime(pro.getModifyTime());
productVO.setRemark(pro.getRemark());
productVO.setAccountId(pro.getAccountId());
productVOS.add(productVO);
}
productVOS.sort(Comparator.comparing(ProductVO::getId));
PageInfo pageInfo=new PageInfo<>(page);
pageInfo.setList(productList);
if (pageInfo != null) {
return ResultUtil.success(pageInfo);
} else {
return ResultUtil.error(ResultEnum.SELECT_ERROR);
}
}
@GetMapping("/product")
@ResponseBody
public Object getProductByID(@RequestParam(name = "projectId")Integer project_id){
Product product=productSerrvice.getById(project_id);
if (product != null) {
return ResultUtil.success(product);
} else {
return ResultUtil.error(ResultEnum.SELECT_ERROR);
}
}
@PostMapping("/products")
@ResponseBody
public Object saveOrUpdate(@RequestBody Product data) {
try {
if(data.getId()==null){
productSerrvice.save(data);
return ResultUtil.success();
}else{
data.setModifyTime(new Date());
Product product=productSerrvice.saveAndReturn(data);
return ResultUtil.success(product);
}
}catch (Exception e) {
//return ResultUtil.error(ResultEnum.SELECT_ERROR);
return e.getMessage().toString();
}
}
}