| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799 | 
							- package api
 
- import (
 
- 	"encoding/json"
 
- 	"errors"
 
- 	"fmt"
 
- 	"io/ioutil"
 
- 	"log"
 
- 	"net/http"
 
- 	"sort"
 
- 	"strconv"
 
- 	"strings"
 
- 	"sync"
 
- 	"time"
 
- 	"tmr-watch/conf/setting"
 
- 	"tmr-watch/http/handle/restful"
 
- 	"tmr-watch/pkg/app"
 
- 	"tmr-watch/pkg/e"
 
- 	"tmr-watch/pkg/logging"
 
- 	"github.com/Anderson-Lu/gofasion/gofasion"
 
- 	"github.com/gin-gonic/gin"
 
- 	"github.com/xormplus/xorm"
 
- )
 
- type feedtemplet struct {
 
- 	Id     int64  `xorm:"id"`
 
- 	Ccname string `xorm:"ccname"`
 
- 	Cname  string `xorm:"tname"`
 
- }
 
- type tmrStruct struct {
 
- 	Id          int64   `xorm:"id"`
 
- 	Tname       string  `xorm:"tname"`
 
- 	Maxstirfeed float64 `xorm:"maxstirfeed"`
 
- }
 
- type lpplanStruct struct {
 
- 	Id          int64   `xorm:"id"`
 
- 	Tmrname     string  `xorm:"tmrname"`
 
- 	Tmrid       int64   `xorm:"tmrid"`
 
- 	Sort        int64   `xorm:"sort"`
 
- 	Sel         int64   `xorm:"sel"`
 
- 	Times       int64   `xorm:"times"`
 
- 	Ftname      string  `xorm:"ftname"`
 
- 	Ftid        int64   `xorm:"ftid"`
 
- 	Maxweight   float64 `xorm:"maxweight"`
 
- 	Sumweight   float64 `xorm:"sumweight"`
 
- 	Sumcowcount int64   `xorm:"sumcowcount"`
 
- 	Pastureid   string  `xorm:"pastureid"`
 
- 	Display     string  `xorm:"display"`
 
- 	Issplit     int64   `xorm:"issplit"`
 
- 	BeginTime   string  `xorm:"begintime"`
 
- }
 
- type sysopt struct {
 
- 	Inforvalue int `xorm:"inforvalue"`
 
- }
 
- type lpplanQueryInfo struct {
 
- 	Pastureid   int64   `xorm:"pastureid"`
 
- 	Lppid       int64   `xorm:"id"`
 
- 	Maxweight   float64 `xorm:"maxweight"`
 
- 	Lweight     float64 `xorm:"lweight"`
 
- 	Lweighthis  float64 `xorm:"lweighthis"`
 
- 	Barid       int64   `xorm:"barid"`
 
- 	Barname     string  `xorm:"barname"`
 
- 	Sort        int64   `xorm:"sort"`
 
- 	Tmrid       int64   `xorm:"tmrid"`
 
- 	Tmrname     string  `xorm:"tmrname"`
 
- 	Cowcount    int64   `xorm:"cowcount"`
 
- 	Ccountradio float64 `xorm:"ccountradio"`
 
- 	Background  string  `xorm:"background"`
 
- 	Times       int64   `xorm:"times"`
 
- 	Ftid        int64   `xorm:"ftid"`
 
- }
 
- type fpdetailQueryInfo struct {
 
- 	Fpdid       int64   `xorm:"fpdid"`
 
- 	Ptsrate     float64 `xorm:"ptsrate"`
 
- 	Ptsuse      float64 `xorm:"ptsuse"`
 
- 	Ptuse       float64 `xorm:"ptuse"`
 
- 	Weight      float64 `xorm:"weight"`
 
- 	Barid       int64   `xorm:"barid"`
 
- 	Barname     string  `xorm:"barname"`
 
- 	Cowcount    int64   `xorm:"cowcount"`
 
- 	Ccountradio float64 `xorm:"ccountradio"`
 
- 	Fttypeid    int64   `xorm:"fttypeid"`
 
- 	Times       int64   `xorm:"times"`
 
- 	Ptid        int64   `xorm:"ptid"`
 
- 	Background  string  `xorm:"background"`
 
- }
 
- type lpplandtl1 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"`
 
- }
 
- //撒料计划 自动生成
 
- func Autogeneration(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	pastureid := fsion.Get("pastureid").ValueStr()
 
- 	typeIN := fsion.Get("type").ValueInt64()
 
- 	log.Println(time.Now())
 
- 	tx := restful.Engine.NewSession()
 
- 	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 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 _, 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
 
- 					}
 
- 					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)
 
- 								}
 
- 								display := tem.Cname
 
- 								if i == 1 {
 
- 									display += "第一班"
 
- 								} else if i == 2 {
 
- 									display += "第二班"
 
- 								} else if i == 3 {
 
- 									display += "第三班"
 
- 								} else if i == 4 {
 
- 									display += "第四班"
 
- 								}
 
- 								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,
 
- 									Display:     display,
 
- 								}
 
- 								err = addLpplan(lpplan, tx)
 
- 								if err != nil {
 
- 									appG.Response(http.StatusOK, e.ERROR, false)
 
- 									return
 
- 								}
 
- 								fpd.Weight = fpd.Weight - tmr.Maxstirfeed
 
- 							}
 
- 							if fpd.Weight < 0 {
 
- 								break
 
- 							}
 
- 						}
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- 	// wg.Wait()
 
- 	err = autogeneration(typeIN, pastureid, tx)
 
- 	if err != nil {
 
- 		appG.Response(http.StatusOK, e.ERROR, false)
 
- 		return
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- //查看配方模板
 
- 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 {
 
- 		log.Println("getfeedtemplet-error", err)
 
- 		return nil, err
 
- 	}
 
- 	return &data, nil
 
- }
 
- //查看tmr设备
 
- func gettmr(pastureid string, tx *xorm.Session) (*[]*tmrStruct, error) {
 
- 	data := make([]*tmrStruct, 0)
 
- 	session := tx.Table("tmr")
 
- 	session.Select("TRIM(id) id,tname,maxstirfeed")
 
- 	session.Where("pastureid = ? ", pastureid)
 
- 	session.Where("enable = 1")
 
- 	session.In("tclassid", "1,2,3")
 
- 	err := session.Find(&data)
 
- 	if err != nil {
 
- 		log.Println("gettmr-error", err)
 
- 		return nil, err
 
- 	}
 
- 	return &data, nil
 
- }
 
- //判断计划种是否有该车次
 
- func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
 
- 	session := tx.Table("lpplan")
 
- 	session.Where("pastureid = ? ", pastureid)
 
- 	session.Where("times = ? ", times)
 
- 	session.Where("tmrid = ? ", tmrid)
 
- 	session.Where("ftid = ? ", ftid)
 
- 	exist, err := session.Exist()
 
- 	if err != nil {
 
- 		log.Println("getLpplanExist-error", err)
 
- 		return false, err
 
- 	}
 
- 	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"`
 
- 	Temid     int64   `xorm:"Temid"`
 
- }
 
- //获取料数量
 
- func getFpdetail(pastureid string, tx *xorm.Session) (*[]*fpdetailWeight, error) {
 
- 	fpdetaillist := make([]*fpdetailWeight, 0)
 
- 	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-ifnull(fpd.ptsrate,0))*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-ifnull(fpd.ptsrate,0))*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(ifnull(fpd.ptsrate,0)*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(ifnull(fpd.ptsrate,0)*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)
 
- 		tx.Rollback()
 
- 		return nil, err
 
- 	}
 
- 	return &fpdetaillist, nil
 
- }
 
- type lpplanList struct {
 
- 	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.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 {
 
- 		log.Println("getLpplanlist-error", err)
 
- 		return nil, err
 
- 	}
 
- 	return &data, nil
 
- }
 
- // 添加车次计划
 
- func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
 
- 	_, err := tx.Table("lpplan").Insert(lpplan)
 
- 	if err != nil {
 
- 		log.Println("addLpplan-error", err)
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
- //获取车次数量
 
- 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 {
 
- 		log.Println("getLpplanCount-error", err)
 
- 		return 0, err
 
- 	}
 
- 	return count, err
 
- }
 
- //获取班次信息
 
