Browse Source

撒料计划优化

bsj 3 năm trước cách đây
mục cha
commit
c7a22114ad
3 tập tin đã thay đổi với 496 bổ sung42 xóa
  1. 256 0
      routers/api/lpplandtl.go
  2. 234 41
      routers/api/spillage.go
  3. 6 1
      routers/router.go

+ 256 - 0
routers/api/lpplandtl.go

@@ -0,0 +1,256 @@
+package api
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net/http"
+
+	"../../pkg/app"
+	"../../pkg/e"
+	"../../routers/restful"
+	"github.com/Anderson-Lu/gofasion/gofasion"
+	"github.com/gin-gonic/gin"
+)
+
+func GetLpplandtl(c *gin.Context) {
+	appG := app.Gin{C: c}
+	dataByte, _ := ioutil.ReadAll(c.Request.Body)
+	fsion := gofasion.NewFasion(string(dataByte))
+	parammaps := fsion.Get("parammaps")
+	lpplanid := parammaps.Get("id").ValueStr()
+	pastureid := parammaps.Get("pastureid").ValueStr()
+	// times := parammaps.Get("times").ValueInt64()
+
+	sqlStr := `SELECT
+		trim(lpplandtl1.lppid) lppid,lweight,
+		lpplandtl1.lweight weight,lpplandtl1.sort,
+		(select bname  from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
+		TRIM(lpplandtl1.fpdid) fpdid,
+		TRIM(lpplandtl1.barid) barid,
+		TRIM(lpplandtl1.id) id,
+		TRIM(lpplandtl1.pastureid) pastureid,
+		(SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
+		lpplandtl1.fttype,
+		trim(lpplandtl1.tmrid) tmrid,
+		ifnull((select eqcode  from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
+		ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
+		fpdetail.ptid,
+		fpdetail.ptsid,
+		fpdetail.times,
+		fpdetail.cowcount,
+		fpdetail.ccountradio
+		FROM
+		  lpplandtl1
+		inner join  fpdetail
+		on  lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
+		WHERE lpplandtl1.pastureid = ?   and lpplandtl1.lweight>0 and lpplandtl1.lppid = ?
+		`
+	// sqlStr += " and times = ? "
+	sqlStr += " ORDER BY  lpplandtl1.sort"
+
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+
+	data, err := tx.SQL(sqlStr, pastureid, lpplanid).QueryString()
+	if err != nil {
+		log.Println("GetLpplandtl-error-1: ", err)
+		appG.Response(http.StatusOK, e.ERROR, err)
+		return
+	}
+
+	appG.Response(http.StatusOK, e.SUCCESS, data)
+}
+
+type ReqDelLpplandtl struct {
+	PastureId   string `xorm:"pastureid"`
+	LpplandtlId string `xorm:"lpplandtlid"`
+	FtType      string `xorm:"fttype"`
+	Lweight     string `xorm:"lweight"`
+	FpdId       string `xorm:"fpdid"`
+}
+
+func DelLpplandtl(c *gin.Context) {
+	appG := app.Gin{C: c}
+
+	reqList := make([]*ReqDelLpplandtl, 0)
+	// if err := c.Bind(&reqList); err != nil {
+	// 	log.Println("DelLpplandtl-error-1: ", err)
+	// 	appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
+	// 	return
+	// }
+
+	dataByte, _ := ioutil.ReadAll(c.Request.Body)
+
+	if err := json.Unmarshal(dataByte, &reqList); err != nil {
+		log.Println("DelLpplandtl-error-1: ", err)
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
+		return
+	}
+
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+
+	tx.Begin()
+
+	for _, req := range reqList {
+		if len(req.Lweight) > 0 {
+			req.Lweight = fmt.Sprintf("-%s", req.Lweight)
+		}
+		_, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, req.LpplandtlId, req.PastureId).Execute()
+		if err != nil {
+			log.Println("DelLpplandtl-error-2: ", err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, err)
+			tx.Rollback()
+			return
+		}
+
+		fmt.Println(req.PastureId, req.FpdId, req.Lweight, req.FtType)
+		_, 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=? `, req.FtType, req.Lweight, req.Lweight, req.FtType, req.Lweight, req.Lweight, req.PastureId, req.FpdId).Execute()
+		if err != nil {
+			log.Println("DelLpplandtl-error-3: ", err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, err)
+			tx.Rollback()
+			return
+		}
+	}
+
+	err := tx.Commit()
+	if err != nil {
+		log.Println("DelLpplandtl-error-3: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		tx.Rollback()
+		return
+	}
+	appG.Response(http.StatusOK, e.SUCCESS, true)
+}
+
+type lpplandtl struct {
+	Id          string `xorm:"id"`
+	Pastureid   string `xorm:"pastureid"`
+	Lppid       string `xorm:"lppid"`
+	Barid       string `xorm:"barid"`
+	Barname     string `xorm:"barname"`
+	Fpdid       string `xorm:"fpdid"`
+	Fttype      string `xorm:"fttype"`
+	Lweight     string `xorm:"lweight"`
+	Sort        string  `xorm:"sort"`
+	Tmrid       string `xorm:"tmrid"`
+	Tmrname     string `xorm:"tmrname"`
+	Background  string `xorm:"background"`
+	Ccountradio string `xorm:"ccountradio"`
+}
+
+type ReqRestoreLpplandtl struct {
+	Old []*lpplandtl `json:"old"`
+	New []*lpplandtl `json:"new"`
+}
+
+func RestoreLpplandtl(c *gin.Context) {
+	
+	appG := app.Gin{C: c}
+	req := new(ReqRestoreLpplandtl)
+	// if err := c.Bind(req); err != nil {
+	// 	log.Println("RestoreLpplandtl-error-1: ", err)
+	// 	appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
+	// 	return
+	// }
+
+	dataByte, _ := ioutil.ReadAll(c.Request.Body)
+	if err := json.Unmarshal(dataByte, req); err != nil {
+		log.Println("DelLpplandtl-error-1: ", err)
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
+		return
+	}
+
+	fmt.Println(req)
+	// exist := false
+	// for _, new := range req.New {
+	// 	for _, old := range req.Old {
+	// 		if old.Id == new.Id {
+	// 			if old.Lweight != new.Lweight {
+	// 				exist = true
+	// 				break
+	// 			}
+	// 		}
+	// 	}
+	// 	if exist {
+	// 		break
+	// 	}
+	// }
+
+	// if !exist {
+	// 	appG.Response(http.StatusOK, e.SUCCESS, true)
+	// }
+
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+
+	tx.Begin()
+
+	for _, item := range req.New {
+		if len(item.Lweight) > 0 {
+			item.Lweight = fmt.Sprintf("-%s", item.Lweight)
+		}
+		_, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, item.Id, item.Pastureid).Execute()
+		if err != nil {
+			log.Println("RestoreLpplandtl-error-2: ", err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, err)
+			tx.Rollback()
+			return
+		}
+
+		_, 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=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
+		if err != nil {
+			log.Println("RestoreLpplandtl-error-3: ", err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, err)
+			tx.Rollback()
+			return
+		}
+	}
+
+	for _, item := range req.Old {
+		_, 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=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
+		if err != nil {
+			log.Println("RestoreLpplandtl-error-4", err)
+			tx.Rollback()
+			return
+		}
+
+		// tx.Table("lpplandtl1").Insert(item)
+		_, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,ccountradio,background)
+		value(?,?,?,?,?,?,?,?,?,?,?,?)`, item.Pastureid,
+			item.Lppid,
+			item.Barid,
+			item.Barname,
+			item.Fpdid,
+			item.Fttype,
+			item.Lweight,
+			item.Sort,
+			item.Tmrid,
+			item.Tmrname,
+			item.Ccountradio,
+			item.Background).Execute()
+		if err != nil {
+			log.Println("RestoreLpplandtl-error-5", err)
+			tx.Rollback()
+			return
+		}
+	}
+
+	err := tx.Commit()
+	if err != nil {
+		log.Println("RestoreLpplandtl-error-6: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		tx.Rollback()
+		return
+	}
+
+	appG.Response(http.StatusOK, e.SUCCESS, true)
+}

