CustomerController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.free.controller.system;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.validation.Valid;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.cglib.beans.BeanCopier;
  8. import org.springframework.web.bind.annotation.DeleteMapping;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PathVariable;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  17. import com.baomidou.mybatisplus.core.metadata.IPage;
  18. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  19. import com.free.config.CustomizationException;
  20. import com.free.config.ExceptionEnum;
  21. import com.free.config.ResponseFormat;
  22. import com.free.dto.system.CustomerCreateDTO;
  23. import com.free.entity.system.Customer;
  24. import com.free.service.system.CustomerService;
  25. import com.free.utils.BcryptUtil;
  26. import io.swagger.annotations.Api;
  27. import io.swagger.annotations.ApiOperation;
  28. @RestController
  29. @RequestMapping("/customer")
  30. @Api(tags = "客服人员")
  31. public class CustomerController {
  32. @Autowired
  33. private CustomerService service;
  34. /** 创建数据 */
  35. @ApiOperation("创建数据")
  36. @PostMapping("")
  37. public Object save(@RequestBody @Valid CustomerCreateDTO data) {
  38. // 检查账号是否重复
  39. QueryWrapper<Customer> qw = new QueryWrapper<Customer>();
  40. qw.eq("account", data.getAccount());
  41. Long num = this.service.count(qw);
  42. // 重复抛出异常
  43. if (num > 0) {
  44. throw new CustomizationException(ExceptionEnum.ACCOUNT_IS_EXIST);
  45. }
  46. BeanCopier copier = BeanCopier.create(CustomerCreateDTO.class, Customer.class, false);
  47. Customer insertData = new Customer();
  48. copier.copy(data, insertData, null);
  49. // 密码加密
  50. String ep = BcryptUtil.encryptPassword(insertData.getPassword());
  51. if (null != ep) {
  52. insertData.setPassword(ep);
  53. }
  54. this.service.save(insertData);
  55. // 再将数据查出来返回
  56. Customer returnData = this.service.getById(insertData.getId());
  57. return ResponseFormat.success(returnData);
  58. }
  59. /** 修改数据 */
  60. @ApiOperation("修改数据")
  61. @PostMapping("/{id}")
  62. public Object update(@PathVariable long id, @RequestBody Customer data) {
  63. QueryWrapper<Customer> qw = new QueryWrapper<Customer>();
  64. qw.eq("id", id);
  65. Long num = this.service.count(qw);
  66. if (num <= 0) {
  67. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  68. }
  69. data.setId(id);
  70. // 密码不在修改范围内,清空
  71. data.setPassword(null);
  72. this.service.updateById(data);
  73. Object newData = this.service.getById(id);
  74. return ResponseFormat.success(newData);
  75. }
  76. /** 列表查询 */
  77. @ApiOperation("查询数据列表")
  78. @SuppressWarnings({ "unchecked" })
  79. @GetMapping()
  80. public Object list(@RequestParam Map<String, Object> allParams) {
  81. Long skip = null, limit = null;
  82. Map map = new HashMap();
  83. QueryWrapper qw = new QueryWrapper<>();
  84. /** 参数处理处理 */
  85. for (String key : allParams.keySet()) {
  86. Object value = allParams.get(key);
  87. if (key.equals("skip")) {
  88. skip = Long.valueOf(String.valueOf(value));
  89. } else if (key.equals("limit")) {
  90. limit = Long.valueOf(String.valueOf(value));
  91. } else {
  92. // 其他为查询条件
  93. qw.eq(key, value);
  94. }
  95. }
  96. /** 分页处理 */
  97. if (null != skip && null != limit) {
  98. IPage page = new Page<>(skip, limit);
  99. IPage pageResult = service.page(page, qw);
  100. List data = pageResult.getRecords();
  101. long total = pageResult.getTotal();
  102. map.put("data", data);
  103. map.put("total", total);
  104. } else {
  105. List list = service.list(qw);
  106. map.put("data", list);
  107. }
  108. return ResponseFormat.success(map);
  109. }
  110. /** 根据id查询 */
  111. @ApiOperation("查询数据")
  112. @GetMapping("/{id}")
  113. public Object fetch(@PathVariable long id) {
  114. Object newData = service.getById(id);
  115. return ResponseFormat.success(newData);
  116. }
  117. /** 根据id删除数据 */
  118. @ApiOperation("删除数据")
  119. @DeleteMapping("/{id}")
  120. public Object delete(@PathVariable long id) {
  121. QueryWrapper<Customer> qw = new QueryWrapper<>();
  122. qw.eq("id", id);
  123. Long num = service.count(qw);
  124. if (num <= 0) {
  125. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  126. }
  127. service.removeById(id);
  128. return ResponseFormat.success();
  129. }
  130. }