- func gettimes(pastureid string, tx *xorm.Session) (int, error) {
 
- 	session := tx.Table("sysopt")
 
- 	session.Where("pastureid = ? ", pastureid)
 
- 	session.Where("inforname = 'times' ")
 
- 	data := new(sysopt)
 
- 	_, err := session.Get(data)
 
- 	if err != nil {
 
- 		log.Println("gettimes-error", err)
 
- 		return 0, err
 
- 	}
 
- 	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()
 
- 	if typeIN == 1 {
 
- 		_, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
 
- 		if err != nil {
 
- 			log.Println("autogeneration-error-1", err)
 
- 			tx.Rollback()
 
- 			return err
 
- 		}
 
- 		_, err = tx.SQL(`update  fpdetail
 
- 		 set ptuse = 0,ptsuse=0
 
- 		WHERE pastureid = ?`, pastureid).Execute()
 
- 		if err != nil {
 
- 			log.Println("autogeneration-error-2", err)
 
- 			tx.Rollback()
 
- 			return err
 
- 		}
 
- 	}
 
- 	lpplansession := tx.SQL(`select  
 
- 	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)
 
- 	lpplanlist := make([]*lpplanQueryInfo, 0)
 
- 	err := lpplansession.Find(&lpplanlist)
 
- 	if err != nil {
 
- 		log.Println("autogeneration-error-3", err)
 
- 		tx.Rollback()
 
- 		return err
 
- 	}
 
- 	fpdetaillist := make([]*fpdetailQueryInfo, 0)
 
- 	err = tx.SQL(`SELECT ft.tcolor  background,fpd.ptid,ft.fttypeid,fpd.cowcount,fpd.ccountradio,fpd.id as fpdid,times,tratio,ROUND((1-ifnull(fpd.ptsrate,0))*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-ifnull(fpd.ptsrate,0))*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:  fpdetail.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()
 
- 	}
 
- 	if typeIN == 1 {
 
- 		_, err = tx.SQL("update lpplan set sel = 1 where pastureid = ?", pastureid).Execute()
 
- 		if err != nil {
 
- 			log.Println("autogeneration-error-10", err)
 
- 			return err
 
- 		}
 
- 	}
 
- 	supplement := tx.Table("fpdetail").Alias("fpd")
 
- 	supplement.Select("ft.tcolor  background,fpd.id as fpdid,fpd.barid,ft.id as temid, fpd.times,ROUND(ifnull(fpd.ptsrate,0)*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(ifnull(fpd.ptsrate,0)*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
 
- 			}
 
- 			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))
 
- 				}
 
- 			}
 
- 			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-11", err)
 
- 				return err
 
- 			}
 
- 			for _, fpdetail := range supplementList {
 
- 				if lpplan.Times != fpdetail.Times {
 
- 					continue
 
- 				}
 
- 				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
 
- 				}
 
- 				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:      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:  fpdetail.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 err
 
- 						return
 
- 					}
 
- 				}(up)
 
- 			}
 
- 			wg.Wait()
 
- 		}
 
- 	}
 
- 	err = tx.Commit()
 
