Browse Source

撒料计划 自动生成 bug 修改

bsj 3 years ago
parent
commit
47b0346fcf
1 changed files with 178 additions and 79 deletions
  1. 178 79
      routers/api/spillage.go

+ 178 - 79
routers/api/spillage.go

@@ -68,6 +68,7 @@ type lpplanQueryInfo struct {
 
 type fpdetailQueryInfo struct {
 	Fpdid       int64   `xorm:"fpdid"`
+	Ptsrate     float64 `xorm:"ptsrate"`
 	Ptsuse      float64 `xorm:"ptsuse"`
 	Ptuse       float64 `xorm:"ptuse"`
 	Weight      float64 `xorm:"weight"`
@@ -75,6 +76,7 @@ type fpdetailQueryInfo struct {
 	Barname     string  `xorm:"barname"`
 	Cowcount    int64   `xorm:"cowcount"`
 	Ccountradio float64 `xorm:"ccountradio"`
+	Fttypeid    int64   `xorm:"fttypeid"`
 }
 
 type lpplandtl1 struct {
@@ -126,12 +128,15 @@ func Autogeneration(c *gin.Context) {
 		appG.Response(http.StatusOK, e.ERROR, false)
 		return
 	}
-	// var wg sync.WaitGroup
-	// wg.Add(len(feedtempletlist))
+
+	fpdetailWeightList, err := getFpdetail(pastureid, tx)
+	if err != nil {
+		appG.Response(http.StatusOK, e.ERROR, false)
+		return
+	}
+
 	for _, tem := range feedtempletlist {
 
-		// go func(tem *feedtemplet) {
-		// defer wg.Done()
 		count, err := getLpplanCount(tx)
 		if err != nil {
 			appG.Response(http.StatusOK, e.ERROR, false)
@@ -140,61 +145,102 @@ func Autogeneration(c *gin.Context) {
 		for _, tmr := range tmrlist {
 			for i := 1; i <= times; i++ {
 				exist := false
+				var lppLweight, maxweight float64
 				for _, lpp := range lpplanlist {
-
-					// session.Where("pastureid = ? ", pastureid)
-					// session.Where("times = ? ", times)
-					// session.Where("tmrid = ? ", tmrid)
-					// session.Where("ftid = ? ", ftid)
 					if lpp.Tmrid == tmr.Id && lpp.Times == int64(i) && lpp.Ftid == tem.Id {
 						exist = true
-						break
+						lppLweight += lpp.Lweight
+						maxweight += lpp.Maxweight
 					}
 				}
-				// exist, err := getLpplanExist(tmr.Id, tem.Id, i, pastureid, tx)
-				// if err != nil {
-				// 	appG.Response(http.StatusOK, e.ERROR, false)
-				// 	return
-				// }
-				if !exist {
-					ids, err := setting.SnowIds.NextId()
-					if err != nil {
-						ids = time.Now().UnixNano()
-						logging.Info("create SnowIds err", err)
-					}
-					count++
-					lpplan := &lpplanStruct{
-						Id:          ids,
-						Tmrname:     tmr.Tname,
-						Tmrid:       tmr.Id,
-						Sort:        count,
-						Sel:         1,
-						Times:       int64(i),
-						Ftname:      tem.Cname,
-						Ftid:        tem.Id,
-						Maxweight:   tmr.Maxstirfeed,
-						Sumweight:   0,
-						Sumcowcount: 0,
-						Pastureid:   pastureid,
-					}
-					err = addLpplan(lpplan, tx)
-					if err != nil {
-						appG.Response(http.StatusOK, e.ERROR, false)
-						return
+				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 {
+								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
+								}
+							}
+						} 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
+							}
+						}
+						break
 					}
+					// }
+
 				}
 			}
 		}
 		// }(tem)
 	}
 
-	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)
+	// 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)
 }
 
@@ -246,16 +292,50 @@ func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Ses
 	return exist, nil
 }
 
