baishaojie 9 ماه پیش
والد
کامیت
f7ea7b6f01
2فایلهای تغییر یافته به همراه660 افزوده شده و 1 حذف شده
  1. BIN
      __debug_bin.exe
  2. 660 1
      http/handle/yq/yq.go

BIN
__debug_bin.exe


+ 660 - 1
http/handle/yq/yq.go

@@ -7,12 +7,16 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"strconv"
 	"strings"
 	"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/astaxie/beego/logs"
 	"github.com/robfig/cron"
 	"github.com/xormplus/xorm"
@@ -571,7 +575,6 @@ func syncBar(token, pastureid string) {
 	defer resp.Body.Close()
 
 	body, _ := ioutil.ReadAll(resp.Body)
-	// fmt.Println(string(body)
 	data := make(map[string]interface{}, 0)
 	json.Unmarshal(body, &data)
 	dataList := data["data"].([]interface{})
@@ -585,6 +588,662 @@ func syncBar(token, pastureid string) {
 		_, err := tx.Exec(` insert into bar(pen,bname,bcode,pastureid)values(?,?,?,?)  ON DUPLICATE KEY UPDATE  pen = ? `, arg["districtName"],
 			fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"]), fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"]), pastureid, arg["houseName"])
 		fmt.Println(err)