- 	if err != nil {
 
- 		log.Println("autogeneration-error-11", err)
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
- func GetSpillage(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	parammaps := fsion.Get("parammaps")
 
- 	pastureid := parammaps.Get("pastureid").ValueInt64()
 
- 	times := parammaps.Get("times").ValueInt64()
 
- 	ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
 
- 	// refresh 0  全部 , 1 车次信息, 2 班次
 
- 	refresh := parammaps.Get("refresh").ValueInt64()
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	data := make(map[string]interface{})
 
- 	if refresh == 0 || refresh == 2 {
 
- 		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
 
- 	on ft.id = feedp.ftid and  ft.pastureid=?
 
- 	WHERE feedp.pastureid = ?  %s
 
- 	GROUP BY feedp.ftid
 
- 	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
 
- 	inner join  feedtemplet ft
 
- 	on ft.id = feedp.ptsfid and  ft.pastureid=?
 
- 	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 {
 
- 			log.Println("GetSpillage-error-1: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		ftDetailList, err := tx.SQL(`SELECT TRIM(id) id,times,tratio,ROUND(ifnull(ptsrate,0)*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,0 AS fttype,cowcount,ccountradio,1 isfill  FROM fpdetail
 
- 	WHERE fpdetail.pastureid = ?  AND times=? AND  ptsuse< ROUND(ifnull(ptsrate,0)*weight,2)  
 
- 	and (select id from bar where id = fpdetail.barid and pastureid =  fpdetail.pastureid) is not null
 
- 	UNION
 
- 	SELECT TRIM(id) id,times,tratio,ROUND((1-ifnull(ptsrate,0))*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,1 AS fttype,cowcount,ccountradio,0 isfill  FROM fpdetail
 
- 	WHERE fpdetail.pastureid = ?  AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2)
 
- 	and (select id from bar where id = fpdetail.barid and pastureid =  fpdetail.pastureid) is not null
 
- 	ORDER BY  barid`, pastureid, times, pastureid, times).Query().List()
 
- 		if err != nil {
 
- 			log.Println("GetSpillage-error-2: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		for _, ft := range ftList {
 
- 			arrList := make([]map[string]interface{}, 0)
 
- 			arrid := ft["arrid"].(string)
 
- 			for _, detail := range ftDetailList {
 
- 				if _, ok := detail["ptid"]; ok {
 
- 					if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
 
- 						detail["background"] = ft["background"]
 
- 						arrList = append(arrList, detail)
 
- 					}
 
- 				} else if _, ok := detail["ptsid"]; ok {
 
- 					if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
 
- 						detail["background"] = ft["background"]
 
- 						arrList = append(arrList, detail)
 
- 					}
 
- 				}
 
- 			}
 
- 			if len(arrList) > 0 {
 
- 				ft["arrList"] = arrList
 
- 			}
 
- 		}
 
- 		data["ftlist"] = ftList
 
- 	}
 
- 	if refresh == 0 || refresh == 1 {
 
- 		sqlstr := `SELECT
 
- 		ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) tmrname,trim(tmrid) tmrid,sort,sel,
 
- 		CASE times
 
- 		WHEN 1 THEN
 
- 		'第一班'
 
- 	WHEN 2 THEN
 
- 	'第二班'
 
- 	WHEN 3 THEN
 
- 	'第三班'
 
- 	WHEN 4 THEN
 
- 	'第四班'
 
- 	END timesstr,issplit,
 
- 	times,display,begintime,
 
- 	(select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
 
- 	IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1
 
- 	inner join  fpdetail
 
- 	on  lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
 
- 	 WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.pastureid =lpplan.pastureid ),2),0) sumweight ,
 
- 	ifnull((select maxstirfeed from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),100000) maxweight,
 
- 	TRIM(ftid) ftid,
 
- 	TRIM(id) id,
 
- 	TRIM(id) lppid,
 
- 	TRIM(pastureid) pastureid
 
- 	
 
- 	FROM
 
- 	  lpplan
 
- 	WHERE pastureid = ?   and times <= (SELECT sysopt.inforvalue  FROM sysopt 
 
- 	 WHERE sysopt.pastureid= lpplan.pastureid  AND sysopt.inforname='times')
 
- 	`
 
- 		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()
 
- 		if err != nil {
 
- 			log.Println("GetSpillage-error-3: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		arrsql += " ORDER BY  lpplandtl1.sort"
 
- 		arrList, err := tx.SQL(arrsql, args...).QueryString() //获取
 
- 		if err != nil {
 
- 			log.Println("GetSpillage-error-4: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		for _, query := range lppList {
 
- 			list := make([]map[string]string, 0)
 
- 			for _, arr := range arrList {
 
- 				if query["lppid"].(string) == arr["lppid"] {
 
- 					list = append(list, arr)
 
- 				}
 
- 			}
 
- 			query["arrList"] = list
 
- 		}
 
- 		data["list"] = lppList
 
- 		data["total"] = len(lppList)
 
- 	}
 
- 	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}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	lppList := make([]*lpplanTrains, 0)
 
- 	err := json.Unmarshal(dataByte, &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)
 
- }
 
- func UpdateTrainsDay(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	lppList := make([]*lpplanTrains, 0)
 
- 	err := json.Unmarshal(dataByte, &lppList)
 
- 	if err != nil {
 
- 		log.Println("UpdateTrainsDay-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("lpplandate").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
 
- 		if err != nil {
 
- 			log.Println("UpdateTrainsDay-error-2: ", err)
 
- 			appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 			tx.Rollback()
 
- 			return
 
- 		}
 
- 	}
 
- 	err = tx.Commit()
 
- 	if err != nil {
 
- 		log.Println("UpdateTrainsDay-error-3: ", 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)
 
- 	datacount, err := tx.Table("lpplan").Select("max(sort) as sort").Where("pastureid = ?", req.Pastureid).And(" times = ?", req.Times).QueryString()
 
- 	if err != nil {
 
- 		log.Println("AddLpplan-error-2: ", err)
 
- 		appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	var count int64 = 0
 
- 	for _, d := range datacount {
 
- 		if _, ok := d["sort"]; ok {
 
- 			count, _ = strconv.ParseInt(d["sort"], 10, 64)
 
- 		}
 
- 	}
 
- 	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)
 
- }
 
- type ReqAddLpplanDay 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"`
 
- 	Date        string `xorm:"date"`
 
- }
 
- func AddLpplanDay(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(ReqAddLpplanDay)
 
- 	err := json.Unmarshal([]byte(parammaps.Json()), req)
 
- 	if err != nil {
 
- 		log.Println("AddLpplanDay-error-1: ", err)
 
- 		appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	if req.Times == 0 {
 
- 		log.Println("AddLpplanDay-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("AddLpplanDay-error-1: ", err)
 
- 	}
 
- 	req.Id = strconv.FormatInt(ids, 10)
 
- 	count, err := tx.Table("lpplandate").Where("pastureid = ?", req.Pastureid).And(" date = ?", req.Date).And(" times = ?", req.Times).Count()
 
- 	if err != nil {
 
- 		log.Println("AddLpplanDay-error-2: ", err)
 
- 		appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	req.Sort = count + 1
 
- 	_, err = tx.Table("lpplandate").Insert(req)
 
- 	if err != nil {
 
- 		log.Println("AddLpplanDay-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()
 
- 	display := parammaps.Get("display").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
 
- 	lpplan.Display = display
 
- 	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("display,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
 
- 	}
 
- 	err = tx.Commit()
 
- 	if err != nil {
 
- 		log.Println("UpdateLpplan-error-3: ", err)
 
- 		appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 		tx.Rollback()
 
- 		return
 
- 	}
 
- 	dataList, err := tx.SQL(`select * from lpplandtl1 where lppid = ? and  pastureid = ? `, lpplanid, pastureid).Query().List()
 
- 	if err != nil {
 
- 		log.Println("UpdateLpplan-error-4: ", err)
 
- 		appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	// var oldbar, bar []int64
 
- 	oldList := fsion.Get("old").Array()
 
- 	if len(dataList) >= len(oldList) {
 
- 		for _, data := range dataList {
 
- 			updateStatus := true
 
- 			for _, old := range oldList {
 
- 				fmt.Println(old.Get("lweight").ValueStr(), data["lweight"], old.Get("barid").ValueInt64(), data["barid"], "aaaaaaaaaaa")
 
- 				if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
 
- 					updateStatus = false
 
- 					break
 
- 				}
 
- 			}
 
- 			if updateStatus {
 
- 				now := time.Now()
 
- 				for _, data := range dataList {
 
- 					_, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
 
- 											background,cowcount,ccountradio,lweighthis,createdate)
 
- 										values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
 
- 						data["pastureid"], data["lppid"], data["barid"],
 
- 						data["barname"], data["fpdid"], data["fttype"],
 
- 						data["lweight"], data["sort"], data["tmrid"],
 
- 						data["tmrname"], data["background"], data["cowcount"],
 
- 						data["ccountradio"], data["lweighthis"], now).Execute()
 
- 					if err != nil {
 
- 						log.Println("UpdateLpplan-error-5: ", err)
 
- 						appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 						return
 
- 					}
 
- 				}
 
- 				break
 
- 			}
 
- 		}
 
- 	} else {
 
- 		for _, old := range oldList {
 
- 			updateStatus := true
 
- 			for _, data := range dataList {
 
- 				if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
 
- 					updateStatus = false
 
- 					break
 
- 				}
 
- 			}
 
- 			if updateStatus {
 
- 				now := time.Now()
 
- 				for _, old := range oldList {
 
- 					exist := false
 
- 					var lweight, tmrid, tmrname interface{}
 
- 					for _, data := range dataList {
 
- 						if old.Get("barid").ValueInt64() == data["barid"].(int64) {
 
- 							lweight = data["lweight"]
 
- 							tmrid = data["tmrid"]
 
- 							tmrname = data["tmrname"]
 
- 							exist = true
 
- 							break
 
- 						}
 
- 					}
 
- 					if !exist {
 
- 						tmrid = old.Get("tmrid").ValueInt64()
 
- 						tmrname = old.Get("tmrname").ValueStr()
 
- 						lweight = 0
 
- 					}
 
- 					_, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
 
- 											background,cowcount,ccountradio,lweighthis,createdate) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
 
- 						old.Get("pastureid").ValueStr(), old.Get("lppid").ValueStr(), old.Get("barid").ValueStr(),
 
- 						old.Get("barname").ValueStr(), old.Get("fpdid").ValueStr(), old.Get("fttype").ValueStr(),
 
- 						lweight, old.Get("sort").ValueStr(), tmrid,
 
- 						tmrname, old.Get("background").ValueStr(), old.Get("cowcount").ValueStr(),
 
- 						old.Get("ccountradio").ValueStr(), old.Get("lweighthis").ValueInt64(), now).Execute()
 
- 					if err != nil {
 
- 						log.Println("UpdateLpplan-error-6: ", err)
 
- 						appG.Response(http.StatusInternalServerError, e.ERROR, err)
 
- 						return
 
- 					}
 
- 				}
 
- 				break
 
- 			}
 
- 		}
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- func GetSpillageDay(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	parammaps := fsion.Get("parammaps")
 
- 	pastureid := parammaps.Get("pastureid").ValueInt64()
 
- 	times := parammaps.Get("times").ValueInt64()
 
- 	ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
 
- 	// refresh 0  全部 , 1 车次信息, 2 班次
 
- 	refresh := parammaps.Get("refresh").ValueInt64()
 
- 	date := parammaps.Get("date").ValueStr()
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	data := make(map[string]interface{})
 
- 	if refresh == 0 || refresh == 2 {
 
- 		sqlStr := `	SELECT TRIM(feedp.pastureid) pastureid,feedp.barname,TRIM(feedp.barid) barid,TRIM(ft.id) arrid,1 AS type,? AS times,ft.tname ftname,ft.tcolor  background,feedp.date,0 isfill
 
- FROM feedpdate feedp
 
- INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid  AND fpd.pastureid = feedp.pastureid
 
- INNER JOIN  feedtemplet ft
 
- ON ft.id = fpd.ptid AND  ft.pastureid= feedp.pastureid
 
- WHERE feedp.pastureid = ? AND feedp.date=? %s
 
- GROUP BY fpd.ptid
 
- UNION
 
- SELECT TRIM(feedp.pastureid) pastureid,feedp.barname,feedp.barid,TRIM(ft.id) arrid,0 AS type,? AS times,ft.tname ptsfname ,ft.tcolor  background ,feedp.date ,1 isfill
 
- FROM feedpdate feedp
 
- INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
 
- INNER JOIN  feedtemplet ft
 
- ON ft.id = fpd.ptsid AND  ft.pastureid=feedp.pastureid
 
- WHERE feedp.pastureid = ?  AND feedp.date=? %s
 
-  AND  (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1 
 
- GROUP BY fpd.ptsid`
 
- 		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, date, times, pastureid, date).Query().List()
 
- 		if err != nil {
 
- 			log.Println("GetSpillageDay-error-1: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		// AND  ( fpdetail.ptsid=?)
 
- 		// AND  (fpdetail.ptid=? )
 
- 		ftDetailList, err := tx.SQL(`SELECT TRIM(id) id,times,tratio,ROUND(ifnull(ptsrate,0)*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,cowcount,ccountradio, 1 isfill  FROM fpdetaildate fpdetail
 
- 		WHERE fpdetail.pastureid = ?  AND times=? AND  ptsuse< ROUND(ifnull(ptsrate,0)*weight,2) and fpdetail.date = ?
 
- 		and (select id from bar where id = fpdetail.barid and pastureid =  fpdetail.pastureid ) is not null
 
- 		UNION
 
- 		SELECT TRIM(id) id,times,tratio,ROUND((1-ifnull(ptsrate,0))*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,cowcount,ccountradio, 0 isfill  FROM fpdetaildate fpdetail
 
- 		WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2) and fpdetail.date = ?
 
- 		and (select id from bar where id = fpdetail.barid and pastureid =  fpdetail.pastureid) is not null
 
- 		ORDER BY  barid`, pastureid, times, date, pastureid, times, date).Query().List()
 
- 		if err != nil {
 
- 			log.Println("GetSpillageDay-error-2: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		for _, ft := range ftList {
 
- 			arrList := make([]map[string]interface{}, 0)
 
- 			arrid := ft["arrid"].(string)
 
- 			for _, detail := range ftDetailList {
 
- 				if _, ok := detail["ptid"]; ok {
 
- 					if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
 
- 						detail["background"] = ft["background"]
 
- 						detail["fttype"] = ft["type"].(int64)
 
- 						arrList = append(arrList, detail)
 
- 					}
 
- 				} else if _, ok := detail["ptsid"]; ok {
 
- 					if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
 
- 						detail["background"] = ft["background"]
 
- 						detail["fttype"] = ft["type"].(int64)
 
- 						arrList = append(arrList, detail)
 
- 					}
 
- 				}
 
- 			}
 
- 			if len(arrList) > 0 {
 
- 				ft["arrList"] = arrList
 
- 			}
 
- 		}
 
- 		data["ftlist"] = ftList
 
- 	}
 
- 	if refresh == 0 || refresh == 1 {
 
- 		sqlstr := `SELECT
 
- 		lpplan.date,
 
- 		-- ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) 
 
- 		lpplan.tmrname tmrname,trim(lpplan.tmrid) tmrid,lpplan.sort,lpplan.sel,
 
- 		CASE lpplan.times
 
- 		WHEN 1 THEN
 
- 		'第一班'
 
- 		WHEN 2 THEN
 
- 		'第二班'
 
- 		WHEN 3 THEN
 
- 		'第三班'
 
- 		WHEN 4 THEN
 
- 		'第四班'
 
- 		END timesstr,issplit,
 
- 		lpplan.times,display,begintime,
 
- 		(select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
 
- 		IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1date  lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.date= ?),2),0) sumweight ,
 
- 		maxweight,
 
- 		TRIM(ftid) ftid,
 
- 		TRIM(lpplan.id) id,
 
- 		TRIM(lpplan.id) lppid,
 
- 		TRIM(lpplan.pastureid) pastureid,
 
- 		ifnull(dpl.havebutton,0) as havebutton
 
- 		FROM
 
- 		  lpplandate lpplan
 
- 			left join downloadedplan dpl on dpl.pid = lpplan.id  and  DATE_FORMAT(dpl.mydate,'%Y-%m-%e') = lpplan.date
 
- 		WHERE lpplan.pastureid = ?  and lpplan.date = ? and lpplan.times <= (SELECT sysopt.inforvalue  FROM sysopt 
 
- 		 WHERE sysopt.pastureid= lpplan.pastureid  AND sysopt.inforname='times') 
 
- 		`
 
- 		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,
 
- 		lpplandtl1.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
 
- 		  lpplandtl1date lpplandtl1	
 
- 		inner join  fpdetaildate fpdetail
 
- 		on  lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid  and lpplandtl1.date = fpdetail.date
 
- 		WHERE lpplandtl1.pastureid = ? and lpplandtl1.date = ? and lpplandtl1.lweight>0 `
 
- 		var args []interface{}
 
- 		args = append(args, date, pastureid, date)
 
- 		if times != 0 {
 
- 			sqlstr += " and lpplan.times = ? "
 
- 			// arrsql += " and times = ? "
 
- 			args = append(args, times)
 
- 		}
 
- 		sqlstr += " group by lpplan.id  ORDER BY  times,lpplan.sort"
 
- 		session := tx.SQL(sqlstr, args...)
 
- 		lppList, err := session.Query().List()
 
- 		if err != nil {
 
- 			log.Println("GetSpillageDay-error-3: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		arrsql += " ORDER BY  lpplandtl1.sort"
 
- 		arrList, err := tx.SQL(arrsql, pastureid, date).QueryString() //获取
 
- 		if err != nil {
 
- 			log.Println("GetSpillage-error-4: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		for _, query := range lppList {
 
- 			list := make([]map[string]string, 0)
 
- 			for _, arr := range arrList {
 
- 				if query["lppid"].(string) == arr["lppid"] {
 
- 					list = append(list, arr)
 
- 				}
 
- 			}
 
- 			query["arrList"] = list
 
- 		}
 
- 		data["list"] = lppList
 
- 		data["total"] = len(lppList)
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, data)
 
- }
 
- // func AddLpplandtl(c *gin.Context) {
 
- // 	appG := app.Gin{C: c}
 
- // 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- // 	fsion := gofasion.NewFasion(string(dataByte))
 
- // 	parammaps := fsion.Get("parammaps")
 
- // 	tx := restful.Engine.NewSession()
 
- // 	defer tx.Close()
 
- // 	exist := false
 
- // 	var err error
 
- // 	lpplandList := make([]*lpplandtl1, 0)
 
- // 	err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
 
- // 		OrderBy("sort").Find(&lpplandList)
 
- // 	if err != nil {
 
- // 		log.Println("AddLpplandtl-error-1: ", err)
 
- // 		appG.Response(http.StatusOK, e.ERROR, err)
 
- // 		return
 
- // 	}
 
- // 	if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
 
- // 		for _, lppand := range lpplandList {
 
- // 			if lppand.Id == parammaps.Get("id").ValueInt64() {
 
- // 				if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
 
- // 					appG.Response(http.StatusOK, e.SUCCESS, true)
 
- // 					return
 
- // 				}
 
- // 				exist = true
 
- // 				break
 
- // 			}
 
- // 		}
 
- // 	}
 
- // 	tmrid := parammaps.Get("tmrid").ValueInt64()
 
- // 	var sort int64 = -1
 
- // 	if !exist {
 
- // 		for _, lppand := range lpplandList {
 
- // 			if lppand.Barid == parammaps.Get("barid").ValueInt64() {
 
- // 				_, err := tx.SQL(" update lpplandtl1 set lweight =lweight +  ?  where id = ?  and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
 
- // 				if err != nil {
 
- // 					log.Println("AddLpplandtl-error-2: ", err)
 
- // 					appG.Response(http.StatusOK, e.ERROR, err)
 
- // 					return
 
- // 				}
 
- // 				appG.Response(http.StatusOK, e.SUCCESS, true)
 
- // 				return
 
- // 			}
 
- // 		}
 
- // 		for i, lppand := range lpplandList {
 
- // 			lppand.Sort = int64(i) + 1
 
- // 		}
 
- // 		updateLpplandList := make([]*lpplandtl1, 0)
 
- // 		for i, lppand := range lpplandList {
 
- // 			n := i + 1
 
- // 			if lppand.Tmrid == tmrid {
 
- // 				sort = int64(n) + 1
 
- // 				lppand.Sort = int64(n)
 
- // 				updateLpplandList = append(updateLpplandList, lppand)
 
- // 			} else if sort > 0 {
 
- // 				lppand.Sort = int64(n) + 2
 
- // 				updateLpplandList = append(updateLpplandList, lppand)
 
- // 			}
 
- // 		}
 
- // 		tx.Begin()
 
- // 		for _, lppand := range updateLpplandList {
 
- // 			_, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ?  and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
 
- // 			if err != nil {
 
- // 				tx.Rollback()
 
- // 				log.Println("AddLpplandtl-error-2: ", err)
 
- // 				appG.Response(http.StatusOK, e.ERROR, err)
 
- // 				return
 
- // 			}
 
- // 		}
 
- // 		if sort < 0 && len(lpplandList) > 0 {
 
- // 			sort = int64(len(lpplandList) + 2)
 
- // 		} else if len(lpplandList) == 0 {
 
- // 			sort = 1
 
- // 		}
 
- // 		_, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
 
- // 	VALUES (?,?,?,?,?,?,?,?,?,
 
- // 	?,?,IF(?='',0,?),IF(?='',0,?))`,
 
- // 			parammaps.Get("pastureid").ValueStr(),
 
- // 			parammaps.Get("lppid").ValueStr(),
 
- // 			parammaps.Get("barid").ValueStr(),
 
- // 			parammaps.Get("barname").ValueStr(),
 
- // 			parammaps.Get("fpdid").ValueStr(),
 
- // 			parammaps.Get("lweight").ValueStr(),
 
- // 			sort,
 
- // 			parammaps.Get("tmrid").ValueStr(),
 
- // 			parammaps.Get("tmrname").ValueStr(),
 
- // 			parammaps.Get("fttype").ValueStr(),
 
- // 			parammaps.Get("background").ValueStr(),
 
- // 			parammaps.Get("cowcount").ValueStr(),
 
- // 			parammaps.Get("cowcount").ValueStr(),
 
- // 			parammaps.Get("ccountradio").ValueStr(),
 
- // 			parammaps.Get("ccountradio").ValueStr()).Execute()
 
- // 		if err != nil {
 
- // 			tx.Rollback()
 
- // 			log.Println("AddLpplandtl-error-3: ", err)
 
- // 			appG.Response(http.StatusOK, e.ERROR, err)
 
- // 			return
 
- // 		}
 
- // 		err = tx.Commit()
 
- // 		if err != nil {
 
- // 			tx.Rollback()
 
- // 			log.Println("AddLpplandtl-error-6: ", err)
 
- // 			appG.Response(http.StatusOK, e.ERROR, err)
 
- // 			return
 
- // 		}
 
- // 	} else {
 
- // 		lpplandid := parammaps.Get("id").ValueInt64()
 
- // 		updateLpplandList := make([]*lpplandtl1, 0)
 
- // 		var oldsort int64
 
- // 		for i, lppand := range lpplandList {
 
- // 			lppand.Sort = int64(i) + 1
 
- // 		}
 
- // 		for _, lppand := range lpplandList {
 
- // 			if lppand.Id == lpplandid {
 
- // 				oldsort = lppand.Sort
 
- // 				break
 
- // 			}
 
- // 		}
 
- // 		for _, lppand := range lpplandList {
 
- // 			if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
 
- // 				sort = lppand.Sort
 
- // 				break
 
- // 			}
 
- // 		}
 
- // 		if sort == -1 {
 
- // 			sort = 1
 
- // 		}
 
- // 		if oldsort < sort {
 
- // 			for _, lppand := range lpplandList {
 
- // 				if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
 
- // 					lppand.Sort--
 
- // 					updateLpplandList = append(updateLpplandList, lppand)
 
- // 				} else if lppand.Sort > sort && lppand.Id != lpplandid {
 
- // 					lppand.Sort++
 
- // 					updateLpplandList = append(updateLpplandList, lppand)
 
- // 				}
 
- // 			}
 
- // 			sort--
 
- // 		} else {
 
- // 			for _, lppand := range lpplandList {
 
- // 				if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
 
- // 					lppand.Sort++
 
- // 					updateLpplandList = append(updateLpplandList, lppand)
 
- // 				}
 
- // 			}
 
- // 		}
 
- // 		var tmrlpplandList []*lpplandtl1
 
- // 		for _, lppand := range lpplandList {
 
- // 			exist := false
 
- // 			for _, lppand1 := range updateLpplandList {
 
- // 				if lppand.Id == lppand1.Id {
 
- // 					exist = true
 
- // 					break
 
- // 				}
 
- // 			}
 
- // 			if !exist {
 
- // 				tmrlpplandList = append(tmrlpplandList, lppand)
 
- // 			}
 
- // 		}
 
- // 		updateLpplandList = append(updateLpplandList, tmrlpplandList...)
 
- // 		if len(updateLpplandList) > 0 {
 
- // 			tx.Begin()
 
- // 			for _, lppand := range updateLpplandList {
 
- // 				_, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ?  and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
 
- // 				if err != nil {
 
- // 					tx.Rollback()
 
- // 					log.Println("AddLpplandtl-error-2: ", err)
 
- // 					appG.Response(http.StatusOK, e.ERROR, err)
 
- // 					return
 
- // 				}
 
- // 			}
 
- // 			_, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?,sort = ?   where id = ?  and pastureid = ? ", parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), sort, lpplandid, parammaps.Get("pastureid").ValueStr()).Execute()
 
- // 			if err != nil {
 
- // 				tx.Rollback()
 
- // 				log.Println("AddLpplandtl-error-2: ", err)
 
- // 				appG.Response(http.StatusOK, e.ERROR, err)
 
- // 				return
 
- // 			}
 
- // 			tx.Commit()
 
- // 		}
 
- // 	}
 
- // 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- // }
 
- func AddLpplandtl(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	parammaps := fsion.Get("parammaps")
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	exist := false
 
- 	var err error
 
- 	lpplandList := make([]*lpplandtl1, 0)
 
- 	err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
 
- 		OrderBy("sort").Find(&lpplandList)
 
- 	if err != nil {
 
- 		log.Println("AddLpplandtl-error-1: ", err)
 
- 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
 
- 		for _, lppand := range lpplandList {
 
- 			if lppand.Id == parammaps.Get("id").ValueInt64() {
 
- 				if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
 
- 					appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 					return
 
- 				}
 
- 				exist = true
 
- 				break
 
- 			}
 
- 		}
 
- 	}
 
- 	tmrid := parammaps.Get("tmrid").ValueInt64()
 
- 	if !exist {
 
- 		var sort int64 = -1
 
- 		var maxsort int64
 
- 		for _, lppand := range lpplandList {
 
- 			if maxsort <= lppand.Sort {
 
- 				maxsort = lppand.Sort
 
- 			}
 
- 			if lppand.Barid == parammaps.Get("barid").ValueInt64() {
 
- 				_, err := tx.SQL(" update lpplandtl1 set lweight =lweight +  ?  where id = ?  and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 				if err != nil {
 
- 					log.Println("AddLpplandtl-error-2: ", err)
 
- 					appG.Response(http.StatusOK, e.ERROR, err)
 
- 					return
 
- 				}
 
- 				appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 				return
 
- 			}
 
- 		}
 
- 		for i, lppand := range lpplandList {
 
- 			lppand.Sort = int64(i) + 1
 
- 		}
 
- 		updateLpplandList := make([]*lpplandtl1, 0)
 
- 		for i, lppand := range lpplandList {
 
- 			n := i + 1
 
- 			if lppand.Tmrid == tmrid {
 
- 				sort = int64(n) + 1
 
- 				lppand.Sort = int64(n)
 
- 				updateLpplandList = append(updateLpplandList, lppand)
 
- 			} else if sort > 0 {
 
- 				lppand.Sort = int64(n) + 2
 
- 				updateLpplandList = append(updateLpplandList, lppand)
 
- 			}
 
- 		}
 
- 		tx.Begin()
 
- 		for _, lppand := range updateLpplandList {
 
- 			_, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ?  and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
 
- 			if err != nil {
 
- 				tx.Rollback()
 
- 				log.Println("AddLpplandtl-error-2: ", err)
 
- 				appG.Response(http.StatusOK, e.ERROR, err)
 
- 				return
 
- 			}
 
- 		}
 
- 		if sort < 0 && len(lpplandList) > 0 {
 
- 			sort = maxsort + 1
 
- 		} else if len(lpplandList) == 0 {
 
- 			sort = 1
 
- 		}
 
- 		_, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio) 
 
- 	VALUES (?,?,?,?,?,?,?,?,?,
 
- 	?,?,IF(?='',0,?),IF(?='',0,?))`,
 
- 			parammaps.Get("pastureid").ValueStr(),
 
- 			parammaps.Get("lppid").ValueStr(),
 
- 			parammaps.Get("barid").ValueStr(),
 
- 			parammaps.Get("barname").ValueStr(),
 
- 			parammaps.Get("fpdid").ValueStr(),
 
- 			parammaps.Get("lweight").ValueStr(),
 
- 			sort,
 
- 			parammaps.Get("tmrid").ValueStr(),
 
- 			parammaps.Get("tmrname").ValueStr(),
 
- 			parammaps.Get("fttype").ValueStr(),
 
- 			parammaps.Get("background").ValueStr(),
 
- 			parammaps.Get("cowcount").ValueStr(),
 
- 			parammaps.Get("cowcount").ValueStr(),
 
- 			parammaps.Get("ccountradio").ValueStr(),
 
- 			parammaps.Get("ccountradio").ValueStr()).Execute()
 
- 		if err != nil {
 
- 			tx.Rollback()
 
- 			log.Println("AddLpplandtl-error-3: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, err)
 
- 			return
 
- 		}
 
- 		err = tx.Commit()
 
- 		if err != nil {
 
- 			tx.Rollback()
 
- 			log.Println("AddLpplandtl-error-6: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, err)
 
- 			return
 
- 		}
 
- 	} else {
 
- 		lpplandid := parammaps.Get("id").ValueInt64()
 
- 		// updateLpplandList := make([]*lpplandtl1, 0)
 
- 		var oldsort int64
 
- 		for i, lppand := range lpplandList {
 
- 			lppand.Sort = int64(i) + 1
 
- 		}
 
- 		noup := false
 
- 		for i, lppand := range lpplandList {
 
- 			if lppand.Id == lpplandid {
 
- 				oldsort = lppand.Sort
 
- 				b := false
 
- 				for _, lppand := range lpplandList {
 
- 					if lppand.Tmrid == tmrid {
 
- 						noup = false
 
- 						b = true
 
- 						break
 
- 					}
 
- 				}
 
- 				if !b {
 
- 					if i == 0 || i == len(lpplandList)-1 {
 
- 						noup = true
 
- 					}
 
- 					if i != 0 && i < len(lpplandList)-1 {
 
- 						if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
 
- 							noup = true
 
- 						}
 
- 					}
 
- 					// if lpplandList[i+1].Tmrid == tmrid {
 
- 					// 	_, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ? ", parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 					// 	if err != nil {
 
- 					// 		tx.Rollback()
 
- 					// 		log.Println("AddLpplandtl-error-2: ", err)
 
- 					// 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 					// 		return
 
- 					// 	}
 
- 					// 	tx.Commit()
 
- 					// 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 					// 	return
 
- 					// }
 
- 					// }
 
- 					// break
 
- 					// 	}
 
- 					// }
 
- 				}
 
- 				break
 
- 			}
 
- 		}
 
- 		if noup {
 
- 			_, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ? ", parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 			if err != nil {
 
- 				tx.Rollback()
 
- 				log.Println("AddLpplandtl-error-2: ", err)
 
- 				appG.Response(http.StatusOK, e.ERROR, err)
 
- 				return
 
- 			}
 
- 		} else {
 
- 			for i, lppand := range lpplandList {
 
- 				if lppand.Id == lpplandid {
 
- 					if i != 0 {
 
- 						if lpplandList[i-1].Tmrid == tmrid {
 
- 							_, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ? ", parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 							if err != nil {
 
- 								tx.Rollback()
 
- 								log.Println("AddLpplandtl-error-2: ", err)
 
- 								appG.Response(http.StatusOK, e.ERROR, err)
 
- 								return
 
- 							}
 
- 							tx.Commit()
 
- 							appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 							return
 
- 						}
 
- 					}
 
- 					if i < len(lpplandList)-1 {
 
- 						if lpplandList[i+1].Tmrid == tmrid {
 
- 							_, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ? ", parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 							if err != nil {
 
- 								tx.Rollback()
 
- 								log.Println("AddLpplandtl-error-2: ", err)
 
- 								appG.Response(http.StatusOK, e.ERROR, err)
 
- 								return
 
- 							}
 
- 							tx.Commit()
 
- 							appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 							return
 
- 						}
 
- 					}
 
- 					break
 
- 				}
 
- 			}
 
- 			exist := false
 
- 			sort1 := 0
 
- 			a := false
 
- 			for _, lppand := range lpplandList {
 
- 				if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
 
- 					sort1 = int(lppand.Sort)
 
- 					if !a {
 
- 						for _, lppand1 := range lpplandList {
 
- 							if lppand1.Id == lpplandid {
 
- 								lppand1.Tmrid = tmrid
 
- 								lppand1.Tmrname = lppand.Tmrname
 
- 								a = true
 
- 								// lppand1.Sort = lppand.Sort
 
- 								break
 
- 							}
 
- 						}
 
- 					}
 
- 					exist = true
 
- 				}
 
- 			}
 
- 			if exist {
 
- 				if oldsort > int64(sort1) {
 
- 					for _, lppand := range lpplandList {
 
- 						if lppand.Id == lpplandid {
 
- 							lppand.Sort = int64(sort1) + 1
 
- 							continue
 
- 						}
 
- 						if lppand.Sort >= int64(sort1)+1 {
 
- 							lppand.Sort++
 
- 						}
 
- 					}
 
- 				} else {
 
- 					for _, lppand := range lpplandList {
 
- 						if lppand.Id == lpplandid {
 
- 							lppand.Sort = int64(sort1) + 1
 
- 							continue
 
- 						}
 
- 						if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
 
- 							lppand.Sort--
 
- 						} else if lppand.Sort <= int64(sort1)+1 {
 
- 							lppand.Sort++
 
- 						}
 
- 					}
 
- 				}
 
- 			} else {
 
- 				for _, lppand := range lpplandList {
 
- 					if lppand.Id == lpplandid {
 
- 						lppand.Sort = int64(len(lpplandList)) + 1
 
- 						lppand.Tmrid = tmrid
 
- 						lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
 
- 						break
 
- 					}
 
- 				}
 
- 			}
 
- 			sort.Slice(lpplandList, func(i, j int) bool {
 
- 				return lpplandList[i].Sort < lpplandList[j].Sort // 升序
 
- 			})
 
- 			for i, lppand := range lpplandList {
 
- 				lppand.Sort = int64(i) + 1
 
- 			}
 
- 			for _, lppand := range lpplandList {
 
- 				_, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?,sort = ?   where id = ?  and pastureid = ? ", lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
 
- 				if err != nil {
 
- 					tx.Rollback()
 
- 					log.Println("AddLpplandtl-error-2: ", err)
 
- 					appG.Response(http.StatusOK, e.ERROR, err)
 
- 					return
 
- 				}
 
- 			}
 
- 		}
 
- 		tx.Commit()
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- func UpdateLpplandtlSort(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	parammaps := fsion.Get("parammaps")
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	var err error
 
- 	lpplandList := make([]*lpplandtl1, 0)
 
- 	err = tx.Table("lpplandtl1").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
 
- 		OrderBy("sort").Find(&lpplandList)
 
- 	if err != nil {
 
- 		log.Println("UpdateLpplandtlSort-error-1: ", err)
 
- 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	var oldsort, lpplandtlsort int64
 
- 	tmrid := parammaps.Get("tmrid").ValueInt64()
 
- 	sort := parammaps.Get("sort").ValueInt64()
 
- 	lpplandtlid := parammaps.Get("id").ValueInt64()
 
- 	sort1 := parammaps.Get("sort").ValueInt64()
 
- 	for i, lppland := range lpplandList {
 
- 		lppland.Sort = int64(i) + 1
 
- 	}
 
- 	for _, lppland := range lpplandList {
 
- 		if tmrid == lppland.Tmrid {
 
- 			oldsort = lppland.Sort
 
- 			break
 
- 		}
 
- 	}
 
- 	for _, lppland := range lpplandList {
 
- 		if lppland.Sort == sort {
 
- 			for _, lppland1 := range lpplandList {
 
- 				if lppland.Tmrid == lppland1.Tmrid {
 
- 					sort = lppland1.Sort
 
- 					if oldsort > sort {
 
- 						break
 
- 					}
 
- 				}
 
- 			}
 
- 			break
 
- 		}
 
- 	}
 
- 	if oldsort == sort {
 
- 		appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 		return
 
- 	}
 
- 	tmrLpplandList := make([]*lpplandtl1, 0)
 
- 	for _, lppland := range lpplandList {
 
- 		if lppland.Tmrid == tmrid {
 
- 			tmrLpplandList = append(tmrLpplandList, lppland)
 
- 			if lppland.Id == lpplandtlid {
 
- 				lpplandtlsort = lppland.Sort
 
- 			}
 
- 		}
 
- 	}
 
- 	exist := false
 
- 	for _, tmrLppland := range tmrLpplandList {
 
- 		if sort1 == tmrLppland.Sort {
 
- 			exist = true
 
- 			break
 
- 		}
 
- 	}
 
- 	if exist {
 
- 		for _, tmrLppland := range tmrLpplandList {
 
- 			if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
 
- 				tmrLppland.Sort++
 
- 			} else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
 
- 				tmrLppland.Sort--
 
- 			}
 
- 		}
 
- 		for _, tmrLppland := range tmrLpplandList {
 
- 			if tmrLppland.Id == lpplandtlid {
 
- 				tmrLppland.Sort = sort1
 
- 				break
 
- 			}
 
- 		}
 
- 	} else {
 
- 		if oldsort < sort && len(tmrLpplandList) > 1 {
 
- 			for i, tmrLppland := range tmrLpplandList {
 
- 				tmrLppland.Sort = sort + int64(i) - 1
 
- 			}
 
- 		} else {
 
- 			for i, tmrLppland := range tmrLpplandList {
 
- 				tmrLppland.Sort = sort + int64(i)
 
- 			}
 
- 		}
 
- 	}
 
- 	if oldsort > sort {
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort == sort {
 
- 				// uptmrid = lppland.Tmrid
 
- 				sort = lppland.Sort
 
- 				break
 
- 			}
 
- 		}
 
- 	} else {
 
- 		var uptmrid int64
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort == sort && lppland.Tmrid != tmrid {
 
- 				uptmrid = lppland.Tmrid
 
- 				sort = lppland.Sort
 
- 			} else if uptmrid != 0 && uptmrid == lppland.Tmrid {
 
- 				sort = lppland.Sort
 
- 			}
 
- 		}
 
- 	}
 
- 	upcount := int64(len(tmrLpplandList))
 
- 	if oldsort > sort {
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort >= sort && lppland.Tmrid != tmrid {
 
- 				lppland.Sort += upcount
 
- 				tmrLpplandList = append(tmrLpplandList, lppland)
 
- 			}
 
- 		}
 
- 	} else {
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
 
- 				lppland.Sort -= upcount
 
- 				tmrLpplandList = append(tmrLpplandList, lppland)
 
- 			}
 
- 		}
 
- 	}
 
- 	updateLpplandList := make([]*lpplandtl1, 0)
 
- 	for _, lppland := range lpplandList {
 
- 		exist := false
 
- 		for _, tmrLppland := range tmrLpplandList {
 
- 			if lppland.Id == tmrLppland.Id {
 
- 				exist = true
 
- 				break
 
- 			}
 
- 		}
 
- 		if !exist {
 
- 			updateLpplandList = append(updateLpplandList, lppland)
 
- 		}
 
- 	}
 
- 	tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
 
- 	// tmrLpplandMap := make(map[int64]bool)
 
- 	// for _, item := range tmrLpplandList {
 
- 	// 	tmrLpplandMap[item.Sort] = false
 
- 	// }
 
- 	for _, item := range tmrLpplandList {
 
- 		exist := false
 
- 		for _, item1 := range tmrLpplandList {
 
- 			if item.Sort == item1.Sort && item.Id != item1.Id {
 
- 				exist = true
 
- 			}
 
- 		}
 
- 		if exist {
 
- 			appG.Response(http.StatusOK, e.ERROR, "顺序调整有误,请重新调整!!!")
 
- 			return
 
- 		}
 
- 	}
 
- 	tx.Begin()
 
- 	var wg sync.WaitGroup
 
- 	for _, lppland := range tmrLpplandList {
 
- 		wg.Add(1)
 
- 		go func(lppland *lpplandtl1) {
 
- 			defer wg.Done()
 
- 			_, err = tx.SQL(`update  lpplandtl1 set sort = ?  where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
 
- 			if err != nil {
 
- 				log.Println("UpdateLpplandtlSort-error-2: ", err)
 
- 				tx.Rollback()
 
- 				appG.Response(http.StatusOK, e.ERROR, err)
 
- 				return
 
- 			}
 
- 		}(lppland)
 
- 	}
 
- 	wg.Wait()
 
- 	err = tx.Commit()
 
- 	if err != nil {
 
- 		log.Println("UpdateLpplandtlSort-error-3: ", err)
 
- 		tx.Rollback()
 
- 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- func AddLpplandtlDate(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	parammaps := fsion.Get("parammaps")
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	exist := false
 
- 	var err error
 
- 	lpplandList := make([]*lpplandtl1, 0)
 
- 	err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
 
- 		And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
 
- 	if err != nil {
 
- 		log.Println("AddLpplandtl-error-1: ", err)
 
- 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
 
- 		for _, lppand := range lpplandList {
 
- 			fmt.Println(parammaps.Get("id").ValueInt64())
 
- 			if lppand.Id == parammaps.Get("id").ValueInt64() {
 
- 				if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
 
- 					appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 					return
 
- 				}
 
- 				exist = true
 
- 				break
 
- 			}
 
- 		}
 
- 	}
 
- 	tmrid := parammaps.Get("tmrid").ValueInt64()
 
- 	if !exist {
 
- 		var sort int64 = -1
 
- 		for _, lppand := range lpplandList {
 
- 			if lppand.Barid == parammaps.Get("barid").ValueInt64() {
 
- 				_, err := tx.SQL(" update lpplandtl1date set lweight =lweight + ?  where id = ?  and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 				if err != nil {
 
- 					log.Println("AddLpplandtl-error-2: ", err)
 
- 					appG.Response(http.StatusOK, e.ERROR, err)
 
- 					return
 
- 				}
 
- 				appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 				return
 
- 			}
 
- 		}
 
- 		for i, lppand := range lpplandList {
 
- 			lppand.Sort = int64(i) + 1
 
- 		}
 
- 		updateLpplandList := make([]*lpplandtl1, 0)
 
- 		for i, lppand := range lpplandList {
 
- 			n := i + 1
 
- 			if lppand.Tmrid == tmrid {
 
- 				sort = int64(n) + 1
 
- 				lppand.Sort = int64(n)
 
- 				updateLpplandList = append(updateLpplandList, lppand)
 
- 			} else if sort > 0 {
 
- 				lppand.Sort = int64(n) + 2
 
- 				updateLpplandList = append(updateLpplandList, lppand)
 
- 			}
 
- 		}
 
- 		tx.Begin()
 
- 		for _, lppand := range updateLpplandList {
 
- 			_, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ?  and pastureid = ? and date = ? ", lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
 
- 			if err != nil {
 
- 				tx.Rollback()
 
- 				log.Println("AddLpplandtl-error-2: ", err)
 
- 				appG.Response(http.StatusOK, e.ERROR, err)
 
- 				return
 
- 			}
 
- 		}
 
- 		if sort < 0 && len(lpplandList) > 0 {
 
- 			sort = int64(len(lpplandList) + 1)
 
- 		} else if len(lpplandList) == 0 {
 
- 			sort = 1
 
- 		}
 
- 		_, err = tx.SQL(`INSERT INTO lpplandtl1date(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio,date) 
 
- 	VALUES (?,?,?,?,?,?,?,?,?,
 
- 	?,?,IF(?='',0,?),IF(?='',0,?),?)`,
 
- 			parammaps.Get("pastureid").ValueStr(),
 
- 			parammaps.Get("lppid").ValueStr(),
 
- 			parammaps.Get("barid").ValueStr(),
 
- 			parammaps.Get("barname").ValueStr(),
 
- 			parammaps.Get("fpdid").ValueStr(),
 
- 			parammaps.Get("lweight").ValueStr(),
 
- 			sort,
 
- 			parammaps.Get("tmrid").ValueStr(),
 
- 			parammaps.Get("tmrname").ValueStr(),
 
- 			parammaps.Get("fttype").ValueStr(),
 
- 			parammaps.Get("background").ValueStr(),
 
- 			parammaps.Get("cowcount").ValueStr(),
 
- 			parammaps.Get("cowcount").ValueStr(),
 
- 			parammaps.Get("ccountradio").ValueStr(),
 
- 			parammaps.Get("ccountradio").ValueStr(),
 
- 			parammaps.Get("date").ValueStr()).Execute()
 
- 		if err != nil {
 
- 			tx.Rollback()
 
- 			log.Println("AddLpplandtl-error-3: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, err)
 
- 			return
 
- 		}
 
- 		err = tx.Commit()
 
- 		if err != nil {
 
- 			tx.Rollback()
 
- 			log.Println("AddLpplandtl-error-6: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, err)
 
- 			return
 
- 		}
 
- 	} else {
 
- 		lpplandid := parammaps.Get("id").ValueInt64()
 
- 		// updateLpplandList := make([]*lpplandtl1, 0)
 
- 		var oldsort int64
 
- 		for i, lppand := range lpplandList {
 
- 			lppand.Sort = int64(i) + 1
 
- 		}
 
- 		noup := false
 
- 		for i, lppand := range lpplandList {
 
- 			if lppand.Id == lpplandid {
 
- 				oldsort = lppand.Sort
 
- 				b := false
 
- 				for _, lppand := range lpplandList {
 
- 					if lppand.Tmrid == tmrid {
 
- 						noup = false
 
- 						b = true
 
- 						break
 
- 					}
 
- 				}
 
- 				if !b {
 
- 					if i == 0 || i == len(lpplandList)-1 {
 
- 						noup = true
 
- 					}
 
- 					if i != 0 && i < len(lpplandList)-1 {
 
- 						if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
 
- 							noup = true
 
- 						}
 
- 					}
 
- 				}
 
- 				break
 
- 			}
 
- 		}
 
- 		if noup {
 
- 			_, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ? and date = ? ",
 
- 				parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
 
- 			if err != nil {
 
- 				tx.Rollback()
 
- 				log.Println("AddLpplandtl-error-2: ", err)
 
- 				appG.Response(http.StatusOK, e.ERROR, err)
 
- 				return
 
- 			}
 
- 		} else {
 
- 			for i, lppand := range lpplandList {
 
- 				if lppand.Id == lpplandid {
 
- 					if i != 0 {
 
- 						if lpplandList[i-1].Tmrid == tmrid {
 
- 							_, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ?  and date = ?  ",
 
- 								parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
 
- 							if err != nil {
 
- 								tx.Rollback()
 
- 								log.Println("AddLpplandtl-error-2: ", err)
 
- 								appG.Response(http.StatusOK, e.ERROR, err)
 
- 								return
 
- 							}
 
- 							tx.Commit()
 
- 							appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 							return
 
- 						}
 
- 					}
 
- 					if i < len(lpplandList)-1 {
 
- 						if lpplandList[i+1].Tmrid == tmrid {
 
- 							_, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?   where id = ?  and pastureid = ?  and date = ? ", parammaps.Get("tmrid").ValueStr(),
 
- 								parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
 
- 							if err != nil {
 
- 								tx.Rollback()
 
- 								log.Println("AddLpplandtl-error-2: ", err)
 
- 								appG.Response(http.StatusOK, e.ERROR, err)
 
- 								return
 
- 							}
 
- 							tx.Commit()
 
- 							appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 							return
 
- 						}
 
- 					}
 
- 					break
 
- 				}
 
- 			}
 
- 			exist := false
 
- 			sort1 := 0
 
- 			a := false
 
- 			for _, lppand := range lpplandList {
 
- 				if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
 
- 					sort1 = int(lppand.Sort)
 
- 					if !a {
 
- 						for _, lppand1 := range lpplandList {
 
- 							if lppand1.Id == lpplandid {
 
- 								lppand1.Tmrid = tmrid
 
- 								lppand1.Tmrname = lppand.Tmrname
 
- 								a = true
 
- 								// lppand1.Sort = lppand.Sort
 
- 								break
 
- 							}
 
- 						}
 
- 					}
 
- 					exist = true
 
- 				}
 
- 			}
 
- 			if exist {
 
- 				if oldsort > int64(sort1) {
 
- 					for _, lppand := range lpplandList {
 
- 						if lppand.Id == lpplandid {
 
- 							lppand.Sort = int64(sort1) + 1
 
- 							continue
 
- 						}
 
- 						if lppand.Sort >= int64(sort1)+1 {
 
- 							lppand.Sort++
 
- 						}
 
- 					}
 
- 				} else {
 
- 					for _, lppand := range lpplandList {
 
- 						if lppand.Id == lpplandid {
 
- 							lppand.Sort = int64(sort1) + 1
 
- 							continue
 
- 						}
 
- 						// if lppand.Sort > oldsort {
 
- 						// 	lppand.Sort--
 
- 						// }
 
- 						if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
 
- 							lppand.Sort--
 
- 						} else if lppand.Sort <= int64(sort1)+1 {
 
- 							lppand.Sort++
 
- 						}
 
- 					}
 
- 				}
 
- 			} else {
 
- 				for _, lppand := range lpplandList {
 
- 					if lppand.Id == lpplandid {
 
- 						lppand.Sort = int64(len(lpplandList)) + 1
 
- 						lppand.Tmrid = tmrid
 
- 						lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
 
- 						break
 
- 					}
 
- 				}
 
- 			}
 
- 			sort.Slice(lpplandList, func(i, j int) bool {
 
- 				return lpplandList[i].Sort < lpplandList[j].Sort // 升序
 
- 			})
 
- 			for i, lppand := range lpplandList {
 
- 				lppand.Sort = int64(i) + 1
 
- 			}
 
- 			for _, lppand := range lpplandList {
 
- 				_, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?,sort = ?   where id = ?  and pastureid = ?  and date = ? ",
 
- 					lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
 
- 				if err != nil {
 
- 					tx.Rollback()
 
- 					log.Println("AddLpplandtl-error-2: ", err)
 
- 					appG.Response(http.StatusOK, e.ERROR, err)
 
- 					return
 
- 				}
 
- 			}
 
- 		}
 
- 		tx.Commit()
 
- 		// lpplandid := parammaps.Get("id").ValueInt64()
 
- 		// updateLpplandList := make([]*lpplandtl1, 0)
 
- 		// var oldsort int64
 
- 		// for i, lppand := range lpplandList {
 
- 		// 	lppand.Sort = int64(i) + 1
 
- 		// }
 
- 		// for _, lppand := range lpplandList {
 
- 		// 	if lppand.Id == lpplandid {
 
- 		// 		oldsort = lppand.Sort
 
- 		// 		break
 
- 		// 	}
 
- 		// }
 
- 		// for _, lppand := range lpplandList {
 
- 		// 	if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
 
- 		// 		sort = lppand.Sort
 
- 		// 		break
 
- 		// 	}
 
- 		// }
 
- 		// if sort == -1 {
 
- 		// 	sort = 1
 
- 		// }
 
- 		// if oldsort < sort {
 
- 		// 	for _, lppand := range lpplandList {
 
- 		// 		if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
 
- 		// 			lppand.Sort--
 
- 		// 			updateLpplandList = append(updateLpplandList, lppand)
 
- 		// 		} else if lppand.Sort > sort && lppand.Id != lpplandid {
 
- 		// 			lppand.Sort++
 
- 		// 			updateLpplandList = append(updateLpplandList, lppand)
 
- 		// 		}
 
- 		// 	}
 
- 		// 	sort--
 
- 		// } else {
 
- 		// 	for _, lppand := range lpplandList {
 
- 		// 		if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
 
- 		// 			lppand.Sort++
 
- 		// 			updateLpplandList = append(updateLpplandList, lppand)
 
- 		// 		}
 
- 		// 	}
 
- 		// }
 
- 		// var tmrlpplandList []*lpplandtl1
 
- 		// for _, lppand := range lpplandList {
 
- 		// 	exist := false
 
- 		// 	for _, lppand1 := range updateLpplandList {
 
- 		// 		if lppand.Id == lppand1.Id {
 
- 		// 			exist = true
 
- 		// 			break
 
- 		// 		}
 
- 		// 	}
 
- 		// 	if !exist {
 
- 		// 		tmrlpplandList = append(tmrlpplandList, lppand)
 
- 		// 	}
 
- 		// }
 
- 		// updateLpplandList = append(updateLpplandList, tmrlpplandList...)
 
- 		// if len(updateLpplandList) > 0 {
 
- 		// 	tx.Begin()
 
- 		// 	for _, lppand := range updateLpplandList {
 
- 		// 		_, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ?  and pastureid = ?  ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
 
- 		// 		if err != nil {
 
- 		// 			tx.Rollback()
 
- 		// 			log.Println("AddLpplandtl-error-2: ", err)
 
- 		// 			appG.Response(http.StatusOK, e.ERROR, err)
 
- 		// 			return
 
- 		// 		}
 
- 		// 	}
 
- 		// 	_, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?,sort = ?  where id = ?  and pastureid = ?  ", parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), sort, lpplandid, parammaps.Get("pastureid").ValueStr()).Execute()
 
- 		// 	if err != nil {
 
- 		// 		tx.Rollback()
 
- 		// 		log.Println("AddLpplandtl-error-2: ", err)
 
- 		// 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		// 		return
 
- 		// 	}
 
- 		// 	tx.Commit()
 
- 		// }
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- func UpdateLpplandtlDateSort(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	parammaps := fsion.Get("parammaps")
 
- 	date := parammaps.Get("date").ValueStr()
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	var err error
 
- 	lpplandList := make([]*lpplandtl1, 0)
 
- 	err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
 
- 		And("date = ? ", date).OrderBy("sort").Find(&lpplandList)
 
- 	if err != nil {
 
- 		log.Println("UpdateLpplandtlSort-error-1: ", err)
 
- 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	var oldsort, lpplandtlsort int64
 
- 	tmrid := parammaps.Get("tmrid").ValueInt64()
 
- 	sort := parammaps.Get("sort").ValueInt64()
 
- 	lpplandtlid := parammaps.Get("id").ValueInt64()
 
- 	sort1 := parammaps.Get("sort").ValueInt64()
 
- 	for i, lppland := range lpplandList {
 
- 		lppland.Sort = int64(i) + 1
 
- 	}
 
- 	for _, lppland := range lpplandList {
 
- 		if tmrid == lppland.Tmrid {
 
- 			oldsort = lppland.Sort
 
- 			break
 
- 		}
 
- 	}
 
- 	for _, lppland := range lpplandList {
 
- 		if lppland.Sort == sort {
 
- 			for _, lppland1 := range lpplandList {
 
- 				if lppland.Tmrid == lppland1.Tmrid {
 
- 					sort = lppland1.Sort
 
- 					if oldsort > sort {
 
- 						break
 
- 					}
 
- 				}
 
- 			}
 
- 			break
 
- 		}
 
- 	}
 
- 	if oldsort == sort {
 
- 		appG.Response(http.StatusOK, e.SUCCESS, true)
 
- 		return
 
- 	}
 
- 	tmrLpplandList := make([]*lpplandtl1, 0)
 
- 	for _, lppland := range lpplandList {
 
- 		if lppland.Tmrid == tmrid {
 
- 			tmrLpplandList = append(tmrLpplandList, lppland)
 
- 			if lppland.Id == lpplandtlid {
 
- 				lpplandtlsort = lppland.Sort
 
- 			}
 
- 		}
 
- 	}
 
- 	exist := false
 
- 	for _, tmrLppland := range tmrLpplandList {
 
- 		if sort1 == tmrLppland.Sort {
 
- 			exist = true
 
- 			break
 
- 		}
 
- 	}
 
- 	if exist {
 
- 		for _, tmrLppland := range tmrLpplandList {
 
- 			if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
 
- 				tmrLppland.Sort++
 
- 			} else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
 
- 				tmrLppland.Sort--
 
- 			}
 
- 		}
 
- 		for _, tmrLppland := range tmrLpplandList {
 
- 			if tmrLppland.Id == lpplandtlid {
 
- 				tmrLppland.Sort = sort1
 
- 				break
 
- 			}
 
- 		}
 
- 	} else {
 
- 		if oldsort < sort && len(tmrLpplandList) > 1 {
 
- 			for i, tmrLppland := range tmrLpplandList {
 
- 				tmrLppland.Sort = sort + int64(i) - 1
 
- 			}
 
- 		} else {
 
- 			for i, tmrLppland := range tmrLpplandList {
 
- 				tmrLppland.Sort = sort + int64(i)
 
- 			}
 
- 		}
 
- 	}
 
- 	if oldsort > sort {
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort == sort {
 
- 				// uptmrid = lppland.Tmrid
 
- 				sort = lppland.Sort
 
- 				break
 
- 			}
 
- 		}
 
- 	} else {
 
- 		var uptmrid int64
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort == sort && lppland.Tmrid != tmrid {
 
- 				uptmrid = lppland.Tmrid
 
- 				sort = lppland.Sort
 
- 			} else if uptmrid != 0 && uptmrid == lppland.Tmrid {
 
- 				sort = lppland.Sort
 
- 			}
 
- 		}
 
- 	}
 
- 	upcount := int64(len(tmrLpplandList))
 
- 	if oldsort > sort {
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort >= sort && lppland.Tmrid != tmrid {
 
- 				lppland.Sort += upcount
 
- 				tmrLpplandList = append(tmrLpplandList, lppland)
 
- 			}
 
- 		}
 
- 	} else {
 
- 		for _, lppland := range lpplandList {
 
- 			if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
 
- 				lppland.Sort -= upcount
 
- 				tmrLpplandList = append(tmrLpplandList, lppland)
 
- 			}
 
- 		}
 
- 	}
 
