AccountController.java
2.29 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
package vion.controller;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.Account;
import vion.model.Store;
import vion.service.IAccountSerrvice;
import vion.utils.ResultUtil;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping(Global.BASE_URL)
@Slf4j
public class AccountController {
@Autowired
private IAccountSerrvice accountSerrvice;
@GetMapping("/accounts")
@ResponseBody
public Object getAccountList(@RequestParam(name = "pageNum")Integer pageNum,
@RequestParam(name = "pageSize")Integer pageSize){
//pageNum当前页,pageSize每页条数
Page page= PageHelper.startPage(pageNum,pageSize);
List<Account> accountList=accountSerrvice.list();
accountList.sort(Comparator.comparing(Account::getId));
PageInfo pageInfo=new PageInfo<>(page);
pageInfo.setList(accountList);
if (accountList != null) {
return ResultUtil.success(pageInfo);
} else {
return ResultUtil.error(ResultEnum.SELECT_ERROR);
}
}
@GetMapping("/account")
@ResponseBody
public Object getProductByID(@RequestParam(name = "id")Integer id){
Account account=accountSerrvice.getById(id);//accountDao.getAccountById(id);
if (account != null) {
return ResultUtil.success(account);
} else {
return ResultUtil.error(ResultEnum.SELECT_ERROR);
}
}
@PostMapping("/accounts")
@ResponseBody
public Object saveOrUpdate(@RequestBody Account data) {
try {
if(data.getId()==null){
data.setCreateTime(new Date());
}else{
data.setModifyTime(new Date());
}
Account account= accountSerrvice.saveAndReturn(data);
return ResultUtil.success(account);
}catch (Exception e) {
return ResultUtil.error(ResultEnum.SELECT_ERROR);
}
}
}