spillage.go 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793
  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. for _, lppand := range lpplandList {
  1576. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1577. _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1578. if err != nil {
  1579. log.Println("AddLpplandtl-error-2: ", err)
  1580. appG.Response(http.StatusOK, e.ERROR, err)
  1581. return
  1582. }
  1583. appG.Response(http.StatusOK, e.SUCCESS, true)
  1584. return
  1585. }
  1586. }
  1587. for i, lppand := range lpplandList {
  1588. lppand.Sort = int64(i) + 1
  1589. }
  1590. updateLpplandList := make([]*lpplandtl1, 0)
  1591. for i, lppand := range lpplandList {
  1592. n := i + 1
  1593. if lppand.Tmrid == tmrid {
  1594. sort = int64(n) + 1
  1595. lppand.Sort = int64(n)
  1596. updateLpplandList = append(updateLpplandList, lppand)
  1597. } else if sort > 0 {
  1598. lppand.Sort = int64(n) + 2
  1599. updateLpplandList = append(updateLpplandList, lppand)
  1600. }
  1601. }
  1602. tx.Begin()
  1603. for _, lppand := range updateLpplandList {
  1604. _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1605. if err != nil {
  1606. tx.Rollback()
  1607. log.Println("AddLpplandtl-error-2: ", err)
  1608. appG.Response(http.StatusOK, e.ERROR, err)
  1609. return
  1610. }
  1611. }
  1612. if sort < 0 && len(lpplandList) > 0 {
  1613. sort = int64(len(lpplandList) + 2)
  1614. } else if len(lpplandList) == 0 {
  1615. sort = 1
  1616. }
  1617. _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1618. VALUES (?,?,?,?,?,?,?,?,?,
  1619. ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1620. parammaps.Get("pastureid").ValueStr(),
  1621. parammaps.Get("lppid").ValueStr(),
  1622. parammaps.Get("barid").ValueStr(),
  1623. parammaps.Get("barname").ValueStr(),
  1624. parammaps.Get("fpdid").ValueStr(),
  1625. parammaps.Get("lweight").ValueStr(),
  1626. sort,
  1627. parammaps.Get("tmrid").ValueStr(),
  1628. parammaps.Get("tmrname").ValueStr(),
  1629. parammaps.Get("fttype").ValueStr(),
  1630. parammaps.Get("background").ValueStr(),
  1631. parammaps.Get("cowcount").ValueStr(),
  1632. parammaps.Get("cowcount").ValueStr(),
  1633. parammaps.Get("ccountradio").ValueStr(),
  1634. parammaps.Get("ccountradio").ValueStr()).Execute()
  1635. if err != nil {
  1636. tx.Rollback()
  1637. log.Println("AddLpplandtl-error-3: ", err)
  1638. appG.Response(http.StatusOK, e.ERROR, err)
  1639. return
  1640. }
  1641. err = tx.Commit()
  1642. if err != nil {
  1643. tx.Rollback()
  1644. log.Println("AddLpplandtl-error-6: ", err)
  1645. appG.Response(http.StatusOK, e.ERROR, err)
  1646. return
  1647. }
  1648. } else {
  1649. lpplandid := parammaps.Get("id").ValueInt64()
  1650. // updateLpplandList := make([]*lpplandtl1, 0)
  1651. var oldsort int64
  1652. for i, lppand := range lpplandList {
  1653. lppand.Sort = int64(i) + 1
  1654. }
  1655. noup := false
  1656. for i, lppand := range lpplandList {
  1657. if lppand.Id == lpplandid {
  1658. oldsort = lppand.Sort
  1659. b := false
  1660. for _, lppand := range lpplandList {
  1661. if lppand.Tmrid == tmrid {
  1662. noup = false
  1663. b = true
  1664. break
  1665. }
  1666. }
  1667. if !b {
  1668. if i == 0 || i == len(lpplandList)-1 {
  1669. noup = true
  1670. }
  1671. if i != 0 && i < len(lpplandList)-1 {
  1672. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  1673. noup = true
  1674. }
  1675. }
  1676. // if lpplandList[i+1].Tmrid == tmrid {
  1677. // _, 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()
  1678. // if err != nil {
  1679. // tx.Rollback()
  1680. // log.Println("AddLpplandtl-error-2: ", err)
  1681. // appG.Response(http.StatusOK, e.ERROR, err)
  1682. // return
  1683. // }
  1684. // tx.Commit()
  1685. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1686. // return
  1687. // }
  1688. // }
  1689. // break
  1690. // }
  1691. // }
  1692. }
  1693. break
  1694. }
  1695. }
  1696. if noup {
  1697. _, 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()
  1698. if err != nil {
  1699. tx.Rollback()
  1700. log.Println("AddLpplandtl-error-2: ", err)
  1701. appG.Response(http.StatusOK, e.ERROR, err)
  1702. return
  1703. }
  1704. } else {
  1705. for i, lppand := range lpplandList {
  1706. if lppand.Id == lpplandid {
  1707. if i != 0 {
  1708. if lpplandList[i-1].Tmrid == tmrid {
  1709. _, 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()
  1710. if err != nil {
  1711. tx.Rollback()
  1712. log.Println("AddLpplandtl-error-2: ", err)
  1713. appG.Response(http.StatusOK, e.ERROR, err)
  1714. return
  1715. }
  1716. tx.Commit()
  1717. appG.Response(http.StatusOK, e.SUCCESS, true)
  1718. return
  1719. }
  1720. }
  1721. if i < len(lpplandList)-1 {
  1722. if lpplandList[i+1].Tmrid == tmrid {
  1723. _, 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()
  1724. if err != nil {
  1725. tx.Rollback()
  1726. log.Println("AddLpplandtl-error-2: ", err)
  1727. appG.Response(http.StatusOK, e.ERROR, err)
  1728. return
  1729. }
  1730. tx.Commit()
  1731. appG.Response(http.StatusOK, e.SUCCESS, true)
  1732. return
  1733. }
  1734. }
  1735. break
  1736. }
  1737. }
  1738. exist := false
  1739. sort1 := 0
  1740. a := false
  1741. for _, lppand := range lpplandList {
  1742. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  1743. sort1 = int(lppand.Sort)
  1744. if !a {
  1745. for _, lppand1 := range lpplandList {
  1746. if lppand1.Id == lpplandid {
  1747. lppand1.Tmrid = tmrid
  1748. lppand1.Tmrname = lppand.Tmrname
  1749. a = true
  1750. // lppand1.Sort = lppand.Sort
  1751. break
  1752. }
  1753. }
  1754. }
  1755. exist = true
  1756. }
  1757. }
  1758. if exist {
  1759. if oldsort > int64(sort1) {
  1760. for _, lppand := range lpplandList {
  1761. if lppand.Id == lpplandid {
  1762. lppand.Sort = int64(sort1) + 1
  1763. continue
  1764. }
  1765. if lppand.Sort >= int64(sort1)+1 {
  1766. lppand.Sort++
  1767. }
  1768. }
  1769. } else {
  1770. for _, lppand := range lpplandList {
  1771. if lppand.Id == lpplandid {
  1772. lppand.Sort = int64(sort1) + 1
  1773. continue
  1774. }
  1775. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  1776. lppand.Sort--
  1777. } else if lppand.Sort <= int64(sort1)+1 {
  1778. lppand.Sort++
  1779. }
  1780. }
  1781. }
  1782. } else {
  1783. for _, lppand := range lpplandList {
  1784. if lppand.Id == lpplandid {
  1785. lppand.Sort = int64(len(lpplandList)) + 1
  1786. lppand.Tmrid = tmrid
  1787. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  1788. break
  1789. }
  1790. }
  1791. }
  1792. sort.Slice(lpplandList, func(i, j int) bool {
  1793. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  1794. })
  1795. for i, lppand := range lpplandList {
  1796. lppand.Sort = int64(i) + 1
  1797. }
  1798. for _, lppand := range lpplandList {
  1799. _, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? ", lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1800. if err != nil {
  1801. tx.Rollback()
  1802. log.Println("AddLpplandtl-error-2: ", err)
  1803. appG.Response(http.StatusOK, e.ERROR, err)
  1804. return
  1805. }
  1806. }
  1807. }
  1808. tx.Commit()
  1809. }
  1810. appG.Response(http.StatusOK, e.SUCCESS, true)
  1811. }
  1812. func UpdateLpplandtlSort(c *gin.Context) {
  1813. appG := app.Gin{C: c}
  1814. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1815. fsion := gofasion.NewFasion(string(dataByte))
  1816. parammaps := fsion.Get("parammaps")
  1817. tx := restful.Engine.NewSession()
  1818. defer tx.Close()
  1819. var err error
  1820. lpplandList := make([]*lpplandtl1, 0)
  1821. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1822. OrderBy("sort").Find(&lpplandList)
  1823. if err != nil {
  1824. log.Println("UpdateLpplandtlSort-error-1: ", err)
  1825. appG.Response(http.StatusOK, e.ERROR, err)
  1826. return
  1827. }
  1828. var oldsort, lpplandtlsort int64
  1829. tmrid := parammaps.Get("tmrid").ValueInt64()
  1830. sort := parammaps.Get("sort").ValueInt64()
  1831. lpplandtlid := parammaps.Get("id").ValueInt64()
  1832. sort1 := parammaps.Get("sort").ValueInt64()
  1833. for i, lppland := range lpplandList {
  1834. lppland.Sort = int64(i) + 1
  1835. }
  1836. for _, lppland := range lpplandList {
  1837. if tmrid == lppland.Tmrid {
  1838. oldsort = lppland.Sort
  1839. break
  1840. }
  1841. }
  1842. for _, lppland := range lpplandList {
  1843. if lppland.Sort == sort {
  1844. for _, lppland1 := range lpplandList {
  1845. if lppland.Tmrid == lppland1.Tmrid {
  1846. sort = lppland1.Sort
  1847. if oldsort > sort {
  1848. break
  1849. }
  1850. }
  1851. }
  1852. break
  1853. }
  1854. }
  1855. if oldsort == sort {
  1856. appG.Response(http.StatusOK, e.SUCCESS, true)
  1857. return
  1858. }
  1859. tmrLpplandList := make([]*lpplandtl1, 0)
  1860. for _, lppland := range lpplandList {
  1861. if lppland.Tmrid == tmrid {
  1862. tmrLpplandList = append(tmrLpplandList, lppland)
  1863. if lppland.Id == lpplandtlid {
  1864. lpplandtlsort = lppland.Sort
  1865. }
  1866. }
  1867. }
  1868. exist := false
  1869. for _, tmrLppland := range tmrLpplandList {
  1870. if sort1 == tmrLppland.Sort {
  1871. exist = true
  1872. break
  1873. }
  1874. }
  1875. if exist {
  1876. for _, tmrLppland := range tmrLpplandList {
  1877. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  1878. tmrLppland.Sort++
  1879. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  1880. tmrLppland.Sort--
  1881. }
  1882. }
  1883. for _, tmrLppland := range tmrLpplandList {
  1884. if tmrLppland.Id == lpplandtlid {
  1885. tmrLppland.Sort = sort1
  1886. break
  1887. }
  1888. }
  1889. } else {
  1890. if oldsort < sort && len(tmrLpplandList) > 1 {
  1891. for i, tmrLppland := range tmrLpplandList {
  1892. tmrLppland.Sort = sort + int64(i) - 1
  1893. }
  1894. } else {
  1895. for i, tmrLppland := range tmrLpplandList {
  1896. tmrLppland.Sort = sort + int64(i)
  1897. }
  1898. }
  1899. }
  1900. if oldsort > sort {
  1901. for _, lppland := range lpplandList {
  1902. if lppland.Sort == sort {
  1903. // uptmrid = lppland.Tmrid
  1904. sort = lppland.Sort
  1905. break
  1906. }
  1907. }
  1908. } else {
  1909. var uptmrid int64
  1910. for _, lppland := range lpplandList {
  1911. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  1912. uptmrid = lppland.Tmrid
  1913. sort = lppland.Sort
  1914. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  1915. sort = lppland.Sort
  1916. }
  1917. }
  1918. }
  1919. upcount := int64(len(tmrLpplandList))
  1920. if oldsort > sort {
  1921. for _, lppland := range lpplandList {
  1922. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  1923. lppland.Sort += upcount
  1924. tmrLpplandList = append(tmrLpplandList, lppland)
  1925. }
  1926. }
  1927. } else {
  1928. for _, lppland := range lpplandList {
  1929. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  1930. lppland.Sort -= upcount
  1931. tmrLpplandList = append(tmrLpplandList, lppland)
  1932. }
  1933. }
  1934. }
  1935. updateLpplandList := make([]*lpplandtl1, 0)
  1936. for _, lppland := range lpplandList {
  1937. exist := false
  1938. for _, tmrLppland := range tmrLpplandList {
  1939. if lppland.Id == tmrLppland.Id {
  1940. exist = true
  1941. break
  1942. }
  1943. }
  1944. if !exist {
  1945. updateLpplandList = append(updateLpplandList, lppland)
  1946. }
  1947. }
  1948. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  1949. // tmrLpplandMap := make(map[int64]bool)
  1950. // for _, item := range tmrLpplandList {
  1951. // tmrLpplandMap[item.Sort] = false
  1952. // }
  1953. for _, item := range tmrLpplandList {
  1954. exist := false
  1955. for _, item1 := range tmrLpplandList {
  1956. if item.Sort == item1.Sort && item.Id != item1.Id {
  1957. exist = true
  1958. }
  1959. }
  1960. if exist {
  1961. appG.Response(http.StatusOK, e.ERROR, "顺序调整有误,请重新调整!!!")
  1962. return
  1963. }
  1964. }
  1965. tx.Begin()
  1966. var wg sync.WaitGroup
  1967. for _, lppland := range tmrLpplandList {
  1968. wg.Add(1)
  1969. go func(lppland *lpplandtl1) {
  1970. defer wg.Done()
  1971. _, err = tx.SQL(`update lpplandtl1 set sort = ? where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
  1972. if err != nil {
  1973. log.Println("UpdateLpplandtlSort-error-2: ", err)
  1974. tx.Rollback()
  1975. appG.Response(http.StatusOK, e.ERROR, err)
  1976. return
  1977. }
  1978. }(lppland)
  1979. }
  1980. wg.Wait()
  1981. err = tx.Commit()
  1982. if err != nil {
  1983. log.Println("UpdateLpplandtlSort-error-3: ", err)
  1984. tx.Rollback()
  1985. appG.Response(http.StatusOK, e.ERROR, err)
  1986. return
  1987. }
  1988. appG.Response(http.StatusOK, e.SUCCESS, true)
  1989. }
  1990. func AddLpplandtlDate(c *gin.Context) {
  1991. appG := app.Gin{C: c}
  1992. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1993. fsion := gofasion.NewFasion(string(dataByte))
  1994. parammaps := fsion.Get("parammaps")
  1995. tx := restful.Engine.NewSession()
  1996. defer tx.Close()
  1997. exist := false
  1998. var err error
  1999. lpplandList := make([]*lpplandtl1, 0)
  2000. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2001. And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
  2002. if err != nil {
  2003. log.Println("AddLpplandtl-error-1: ", err)
  2004. appG.Response(http.StatusOK, e.ERROR, err)
  2005. return
  2006. }
  2007. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  2008. for _, lppand := range lpplandList {
  2009. fmt.Println(parammaps.Get("id").ValueInt64())
  2010. if lppand.Id == parammaps.Get("id").ValueInt64() {
  2011. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  2012. appG.Response(http.StatusOK, e.SUCCESS, true)
  2013. return
  2014. }
  2015. exist = true
  2016. break
  2017. }
  2018. }
  2019. }
  2020. tmrid := parammaps.Get("tmrid").ValueInt64()
  2021. if !exist {
  2022. var sort int64 = -1
  2023. for _, lppand := range lpplandList {
  2024. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  2025. _, err := tx.SQL(" update lpplandtl1date set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  2026. if err != nil {
  2027. log.Println("AddLpplandtl-error-2: ", err)
  2028. appG.Response(http.StatusOK, e.ERROR, err)
  2029. return
  2030. }
  2031. appG.Response(http.StatusOK, e.SUCCESS, true)
  2032. return
  2033. }
  2034. }
  2035. for i, lppand := range lpplandList {
  2036. lppand.Sort = int64(i) + 1
  2037. }
  2038. updateLpplandList := make([]*lpplandtl1, 0)
  2039. for i, lppand := range lpplandList {
  2040. n := i + 1
  2041. if lppand.Tmrid == tmrid {
  2042. sort = int64(n) + 1
  2043. lppand.Sort = int64(n)
  2044. updateLpplandList = append(updateLpplandList, lppand)
  2045. } else if sort > 0 {
  2046. lppand.Sort = int64(n) + 2
  2047. updateLpplandList = append(updateLpplandList, lppand)
  2048. }
  2049. }
  2050. tx.Begin()
  2051. for _, lppand := range updateLpplandList {
  2052. _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? ", lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2053. if err != nil {
  2054. tx.Rollback()
  2055. log.Println("AddLpplandtl-error-2: ", err)
  2056. appG.Response(http.StatusOK, e.ERROR, err)
  2057. return
  2058. }
  2059. }
  2060. if sort < 0 && len(lpplandList) > 0 {
  2061. sort = int64(len(lpplandList) + 1)
  2062. } else if len(lpplandList) == 0 {
  2063. sort = 1
  2064. }
  2065. _, err = tx.SQL(`INSERT INTO lpplandtl1date(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio,date)
  2066. VALUES (?,?,?,?,?,?,?,?,?,
  2067. ?,?,IF(?='',0,?),IF(?='',0,?),?)`,
  2068. parammaps.Get("pastureid").ValueStr(),
  2069. parammaps.Get("lppid").ValueStr(),
  2070. parammaps.Get("barid").ValueStr(),
  2071. parammaps.Get("barname").ValueStr(),
  2072. parammaps.Get("fpdid").ValueStr(),
  2073. parammaps.Get("lweight").ValueStr(),
  2074. sort,
  2075. parammaps.Get("tmrid").ValueStr(),
  2076. parammaps.Get("tmrname").ValueStr(),
  2077. parammaps.Get("fttype").ValueStr(),
  2078. parammaps.Get("background").ValueStr(),
  2079. parammaps.Get("cowcount").ValueStr(),
  2080. parammaps.Get("cowcount").ValueStr(),
  2081. parammaps.Get("ccountradio").ValueStr(),
  2082. parammaps.Get("ccountradio").ValueStr(),
  2083. parammaps.Get("date").ValueStr()).Execute()
  2084. if err != nil {
  2085. tx.Rollback()
  2086. log.Println("AddLpplandtl-error-3: ", err)
  2087. appG.Response(http.StatusOK, e.ERROR, err)
  2088. return
  2089. }
  2090. err = tx.Commit()
  2091. if err != nil {
  2092. tx.Rollback()
  2093. log.Println("AddLpplandtl-error-6: ", err)
  2094. appG.Response(http.StatusOK, e.ERROR, err)
  2095. return
  2096. }
  2097. } else {
  2098. lpplandid := parammaps.Get("id").ValueInt64()
  2099. // updateLpplandList := make([]*lpplandtl1, 0)
  2100. var oldsort int64
  2101. for i, lppand := range lpplandList {
  2102. lppand.Sort = int64(i) + 1
  2103. }
  2104. noup := false
  2105. for i, lppand := range lpplandList {
  2106. if lppand.Id == lpplandid {
  2107. oldsort = lppand.Sort
  2108. b := false
  2109. for _, lppand := range lpplandList {
  2110. if lppand.Tmrid == tmrid {
  2111. noup = false
  2112. b = true
  2113. break
  2114. }
  2115. }
  2116. if !b {
  2117. if i == 0 || i == len(lpplandList)-1 {
  2118. noup = true
  2119. }
  2120. if i != 0 && i < len(lpplandList)-1 {
  2121. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  2122. noup = true
  2123. }
  2124. }
  2125. }
  2126. break
  2127. }
  2128. }
  2129. if noup {
  2130. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2131. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2132. if err != nil {
  2133. tx.Rollback()
  2134. log.Println("AddLpplandtl-error-2: ", err)
  2135. appG.Response(http.StatusOK, e.ERROR, err)
  2136. return
  2137. }
  2138. } else {
  2139. for i, lppand := range lpplandList {
  2140. if lppand.Id == lpplandid {
  2141. if i != 0 {
  2142. if lpplandList[i-1].Tmrid == tmrid {
  2143. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2144. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2145. if err != nil {
  2146. tx.Rollback()
  2147. log.Println("AddLpplandtl-error-2: ", err)
  2148. appG.Response(http.StatusOK, e.ERROR, err)
  2149. return
  2150. }
  2151. tx.Commit()
  2152. appG.Response(http.StatusOK, e.SUCCESS, true)
  2153. return
  2154. }
  2155. }
  2156. if i < len(lpplandList)-1 {
  2157. if lpplandList[i+1].Tmrid == tmrid {
  2158. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ", parammaps.Get("tmrid").ValueStr(),
  2159. parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2160. if err != nil {
  2161. tx.Rollback()
  2162. log.Println("AddLpplandtl-error-2: ", err)
  2163. appG.Response(http.StatusOK, e.ERROR, err)
  2164. return
  2165. }
  2166. tx.Commit()
  2167. appG.Response(http.StatusOK, e.SUCCESS, true)
  2168. return
  2169. }
  2170. }
  2171. break
  2172. }
  2173. }
  2174. exist := false
  2175. sort1 := 0
  2176. a := false
  2177. for _, lppand := range lpplandList {
  2178. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  2179. sort1 = int(lppand.Sort)
  2180. if !a {
  2181. for _, lppand1 := range lpplandList {
  2182. if lppand1.Id == lpplandid {
  2183. lppand1.Tmrid = tmrid
  2184. lppand1.Tmrname = lppand.Tmrname
  2185. a = true
  2186. // lppand1.Sort = lppand.Sort
  2187. break
  2188. }
  2189. }
  2190. }
  2191. exist = true
  2192. }
  2193. }
  2194. if exist {
  2195. if oldsort > int64(sort1) {
  2196. for _, lppand := range lpplandList {
  2197. if lppand.Id == lpplandid {
  2198. lppand.Sort = int64(sort1) + 1
  2199. continue
  2200. }
  2201. if lppand.Sort >= int64(sort1)+1 {
  2202. lppand.Sort++
  2203. }
  2204. }
  2205. } else {
  2206. for _, lppand := range lpplandList {
  2207. if lppand.Id == lpplandid {
  2208. lppand.Sort = int64(sort1) + 1
  2209. continue
  2210. }
  2211. // if lppand.Sort > oldsort {
  2212. // lppand.Sort--
  2213. // }
  2214. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  2215. lppand.Sort--
  2216. } else if lppand.Sort <= int64(sort1)+1 {
  2217. lppand.Sort++
  2218. }
  2219. }
  2220. }
  2221. } else {
  2222. for _, lppand := range lpplandList {
  2223. if lppand.Id == lpplandid {
  2224. lppand.Sort = int64(len(lpplandList)) + 1
  2225. lppand.Tmrid = tmrid
  2226. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  2227. break
  2228. }
  2229. }
  2230. }
  2231. sort.Slice(lpplandList, func(i, j int) bool {
  2232. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  2233. })
  2234. for i, lppand := range lpplandList {
  2235. lppand.Sort = int64(i) + 1
  2236. }
  2237. for _, lppand := range lpplandList {
  2238. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? and date = ? ",
  2239. lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2240. if err != nil {
  2241. tx.Rollback()
  2242. log.Println("AddLpplandtl-error-2: ", err)
  2243. appG.Response(http.StatusOK, e.ERROR, err)
  2244. return
  2245. }
  2246. }
  2247. }
  2248. tx.Commit()
  2249. // lpplandid := parammaps.Get("id").ValueInt64()
  2250. // updateLpplandList := make([]*lpplandtl1, 0)
  2251. // var oldsort int64
  2252. // for i, lppand := range lpplandList {
  2253. // lppand.Sort = int64(i) + 1
  2254. // }
  2255. // for _, lppand := range lpplandList {
  2256. // if lppand.Id == lpplandid {
  2257. // oldsort = lppand.Sort
  2258. // break
  2259. // }
  2260. // }
  2261. // for _, lppand := range lpplandList {
  2262. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  2263. // sort = lppand.Sort
  2264. // break
  2265. // }
  2266. // }
  2267. // if sort == -1 {
  2268. // sort = 1
  2269. // }
  2270. // if oldsort < sort {
  2271. // for _, lppand := range lpplandList {
  2272. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  2273. // lppand.Sort--
  2274. // updateLpplandList = append(updateLpplandList, lppand)
  2275. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  2276. // lppand.Sort++
  2277. // updateLpplandList = append(updateLpplandList, lppand)
  2278. // }
  2279. // }
  2280. // sort--
  2281. // } else {
  2282. // for _, lppand := range lpplandList {
  2283. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  2284. // lppand.Sort++
  2285. // updateLpplandList = append(updateLpplandList, lppand)
  2286. // }
  2287. // }
  2288. // }
  2289. // var tmrlpplandList []*lpplandtl1
  2290. // for _, lppand := range lpplandList {
  2291. // exist := false
  2292. // for _, lppand1 := range updateLpplandList {
  2293. // if lppand.Id == lppand1.Id {
  2294. // exist = true
  2295. // break
  2296. // }
  2297. // }
  2298. // if !exist {
  2299. // tmrlpplandList = append(tmrlpplandList, lppand)
  2300. // }
  2301. // }
  2302. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  2303. // if len(updateLpplandList) > 0 {
  2304. // tx.Begin()
  2305. // for _, lppand := range updateLpplandList {
  2306. // _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  2307. // if err != nil {
  2308. // tx.Rollback()
  2309. // log.Println("AddLpplandtl-error-2: ", err)
  2310. // appG.Response(http.StatusOK, e.ERROR, err)
  2311. // return
  2312. // }
  2313. // }
  2314. // _, 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()
  2315. // if err != nil {
  2316. // tx.Rollback()
  2317. // log.Println("AddLpplandtl-error-2: ", err)
  2318. // appG.Response(http.StatusOK, e.ERROR, err)
  2319. // return
  2320. // }
  2321. // tx.Commit()
  2322. // }
  2323. }
  2324. appG.Response(http.StatusOK, e.SUCCESS, true)
  2325. }
  2326. func UpdateLpplandtlDateSort(c *gin.Context) {
  2327. appG := app.Gin{C: c}
  2328. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2329. fsion := gofasion.NewFasion(string(dataByte))
  2330. parammaps := fsion.Get("parammaps")
  2331. date := parammaps.Get("date").ValueStr()
  2332. tx := restful.Engine.NewSession()
  2333. defer tx.Close()
  2334. var err error
  2335. lpplandList := make([]*lpplandtl1, 0)
  2336. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2337. And("date = ? ", date).OrderBy("sort").Find(&lpplandList)
  2338. if err != nil {
  2339. log.Println("UpdateLpplandtlSort-error-1: ", err)
  2340. appG.Response(http.StatusOK, e.ERROR, err)
  2341. return
  2342. }
  2343. var oldsort, lpplandtlsort int64
  2344. tmrid := parammaps.Get("tmrid").ValueInt64()
  2345. sort := parammaps.Get("sort").ValueInt64()
  2346. lpplandtlid := parammaps.Get("id").ValueInt64()
  2347. sort1 := parammaps.Get("sort").ValueInt64()
  2348. for i, lppland := range lpplandList {
  2349. lppland.Sort = int64(i) + 1
  2350. }
  2351. for _, lppland := range lpplandList {
  2352. if tmrid == lppland.Tmrid {
  2353. oldsort = lppland.Sort
  2354. break
  2355. }
  2356. }
  2357. for _, lppland := range lpplandList {
  2358. if lppland.Sort == sort {
  2359. for _, lppland1 := range lpplandList {
  2360. if lppland.Tmrid == lppland1.Tmrid {
  2361. sort = lppland1.Sort
  2362. if oldsort > sort {
  2363. break
  2364. }
  2365. }
  2366. }
  2367. break
  2368. }
  2369. }
  2370. if oldsort == sort {
  2371. appG.Response(http.StatusOK, e.SUCCESS, true)
  2372. return
  2373. }
  2374. tmrLpplandList := make([]*lpplandtl1, 0)
  2375. for _, lppland := range lpplandList {
  2376. if lppland.Tmrid == tmrid {
  2377. tmrLpplandList = append(tmrLpplandList, lppland)
  2378. if lppland.Id == lpplandtlid {
  2379. lpplandtlsort = lppland.Sort
  2380. }
  2381. }
  2382. }
  2383. exist := false
  2384. for _, tmrLppland := range tmrLpplandList {
  2385. if sort1 == tmrLppland.Sort {
  2386. exist = true
  2387. break
  2388. }
  2389. }
  2390. if exist {
  2391. for _, tmrLppland := range tmrLpplandList {
  2392. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  2393. tmrLppland.Sort++
  2394. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  2395. tmrLppland.Sort--
  2396. }
  2397. }
  2398. for _, tmrLppland := range tmrLpplandList {
  2399. if tmrLppland.Id == lpplandtlid {
  2400. tmrLppland.Sort = sort1
  2401. break
  2402. }
  2403. }
  2404. } else {
  2405. if oldsort < sort && len(tmrLpplandList) > 1 {
  2406. for i, tmrLppland := range tmrLpplandList {
  2407. tmrLppland.Sort = sort + int64(i) - 1
  2408. }
  2409. } else {
  2410. for i, tmrLppland := range tmrLpplandList {
  2411. tmrLppland.Sort = sort + int64(i)
  2412. }
  2413. }
  2414. }
  2415. if oldsort > sort {
  2416. for _, lppland := range lpplandList {
  2417. if lppland.Sort == sort {
  2418. // uptmrid = lppland.Tmrid
  2419. sort = lppland.Sort
  2420. break
  2421. }
  2422. }
  2423. } else {
  2424. var uptmrid int64
  2425. for _, lppland := range lpplandList {
  2426. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  2427. uptmrid = lppland.Tmrid
  2428. sort = lppland.Sort
  2429. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  2430. sort = lppland.Sort
  2431. }
  2432. }
  2433. }
  2434. upcount := int64(len(tmrLpplandList))
  2435. if oldsort > sort {
  2436. for _, lppland := range lpplandList {
  2437. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  2438. lppland.Sort += upcount
  2439. tmrLpplandList = append(tmrLpplandList, lppland)
  2440. }
  2441. }
  2442. } else {
  2443. for _, lppland := range lpplandList {
  2444. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  2445. lppland.Sort -= upcount
  2446. tmrLpplandList = append(tmrLpplandList, lppland)
  2447. }
  2448. }
  2449. }
  2450. updateLpplandList := make([]*lpplandtl1, 0)
  2451. for _, lppland := range lpplandList {
  2452. exist := false
  2453. for _, tmrLppland := range tmrLpplandList {
  2454. if lppland.Id == tmrLppland.Id {
  2455. exist = true
  2456. break
  2457. }
  2458. }
  2459. if !exist {
  2460. updateLpplandList = append(updateLpplandList, lppland)
  2461. }
  2462. }
  2463. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  2464. tx.Begin()
  2465. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  2466. var wg sync.WaitGroup
  2467. for _, lppland := range tmrLpplandList {
  2468. wg.Add(1)
  2469. go func(lppland *lpplandtl1) {
  2470. defer wg.Done()
  2471. _, err = tx.SQL(`update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? `, lppland.Sort, lppland.Id, lppland.Pastureid, date).Execute()
  2472. if err != nil {
  2473. log.Println("UpdateLpplandtlSort-error-2: ", err)
  2474. tx.Rollback()
  2475. appG.Response(http.StatusOK, e.ERROR, err)
  2476. return
  2477. }
  2478. }(lppland)
  2479. }
  2480. wg.Wait()
  2481. err = tx.Commit()
  2482. if err != nil {
  2483. log.Println("UpdateLpplandtlSort-error-3: ", err)
  2484. tx.Rollback()
  2485. appG.Response(http.StatusOK, e.ERROR, err)
  2486. return
  2487. }
  2488. appG.Response(http.StatusOK, e.SUCCESS, true)
  2489. }
  2490. func GetFeedpUndistributed(c *gin.Context) {
  2491. appG := app.Gin{C: c}
  2492. tx := restful.Engine.NewSession()
  2493. defer tx.Close()
  2494. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2495. fsion := gofasion.NewFasion(string(dataByte))
  2496. timesMax := fsion.Get("times").ValueInt()
  2497. pastureid := fsion.Get("pastureid").ValueStr()
  2498. data := make(map[string]interface{})
  2499. for times := 1; times <= timesMax; times++ {
  2500. 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
  2501. inner join feedtemplet ft
  2502. on ft.id = feedp.ftid and ft.pastureid=?
  2503. WHERE feedp.pastureid = ?
  2504. GROUP BY feedp.ftid
  2505. UNION
  2506. 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
  2507. inner join feedtemplet ft
  2508. on ft.id = feedp.ptsfid and ft.pastureid=?
  2509. WHERE feedp.pastureid = ? and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  2510. GROUP BY feedp.ptsfid`
  2511. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  2512. if err != nil {
  2513. log.Println("GetFeedpUndistributed-error-1: ", err)
  2514. appG.Response(http.StatusOK, e.ERROR, nil)
  2515. return
  2516. }
  2517. 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 ,
  2518. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  2519. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  2520. UNION
  2521. 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 ,
  2522. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  2523. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  2524. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  2525. if err != nil {
  2526. log.Println("GetFeedpUndistributed-error-2: ", err)
  2527. appG.Response(http.StatusOK, e.ERROR, nil)
  2528. return
  2529. }
  2530. for _, ft := range ftList {
  2531. arrList := make([]map[string]interface{}, 0)
  2532. arrid := ft["arrid"].(string)
  2533. for _, detail := range ftDetailList {
  2534. if _, ok := detail["ptid"]; ok {
  2535. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2536. detail["background"] = ft["background"]
  2537. arrList = append(arrList, detail)
  2538. }
  2539. } else if _, ok := detail["ptsid"]; ok {
  2540. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2541. detail["background"] = ft["background"]
  2542. arrList = append(arrList, detail)
  2543. }
  2544. }
  2545. }
  2546. if len(arrList) > 0 {
  2547. ft["arrList"] = arrList
  2548. }
  2549. }
  2550. var key string
  2551. if times == 1 {
  2552. key = "one"
  2553. } else if times == 2 {
  2554. key = "two"
  2555. } else if times == 3 {
  2556. key = "three"
  2557. } else if times == 4 {
  2558. key = "four"
  2559. } else if times == 5 {
  2560. key = "five"
  2561. } else {
  2562. key = "six"
  2563. }
  2564. data[key] = ftList
  2565. }
  2566. appG.Response(http.StatusOK, e.SUCCESS, data)
  2567. }