surplus.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. appG.Response(http.StatusOK, e.SUCCESS, true)
  48. return
  49. }
  50. _, err := tx.Table("surplus").Insert(s)
  51. if err != nil {
  52. log.Println("AddSurplus-error-2: ", err)
  53. tx.Rollback()
  54. appG.Response(http.StatusOK, e.ERROR, err)
  55. return
  56. }
  57. s1 := new(surplus)
  58. err = tx.Table("surplus").Where(" pastureId = ? ", s.PastureId).Where(" surplus = ? ", s.Surplus).GetFirst(s1).Error
  59. if err != nil {
  60. log.Println("AddSurplus-error-3: ", err)
  61. tx.Rollback()
  62. appG.Response(http.StatusOK, e.ERROR, err)
  63. return
  64. }
  65. ids, err := setting.SnowIds.NextId()
  66. if err != nil {
  67. ids = time.Now().UnixNano()
  68. logging.Info("create SnowIds err", err)
  69. }
  70. _, err = tx.Exec(`insert into feed(id,pastureid,feedcode,fname,fclass,fclassid,backup3,is_surplus)values(?,?,?,?,?,
  71. (select id from feedclass where pastureid = ? and fcname = ? ),?,1)`,
  72. ids, s.PastureId, s.Surplus+"_剩料", s.Surplus+"_剩料", "剩料", s.PastureId, "剩料", s1.Id)
  73. if err != nil {
  74. log.Println("AddSurplus-error-4: ", err)
  75. tx.Rollback()
  76. appG.Response(http.StatusOK, e.ERROR, err)
  77. return
  78. }
  79. err = tx.Commit()
  80. if err != nil {
  81. log.Println("AddSurplus-error-7: ", err)
  82. appG.Response(http.StatusOK, e.ERROR, err)
  83. tx.Rollback()
  84. return
  85. }
  86. appG.Response(http.StatusOK, e.SUCCESS, true)
  87. }
  88. func DelSurplus(c *gin.Context) {
  89. appG := app.Gin{C: c}
  90. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  91. fsions := gofasion.NewFasion(string(dataByte))
  92. id := fsions.Get("id").ValueStr()
  93. tx := restful.Engine.NewSession()
  94. defer tx.Close()
  95. tx.Begin()
  96. _, err := tx.Exec(` delete from surplus where id = ? `, id)
  97. if err != nil {
  98. log.Println("DelSurplus-error-1: ", err)
  99. tx.Rollback()
  100. appG.Response(http.StatusOK, e.ERROR, err)
  101. return
  102. }
  103. _, err = tx.Exec(` delete from feed where backup3 = ? `, 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.Commit()
  111. if err != nil {
  112. log.Println("DelSurplus-error-2: ", err)
  113. appG.Response(http.StatusOK, e.ERROR, err)
  114. tx.Rollback()
  115. return
  116. }
  117. appG.Response(http.StatusOK, e.SUCCESS, true)
  118. }
  119. func GetelSurplus(c *gin.Context) {
  120. appG := app.Gin{C: c}
  121. pastureId := c.Query("pastureId")
  122. tx := restful.Engine.NewSession()
  123. defer tx.Close()
  124. surplusList := make([]*surplus, 0)
  125. err := tx.Table("surplus").Where("pastureId = ? ", pastureId).Find(&surplusList)
  126. if err != nil {
  127. log.Println("GetelSurplus-error-1: ", err)
  128. appG.Response(http.StatusOK, e.ERROR, err)
  129. return
  130. }
  131. appG.Response(http.StatusOK, e.SUCCESS, surplusList)
  132. }