敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.8 KiB

2 years ago
  1. package com.whn.hellospring.controller;
  2. import com.whn.hellospring.common.StateMessage;
  3. import com.whn.hellospring.common.Status;
  4. import com.whn.hellospring.model.CustomerDO;
  5. import com.whn.hellospring.request.CustomerDetailRequest;
  6. import com.whn.hellospring.request.CustomerLoginRequest;
  7. import com.whn.hellospring.request.DeleteCustomerRequest;
  8. import com.whn.hellospring.service.CustomerService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import javax.validation.Valid;
  12. import java.util.List;
  13. @RestController //处理http请求,默认返回json格式数据
  14. @RequestMapping(value = "/customer") //配置映射在/customer
  15. public class CustomerController {
  16. @Autowired
  17. CustomerService service;
  18. /**
  19. * 创建客户
  20. */
  21. @PostMapping(value = "/create")
  22. public Status postCustomer(@Valid @RequestBody CustomerDO customerDO) {
  23. try {
  24. CustomerDO customer = service.createCustomer(customerDO);
  25. Status status = new Status(StateMessage.SUCCESS, customer);
  26. return status;
  27. } catch (Exception e) {
  28. return new Status(StateMessage.UN_KNOW_REASON);
  29. }
  30. }
  31. /**
  32. * 编辑客户
  33. */
  34. @PostMapping(value = "/update")
  35. public Status updateCustomer(@Valid @RequestBody CustomerDO customerDO) {
  36. try {
  37. CustomerDO customer = service.updateCustomer(customerDO);
  38. Status status = new Status(StateMessage.SUCCESS, customer);
  39. return status;
  40. } catch (Exception e) {
  41. return new Status(StateMessage.UN_KNOW_REASON);
  42. }
  43. }
  44. /**
  45. * 获取顾客列表
  46. */
  47. @GetMapping(value = "/list")
  48. public Status getCustomerList() {
  49. try {
  50. List<CustomerDO> customerList = service.getCustomerList();
  51. Status status = new Status(StateMessage.SUCCESS, customerList);
  52. return status;
  53. } catch (Exception e) {
  54. return new Status(StateMessage.UN_KNOW_REASON);
  55. }
  56. }
  57. /**
  58. * 删除顾客1
  59. */
  60. @DeleteMapping(value = "/delete/{id}")
  61. public String deleteCustomer(@PathVariable Long id) {
  62. if (service.deleteCustomer(id) == 1) {
  63. return "success";
  64. } else {
  65. return "error";
  66. }
  67. }
  68. /**
  69. * 删除顾客2
  70. * @return
  71. */
  72. @PostMapping(value = "/delete")
  73. public Status postDeleteCustomer(@RequestBody DeleteCustomerRequest request) {
  74. try {
  75. if (service.deleteCustomer(request.getId()) == 1) {
  76. return new Status(StateMessage.SUCCESS, "success");
  77. }else{
  78. return new Status(StateMessage.SUCCESS, "error");
  79. }
  80. } catch (Exception e) {
  81. return new Status(StateMessage.UN_KNOW_REASON);
  82. }
  83. }
  84. /**
  85. * 客户详情
  86. */
  87. @PostMapping(value = "/detail")
  88. public Status detail(@RequestBody CustomerDetailRequest request) {
  89. try {
  90. CustomerDO customerDetail = service.getCustomerDetail(request.getCustomer_id());
  91. return new Status(StateMessage.SUCCESS, customerDetail);
  92. } catch (Exception e) {
  93. return new Status(StateMessage.UN_KNOW_REASON);
  94. }
  95. }
  96. /**
  97. * 客户登录
  98. */
  99. @PostMapping(value = "/login")
  100. public Status login(@RequestBody CustomerLoginRequest request) {
  101. try {
  102. String phone = request.getPhone();
  103. String password = request.getPassword();
  104. String loginMsg = service.login(phone, password);
  105. Status status = new Status(StateMessage.SUCCESS, loginMsg);
  106. return status;
  107. } catch (Exception e) {
  108. return new Status(StateMessage.UN_KNOW_REASON);
  109. }
  110. }
  111. @GetMapping(value = "/test")
  112. public String test() {
  113. return "hello";
  114. }
  115. }