spillage.go 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. "../../pkg/app"
  15. "../../pkg/e"
  16. "../../pkg/logging"
  17. "../../pkg/setting"
  18. "../../routers/restful"
  19. "github.com/Anderson-Lu/gofasion/gofasion"
  20. "github.com/gin-gonic/gin"
  21. "github.com/xormplus/xorm"
  22. )
  23. type feedtemplet struct {
  24. Id int64 `xorm:"id"`
  25. Ccname string `xorm:"ccname"`
  26. Cname string `xorm:"tname"`
  27. }
  28. type tmrStruct struct {
  29. Id int64 `xorm:"id"`
  30. Tname string `xorm:"tname"`
  31. Maxstirfeed float64 `xorm:"maxstirfeed"`
  32. }
  33. type lpplanStruct struct {
  34. Id int64 `xorm:"id"`
  35. Tmrname string `xorm:"tmrname"`
  36. Tmrid int64 `xorm:"tmrid"`
  37. Sort int64 `xorm:"sort"`
  38. Sel int64 `xorm:"sel"`
  39. Times int64 `xorm:"times"`
  40. Ftname string `xorm:"ftname"`
  41. Ftid int64 `xorm:"ftid"`
  42. Maxweight float64 `xorm:"maxweight"`
  43. Sumweight float64 `xorm:"sumweight"`
  44. Sumcowcount int64 `xorm:"sumcowcount"`
  45. Pastureid string `xorm:"pastureid"`
  46. Display string `xorm:"display"`
  47. Issplit int64 `xorm:"issplit"`
  48. BeginTime string `xorm:"begintime"`
  49. }
  50. type sysopt struct {
  51. Inforvalue int `xorm:"inforvalue"`
  52. }
  53. type lpplanQueryInfo struct {
  54. Pastureid int64 `xorm:"pastureid"`
  55. Lppid int64 `xorm:"id"`
  56. Maxweight float64 `xorm:"maxweight"`
  57. Lweight float64 `xorm:"lweight"`
  58. Lweighthis float64 `xorm:"lweighthis"`
  59. Barid int64 `xorm:"barid"`
  60. Barname string `xorm:"barname"`
  61. Sort int64 `xorm:"sort"`
  62. Tmrid int64 `xorm:"tmrid"`
  63. Tmrname string `xorm:"tmrname"`
  64. Cowcount int64 `xorm:"cowcount"`
  65. Ccountradio float64 `xorm:"ccountradio"`
  66. Background string `xorm:"background"`
  67. Times int64 `xorm:"times"`
  68. Ftid int64 `xorm:"ftid"`
  69. }
  70. type fpdetailQueryInfo struct {
  71. Fpdid int64 `xorm:"fpdid"`
  72. Ptsrate float64 `xorm:"ptsrate"`
  73. Ptsuse float64 `xorm:"ptsuse"`
  74. Ptuse float64 `xorm:"ptuse"`
  75. Weight float64 `xorm:"weight"`
  76. Barid int64 `xorm:"barid"`
  77. Barname string `xorm:"barname"`
  78. Cowcount int64 `xorm:"cowcount"`
  79. Ccountradio float64 `xorm:"ccountradio"`
  80. Fttypeid int64 `xorm:"fttypeid"`
  81. Times int64 `xorm:"times"`
  82. Ptid int64 `xorm:"ptid"`
  83. Background string `xorm:"background"`
  84. }
  85. type lpplandtl1 struct {
  86. Id int64 `xorm:"id"`
  87. Pastureid int64 `xorm:"pastureid"`
  88. Lppid int64 `xorm:"lppid"`
  89. Barid int64 `xorm:"barid"`
  90. Barname string `xorm:"barname"`
  91. Fpdid int64 `xorm:"fpdid"`
  92. Fttype int64 `xorm:"fttype"`
  93. Lweight float64 `xorm:"lweight"`
  94. Sort int64 `xorm:"sort"`
  95. Tmrid int64 `xorm:"tmrid"`
  96. Tmrname string `xorm:"tmrname"`
  97. Cowcount int64 `xorm:"cowcount"`
  98. Ccountradio float64 `xorm:"ccountradio"`
  99. Background string `xorm:"background"`
  100. Lweighthis float64 `xorm:"lweighthis"`
  101. }
  102. //撒料计划 自动生成
  103. func Autogeneration(c *gin.Context) {
  104. appG := app.Gin{C: c}
  105. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  106. fsion := gofasion.NewFasion(string(dataByte))
  107. pastureid := fsion.Get("pastureid").ValueStr()
  108. typeIN := fsion.Get("type").ValueInt64()
  109. log.Println(time.Now())
  110. tx := restful.Engine.NewSession()
  111. defer tx.Close()
  112. var times int
  113. var wg sync.WaitGroup
  114. var tmrlist *[]*tmrStruct
  115. var feedtempletlist *[]*feedtemplet
  116. var err error
  117. var lpplanlist *[]*lpplanList
  118. var fpdetailWeightList *[]*fpdetailWeight
  119. wg.Add(1)
  120. go func() {
  121. defer wg.Done()
  122. feedtempletlist, err = getfeedtemplet(pastureid, tx)
  123. if err != nil {
  124. appG.Response(http.StatusOK, e.ERROR, false)
  125. return
  126. }
  127. }()
  128. wg.Wait()
  129. wg.Add(1)
  130. go func() {
  131. defer wg.Done()
  132. tmrlist, err = gettmr(pastureid, tx)
  133. if err != nil {
  134. appG.Response(http.StatusOK, e.ERROR, false)
  135. return
  136. }
  137. }()
  138. wg.Wait()
  139. wg.Add(1)
  140. go func() {
  141. defer wg.Done()
  142. times, err = gettimes(pastureid, tx)
  143. if err != nil {
  144. appG.Response(http.StatusOK, e.ERROR, false)
  145. return
  146. }
  147. }()
  148. wg.Wait()
  149. wg.Add(1)
  150. go func() {
  151. defer wg.Done()
  152. lpplanlist, err = getLpplanlist(pastureid, tx)
  153. if err != nil {
  154. appG.Response(http.StatusOK, e.ERROR, false)
  155. return
  156. }
  157. }()
  158. wg.Wait()
  159. wg.Add(1)
  160. go func() {
  161. defer wg.Done()
  162. fpdetailWeightList, err = getFpdetail(pastureid, tx)
  163. if err != nil {
  164. appG.Response(http.StatusOK, e.ERROR, false)
  165. return
  166. }
  167. for _, fpd := range *fpdetailWeightList {
  168. for _, lpp := range *lpplanlist {
  169. if (lpp.Ftid == fpd.Ptid || lpp.Ftid == fpd.Ptsid) && lpp.Times == fpd.Times {
  170. fpd.Weight = fpd.Weight - (lpp.Maxweight - lpp.Lweight)
  171. }
  172. }
  173. }
  174. }()
  175. wg.Wait()
  176. // 避免车次数量为0造成死循环
  177. max := false
  178. for _, tmr := range *tmrlist {
  179. if tmr.Maxstirfeed > 0 {
  180. max = true
  181. break
  182. }
  183. }
  184. for i := 1; i <= times; i++ {
  185. count, err := getLpplanCount(tx, i, pastureid)
  186. if err != nil {
  187. appG.Response(http.StatusOK, e.ERROR, false)
  188. return
  189. }
  190. for _, tem := range *feedtempletlist {
  191. for _, fpd := range *fpdetailWeightList {
  192. if tem.Id == fpd.Temid && i == int(fpd.Times) {
  193. var ftid int64
  194. if fpd.Ptid != 0 {
  195. ftid = fpd.Ptid
  196. } else {
  197. ftid = fpd.Ptsid
  198. }
  199. for fpd.Weight > 0 && max {
  200. for _, tmr := range *tmrlist {
  201. if tmr.Maxstirfeed > 0 {
  202. count++
  203. ids, err := setting.SnowIds.NextId()
  204. if err != nil {
  205. ids = time.Now().UnixNano()
  206. logging.Info("create SnowIds err", err)
  207. }
  208. display := tem.Cname
  209. if i == 1 {
  210. display += "第一班"
  211. } else if i == 2 {
  212. display += "第二班"
  213. } else if i == 3 {
  214. display += "第三班"
  215. } else if i == 4 {
  216. display += "第四班"
  217. }
  218. lpplan := &lpplanStruct{
  219. Id: ids,
  220. Tmrname: tmr.Tname,
  221. Tmrid: tmr.Id,
  222. Sort: count,
  223. Sel: 1,
  224. Times: int64(i),
  225. Ftname: tem.Cname,
  226. Ftid: ftid,
  227. Maxweight: tmr.Maxstirfeed,
  228. Sumweight: 0,
  229. Sumcowcount: 0,
  230. Pastureid: pastureid,
  231. Display: display,
  232. }
  233. err = addLpplan(lpplan, tx)
  234. if err != nil {
  235. appG.Response(http.StatusOK, e.ERROR, false)
  236. return
  237. }
  238. fpd.Weight = fpd.Weight - tmr.Maxstirfeed
  239. }
  240. if fpd.Weight < 0 {
  241. break
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. // wg.Wait()
  250. err = autogeneration(typeIN, pastureid, tx)
  251. if err != nil {
  252. appG.Response(http.StatusOK, e.ERROR, false)
  253. return
  254. }
  255. appG.Response(http.StatusOK, e.SUCCESS, true)
  256. }
  257. //查看配方模板
  258. func getfeedtemplet(pastureid string, tx *xorm.Session) (*[]*feedtemplet, error) {
  259. data := make([]*feedtemplet, 0)
  260. err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
  261. if err != nil {
  262. log.Println("getfeedtemplet-error", err)
  263. return nil, err
  264. }
  265. return &data, nil
  266. }
  267. //查看tmr设备
  268. func gettmr(pastureid string, tx *xorm.Session) (*[]*tmrStruct, error) {
  269. data := make([]*tmrStruct, 0)
  270. session := tx.Table("tmr")
  271. session.Select("TRIM(id) id,tname,maxstirfeed")
  272. session.Where("pastureid = ? ", pastureid)
  273. session.Where("enable = 1")
  274. session.In("tclassid", "1,2,3")
  275. err := session.Find(&data)
  276. if err != nil {
  277. log.Println("gettmr-error", err)
  278. return nil, err
  279. }
  280. return &data, nil
  281. }
  282. //判断计划种是否有该车次
  283. func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
  284. session := tx.Table("lpplan")
  285. session.Where("pastureid = ? ", pastureid)
  286. session.Where("times = ? ", times)
  287. session.Where("tmrid = ? ", tmrid)
  288. session.Where("ftid = ? ", ftid)
  289. exist, err := session.Exist()
  290. if err != nil {
  291. log.Println("getLpplanExist-error", err)
  292. return false, err
  293. }
  294. return exist, nil
  295. }
  296. type fpdetailWeight struct {
  297. Times int64 `xorm:"times"`
  298. Weight float64 `xorm:"weight"`
  299. Pastureid int64 `xorm:"pastureid"`
  300. Ptid int64 `xorm:"ptid"`
  301. Ptsid int64 `xorm:"ptsid"`
  302. Cowcount int64 `xorm:"cowcount"`
  303. Temid int64 `xorm:"Temid"`
  304. }
  305. //获取料数量
  306. func getFpdetail(pastureid string, tx *xorm.Session) (*[]*fpdetailWeight, error) {
  307. fpdetaillist := make([]*fpdetailWeight, 0)
  308. 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
  309. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  310. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  311. WHERE fpd.pastureid = ? AND fpd.ptuse<ROUND((1-ifnull(fpd.ptsrate,0))*fpd.weight,2) and fpd.ptid > 0
  312. group by fpd.id,fpd.times) t1
  313. 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 ,
  314. 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 ,
  315. cowcount FROM fpdetail fpd
  316. join feedp fp on fp.ptsfid = fpd.ptsid and fp.pastureid = fpd.pastureid
  317. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  318. WHERE fpd.pastureid = ? AND fpd.ptsuse< ROUND(ifnull(fpd.ptsrate,0)*fpd.weight,2) and fpd.ptsid > 0
  319. group by fpd.id,fpd.times) t1
  320. group by t1.temid,t1.times) t2 on t2.temid = t1.temid and t2.times = t1.times
  321. group by t1.temid,t1.times
  322. `, pastureid, pastureid).Find(&fpdetaillist)
  323. if err != nil {
  324. log.Println("getFpdetail-error", err)
  325. tx.Rollback()
  326. return nil, err
  327. }
  328. return &fpdetaillist, nil
  329. }
  330. type lpplanList struct {
  331. Tmrid int64 `xorm:"tmrid"`
  332. Times int64 `xorm:"times"`
  333. Ftid int64 `xorm:"ftid"`
  334. Maxweight float64 `xorm:"maxweight"`
  335. Lweight float64 `xorm:"lweight"`
  336. }
  337. func getLpplanlist(patsureid string, tx *xorm.Session) (*[]*lpplanList, error) {
  338. session := tx.SQL(` select t1.times,t1.tmrid,t1.ftid,t1.maxweight ,ifnull(t2.lweight,0)as lweight from lpplan t1
  339. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  340. where t1.pastureid = ? `, patsureid, patsureid)
  341. data := make([]*lpplanList, 0)
  342. err := session.Find(&data)
  343. if err != nil {
  344. log.Println("getLpplanlist-error", err)
  345. return nil, err
  346. }
  347. return &data, nil
  348. }
  349. // 添加车次计划
  350. func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
  351. _, err := tx.Table("lpplan").Insert(lpplan)
  352. if err != nil {
  353. log.Println("addLpplan-error", err)
  354. return err
  355. }
  356. return nil
  357. }
  358. //获取车次数量
  359. func getLpplanCount(tx *xorm.Session, times int, pastureid string) (int64, error) {
  360. count, err := tx.Table("lpplan").Where("times = ?", times).And("pastureid = ?", pastureid).Count()
  361. if err != nil {
  362. log.Println("getLpplanCount-error", err)
  363. return 0, err
  364. }
  365. return count, err
  366. }
  367. //获取班次信息
  368. func gettimes(pastureid string, tx *xorm.Session) (int, error) {
  369. session := tx.Table("sysopt")
  370. session.Where("pastureid = ? ", pastureid)
  371. session.Where("inforname = 'times' ")
  372. data := new(sysopt)
  373. _, err := session.Get(data)
  374. if err != nil {
  375. log.Println("gettimes-error", err)
  376. return 0, err
  377. }
  378. return data.Inforvalue, nil
  379. }
  380. type upfpdetail struct {
  381. insertlweight float64
  382. fttypeid int64
  383. fpdid int64
  384. }
  385. func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
  386. tx.Begin()
  387. if typeIN == 1 {
  388. _, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
  389. if err != nil {
  390. log.Println("autogeneration-error-1", err)
  391. tx.Rollback()
  392. return err
  393. }
  394. _, err = tx.SQL(`update fpdetail
  395. set ptuse = 0,ptsuse=0
  396. WHERE pastureid = ?`, pastureid).Execute()
  397. if err != nil {
  398. log.Println("autogeneration-error-2", err)
  399. tx.Rollback()
  400. return err
  401. }
  402. }
  403. lpplansession := tx.SQL(`select
  404. t1.sort,t1.tmrid,t1.tmrname,t1.ftid,
  405. t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight,t1.times from lpplan t1
  406. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  407. where t1.pastureid = ? and t1.maxweight > ifnull(t2.lweight,0) `, pastureid, pastureid)
  408. lpplanlist := make([]*lpplanQueryInfo, 0)
  409. err := lpplansession.Find(&lpplanlist)
  410. if err != nil {
  411. log.Println("autogeneration-error-3", err)
  412. tx.Rollback()
  413. return err
  414. }
  415. fpdetaillist := make([]*fpdetailQueryInfo, 0)
  416. 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 ,
  417. (select bname from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,cowcount,ccountradio,0 isfill FROM fpdetail fpd
  418. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  419. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  420. WHERE fpd.pastureid = ? AND ptuse<ROUND((1-ifnull(fpd.ptsrate,0))*fpd.weight,2)
  421. group by fpd.id , fpd.times `, pastureid).Find(&fpdetaillist)
  422. if err != nil {
  423. log.Println("autogeneration-error-4", err)
  424. tx.Rollback()
  425. return err
  426. }
  427. log.Println(time.Now(), "333333333333")
  428. var wg sync.WaitGroup
  429. for _, lpplan := range lpplanlist {
  430. lpplandtl1List := make([]*lpplandtl1, 0)
  431. upfpdetailList := make([]*upfpdetail, 0)
  432. for _, fpdetail := range fpdetaillist {
  433. if fpdetail.Ptid == lpplan.Ftid && fpdetail.Times == lpplan.Times {
  434. var weight float64
  435. weight = fpdetail.Weight
  436. if lpplan.Maxweight <= lpplan.Lweight || weight <= 0 {
  437. continue
  438. }
  439. var insertlweight float64
  440. if lpplan.Maxweight-lpplan.Lweight > weight {
  441. insertlweight = weight
  442. } else {
  443. insertlweight = lpplan.Maxweight - lpplan.Lweight
  444. }
  445. lpplan.Lweight += insertlweight
  446. lpplan.Lweighthis = insertlweight
  447. exist := false
  448. for _, up := range upfpdetailList {
  449. if up.fpdid == fpdetail.Fpdid {
  450. up.insertlweight += insertlweight
  451. exist = true
  452. break
  453. }
  454. }
  455. if !exist {
  456. upfpdetailList = append(upfpdetailList, &upfpdetail{
  457. fpdid: fpdetail.Fpdid,
  458. insertlweight: insertlweight,
  459. fttypeid: fpdetail.Fttypeid,
  460. })
  461. }
  462. fpdetail.Weight = fpdetail.Weight - insertlweight
  463. exist = false
  464. for _, lpplandtl1 := range lpplandtl1List {
  465. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  466. lpplandtl1.Lweight += insertlweight
  467. lpplandtl1.Lweighthis = insertlweight
  468. exist = true
  469. break
  470. }
  471. }
  472. if !exist {
  473. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  474. Pastureid: lpplan.Pastureid,
  475. Lppid: lpplan.Lppid,
  476. Barid: fpdetail.Barid,
  477. Barname: fpdetail.Barname,
  478. Fpdid: fpdetail.Fpdid,
  479. Fttype: fpdetail.Fttypeid,
  480. Lweight: insertlweight,
  481. Sort: lpplan.Sort,
  482. Tmrid: lpplan.Tmrid,
  483. Tmrname: lpplan.Tmrname,
  484. Cowcount: lpplan.Cowcount,
  485. Ccountradio: lpplan.Ccountradio,
  486. Background: fpdetail.Background,
  487. Lweighthis: insertlweight,
  488. })
  489. }
  490. }
  491. }
  492. for _, lpplpplandtl1 := range lpplandtl1List {
  493. wg.Add(1)
  494. go func(lpplpplandtl1 *lpplandtl1) {
  495. defer wg.Done()
  496. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  497. cowcount,ccountradio,background,lweighthis)
  498. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  499. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  500. lpplpplandtl1.Lppid,
  501. lpplpplandtl1.Barid,
  502. lpplpplandtl1.Barname,
  503. lpplpplandtl1.Fpdid,
  504. lpplpplandtl1.Fttype,
  505. lpplpplandtl1.Lweight,
  506. lpplpplandtl1.Sort,
  507. lpplpplandtl1.Tmrid,
  508. lpplpplandtl1.Tmrname,
  509. lpplpplandtl1.Cowcount,
  510. lpplpplandtl1.Ccountradio,
  511. lpplpplandtl1.Background,
  512. lpplpplandtl1.Lweighthis).Execute()
  513. if err != nil {
  514. log.Println("autogeneration-error-6", err)
  515. tx.Rollback()
  516. return
  517. }
  518. }(lpplpplandtl1)
  519. }
  520. wg.Wait()
  521. for _, up := range upfpdetailList {
  522. wg.Add(1)
  523. go func(up *upfpdetail) {
  524. defer wg.Done()
  525. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  526. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  527. if err != nil {
  528. log.Println("autogeneration-error-5", err)
  529. tx.Rollback()
  530. return
  531. }
  532. }(up)
  533. }
  534. wg.Wait()
  535. }
  536. if typeIN == 1 {
  537. _, err = tx.SQL("update lpplan set sel = 1 where pastureid = ?", pastureid).Execute()
  538. if err != nil {
  539. log.Println("autogeneration-error-10", err)
  540. return err
  541. }
  542. }
  543. supplement := tx.Table("fpdetail").Alias("fpd")
  544. 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")
  545. supplement.Join("inner", []string{"feedp", "fp"}, "fp.ptsfid = fpd.ptsid").And("fp.pastureid = fpd.pastureid")
  546. supplement.Join("inner", []string{"feedtemplet", "ft"}, "ft.id = fp.ftid").And("ft.pastureid = fpd.pastureid")
  547. supplement.Where("fpd.pastureid = ?", pastureid).And("fpd.ptsuse< ROUND(ifnull(fpd.ptsrate,0)*fpd.weight,2)").And("fpd.ptsid > 0")
  548. supplement.GroupBy("fpd.id,fpd.times")
  549. supplementList := make([]*fpdetailQueryInfo, 0)
  550. err = supplement.Find(&supplementList)
  551. if err != nil {
  552. log.Println("autogeneration-error-11", err)
  553. return err
  554. }
  555. if len(supplementList) > 0 {
  556. for _, lpplan := range lpplanlist {
  557. if lpplan.Maxweight <= lpplan.Lweight {
  558. continue
  559. }
  560. lppids := []string{strconv.FormatInt(lpplan.Lppid, 10)}
  561. for _, lpp := range lpplanlist {
  562. if lpp.Ftid == lpplan.Ftid && lpp.Times == lpplan.Times {
  563. lppids = append(lppids, strconv.FormatInt(lpp.Lppid, 10))
  564. }
  565. }
  566. lpplandtl1List := make([]*lpplandtl1, 0)
  567. upfpdetailList := make([]*upfpdetail, 0)
  568. lpplandtllist := make([]*lpplandtl1, 0)
  569. err = tx.Table("lpplandtl1").Select("barid").Where(fmt.Sprintf("lppid in (%s)", strings.Join(lppids, ","))).And("").Find(&lpplandtllist)
  570. if err != nil {
  571. log.Println("autogeneration-error-11", err)
  572. return err
  573. }
  574. for _, fpdetail := range supplementList {
  575. if lpplan.Times != fpdetail.Times {
  576. continue
  577. }
  578. exist := false
  579. for _, bar := range lpplandtllist {
  580. if bar.Barid == fpdetail.Barid {
  581. exist = true
  582. break
  583. }
  584. }
  585. if !exist {
  586. continue
  587. }
  588. var weight float64
  589. weight = fpdetail.Weight
  590. if weight <= 0 {
  591. continue
  592. }
  593. var insertlweight float64
  594. if lpplan.Maxweight-lpplan.Lweight > weight {
  595. insertlweight = weight
  596. } else {
  597. insertlweight = lpplan.Maxweight - lpplan.Lweight
  598. }
  599. lpplan.Lweight += insertlweight
  600. lpplan.Lweighthis = insertlweight
  601. exist = false
  602. for _, up := range upfpdetailList {
  603. if up.fpdid == fpdetail.Fpdid {
  604. up.insertlweight += insertlweight
  605. exist = true
  606. break
  607. }
  608. }
  609. if !exist {
  610. upfpdetailList = append(upfpdetailList, &upfpdetail{
  611. fpdid: fpdetail.Fpdid,
  612. insertlweight: insertlweight,
  613. fttypeid: 0,
  614. })
  615. }
  616. fpdetail.Weight = fpdetail.Weight - insertlweight
  617. exist = false
  618. for _, lpplandtl1 := range lpplandtl1List {
  619. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  620. lpplandtl1.Lweight += insertlweight
  621. lpplandtl1.Lweighthis = insertlweight
  622. exist = true
  623. break
  624. }
  625. }
  626. if !exist {
  627. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  628. Pastureid: lpplan.Pastureid,
  629. Lppid: lpplan.Lppid,
  630. Barid: fpdetail.Barid,
  631. Barname: fpdetail.Barname,
  632. Fpdid: fpdetail.Fpdid,
  633. Fttype: 0,
  634. Lweight: insertlweight,
  635. Sort: lpplan.Sort,
  636. Tmrid: lpplan.Tmrid,
  637. Tmrname: lpplan.Tmrname,
  638. Cowcount: lpplan.Cowcount,
  639. Ccountradio: lpplan.Ccountradio,
  640. Background: fpdetail.Background,
  641. Lweighthis: insertlweight,
  642. })
  643. }
  644. }
  645. for _, lpplpplandtl1 := range lpplandtl1List {
  646. wg.Add(1)
  647. go func(lpplpplandtl1 *lpplandtl1) {
  648. defer wg.Done()
  649. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  650. cowcount,ccountradio,background,lweighthis)
  651. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  652. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  653. lpplpplandtl1.Lppid,
  654. lpplpplandtl1.Barid,
  655. lpplpplandtl1.Barname,
  656. lpplpplandtl1.Fpdid,
  657. lpplpplandtl1.Fttype,
  658. lpplpplandtl1.Lweight,
  659. lpplpplandtl1.Sort,
  660. lpplpplandtl1.Tmrid,
  661. lpplpplandtl1.Tmrname,
  662. lpplpplandtl1.Cowcount,
  663. lpplpplandtl1.Ccountradio,
  664. lpplpplandtl1.Background,
  665. lpplpplandtl1.Lweighthis).Execute()
  666. if err != nil {
  667. log.Println("autogeneration-error-6", err)
  668. tx.Rollback()
  669. return
  670. }
  671. }(lpplpplandtl1)
  672. }
  673. wg.Wait()
  674. for _, up := range upfpdetailList {
  675. wg.Add(1)
  676. go func(up *upfpdetail) {
  677. defer wg.Done()
  678. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  679. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  680. if err != nil {
  681. log.Println("autogeneration-error-5", err)
  682. tx.Rollback()
  683. // return err
  684. return
  685. }
  686. }(up)
  687. }
  688. wg.Wait()
  689. }
  690. }
  691. err = tx.Commit()
  692. if err != nil {
  693. log.Println("autogeneration-error-11", err)
  694. return err
  695. }
  696. return nil
  697. }
  698. func GetSpillage(c *gin.Context) {
  699. appG := app.Gin{C: c}
  700. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  701. fsion := gofasion.NewFasion(string(dataByte))
  702. parammaps := fsion.Get("parammaps")
  703. pastureid := parammaps.Get("pastureid").ValueInt64()
  704. times := parammaps.Get("times").ValueInt64()
  705. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  706. // refresh 0 全部 , 1 车次信息, 2 班次
  707. refresh := parammaps.Get("refresh").ValueInt64()
  708. tx := restful.Engine.NewSession()
  709. defer tx.Close()
  710. data := make(map[string]interface{})
  711. if refresh == 0 || refresh == 2 {
  712. 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
  713. inner join feedtemplet ft
  714. on ft.id = feedp.ftid and ft.pastureid=?
  715. WHERE feedp.pastureid = ? %s
  716. GROUP BY feedp.ftid
  717. UNION
  718. 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
  719. inner join feedtemplet ft
  720. on ft.id = feedp.ptsfid and ft.pastureid=?
  721. WHERE feedp.pastureid = ? %s and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  722. GROUP BY feedp.ptsfid`
  723. if ftid != 0 {
  724. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  725. } else {
  726. sqlStr = fmt.Sprintf(sqlStr, "", "")
  727. }
  728. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  729. if err != nil {
  730. log.Println("GetSpillage-error-1: ", err)
  731. appG.Response(http.StatusOK, e.ERROR, nil)
  732. return
  733. }
  734. 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 ,
  735. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  736. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ifnull(ptsrate,0)*weight,2)
  737. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  738. UNION
  739. 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 ,
  740. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  741. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2)
  742. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  743. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  744. if err != nil {
  745. log.Println("GetSpillage-error-2: ", err)
  746. appG.Response(http.StatusOK, e.ERROR, nil)
  747. return
  748. }
  749. for _, ft := range ftList {
  750. arrList := make([]map[string]interface{}, 0)
  751. arrid := ft["arrid"].(string)
  752. for _, detail := range ftDetailList {
  753. if _, ok := detail["ptid"]; ok {
  754. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  755. detail["background"] = ft["background"]
  756. arrList = append(arrList, detail)
  757. }
  758. } else if _, ok := detail["ptsid"]; ok {
  759. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  760. detail["background"] = ft["background"]
  761. arrList = append(arrList, detail)
  762. }
  763. }
  764. }
  765. if len(arrList) > 0 {
  766. ft["arrList"] = arrList
  767. }
  768. }
  769. data["ftlist"] = ftList
  770. }
  771. if refresh == 0 || refresh == 1 {
  772. sqlstr := `SELECT
  773. ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) tmrname,trim(tmrid) tmrid,sort,sel,
  774. CASE times
  775. WHEN 1 THEN
  776. '第一班'
  777. WHEN 2 THEN
  778. '第二班'
  779. WHEN 3 THEN
  780. '第三班'
  781. WHEN 4 THEN
  782. '第四班'
  783. END timesstr,issplit,
  784. times,display,begintime,
  785. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  786. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1
  787. inner join fpdetail
  788. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  789. WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.pastureid =lpplan.pastureid ),2),0) sumweight ,
  790. ifnull((select maxstirfeed from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),100000) maxweight,
  791. TRIM(ftid) ftid,
  792. TRIM(id) id,
  793. TRIM(id) lppid,
  794. TRIM(pastureid) pastureid
  795. FROM
  796. lpplan
  797. WHERE pastureid = ? and times <= (SELECT sysopt.inforvalue FROM sysopt
  798. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  799. `
  800. arrsql := `SELECT
  801. trim(lpplandtl1.lppid) lppid,lweight,
  802. lpplandtl1.lweight weight,lpplandtl1.sort,
  803. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  804. TRIM(lpplandtl1.fpdid) fpdid,
  805. TRIM(lpplandtl1.barid) barid,
  806. TRIM(lpplandtl1.id) id,
  807. TRIM(lpplandtl1.pastureid) pastureid,
  808. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  809. lpplandtl1.fttype,
  810. trim(lpplandtl1.tmrid) tmrid,
  811. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  812. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  813. fpdetail.ptid,
  814. fpdetail.ptsid,
  815. fpdetail.times,
  816. fpdetail.cowcount,
  817. fpdetail.ccountradio
  818. FROM
  819. lpplandtl1
  820. inner join fpdetail
  821. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  822. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0
  823. `
  824. var args []interface{}
  825. args = append(args, pastureid)
  826. if times != 0 {
  827. sqlstr += " and times = ? "
  828. arrsql += " and times = ? "
  829. args = append(args, times)
  830. }
  831. sqlstr += "ORDER BY times,lpplan.sort"
  832. session := tx.SQL(sqlstr, args...)
  833. lppList, err := session.Query().List()
  834. if err != nil {
  835. log.Println("GetSpillage-error-3: ", err)
  836. appG.Response(http.StatusOK, e.ERROR, nil)
  837. return
  838. }
  839. arrsql += " ORDER BY lpplandtl1.sort"
  840. arrList, err := tx.SQL(arrsql, args...).QueryString() //获取
  841. if err != nil {
  842. log.Println("GetSpillage-error-4: ", err)
  843. appG.Response(http.StatusOK, e.ERROR, nil)
  844. return
  845. }
  846. for _, query := range lppList {
  847. list := make([]map[string]string, 0)
  848. for _, arr := range arrList {
  849. if query["lppid"].(string) == arr["lppid"] {
  850. list = append(list, arr)
  851. }
  852. }
  853. query["arrList"] = list
  854. }
  855. data["list"] = lppList
  856. data["total"] = len(lppList)
  857. }
  858. appG.Response(http.StatusOK, e.SUCCESS, data)
  859. }
  860. type lpplanTrains struct {
  861. Id string `xorm:"id"`
  862. Sort int64 `xorm:"sort"`
  863. Pastureid string `xorm:"pastureid"`
  864. }
  865. //修改车次顺序
  866. func UpdateTrains(c *gin.Context) {
  867. appG := app.Gin{C: c}
  868. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  869. lppList := make([]*lpplanTrains, 0)
  870. err := json.Unmarshal(dataByte, &lppList)
  871. if err != nil {
  872. log.Println("UpdateTrains-error-1: ", err)
  873. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  874. return
  875. }
  876. tx := restful.Engine.NewSession()
  877. defer tx.Close()
  878. tx.Begin()
  879. for _, item := range lppList {
  880. _, err = tx.Table("lpplan").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  881. if err != nil {
  882. log.Println("UpdateTrains-error-2: ", err)
  883. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  884. tx.Rollback()
  885. return
  886. }
  887. }
  888. err = tx.Commit()
  889. if err != nil {
  890. log.Println("UpdateTrains-error-2: ", err)
  891. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  892. tx.Rollback()
  893. return
  894. }
  895. appG.Response(http.StatusOK, e.SUCCESS, true)
  896. }
  897. func UpdateTrainsDay(c *gin.Context) {
  898. appG := app.Gin{C: c}
  899. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  900. lppList := make([]*lpplanTrains, 0)
  901. err := json.Unmarshal(dataByte, &lppList)
  902. if err != nil {
  903. log.Println("UpdateTrainsDay-error-1: ", err)
  904. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  905. return
  906. }
  907. tx := restful.Engine.NewSession()
  908. defer tx.Close()
  909. tx.Begin()
  910. for _, item := range lppList {
  911. _, err = tx.Table("lpplandate").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  912. if err != nil {
  913. log.Println("UpdateTrainsDay-error-2: ", err)
  914. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  915. tx.Rollback()
  916. return
  917. }
  918. }
  919. err = tx.Commit()
  920. if err != nil {
  921. log.Println("UpdateTrainsDay-error-3: ", err)
  922. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  923. tx.Rollback()
  924. return
  925. }
  926. appG.Response(http.StatusOK, e.SUCCESS, true)
  927. }
  928. type ReqAddLpplan struct {
  929. Id string `xorm:"id"`
  930. Tmrid string `xorm:"tmrid"`
  931. TmrName string `xorm:"tmrname"`
  932. Sort int64 `xorm:"sort"`
  933. Sel int64 `xorm:"sel"`
  934. Times int64 `xorm:"times"`
  935. Ftname string `xorm:"ftname"`
  936. Ftid string `xorm:"ftid"`
  937. Maxweight float64 `xorm:"maxweight"`
  938. // Sumweight int64 `xorm:"sumweight"`
  939. Sumcowcount int64 `xorm:"sumcowcount"`
  940. Pastureid string `xorm:"pastureid"`
  941. Display string `xorm:"display"`
  942. Issplit int64 `xorm:"issplit"`
  943. BeginTime string `xorm:"begintime"`
  944. }
  945. //添加车次并返回添加的车次信息
  946. func AddLpplan(c *gin.Context) {
  947. appG := app.Gin{C: c}
  948. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  949. fsion := gofasion.NewFasion(string(dataByte))
  950. parammaps := fsion.Get("parammaps")
  951. req := new(ReqAddLpplan)
  952. err := json.Unmarshal([]byte(parammaps.Json()), req)
  953. if err != nil {
  954. log.Println("AddLpplan-error-1: ", err)
  955. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  956. return
  957. }
  958. if req.Times == 0 {
  959. log.Println("AddLpplan-error-1: ", err)
  960. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  961. return
  962. }
  963. tx := restful.Engine.NewSession()
  964. defer tx.Close()
  965. ids, err := setting.SnowIds.NextId()
  966. if err != nil {
  967. ids = time.Now().UnixNano()
  968. logging.Info("AddLpplan-error-1: ", err)
  969. }
  970. req.Id = strconv.FormatInt(ids, 10)
  971. datacount, err := tx.Table("lpplan").Select("max(sort) as sort").Where("pastureid = ?", req.Pastureid).And(" times = ?", req.Times).QueryString()
  972. if err != nil {
  973. log.Println("AddLpplan-error-2: ", err)
  974. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  975. return
  976. }
  977. var count int64 = 0
  978. for _, d := range datacount {
  979. if _, ok := d["sort"]; ok {
  980. count, _ = strconv.ParseInt(d["sort"], 10, 64)
  981. }
  982. }
  983. req.Sort = count + 1
  984. _, err = tx.Table("lpplan").Insert(req)
  985. if err != nil {
  986. log.Println("AddLpplan-error-3: ", err)
  987. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  988. return
  989. }
  990. appG.Response(http.StatusOK, e.SUCCESS, req)
  991. }
  992. type ReqAddLpplanDay struct {
  993. Id string `xorm:"id"`
  994. Tmrid string `xorm:"tmrid"`
  995. TmrName string `xorm:"tmrname"`
  996. Sort int64 `xorm:"sort"`
  997. Sel int64 `xorm:"sel"`
  998. Times int64 `xorm:"times"`
  999. Ftname string `xorm:"ftname"`
  1000. Ftid string `xorm:"ftid"`
  1001. Maxweight float64 `xorm:"maxweight"`
  1002. // Sumweight int64 `xorm:"sumweight"`
  1003. Sumcowcount int64 `xorm:"sumcowcount"`
  1004. Pastureid string `xorm:"pastureid"`
  1005. Display string `xorm:"display"`
  1006. Issplit int64 `xorm:"issplit"`
  1007. BeginTime string `xorm:"begintime"`
  1008. Date string `xorm:"date"`
  1009. }
  1010. func AddLpplanDay(c *gin.Context) {
  1011. appG := app.Gin{C: c}
  1012. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1013. fsion := gofasion.NewFasion(string(dataByte))
  1014. parammaps := fsion.Get("parammaps")
  1015. req := new(ReqAddLpplanDay)
  1016. err := json.Unmarshal([]byte(parammaps.Json()), req)
  1017. if err != nil {
  1018. log.Println("AddLpplanDay-error-1: ", err)
  1019. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1020. return
  1021. }
  1022. if req.Times == 0 {
  1023. log.Println("AddLpplanDay-error-1: ", err)
  1024. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  1025. return
  1026. }
  1027. tx := restful.Engine.NewSession()
  1028. defer tx.Close()
  1029. ids, err := setting.SnowIds.NextId()
  1030. if err != nil {
  1031. ids = time.Now().UnixNano()
  1032. logging.Info("AddLpplanDay-error-1: ", err)
  1033. }
  1034. req.Id = strconv.FormatInt(ids, 10)
  1035. count, err := tx.Table("lpplandate").Where("pastureid = ?", req.Pastureid).And(" date = ?", req.Date).And(" times = ?", req.Times).Count()
  1036. if err != nil {
  1037. log.Println("AddLpplanDay-error-2: ", err)
  1038. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1039. return
  1040. }
  1041. req.Sort = count + 1
  1042. _, err = tx.Table("lpplandate").Insert(req)
  1043. if err != nil {
  1044. log.Println("AddLpplanDay-error-3: ", err)
  1045. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1046. return
  1047. }
  1048. appG.Response(http.StatusOK, e.SUCCESS, req)
  1049. }
  1050. //修改车次信息,如果修改tmrid信息则删除栏舍信息
  1051. func UpdateLpplan(c *gin.Context) {
  1052. appG := app.Gin{C: c}
  1053. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1054. fsion := gofasion.NewFasion(string(dataByte))
  1055. parammaps := fsion.Get("parammaps")
  1056. lpplanid := parammaps.Get("id").ValueStr()
  1057. pastureid := parammaps.Get("pastureid").ValueStr()
  1058. tmrid := parammaps.Get("tmrid").ValueStr()
  1059. tmrname := parammaps.Get("tmrname").ValueStr()
  1060. begintime := parammaps.Get("begintime").ValueStr()
  1061. display := parammaps.Get("display").ValueStr()
  1062. times := parammaps.Get("times").ValueInt64()
  1063. ftid := parammaps.Get("ftid").ValueStr()
  1064. ftname := parammaps.Get("ftname").ValueStr()
  1065. sel := parammaps.Get("sel").ValueInt64()
  1066. issplit := parammaps.Get("issplit").ValueInt64()
  1067. lpplan := new(ReqAddLpplan)
  1068. lpplan.Tmrid = tmrid
  1069. lpplan.TmrName = tmrname
  1070. lpplan.BeginTime = begintime
  1071. lpplan.Sel = sel
  1072. lpplan.Issplit = issplit
  1073. lpplan.Times = times
  1074. lpplan.Ftid = ftid
  1075. lpplan.Ftname = ftname
  1076. lpplan.Display = display
  1077. tx := restful.Engine.NewSession()
  1078. defer tx.Close()
  1079. tx.Begin()
  1080. lpplanData := new(lpplanStruct)
  1081. _, err := tx.Table("lpplan").Select("tmrid").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).Get(lpplanData)
  1082. if err != nil {
  1083. log.Println("UpdateLpplan-error-1: ", err)
  1084. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1085. tx.Rollback()
  1086. return
  1087. }
  1088. _, err = tx.Table("lpplan").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).
  1089. MustCols("display,tmrid,tmrname,begintime,sel,issplit,times,ftid,ftname").Update(lpplan)
  1090. if err != nil {
  1091. log.Println("UpdateLpplan-error-1: ", err)
  1092. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1093. tx.Rollback()
  1094. return
  1095. }
  1096. err = tx.Commit()
  1097. if err != nil {
  1098. log.Println("UpdateLpplan-error-3: ", err)
  1099. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1100. tx.Rollback()
  1101. return
  1102. }
  1103. dataList, err := tx.SQL(`select * from lpplandtl1 where lppid = ? and pastureid = ? `, lpplanid, pastureid).Query().List()
  1104. if err != nil {
  1105. log.Println("UpdateLpplan-error-4: ", err)
  1106. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1107. return
  1108. }
  1109. // var oldbar, bar []int64
  1110. oldList := fsion.Get("old").Array()
  1111. if len(dataList) >= len(oldList) {
  1112. for _, data := range dataList {
  1113. updateStatus := true
  1114. for _, old := range oldList {
  1115. fmt.Println(old.Get("lweight").ValueStr(), data["lweight"], old.Get("barid").ValueInt64(), data["barid"], "aaaaaaaaaaa")
  1116. if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1117. updateStatus = false
  1118. break
  1119. }
  1120. }
  1121. if updateStatus {
  1122. now := time.Now()
  1123. for _, data := range dataList {
  1124. _, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  1125. background,cowcount,ccountradio,lweighthis,createdate)
  1126. values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  1127. data["pastureid"], data["lppid"], data["barid"],
  1128. data["barname"], data["fpdid"], data["fttype"],
  1129. data["lweight"], data["sort"], data["tmrid"],
  1130. data["tmrname"], data["background"], data["cowcount"],
  1131. data["ccountradio"], data["lweighthis"], now).Execute()
  1132. if err != nil {
  1133. log.Println("UpdateLpplan-error-5: ", err)
  1134. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1135. return
  1136. }
  1137. }
  1138. break
  1139. }
  1140. }
  1141. } else {
  1142. for _, old := range oldList {
  1143. updateStatus := true
  1144. for _, data := range dataList {
  1145. if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1146. updateStatus = false
  1147. break
  1148. }
  1149. }
  1150. if updateStatus {
  1151. now := time.Now()
  1152. for _, old := range oldList {
  1153. exist := false
  1154. var lweight, tmrid, tmrname interface{}
  1155. for _, data := range dataList {
  1156. if old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1157. lweight = data["lweight"]
  1158. tmrid = data["tmrid"]
  1159. tmrname = data["tmrname"]
  1160. exist = true
  1161. break
  1162. }
  1163. }
  1164. if !exist {
  1165. tmrid = old.Get("tmrid").ValueInt64()
  1166. tmrname = old.Get("tmrname").ValueStr()
  1167. lweight = 0
  1168. }
  1169. _, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  1170. background,cowcount,ccountradio,lweighthis,createdate) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  1171. old.Get("pastureid").ValueStr(), old.Get("lppid").ValueStr(), old.Get("barid").ValueStr(),
  1172. old.Get("barname").ValueStr(), old.Get("fpdid").ValueStr(), old.Get("fttype").ValueStr(),
  1173. lweight, old.Get("sort").ValueStr(), tmrid,
  1174. tmrname, old.Get("background").ValueStr(), old.Get("cowcount").ValueStr(),
  1175. old.Get("ccountradio").ValueStr(), old.Get("lweighthis").ValueInt64(), now).Execute()
  1176. if err != nil {
  1177. log.Println("UpdateLpplan-error-6: ", err)
  1178. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1179. return
  1180. }
  1181. }
  1182. break
  1183. }
  1184. }
  1185. }
  1186. appG.Response(http.StatusOK, e.SUCCESS, true)
  1187. }
  1188. func GetSpillageDay(c *gin.Context) {
  1189. appG := app.Gin{C: c}
  1190. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1191. fsion := gofasion.NewFasion(string(dataByte))
  1192. parammaps := fsion.Get("parammaps")
  1193. pastureid := parammaps.Get("pastureid").ValueInt64()
  1194. times := parammaps.Get("times").ValueInt64()
  1195. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  1196. // refresh 0 全部 , 1 车次信息, 2 班次
  1197. refresh := parammaps.Get("refresh").ValueInt64()
  1198. date := parammaps.Get("date").ValueStr()
  1199. tx := restful.Engine.NewSession()
  1200. defer tx.Close()
  1201. data := make(map[string]interface{})
  1202. if refresh == 0 || refresh == 2 {
  1203. 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
  1204. FROM feedpdate feedp
  1205. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1206. INNER JOIN feedtemplet ft
  1207. ON ft.id = fpd.ptid AND ft.pastureid= feedp.pastureid
  1208. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1209. GROUP BY fpd.ptid
  1210. UNION
  1211. 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
  1212. FROM feedpdate feedp
  1213. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1214. INNER JOIN feedtemplet ft
  1215. ON ft.id = fpd.ptsid AND ft.pastureid=feedp.pastureid
  1216. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1217. AND (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1218. GROUP BY fpd.ptsid`
  1219. if ftid != 0 {
  1220. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  1221. } else {
  1222. sqlStr = fmt.Sprintf(sqlStr, "", "")
  1223. }
  1224. ftList, err := tx.SQL(sqlStr, times, pastureid, date, times, pastureid, date).Query().List()
  1225. if err != nil {
  1226. log.Println("GetSpillageDay-error-1: ", err)
  1227. appG.Response(http.StatusOK, e.ERROR, nil)
  1228. return
  1229. }
  1230. // AND ( fpdetail.ptsid=?)
  1231. // AND (fpdetail.ptid=? )
  1232. 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 ,
  1233. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 1 isfill FROM fpdetaildate fpdetail
  1234. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ifnull(ptsrate,0)*weight,2) and fpdetail.date = ?
  1235. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid ) is not null
  1236. UNION
  1237. 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 ,
  1238. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 0 isfill FROM fpdetaildate fpdetail
  1239. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2) and fpdetail.date = ?
  1240. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  1241. ORDER BY barid`, pastureid, times, date, pastureid, times, date).Query().List()
  1242. if err != nil {
  1243. log.Println("GetSpillageDay-error-2: ", err)
  1244. appG.Response(http.StatusOK, e.ERROR, nil)
  1245. return
  1246. }
  1247. for _, ft := range ftList {
  1248. arrList := make([]map[string]interface{}, 0)
  1249. arrid := ft["arrid"].(string)
  1250. for _, detail := range ftDetailList {
  1251. if _, ok := detail["ptid"]; ok {
  1252. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1253. detail["background"] = ft["background"]
  1254. detail["fttype"] = ft["type"].(int64)
  1255. arrList = append(arrList, detail)
  1256. }
  1257. } else if _, ok := detail["ptsid"]; ok {
  1258. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1259. detail["background"] = ft["background"]
  1260. detail["fttype"] = ft["type"].(int64)
  1261. arrList = append(arrList, detail)
  1262. }
  1263. }
  1264. }
  1265. if len(arrList) > 0 {
  1266. ft["arrList"] = arrList
  1267. }
  1268. }
  1269. data["ftlist"] = ftList
  1270. }
  1271. if refresh == 0 || refresh == 1 {
  1272. sqlstr := `SELECT
  1273. lpplan.date,
  1274. -- ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname)
  1275. lpplan.tmrname tmrname,trim(lpplan.tmrid) tmrid,lpplan.sort,lpplan.sel,
  1276. CASE lpplan.times
  1277. WHEN 1 THEN
  1278. '第一班'
  1279. WHEN 2 THEN
  1280. '第二班'
  1281. WHEN 3 THEN
  1282. '第三班'
  1283. WHEN 4 THEN
  1284. '第四班'
  1285. END timesstr,issplit,
  1286. lpplan.times,display,begintime,
  1287. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  1288. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1date lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.date= ?),2),0) sumweight ,
  1289. maxweight,
  1290. TRIM(ftid) ftid,
  1291. TRIM(lpplan.id) id,
  1292. TRIM(lpplan.id) lppid,
  1293. TRIM(lpplan.pastureid) pastureid,
  1294. ifnull(dpl.havebutton,0) as havebutton
  1295. FROM
  1296. lpplandate lpplan
  1297. left join downloadedplan dpl on dpl.pid = lpplan.id and DATE_FORMAT(dpl.mydate,'%Y-%m-%e') = lpplan.date
  1298. WHERE lpplan.pastureid = ? and lpplan.date = ? and lpplan.times <= (SELECT sysopt.inforvalue FROM sysopt
  1299. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  1300. `
  1301. arrsql := `SELECT
  1302. trim(lpplandtl1.lppid) lppid,lweight,
  1303. lpplandtl1.lweight weight,lpplandtl1.sort,
  1304. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  1305. TRIM(lpplandtl1.fpdid) fpdid,
  1306. TRIM(lpplandtl1.barid) barid,
  1307. TRIM(lpplandtl1.id) id,
  1308. TRIM(lpplandtl1.pastureid) pastureid,
  1309. lpplandtl1.background,
  1310. lpplandtl1.fttype,
  1311. trim(lpplandtl1.tmrid) tmrid,
  1312. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  1313. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  1314. fpdetail.ptid,
  1315. fpdetail.ptsid,
  1316. fpdetail.times,
  1317. fpdetail.cowcount,
  1318. fpdetail.ccountradio
  1319. FROM
  1320. lpplandtl1date lpplandtl1
  1321. inner join fpdetaildate fpdetail
  1322. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid and lpplandtl1.date = fpdetail.date
  1323. WHERE lpplandtl1.pastureid = ? and lpplandtl1.date = ? and lpplandtl1.lweight>0 `
  1324. var args []interface{}
  1325. args = append(args, date, pastureid, date)
  1326. if times != 0 {
  1327. sqlstr += " and lpplan.times = ? "
  1328. // arrsql += " and times = ? "
  1329. args = append(args, times)
  1330. }
  1331. sqlstr += " group by lpplan.id ORDER BY times,lpplan.sort"
  1332. session := tx.SQL(sqlstr, args...)
  1333. lppList, err := session.Query().List()
  1334. if err != nil {
  1335. log.Println("GetSpillageDay-error-3: ", err)
  1336. appG.Response(http.StatusOK, e.ERROR, nil)
  1337. return
  1338. }
  1339. arrsql += " ORDER BY lpplandtl1.sort"
  1340. arrList, err := tx.SQL(arrsql, pastureid, date).QueryString() //获取
  1341. if err != nil {
  1342. log.Println("GetSpillage-error-4: ", err)
  1343. appG.Response(http.StatusOK, e.ERROR, nil)
  1344. return
  1345. }
  1346. for _, query := range lppList {
  1347. list := make([]map[string]string, 0)
  1348. for _, arr := range arrList {
  1349. if query["lppid"].(string) == arr["lppid"] {
  1350. list = append(list, arr)
  1351. }
  1352. }
  1353. query["arrList"] = list
  1354. }
  1355. data["list"] = lppList
  1356. data["total"] = len(lppList)
  1357. }
  1358. appG.Response(http.StatusOK, e.SUCCESS, data)
  1359. }
  1360. // func AddLpplandtl(c *gin.Context) {
  1361. // appG := app.Gin{C: c}
  1362. // dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1363. // fsion := gofasion.NewFasion(string(dataByte))
  1364. // parammaps := fsion.Get("parammaps")
  1365. // tx := restful.Engine.NewSession()
  1366. // defer tx.Close()
  1367. // exist := false
  1368. // var err error
  1369. // lpplandList := make([]*lpplandtl1, 0)
  1370. // err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1371. // OrderBy("sort").Find(&lpplandList)
  1372. // if err != nil {
  1373. // log.Println("AddLpplandtl-error-1: ", err)
  1374. // appG.Response(http.StatusOK, e.ERROR, err)
  1375. // return
  1376. // }
  1377. // if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1378. // for _, lppand := range lpplandList {
  1379. // if lppand.Id == parammaps.Get("id").ValueInt64() {
  1380. // if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1381. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1382. // return
  1383. // }
  1384. // exist = true
  1385. // break
  1386. // }
  1387. // }
  1388. // }
  1389. // tmrid := parammaps.Get("tmrid").ValueInt64()
  1390. // var sort int64 = -1
  1391. // if !exist {
  1392. // for _, lppand := range lpplandList {
  1393. // if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1394. // _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1395. // if err != nil {
  1396. // log.Println("AddLpplandtl-error-2: ", err)
  1397. // appG.Response(http.StatusOK, e.ERROR, err)
  1398. // return
  1399. // }
  1400. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1401. // return
  1402. // }
  1403. // }
  1404. // for i, lppand := range lpplandList {
  1405. // lppand.Sort = int64(i) + 1
  1406. // }
  1407. // updateLpplandList := make([]*lpplandtl1, 0)
  1408. // for i, lppand := range lpplandList {
  1409. // n := i + 1
  1410. // if lppand.Tmrid == tmrid {
  1411. // sort = int64(n) + 1
  1412. // lppand.Sort = int64(n)
  1413. // updateLpplandList = append(updateLpplandList, lppand)
  1414. // } else if sort > 0 {
  1415. // lppand.Sort = int64(n) + 2
  1416. // updateLpplandList = append(updateLpplandList, lppand)
  1417. // }
  1418. // }
  1419. // tx.Begin()
  1420. // for _, lppand := range updateLpplandList {
  1421. // _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1422. // if err != nil {
  1423. // tx.Rollback()
  1424. // log.Println("AddLpplandtl-error-2: ", err)
  1425. // appG.Response(http.StatusOK, e.ERROR, err)
  1426. // return
  1427. // }
  1428. // }
  1429. // if sort < 0 && len(lpplandList) > 0 {
  1430. // sort = int64(len(lpplandList) + 2)
  1431. // } else if len(lpplandList) == 0 {
  1432. // sort = 1
  1433. // }
  1434. // _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1435. // VALUES (?,?,?,?,?,?,?,?,?,
  1436. // ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1437. // parammaps.Get("pastureid").ValueStr(),
  1438. // parammaps.Get("lppid").ValueStr(),
  1439. // parammaps.Get("barid").ValueStr(),
  1440. // parammaps.Get("barname").ValueStr(),
  1441. // parammaps.Get("fpdid").ValueStr(),
  1442. // parammaps.Get("lweight").ValueStr(),
  1443. // sort,
  1444. // parammaps.Get("tmrid").ValueStr(),
  1445. // parammaps.Get("tmrname").ValueStr(),
  1446. // parammaps.Get("fttype").ValueStr(),
  1447. // parammaps.Get("background").ValueStr(),
  1448. // parammaps.Get("cowcount").ValueStr(),
  1449. // parammaps.Get("cowcount").ValueStr(),
  1450. // parammaps.Get("ccountradio").ValueStr(),
  1451. // parammaps.Get("ccountradio").ValueStr()).Execute()
  1452. // if err != nil {
  1453. // tx.Rollback()
  1454. // log.Println("AddLpplandtl-error-3: ", err)
  1455. // appG.Response(http.StatusOK, e.ERROR, err)
  1456. // return
  1457. // }
  1458. // err = tx.Commit()
  1459. // if err != nil {
  1460. // tx.Rollback()
  1461. // log.Println("AddLpplandtl-error-6: ", err)
  1462. // appG.Response(http.StatusOK, e.ERROR, err)
  1463. // return
  1464. // }
  1465. // } else {
  1466. // lpplandid := parammaps.Get("id").ValueInt64()
  1467. // updateLpplandList := make([]*lpplandtl1, 0)
  1468. // var oldsort int64
  1469. // for i, lppand := range lpplandList {
  1470. // lppand.Sort = int64(i) + 1
  1471. // }
  1472. // for _, lppand := range lpplandList {
  1473. // if lppand.Id == lpplandid {
  1474. // oldsort = lppand.Sort
  1475. // break
  1476. // }
  1477. // }
  1478. // for _, lppand := range lpplandList {
  1479. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  1480. // sort = lppand.Sort
  1481. // break
  1482. // }
  1483. // }
  1484. // if sort == -1 {
  1485. // sort = 1
  1486. // }
  1487. // if oldsort < sort {
  1488. // for _, lppand := range lpplandList {
  1489. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  1490. // lppand.Sort--
  1491. // updateLpplandList = append(updateLpplandList, lppand)
  1492. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  1493. // lppand.Sort++
  1494. // updateLpplandList = append(updateLpplandList, lppand)
  1495. // }
  1496. // }
  1497. // sort--
  1498. // } else {
  1499. // for _, lppand := range lpplandList {
  1500. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  1501. // lppand.Sort++
  1502. // updateLpplandList = append(updateLpplandList, lppand)
  1503. // }
  1504. // }
  1505. // }
  1506. // var tmrlpplandList []*lpplandtl1
  1507. // for _, lppand := range lpplandList {
  1508. // exist := false
  1509. // for _, lppand1 := range updateLpplandList {
  1510. // if lppand.Id == lppand1.Id {
  1511. // exist = true
  1512. // break
  1513. // }
  1514. // }
  1515. // if !exist {
  1516. // tmrlpplandList = append(tmrlpplandList, lppand)
  1517. // }
  1518. // }
  1519. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  1520. // if len(updateLpplandList) > 0 {
  1521. // tx.Begin()
  1522. // for _, lppand := range updateLpplandList {
  1523. // _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1524. // if err != nil {
  1525. // tx.Rollback()
  1526. // log.Println("AddLpplandtl-error-2: ", err)
  1527. // appG.Response(http.StatusOK, e.ERROR, err)
  1528. // return
  1529. // }
  1530. // }
  1531. // _, 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()
  1532. // if err != nil {
  1533. // tx.Rollback()
  1534. // log.Println("AddLpplandtl-error-2: ", err)
  1535. // appG.Response(http.StatusOK, e.ERROR, err)
  1536. // return
  1537. // }
  1538. // tx.Commit()
  1539. // }
  1540. // }
  1541. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1542. // }
  1543. func AddLpplandtl(c *gin.Context) {
  1544. appG := app.Gin{C: c}
  1545. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1546. fsion := gofasion.NewFasion(string(dataByte))
  1547. parammaps := fsion.Get("parammaps")
  1548. tx := restful.Engine.NewSession()
  1549. defer tx.Close()
  1550. exist := false
  1551. var err error
  1552. lpplandList := make([]*lpplandtl1, 0)
  1553. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1554. OrderBy("sort").Find(&lpplandList)
  1555. if err != nil {
  1556. log.Println("AddLpplandtl-error-1: ", err)
  1557. appG.Response(http.StatusOK, e.ERROR, err)
  1558. return
  1559. }
  1560. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1561. for _, lppand := range lpplandList {
  1562. if lppand.Id == parammaps.Get("id").ValueInt64() {
  1563. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1564. appG.Response(http.StatusOK, e.SUCCESS, true)
  1565. return
  1566. }
  1567. exist = true
  1568. break
  1569. }
  1570. }
  1571. }
  1572. tmrid := parammaps.Get("tmrid").ValueInt64()
  1573. if !exist {
  1574. var sort int64 = -1
  1575. var maxsort int64
  1576. for _, lppand := range lpplandList {
  1577. if maxsort <= lppand.Sort {
  1578. maxsort = lppand.Sort
  1579. }
  1580. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1581. _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1582. if err != nil {
  1583. log.Println("AddLpplandtl-error-2: ", err)
  1584. appG.Response(http.StatusOK, e.ERROR, err)
  1585. return
  1586. }
  1587. appG.Response(http.StatusOK, e.SUCCESS, true)
  1588. return
  1589. }
  1590. }
  1591. for i, lppand := range lpplandList {
  1592. lppand.Sort = int64(i) + 1
  1593. }
  1594. updateLpplandList := make([]*lpplandtl1, 0)
  1595. for i, lppand := range lpplandList {
  1596. n := i + 1
  1597. if lppand.Tmrid == tmrid {
  1598. sort = int64(n) + 1
  1599. lppand.Sort = int64(n)
  1600. updateLpplandList = append(updateLpplandList, lppand)
  1601. } else if sort > 0 {
  1602. lppand.Sort = int64(n) + 2
  1603. updateLpplandList = append(updateLpplandList, lppand)
  1604. }
  1605. }
  1606. tx.Begin()
  1607. for _, lppand := range updateLpplandList {
  1608. _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1609. if err != nil {
  1610. tx.Rollback()
  1611. log.Println("AddLpplandtl-error-2: ", err)
  1612. appG.Response(http.StatusOK, e.ERROR, err)
  1613. return
  1614. }
  1615. }
  1616. if sort < 0 && len(lpplandList) > 0 {
  1617. sort = maxsort + 1
  1618. } else if len(lpplandList) == 0 {
  1619. sort = 1
  1620. }
  1621. _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1622. VALUES (?,?,?,?,?,?,?,?,?,
  1623. ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1624. parammaps.Get("pastureid").ValueStr(),
  1625. parammaps.Get("lppid").ValueStr(),
  1626. parammaps.Get("barid").ValueStr(),
  1627. parammaps.Get("barname").ValueStr(),
  1628. parammaps.Get("fpdid").ValueStr(),
  1629. parammaps.Get("lweight").ValueStr(),
  1630. sort,
  1631. parammaps.Get("tmrid").ValueStr(),
  1632. parammaps.Get("tmrname").ValueStr(),
  1633. parammaps.Get("fttype").ValueStr(),
  1634. parammaps.Get("background").ValueStr(),
  1635. parammaps.Get("cowcount").ValueStr(),
  1636. parammaps.Get("cowcount").ValueStr(),
  1637. parammaps.Get("ccountradio").ValueStr(),
  1638. parammaps.Get("ccountradio").ValueStr()).Execute()
  1639. if err != nil {
  1640. tx.Rollback()
  1641. log.Println("AddLpplandtl-error-3: ", err)
  1642. appG.Response(http.StatusOK, e.ERROR, err)
  1643. return
  1644. }
  1645. err = tx.Commit()
  1646. if err != nil {
  1647. tx.Rollback()
  1648. log.Println("AddLpplandtl-error-6: ", err)
  1649. appG.Response(http.StatusOK, e.ERROR, err)
  1650. return
  1651. }
  1652. } else {
  1653. lpplandid := parammaps.Get("id").ValueInt64()
  1654. // updateLpplandList := make([]*lpplandtl1, 0)
  1655. var oldsort int64
  1656. for i, lppand := range lpplandList {
  1657. lppand.Sort = int64(i) + 1
  1658. }
  1659. noup := false
  1660. for i, lppand := range lpplandList {
  1661. if lppand.Id == lpplandid {
  1662. oldsort = lppand.Sort
  1663. b := false
  1664. for _, lppand := range lpplandList {
  1665. if lppand.Tmrid == tmrid {
  1666. noup = false
  1667. b = true
  1668. break
  1669. }
  1670. }
  1671. if !b {
  1672. if i == 0 || i == len(lpplandList)-1 {
  1673. noup = true
  1674. }
  1675. if i != 0 && i < len(lpplandList)-1 {
  1676. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  1677. noup = true
  1678. }
  1679. }
  1680. // if lpplandList[i+1].Tmrid == tmrid {
  1681. // _, 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()
  1682. // if err != nil {
  1683. // tx.Rollback()
  1684. // log.Println("AddLpplandtl-error-2: ", err)
  1685. // appG.Response(http.StatusOK, e.ERROR, err)
  1686. // return
  1687. // }
  1688. // tx.Commit()
  1689. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1690. // return
  1691. // }
  1692. // }
  1693. // break
  1694. // }
  1695. // }
  1696. }
  1697. break
  1698. }
  1699. }
  1700. if noup {
  1701. _, 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()
  1702. if err != nil {
  1703. tx.Rollback()
  1704. log.Println("AddLpplandtl-error-2: ", err)
  1705. appG.Response(http.StatusOK, e.ERROR, err)
  1706. return
  1707. }
  1708. } else {
  1709. for i, lppand := range lpplandList {
  1710. if lppand.Id == lpplandid {
  1711. if i != 0 {
  1712. if lpplandList[i-1].Tmrid == tmrid {
  1713. _, 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()
  1714. if err != nil {
  1715. tx.Rollback()
  1716. log.Println("AddLpplandtl-error-2: ", err)
  1717. appG.Response(http.StatusOK, e.ERROR, err)
  1718. return
  1719. }
  1720. tx.Commit()
  1721. appG.Response(http.StatusOK, e.SUCCESS, true)
  1722. return
  1723. }
  1724. }
  1725. if i < len(lpplandList)-1 {
  1726. if lpplandList[i+1].Tmrid == tmrid {
  1727. _, 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()
  1728. if err != nil {
  1729. tx.Rollback()
  1730. log.Println("AddLpplandtl-error-2: ", err)
  1731. appG.Response(http.StatusOK, e.ERROR, err)
  1732. return
  1733. }
  1734. tx.Commit()
  1735. appG.Response(http.StatusOK, e.SUCCESS, true)
  1736. return
  1737. }
  1738. }
  1739. break
  1740. }
  1741. }
  1742. exist := false
  1743. sort1 := 0
  1744. a := false
  1745. for _, lppand := range lpplandList {
  1746. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  1747. sort1 = int(lppand.Sort)
  1748. if !a {
  1749. for _, lppand1 := range lpplandList {
  1750. if lppand1.Id == lpplandid {
  1751. lppand1.Tmrid = tmrid
  1752. lppand1.Tmrname = lppand.Tmrname
  1753. a = true
  1754. // lppand1.Sort = lppand.Sort
  1755. break
  1756. }
  1757. }
  1758. }
  1759. exist = true
  1760. }
  1761. }
  1762. if exist {
  1763. if oldsort > int64(sort1) {
  1764. for _, lppand := range lpplandList {
  1765. if lppand.Id == lpplandid {
  1766. lppand.Sort = int64(sort1) + 1
  1767. continue
  1768. }
  1769. if lppand.Sort >= int64(sort1)+1 {
  1770. lppand.Sort++
  1771. }
  1772. }
  1773. } else {
  1774. for _, lppand := range lpplandList {
  1775. if lppand.Id == lpplandid {
  1776. lppand.Sort = int64(sort1) + 1
  1777. continue
  1778. }
  1779. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  1780. lppand.Sort--
  1781. } else if lppand.Sort <= int64(sort1)+1 {
  1782. lppand.Sort++
  1783. }
  1784. }
  1785. }
  1786. } else {
  1787. for _, lppand := range lpplandList {
  1788. if lppand.Id == lpplandid {
  1789. lppand.Sort = int64(len(lpplandList)) + 1
  1790. lppand.Tmrid = tmrid
  1791. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  1792. break
  1793. }
  1794. }
  1795. }
  1796. sort.Slice(lpplandList, func(i, j int) bool {
  1797. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  1798. })
  1799. for i, lppand := range lpplandList {
  1800. lppand.Sort = int64(i) + 1
  1801. }
  1802. for _, lppand := range lpplandList {
  1803. _, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? ", lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1804. if err != nil {
  1805. tx.Rollback()
  1806. log.Println("AddLpplandtl-error-2: ", err)
  1807. appG.Response(http.StatusOK, e.ERROR, err)
  1808. return
  1809. }
  1810. }
  1811. }
  1812. tx.Commit()
  1813. }
  1814. appG.Response(http.StatusOK, e.SUCCESS, true)
  1815. }
  1816. func UpdateLpplandtlSort(c *gin.Context) {
  1817. appG := app.Gin{C: c}
  1818. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1819. fsion := gofasion.NewFasion(string(dataByte))
  1820. parammaps := fsion.Get("parammaps")
  1821. tx := restful.Engine.NewSession()
  1822. defer tx.Close()
  1823. var err error
  1824. lpplandList := make([]*lpplandtl1, 0)
  1825. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1826. OrderBy("sort").Find(&lpplandList)
  1827. if err != nil {
  1828. log.Println("UpdateLpplandtlSort-error-1: ", err)
  1829. appG.Response(http.StatusOK, e.ERROR, err)
  1830. return
  1831. }
  1832. var oldsort, lpplandtlsort int64
  1833. tmrid := parammaps.Get("tmrid").ValueInt64()
  1834. sort := parammaps.Get("sort").ValueInt64()
  1835. lpplandtlid := parammaps.Get("id").ValueInt64()
  1836. sort1 := parammaps.Get("sort").ValueInt64()
  1837. for i, lppland := range lpplandList {
  1838. lppland.Sort = int64(i) + 1
  1839. }
  1840. for _, lppland := range lpplandList {
  1841. if tmrid == lppland.Tmrid {
  1842. oldsort = lppland.Sort
  1843. break
  1844. }
  1845. }
  1846. for _, lppland := range lpplandList {
  1847. if lppland.Sort == sort {
  1848. for _, lppland1 := range lpplandList {
  1849. if lppland.Tmrid == lppland1.Tmrid {
  1850. sort = lppland1.Sort
  1851. if oldsort > sort {
  1852. break
  1853. }
  1854. }
  1855. }
  1856. break
  1857. }
  1858. }
  1859. if oldsort == sort {
  1860. appG.Response(http.StatusOK, e.SUCCESS, true)
  1861. return
  1862. }
  1863. tmrLpplandList := make([]*lpplandtl1, 0)
  1864. for _, lppland := range lpplandList {
  1865. if lppland.Tmrid == tmrid {
  1866. tmrLpplandList = append(tmrLpplandList, lppland)
  1867. if lppland.Id == lpplandtlid {
  1868. lpplandtlsort = lppland.Sort
  1869. }
  1870. }
  1871. }
  1872. exist := false
  1873. for _, tmrLppland := range tmrLpplandList {
  1874. if sort1 == tmrLppland.Sort {
  1875. exist = true
  1876. break
  1877. }
  1878. }
  1879. if exist {
  1880. for _, tmrLppland := range tmrLpplandList {
  1881. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  1882. tmrLppland.Sort++
  1883. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  1884. tmrLppland.Sort--
  1885. }
  1886. }
  1887. for _, tmrLppland := range tmrLpplandList {
  1888. if tmrLppland.Id == lpplandtlid {
  1889. tmrLppland.Sort = sort1
  1890. break
  1891. }
  1892. }
  1893. } else {
  1894. if oldsort < sort && len(tmrLpplandList) > 1 {
  1895. for i, tmrLppland := range tmrLpplandList {
  1896. tmrLppland.Sort = sort + int64(i) - 1
  1897. }
  1898. } else {
  1899. for i, tmrLppland := range tmrLpplandList {
  1900. tmrLppland.Sort = sort + int64(i)
  1901. }
  1902. }
  1903. }
  1904. if oldsort > sort {
  1905. for _, lppland := range lpplandList {
  1906. if lppland.Sort == sort {
  1907. // uptmrid = lppland.Tmrid
  1908. sort = lppland.Sort
  1909. break
  1910. }
  1911. }
  1912. } else {
  1913. var uptmrid int64
  1914. for _, lppland := range lpplandList {
  1915. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  1916. uptmrid = lppland.Tmrid
  1917. sort = lppland.Sort
  1918. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  1919. sort = lppland.Sort
  1920. }
  1921. }
  1922. }
  1923. upcount := int64(len(tmrLpplandList))
  1924. if oldsort > sort {
  1925. for _, lppland := range lpplandList {
  1926. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  1927. lppland.Sort += upcount
  1928. tmrLpplandList = append(tmrLpplandList, lppland)
  1929. }
  1930. }
  1931. } else {
  1932. for _, lppland := range lpplandList {
  1933. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  1934. lppland.Sort -= upcount
  1935. tmrLpplandList = append(tmrLpplandList, lppland)
  1936. }
  1937. }
  1938. }
  1939. updateLpplandList := make([]*lpplandtl1, 0)
  1940. for _, lppland := range lpplandList {
  1941. exist := false
  1942. for _, tmrLppland := range tmrLpplandList {
  1943. if lppland.Id == tmrLppland.Id {
  1944. exist = true
  1945. break
  1946. }
  1947. }
  1948. if !exist {
  1949. updateLpplandList = append(updateLpplandList, lppland)
  1950. }
  1951. }
  1952. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  1953. // tmrLpplandMap := make(map[int64]bool)
  1954. // for _, item := range tmrLpplandList {
  1955. // tmrLpplandMap[item.Sort] = false
  1956. // }
  1957. for _, item := range tmrLpplandList {
  1958. exist := false
  1959. for _, item1 := range tmrLpplandList {
  1960. if item.Sort == item1.Sort && item.Id != item1.Id {
  1961. exist = true
  1962. }
  1963. }
  1964. if exist {
  1965. appG.Response(http.StatusOK, e.ERROR, "顺序调整有误,请重新调整!!!")
  1966. return
  1967. }
  1968. }
  1969. tx.Begin()
  1970. var wg sync.WaitGroup
  1971. for _, lppland := range tmrLpplandList {
  1972. wg.Add(1)
  1973. go func(lppland *lpplandtl1) {
  1974. defer wg.Done()
  1975. _, err = tx.SQL(`update lpplandtl1 set sort = ? where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
  1976. if err != nil {
  1977. log.Println("UpdateLpplandtlSort-error-2: ", err)
  1978. tx.Rollback()
  1979. appG.Response(http.StatusOK, e.ERROR, err)
  1980. return
  1981. }
  1982. }(lppland)
  1983. }
  1984. wg.Wait()
  1985. err = tx.Commit()
  1986. if err != nil {
  1987. log.Println("UpdateLpplandtlSort-error-3: ", err)
  1988. tx.Rollback()
  1989. appG.Response(http.StatusOK, e.ERROR, err)
  1990. return
  1991. }
  1992. appG.Response(http.StatusOK, e.SUCCESS, true)
  1993. }
  1994. func AddLpplandtlDate(c *gin.Context) {
  1995. appG := app.Gin{C: c}
  1996. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1997. fsion := gofasion.NewFasion(string(dataByte))
  1998. parammaps := fsion.Get("parammaps")
  1999. tx := restful.Engine.NewSession()
  2000. defer tx.Close()
  2001. exist := false
  2002. var err error
  2003. lpplandList := make([]*lpplandtl1, 0)
  2004. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2005. And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
  2006. if err != nil {
  2007. log.Println("AddLpplandtl-error-1: ", err)
  2008. appG.Response(http.StatusOK, e.ERROR, err)
  2009. return
  2010. }
  2011. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  2012. for _, lppand := range lpplandList {
  2013. fmt.Println(parammaps.Get("id").ValueInt64())
  2014. if lppand.Id == parammaps.Get("id").ValueInt64() {
  2015. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  2016. appG.Response(http.StatusOK, e.SUCCESS, true)
  2017. return
  2018. }
  2019. exist = true
  2020. break
  2021. }
  2022. }
  2023. }
  2024. tmrid := parammaps.Get("tmrid").ValueInt64()
  2025. if !exist {
  2026. var sort int64 = -1
  2027. for _, lppand := range lpplandList {
  2028. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  2029. _, err := tx.SQL(" update lpplandtl1date set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  2030. if err != nil {
  2031. log.Println("AddLpplandtl-error-2: ", err)
  2032. appG.Response(http.StatusOK, e.ERROR, err)
  2033. return
  2034. }
  2035. appG.Response(http.StatusOK, e.SUCCESS, true)
  2036. return
  2037. }
  2038. }
  2039. for i, lppand := range lpplandList {
  2040. lppand.Sort = int64(i) + 1
  2041. }
  2042. updateLpplandList := make([]*lpplandtl1, 0)
  2043. for i, lppand := range lpplandList {
  2044. n := i + 1
  2045. if lppand.Tmrid == tmrid {
  2046. sort = int64(n) + 1
  2047. lppand.Sort = int64(n)
  2048. updateLpplandList = append(updateLpplandList, lppand)
  2049. } else if sort > 0 {
  2050. lppand.Sort = int64(n) + 2
  2051. updateLpplandList = append(updateLpplandList, lppand)
  2052. }
  2053. }
  2054. tx.Begin()
  2055. for _, lppand := range updateLpplandList {
  2056. _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? ", lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2057. if err != nil {
  2058. tx.Rollback()
  2059. log.Println("AddLpplandtl-error-2: ", err)
  2060. appG.Response(http.StatusOK, e.ERROR, err)
  2061. return
  2062. }
  2063. }
  2064. if sort < 0 && len(lpplandList) > 0 {
  2065. sort = int64(len(lpplandList) + 1)
  2066. } else if len(lpplandList) == 0 {
  2067. sort = 1
  2068. }
  2069. _, err = tx.SQL(`INSERT INTO lpplandtl1date(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio,date)
  2070. VALUES (?,?,?,?,?,?,?,?,?,
  2071. ?,?,IF(?='',0,?),IF(?='',0,?),?)`,
  2072. parammaps.Get("pastureid").ValueStr(),
  2073. parammaps.Get("lppid").ValueStr(),
  2074. parammaps.Get("barid").ValueStr(),
  2075. parammaps.Get("barname").ValueStr(),
  2076. parammaps.Get("fpdid").ValueStr(),
  2077. parammaps.Get("lweight").ValueStr(),
  2078. sort,
  2079. parammaps.Get("tmrid").ValueStr(),
  2080. parammaps.Get("tmrname").ValueStr(),
  2081. parammaps.Get("fttype").ValueStr(),
  2082. parammaps.Get("background").ValueStr(),
  2083. parammaps.Get("cowcount").ValueStr(),
  2084. parammaps.Get("cowcount").ValueStr(),
  2085. parammaps.Get("ccountradio").ValueStr(),
  2086. parammaps.Get("ccountradio").ValueStr(),
  2087. parammaps.Get("date").ValueStr()).Execute()
  2088. if err != nil {
  2089. tx.Rollback()
  2090. log.Println("AddLpplandtl-error-3: ", err)
  2091. appG.Response(http.StatusOK, e.ERROR, err)
  2092. return
  2093. }
  2094. err = tx.Commit()
  2095. if err != nil {
  2096. tx.Rollback()
  2097. log.Println("AddLpplandtl-error-6: ", err)
  2098. appG.Response(http.StatusOK, e.ERROR, err)
  2099. return
  2100. }
  2101. } else {
  2102. lpplandid := parammaps.Get("id").ValueInt64()
  2103. // updateLpplandList := make([]*lpplandtl1, 0)
  2104. var oldsort int64
  2105. for i, lppand := range lpplandList {
  2106. lppand.Sort = int64(i) + 1
  2107. }
  2108. noup := false
  2109. for i, lppand := range lpplandList {
  2110. if lppand.Id == lpplandid {
  2111. oldsort = lppand.Sort
  2112. b := false
  2113. for _, lppand := range lpplandList {
  2114. if lppand.Tmrid == tmrid {
  2115. noup = false
  2116. b = true
  2117. break
  2118. }
  2119. }
  2120. if !b {
  2121. if i == 0 || i == len(lpplandList)-1 {
  2122. noup = true
  2123. }
  2124. if i != 0 && i < len(lpplandList)-1 {
  2125. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  2126. noup = true
  2127. }
  2128. }
  2129. }
  2130. break
  2131. }
  2132. }
  2133. if noup {
  2134. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2135. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2136. if err != nil {
  2137. tx.Rollback()
  2138. log.Println("AddLpplandtl-error-2: ", err)
  2139. appG.Response(http.StatusOK, e.ERROR, err)
  2140. return
  2141. }
  2142. } else {
  2143. for i, lppand := range lpplandList {
  2144. if lppand.Id == lpplandid {
  2145. if i != 0 {
  2146. if lpplandList[i-1].Tmrid == tmrid {
  2147. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2148. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2149. if err != nil {
  2150. tx.Rollback()
  2151. log.Println("AddLpplandtl-error-2: ", err)
  2152. appG.Response(http.StatusOK, e.ERROR, err)
  2153. return
  2154. }
  2155. tx.Commit()
  2156. appG.Response(http.StatusOK, e.SUCCESS, true)
  2157. return
  2158. }
  2159. }
  2160. if i < len(lpplandList)-1 {
  2161. if lpplandList[i+1].Tmrid == tmrid {
  2162. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ", parammaps.Get("tmrid").ValueStr(),
  2163. parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2164. if err != nil {
  2165. tx.Rollback()
  2166. log.Println("AddLpplandtl-error-2: ", err)
  2167. appG.Response(http.StatusOK, e.ERROR, err)
  2168. return
  2169. }
  2170. tx.Commit()
  2171. appG.Response(http.StatusOK, e.SUCCESS, true)
  2172. return
  2173. }
  2174. }
  2175. break
  2176. }
  2177. }
  2178. exist := false
  2179. sort1 := 0
  2180. a := false
  2181. for _, lppand := range lpplandList {
  2182. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  2183. sort1 = int(lppand.Sort)
  2184. if !a {
  2185. for _, lppand1 := range lpplandList {
  2186. if lppand1.Id == lpplandid {
  2187. lppand1.Tmrid = tmrid
  2188. lppand1.Tmrname = lppand.Tmrname
  2189. a = true
  2190. // lppand1.Sort = lppand.Sort
  2191. break
  2192. }
  2193. }
  2194. }
  2195. exist = true
  2196. }
  2197. }
  2198. if exist {
  2199. if oldsort > int64(sort1) {
  2200. for _, lppand := range lpplandList {
  2201. if lppand.Id == lpplandid {
  2202. lppand.Sort = int64(sort1) + 1
  2203. continue
  2204. }
  2205. if lppand.Sort >= int64(sort1)+1 {
  2206. lppand.Sort++
  2207. }
  2208. }
  2209. } else {
  2210. for _, lppand := range lpplandList {
  2211. if lppand.Id == lpplandid {
  2212. lppand.Sort = int64(sort1) + 1
  2213. continue
  2214. }
  2215. // if lppand.Sort > oldsort {
  2216. // lppand.Sort--
  2217. // }
  2218. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  2219. lppand.Sort--
  2220. } else if lppand.Sort <= int64(sort1)+1 {
  2221. lppand.Sort++
  2222. }
  2223. }
  2224. }
  2225. } else {
  2226. for _, lppand := range lpplandList {
  2227. if lppand.Id == lpplandid {
  2228. lppand.Sort = int64(len(lpplandList)) + 1
  2229. lppand.Tmrid = tmrid
  2230. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  2231. break
  2232. }
  2233. }
  2234. }
  2235. sort.Slice(lpplandList, func(i, j int) bool {
  2236. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  2237. })
  2238. for i, lppand := range lpplandList {
  2239. lppand.Sort = int64(i) + 1
  2240. }
  2241. for _, lppand := range lpplandList {
  2242. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? and date = ? ",
  2243. lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2244. if err != nil {
  2245. tx.Rollback()
  2246. log.Println("AddLpplandtl-error-2: ", err)
  2247. appG.Response(http.StatusOK, e.ERROR, err)
  2248. return
  2249. }
  2250. }
  2251. }
  2252. tx.Commit()
  2253. // lpplandid := parammaps.Get("id").ValueInt64()
  2254. // updateLpplandList := make([]*lpplandtl1, 0)
  2255. // var oldsort int64
  2256. // for i, lppand := range lpplandList {
  2257. // lppand.Sort = int64(i) + 1
  2258. // }
  2259. // for _, lppand := range lpplandList {
  2260. // if lppand.Id == lpplandid {
  2261. // oldsort = lppand.Sort
  2262. // break
  2263. // }
  2264. // }
  2265. // for _, lppand := range lpplandList {
  2266. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  2267. // sort = lppand.Sort
  2268. // break
  2269. // }
  2270. // }
  2271. // if sort == -1 {
  2272. // sort = 1
  2273. // }
  2274. // if oldsort < sort {
  2275. // for _, lppand := range lpplandList {
  2276. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  2277. // lppand.Sort--
  2278. // updateLpplandList = append(updateLpplandList, lppand)
  2279. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  2280. // lppand.Sort++
  2281. // updateLpplandList = append(updateLpplandList, lppand)
  2282. // }
  2283. // }
  2284. // sort--
  2285. // } else {
  2286. // for _, lppand := range lpplandList {
  2287. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  2288. // lppand.Sort++
  2289. // updateLpplandList = append(updateLpplandList, lppand)
  2290. // }
  2291. // }
  2292. // }
  2293. // var tmrlpplandList []*lpplandtl1
  2294. // for _, lppand := range lpplandList {
  2295. // exist := false
  2296. // for _, lppand1 := range updateLpplandList {
  2297. // if lppand.Id == lppand1.Id {
  2298. // exist = true
  2299. // break
  2300. // }
  2301. // }
  2302. // if !exist {
  2303. // tmrlpplandList = append(tmrlpplandList, lppand)
  2304. // }
  2305. // }
  2306. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  2307. // if len(updateLpplandList) > 0 {
  2308. // tx.Begin()
  2309. // for _, lppand := range updateLpplandList {
  2310. // _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  2311. // if err != nil {
  2312. // tx.Rollback()
  2313. // log.Println("AddLpplandtl-error-2: ", err)
  2314. // appG.Response(http.StatusOK, e.ERROR, err)
  2315. // return
  2316. // }
  2317. // }
  2318. // _, 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()
  2319. // if err != nil {
  2320. // tx.Rollback()
  2321. // log.Println("AddLpplandtl-error-2: ", err)
  2322. // appG.Response(http.StatusOK, e.ERROR, err)
  2323. // return
  2324. // }
  2325. // tx.Commit()
  2326. // }
  2327. }
  2328. appG.Response(http.StatusOK, e.SUCCESS, true)
  2329. }
  2330. func UpdateLpplandtlDateSort(c *gin.Context) {
  2331. appG := app.Gin{C: c}
  2332. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2333. fsion := gofasion.NewFasion(string(dataByte))
  2334. parammaps := fsion.Get("parammaps")
  2335. date := parammaps.Get("date").ValueStr()
  2336. tx := restful.Engine.NewSession()
  2337. defer tx.Close()
  2338. var err error
  2339. lpplandList := make([]*lpplandtl1, 0)
  2340. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2341. And("date = ? ", date).OrderBy("sort").Find(&lpplandList)
  2342. if err != nil {
  2343. log.Println("UpdateLpplandtlSort-error-1: ", err)
  2344. appG.Response(http.StatusOK, e.ERROR, err)
  2345. return
  2346. }
  2347. var oldsort, lpplandtlsort int64
  2348. tmrid := parammaps.Get("tmrid").ValueInt64()
  2349. sort := parammaps.Get("sort").ValueInt64()
  2350. lpplandtlid := parammaps.Get("id").ValueInt64()
  2351. sort1 := parammaps.Get("sort").ValueInt64()
  2352. for i, lppland := range lpplandList {
  2353. lppland.Sort = int64(i) + 1
  2354. }
  2355. for _, lppland := range lpplandList {
  2356. if tmrid == lppland.Tmrid {
  2357. oldsort = lppland.Sort
  2358. break
  2359. }
  2360. }
  2361. for _, lppland := range lpplandList {
  2362. if lppland.Sort == sort {
  2363. for _, lppland1 := range lpplandList {
  2364. if lppland.Tmrid == lppland1.Tmrid {
  2365. sort = lppland1.Sort
  2366. if oldsort > sort {
  2367. break
  2368. }
  2369. }
  2370. }
  2371. break
  2372. }
  2373. }
  2374. if oldsort == sort {
  2375. appG.Response(http.StatusOK, e.SUCCESS, true)
  2376. return
  2377. }
  2378. tmrLpplandList := make([]*lpplandtl1, 0)
  2379. for _, lppland := range lpplandList {
  2380. if lppland.Tmrid == tmrid {
  2381. tmrLpplandList = append(tmrLpplandList, lppland)
  2382. if lppland.Id == lpplandtlid {
  2383. lpplandtlsort = lppland.Sort
  2384. }
  2385. }
  2386. }
  2387. exist := false
  2388. for _, tmrLppland := range tmrLpplandList {
  2389. if sort1 == tmrLppland.Sort {
  2390. exist = true
  2391. break
  2392. }
  2393. }
  2394. if exist {
  2395. for _, tmrLppland := range tmrLpplandList {
  2396. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  2397. tmrLppland.Sort++
  2398. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  2399. tmrLppland.Sort--
  2400. }
  2401. }
  2402. for _, tmrLppland := range tmrLpplandList {
  2403. if tmrLppland.Id == lpplandtlid {
  2404. tmrLppland.Sort = sort1
  2405. break
  2406. }
  2407. }
  2408. } else {
  2409. if oldsort < sort && len(tmrLpplandList) > 1 {
  2410. for i, tmrLppland := range tmrLpplandList {
  2411. tmrLppland.Sort = sort + int64(i) - 1
  2412. }
  2413. } else {
  2414. for i, tmrLppland := range tmrLpplandList {
  2415. tmrLppland.Sort = sort + int64(i)
  2416. }
  2417. }
  2418. }
  2419. if oldsort > sort {
  2420. for _, lppland := range lpplandList {
  2421. if lppland.Sort == sort {
  2422. // uptmrid = lppland.Tmrid
  2423. sort = lppland.Sort
  2424. break
  2425. }
  2426. }
  2427. } else {
  2428. var uptmrid int64
  2429. for _, lppland := range lpplandList {
  2430. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  2431. uptmrid = lppland.Tmrid
  2432. sort = lppland.Sort
  2433. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  2434. sort = lppland.Sort
  2435. }
  2436. }
  2437. }
  2438. upcount := int64(len(tmrLpplandList))
  2439. if oldsort > sort {
  2440. for _, lppland := range lpplandList {
  2441. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  2442. lppland.Sort += upcount
  2443. tmrLpplandList = append(tmrLpplandList, lppland)
  2444. }
  2445. }
  2446. } else {
  2447. for _, lppland := range lpplandList {
  2448. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  2449. lppland.Sort -= upcount
  2450. tmrLpplandList = append(tmrLpplandList, lppland)
  2451. }
  2452. }
  2453. }
  2454. updateLpplandList := make([]*lpplandtl1, 0)
  2455. for _, lppland := range lpplandList {
  2456. exist := false
  2457. for _, tmrLppland := range tmrLpplandList {
  2458. if lppland.Id == tmrLppland.Id {
  2459. exist = true
  2460. break
  2461. }
  2462. }
  2463. if !exist {
  2464. updateLpplandList = append(updateLpplandList, lppland)
  2465. }
  2466. }
  2467. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  2468. tx.Begin()
  2469. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  2470. var wg sync.WaitGroup
  2471. for _, lppland := range tmrLpplandList {
  2472. wg.Add(1)
  2473. go func(lppland *lpplandtl1) {
  2474. defer wg.Done()
  2475. _, err = tx.SQL(`update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? `, lppland.Sort, lppland.Id, lppland.Pastureid, date).Execute()
  2476. if err != nil {
  2477. log.Println("UpdateLpplandtlSort-error-2: ", err)
  2478. tx.Rollback()
  2479. appG.Response(http.StatusOK, e.ERROR, err)
  2480. return
  2481. }
  2482. }(lppland)
  2483. }
  2484. wg.Wait()
  2485. err = tx.Commit()
  2486. if err != nil {
  2487. log.Println("UpdateLpplandtlSort-error-3: ", err)
  2488. tx.Rollback()
  2489. appG.Response(http.StatusOK, e.ERROR, err)
  2490. return
  2491. }
  2492. appG.Response(http.StatusOK, e.SUCCESS, true)
  2493. }
  2494. func GetFeedpUndistributed(c *gin.Context) {
  2495. appG := app.Gin{C: c}
  2496. tx := restful.Engine.NewSession()
  2497. defer tx.Close()
  2498. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2499. fsion := gofasion.NewFasion(string(dataByte))
  2500. timesMax := fsion.Get("times").ValueInt()
  2501. pastureid := fsion.Get("pastureid").ValueStr()
  2502. data := make(map[string]interface{})
  2503. for times := 1; times <= timesMax; times++ {
  2504. 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
  2505. inner join feedtemplet ft
  2506. on ft.id = feedp.ftid and ft.pastureid=?
  2507. WHERE feedp.pastureid = ?
  2508. GROUP BY feedp.ftid
  2509. UNION
  2510. 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
  2511. inner join feedtemplet ft
  2512. on ft.id = feedp.ptsfid and ft.pastureid=?
  2513. WHERE feedp.pastureid = ? and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  2514. GROUP BY feedp.ptsfid`
  2515. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  2516. if err != nil {
  2517. log.Println("GetFeedpUndistributed-error-1: ", err)
  2518. appG.Response(http.StatusOK, e.ERROR, nil)
  2519. return
  2520. }
  2521. 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 ,
  2522. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  2523. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  2524. UNION
  2525. 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 ,
  2526. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  2527. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  2528. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  2529. if err != nil {
  2530. log.Println("GetFeedpUndistributed-error-2: ", err)
  2531. appG.Response(http.StatusOK, e.ERROR, nil)
  2532. return
  2533. }
  2534. for _, ft := range ftList {
  2535. arrList := make([]map[string]interface{}, 0)
  2536. arrid := ft["arrid"].(string)
  2537. for _, detail := range ftDetailList {
  2538. if _, ok := detail["ptid"]; ok {
  2539. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2540. detail["background"] = ft["background"]
  2541. arrList = append(arrList, detail)
  2542. }
  2543. } else if _, ok := detail["ptsid"]; ok {
  2544. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2545. detail["background"] = ft["background"]
  2546. arrList = append(arrList, detail)
  2547. }
  2548. }
  2549. }
  2550. if len(arrList) > 0 {
  2551. ft["arrList"] = arrList
  2552. }
  2553. }
  2554. var key string
  2555. if times == 1 {
  2556. key = "one"
  2557. } else if times == 2 {
  2558. key = "two"
  2559. } else if times == 3 {
  2560. key = "three"
  2561. } else if times == 4 {
  2562. key = "four"
  2563. } else if times == 5 {
  2564. key = "five"
  2565. } else {
  2566. key = "six"
  2567. }
  2568. data[key] = ftList
  2569. }
  2570. appG.Response(http.StatusOK, e.SUCCESS, data)
  2571. }