+ 234 - 41
routers/api/spillage.go

@@ -1,6 +1,8 @@
 package api
 package api
 
 
 import (
 import (
+	"encoding/json"
+	"errors"
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"log"
 	"log"
@@ -47,6 +49,8 @@ type lpplanStruct struct {
 	Sumcowcount int64   `xorm:"sumcowcount"`
 	Sumcowcount int64   `xorm:"sumcowcount"`
 	Pastureid   string  `xorm:"pastureid"`
 	Pastureid   string  `xorm:"pastureid"`
 	Display     string  `xorm:"display"`
 	Display     string  `xorm:"display"`
+	Issplit     int64   `xorm:"issplit"`
+	BeginTime   string  `xorm:"begintime"`
 }
 }
 
 
 type sysopt struct {
 type sysopt struct {
@@ -113,7 +117,7 @@ func Autogeneration(c *gin.Context) {
 	typeIN := fsion.Get("type").ValueInt64()
 	typeIN := fsion.Get("type").ValueInt64()
 	log.Println(time.Now())
 	log.Println(time.Now())
 	tx := restful.Engine.NewSession()
 	tx := restful.Engine.NewSession()
-	// defer tx.Close()
+	defer tx.Close()
 	var times int
 	var times int
 	var wg sync.WaitGroup
 	var wg sync.WaitGroup
 	var tmrlist *[]*tmrStruct
 	var tmrlist *[]*tmrStruct
@@ -189,12 +193,12 @@ func Autogeneration(c *gin.Context) {
 		}
 		}
 	}
 	}
 
 
-	count, err := getLpplanCount(tx)
-	if err != nil {
-		appG.Response(http.StatusOK, e.ERROR, false)
-		return
-	}
 	for i := 1; i <= times; i++ {
 	for i := 1; i <= times; i++ {
+		count, err := getLpplanCount(tx, i, pastureid)
+		if err != nil {
+			appG.Response(http.StatusOK, e.ERROR, false)
+			return
+		}
 		for _, tem := range *feedtempletlist {
 		for _, tem := range *feedtempletlist {
 			for _, fpd := range *fpdetailWeightList {
 			for _, fpd := range *fpdetailWeightList {
 				if tem.Id == fpd.Temid && i == int(fpd.Times) {
 				if tem.Id == fpd.Temid && i == int(fpd.Times) {
@@ -384,8 +388,8 @@ func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
 }
 }
 
 
 //获取车次数量
 //获取车次数量
-func getLpplanCount(tx *xorm.Session) (int64, error) {
-	count, err := tx.Table("lpplan").Count()
+func getLpplanCount(tx *xorm.Session, times int, pastureid string) (int64, error) {
+	count, err := tx.Table("lpplan").Where("times = ?", times).And("pastureid = ?", pastureid).Count()
 	if err != nil {
 	if err != nil {
 		log.Println("getLpplanCount-error", err)
 		log.Println("getLpplanCount-error", err)
 		return 0, err
 		return 0, err
@@ -757,7 +761,7 @@ func GetSpillage(c *gin.Context) {
 	parammaps := fsion.Get("parammaps")
 	parammaps := fsion.Get("parammaps")
 	pastureid := parammaps.Get("pastureid").ValueInt64()
 	pastureid := parammaps.Get("pastureid").ValueInt64()
 	times := parammaps.Get("times").ValueInt64()
 	times := parammaps.Get("times").ValueInt64()
-
+	ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
 	// refresh 0  全部 , 1 车次信息, 2 班次
 	// refresh 0  全部 , 1 车次信息, 2 班次
 	refresh := parammaps.Get("refresh").ValueInt64()
 	refresh := parammaps.Get("refresh").ValueInt64()
 	tx := restful.Engine.NewSession()
 	tx := restful.Engine.NewSession()
@@ -766,18 +770,25 @@ func GetSpillage(c *gin.Context) {
 	data := make(map[string]interface{})
 	data := make(map[string]interface{})
 
 
 	if refresh == 0 || refresh == 2 {
 	if refresh == 0 || refresh == 2 {
-
-		ftList, err := tx.SQL(`SELECT trim(feedp.pastureid) pastureid,barname,trim(barid) barid,trim(ftid) arrid,1 as type,? as times,ft.tname ftname,ft.tcolor  background,0 isfill FROM feedp 
+		sqlStr := `SELECT trim(feedp.pastureid) pastureid,barname,trim(barid) barid,trim(ftid) arrid,1 as type,? as times,ft.tname ftname,ft.tcolor  background,0 isfill FROM feedp 
 		inner join  feedtemplet ft
 		inner join  feedtemplet ft
 	on ft.id = feedp.ftid and  ft.pastureid=?
 	on ft.id = feedp.ftid and  ft.pastureid=?
-	WHERE feedp.pastureid = ?
+	WHERE feedp.pastureid = ?  %s
 	GROUP BY feedp.ftid
 	GROUP BY feedp.ftid
 	UNION
 	UNION
 	SELECT trim(feedp.pastureid) pastureid,barname,barid,trim(ptsfid) arrid,0 as type,? as times,ft.tname ptsfname ,ft.tcolor  background,1 isfill  FROM feedp
 	SELECT trim(feedp.pastureid) pastureid,barname,barid,trim(ptsfid) arrid,0 as type,? as times,ft.tname ptsfname ,ft.tcolor  background,1 isfill  FROM feedp
 	inner join  feedtemplet ft
 	inner join  feedtemplet ft
 	on ft.id = feedp.ptsfid and  ft.pastureid=?
 	on ft.id = feedp.ptsfid and  ft.pastureid=?
-	WHERE feedp.pastureid = ?  and  (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
-	GROUP BY feedp.ptsfid`, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
+	WHERE feedp.pastureid = ?  %s and  (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
+	GROUP BY feedp.ptsfid`
+
+		if ftid != 0 {
+			sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
+		} else {
+			sqlStr = fmt.Sprintf(sqlStr, "", "")
+		}
+
+		ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
 		if err != nil {
 		if err != nil {
 			log.Println("GetSpillage-error-1: ", err)
 			log.Println("GetSpillage-error-1: ", err)
 			appG.Response(http.StatusOK, e.ERROR, nil)
 			appG.Response(http.StatusOK, e.ERROR, nil)
@@ -848,8 +859,41 @@ func GetSpillage(c *gin.Context) {
 	  lpplan
 	  lpplan
 	WHERE pastureid = ?   and times <= (SELECT sysopt.inforvalue  FROM sysopt 
 	WHERE pastureid = ?   and times <= (SELECT sysopt.inforvalue  FROM sysopt 
 	 WHERE sysopt.pastureid= lpplan.pastureid  AND sysopt.inforname='times')
 	 WHERE sysopt.pastureid= lpplan.pastureid  AND sysopt.inforname='times')
-	ORDER BY  times,lpplan.sort`
-		session := tx.SQL(sqlstr, pastureid)
+	`
+		arrsql := `SELECT
+		trim(lpplandtl1.lppid) lppid,lweight,
+		lpplandtl1.lweight weight,lpplandtl1.sort,
+		(select bname  from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
+		TRIM(lpplandtl1.fpdid) fpdid,
+		TRIM(lpplandtl1.barid) barid,
+		TRIM(lpplandtl1.id) id,
+		TRIM(lpplandtl1.pastureid) pastureid,
+		(SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
+		lpplandtl1.fttype,
+		trim(lpplandtl1.tmrid) tmrid,
+		ifnull((select eqcode  from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
+		ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
+		fpdetail.ptid,
+		fpdetail.ptsid,
+		fpdetail.times,
+		fpdetail.cowcount,
+		fpdetail.ccountradio
+		FROM
+		  lpplandtl1
+		inner join  fpdetail
+		on  lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
+		WHERE lpplandtl1.pastureid = ?   and lpplandtl1.lweight>0
+		`
+
+		var args []interface{}
+		args = append(args, pastureid)
+		if times != 0 {
+			sqlstr += " and times = ? "
+			arrsql += " and times = ? "
+			args = append(args, times)
+		}
+		sqlstr += "ORDER BY  times,lpplan.sort"
+		session := tx.SQL(sqlstr, args...)
 		lppList, err := session.Query().List()
 		lppList, err := session.Query().List()
 		if err != nil {
 		if err != nil {
 			log.Println("GetSpillage-error-3: ", err)
 			log.Println("GetSpillage-error-3: ", err)
@@ -857,32 +901,10 @@ func GetSpillage(c *gin.Context) {
 			return
 			return
 		}
 		}
 
 
-		arrList, err := tx.SQL(`SELECT
-			trim(lpplandtl1.lppid) lppid,lweight,
-			lpplandtl1.lweight weight,lpplandtl1.sort,
-			(select bname  from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
-			TRIM(lpplandtl1.fpdid) fpdid,
-			TRIM(lpplandtl1.barid) barid,
-			TRIM(lpplandtl1.id) id,
-			TRIM(lpplandtl1.pastureid) pastureid,
-			(SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
-			lpplandtl1.fttype,
-			trim(lpplandtl1.tmrid) tmrid,
-			ifnull((select eqcode  from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
-			ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
-			fpdetail.ptid,
-			fpdetail.ptsid,
-			fpdetail.times,
-			fpdetail.cowcount,
-			fpdetail.ccountradio
-			FROM
-			  lpplandtl1
-			inner join  fpdetail
-			on  lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
-			WHERE lpplandtl1.pastureid = ?   and lpplandtl1.lweight>0
-			ORDER BY  lpplandtl1.sort`, pastureid).QueryString() //获取
+		arrsql += " ORDER BY  lpplandtl1.sort"
+		arrList, err := tx.SQL(arrsql, args...).QueryString() //获取
 		if err != nil {
 		if err != nil {
-			log.Println("GetSpillage-error-2: ", err)
+			log.Println("GetSpillage-error-4: ", err)
 			appG.Response(http.StatusOK, e.ERROR, nil)
 			appG.Response(http.StatusOK, e.ERROR, nil)
 			return
 			return
 		}
 		}
@@ -902,3 +924,174 @@ func GetSpillage(c *gin.Context) {
 
 
 	appG.Response(http.StatusOK, e.SUCCESS, data)
 	appG.Response(http.StatusOK, e.SUCCESS, data)
 }
 }
+
+type lpplanTrains struct {
+	Id        string `xorm:"id"`
+	Sort      int64  `xorm:"sort"`
+	Pastureid string `xorm:"pastureid"`
+}
+
+//修改车次顺序
+func UpdateTrains(c *gin.Context) {
+	appG := app.Gin{C: c}
+	lppList := make([]*lpplanTrains, 0)
+
+	err := c.Bind(&lppList)
+	if err != nil {
+		log.Println("UpdateTrains-error-1: ", err)
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
+		return
+	}
+
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+	tx.Begin()
+	for _, item := range lppList {
+		_, err = tx.Table("lpplan").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
+		if err != nil {
+			log.Println("UpdateTrains-error-2: ", err)
+			appG.Response(http.StatusInternalServerError, e.ERROR, err)
+			tx.Rollback()
+			return
+		}
+	}
+	err = tx.Commit()
+	if err != nil {
+		log.Println("UpdateTrains-error-2: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		tx.Rollback()
+		return
+	}
+	appG.Response(http.StatusOK, e.SUCCESS, true)
+}
+
+type ReqAddLpplan struct {
+	Id        string  `xorm:"id"`
+	Tmrid     string  `xorm:"tmrid"`
+	TmrName   string  `xorm:"tmrname"`
+	Sort      int64   `xorm:"sort"`
+	Sel       int64   `xorm:"sel"`
+	Times     int64   `xorm:"times"`
+	Ftname    string  `xorm:"ftname"`
+	Ftid      string  `xorm:"ftid"`
+	Maxweight float64 `xorm:"maxweight"`
+	// Sumweight   int64   `xorm:"sumweight"`
+	Sumcowcount int64  `xorm:"sumcowcount"`
+	Pastureid   string `xorm:"pastureid"`
+	Display     string `xorm:"display"`
+	Issplit     int64  `xorm:"issplit"`
+	BeginTime   string `xorm:"begintime"`
+}
+
+//添加车次并返回添加的车次信息
+func AddLpplan(c *gin.Context) {
+	appG := app.Gin{C: c}
+	dataByte, _ := ioutil.ReadAll(c.Request.Body)
+	fsion := gofasion.NewFasion(string(dataByte))
+	parammaps := fsion.Get("parammaps")
+	req := new(ReqAddLpplan)
+	err := json.Unmarshal([]byte(parammaps.Json()), req)
+	if err != nil {
+		log.Println("AddLpplan-error-1: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		return
+	}
+
+	if req.Times == 0 {
+		log.Println("AddLpplan-error-1: ", err)
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0  !!!"))
+		return
+	}
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+
+	ids, err := setting.SnowIds.NextId()
+	if err != nil {
+		ids = time.Now().UnixNano()
+		logging.Info("AddLpplan-error-1: ", err)
+	}
+	req.Id = strconv.FormatInt(ids, 10)
+
+	count, err := tx.Table("lpplan").Where("pastureid = ?", req.Pastureid).And(" times = ?", req.Times).Count()
+	if err != nil {
+		log.Println("AddLpplan-error-2: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		return
+	}
+	req.Sort = count + 1
+	_, err = tx.Table("lpplan").Insert(req)
+	if err != nil {
+		log.Println("AddLpplan-error-3: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		return
+	}
+
+	appG.Response(http.StatusOK, e.SUCCESS, req)
+}
+
+//修改车次信息,如果修改tmrid信息则删除栏舍信息
+func UpdateLpplan(c *gin.Context) {
+	appG := app.Gin{C: c}
+	dataByte, _ := ioutil.ReadAll(c.Request.Body)
+	fsion := gofasion.NewFasion(string(dataByte))
+	parammaps := fsion.Get("parammaps")
+	lpplanid := parammaps.Get("id").ValueStr()
+	pastureid := parammaps.Get("pastureid").ValueStr()
+	tmrid := parammaps.Get("tmrid").ValueStr()
+	tmrname := parammaps.Get("tmrname").ValueStr()
+	begintime := parammaps.Get("begintime").ValueStr()
+
+	times := parammaps.Get("times").ValueInt64()
+	ftid := parammaps.Get("ftid").ValueStr()
+	ftname := parammaps.Get("ftname").ValueStr()
+	sel := parammaps.Get("sel").ValueInt64()
+	issplit := parammaps.Get("issplit").ValueInt64()
+	lpplan := new(ReqAddLpplan)
+	lpplan.Tmrid = tmrid
+	lpplan.TmrName = tmrname
+	lpplan.BeginTime = begintime
+	lpplan.Sel = sel
+	lpplan.Issplit = issplit
+	lpplan.Times = times
+	lpplan.Ftid = ftid
+	lpplan.Ftname = ftname
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+	tx.Begin()
+	lpplanData := new(lpplanStruct)
+	_, err := tx.Table("lpplan").Select("tmrid").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).Get(lpplanData)
+	if err != nil {
+		log.Println("UpdateLpplan-error-1: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		tx.Rollback()
+		return
+	}
+
+	_, err = tx.Table("lpplan").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).
+		MustCols("tmrid,tmrname,begintime,sel,issplit,times,ftid,ftname").Update(lpplan)
+	if err != nil {
+		log.Println("UpdateLpplan-error-1: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		tx.Rollback()
+		return
+	}
+
+	// if tmrid != lpplan.Tmrid {
+	// 	_, err = tx.SQL(`delete from lpplandtl1 where lppid = ? and pastureid = ? `, lpplanid, pastureid).Execute()
+	// 	if err != nil {
+	// 		log.Println("UpdateLpplan-error-2: ", err)
+	// 		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+	// 		tx.Rollback()
+	// 		return
+	// 	}
+	// }
+	err = tx.Commit()
+	if err != nil {
+		log.Println("UpdateLpplan-error-3: ", err)
+		appG.Response(http.StatusInternalServerError, e.ERROR, err)
+		tx.Rollback()
+		return
+	}
+
+	appG.Response(http.StatusOK, e.SUCCESS, true)
+}

+ 6 - 1
routers/router.go

@@ -110,7 +110,12 @@ func InitRouter() *gin.Engine {
 
 
 		apiV1.POST("/autogeneration", api.Autogeneration)
 		apiV1.POST("/autogeneration", api.Autogeneration)
 		apiV1.POST("/spillage", api.GetSpillage)
 		apiV1.POST("/spillage", api.GetSpillage)
-
+		apiV1.POST("/trains", api.UpdateTrains)
+		apiV1.POST("/lpplan/edit", api.UpdateLpplan)
+		apiV1.POST("/lpplan/add", api.AddLpplan)
+		apiV1.POST("/lpplandtl", api.GetLpplandtl)
+		apiV1.POST("/lpplandtl/restore", api.RestoreLpplandtl)
+		apiV1.POST("/lpplandtl/del", api.DelLpplandtl)
 	}
 	}
 
 
 	//不需要登录验证的接口
 	//不需要登录验证的接口