- 	updateLpplandList := make([]*lpplandtl1, 0)
 
- 	for _, lppland := range lpplandList {
 
- 		exist := false
 
- 		for _, tmrLppland := range tmrLpplandList {
 
- 			if lppland.Id == tmrLppland.Id {
 
- 				exist = true
 
- 				break
 
- 			}
 
- 		}
 
- 		if !exist {
 
- 			updateLpplandList = append(updateLpplandList, lppland)
 
- 		}
 
- 	}
 
- 	tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
 
- 	tx.Begin()
 
- 	// _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
 
- 	var wg sync.WaitGroup
 
- 	for _, lppland := range tmrLpplandList {
 
- 		wg.Add(1)
 
- 		go func(lppland *lpplandtl1) {
 
- 			defer wg.Done()
 
- 			_, err = tx.SQL(`update  lpplandtl1date set sort = ?  where id = ? and pastureid = ? and date = ? `, lppland.Sort, lppland.Id, lppland.Pastureid, date).Execute()
 
- 			if err != nil {
 
- 				log.Println("UpdateLpplandtlSort-error-2: ", err)
 
- 				tx.Rollback()
 
- 				appG.Response(http.StatusOK, e.ERROR, err)
 
- 				return
 
- 			}
 
- 		}(lppland)
 
- 	}
 
