warehouse_material.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package handlers
  2. import (
  3. "net/http"
  4. "easydo-echo_win7/models"
  5. "easydo-echo_win7/services"
  6. "easydo-echo_win7/utils"
  7. "easydo-echo_win7/middleware"
  8. "github.com/labstack/echo-contrib/session"
  9. "github.com/labstack/echo/v4"
  10. )
  11. func Add_warehouse_material_to_routes(e *echo.Echo) {
  12. group := e.Group("/warehouseMaterial")
  13. group.Use(middleware.AuthMiddleware)
  14. group.POST("/getPage", warehouseMaterialGetPage)
  15. group.POST("/getList", warehouseMaterialGetList)
  16. group.POST("/save", warehouseMaterialSave)
  17. group.POST("/update", warehouseMaterialUpdate)
  18. // group.POST("/remove", warehouseMaterialRemove)
  19. }
  20. func warehouseMaterialGetPage(c echo.Context) error {
  21. var paramMap map[string]interface{}
  22. if err := c.Bind(&paramMap); err != nil {
  23. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  24. }
  25. result, err := services.JdbcClient.GetJdbcPage(paramMap, models.WarehouseMaterial{})
  26. if err != nil {
  27. utils.PrintSqlErr(err)
  28. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  29. }
  30. list := utils.ConvertInterface[[]models.WarehouseMaterial](result.Records)
  31. if len(list) == 0 {
  32. list = []models.WarehouseMaterial{}
  33. }
  34. for i := range list {
  35. model := list[i]
  36. material := new(models.ProductMaterial)
  37. material.Code = model.MaterialCode
  38. err = services.JdbcClient.GetJdbcModel(material)
  39. if err != nil {
  40. utils.PrintSqlErr(err)
  41. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  42. }
  43. model.ProductMaterial = material
  44. warehouse := new(models.Warehouse)
  45. warehouse.ID = model.WarehouseId
  46. err = services.JdbcClient.GetJdbcModelById(warehouse)
  47. if err != nil {
  48. utils.PrintSqlErr(err)
  49. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  50. }
  51. model.Warehouse = warehouse
  52. list[i] = model
  53. }
  54. result.Records = list
  55. return c.JSON(http.StatusOK, result)
  56. }
  57. func warehouseMaterialGetList(c echo.Context) error {
  58. var paramMap map[string]interface{}
  59. if err := c.Bind(&paramMap); err != nil {
  60. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  61. }
  62. result, err := services.JdbcClient.GetJdbcList(paramMap, models.WarehouseMaterial{})
  63. if err != nil {
  64. utils.PrintSqlErr(err)
  65. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  66. }
  67. list := utils.ConvertInterface[[]models.WarehouseMaterial](result)
  68. if len(list) == 0 {
  69. return c.JSON(http.StatusOK, []string{})
  70. }
  71. return c.JSON(http.StatusOK, list)
  72. }
  73. func warehouseMaterialSave(c echo.Context) error {
  74. tx, _ := services.MYSQL_DB.Beginx()
  75. sess, _ := session.Get("auth_session", c)
  76. user_id, ok := sess.Values["user_id"].(int64)
  77. if !ok {
  78. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  79. }
  80. warehouse_material := new(models.WarehouseMaterial)
  81. if err := c.Bind(warehouse_material); err != nil {
  82. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  83. }
  84. status := models.Status_Enable
  85. warehouse_material.Status = &status
  86. warehouse_material.CreateId = &user_id
  87. services.JdbcClient.JdbcInsert(warehouse_material, tx)
  88. warehouse_record := new(models.WarehouseRecord)
  89. warehouse_record.MaterialCode = warehouse_material.MaterialCode
  90. record_type := models.Warehouse_Record_Type_In
  91. warehouse_record.Type = &record_type
  92. warehouse_record.CreateId = &user_id
  93. warehouse_record.Number = warehouse_material.Number
  94. warehouse_record.TenantId = warehouse_material.TenantId
  95. warehouse_record.ToWarehouseId = warehouse_material.WarehouseId
  96. err := services.JdbcClient.JdbcInsert(warehouse_record, tx)
  97. if err != nil {
  98. utils.PrintSqlErr(err)
  99. tx.Rollback()
  100. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  101. }
  102. tx.Commit()
  103. return c.JSON(http.StatusOK, warehouse_material)
  104. }
  105. func warehouseMaterialUpdate(c echo.Context) error {
  106. tx, _ := services.MYSQL_DB.Beginx()
  107. warehouse_material := new(models.WarehouseMaterial)
  108. if err := c.Bind(warehouse_material); err != nil {
  109. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  110. }
  111. exist_warehouse_material := new(models.WarehouseMaterial)
  112. exist_warehouse_material.ID = warehouse_material.ID
  113. err := services.JdbcClient.GetJdbcModelById(exist_warehouse_material, tx)
  114. if err != nil {
  115. utils.PrintSqlErr(err)
  116. tx.Rollback()
  117. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  118. }
  119. sess, _ := session.Get("auth_session", c)
  120. user_id, ok := sess.Values["user_id"].(int64)
  121. if !ok {
  122. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  123. }
  124. warehouse_material.UpdateId = &user_id
  125. err = services.JdbcClient.JdbcUpdateById(warehouse_material, tx)
  126. if err != nil {
  127. utils.PrintSqlErr(err)
  128. tx.Rollback()
  129. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  130. }
  131. record := new(models.WarehouseRecord)
  132. record.MaterialCode = warehouse_material.MaterialCode
  133. record_type := ""
  134. if *exist_warehouse_material.LockedNumber < *warehouse_material.LockedNumber {
  135. record_type = models.Warehouse_Record_Type_Lock
  136. } else if *exist_warehouse_material.Number < *warehouse_material.Number {
  137. record_type = models.Warehouse_Record_Type_In
  138. } else if *exist_warehouse_material.Number > *warehouse_material.Number {
  139. record_type = models.Warehouse_Record_Type_Out
  140. } else {
  141. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("记录类型出现异常", ""))
  142. }
  143. record.Type = &record_type
  144. record.CreateId = &user_id
  145. record.Number = warehouse_material.Number
  146. record.TenantId = warehouse_material.TenantId
  147. record.ToWarehouseId = warehouse_material.WarehouseId
  148. err = services.JdbcClient.JdbcInsert(record, tx)
  149. if err != nil {
  150. utils.PrintSqlErr(err)
  151. tx.Rollback()
  152. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  153. }
  154. tx.Commit()
  155. return c.JSON(http.StatusOK, warehouse_material)
  156. }
  157. func warehouseMaterialRemove(c echo.Context) error {
  158. tx, _ := services.MYSQL_DB.Beginx()
  159. warehouse_material := new(models.WarehouseMaterial)
  160. if err := c.Bind(warehouse_material); err != nil {
  161. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  162. }
  163. sess, _ := session.Get("auth_session", c)
  164. user_id, ok := sess.Values["user_id"].(int64)
  165. if !ok {
  166. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  167. }
  168. err := services.JdbcClient.JdbcRemoveById(warehouse_material, tx)
  169. if err != nil {
  170. utils.PrintSqlErr(err)
  171. tx.Rollback()
  172. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  173. }
  174. record := new(models.WarehouseRecord)
  175. record.MaterialCode = warehouse_material.MaterialCode
  176. record_type := models.Warehouse_Record_Type_Out
  177. record.Type = &record_type
  178. record.CreateId = &user_id
  179. record.Number = warehouse_material.Number
  180. record.TenantId = warehouse_material.TenantId
  181. record.ToWarehouseId = warehouse_material.WarehouseId
  182. err = services.JdbcClient.JdbcInsert(record, tx)
  183. if err != nil {
  184. utils.PrintSqlErr(err)
  185. tx.Rollback()
  186. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  187. }
  188. tx.Commit()
  189. return c.JSON(http.StatusOK, warehouse_material)
  190. }