surplus.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package api
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "time"
  8. "tmr-watch/conf/setting"
  9. "tmr-watch/http/handle/restful"
  10. "tmr-watch/pkg/app"
  11. "tmr-watch/pkg/e"
  12. "tmr-watch/pkg/logging"
  13. "github.com/Anderson-Lu/gofasion/gofasion"
  14. "github.com/gin-gonic/gin"
  15. )
  16. type surplus struct {
  17. Id int64 `xorm:"id" json:"id"`
  18. PastureId int64 `xorm:"pastureId" json:"pastureId"`
  19. Surplus string `xorm:"surplus" json:"surplus"`
  20. FeedId string `xorm:"feedId" json:"feedId"`
  21. }
  22. func AddSurplus(c *gin.Context) {
  23. appG := app.Gin{C: c}
  24. s := new(surplus)
  25. if err := c.ShouldBind(&s); err != nil {
  26. appG.Response(500, e.ERROR, "数据格式不正确!")
  27. return
  28. }
  29. tx := restful.Engine.NewSession()
  30. defer tx.Close()
  31. tx.Begin()
  32. if s.Id > 0 {
  33. _, err := tx.Exec(` update surplus set surplus = ?,feedId = ? where id = ? `, s.Surplus, s.FeedId, s.Id)
  34. if err != nil {
  35. log.Println("AddSurplus-error-5: ", err)
  36. tx.Rollback()
  37. appG.Response(http.StatusOK, e.ERROR, "该剩料已存在!")
  38. return
  39. }
  40. _, err = tx.Exec(` update feed set feedcode = ?, fname = ? where backup3 = ? `, s.Surplus+"_剩料", s.Surplus+"_剩料", fmt.Sprintf("%d", s.Id))
  41. if err != nil {
  42. log.Println("AddSurplus-error-6: ", err)
  43. tx.Rollback()
  44. appG.Response(http.StatusOK, e.ERROR, err)
  45. return
  46. }
  47. err = tx.Commit()
  48. if err != nil {
  49. log.Println("AddSurplus-error-8: ", err)
  50. appG.Response(http.StatusOK, e.ERROR, err)
  51. tx.Rollback()
  52. return
  53. }
  54. appG.Response(http.StatusOK, e.SUCCESS, true)
  55. return
  56. }
  57. _, err := tx.Table("surplus").Insert(s)
  58. if err != nil {
  59. log.Println("AddSurplus-error-2: ", err)
  60. tx.Rollback()
  61. appG.Response(http.StatusOK, e.ERROR, err)
  62. return
  63. }
  64. s1 := new(surplus)
  65. err = tx.Table("surplus").Where(" pastureId = ? ", s.PastureId).Where(" surplus = ? ", s.Surplus).GetFirst(s1).Error
  66. if err != nil {
  67. log.Println("AddSurplus-error-3: ", err)
  68. tx.Rollback()
  69. appG.Response(http.StatusOK, e.ERROR, err)
  70. return
  71. }
  72. ids, err := setting.SnowIds.NextId()
  73. if err != nil {
  74. ids = time.Now().UnixNano()
  75. logging.Info("create SnowIds err", err)
  76. }
  77. _, err = tx.Exec(`insert into feed(id,pastureid,feedcode,fname,fclass,fclassid,backup3,is_surplus)values(?,?,?,?,?,
  78. (select id from feedclass where pastureid = ? and fcname = ? ),?,1)`,
  79. ids, s.PastureId, s.Surplus+"_剩料", s.Surplus+"_剩料", "剩料", s.PastureId, "剩料", s1.Id)
  80. if err != nil {
  81. log.Println("AddSurplus-error-4: ", err)
  82. tx.Rollback()
  83. appG.Response(http.StatusOK, e.ERROR, err)
  84. return
  85. }
  86. err = tx.Commit()
  87. if err != nil {
  88. log.Println("AddSurplus-error-7: ", err)
  89. appG.Response(http.StatusOK, e.ERROR, err)
  90. tx.Rollback()
  91. return
  92. }
  93. appG.Response(http.StatusOK, e.SUCCESS, true)
  94. }
  95. func DelSurplus(c *gin.Context) {
  96. appG := app.Gin{C: c}
  97. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  98. fsions := gofasion.NewFasion(string(dataByte))
  99. id := fsions.Get("id").ValueStr()
  100. tx := restful.Engine.NewSession()
  101. defer tx.Close()
  102. tx.Begin()
  103. _, err := tx.Exec(` delete from surplus where id = ? `, id)
  104. if err != nil {
  105. log.Println("DelSurplus-error-1: ", err)
  106. tx.Rollback()
  107. appG.Response(http.StatusOK, e.ERROR, err)
  108. return
  109. }
  110. _, err = tx.Exec(` delete from feed where backup3 = ? `, id)
  111. if err != nil {
  112. log.Println("DelSurplus-error-2: ", err)
  113. tx.Rollback()
  114. appG.Response(http.StatusOK, e.ERROR, err)
  115. return
  116. }
  117. err = tx.Commit()
  118. if err != nil {
  119. log.Println("DelSurplus-error-2: ", err)
  120. appG.Response(http.StatusOK, e.ERROR, err)
  121. tx.Rollback()
  122. return
  123. }
  124. appG.Response(http.StatusOK, e.SUCCESS, true)
  125. }
  126. func GetelSurplus(c *gin.Context) {
  127. appG := app.Gin{C: c}
  128. pastureId := c.Query("pastureId")
  129. tx := restful.Engine.NewSession()
  130. defer tx.Close()
  131. surplusList := make([]*surplus, 0)
  132. err := tx.Table("surplus").Where("pastureId = ? ", pastureId).Find(&surplusList)
  133. if err != nil {
  134. log.Println("GetelSurplus-error-1: ", err)
  135. appG.Response(http.StatusOK, e.ERROR, err)
  136. return
  137. }
  138. appG.Response(http.StatusOK, e.SUCCESS, surplusList)
  139. }