oursourcing_plan.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/jmoiron/sqlx"
  9. "github.com/labstack/echo-contrib/session"
  10. "github.com/labstack/echo/v4"
  11. )
  12. func Add_outsourcing_plan_to_routes(e *echo.Echo) {
  13. group := e.Group("/outsourcingPlan")
  14. group.Use(middleware.AuthMiddleware)
  15. group.POST("/getPage", outsourcingPlanGetPage)
  16. group.POST("/getList", outsourcingPlanGetList)
  17. group.POST("/save", outsourcingPlanSave)
  18. group.POST("/update", outsourcingPlanUpdate)
  19. group.POST("/remove", outsourcingPlanRemove)
  20. }
  21. func outsourcingPlanGetPage(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.OutsourcingPlan{})
  27. if err != nil {
  28. utils.PrintSqlErr(err)
  29. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  30. }
  31. list := utils.ConvertInterface[[]models.OutsourcingPlan](result.Records)
  32. if len(list) == 0 {
  33. list = []models.OutsourcingPlan{}
  34. }
  35. for i := range list {
  36. model := list[i]
  37. minioList, err := services.JdbcClient.GetMinioFile(model)
  38. if err != nil {
  39. utils.PrintSearchFileErr(err)
  40. }
  41. model.FileList = &minioList
  42. sale_order := new(models.SaleOrder)
  43. sale_order.ID = model.SaleOrderId
  44. err = services.JdbcClient.GetJdbcModelById(sale_order)
  45. if err != nil {
  46. utils.PrintSqlErr(err)
  47. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  48. }
  49. model.SaleOrder = sale_order
  50. detailParam := new(models.OutsourcingPlanDetail)
  51. detailParam.PlanId = model.ID
  52. result, err := services.JdbcClient.GetJdbcListByObject(detailParam)
  53. if err != nil {
  54. utils.PrintSqlErr(err)
  55. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  56. }
  57. detail_list := utils.ConvertInterface[[]models.OutsourcingPlanDetail](result)
  58. for j := range detail_list {
  59. detail := detail_list[j]
  60. material := new(models.ProductMaterial)
  61. material.Code = detail.MaterialCode
  62. err = services.JdbcClient.GetJdbcModel(material)
  63. if err != nil {
  64. utils.PrintSqlErr(err)
  65. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  66. }
  67. detail.ProductMaterial = material
  68. detail_list[j] = detail
  69. }
  70. model.ChildrenList = &detail_list
  71. list[i] = model
  72. }
  73. result.Records = list
  74. return c.JSON(http.StatusOK, result)
  75. }
  76. func outsourcingPlanGetList(c echo.Context) error {
  77. var paramMap map[string]interface{}
  78. if err := c.Bind(&paramMap); err != nil {
  79. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  80. }
  81. result, err := services.JdbcClient.GetJdbcList(paramMap, models.OutsourcingPlan{})
  82. if err != nil {
  83. utils.PrintSqlErr(err)
  84. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", err.Error()))
  85. }
  86. list := utils.ConvertInterface[[]models.OutsourcingPlan](result)
  87. if len(list) == 0 {
  88. return c.JSON(http.StatusOK, []string{})
  89. }
  90. return c.JSON(http.StatusOK, list)
  91. }
  92. func outsourcingPlanSave(c echo.Context) error {
  93. tx, _ := services.MYSQL_DB.Beginx()
  94. sess, _ := session.Get("auth_session", c)
  95. user_id, ok := sess.Values["user_id"].(int64)
  96. if !ok {
  97. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  98. }
  99. plan := new(models.OutsourcingPlan)
  100. if err := c.Bind(plan); err != nil {
  101. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  102. }
  103. err_msg := outsourcingPlan_generateCode(plan, tx)
  104. if err_msg != "" {
  105. tx.Rollback()
  106. return c.JSON(http.StatusBadRequest, utils.ErrorResponse(err_msg, ""))
  107. }
  108. status := models.Outsourcing_Plan_Status_Pending
  109. plan.Status = &status
  110. plan.CreateId = &user_id
  111. services.JdbcClient.JdbcInsert(plan, tx)
  112. detail_list := *plan.ChildrenList
  113. for _, detail := range detail_list {
  114. detail.PlanId = plan.ID
  115. err := services.JdbcClient.JdbcInsert(&detail, tx)
  116. if err != nil {
  117. utils.PrintSqlErr(err)
  118. tx.Rollback()
  119. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  120. }
  121. }
  122. if plan.FileList == nil {
  123. tx.Commit()
  124. return c.JSON(http.StatusOK, plan)
  125. }
  126. fileList := *plan.FileList
  127. for _, file := range fileList {
  128. file.RefId = plan.ID
  129. refType := models.File_Ref_Type_Outsourcing_Plan
  130. file.RefType = &refType
  131. err := services.JdbcClient.JdbcInsert(&file, tx)
  132. if err != nil {
  133. utils.PrintSqlErr(err)
  134. tx.Rollback()
  135. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  136. }
  137. }
  138. tx.Commit()
  139. return c.JSON(http.StatusOK, plan)
  140. }
  141. func outsourcingPlanUpdate(c echo.Context) error {
  142. tx, _ := services.MYSQL_DB.Beginx()
  143. plan := new(models.OutsourcingPlan)
  144. if err := c.Bind(plan); err != nil {
  145. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  146. }
  147. sess, _ := session.Get("auth_session", c)
  148. user_id, ok := sess.Values["user_id"].(int64)
  149. if !ok {
  150. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("用户请先登录", ""))
  151. }
  152. plan.UpdateId = &user_id
  153. err := services.JdbcClient.JdbcUpdateById(plan, tx)
  154. if err != nil {
  155. utils.PrintSqlErr(err)
  156. tx.Rollback()
  157. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  158. }
  159. detail := new(models.OutsourcingPlanDetail)
  160. detail.PlanId = plan.ID
  161. err = services.JdbcClient.JdbcRemove(detail, tx)
  162. if err != nil {
  163. utils.PrintSqlErr(err)
  164. tx.Rollback()
  165. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  166. }
  167. detail_list := *plan.ChildrenList
  168. for _, detail := range detail_list {
  169. detail.PlanId = plan.ID
  170. err := services.JdbcClient.JdbcInsert(&detail, tx)
  171. if err != nil {
  172. utils.PrintSqlErr(err)
  173. tx.Rollback()
  174. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  175. }
  176. }
  177. if plan.FileList == nil {
  178. tx.Commit()
  179. return c.JSON(http.StatusOK, plan)
  180. }
  181. fileList := *plan.FileList
  182. for _, file := range fileList {
  183. file.RefId = plan.ID
  184. refType := models.File_Ref_Type_Outsourcing_Plan
  185. file.RefType = &refType
  186. err := services.JdbcClient.JdbcInsert(&file, tx)
  187. if err != nil {
  188. utils.PrintSqlErr(err)
  189. tx.Rollback()
  190. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  191. }
  192. }
  193. tx.Commit()
  194. return c.JSON(http.StatusOK, plan)
  195. }
  196. func outsourcingPlanRemove(c echo.Context) error {
  197. tx, _ := services.MYSQL_DB.Beginx()
  198. plan := new(models.OutsourcingPlan)
  199. if err := c.Bind(plan); err != nil {
  200. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("参数解析失败", err.Error()))
  201. }
  202. err := services.JdbcClient.JdbcRemoveById(plan, tx)
  203. if err != nil {
  204. utils.PrintSqlErr(err)
  205. tx.Rollback()
  206. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  207. }
  208. minio_file := new(models.MinioFile)
  209. minio_file.RefId = plan.ID
  210. refType := models.File_Ref_Type_Outsourcing_Plan
  211. minio_file.RefType = &refType
  212. err = services.JdbcClient.JdbcRemove(minio_file, tx)
  213. if err != nil {
  214. utils.PrintSqlErr(err)
  215. tx.Rollback()
  216. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  217. }
  218. detail := new(models.OutsourcingPlanDetail)
  219. detail.PlanId = plan.ID
  220. err = services.JdbcClient.JdbcRemove(detail, tx)
  221. if err != nil {
  222. utils.PrintSqlErr(err)
  223. tx.Rollback()
  224. return c.JSON(http.StatusInternalServerError, utils.ErrorResponse("系统错误", ""))
  225. }
  226. tx.Commit()
  227. return c.JSON(http.StatusOK, plan)
  228. }
  229. func outsourcingPlan_generateCode(model *models.OutsourcingPlan, tx *sqlx.Tx) string {
  230. sync := new(models.Synchronized)
  231. sync.Sync.Lock()
  232. defer sync.Sync.Unlock()
  233. if model.Code != nil && len(*model.Code) > 0 {
  234. modelParam := new(models.OutsourcingPlan)
  235. modelParam.Code = model.Code
  236. modelParam.TenantId = model.TenantId
  237. count, err := services.JdbcClient.GetJdbcCount(modelParam, tx)
  238. if err != nil {
  239. utils.PrintSqlErr(err)
  240. return "SQL执行失败"
  241. }
  242. if count > 0 {
  243. return "编号已存在,请重新填写"
  244. }
  245. } else {
  246. for {
  247. code, err := services.GetFlowNo(models.Flow_No_Type_Outsourcing_Plan, *model.TenantId, tx)
  248. if err != nil {
  249. return "自动生成编码失败"
  250. }
  251. modelParam := new(models.OutsourcingPlan)
  252. modelParam.Code = &code
  253. modelParam.TenantId = model.TenantId
  254. count, err := services.JdbcClient.GetJdbcCount(modelParam, tx)
  255. if err != nil {
  256. utils.PrintSqlErr(err)
  257. return "SQL执行失败"
  258. }
  259. if count == 0 {
  260. model.Code = &code
  261. break
  262. }
  263. }
  264. }
  265. return ""
  266. }