product_bom.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_bom_to_routes(e *echo.Echo) {
  13. group := e.Group("/productBom")
  14. group.Use(middleware.AuthMiddleware)
  15. group.POST("/getPage", productBomGetPage)
  16. group.POST("/getChildrenList", productBomGetChildrenList)
  17. group.POST("/save", productBomSave)
  18. group.POST("/update", productBomUpdate)
  19. group.POST("/remove", productBomRemove)
  20. }
  21. func productBomGetPage(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.ProductBom{})
  27. if err != nil {
  28. utils.PrintSqlErr(err)
  29. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  30. }
  31. list := utils.ConvertInterface[[]models.ProductBom](result.Records)
  32. if len(list) == 0 {
  33. list = []models.ProductBom{}
  34. }
  35. for i := range list {
  36. model := list[i]
  37. material := new(models.ProductMaterial)
  38. material.Code = model.MaterialCode
  39. err = services.JdbcClient.GetJdbcModel(material)
  40. if err != nil {
  41. utils.PrintSqlErr(err)
  42. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  43. }
  44. model.Material = material
  45. bomParam := new(models.ProductBom)
  46. bomParam.ParentId = model.ID
  47. count, err := services.JdbcClient.GetJdbcCount(bomParam)
  48. if err != nil {
  49. utils.PrintSqlErr(err)
  50. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  51. }
  52. if count > 0 {
  53. isHaveChildren := models.Common_Value_True_Value
  54. model.IsHaveChildren = &isHaveChildren
  55. } else {
  56. isHaveChildren := models.Common_Value_False_Value
  57. model.IsHaveChildren = &isHaveChildren
  58. }
  59. if utils.IsNotEmpty(model.RouteId) {
  60. route := new(models.ProcessRoute)
  61. route.ID = model.RouteId
  62. err = services.JdbcClient.GetJdbcModelById(route)
  63. if err != nil {
  64. utils.PrintSqlErr(err)
  65. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  66. }
  67. model.RouteName = route.Name
  68. }
  69. list[i] = model
  70. }
  71. result.Records = list
  72. return c.JSON(http.StatusOK, result)
  73. }
  74. func productBomGetChildrenList(c echo.Context) error {
  75. var paramMap map[string]interface{}
  76. if err := c.Bind(&paramMap); err != nil {
  77. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  78. }
  79. result, err := services.JdbcClient.GetJdbcList(paramMap, models.ProductBom{})
  80. if err != nil {
  81. utils.PrintSqlErr(err)
  82. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  83. }
  84. list := utils.ConvertInterface[[]models.ProductBom](result)
  85. if len(list) == 0 {
  86. return c.JSON(http.StatusOK, []string{})
  87. }
  88. for i := range list {
  89. model := list[i]
  90. material := new(models.ProductMaterial)
  91. material.Code = model.MaterialCode
  92. err = services.JdbcClient.GetJdbcModel(material)
  93. if err != nil {
  94. utils.PrintSqlErr(err)
  95. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  96. }
  97. model.Material = material
  98. bomParam := new(models.ProductBom)
  99. bomParam.ParentId = model.ID
  100. count, err := services.JdbcClient.GetJdbcCount(bomParam)
  101. if err != nil {
  102. utils.PrintSqlErr(err)
  103. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  104. }
  105. if count > 0 {
  106. isHaveChildren := models.Common_Value_True_Value
  107. model.IsHaveChildren = &isHaveChildren
  108. } else {
  109. isHaveChildren := models.Common_Value_False_Value
  110. model.IsHaveChildren = &isHaveChildren
  111. }
  112. if utils.IsNotEmpty(model.RouteId) {
  113. route := new(models.ProcessRoute)
  114. route.ID = model.RouteId
  115. err = services.JdbcClient.GetJdbcModelById(route)
  116. if err != nil {
  117. utils.PrintSqlErr(err)
  118. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  119. }
  120. model.RouteName = route.Name
  121. }
  122. list[i] = model
  123. }
  124. return c.JSON(http.StatusOK, list)
  125. }
  126. func productBomSave(c echo.Context) error {
  127. tx, _ := services.MYSQL_DB.Beginx()
  128. bom := new(models.ProductBom)
  129. if err := c.Bind(bom); err != nil {
  130. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  131. }
  132. if *bom.ParentId == "0" {
  133. exist_bom := new(models.ProductBom)
  134. exist_bom.MaterialCode = bom.MaterialCode
  135. exist_bom.ParentId = bom.ParentId
  136. count, err := services.JdbcClient.GetJdbcCount(exist_bom, tx)
  137. if err != nil {
  138. utils.PrintSqlErr(err)
  139. tx.Rollback()
  140. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  141. }
  142. if count > 0 {
  143. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("该物料已经添加过BOM,请勿重复添加", ""))
  144. }
  145. }
  146. status := models.Status_Enable
  147. bom.Status = &status
  148. if bom.ID == nil {
  149. sess, _ := session.Get("auth_session", c)
  150. err_msg := productBom_generateCode(bom, tx)
  151. if err_msg != "" {
  152. tx.Rollback()
  153. return c.JSON(http.StatusBadRequest, utils.ErrorResponse(err_msg, ""))
  154. }
  155. user_id, ok := sess.Values["user_id"].(int64)
  156. if !ok {
  157. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  158. }
  159. bom.CreateId = &user_id
  160. err := services.JdbcClient.JdbcInsert(bom, tx)
  161. if err != nil {
  162. utils.PrintSqlErr(err)
  163. tx.Rollback()
  164. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  165. }
  166. } else {
  167. err := services.JdbcClient.JdbcUpdateById(bom, tx)
  168. if err != nil {
  169. utils.PrintSqlErr(err)
  170. tx.Rollback()
  171. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  172. }
  173. }
  174. if bom.ChildrenList == nil {
  175. tx.Commit()
  176. return c.JSON(http.StatusOK, bom)
  177. }
  178. detail_list := *bom.ChildrenList
  179. for _, detail := range detail_list {
  180. detail.ParentId = bom.ID
  181. detail.BomCode = bom.BomCode
  182. err := services.JdbcClient.JdbcInsert(&detail, tx)
  183. if err != nil {
  184. utils.PrintSqlErr(err)
  185. tx.Rollback()
  186. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  187. }
  188. }
  189. tx.Commit()
  190. return c.JSON(http.StatusOK, bom)
  191. }
  192. func productBomUpdate(c echo.Context) error {
  193. tx, _ := services.MYSQL_DB.Beginx()
  194. bom := new(models.ProductBom)
  195. if err := c.Bind(bom); err != nil {
  196. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  197. }
  198. sess, _ := session.Get("auth_session", c)
  199. user_id, ok := sess.Values["user_id"].(int64)
  200. if !ok {
  201. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  202. }
  203. bom.UpdateId = &user_id
  204. err := services.JdbcClient.JdbcUpdateById(bom, tx)
  205. if err != nil {
  206. utils.PrintSqlErr(err)
  207. tx.Rollback()
  208. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  209. }
  210. bomParam := new(models.ProductBom)
  211. bomParam.ParentId = bom.ID
  212. result, err := services.JdbcClient.GetJdbcListByObject(bomParam)
  213. if err != nil {
  214. utils.PrintSqlErr(err)
  215. tx.Rollback()
  216. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  217. }
  218. exist_list := utils.ConvertInterface[[]models.ProductBom](result)
  219. remove_id_list := utils.Map(exist_list, func(product_bom models.ProductBom) string {
  220. return *product_bom.ID
  221. })
  222. if bom.ChildrenList == nil {
  223. tx.Commit()
  224. return c.JSON(http.StatusOK, bom)
  225. }
  226. detail_list := *bom.ChildrenList
  227. insert_list := utils.Filter(detail_list, func(item models.ProductBom) bool {
  228. return item.ID == nil || len(*item.ID) == 0
  229. })
  230. update_list := utils.Filter(detail_list, func(item models.ProductBom) bool {
  231. return item.ID != nil && len(*item.ID) > 0
  232. })
  233. update_id_list := utils.Map(update_list, func(product_bom models.ProductBom) string {
  234. return *product_bom.ID
  235. })
  236. remove_id_list = utils.SliceSubtract(remove_id_list, update_id_list)
  237. for _, insert_model := range insert_list {
  238. insert_model.ParentId = bom.ID
  239. insert_model.BomCode = bom.BomCode
  240. err := services.JdbcClient.JdbcInsert(&insert_model, tx)
  241. if err != nil {
  242. utils.PrintSqlErr(err)
  243. tx.Rollback()
  244. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  245. }
  246. }
  247. for _, update_model := range update_list {
  248. err := services.JdbcClient.JdbcUpdateById(&update_model, tx)
  249. if err != nil {
  250. utils.PrintSqlErr(err)
  251. tx.Rollback()
  252. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  253. }
  254. }
  255. for _, remove_id := range remove_id_list {
  256. remove_bom := new(models.ProductBom)
  257. remove_bom.ID = &remove_id
  258. err := services.JdbcClient.JdbcRemoveById(remove_bom, tx)
  259. if err != nil {
  260. utils.PrintSqlErr(err)
  261. tx.Rollback()
  262. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  263. }
  264. }
  265. tx.Commit()
  266. return c.JSON(http.StatusOK, bom)
  267. }
  268. func productBomRemove(c echo.Context) error {
  269. bom := new(models.ProductBom)
  270. if err := c.Bind(bom); err != nil {
  271. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  272. }
  273. err := services.JdbcClient.JdbcRemoveById(bom)
  274. if err != nil {
  275. utils.PrintSqlErr(err)
  276. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  277. }
  278. return c.JSON(http.StatusOK, bom)
  279. }
  280. func productBom_generateCode(model *models.ProductBom, tx *sqlx.Tx) string {
  281. sync := new(models.Synchronized)
  282. sync.Sync.Lock()
  283. defer sync.Sync.Unlock()
  284. if model.BomCode != nil && len(*model.BomCode) > 0 {
  285. modelParam := new(models.ProductBom)
  286. modelParam.BomCode = model.BomCode
  287. modelParam.TenantId = model.TenantId
  288. count, err := services.JdbcClient.GetJdbcCount(modelParam, tx)
  289. if err != nil {
  290. utils.PrintSqlErr(err)
  291. return "SQL执行失败"
  292. }
  293. if count > 0 {
  294. return "编号已存在,请重新填写"
  295. }
  296. } else {
  297. for {
  298. code, err := services.GetFlowNo(models.Flow_No_Type_Product_Bom, *model.TenantId, tx)
  299. if err != nil {
  300. return "自动生成编码失败"
  301. }
  302. modelParam := new(models.ProductBom)
  303. modelParam.BomCode = &code
  304. modelParam.TenantId = model.TenantId
  305. count, err := services.JdbcClient.GetJdbcCount(modelParam, tx)
  306. if err != nil {
  307. utils.PrintSqlErr(err)
  308. return "SQL执行失败"
  309. }
  310. if count == 0 {
  311. model.BomCode = &code
  312. break
  313. }
  314. }
  315. }
  316. return ""
  317. }