Browse Source

撒料计划 自动生成 bug 修改

bsj 3 years ago
parent
commit
4da9ee0af7
1 changed files with 417 additions and 243 deletions
  1. 417 243
      routers/api/spillage.go

+ 417 - 243
routers/api/spillage.go

@@ -1,9 +1,13 @@
 package api
 
 import (
+	"fmt"
 	"io/ioutil"
 	"log"
 	"net/http"
+	"strconv"
+	"strings"
+	"sync"
 	"time"
 
 	"../../pkg/app"
@@ -77,6 +81,8 @@ type fpdetailQueryInfo struct {
 	Cowcount    int64   `xorm:"cowcount"`
 	Ccountradio float64 `xorm:"ccountradio"`
 	Fttypeid    int64   `xorm:"fttypeid"`
+	Times       int64   `xorm:"times"`
+	Ptid        int64   `xorm:"ptid"`
 }
 
 type lpplandtl1 struct {
@@ -103,149 +109,154 @@ func Autogeneration(c *gin.Context) {
 	fsion := gofasion.NewFasion(string(dataByte))
 	pastureid := fsion.Get("pastureid").ValueStr()
 	typeIN := fsion.Get("type").ValueInt64()
-
+	log.Println(time.Now())
 	tx := restful.Engine.NewSession()
-	feedtempletlist, err := getfeedtemplet(pastureid, tx)
-	if err != nil {
-		appG.Response(http.StatusOK, e.ERROR, false)
-		return
-	}
-
-	tmrlist, err := gettmr(pastureid, tx)
-	if err != nil {
-		appG.Response(http.StatusOK, e.ERROR, false)
-		return
-	}
-
-	times, err := gettimes(pastureid, tx)
-	if err != nil {
-		appG.Response(http.StatusOK, e.ERROR, false)
-		return
-	}
-
-	lpplanlist, err := getLpplanlist(pastureid, tx)
-	if err != nil {
-		appG.Response(http.StatusOK, e.ERROR, false)
-		return
-	}
-
-	fpdetailWeightList, err := getFpdetail(pastureid, tx)
-	if err != nil {
-		appG.Response(http.StatusOK, e.ERROR, false)
-		return
+	// defer tx.Close()
+	var times int
+	var wg sync.WaitGroup
+	var tmrlist *[]*tmrStruct
+	var feedtempletlist *[]*feedtemplet
+	var err error
+	var lpplanlist *[]*lpplanList
+	var fpdetailWeightList *[]*fpdetailWeight
+
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		feedtempletlist, err = getfeedtemplet(pastureid, tx)
+		if err != nil {
+			appG.Response(http.StatusOK, e.ERROR, false)
+			return
+		}
+	}()
+	wg.Wait()
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		tmrlist, err = gettmr(pastureid, tx)
+		if err != nil {
+			appG.Response(http.StatusOK, e.ERROR, false)
+			return
+		}
+	}()
+	wg.Wait()
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		times, err = gettimes(pastureid, tx)
+		if err != nil {
+			appG.Response(http.StatusOK, e.ERROR, false)
+			return
+		}
+	}()
+	wg.Wait()
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		lpplanlist, err = getLpplanlist(pastureid, tx)
+		if err != nil {
+			appG.Response(http.StatusOK, e.ERROR, false)
+			return
+		}
+	}()
+	wg.Wait()
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		fpdetailWeightList, err = getFpdetail(pastureid, tx)
+		if err != nil {
+			appG.Response(http.StatusOK, e.ERROR, false)
+			return
+		}
+		for _, fpd := range *fpdetailWeightList {
+			for _, lpp := range *lpplanlist {
+				if (lpp.Ftid == fpd.Ptid || lpp.Ftid == fpd.Ptsid) && lpp.Times == fpd.Times {
+					fpd.Weight = fpd.Weight - (lpp.Maxweight - lpp.Lweight)
+				}
+			}
+		}
+	}()
+
+	wg.Wait()
+	// 避免车次数量为0造成死循环
+	max := false
+	for _, tmr := range *tmrlist {
+		if tmr.Maxstirfeed > 0 {
+			max = true
+			break
+		}
 	}
 