+
+		insertSql := `insert into feedp(pastureid,barname,barid,ccount)VALUES(?,?,(select id from bar where bname = ?),?)
+				ON DUPLICATE KEY UPDATE ccount = ? ,barname = ?  `
+		tx.SQL(insertSql, pastureid, fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"]),
+			fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"]), arg["sheepCount"], arg["sheepCount"],
+			fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"])).Execute()
+
+		tx.SQL(` select  from feedp where barname = ? `)
+
+		// barList := make([]*bar,0)
+		b := new(bar)
+		// b.BarName = fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"])
+		// b.Ccount = arg["sheepCount"]
+		err = tx.SQL(`select fp.barname,fp.ccount,ratio,ft.tname ftname,(select tratio from fpdetail  where barid = fp.barid  and times = 1 ) times1,
+(select tratio from fpdetail  where barid = fp.barid  and times = 2 ) times2 ,
+(select tratio from fpdetail  where barid = fp.barid  and times = 3 ) times3 ,
+(select tratio from fpdetail  where barid = fp.barid  and times = 4 ) times4 ,
+(select tratio from fpdetail  where barid = fp.barid  and times =5 ) times5  ,
+(select tratio from fpdetail  where barid = fp.barid  and times =6 ) times6  
+ from feedp fp  join feedtemplet ft on ft.id = fp.ftid  where ratio > 0  and fp.barname = ? `, fmt.Sprintf("%v-%v", arg["houseName"], arg["compartmentName"])).GetFirst(&b).Error
+
+		// {"pastureId":"1653271339","barList":[{"barname":"14","ccount":155,"ratio":"100.00","ftname":"高产2配方",
+		// "times1":"41","times2":"25","times3":"25","times4":0,"times5":0,"times6":0}]}
+
+	}
+}
+
+type bar struct {
+	BarName string `xorm:"barname" json:"barname"`
+	Ccount  string `xorm:"ccount" json:"ccount"`
+	Ratio   string `xorm:"ratio" json:"ratio"`
+	Ftname  string `xorm:"ftname" json:"ftname"`
+	Times1  int64  `xorm:"times1" json:"times1"`
+	Times2  int64  `xorm:"times2" json:"times2"`
+	Times3  int64  `xorm:"times3" json:"times3"`
+	Times4  int64  `xorm:"times4" json:"times4"`
+	Times5  int64  `xorm:"times5" json:"times5"`
+	Times6  int64  `xorm:"times6" json:"times6"`
+}
+
+func fpdBatchEdit(pastureid string ,b *bar) {
+
+	// appG := app.Gin{C: c}
+	// dataByte, _ := ioutil.ReadAll(c.Request.Body)
+	// fsions := gofasion.NewFasion(string(dataByte))
+	// barList := fsions.Get("barList").Array()
+	// pastureid := fsions.Get("pastureId").ValueStr()
+
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+
+	oldFpdList, err := tx.SQL(` select  ifnull(ROUND((1-ptsrate)*weight,2)-ptuse,"") fweight,fpd.id,fpd.barname,fpd.weight,fpd.times,fp.ftweight,fp.supplyweight  from fpdetail fpd
+	join feedp fp on fp.barid = fpd.barid and fpd.pastureid = fp.pastureid where fpd.pastureid = ?`, pastureid).Query().List()
+	if err != nil {
+		// appG.Response(http.StatusInternalServerError, e.ERROR, false)
+		return
+	}
+
+	// for _, barFsions := range barList {
+
+		dataexcel := make(map[string]interface{}, 0)
+		dataexcel["barname"] =b.BarName
+		dataexcel["ccount"] = b.Ccount
+		dataexcel["ratio"] = b.Ratio
+		dataexcel["ftname"] = b.Ftname
+		// dataexcel["ptsfname"] = barFsions.Get("ptsfname").ValueStr()
+		if dataexcel["ftname"] == "" || dataexcel["ccount"] == "0" || dataexcel["ratio"] == "0" {
+			return
+		}
+
+		dataexcel["1"] = b.Times1
+		dataexcel["2"] = b.Times2
+		dataexcel["3"] = b.Times3
+		dataexcel["4"] = b.Times4
+		dataexcel["5"] = b.Times5
+		dataexcel["6"] = b.Times6
+
+		ftexist, err := tx.SQL(` select id from  feedp where barname = ?  and pastureid = ? and ftname = ?  `, dataexcel["barname"], pastureid, dataexcel["ftname"]).Exist()
+		if err != nil {
+			logs.Error(err)
+	
+			return
+		}
+		if !ftexist {
+			fmt.Println(dataexcel["barname"], pastureid, dataexcel["ftname"])
+			tx.SQL(` delete from lpplandtl1 where barname = ? and pastureid = ?  `, dataexcel["barname"], pastureid).Exist()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+			tx.SQL(`  update fpdetail set ptuse = 0  where  barname = ? and pastureid = ?   `, dataexcel["barname"], pastureid).Exist()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+
+		exist, err := tx.SQL(` select id from  feedp where barname = ?  and pastureid = ?  `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		if !exist {
+			_, err := tx.SQL(` insert into  feedp(pastureid,barid,barname,ccount)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,?) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"], 0).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+
+		var feedpargs []interface{}
+		upFeedpSql := `update  feedp set ccount = ? ,ratio = ? ,ccountratio = ?*(?*0.01)  `
+		feedpargs = append(feedpargs, dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"])
+		if _, ok := dataexcel["ftname"]; ok {
+			feedpargs = append(feedpargs, dataexcel["ftname"], pastureid, dataexcel["ftname"])
+			upFeedpSql += ` , ftid = (select id from feedtemplet where tname = ? and pastureid = ? ),ftname = ? `
+
+			upFeedpSql += `,ftweight = (select sum(fweight) fweight from ftdetail 
+	where  ftid = (select  id from feedtemplet  where tname = ? and pastureid = ?  )) *  (?*(?*0.01)) `
+			feedpargs = append(feedpargs, dataexcel["ftname"], pastureid, dataexcel["ccount"], dataexcel["ratio"])
+		} else {
+			upFeedpSql += ` , ftid = -1,ftname = '' ,ftweight = 0 `
+		}
+
+		if _, ok := dataexcel["ptsfname"]; ok {
+			feedpargs = append(feedpargs, dataexcel["ptsfname"], pastureid, dataexcel["ptsfname"])
+			upFeedpSql += ` , ptsfid = (select id from feedtemplet where tname = ? and pastureid = ? ),ptsfname = ? `
+
+			upFeedpSql += `,supplyweight = (select sum(fweight) fweight from ftdetail 
+	where  ftid = (select  id from feedtemplet  where tname = ? and pastureid = ?  )) *  (?*(?*0.01))`
+			feedpargs = append(feedpargs, dataexcel["ptsfname"], pastureid, dataexcel["ccount"], dataexcel["ratio"])
+		} else {
+			upFeedpSql += ` , ptsfid = -1,ptsfname = '',supplyweight = 0  `
+		}
+		upFeedpSql += " where barname = ?  and pastureid = ?  "
+		feedpargs = append(feedpargs, dataexcel["barname"], pastureid)
+		_, err = tx.SQL(upFeedpSql, feedpargs...).Execute()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+
+		exist1, err := tx.SQL(` select id from  fpdetail where barname = ?  and pastureid = ? and times = 1 `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+
+		if !exist1 {
+			_, err := tx.SQL(` insert into  fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,1,0,0,0,-1,-1) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+		exist2, err := tx.SQL(` select id from  fpdetail where barname = ?  and pastureid = ? and times = 2 `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		if !exist2 {
+			_, err = tx.SQL(` insert into  fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,2,0,0,0,-1,-1) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+
+		exist3, err := tx.SQL(` select id from  fpdetail where barname = ?  and pastureid = ? and times =3 `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		if !exist3 {
+			_, err = tx.SQL(` insert into  fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,3,0,0,0,-1,-1) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+		exist4, err := tx.SQL(` select id from  fpdetail where barname = ?  and pastureid = ? and times = 4 `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		if !exist4 {
+			_, err = tx.SQL(` insert into  fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,4,0,0,0,-1,-1) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+		exist5, err := tx.SQL(` select id from  fpdetail where barname = ?  and pastureid = ? and times = 5 `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		if !exist5 {
+			_, err = tx.SQL(` insert into  fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,5,0,0,0,-1,-1) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+		exist6, err := tx.SQL(` select id from  fpdetail where barname = ?  and pastureid = ? and times = 6 `, dataexcel["barname"], pastureid).Exist()
+		if err != nil {
+			logs.Error(err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		if !exist6 {
+			_, err = tx.SQL(` insert into  fpdetail(pastureid,barid,barname,times,ptuse,ptsuse,ptsrate,ptsid,ptid)values(?,(select id from bar where  bname = ? and pastureid = ? ),?,6,0,0,0,-1,-1) `,
+				pastureid, dataexcel["barname"], pastureid, dataexcel["barname"]).Execute()
+			if err != nil {
+				logs.Error(err)
+				appG.Response(http.StatusInternalServerError, e.ERROR, false)
+				return
+			}
+		}
+
+		upFpdSql := ` update fpdetail set  tratio = ? ,weight = (? *0.01) * ((select sum(fweight) fweight from ftdetail 
+	where  ftid = (select  id from feedtemplet  where tname = ? and pastureid = ?  )) *  (?*(?*0.01))) ,cowcount = ? ,
+	ccountradio= ?,ptid= (select id from feedtemplet where tname = ? and pastureid = ? )   where barname = ?
+	and pastureid = ? and times = ?`
+		var fpdargs1 []interface{}
+		fpdargs1 = append(fpdargs1, dataexcel["1"], dataexcel["1"], dataexcel["ftname"], pastureid,
+			dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
+			dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 1)
+		_, err = tx.SQL(upFpdSql, fpdargs1...).Execute()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+
+		var fpdargs2 []interface{}
+		fpdargs2 = append(fpdargs2, dataexcel["2"], dataexcel["2"], dataexcel["ftname"], pastureid,
+			dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
+			dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 2)
+		_, err = tx.SQL(upFpdSql, fpdargs2...).Execute()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		var fpdargs3 []interface{}
+		fpdargs3 = append(fpdargs3, dataexcel["3"], dataexcel["3"], dataexcel["ftname"], pastureid,
+			dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
+			dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 3)
+		_, err = tx.SQL(upFpdSql, fpdargs3...).Execute()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		var fpdargs4 []interface{}
+		fpdargs4 = append(fpdargs4, dataexcel["4"], dataexcel["4"], dataexcel["ftname"], pastureid,
+			dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
+			dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 4)
+		_, err = tx.SQL(upFpdSql, fpdargs4...).Execute()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+
+		var fpdargs5 []interface{}
+		fpdargs5 = append(fpdargs5, dataexcel["5"], dataexcel["5"], dataexcel["ftname"], pastureid,
+			dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
+			dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 5)
+		_, err = tx.SQL(upFpdSql, fpdargs5...).Execute()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+
+		var fpdargs6 []interface{}
+		fpdargs6 = append(fpdargs6, dataexcel["6"], dataexcel["6"], dataexcel["ftname"], pastureid,
+			dataexcel["ccount"], dataexcel["ratio"], dataexcel["ccount"], dataexcel["ratio"],
+			dataexcel["ftname"], pastureid, dataexcel["barname"], pastureid, 6)
+		_, err = tx.SQL(upFpdSql, fpdargs6...).Execute()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		// _, err = tx.SQL(`call updateLPPbyFPChange(?,( select id from feedp where barname = ?  and pastureid = ?))`, pastureid, dataexcel["barname"], pastureid).Execute()
+
+		newList, err := tx.SQL(` select  ifnull(ROUND((1-ptsrate)*weight,2)-ptuse,"") fweight,id,ptid,weight,times from fpdetail where barname = ? and  pastureid = ? `, dataexcel["barname"], pastureid).Query().List()
+		if err != nil {
+			appG.Response(http.StatusInternalServerError, e.ERROR, false)
+			return
+		}
+		// oldFpdList, err := tx.SQL(` select  fpd.id,fpd.barname,fpd.weight,fpd.times,fp.ftweight,fp.supplyweight  from fpdetail fpd
+		// join feedp fp on fp.barid = fpd.barid and fpd.pastureid = fp.pastureid where fpd.pastureid = ?`, pastureid).Query().List()
+		upFpdetailList := make([]*upFpdetail, 0)
+		for _, newfpd := range newList {
+			for _, oldfpd := range oldFpdList {
+				if oldfpd["id"].(int64) == newfpd["id"].(int64) {
+					var oldweight, newweight float64
+					if oldfpd["weight"] != nil {
+						oldweight, _ = strconv.ParseFloat(oldfpd["weight"].(string), 64)
+					}
+					if newfpd["weight"] != nil {
+						newweight, _ = strconv.ParseFloat(newfpd["weight"].(string), 64)
+					}
+					fweight, _ := strconv.ParseFloat(newfpd["fweight"].(string), 64)
+					upFpdetailList = append(upFpdetailList, &upFpdetail{
+						Old:     oldweight,
+						New:     newweight,
+						Times:   newfpd["times"].(int64),
+						Ptid:    newfpd["ptid"].(int64),
+						Fweight: fweight,
+						Id:      oldfpd["id"].(int64),
+					})
+					break
+				}
+			}
+		}
+
+		updateFpdetailByBar(pastureid, dataexcel["barname"].(string), upFpdetailList)
+	// }
+	appG.Response(http.StatusOK, e.SUCCESS, "ok")
+}
+
+type lpplandtlInfo struct {
+	Id          int64   `xorm:"id"`
+	Pastureid   int64   `xorm:"pastureid"`
+	Lppid       int64   `xorm:"lppid"`
+	Barid       int64   `xorm:"barid"`
+	Barname     string  `xorm:"barname"`
+	Fpdid       int64   `xorm:"fpdid"`
+	Fttype      int64   `xorm:"fttype"`
+	Lweight     float64 `xorm:"lweight"`
+	Sort        int64   `xorm:"sort"`
+	Tmrid       int64   `xorm:"tmrid"`
+	Tmrname     string  `xorm:"tmrname"`
+	Cowcount    int64   `xorm:"cowcount"`
+	Ccountradio float64 `xorm:"ccountradio"`
+	Background  string  `xorm:"background"`
+	Lweighthis  float64 `xorm:"lweighthis"`
+	Maxweight   float64 `xorm:"maxweight"`
+}
+
+type upFpdetail struct {
+	Old     float64
+	New     float64
+	Times   int64
+	Ptid    int64
+	Fweight float64
+	Id      int64
+}
+
+func updateFpdetailByBar(pastureid, barname string, dataList []*upFpdetail) error {
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+	tx.Begin()
+
+	fpdList, err := tx.SQL(` select  id,ifnull(ROUND((1-ifnull(ptsrate,0))*weight,2),"") weight,ptuse,times  from fpdetail where pastureid = ?  and barname = ? order by times `,
+		pastureid, barname).Query().List()
+	if err != nil {
+		logs.Error(err)
 	}
 
+	for _, fpd := range fpdList {
+
+		lpplandList, err := tx.SQL(` select  lppland.id,ifnull(lppland.lweight,0) lweight from lpplandtl1 lppland join lpplan lpp on lpp.id = lppland.lppid 
+where lppland.pastureid = ?  and lppland.fpdid = ? and lpp.times = ?  order by lpp.sort desc  `,
+			pastureid, fpd["id"], fpd["times"]).Query().List()
+		if err != nil {
+			tx.Rollback()
+			logs.Error(err)
+			return err
+		}
+		var lweight float64
+		for _, lppland := range lpplandList {
+			lweight1, _ := strconv.ParseFloat(lppland["lweight"].(string), 64)
+			lweight += lweight1
+		}
+
+		fpdweight, _ := strconv.ParseFloat(fpd["weight"].(string), 64)
+		ptuse, _ := strconv.ParseFloat(fpd["ptuse"].(string), 64)
+
+		if fpdweight >= ptuse && lweight == ptuse {
+			continue
+		} else {
+			if lweight <= fpdweight {
+				_, err := tx.SQL(` update fpdetail set ptuse = ?  where id = ? `, lweight, fpd["id"]).Execute()
+				if err != nil {
+					tx.Rollback()
+					logs.Error(err)
+					return err
+				}
+			} else {
+				weight := lweight - fpdweight
+				_, err = tx.SQL(`UPDATE fpdetail SET ptuse=? 
+				WHERE pastureid=? AND id=?`, fpdweight, pastureid, fpd["id"]).Execute()
+				if err != nil {
+					tx.Rollback()
+					logs.Error(err)
+					return err
+				}
+
+				for _, lppland := range lpplandList {
+					lpplandlweight, _ := strconv.ParseFloat(lppland["lweight"].(string), 64)
+					if weight >= lpplandlweight {
+						weight = weight - lpplandlweight
+						_, err := tx.SQL(` delete from  lpplandtl1  where id = ? `, lppland["id"]).Execute()
+						if err != nil {
+							tx.Rollback()
+							logs.Error(err)
+							return err
+						}
+					} else {
+						lpplandlweight, _ := strconv.ParseFloat(lppland["lweight"].(string), 64)
+						_, err := tx.SQL(` update   lpplandtl1 set lweight = ? where id = ? `, lpplandlweight-weight, lppland["id"]).Execute()
+						if err != nil {
+							tx.Rollback()
+							logs.Error(err)
+							return err
+						}
+						weight = 0
+						break
+					}
+				}
+			}
+		}
+	}
+
+	for _, list := range dataList {
+		ftmap := make(map[string]interface{})
+		ftmap["old"] = list.Old
+		ftmap["new"] = list.New
+		ftmap["times"] = list.Times
+		ftmap["ptid"] = strconv.FormatInt(list.Ptid, 10)
+		ftmap["ptsid"] = ""
+		// fmt.Println(ftmap)
+		// select * from (SELECT TRIM(id) id,times,tratio,ifnull(ROUND(ptsrate*weight,2)-ptsuse,"") weight,TRIM(barid) barid,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
+		// (select bname  from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,concat(0) AS fttype,cowcount,ccountradio,1 isfill  FROM fpdetail
+		// WHERE fpdetail.pastureid = ?   and barname = ? and  times = ?
+		// UNION
+		fpdDataList, err := tx.SQL(`
+			SELECT TRIM(id) id,times,tratio,ifnull(ROUND((1-ptsrate)*weight,2)-ptuse,"") weight,TRIM(barid) barid,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
+			(select bname  from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,concat(1) AS fttype,cowcount,ccountradio,0 isfill  FROM fpdetail
+			WHERE fpdetail.pastureid = ?  and barname = ? and  times = ? `, pastureid, barname, ftmap["times"]).Query().List()
+		// fmt.Println(pastureid, id, ftmap["times"], pastureid, id, ftmap["times"])
+		if err != nil {
+			tx.Rollback()
+			logs.Error(err)
+		}
+		for _, fpd := range fpdDataList {
+			if fpd["times"].(int64) == ftmap["times"].(int64) && (fpd["ptid"].(string) == ftmap["ptid"].(string) || fpd["ptsid"].(string) == ftmap["ptsid"].(string)) {
+				// fttype := -1
+				// if fpd["fttype"].(string) == "1" {
+				fttype := 1
+				// }
+
+				var fweight float64
+				var lweight float64
+				lpplandtl1List := make([]*lpplandtlInfo, 0)
+				err = tx.SQL(`select lppland.*,t.maxstirfeed  maxweight from lpplan lpp
+					join lpplandtl1 lppland on lpp.id = lppland.lppid  
+					join tmr t on t.id = lpp.tmrid
+					where lppland.fpdid =?
+					and lppland.fttype = ? and lpp.pastureid = ?  and lpp.times  = ? order by lpp.sort desc `,
+					fpd["id"], fttype, pastureid, ftmap["times"]).Find(&lpplandtl1List)
+				if err != nil {
+					tx.Rollback()
+					logs.Error(err)
+				}
+				// [map[barid:4719430583171155825 barname:泌乳1-5西 ccountradio:131 cowcount:30 fttype:0 id:4828050527277810688 isfill:1 pastureid:1705635208 ptid:83
+				//  ptsid:-1 times:1 tratio:48.000 weight:0.00] map[barid:4719430583171155825
+				//  barname:泌乳1-5西 ccountradio:131 cowcount:30 fttype:1 id:4828050527277810688 isfill:0 pastureid:1705635208 ptid:83 ptsid:-1 times:1 tratio:48.000 weight:32.0
+				weight, _ := strconv.ParseFloat(fpd["weight"].(string), 64)
+
+				var w float64
+				var status int
+
+				if ftmap["new"].(float64) < ftmap["old"].(float64) && weight <= 0 {
+					if weight >= 0 {
+						continue
+					}
+					w = weight
+					status = 0
+				} else {
+					// if weight-(ftmap["new"].(float64)-ftmap["old"].(float64)) > 0 {
+					// 	continue
+					// }
+					w = weight
+					status = 1
+				}
+
+				// for _, lppland := range lpplandtl1List {
+				// 	lweight += lppland.Lweight
+				// }
+
+				for _, lppland := range lpplandtl1List {
+					lpplandInfo := new(lpplandtlInfo)
+					err := tx.SQL(` select ifnull(sum(lweight),0) lweight from lpplandtl1  where lppid = ? `, lppland.Lppid).GetFirst(lpplandInfo).Error
+					lweight = lpplandInfo.Lweight
+
+					if status == 1 {
+						// if w <= lppland.Lweight {
+						if lppland.Maxweight <= lweight {
+							continue
+						}
+						if w+lweight <= lppland.Maxweight {
+							lppland.Lweight += w
+							lweight += w
+							fweight += w
+							w = 0
+						} else {
+							w = w - (lppland.Maxweight - lweight)
+							n := (lppland.Maxweight - lweight)
+							lppland.Lweight = lppland.Lweight + n
+							lweight += n
+							fweight += n
+
+						}
+					} else {
+						if lppland.Lweight == 0 {
+							continue
+						}
+						if w+lppland.Lweight >= 0 {
+							lppland.Lweight = lppland.Lweight + w
+							lweight = lweight + w
+							fweight = fweight + w
+							w = 0
+						} else {
+							w = w + lppland.Lweight
+							lweight = lweight - lppland.Lweight
+							fweight = fweight - lppland.Lweight
+							lppland.Lweight = 0
+						}
+
+					}
+
+					if lppland.Lweight > 0 {
+
+						_, err = tx.SQL(`update lpplandtl1  set Lweight = ?  where id = ? and pastureid = ?`, lppland.Lweight, lppland.Id, pastureid).Execute()
+						if err != nil {
+							tx.Rollback()
+							logs.Error(err)
+							return err
+						}
+					} else {
+						_, err = tx.SQL(`delete from  lpplandtl1    where id = ? and pastureid = ?`, lppland.Id, pastureid).Execute()
+						if err != nil {
+							tx.Rollback()
+							logs.Error(err)
+							return err
+						}
+					}
+					if w == 0 {
+						break
+					}
+				}
+				_, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
+					ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
+					WHERE pastureid=? AND id=?`, fttype, fweight, fweight, fttype, fweight, fweight, pastureid, fpd["id"]).Execute()
+				if err != nil {
+					tx.Rollback()
+					logs.Error(err)
+				}
+				if w == 0 {
+					break
+				}
+			}
+
+		}
+	}
+
+	err = tx.Commit()
+	if err != nil {
+		logs.Error(err)
+		tx.Rollback()
+		return err
+	}
+
+	now := time.Now()
+	newLpplandtl1List, err := tx.SQL(` select  lppland.* from lpplandtl1  lppland   where lppland.barname = ?  and pastureid = ? `, barname, pastureid).Query().List()
+	if err != nil {
+		logs.Error(err)
+		return nil
+	}
+
+	newFpdDataList, err := tx.SQL(` select *  from fpdetail where barname = ?  and pastureid = ?  `, barname, pastureid).Query().List()
+	if err != nil {
+		logs.Error(err)
+		return nil
+	}
+
+	newFeedpList, err := tx.SQL(` select *  from feedp    where barname = ?  and pastureid = ?  `, barname, pastureid).Query().List()
+	if err != nil {
+		logs.Error(err)
+		return nil
+	}
+
+	for _, newLpplandtl1 := range newLpplandtl1List {
+		_, err := tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
+						background,cowcount,ccountradio,lweighthis,createdate)
+					values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
+			newLpplandtl1["pastureid"], newLpplandtl1["lppid"], newLpplandtl1["barid"],
+			newLpplandtl1["barname"], newLpplandtl1["fpdid"], newLpplandtl1["fttype"],
+			newLpplandtl1["lweight"], newLpplandtl1["sort"], newLpplandtl1["tmrid"],
+			newLpplandtl1["tmrname"], newLpplandtl1["background"], newLpplandtl1["cowcount"],
+			newLpplandtl1["ccountradio"], newLpplandtl1["lweighthis"], now).Execute()
+		if err != nil {
+			logs.Error(err)
+			// appG.Response(http.StatusOK, e.ERROR, false)
+			return err
+		}
+	}
+
+	for _, newFpd := range newFpdDataList {
+		_, err := tx.SQL(` insert into fpdetailhistory(pastureid,barid,barname,times,tratio,weight,ptsrate,cowcount,ccountradio,ptid,ptsid,ptuse,ptsuse,supplement,createdate)
+					 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, newFpd["pastureid"], newFpd["barid"], newFpd["barname"], newFpd["times"],
+			newFpd["tratio"], newFpd["weight"], newFpd["ptsrate"], newFpd["cowcount"], newFpd["ccountradio"],
+			newFpd["ptid"], newFpd["ptsid"], newFpd["ptuse"], newFpd["ptsuse"], newFpd["supplement"], now).Execute()
+		if err != nil {
+			logs.Error(err)
+			// appG.Response(http.StatusOK, e.ERROR, false)
+			return err
+		}
+	}
+	for _, newFeedp := range newFeedpList {
+		_, err := tx.SQL(` insert into feedphistory(pastureid,barname,barid,softccount,ccount,ratio,ccountratio,ftid,
+					ftname,ptsfid,ptsfname,feedweight,ftweight,supplyweight,owner,createdate)
+				values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
+			newFeedp["pastureid"], newFeedp["barname"], newFeedp["barid"],
+			newFeedp["softccount"], newFeedp["ccount"], newFeedp["ratio"],
+			newFeedp["ccountratio"], newFeedp["ftid"], newFeedp["ftname"],
+			newFeedp["ptsfid"], newFeedp["ptsfname"], newFeedp["feedweight"],
+			newFeedp["ftweight"], newFeedp["supplyweight"], newFeedp["owner"], now).Execute()
+		if err != nil {
+			logs.Error(err)
+			// appG.Response(http.StatusOK, e.ERROR, false)
+			return err
+		}
+	}
+
+	return nil
 }