- 	wg.Wait()
 
- 	err = tx.Commit()
 
- 	if err != nil {
 
- 		log.Println("UpdateLpplandtlSort-error-3: ", err)
 
- 		tx.Rollback()
 
- 		appG.Response(http.StatusOK, e.ERROR, err)
 
- 		return
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- func GetFeedpUndistributed(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	dataByte, _ := ioutil.ReadAll(c.Request.Body)
 
- 	fsion := gofasion.NewFasion(string(dataByte))
 
- 	timesMax := fsion.Get("times").ValueInt()
 
- 	pastureid := fsion.Get("pastureid").ValueStr()
 
- 	data := make(map[string]interface{})
 
- 	for times := 1; times <= timesMax; times++ {
 
- 		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
 
- on ft.id = feedp.ftid and  ft.pastureid=?
 
- WHERE feedp.pastureid = ? 
 
- GROUP BY feedp.ftid
 
- 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
 
- inner join  feedtemplet ft
 
- 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`
 
- 		ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
 
- 		if err != nil {
 
- 			log.Println("GetFeedpUndistributed-error-1: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		ftDetailList, err := tx.SQL(`SELECT TRIM(id) id,times,tratio,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,0 AS fttype,cowcount,ccountradio,1 isfill  FROM fpdetail
 
- WHERE fpdetail.pastureid = ?  AND times=? AND  ptsuse< ROUND(ptsrate*weight,2) 
 
- UNION
 
- SELECT TRIM(id) id,times,tratio,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,1 AS fttype,cowcount,ccountradio,0 isfill  FROM fpdetail
 
- WHERE fpdetail.pastureid = ?  AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
 
- ORDER BY  barid`, pastureid, times, pastureid, times).Query().List()
 
- 		if err != nil {
 
- 			log.Println("GetFeedpUndistributed-error-2: ", err)
 
- 			appG.Response(http.StatusOK, e.ERROR, nil)
 
- 			return
 
- 		}
 
- 		for _, ft := range ftList {
 
- 			arrList := make([]map[string]interface{}, 0)
 
- 			arrid := ft["arrid"].(string)
 
- 			for _, detail := range ftDetailList {
 
- 				if _, ok := detail["ptid"]; ok {
 
- 					if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
 
- 						detail["background"] = ft["background"]
 
- 						arrList = append(arrList, detail)
 
- 					}
 
- 				} else if _, ok := detail["ptsid"]; ok {
 
- 					if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
 
- 						detail["background"] = ft["background"]
 
- 						arrList = append(arrList, detail)
 
- 					}
 
- 				}
 
- 			}
 
- 			if len(arrList) > 0 {
 
- 				ft["arrList"] = arrList
 
- 			}
 
- 		}
 
- 		var key string
 
- 		if times == 1 {
 
- 			key = "one"
 
- 		} else if times == 2 {
 
- 			key = "two"
 
- 		} else if times == 3 {
 
- 			key = "three"
 
- 		} else if times == 4 {
 
- 			key = "four"
 
- 		} else if times == 5 {
 
- 			key = "five"
 
- 		} else {
 
- 			key = "six"
 
- 		}
 
- 		data[key] = ftList
 
- 	}
 
- 	appG.Response(http.StatusOK, e.SUCCESS, data)
 
- }
 
 
  |