SalePlanController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package easydo.technology.controller;
  2. import easydo.technology.components.JdbcClient;
  3. import easydo.technology.enums.MESEnum;
  4. import easydo.technology.exception.BizException;
  5. import easydo.technology.model.SalePlan;
  6. import easydo.technology.service.FlowNoService;
  7. import easydo.technology.utils.StringUtil;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.RequestBody;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import javax.annotation.Resource;
  14. import javax.sql.DataSource;
  15. import java.sql.Connection;
  16. import java.util.List;
  17. import java.util.Map;
  18. @RestController
  19. @RequestMapping("/salePlan")
  20. public class SalePlanController {
  21. @Resource
  22. JdbcClient jdbcClient;
  23. @Resource
  24. DataSource dataSource;
  25. @Resource
  26. FlowNoService flowNoService;
  27. @RequestMapping(value = "/getList")
  28. public Object getList(@RequestBody Map<String, Object> map) throws Exception {
  29. List<SalePlan> list = jdbcClient.getJdbcList(map, SalePlan.class);
  30. return new ResponseEntity<>(list, HttpStatus.OK);
  31. }
  32. @RequestMapping(value = "/getPage")
  33. public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
  34. Connection connection = dataSource.getConnection();
  35. try {
  36. Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, SalePlan.class, connection);
  37. List<SalePlan> records = (List<SalePlan>) recordsPage.get("records");
  38. return new ResponseEntity<>(recordsPage, HttpStatus.OK);
  39. } catch (Exception e) {
  40. throw new BizException(e.getMessage());
  41. } finally {
  42. connection.close();
  43. }
  44. }
  45. @RequestMapping(value = "/save")
  46. public Object save(@RequestBody SalePlan model) throws Exception {
  47. Connection connection = dataSource.getConnection();
  48. try {
  49. connection.setAutoCommit(false);
  50. generateCode(model, connection);
  51. jdbcClient.jdbcInsert(model, connection);
  52. connection.commit();
  53. return new ResponseEntity<>(model, HttpStatus.OK);
  54. } catch (Exception e) {
  55. connection.rollback();
  56. throw new BizException(e.getMessage());
  57. } finally {
  58. connection.close();
  59. }
  60. }
  61. @RequestMapping(value = "/update")
  62. public Object update(@RequestBody SalePlan model) throws Exception {
  63. Connection connection = dataSource.getConnection();
  64. try {
  65. connection.setAutoCommit(false);
  66. jdbcClient.jdbcUpdateById(model, connection);
  67. connection.commit();
  68. return new ResponseEntity<>(HttpStatus.OK);
  69. } catch (Exception e) {
  70. connection.rollback();
  71. throw new BizException(e.getMessage());
  72. } finally {
  73. connection.close();
  74. }
  75. }
  76. @RequestMapping(value = "/remove")
  77. public Object deleteProgram(@RequestBody SalePlan model) throws Exception {
  78. Connection connection = dataSource.getConnection();
  79. try {
  80. jdbcClient.jdbcRemoveById(model, connection);
  81. return new ResponseEntity<>(HttpStatus.OK);
  82. } catch (Exception e) {
  83. throw new BizException(e.getMessage());
  84. } finally {
  85. connection.close();
  86. }
  87. }
  88. private synchronized void generateCode(SalePlan model, Connection connection) throws Exception {
  89. if (StringUtil.isEmpty(model.getCode())) {
  90. while (true) {
  91. String flowNo = flowNoService.getFlowNo(MESEnum.FLOW_NO_TYPE_SALE_PLAN.getValue(), connection);
  92. SalePlan param = new SalePlan();
  93. param.setCode(flowNo);
  94. int count = jdbcClient.getJdbcCount(param, connection);
  95. if (count == 0) {
  96. model.setCode(flowNo);
  97. break;
  98. }
  99. }
  100. } else {
  101. SalePlan param = new SalePlan();
  102. param.setCode(model.getCode());
  103. int count = jdbcClient.getJdbcCount(param, connection);
  104. if (count > 0) {
  105. throw new BizException("编号已存在,请重新填写");
  106. }
  107. }
  108. }
  109. }