product_material.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package handlers
  2. import (
  3. "net/http"
  4. "easydo-echo_win7/middleware"
  5. "easydo-echo_win7/models"
  6. "easydo-echo_win7/services"
  7. "easydo-echo_win7/utils"
  8. "github.com/jmoiron/sqlx"
  9. "github.com/labstack/echo-contrib/session"
  10. "github.com/labstack/echo/v4"
  11. )
  12. func Add_product_material_to_routes(e *echo.Echo) {
  13. group := e.Group("/processMaterial")
  14. group.Use(middleware.AuthMiddleware)
  15. group.POST("/getPage", productMaterialGetPage)
  16. group.POST("/getList", productMaterialGetList)
  17. group.POST("/save", productMaterialSave)
  18. group.POST("/update", productMaterialUpdate)
  19. group.POST("/remove", productMaterialRemove)
  20. }
  21. func productMaterialGetPage(c echo.Context) error {
  22. var paramMap map[string]interface{}
  23. if err := c.Bind(&paramMap); err != nil {
  24. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  25. }
  26. result, err := services.JdbcClient.GetJdbcPage(paramMap, models.ProductMaterial{})
  27. if err != nil {
  28. utils.PrintSqlErr(err)
  29. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  30. }
  31. list := utils.ConvertInterface[[]models.ProductMaterial](result.Records)
  32. if len(list) == 0 {
  33. list = []models.ProductMaterial{}
  34. }
  35. result.Records = list
  36. return c.JSON(http.StatusOK, result)
  37. }
  38. func productMaterialGetList(c echo.Context) error {
  39. var paramMap map[string]interface{}
  40. if err := c.Bind(&paramMap); err != nil {
  41. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  42. }
  43. result, err := services.JdbcClient.GetJdbcList(paramMap, models.ProductMaterial{})
  44. if err != nil {
  45. utils.PrintSqlErr(err)
  46. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  47. }
  48. list := utils.ConvertInterface[[]models.ProductMaterial](result)
  49. if len(list) == 0 {
  50. return c.JSON(http.StatusOK, []string{})
  51. }
  52. return c.JSON(http.StatusOK, list)
  53. }
  54. func productMaterialSave(c echo.Context) error {
  55. tx, _ := services.MYSQL_DB.Beginx()
  56. material := new(models.ProductMaterial)
  57. if err := c.Bind(material); err != nil {
  58. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  59. }
  60. sess, _ := session.Get("auth_session", c)
  61. err_msg := productMaterial_generateCode(material, tx)
  62. if err_msg != "" {
  63. tx.Rollback()
  64. return c.JSON(http.StatusBadRequest, utils.ErrorResponse(err_msg, ""))
  65. }
  66. user_id, ok := sess.Values["user_id"].(int64)
  67. if !ok {
  68. return c.JSON(http.StatusBadRequest, utils.ErrorResponse("用户请先登录", ""))
  69. }
  70. status := models.Status_Enable
  71. material.Status = &status
  72. material.CreateId = &user_id
  73. err := services.JdbcClient.JdbcInsert(material, tx)
  74. if err != nil {
  75. utils.PrintSqlErr(err)
  76. tx.Rollback()
  77. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  78. }
  79. tx.Commit()
  80. return c.JSON(http.StatusOK, material)
  81. }
  82. func productMaterialUpdate(c echo.Context) error {
  83. tx, _ := services.MYSQL_DB.Beginx()
  84. material := new(models.ProductMaterial)
  85. if err := c.Bind(material); err != nil {
  86. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  87. }
  88. sess, _ := session.Get("auth_session", c)
  89. user_id, ok := sess.Values["user_id"].(int64)
  90. if !ok {
  91. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  92. }
  93. material.UpdateId = &user_id
  94. err := services.JdbcClient.JdbcUpdateById(material, tx)
  95. if err != nil {
  96. utils.PrintSqlErr(err)
  97. tx.Rollback()
  98. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  99. }
  100. bom := new(models.ProductBom)
  101. bom.MaterialName = material.Name
  102. paramMap := map[string]interface{}{
  103. "materialCode": material.Code,
  104. }
  105. err = services.JdbcClient.JdbcUpdate(bom, paramMap, tx)
  106. if err != nil {
  107. utils.PrintSqlErr(err)
  108. tx.Rollback()
  109. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  110. }
  111. tx.Commit()
  112. return c.JSON(http.StatusOK, material)
  113. }
  114. func productMaterialRemove(c echo.Context) error {
  115. material := new(models.ProductMaterial)
  116. if err := c.Bind(material); err != nil {
  117. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  118. }
  119. err := services.JdbcClient.GetJdbcModelById(material)
  120. if err != nil {
  121. utils.PrintSqlErr(err)
  122. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  123. }
  124. bom := new(models.ProductBom)
  125. bom.MaterialCode = material.Code
  126. count, err := services.JdbcClient.GetJdbcCount(bom)
  127. if err != nil {
  128. utils.PrintSqlErr(err)
  129. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  130. }
  131. if count > 0 {
  132. return c.JSON(http.StatusBadRequest, utils.ErrorResponse("该物料在bom管理中存在,无法删除", ""))
  133. }
  134. err = services.JdbcClient.JdbcRemoveById(material)
  135. if err != nil {
  136. utils.PrintSqlErr(err)
  137. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  138. }
  139. return c.JSON(http.StatusOK, material)
  140. }
  141. func productMaterial_generateCode(model *models.ProductMaterial, tx *sqlx.Tx) string {
  142. sync := new(models.Synchronized)
  143. sync.Sync.Lock()
  144. defer sync.Sync.Unlock()
  145. if model.Code != nil && len(*model.Code) > 0 {
  146. modelParam := new(models.ProductMaterial)
  147. modelParam.Code = model.Code
  148. modelParam.TenantId = model.TenantId
  149. count, err := services.JdbcClient.GetJdbcCount(modelParam, tx)
  150. if err != nil {
  151. utils.PrintSqlErr(err)
  152. return "SQL执行失败"
  153. }
  154. if count > 0 {
  155. return "编号已存在,请重新填写"
  156. }
  157. } else {
  158. for {
  159. code, err := services.GetFlowNo(models.Flow_No_Type_Product_Material, *model.TenantId, tx)
  160. if err != nil {
  161. return "自动生成编码失败"
  162. }
  163. modelParam := new(models.ProductMaterial)
  164. modelParam.Code = &code
  165. modelParam.TenantId = model.TenantId
  166. count, err := services.JdbcClient.GetJdbcCount(modelParam, tx)
  167. if err != nil {
  168. utils.PrintSqlErr(err)
  169. return "SQL执行失败"
  170. }
  171. if count == 0 {
  172. model.Code = &code
  173. break
  174. }
  175. }
  176. }
  177. return ""
  178. }