-	for _, tem := range feedtempletlist {
-
+	for _, tem := range *feedtempletlist {
 		count, err := getLpplanCount(tx)
 		if err != nil {
 			appG.Response(http.StatusOK, e.ERROR, false)
 			return
 		}
-		for _, tmr := range tmrlist {
-			for i := 1; i <= times; i++ {
-				exist := false
-				var lppLweight, maxweight float64
-				for _, lpp := range lpplanlist {
-					if lpp.Tmrid == tmr.Id && lpp.Times == int64(i) && lpp.Ftid == tem.Id {
-						exist = true
-						lppLweight += lpp.Lweight
-						maxweight += lpp.Maxweight
+		for i := 1; i <= times; i++ {
+			for _, fpd := range *fpdetailWeightList {
+				if tem.Id == fpd.Temid && i == int(fpd.Times) {
+					var ftid int64
+					if fpd.Ptid != 0 {
+						ftid = fpd.Ptid
+					} else {
+						ftid = fpd.Ptsid
 					}
-				}
-				lppLweight = maxweight - lppLweight
-				for _, fpd := range fpdetailWeightList {
-					if (tem.Id == fpd.Ptid || tem.Id == fpd.Ptsid) && i == int(fpd.Times) {
-						var ftid int64
-						if fpd.Ptid != 0 {
-							ftid = fpd.Ptid
-						} else {
-							ftid = fpd.Ptsid
-						}
-						if tmr.Maxstirfeed <= 0 {
-							if !exist {
+					for fpd.Weight > 0 && max {
+						for _, tmr := range *tmrlist {
+							if tmr.Maxstirfeed > 0 {
 								count++
 								ids, err := setting.SnowIds.NextId()
 								if err != nil {
 									ids = time.Now().UnixNano()
 									logging.Info("create SnowIds err", err)
 								}
-								lpplan := &lpplanStruct{
-									Id:          ids,
-									Tmrname:     tmr.Tname,
-									Tmrid:       tmr.Id,
-									Sort:        count,
-									Sel:         1,
-									Times:       int64(i),
-									Ftname:      tem.Cname,
-									Ftid:        ftid,
-									Maxweight:   tmr.Maxstirfeed,
-									Sumweight:   0,
-									Sumcowcount: 0,
-									Pastureid:   pastureid,
-								}
-								err = addLpplan(lpplan, tx)
-								if err != nil {
-									appG.Response(http.StatusOK, e.ERROR, false)
-									return
-								}
+								wg.Add(1)
+								go func(ids int64, tmr *tmrStruct, count, i, ftid int64, tem *feedtemplet) {
+									defer wg.Done()
+									lpplan := &lpplanStruct{
+										Id:          ids,
+										Tmrname:     tmr.Tname,
+										Tmrid:       tmr.Id,
+										Sort:        count,
+										Sel:         1,
+										Times:       int64(i),
+										Ftname:      tem.Cname,
+										Ftid:        ftid,
+										Maxweight:   tmr.Maxstirfeed,
+										Sumweight:   0,
+										Sumcowcount: 0,
+										Pastureid:   pastureid,
+									}
+									err = addLpplan(lpplan, tx)
+									if err != nil {
+										appG.Response(http.StatusOK, e.ERROR, false)
+										return
+									}
+								}(ids, tmr, count, int64(i), ftid, tem)
+								fpd.Weight = fpd.Weight - tmr.Maxstirfeed
 							}
-						} else {
-
-							var weight float64
-							weight = fpd.Weight - lppLweight
-							for weight > 0 {
-								count++
-								ids, err := setting.SnowIds.NextId()
-								if err != nil {
-									ids = time.Now().UnixNano()
-									logging.Info("create SnowIds err", err)
-								}
-
-								lpplan := &lpplanStruct{
-									Id:          ids,
-									Tmrname:     tmr.Tname,
-									Tmrid:       tmr.Id,
-									Sort:        count,
-									Sel:         1,
-									Times:       int64(i),
-									Ftname:      tem.Cname,
-									Ftid:        ftid,
-									Maxweight:   tmr.Maxstirfeed,
-									Sumweight:   0,
-									Sumcowcount: 0,
-									Pastureid:   pastureid,
-								}
-								err = addLpplan(lpplan, tx)
-								if err != nil {
-									appG.Response(http.StatusOK, e.ERROR, false)
-									return
-								}
-								weight = weight - tmr.Maxstirfeed
+							if fpd.Weight < 0 {
+								break
 							}
 						}
-						break
 					}
-					// }
-
 				}
 			}
 		}
-		// }(tem)
 	}
+	wg.Wait()
 
-	// go func(typeIN int64, pastureid string, tx *xorm.Session) {
 	err = autogeneration(typeIN, pastureid, tx)
 	if err != nil {
 		appG.Response(http.StatusOK, e.ERROR, false)
 		return
 	}
-	// }(typeIN, pastureid, tx)
 	appG.Response(http.StatusOK, e.SUCCESS, true)
 }
 
 //查看配方模板
-func getfeedtemplet(pastureid string, tx *xorm.Session) ([]*feedtemplet, error) {
+func getfeedtemplet(pastureid string, tx *xorm.Session) (*[]*feedtemplet, error) {
 	data := make([]*feedtemplet, 0)
 	err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
 	if err != nil {
@@ -253,11 +264,11 @@ func getfeedtemplet(pastureid string, tx *xorm.Session) ([]*feedtemplet, error)
 		return nil, err
 	}
 
-	return data, nil
+	return &data, nil
 }
 
 //查看tmr设备
-func gettmr(pastureid string, tx *xorm.Session) ([]*tmrStruct, error) {
+func gettmr(pastureid string, tx *xorm.Session) (*[]*tmrStruct, error) {
 	data := make([]*tmrStruct, 0)
 	session := tx.Table("tmr")
 
@@ -272,7 +283,7 @@ func gettmr(pastureid string, tx *xorm.Session) ([]*tmrStruct, error) {
 		return nil, err
 	}
 
-	return data, nil
+	return &data, nil
 }
 
 //判断计划种是否有该车次
@@ -299,20 +310,27 @@ type fpdetailWeight struct {
 	Ptid      int64   `xorm:"ptid"`
 	Ptsid     int64   `xorm:"ptsid"`
 	Cowcount  int64   `xorm:"cowcount"`
+	Temid     int64   `xorm:"Temid"`
 }
 
 //获取料数量
-func getFpdetail(pastureid string, tx *xorm.Session) ([]*fpdetailWeight, error) {
+func getFpdetail(pastureid string, tx *xorm.Session) (*[]*fpdetailWeight, error) {
 	fpdetaillist := make([]*fpdetailWeight, 0)
 
-	err := tx.SQL(`SELECT times,sum(ROUND(ptsrate*weight,2)-ptsuse)  weight,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
-	cowcount FROM fpdetail
-	WHERE fpdetail.pastureid = ? AND  ptsuse< ROUND(ptsrate*weight,2)    and ptsid !=0
-	group by ptid,times,ptsid
-	UNION
-	SELECT times,sum(ROUND((1-ptsrate)*weight,2)-ptuse)weight,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,cowcount  FROM fpdetail
-	WHERE fpdetail.pastureid = ? AND ptuse<ROUND((1-ptsrate)*weight,2) and ptid !=0
-	group by ptid,times,ptsid
+	err := tx.SQL(`	select t1.temid,t1.times,ifnull(t2.weight,0)+sum(t1.weight) as weight ,TRIM(t1.pastureid) pastureid,TRIM(t1.ptid) ptid,TRIM(t1.ptsid ) ptsid ,t1.cowcount  from (SELECT fpd.id,ft.id as temid,fpd.times, ROUND((1-fpd.ptsrate)*fpd.weight,2)-fpd.ptuse weight,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,fpd.cowcount  FROM fpdetail fpd
+	join feedp  fp on    fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
+	inner join  feedtemplet ft on ft.id = fp.ftid  and ft.pastureid = fpd.pastureid
+	WHERE fpd.pastureid = ? AND fpd.ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptid > 0
+	group by fpd.id,fpd.times) t1  
+left join (	select t1.id,t1.temid as temid, t1.times,sum(t1.weight)  weight,TRIM(t1.pastureid) pastureid,TRIM(t1.ptid) ptid,TRIM(t1.ptsid ) ptsid ,
+	t1.cowcount from  (SELECT fpd.id,ft.id as temid, fpd.times,ROUND(fpd.ptsrate*fpd.weight,2)-fpd.ptsuse weight,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
+	cowcount FROM fpdetail fpd
+	join feedp  fp on   fp.ptsfid  = fpd.ptsid and fp.pastureid = fpd.pastureid
+	inner join  feedtemplet ft on ft.id = fp.ftid  and ft.pastureid = fpd.pastureid
+	WHERE fpd.pastureid = ? AND  fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2)   and fpd.ptsid > 0
+	group by fpd.id,fpd.times) t1 
+	group by t1.temid,t1.times) t2  on t2.temid = t1.temid and t2.times = t1.times
+group by t1.temid,t1.times
 	`, pastureid, pastureid).Find(&fpdetaillist)
 	if err != nil {
 		log.Println("getFpdetail-error", err)
@@ -320,7 +338,7 @@ func getFpdetail(pastureid string, tx *xorm.Session) ([]*fpdetailWeight, error)
 		return nil, err
 	}
 
-	return fpdetaillist, nil
+	return &fpdetaillist, nil
 }
 
 type lpplanList struct {
@@ -331,7 +349,7 @@ type lpplanList struct {
 	Lweight   float64 `xorm:"lweight"`
 }
 
-func getLpplanlist(patsureid string, tx *xorm.Session) ([]*lpplanList, error) {
+func getLpplanlist(patsureid string, tx *xorm.Session) (*[]*lpplanList, error) {
 
 	session := tx.SQL(` select  t1.times,t1.tmrid,t1.ftid,t1.maxweight ,ifnull(t2.lweight,0)as lweight from lpplan t1 
 	left join ( select  ifnull(sum(lweight),0) as lweight,lppid  from lpplandtl1  where pastureid =?   GROUP BY lppid) t2 on t1.id = t2.lppid
@@ -342,7 +360,7 @@ func getLpplanlist(patsureid string, tx *xorm.Session) ([]*lpplanList, error) {
 		log.Println("getLpplanlist-error", err)
 		return nil, err
 	}
-	return data, nil
+	return &data, nil
 }
 
 // 添加车次计划
@@ -381,6 +399,12 @@ func gettimes(pastureid string, tx *xorm.Session) (int, error) {
 	return data.Inforvalue, nil
 }
 
+type upfpdetail struct {
+	insertlweight float64
+	fttypeid      int64
+	fpdid         int64
+}
+
 func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 
 	tx.Begin()
@@ -415,149 +439,299 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 		tx.Rollback()
 		return err
 	}
-
-	for _, lpplan := range lpplanlist {
-		// fpdetailsession := tx.Table("fpdetail").Alias("fpd")
-		// fpdetailsession.Select("fpd.barid,fpd.barname,fpd.id as fpdid ,fpd.weight as weight,ifnull(fpd.ptuse,0) as ptuse,ifnull(fpd.ptsuse,0) as ptsuse,fpd.barid,fpd.barname,fpd.cowcount,fpd.ccountradio,fpd.ptsrate as ptsrate")
-		// fpdetailsession.Where(" fpd.ptid = ? ", lpplan.Ftid)
-		// fpdetailsession.Where(" fpd.times = ? ", lpplan.Times)
-		// fpdetailsession.Where("fpd.pastureid = ? ", pastureid)
-		// fpdetailsession.GroupBy("fpd.id")
-		fpdetaillist := make([]*fpdetailQueryInfo, 0)
-		err = tx.SQL(`SELECT  0 as fttypeid,fpd.cowcount,fpd.ccountradio,fpd.id as fpdid,fpd.times,fpd.tratio,ROUND(fpd.ptsrate*fpd.weight,2)-fpd.ptsuse weight,
-		TRIM(fpd.barid) barid,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
-		(select bname  from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,
-		cowcount,ccountradio,1 isfill  FROM fpdetail fpd
-				join feedp  fp on   fp.ptsfid  = fpd.ptsid and fp.pastureid = fpd.pastureid
-	inner join  feedtemplet ft on ft.id = fp.ftid  and ft.pastureid = fpd.pastureid
-WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptsid = ?
-		and fpd.times = ?
-		UNION
-		SELECT ft.fttypeid,fpd.cowcount,fpd.ccountradio,fpd.id as fpdid,times,tratio,ROUND((1-fpd.ptsrate)*fpd.weight,2)-fpd.ptuse weight,TRIM(fpd.barid) barid,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
+	fpdetaillist := make([]*fpdetailQueryInfo, 0)
+	err = tx.SQL(`SELECT fpd.ptid,ft.fttypeid,fpd.cowcount,fpd.ccountradio,fpd.id as fpdid,times,tratio,ROUND((1-fpd.ptsrate)*fpd.weight,2)-fpd.ptuse weight,TRIM(fpd.barid) barid,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
 		(select bname  from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,cowcount,ccountradio,0 isfill  FROM fpdetail fpd
 			join feedp  fp on   fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
 	inner join  feedtemplet ft on ft.id = fp.ftid  and ft.pastureid = fpd.pastureid
-		WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptid = ?
-		and fpd.times = ?`, pastureid, lpplan.Ftid, lpplan.Times, pastureid, lpplan.Ftid, lpplan.Times).Find(&fpdetaillist)
+		WHERE  fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2)
+		group by fpd.id , fpd.times `, pastureid).Find(&fpdetaillist)
+	if err != nil {
+		log.Println("autogeneration-error-4", err)
+		tx.Rollback()
+		return err
+	}
+	log.Println(time.Now(), "333333333333")
+	var wg sync.WaitGroup
+	for _, lpplan := range lpplanlist {
+		lpplandtl1List := make([]*lpplandtl1, 0)
+		upfpdetailList := make([]*upfpdetail, 0)
+		for _, fpdetail := range fpdetaillist {
+			if fpdetail.Ptid == lpplan.Ftid && fpdetail.Times == lpplan.Times {
+				var weight float64
+				weight = fpdetail.Weight
+				if lpplan.Maxweight <= lpplan.Lweight || weight <= 0 {
+					continue
+				}
+
+				var insertlweight float64
+				if lpplan.Maxweight-lpplan.Lweight > weight {
+					insertlweight = weight
+				} else {
+					insertlweight = lpplan.Maxweight - lpplan.Lweight
+				}
+
+				lpplan.Lweight += insertlweight
+				lpplan.Lweighthis = insertlweight
+
+				exist := false
+				for _, up := range upfpdetailList {
+					if up.fpdid == fpdetail.Fpdid {
+						up.insertlweight += insertlweight
+						exist = true
+						break
+					}
+				}
+
+				if !exist {
+					upfpdetailList = append(upfpdetailList, &upfpdetail{
+						fpdid:         fpdetail.Fpdid,
+						insertlweight: insertlweight,
+						fttypeid:      fpdetail.Fttypeid,
+					})
+				}
+
+				fpdetail.Weight = fpdetail.Weight - insertlweight
+
+				exist = false
+				for _, lpplandtl1 := range lpplandtl1List {
+					if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
+						lpplandtl1.Lweight += insertlweight
+						lpplandtl1.Lweighthis = insertlweight
+						exist = true
+						break
+					}
+				}
+				if !exist {
+					lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
+						Pastureid:   lpplan.Pastureid,
+						Lppid:       lpplan.Lppid,
+						Barid:       fpdetail.Barid,
+						Barname:     fpdetail.Barname,
+						Fpdid:       fpdetail.Fpdid,
+						Fttype:      fpdetail.Fttypeid,
+						Lweight:     insertlweight,
+						Sort:        lpplan.Sort,
+						Tmrid:       lpplan.Tmrid,
+						Tmrname:     lpplan.Tmrname,
+						Cowcount:    lpplan.Cowcount,
+						Ccountradio: lpplan.Ccountradio,
+						Background:  lpplan.Background,
+						Lweighthis:  insertlweight,
+					})
+				}
+			}
+		}
+
+		for _, lpplpplandtl1 := range lpplandtl1List {
+			wg.Add(1)
+			go func(lpplpplandtl1 *lpplandtl1) {
+				defer wg.Done()
+				_, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
+				cowcount,ccountradio,background,lweighthis)
+			value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+			ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
+					lpplpplandtl1.Lppid,
+					lpplpplandtl1.Barid,
+					lpplpplandtl1.Barname,
+					lpplpplandtl1.Fpdid,
+					lpplpplandtl1.Fttype,
+					lpplpplandtl1.Lweight,
+					lpplpplandtl1.Sort,
+					lpplpplandtl1.Tmrid,
+					lpplpplandtl1.Tmrname,
+					lpplpplandtl1.Cowcount,
+					lpplpplandtl1.Ccountradio,
+					lpplpplandtl1.Background,
+					lpplpplandtl1.Lweighthis).Execute()
+				if err != nil {
+					log.Println("autogeneration-error-6", err)
+					tx.Rollback()
+					return
+				}
+			}(lpplpplandtl1)
+		}
+		wg.Wait()
+
+		for _, up := range upfpdetailList {
+			wg.Add(1)
+			go func(up *upfpdetail) {
+				defer wg.Done()
+				_, 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=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
+				if err != nil {
+					log.Println("autogeneration-error-5", err)
+					tx.Rollback()
+					return
+				}
+			}(up)
+		}
+		wg.Wait()
+	}
 
-		// err = fpdetailsession
+	if typeIN == 1 {
+		_, err = tx.SQL("update lpplan set sel = 1 where pastureid = ?", pastureid).Execute()
 		if err != nil {
-			log.Println("autogeneration-error-4", err)
-			tx.Rollback()
+			log.Println("autogeneration-error-10", err)
 			return err
 		}
+	}
 
-		for _, fpdetail := range fpdetaillist {
-			// 	//剩余料
-			var weight float64
-			weight = fpdetail.Weight
-			if lpplan.Maxweight <= lpplan.Lweight || weight < 0 {
+	supplement := tx.Table("fpdetail").Alias("fpd")
+	supplement.Select("fpd.id as fpdid,fpd.barid,ft.id as temid, fpd.times,ROUND(fpd.ptsrate*fpd.weight,2)-fpd.ptsuse weight,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,cowcount")
+	supplement.Join("inner", []string{"feedp", "fp"}, "fp.ptsfid  = fpd.ptsid").And("fp.pastureid = fpd.pastureid")
+	supplement.Join("inner", []string{"feedtemplet", "ft"}, "ft.id = fp.ftid").And("ft.pastureid = fpd.pastureid")
+	supplement.Where("fpd.pastureid =  ?", pastureid).And("fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2)").And("fpd.ptsid > 0")
+	supplement.GroupBy("fpd.id,fpd.times")
+	supplementList := make([]*fpdetailQueryInfo, 0)
+	err = supplement.Find(&supplementList)
+	if err != nil {
+		log.Println("autogeneration-error-11", err)
+		return err
+	}
+
+	if len(supplementList) > 0 {
+		for _, lpplan := range lpplanlist {
+			if lpplan.Maxweight <= lpplan.Lweight {
 				continue
 			}
-
-			var insertlweight float64
-			if lpplan.Maxweight-lpplan.Lweight > weight {
-				insertlweight = weight
-			} else {
-				insertlweight = lpplan.Maxweight - lpplan.Lweight
+			lppids := []string{strconv.FormatInt(lpplan.Lppid, 10)}
+			for _, lpp := range lpplanlist {
+				if lpp.Ftid == lpplan.Ftid && lpp.Times == lpplan.Times {
+					lppids = append(lppids, strconv.FormatInt(lpp.Lppid, 10))
+				}
 			}
 
-			lpplan.Lweight += insertlweight
-			lpplan.Lweighthis = insertlweight
-			// fpdetail.Ptsuse += insertlweight
-
-			_, 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=? `, fpdetail.Fttypeid, fpdetail.Weight, fpdetail.Weight, fpdetail.Fttypeid, fpdetail.Weight, fpdetail.Weight, pastureid, fpdetail.Fpdid).Execute()
+			lpplandtl1List := make([]*lpplandtl1, 0)
+			upfpdetailList := make([]*upfpdetail, 0)
+			lpplandtllist := make([]*lpplandtl1, 0)
+			err = tx.Table("lpplandtl1").Select("barid").Where(fmt.Sprintf("lppid  in (%s)", strings.Join(lppids, ","))).And("").Find(&lpplandtllist)
 			if err != nil {
-				log.Println("autogeneration-error-5", err)
-				tx.Rollback()
+				log.Println("autogeneration-error-11", err)
 				return err
 			}
-			fpdetail.Weight = fpdetail.Weight - insertlweight
-			lpplandtlExecute, err := tx.SQL(`select id from lpplandtl1 where pastureid= ?
-				and  lppid = ?  and fpdid =? and  fttype = ? `, pastureid, lpplan.Lppid, fpdetail.Fpdid, fpdetail.Fttypeid).Exist()
-			if err != nil {
-				log.Println("autogeneration-error-6", err)
-				tx.Rollback()
-				return err
-			}
-			if lpplandtlExecute {
-				lpplandtl1query := new(lpplandtl1)
-				query := tx.Table("lpplandtl1")
-				query.Where("pastureid = ?", pastureid)
-				query.Where("lppid = ?", lpplan.Lppid)
-				query.Where("fpdid = ?", fpdetail.Fpdid)
-				query.Where("fttype = 1")
-				_, err = query.Get(lpplandtl1query)
-				if err != nil {
-					log.Println("autogeneration-error-7", err)
-					tx.Rollback()
-					return err
-				}
 
-				upLpplandtl1 := tx.Table("lpplandtl1")
-				data := &lpplandtl1{
-					Pastureid:   lpplan.Pastureid,
-					Lppid:       lpplan.Lppid,
-					Barid:       fpdetail.Barid,
-					Barname:     fpdetail.Barname,
-					Fpdid:       fpdetail.Fpdid,
-					Fttype:      fpdetail.Fttypeid,
-					Lweight:     lpplandtl1query.Lweight + insertlweight,
-					Sort:        lpplan.Sort,
-					Tmrid:       lpplan.Tmrid,
-					Tmrname:     lpplan.Tmrname,
-					Cowcount:    fpdetail.Cowcount,
-					Ccountradio: fpdetail.Ccountradio,
-					// Background:  lpplan.Background,
-					Lweighthis: insertlweight,
+			for _, fpdetail := range supplementList {
+				if lpplan.Times != fpdetail.Times {
+					continue
 				}
-				upLpplandtl1.Where("pastureid = ?", pastureid)
-				upLpplandtl1.Where("lppid = ?", lpplan.Lppid)
-				upLpplandtl1.Where("fpdid = ?", fpdetail.Fpdid)
-				upLpplandtl1.Where("fttype = 1")
-				_, err = upLpplandtl1.Update(data)
-				if err != nil {
-					log.Println("autogeneration-error-8", err)
-					tx.Rollback()
-					return err
+				exist := false
+				for _, bar := range lpplandtllist {
+					if bar.Barid == fpdetail.Barid {
+						exist = true
+						break
+					}
+				}
+				if !exist {
+					continue
+				}
+				var weight float64
+				weight = fpdetail.Weight
+				if weight <= 0 {
+					continue
 				}
-			} else {
-				data := &lpplandtl1{
-					Pastureid:   lpplan.Pastureid,
-					Lppid:       lpplan.Lppid,
-					Barid:       lpplan.Barid,
-					Barname:     lpplan.Barname,
-					Fpdid:       fpdetail.Fpdid,
-					Fttype:      fpdetail.Fttypeid,
-					Lweight:     insertlweight,
-					Sort:        lpplan.Sort,
-					Tmrid:       lpplan.Tmrid,
-					Tmrname:     lpplan.Tmrname,
-					Cowcount:    lpplan.Cowcount,
-					Ccountradio: lpplan.Ccountradio,
-					Background:  lpplan.Background,
-					Lweighthis:  insertlweight,
+				var insertlweight float64
+				if lpplan.Maxweight-lpplan.Lweight > weight {
+					insertlweight = weight
+				} else {
+					insertlweight = lpplan.Maxweight - lpplan.Lweight
 				}
+				lpplan.Lweight += insertlweight
+				lpplan.Lweighthis = insertlweight
 
-				addLpplandtl1 := tx.Table("lpplandtl1")
-				_, err = addLpplandtl1.Insert(data)
-				if err != nil {
-					log.Println("autogeneration-error-9", err)
-					tx.Rollback()
-					return err
+				exist = false
+				for _, up := range upfpdetailList {
+					if up.fpdid == fpdetail.Fpdid {
+						up.insertlweight += insertlweight
+						exist = true
+						break
+					}
+				}
+				if !exist {
+					upfpdetailList = append(upfpdetailList, &upfpdetail{
+						fpdid:         fpdetail.Fpdid,
+						insertlweight: insertlweight,
+						fttypeid:      0,
+					})
+				}
+
+				fpdetail.Weight = fpdetail.Weight - insertlweight
+
+				exist = false
+				for _, lpplandtl1 := range lpplandtl1List {
+					if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
+						lpplandtl1.Lweight += insertlweight
+						lpplandtl1.Lweighthis = insertlweight
+						exist = true
+						break
+					}
+				}
+				if !exist {
+					lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
+						Pastureid:   lpplan.Pastureid,
+						Lppid:       lpplan.Lppid,
+						Barid:       fpdetail.Barid,
+						Barname:     fpdetail.Barname,
+						Fpdid:       fpdetail.Fpdid,
+						Fttype:      0,
+						Lweight:     insertlweight,
+						Sort:        lpplan.Sort,
+						Tmrid:       lpplan.Tmrid,
+						Tmrname:     lpplan.Tmrname,
+						Cowcount:    lpplan.Cowcount,
+						Ccountradio: lpplan.Ccountradio,
+						Background:  lpplan.Background,
+						Lweighthis:  insertlweight,
+					})
 				}
 			}
-		}
-		if typeIN == 1 {
-			_, err = tx.SQL("update lpplan set sel = 1 where id = ?", lpplan.Lppid).Execute()
-			if err != nil {
-				log.Println("autogeneration-error-10", err)
-				return err
+			for _, lpplpplandtl1 := range lpplandtl1List {
+				wg.Add(1)
+				go func(lpplpplandtl1 *lpplandtl1) {
+					defer wg.Done()
+					_, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
+					cowcount,ccountradio,background,lweighthis)
+				value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+				ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
+						lpplpplandtl1.Lppid,
+						lpplpplandtl1.Barid,
+						lpplpplandtl1.Barname,
+						lpplpplandtl1.Fpdid,
+						lpplpplandtl1.Fttype,
+						lpplpplandtl1.Lweight,
+						lpplpplandtl1.Sort,
+						lpplpplandtl1.Tmrid,
+						lpplpplandtl1.Tmrname,
+						lpplpplandtl1.Cowcount,
+						lpplpplandtl1.Ccountradio,
+						lpplpplandtl1.Background,
+						lpplpplandtl1.Lweighthis).Execute()
+					if err != nil {
+						log.Println("autogeneration-error-6", err)
+						tx.Rollback()
+						return
+					}
+				}(lpplpplandtl1)
 			}
+			wg.Wait()
+
+			for _, up := range upfpdetailList {
+				wg.Add(1)
+				go func(up *upfpdetail) {
+					defer wg.Done()
+					_, 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=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
+					if err != nil {
+						log.Println("autogeneration-error-5", err)
+						tx.Rollback()
+						// return err
+						return
+					}
+				}(up)
+			}
+			wg.Wait()
 		}
 	}
-
 	err = tx.Commit()
 	if err != nil {
 		log.Println("autogeneration-error-11", err)