+type fpdetailWeight struct {
+	Times     int64   `xorm:"times"`
+	Weight    float64 `xorm:"weight"`
+	Pastureid int64   `xorm:"pastureid"`
+	Ptid      int64   `xorm:"ptid"`
+	Ptsid     int64   `xorm:"ptsid"`
+	Cowcount  int64   `xorm:"cowcount"`
+}
+
+//获取料数量
+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
+	`, pastureid, pastureid).Find(&fpdetaillist)
+	if err != nil {
+		log.Println("getFpdetail-error", err)
+		tx.Rollback()
+		return nil, err
+	}
+
+	return fpdetaillist, nil
+}
+
 type lpplanList struct {
-	Tmrid int64 `xorm:"tmrid"`
-	Times int64 `xorm:"times"`
-	Ftid  int64 `xorm:"ftid"`
+	Tmrid     int64   `xorm:"tmrid"`
+	Times     int64   `xorm:"times"`
+	Ftid      int64   `xorm:"ftid"`
+	Maxweight float64 `xorm:"maxweight"`
+	Lweight   float64 `xorm:"lweight"`
 }
 
 func getLpplanlist(patsureid string, tx *xorm.Session) ([]*lpplanList, error) {
-	session := tx.Table("lpplan")
-	session.Select("times,tmrid,ftid")
-	session.Where("pastureid = ? ", patsureid)
+
+	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
+	where t1.pastureid = ? `, patsureid, patsureid)
 	data := make([]*lpplanList, 0)
 	err := session.Find(&data)
 	if err != nil {
@@ -322,8 +402,8 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 	}
 
 	lpplansession := tx.SQL(`select  
-	t1.sort,t1.tmrid,t1.tmrname,
-	 t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight from lpplan t1 
+	t1.sort,t1.tmrid,t1.tmrname,t1.ftid,
+	 t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight,t1.times 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
 	where t1.pastureid = ?  and t1.maxweight >  ifnull(t2.lweight,0)  `, pastureid, pastureid)
 
@@ -337,16 +417,30 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 	}
 
 	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")
-		fpdetailsession.Join("inner", []string{"lpplan", "lpp"},
-			"lpp.pastureid = fpd.pastureid ").And("lpp.ftid = fpd.ptid").And(
-			"lpp.times = fpd.times").And("lpp.id = ?", lpplan.Lppid)
-		fpdetailsession.Where("fpd.pastureid = ? ", pastureid)
-		fpdetailsession.GroupBy("fpd.id")
-
+		// 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 = fpdetailsession.Find(&fpdetaillist)
+		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 ,
+		(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)
+
+		// err = fpdetailsession
 		if err != nil {
 			log.Println("autogeneration-error-4", err)
 			tx.Rollback()
@@ -355,8 +449,9 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 
 		for _, fpdetail := range fpdetaillist {
 			// 	//剩余料
-			weight := fpdetail.Weight*(1-fpdetail.Ptsuse) - fpdetail.Ptuse
-			if lpplan.Maxweight <= lpplan.Lweight || weight == 0 {
+			var weight float64
+			weight = fpdetail.Weight
+			if lpplan.Maxweight <= lpplan.Lweight || weight < 0 {
 				continue
 			}
 
@@ -366,24 +461,21 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 			} else {
 				insertlweight = lpplan.Maxweight - lpplan.Lweight
 			}
+
 			lpplan.Lweight += insertlweight
 			lpplan.Lweighthis = insertlweight
-			fpdetail.Ptuse += insertlweight
-
-			// updatefpd := tx.Table("fpdetail").Alias("fpd")
-			// updatefpd.Join("INNER", []string{"lpplan", "lpp"}, "lpp.pastureid = fpd.pastureid")
-			// updatefpd.Where("lpp.ftid = fpd.ptid").And("lpp.times = fpd.times")
-			// _, err = updatefpd.Update(fpdetail)
-			_, err = tx.SQL(` update fpdetail fpd  join lpplan lpp  on lpp.pastureid = fpd.pastureid  and  lpp.ftid = fpd.ptid and lpp.times = fpd.times
-			 set   fpd.ptuse = ?  where fpd.id = ? `, fpdetail.Ptuse, fpdetail.Fpdid).Execute()
+			// 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()
 			if err != nil {
 				log.Println("autogeneration-error-5", err)
 				tx.Rollback()
 				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, 1).Exist()
+				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()
@@ -410,7 +502,7 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 					Barid:       fpdetail.Barid,
 					Barname:     fpdetail.Barname,
 					Fpdid:       fpdetail.Fpdid,
-					Fttype:      1,
+					Fttype:      fpdetail.Fttypeid,
 					Lweight:     lpplandtl1query.Lweight + insertlweight,
 					Sort:        lpplan.Sort,
 					Tmrid:       lpplan.Tmrid,
@@ -437,7 +529,7 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 					Barid:       lpplan.Barid,
 					Barname:     lpplan.Barname,
 					Fpdid:       fpdetail.Fpdid,
-					Fttype:      1,
+					Fttype:      fpdetail.Fttypeid,
 					Lweight:     insertlweight,
 					Sort:        lpplan.Sort,
 					Tmrid:       lpplan.Tmrid,
@@ -457,11 +549,18 @@ func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
 				}
 			}
 		}
+		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
+			}
+		}
 	}
 
 	err = tx.Commit()
 	if err != nil {
-		log.Println("autogeneration-error-10", err)
+		log.Println("autogeneration-error-11", err)
 		return err
 	}
 	return nil