surplus.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package api
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "time"
  7. "tmr-watch/conf/setting"
  8. "tmr-watch/http/handle/restful"
  9. "tmr-watch/models"
  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.Surplus != "日粮" {
  33. feed := new(models.Feed)
  34. err := tx.Table("feed").Where(" pastureId = ? ", s.PastureId).Where(" id = ? ", s.FeedId).GetFirst(feed).Error
  35. if err != nil {
  36. log.Println("AddSurplus-error-3: ", err)
  37. tx.Rollback()
  38. appG.Response(http.StatusOK, e.ERROR, err)
  39. return
  40. }
  41. ids, err := setting.SnowIds.NextId()
  42. if err != nil {
  43. ids = time.Now().UnixNano()
  44. logging.Info("create SnowIds err", err)
  45. }
  46. _, err = tx.Exec(`insert into feed(id,pastureid,feedcode,fname,fclass,fclassid,is_surplus)values(?,?,?,?,?,
  47. (select id from feedclass where pastureid = ? and fcname = ? ),1)`,
  48. ids, s.PastureId, feed.FName+"_剩料", feed.FName+"_剩料", "剩料", s.PastureId, "剩料")
  49. if err != nil {
  50. log.Println("AddSurplus-error-4: ", err)
  51. tx.Rollback()
  52. appG.Response(http.StatusOK, e.ERROR, err)
  53. return
  54. }
  55. _, err = tx.Exec(` insert into surplus(pastureId,surplus,feedId,alternativefeed)values(?,(select fname from feed where id = ? ),?,?)`, s.PastureId, ids, ids, s.FeedId)
  56. // _, err := tx.Table("surplus").Insert(s)
  57. if err != nil {
  58. log.Println("AddSurplus-error-2: ", err)
  59. tx.Rollback()
  60. appG.Response(http.StatusOK, e.ERROR, "该剩料已存在!")
  61. return
  62. }
  63. } else {
  64. ids, err := setting.SnowIds.NextId()
  65. if err != nil {
  66. ids = time.Now().UnixNano()
  67. logging.Info("create SnowIds err", err)
  68. }
  69. _, err = tx.Exec(`insert into feed(id,pastureid,feedcode,fname,fclass,fclassid,is_surplus)values(?,?,?,?,?,
  70. (select id from feedclass where pastureid = ? and fcname = ? ),1)`,
  71. ids, s.PastureId, "日粮"+"_剩料", "日粮"+"_剩料", "剩料", s.PastureId, "剩料")
  72. if err != nil {
  73. log.Println("AddSurplus-error-4: ", err)
  74. tx.Rollback()
  75. appG.Response(http.StatusOK, e.ERROR, err)
  76. return
  77. }
  78. _, err = tx.Exec(` insert into surplus(pastureId,surplus,feedId)values(?,(select fname from feed where id = ? ),?)`, s.PastureId, ids, ids)
  79. if err != nil {
  80. log.Println("AddSurplus-error-2: ", err)
  81. tx.Rollback()
  82. appG.Response(http.StatusOK, e.ERROR, "该剩料已存在!")
  83. return
  84. }
  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 feed where id = (select feedId from surplus where id = ? ) `, id)
  104. if err != nil {
  105. log.Println("DelSurplus-error-2: ", err)
  106. tx.Rollback()
  107. appG.Response(http.StatusOK, e.ERROR, err)
  108. return
  109. }
  110. _, err = tx.Exec(` delete from surplus where id = ? `, id)
  111. if err != nil {
  112. log.Println("DelSurplus-error-1: ", 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. // type surplus1 struct {
  132. // Id string `xorm:"id" json:"id"`
  133. // PastureId string `xorm:"pastureId" json:"pastureId"`
  134. // Surplus string `xorm:"surplus" json:"surplus"`
  135. // FeedId string `xorm:"feedId" json:"feedId"`
  136. // }
  137. surplusList := make([]*surplus, 0)
  138. err := tx.Table("surplus").Where("pastureId = ? ", pastureId).Find(&surplusList)
  139. if err != nil {
  140. log.Println("GetelSurplus-error-1: ", err)
  141. appG.Response(http.StatusOK, e.ERROR, err)
  142. return
  143. }
  144. appG.Response(http.StatusOK, e.SUCCESS, surplusList)
  145. }