123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package api
- import (
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "time"
- "tmr-watch/conf/setting"
- "tmr-watch/http/handle/restful"
- "tmr-watch/pkg/app"
- "tmr-watch/pkg/e"
- "tmr-watch/pkg/logging"
- "github.com/Anderson-Lu/gofasion/gofasion"
- "github.com/gin-gonic/gin"
- )
- type surplus struct {
- Id int64 `xorm:"id" json:"id"`
- PastureId int64 `xorm:"pastureId" json:"pastureId"`
- Surplus string `xorm:"surplus" json:"surplus"`
- FeedId string `xorm:"feedId" json:"feedId"`
- }
- func AddSurplus(c *gin.Context) {
- appG := app.Gin{C: c}
- s := new(surplus)
- if err := c.ShouldBind(&s); err != nil {
- appG.Response(500, e.ERROR, "数据格式不正确!")
- return
- }
- tx := restful.Engine.NewSession()
- defer tx.Close()
- tx.Begin()
- if s.Id > 0 {
- _, err := tx.Exec(` update surplus set surplus = ?,feedId = ? where id = ? `, s.Surplus, s.FeedId, s.Id)
- if err != nil {
- log.Println("AddSurplus-error-5: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, "该剩料已存在!")
- return
- }
- _, err = tx.Exec(` update feed set feedcode = ?, fname = ? where backup3 = ? `, s.Surplus+"_剩料", s.Surplus+"_剩料", fmt.Sprintf("%d", s.Id))
- if err != nil {
- log.Println("AddSurplus-error-6: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- err = tx.Commit()
- if err != nil {
- log.Println("AddSurplus-error-8: ", err)
- appG.Response(http.StatusOK, e.ERROR, err)
- tx.Rollback()
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- return
- }
- _, err := tx.Table("surplus").Insert(s)
- if err != nil {
- log.Println("AddSurplus-error-2: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- s1 := new(surplus)
- err = tx.Table("surplus").Where(" pastureId = ? ", s.PastureId).Where(" surplus = ? ", s.Surplus).GetFirst(s1).Error
- if err != nil {
- log.Println("AddSurplus-error-3: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- _, err = tx.Exec(`insert into feed(id,pastureid,feedcode,fname,fclass,fclassid,backup3,is_surplus)values(?,?,?,?,?,
- (select id from feedclass where pastureid = ? and fcname = ? ),?,1)`,
- ids, s.PastureId, s.Surplus+"_剩料", s.Surplus+"_剩料", "剩料", s.PastureId, "剩料", s1.Id)
- if err != nil {
- log.Println("AddSurplus-error-4: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- err = tx.Commit()
- if err != nil {
- log.Println("AddSurplus-error-7: ", err)
- appG.Response(http.StatusOK, e.ERROR, err)
- tx.Rollback()
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- func DelSurplus(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsions := gofasion.NewFasion(string(dataByte))
- id := fsions.Get("id").ValueStr()
- tx := restful.Engine.NewSession()
- defer tx.Close()
- tx.Begin()
- _, err := tx.Exec(` delete from surplus where id = ? `, id)
- if err != nil {
- log.Println("DelSurplus-error-1: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- _, err = tx.Exec(` delete from feed where backup3 = ? `, id)
- if err != nil {
- log.Println("DelSurplus-error-2: ", err)
- tx.Rollback()
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- err = tx.Commit()
- if err != nil {
- log.Println("DelSurplus-error-2: ", err)
- appG.Response(http.StatusOK, e.ERROR, err)
- tx.Rollback()
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- func GetelSurplus(c *gin.Context) {
- appG := app.Gin{C: c}
- pastureId := c.Query("pastureId")
- tx := restful.Engine.NewSession()
- defer tx.Close()
- surplusList := make([]*surplus, 0)
- err := tx.Table("surplus").Where("pastureId = ? ", pastureId).Find(&surplusList)
- if err != nil {
- log.Println("GetelSurplus-error-1: ", err)
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, surplusList)
- }
|