|  | @@ -0,0 +1,132 @@
 | 
	
		
			
				|  |  | +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"`
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +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 = ?  where id = ? `, s.Surplus, s.Id)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			log.Println("AddSurplus-error-5: ", err)
 | 
	
		
			
				|  |  | +			tx.Rollback()
 | 
	
		
			
				|  |  | +			appG.Response(http.StatusOK, e.ERROR, err)
 | 
	
		
			
				|  |  | +			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
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		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)values(?,?,?,?,?,(select id from feedclass where pastureid = ? and fcname = ? ),?)`,
 | 
	
		
			
				|  |  | +		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)
 | 
	
		
			
				|  |  | +}
 |