surplus.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }
  21. func AddSurplus(c *gin.Context) {
  22. appG := app.Gin{C: c}
  23. s := new(surplus)
  24. if err := c.ShouldBind(&s); err != nil {
  25. appG.Response(500, e.ERROR, "数据格式不正确!")
  26. return
  27. }
  28. tx := restful.Engine.NewSession()
  29. defer tx.Close()
  30. tx.Begin()
  31. if s.Id > 0 {
  32. _, err := tx.Exec(` update surplus set surplus = ? where id = ? `, s.Surplus, s.Id)
  33. if err != nil {
  34. log.Println("AddSurplus-error-5: ", err)
  35. tx.Rollback()
  36. appG.Response(http.StatusOK, e.ERROR, err)
  37. return
  38. }
  39. _, err = tx.Exec(` update feed set feedcode = ?, fname = ? where backup3 = ? `, s.Surplus+"_剩料", s.Surplus+"_剩料", fmt.Sprintf("%d", s.Id))
  40. if err != nil {
  41. log.Println("AddSurplus-error-6: ", err)
  42. tx.Rollback()
  43. appG.Response(http.StatusOK, e.ERROR, err)
  44. return
  45. }
  46. appG.Response(http.StatusOK, e.SUCCESS, true)
  47. return
  48. }
  49. _, err := tx.Table("surplus").Insert(s)
  50. if err != nil {
  51. log.Println("AddSurplus-error-2: ", err)
  52. tx.Rollback()
  53. appG.Response(http.StatusOK, e.ERROR, err)
  54. return
  55. }
  56. s1 := new(surplus)
  57. err = tx.Table("surplus").Where(" pastureId = ? ", s.PastureId).Where(" surplus = ? ", s.Surplus).GetFirst(s1).Error
  58. if err != nil {
  59. log.Println("AddSurplus-error-3: ", err)
  60. tx.Rollback()
  61. appG.Response(http.StatusOK, e.ERROR, err)
  62. return
  63. }
  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,backup3)values(?,?,?,?,?,(select id from feedclass where pastureid = ? and fcname = ? ),?)`,
  70. ids, s.PastureId, s.Surplus+"_剩料", s.Surplus+"_剩料", "剩料", s.PastureId, "剩料", s1.Id)
  71. if err != nil {
  72. log.Println("AddSurplus-error-4: ", err)
  73. tx.Rollback()
  74. appG.Response(http.StatusOK, e.ERROR, err)
  75. return
  76. }
  77. err = tx.Commit()
  78. if err != nil {
  79. log.Println("AddSurplus-error-7: ", err)
  80. appG.Response(http.StatusOK, e.ERROR, err)
  81. tx.Rollback()
  82. return
  83. }
  84. appG.Response(http.StatusOK, e.SUCCESS, true)
  85. }
  86. func DelSurplus(c *gin.Context) {
  87. appG := app.Gin{C: c}
  88. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  89. fsions := gofasion.NewFasion(string(dataByte))
  90. id := fsions.Get("id").ValueStr()
  91. tx := restful.Engine.NewSession()
  92. defer tx.Close()
  93. tx.Begin()
  94. _, err := tx.Exec(` delete from surplus where id = ? `, id)
  95. if err != nil {
  96. log.Println("DelSurplus-error-1: ", err)
  97. tx.Rollback()
  98. appG.Response(http.StatusOK, e.ERROR, err)
  99. return
  100. }
  101. _, err = tx.Exec(` delete from feed where backup3 = ? `, id)
  102. if err != nil {
  103. log.Println("DelSurplus-error-2: ", err)
  104. tx.Rollback()
  105. appG.Response(http.StatusOK, e.ERROR, err)
  106. return
  107. }
  108. err = tx.Commit()
  109. if err != nil {
  110. log.Println("DelSurplus-error-2: ", err)
  111. appG.Response(http.StatusOK, e.ERROR, err)
  112. tx.Rollback()
  113. return
  114. }
  115. appG.Response(http.StatusOK, e.SUCCESS, true)
  116. }