| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 | package apiimport (	"io/ioutil"	"log"	"net/http"	"time"	"tmr-watch/conf/setting"	"tmr-watch/http/handle/restful"	"tmr-watch/models"	"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.Surplus != "日粮" {		feed := new(models.Feed)		err := tx.Table("feed").Where(" pastureId = ? ", s.PastureId).Where(" id = ? ", s.FeedId).GetFirst(feed).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,is_surplus)values(?,?,?,?,?,		(select id from feedclass where pastureid = ? and fcname = ? ),1)`,			ids, s.PastureId, feed.FName+"_剩料", feed.FName+"_剩料", "剩料", s.PastureId, "剩料")		if err != nil {			log.Println("AddSurplus-error-4: ", err)			tx.Rollback()			appG.Response(http.StatusOK, e.ERROR, err)			return		}		_, err = tx.Exec(` insert into surplus(pastureId,surplus,feedId,alternativefeed)values(?,(select fname from feed where id = ? ),?,?)`, s.PastureId, ids, ids, s.FeedId)		// _, err := tx.Table("surplus").Insert(s)		if err != nil {			log.Println("AddSurplus-error-2: ", err)			tx.Rollback()			appG.Response(http.StatusOK, e.ERROR, "该剩料已存在!")			return		}	} else {		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,is_surplus)values(?,?,?,?,?,		(select id from feedclass where pastureid = ? and fcname = ? ),1)`,			ids, s.PastureId, "日粮"+"_剩料", "日粮"+"_剩料", "剩料", s.PastureId, "剩料")		if err != nil {			log.Println("AddSurplus-error-4: ", err)			tx.Rollback()			appG.Response(http.StatusOK, e.ERROR, err)			return		}		_, err = tx.Exec(` insert into surplus(pastureId,surplus,feedId)values(?,(select fname from feed where id = ? ),?)`, s.PastureId, ids, ids)		if err != nil {			log.Println("AddSurplus-error-2: ", err)			tx.Rollback()			appG.Response(http.StatusOK, e.ERROR, "该剩料已存在!")			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 feed where id = (select feedId from surplus where id = ? )  `, id)	if err != nil {		log.Println("DelSurplus-error-2: ", err)		tx.Rollback()		appG.Response(http.StatusOK, e.ERROR, err)		return	}	_, 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.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()	// type surplus1 struct {	// 	Id        string `xorm:"id" json:"id"`	// 	PastureId string `xorm:"pastureId" json:"pastureId"`	// 	Surplus   string `xorm:"surplus" json:"surplus"`	// 	FeedId    string `xorm:"feedId" json:"feedId"`	// }	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)}
 |