spillage.go 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  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. // SELECT TRIM(id) id,times,tratio,ROUND(ptsrate*weight,2)-ptsuse weight,TRIM(barid) barid,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
  724. // (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,
  725. // ? AS background,? AS fttype,cowcount,ccountradio, 1 isfill FROM fpdetaildate fpdetail
  726. // WHERE fpdetail.pastureid = ? AND ( fpdetail.`ptsid`=?) AND times=? AND ptsuse< ROUND(ptsrate*weight,2) and fpdetail.date = ?
  727. // UNION
  728. // 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 ,
  729. // (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,
  730. // ? AS background,? AS fttype,cowcount,ccountradio, 0 isfill FROM fpdetaildate fpdetail
  731. // WHERE fpdetail.pastureid = ? AND (fpdetail.`ptid`=? ) AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2) and fpdetail.date = ?
  732. // ORDER BY barid
  733. if ftid != 0 {
  734. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  735. } else {
  736. sqlStr = fmt.Sprintf(sqlStr, "", "")
  737. }
  738. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  739. if err != nil {
  740. log.Println("GetSpillage-error-1: ", err)
  741. appG.Response(http.StatusOK, e.ERROR, nil)
  742. return
  743. }
  744. 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 ,
  745. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  746. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ifnull(ptsrate,0)*weight,2)
  747. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  748. UNION
  749. 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 ,
  750. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  751. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2)
  752. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  753. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  754. if err != nil {
  755. log.Println("GetSpillage-error-2: ", err)
  756. appG.Response(http.StatusOK, e.ERROR, nil)
  757. return
  758. }
  759. for _, ft := range ftList {
  760. arrList := make([]map[string]interface{}, 0)
  761. arrid := ft["arrid"].(string)
  762. for _, detail := range ftDetailList {
  763. if _, ok := detail["ptid"]; ok {
  764. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  765. detail["background"] = ft["background"]
  766. arrList = append(arrList, detail)
  767. }
  768. } else if _, ok := detail["ptsid"]; ok {
  769. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  770. detail["background"] = ft["background"]
  771. arrList = append(arrList, detail)
  772. }
  773. }
  774. }
  775. if len(arrList) > 0 {
  776. ft["arrList"] = arrList
  777. }
  778. }
  779. data["ftlist"] = ftList
  780. }
  781. if refresh == 0 || refresh == 1 {
  782. sqlstr := `SELECT
  783. ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) tmrname,trim(tmrid) tmrid,sort,sel,
  784. CASE times
  785. WHEN 1 THEN
  786. '第一班'
  787. WHEN 2 THEN
  788. '第二班'
  789. WHEN 3 THEN
  790. '第三班'
  791. WHEN 4 THEN
  792. '第四班'
  793. END timesstr,issplit,
  794. times,display,begintime,
  795. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  796. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1
  797. inner join fpdetail
  798. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  799. WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.pastureid =lpplan.pastureid ),2),0) sumweight ,
  800. ifnull((select maxstirfeed from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),100000) maxweight,
  801. TRIM(ftid) ftid,
  802. TRIM(id) id,
  803. TRIM(id) lppid,
  804. TRIM(pastureid) pastureid
  805. FROM
  806. lpplan
  807. WHERE pastureid = ? and times <= (SELECT sysopt.inforvalue FROM sysopt
  808. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  809. `
  810. arrsql := `SELECT
  811. trim(lpplandtl1.lppid) lppid,lweight,
  812. lpplandtl1.lweight weight,lpplandtl1.sort,
  813. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  814. TRIM(lpplandtl1.fpdid) fpdid,
  815. TRIM(lpplandtl1.barid) barid,
  816. TRIM(lpplandtl1.id) id,
  817. TRIM(lpplandtl1.pastureid) pastureid,
  818. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  819. lpplandtl1.fttype,
  820. trim(lpplandtl1.tmrid) tmrid,
  821. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  822. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  823. fpdetail.ptid,
  824. fpdetail.ptsid,
  825. fpdetail.times,
  826. fpdetail.cowcount,
  827. fpdetail.ccountradio
  828. FROM
  829. lpplandtl1
  830. inner join fpdetail
  831. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  832. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0
  833. `
  834. var args []interface{}
  835. args = append(args, pastureid)
  836. if times != 0 {
  837. sqlstr += " and times = ? "
  838. arrsql += " and times = ? "
  839. args = append(args, times)
  840. }
  841. sqlstr += "ORDER BY times,lpplan.sort"
  842. session := tx.SQL(sqlstr, args...)
  843. lppList, err := session.Query().List()
  844. if err != nil {
  845. log.Println("GetSpillage-error-3: ", err)
  846. appG.Response(http.StatusOK, e.ERROR, nil)
  847. return
  848. }
  849. arrsql += " ORDER BY lpplandtl1.sort"
  850. arrList, err := tx.SQL(arrsql, args...).QueryString() //获取
  851. if err != nil {
  852. log.Println("GetSpillage-error-4: ", err)
  853. appG.Response(http.StatusOK, e.ERROR, nil)
  854. return
  855. }
  856. for _, query := range lppList {
  857. list := make([]map[string]string, 0)
  858. for _, arr := range arrList {
  859. if query["lppid"].(string) == arr["lppid"] {
  860. list = append(list, arr)
  861. }
  862. }
  863. query["arrList"] = list
  864. }
  865. data["list"] = lppList
  866. data["total"] = len(lppList)
  867. }
  868. appG.Response(http.StatusOK, e.SUCCESS, data)
  869. }
  870. type lpplanTrains struct {
  871. Id string `xorm:"id"`
  872. Sort int64 `xorm:"sort"`
  873. Pastureid string `xorm:"pastureid"`
  874. }
  875. //修改车次顺序
  876. func UpdateTrains(c *gin.Context) {
  877. appG := app.Gin{C: c}
  878. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  879. lppList := make([]*lpplanTrains, 0)
  880. err := json.Unmarshal(dataByte, &lppList)
  881. if err != nil {
  882. log.Println("UpdateTrains-error-1: ", err)
  883. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  884. return
  885. }
  886. tx := restful.Engine.NewSession()
  887. defer tx.Close()
  888. tx.Begin()
  889. for _, item := range lppList {
  890. _, err = tx.Table("lpplan").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  891. if err != nil {
  892. log.Println("UpdateTrains-error-2: ", err)
  893. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  894. tx.Rollback()
  895. return
  896. }
  897. }
  898. err = tx.Commit()
  899. if err != nil {
  900. log.Println("UpdateTrains-error-2: ", err)
  901. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  902. tx.Rollback()
  903. return
  904. }
  905. appG.Response(http.StatusOK, e.SUCCESS, true)
  906. }
  907. func UpdateTrainsDay(c *gin.Context) {
  908. appG := app.Gin{C: c}
  909. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  910. lppList := make([]*lpplanTrains, 0)
  911. err := json.Unmarshal(dataByte, &lppList)
  912. if err != nil {
  913. log.Println("UpdateTrainsDay-error-1: ", err)
  914. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  915. return
  916. }
  917. tx := restful.Engine.NewSession()
  918. defer tx.Close()
  919. tx.Begin()
  920. for _, item := range lppList {
  921. _, err = tx.Table("lpplandate").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  922. if err != nil {
  923. log.Println("UpdateTrainsDay-error-2: ", err)
  924. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  925. tx.Rollback()
  926. return
  927. }
  928. }
  929. err = tx.Commit()
  930. if err != nil {
  931. log.Println("UpdateTrainsDay-error-3: ", err)
  932. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  933. tx.Rollback()
  934. return
  935. }
  936. appG.Response(http.StatusOK, e.SUCCESS, true)
  937. }
  938. type ReqAddLpplan struct {
  939. Id string `xorm:"id"`
  940. Tmrid string `xorm:"tmrid"`
  941. TmrName string `xorm:"tmrname"`
  942. Sort int64 `xorm:"sort"`
  943. Sel int64 `xorm:"sel"`
  944. Times int64 `xorm:"times"`
  945. Ftname string `xorm:"ftname"`
  946. Ftid string `xorm:"ftid"`
  947. Maxweight float64 `xorm:"maxweight"`
  948. // Sumweight int64 `xorm:"sumweight"`
  949. Sumcowcount int64 `xorm:"sumcowcount"`
  950. Pastureid string `xorm:"pastureid"`
  951. Display string `xorm:"display"`
  952. Issplit int64 `xorm:"issplit"`
  953. BeginTime string `xorm:"begintime"`
  954. }
  955. //添加车次并返回添加的车次信息
  956. func AddLpplan(c *gin.Context) {
  957. appG := app.Gin{C: c}
  958. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  959. fsion := gofasion.NewFasion(string(dataByte))
  960. parammaps := fsion.Get("parammaps")
  961. req := new(ReqAddLpplan)
  962. err := json.Unmarshal([]byte(parammaps.Json()), req)
  963. if err != nil {
  964. log.Println("AddLpplan-error-1: ", err)
  965. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  966. return
  967. }
  968. if req.Times == 0 {
  969. log.Println("AddLpplan-error-1: ", err)
  970. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  971. return
  972. }
  973. tx := restful.Engine.NewSession()
  974. defer tx.Close()
  975. ids, err := setting.SnowIds.NextId()
  976. if err != nil {
  977. ids = time.Now().UnixNano()
  978. logging.Info("AddLpplan-error-1: ", err)
  979. }
  980. req.Id = strconv.FormatInt(ids, 10)
  981. datacount, err := tx.Table("lpplan").Select("max(sort) as sort").Where("pastureid = ?", req.Pastureid).And(" times = ?", req.Times).QueryString()
  982. if err != nil {
  983. log.Println("AddLpplan-error-2: ", err)
  984. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  985. return
  986. }
  987. var count int64 = 0
  988. for _, d := range datacount {
  989. if _, ok := d["sort"]; ok {
  990. count, _ = strconv.ParseInt(d["sort"], 10, 64)
  991. }
  992. }
  993. req.Sort = count + 1
  994. _, err = tx.Table("lpplan").Insert(req)
  995. if err != nil {
  996. log.Println("AddLpplan-error-3: ", err)
  997. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  998. return
  999. }
  1000. appG.Response(http.StatusOK, e.SUCCESS, req)
  1001. }
  1002. type ReqAddLpplanDay struct {
  1003. Id string `xorm:"id"`
  1004. Tmrid string `xorm:"tmrid"`
  1005. TmrName string `xorm:"tmrname"`
  1006. Sort int64 `xorm:"sort"`
  1007. Sel int64 `xorm:"sel"`
  1008. Times int64 `xorm:"times"`
  1009. Ftname string `xorm:"ftname"`
  1010. Ftid string `xorm:"ftid"`
  1011. Maxweight float64 `xorm:"maxweight"`
  1012. // Sumweight int64 `xorm:"sumweight"`
  1013. Sumcowcount int64 `xorm:"sumcowcount"`
  1014. Pastureid string `xorm:"pastureid"`
  1015. Display string `xorm:"display"`
  1016. Issplit int64 `xorm:"issplit"`
  1017. BeginTime string `xorm:"begintime"`
  1018. Date string `xorm:"date"`
  1019. }
  1020. func AddLpplanDay(c *gin.Context) {
  1021. appG := app.Gin{C: c}
  1022. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1023. fsion := gofasion.NewFasion(string(dataByte))
  1024. parammaps := fsion.Get("parammaps")
  1025. req := new(ReqAddLpplanDay)
  1026. err := json.Unmarshal([]byte(parammaps.Json()), req)
  1027. if err != nil {
  1028. log.Println("AddLpplanDay-error-1: ", err)
  1029. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1030. return
  1031. }
  1032. if req.Times == 0 {
  1033. log.Println("AddLpplanDay-error-1: ", err)
  1034. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  1035. return
  1036. }
  1037. tx := restful.Engine.NewSession()
  1038. defer tx.Close()
  1039. ids, err := setting.SnowIds.NextId()
  1040. if err != nil {
  1041. ids = time.Now().UnixNano()
  1042. logging.Info("AddLpplanDay-error-1: ", err)
  1043. }
  1044. req.Id = strconv.FormatInt(ids, 10)
  1045. count, err := tx.Table("lpplandate").Where("pastureid = ?", req.Pastureid).And(" date = ?", req.Date).And(" times = ?", req.Times).Count()
  1046. if err != nil {
  1047. log.Println("AddLpplanDay-error-2: ", err)
  1048. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1049. return
  1050. }
  1051. req.Sort = count + 1
  1052. _, err = tx.Table("lpplandate").Insert(req)
  1053. if err != nil {
  1054. log.Println("AddLpplanDay-error-3: ", err)
  1055. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1056. return
  1057. }
  1058. appG.Response(http.StatusOK, e.SUCCESS, req)
  1059. }
  1060. //修改车次信息,如果修改tmrid信息则删除栏舍信息
  1061. func UpdateLpplan(c *gin.Context) {
  1062. appG := app.Gin{C: c}
  1063. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1064. fsion := gofasion.NewFasion(string(dataByte))
  1065. parammaps := fsion.Get("parammaps")
  1066. lpplanid := parammaps.Get("id").ValueStr()
  1067. pastureid := parammaps.Get("pastureid").ValueStr()
  1068. tmrid := parammaps.Get("tmrid").ValueStr()
  1069. tmrname := parammaps.Get("tmrname").ValueStr()
  1070. begintime := parammaps.Get("begintime").ValueStr()
  1071. display := parammaps.Get("display").ValueStr()
  1072. times := parammaps.Get("times").ValueInt64()
  1073. ftid := parammaps.Get("ftid").ValueStr()
  1074. ftname := parammaps.Get("ftname").ValueStr()
  1075. sel := parammaps.Get("sel").ValueInt64()
  1076. issplit := parammaps.Get("issplit").ValueInt64()
  1077. lpplan := new(ReqAddLpplan)
  1078. lpplan.Tmrid = tmrid
  1079. lpplan.TmrName = tmrname
  1080. lpplan.BeginTime = begintime
  1081. lpplan.Sel = sel
  1082. lpplan.Issplit = issplit
  1083. lpplan.Times = times
  1084. lpplan.Ftid = ftid
  1085. lpplan.Ftname = ftname
  1086. lpplan.Display = display
  1087. tx := restful.Engine.NewSession()
  1088. defer tx.Close()
  1089. tx.Begin()
  1090. lpplanData := new(lpplanStruct)
  1091. _, err := tx.Table("lpplan").Select("tmrid").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).Get(lpplanData)
  1092. if err != nil {
  1093. log.Println("UpdateLpplan-error-1: ", err)
  1094. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1095. tx.Rollback()
  1096. return
  1097. }
  1098. _, err = tx.Table("lpplan").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).
  1099. MustCols("display,tmrid,tmrname,begintime,sel,issplit,times,ftid,ftname").Update(lpplan)
  1100. if err != nil {
  1101. log.Println("UpdateLpplan-error-1: ", err)
  1102. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1103. tx.Rollback()
  1104. return
  1105. }
  1106. err = tx.Commit()
  1107. if err != nil {
  1108. log.Println("UpdateLpplan-error-3: ", err)
  1109. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1110. tx.Rollback()
  1111. return
  1112. }
  1113. dataList, err := tx.SQL(`select * from lpplandtl1 where lppid = ? and pastureid = ? `, lpplanid, pastureid).Query().List()
  1114. if err != nil {
  1115. log.Println("UpdateLpplan-error-4: ", err)
  1116. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1117. return
  1118. }
  1119. // var oldbar, bar []int64
  1120. oldList := fsion.Get("old").Array()
  1121. if len(dataList) >= len(oldList) {
  1122. for _, data := range dataList {
  1123. updateStatus := true
  1124. for _, old := range oldList {
  1125. fmt.Println(old.Get("lweight").ValueStr(), data["lweight"], old.Get("barid").ValueInt64(), data["barid"], "aaaaaaaaaaa")
  1126. if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1127. updateStatus = false
  1128. break
  1129. }
  1130. }
  1131. if updateStatus {
  1132. now := time.Now()
  1133. for _, data := range dataList {
  1134. _, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  1135. background,cowcount,ccountradio,lweighthis,createdate)
  1136. values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  1137. data["pastureid"], data["lppid"], data["barid"],
  1138. data["barname"], data["fpdid"], data["fttype"],
  1139. data["lweight"], data["sort"], data["tmrid"],
  1140. data["tmrname"], data["background"], data["cowcount"],
  1141. data["ccountradio"], data["lweighthis"], now).Execute()
  1142. if err != nil {
  1143. log.Println("UpdateLpplan-error-5: ", err)
  1144. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1145. return
  1146. }
  1147. }
  1148. break
  1149. }
  1150. }
  1151. } else {
  1152. for _, old := range oldList {
  1153. updateStatus := true
  1154. for _, data := range dataList {
  1155. if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1156. updateStatus = false
  1157. break
  1158. }
  1159. }
  1160. if updateStatus {
  1161. now := time.Now()
  1162. for _, old := range oldList {
  1163. exist := false
  1164. var lweight, tmrid, tmrname interface{}
  1165. for _, data := range dataList {
  1166. if old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1167. lweight = data["lweight"]
  1168. tmrid = data["tmrid"]
  1169. tmrname = data["tmrname"]
  1170. exist = true
  1171. break
  1172. }
  1173. }
  1174. if !exist {
  1175. tmrid = old.Get("tmrid").ValueInt64()
  1176. tmrname = old.Get("tmrname").ValueStr()
  1177. lweight = 0
  1178. }
  1179. _, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  1180. background,cowcount,ccountradio,lweighthis,createdate) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  1181. old.Get("pastureid").ValueStr(), old.Get("lppid").ValueStr(), old.Get("barid").ValueStr(),
  1182. old.Get("barname").ValueStr(), old.Get("fpdid").ValueStr(), old.Get("fttype").ValueStr(),
  1183. lweight, old.Get("sort").ValueStr(), tmrid,
  1184. tmrname, old.Get("background").ValueStr(), old.Get("cowcount").ValueStr(),
  1185. old.Get("ccountradio").ValueStr(), old.Get("lweighthis").ValueInt64(), now).Execute()
  1186. if err != nil {
  1187. log.Println("UpdateLpplan-error-6: ", err)
  1188. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1189. return
  1190. }
  1191. }
  1192. break
  1193. }
  1194. }
  1195. }
  1196. appG.Response(http.StatusOK, e.SUCCESS, true)
  1197. }
  1198. func GetSpillageDay(c *gin.Context) {
  1199. appG := app.Gin{C: c}
  1200. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1201. fsion := gofasion.NewFasion(string(dataByte))
  1202. parammaps := fsion.Get("parammaps")
  1203. pastureid := parammaps.Get("pastureid").ValueInt64()
  1204. times := parammaps.Get("times").ValueInt64()
  1205. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  1206. // refresh 0 全部 , 1 车次信息, 2 班次
  1207. refresh := parammaps.Get("refresh").ValueInt64()
  1208. date := parammaps.Get("date").ValueStr()
  1209. tx := restful.Engine.NewSession()
  1210. defer tx.Close()
  1211. data := make(map[string]interface{})
  1212. if refresh == 0 || refresh == 2 {
  1213. // 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
  1214. // inner join feedtemplet ft
  1215. // on ft.id = feedp.ftid and ft.pastureid=?
  1216. // WHERE feedp.pastureid = ? %s
  1217. // GROUP BY feedp.ftid
  1218. // UNION
  1219. // 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
  1220. // inner join feedtemplet ft
  1221. // on ft.id = feedp.ptsfid and ft.pastureid=?
  1222. // WHERE feedp.pastureid = ? %s and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1223. // GROUP BY feedp.ptsfid`
  1224. 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
  1225. FROM feedpdate feedp
  1226. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1227. INNER JOIN feedtemplet ft
  1228. ON ft.id = fpd.ptid AND ft.pastureid= feedp.pastureid
  1229. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1230. GROUP BY fpd.ptid
  1231. UNION
  1232. 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
  1233. FROM feedpdate feedp
  1234. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1235. INNER JOIN feedtemplet ft
  1236. ON ft.id = fpd.ptsid AND ft.pastureid=feedp.pastureid
  1237. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1238. AND (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1239. GROUP BY fpd.ptsid`
  1240. if ftid != 0 {
  1241. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  1242. } else {
  1243. sqlStr = fmt.Sprintf(sqlStr, "", "")
  1244. }
  1245. ftList, err := tx.SQL(sqlStr, times, pastureid, date, times, pastureid, date).Query().List()
  1246. if err != nil {
  1247. log.Println("GetSpillage-error-1: ", err)
  1248. appG.Response(http.StatusOK, e.ERROR, nil)
  1249. return
  1250. }
  1251. // AND ( fpdetail.ptsid=?)
  1252. // AND (fpdetail.ptid=? )
  1253. 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 ,
  1254. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 1 isfill FROM fpdetaildate fpdetail
  1255. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ifnull(ptsrate,0)*weight,2) and fpdetail.date = ?
  1256. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid ) is not null
  1257. UNION
  1258. 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 ,
  1259. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 0 isfill FROM fpdetaildate fpdetail
  1260. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2) and fpdetail.date = ?
  1261. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  1262. ORDER BY barid`, pastureid, times, date, pastureid, times, date).Query().List()
  1263. if err != nil {
  1264. log.Println("GetSpillage-error-2: ", err)
  1265. appG.Response(http.StatusOK, e.ERROR, nil)
  1266. return
  1267. }
  1268. for _, ft := range ftList {
  1269. arrList := make([]map[string]interface{}, 0)
  1270. arrid := ft["arrid"].(string)
  1271. for _, detail := range ftDetailList {
  1272. if _, ok := detail["ptid"]; ok {
  1273. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1274. detail["background"] = ft["background"]
  1275. detail["fttype"] = ft["type"].(int64)
  1276. arrList = append(arrList, detail)
  1277. }
  1278. } else if _, ok := detail["ptsid"]; ok {
  1279. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1280. detail["background"] = ft["background"]
  1281. detail["fttype"] = ft["type"].(int64)
  1282. arrList = append(arrList, detail)
  1283. }
  1284. }
  1285. }
  1286. if len(arrList) > 0 {
  1287. ft["arrList"] = arrList
  1288. }
  1289. }
  1290. data["ftlist"] = ftList
  1291. }
  1292. if refresh == 0 || refresh == 1 {
  1293. sqlstr := `SELECT
  1294. lpplan.date,
  1295. -- ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname)
  1296. lpplan.tmrname tmrname,trim(lpplan.tmrid) tmrid,lpplan.sort,lpplan.sel,
  1297. CASE lpplan.times
  1298. WHEN 1 THEN
  1299. '第一班'
  1300. WHEN 2 THEN
  1301. '第二班'
  1302. WHEN 3 THEN
  1303. '第三班'
  1304. WHEN 4 THEN
  1305. '第四班'
  1306. END timesstr,issplit,
  1307. lpplan.times,display,begintime,
  1308. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  1309. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1date lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.date= ?),2),0) sumweight ,
  1310. maxweight,
  1311. TRIM(ftid) ftid,
  1312. TRIM(lpplan.id) id,
  1313. TRIM(lpplan.id) lppid,
  1314. TRIM(lpplan.pastureid) pastureid,
  1315. ifnull(dpl.havebutton,0) as havebutton
  1316. FROM
  1317. lpplandate lpplan
  1318. left join downloadedplan dpl on dpl.pid = lpplan.id and DATE_FORMAT(dpl.mydate,'%Y-%m-%e') = lpplan.date
  1319. WHERE lpplan.pastureid = ? and lpplan.date = ? and lpplan.times <= (SELECT sysopt.inforvalue FROM sysopt
  1320. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  1321. `
  1322. arrsql := `SELECT
  1323. trim(lpplandtl1.lppid) lppid,lweight,
  1324. lpplandtl1.lweight weight,lpplandtl1.sort,
  1325. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  1326. TRIM(lpplandtl1.fpdid) fpdid,
  1327. TRIM(lpplandtl1.barid) barid,
  1328. TRIM(lpplandtl1.id) id,
  1329. TRIM(lpplandtl1.pastureid) pastureid,
  1330. lpplandtl1.background,
  1331. lpplandtl1.fttype,
  1332. trim(lpplandtl1.tmrid) tmrid,
  1333. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  1334. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  1335. fpdetail.ptid,
  1336. fpdetail.ptsid,
  1337. fpdetail.times,
  1338. fpdetail.cowcount,
  1339. fpdetail.ccountradio
  1340. FROM
  1341. lpplandtl1date lpplandtl1
  1342. inner join fpdetaildate fpdetail
  1343. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid and lpplandtl1.date = fpdetail.date
  1344. WHERE lpplandtl1.pastureid = ? and lpplandtl1.date = ? and lpplandtl1.lweight>0 `
  1345. var args []interface{}
  1346. args = append(args, date, pastureid, date)
  1347. if times != 0 {
  1348. sqlstr += " and lpplan.times = ? "
  1349. // arrsql += " and times = ? "
  1350. args = append(args, times)
  1351. }
  1352. sqlstr += " group by lpplan.id ORDER BY times,lpplan.sort"
  1353. session := tx.SQL(sqlstr, args...)
  1354. lppList, err := session.Query().List()
  1355. if err != nil {
  1356. log.Println("GetSpillage-error-3: ", err)
  1357. appG.Response(http.StatusOK, e.ERROR, nil)
  1358. return
  1359. }
  1360. arrsql += " ORDER BY lpplandtl1.sort"
  1361. arrList, err := tx.SQL(arrsql, pastureid, date).QueryString() //获取
  1362. if err != nil {
  1363. log.Println("GetSpillage-error-4: ", err)
  1364. appG.Response(http.StatusOK, e.ERROR, nil)
  1365. return
  1366. }
  1367. for _, query := range lppList {
  1368. list := make([]map[string]string, 0)
  1369. for _, arr := range arrList {
  1370. if query["lppid"].(string) == arr["lppid"] {
  1371. list = append(list, arr)
  1372. }
  1373. }
  1374. query["arrList"] = list
  1375. }
  1376. data["list"] = lppList
  1377. data["total"] = len(lppList)
  1378. }
  1379. appG.Response(http.StatusOK, e.SUCCESS, data)
  1380. }
  1381. // func AddLpplandtl(c *gin.Context) {
  1382. // appG := app.Gin{C: c}
  1383. // dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1384. // fsion := gofasion.NewFasion(string(dataByte))
  1385. // parammaps := fsion.Get("parammaps")
  1386. // tx := restful.Engine.NewSession()
  1387. // defer tx.Close()
  1388. // exist := false
  1389. // var err error
  1390. // lpplandList := make([]*lpplandtl1, 0)
  1391. // err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1392. // OrderBy("sort").Find(&lpplandList)
  1393. // if err != nil {
  1394. // log.Println("AddLpplandtl-error-1: ", err)
  1395. // appG.Response(http.StatusOK, e.ERROR, err)
  1396. // return
  1397. // }
  1398. // if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1399. // for _, lppand := range lpplandList {
  1400. // if lppand.Id == parammaps.Get("id").ValueInt64() {
  1401. // if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1402. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1403. // return
  1404. // }
  1405. // exist = true
  1406. // break
  1407. // }
  1408. // }
  1409. // }
  1410. // tmrid := parammaps.Get("tmrid").ValueInt64()
  1411. // var sort int64 = -1
  1412. // if !exist {
  1413. // for _, lppand := range lpplandList {
  1414. // if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1415. // _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1416. // if err != nil {
  1417. // log.Println("AddLpplandtl-error-2: ", err)
  1418. // appG.Response(http.StatusOK, e.ERROR, err)
  1419. // return
  1420. // }
  1421. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1422. // return
  1423. // }
  1424. // }
  1425. // for i, lppand := range lpplandList {
  1426. // lppand.Sort = int64(i) + 1
  1427. // }
  1428. // updateLpplandList := make([]*lpplandtl1, 0)
  1429. // for i, lppand := range lpplandList {
  1430. // n := i + 1
  1431. // if lppand.Tmrid == tmrid {
  1432. // sort = int64(n) + 1
  1433. // lppand.Sort = int64(n)
  1434. // updateLpplandList = append(updateLpplandList, lppand)
  1435. // } else if sort > 0 {
  1436. // lppand.Sort = int64(n) + 2
  1437. // updateLpplandList = append(updateLpplandList, lppand)
  1438. // }
  1439. // }
  1440. // tx.Begin()
  1441. // for _, lppand := range updateLpplandList {
  1442. // _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1443. // if err != nil {
  1444. // tx.Rollback()
  1445. // log.Println("AddLpplandtl-error-2: ", err)
  1446. // appG.Response(http.StatusOK, e.ERROR, err)
  1447. // return
  1448. // }
  1449. // }
  1450. // if sort < 0 && len(lpplandList) > 0 {
  1451. // sort = int64(len(lpplandList) + 2)
  1452. // } else if len(lpplandList) == 0 {
  1453. // sort = 1
  1454. // }
  1455. // _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1456. // VALUES (?,?,?,?,?,?,?,?,?,
  1457. // ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1458. // parammaps.Get("pastureid").ValueStr(),
  1459. // parammaps.Get("lppid").ValueStr(),
  1460. // parammaps.Get("barid").ValueStr(),
  1461. // parammaps.Get("barname").ValueStr(),
  1462. // parammaps.Get("fpdid").ValueStr(),
  1463. // parammaps.Get("lweight").ValueStr(),
  1464. // sort,
  1465. // parammaps.Get("tmrid").ValueStr(),
  1466. // parammaps.Get("tmrname").ValueStr(),
  1467. // parammaps.Get("fttype").ValueStr(),
  1468. // parammaps.Get("background").ValueStr(),
  1469. // parammaps.Get("cowcount").ValueStr(),
  1470. // parammaps.Get("cowcount").ValueStr(),
  1471. // parammaps.Get("ccountradio").ValueStr(),
  1472. // parammaps.Get("ccountradio").ValueStr()).Execute()
  1473. // if err != nil {
  1474. // tx.Rollback()
  1475. // log.Println("AddLpplandtl-error-3: ", err)
  1476. // appG.Response(http.StatusOK, e.ERROR, err)
  1477. // return
  1478. // }
  1479. // err = tx.Commit()
  1480. // if err != nil {
  1481. // tx.Rollback()
  1482. // log.Println("AddLpplandtl-error-6: ", err)
  1483. // appG.Response(http.StatusOK, e.ERROR, err)
  1484. // return
  1485. // }
  1486. // } else {
  1487. // lpplandid := parammaps.Get("id").ValueInt64()
  1488. // updateLpplandList := make([]*lpplandtl1, 0)
  1489. // var oldsort int64
  1490. // for i, lppand := range lpplandList {
  1491. // lppand.Sort = int64(i) + 1
  1492. // }
  1493. // for _, lppand := range lpplandList {
  1494. // if lppand.Id == lpplandid {
  1495. // oldsort = lppand.Sort
  1496. // break
  1497. // }
  1498. // }
  1499. // for _, lppand := range lpplandList {
  1500. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  1501. // sort = lppand.Sort
  1502. // break
  1503. // }
  1504. // }
  1505. // if sort == -1 {
  1506. // sort = 1
  1507. // }
  1508. // if oldsort < sort {
  1509. // for _, lppand := range lpplandList {
  1510. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  1511. // lppand.Sort--
  1512. // updateLpplandList = append(updateLpplandList, lppand)
  1513. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  1514. // lppand.Sort++
  1515. // updateLpplandList = append(updateLpplandList, lppand)
  1516. // }
  1517. // }
  1518. // sort--
  1519. // } else {
  1520. // for _, lppand := range lpplandList {
  1521. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  1522. // lppand.Sort++
  1523. // updateLpplandList = append(updateLpplandList, lppand)
  1524. // }
  1525. // }
  1526. // }
  1527. // var tmrlpplandList []*lpplandtl1
  1528. // for _, lppand := range lpplandList {
  1529. // exist := false
  1530. // for _, lppand1 := range updateLpplandList {
  1531. // if lppand.Id == lppand1.Id {
  1532. // exist = true
  1533. // break
  1534. // }
  1535. // }
  1536. // if !exist {
  1537. // tmrlpplandList = append(tmrlpplandList, lppand)
  1538. // }
  1539. // }
  1540. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  1541. // if len(updateLpplandList) > 0 {
  1542. // tx.Begin()
  1543. // for _, lppand := range updateLpplandList {
  1544. // _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1545. // if err != nil {
  1546. // tx.Rollback()
  1547. // log.Println("AddLpplandtl-error-2: ", err)
  1548. // appG.Response(http.StatusOK, e.ERROR, err)
  1549. // return
  1550. // }
  1551. // }
  1552. // _, 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()
  1553. // if err != nil {
  1554. // tx.Rollback()
  1555. // log.Println("AddLpplandtl-error-2: ", err)
  1556. // appG.Response(http.StatusOK, e.ERROR, err)
  1557. // return
  1558. // }
  1559. // tx.Commit()
  1560. // }
  1561. // }
  1562. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1563. // }
  1564. func AddLpplandtl(c *gin.Context) {
  1565. appG := app.Gin{C: c}
  1566. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1567. fsion := gofasion.NewFasion(string(dataByte))
  1568. parammaps := fsion.Get("parammaps")
  1569. tx := restful.Engine.NewSession()
  1570. defer tx.Close()
  1571. exist := false
  1572. var err error
  1573. lpplandList := make([]*lpplandtl1, 0)
  1574. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1575. OrderBy("sort").Find(&lpplandList)
  1576. if err != nil {
  1577. log.Println("AddLpplandtl-error-1: ", err)
  1578. appG.Response(http.StatusOK, e.ERROR, err)
  1579. return
  1580. }
  1581. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1582. for _, lppand := range lpplandList {
  1583. if lppand.Id == parammaps.Get("id").ValueInt64() {
  1584. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1585. appG.Response(http.StatusOK, e.SUCCESS, true)
  1586. return
  1587. }
  1588. exist = true
  1589. break
  1590. }
  1591. }
  1592. }
  1593. tmrid := parammaps.Get("tmrid").ValueInt64()
  1594. if !exist {
  1595. var sort int64 = -1
  1596. for _, lppand := range lpplandList {
  1597. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1598. _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1599. if err != nil {
  1600. log.Println("AddLpplandtl-error-2: ", err)
  1601. appG.Response(http.StatusOK, e.ERROR, err)
  1602. return
  1603. }
  1604. appG.Response(http.StatusOK, e.SUCCESS, true)
  1605. return
  1606. }
  1607. }
  1608. for i, lppand := range lpplandList {
  1609. lppand.Sort = int64(i) + 1
  1610. }
  1611. updateLpplandList := make([]*lpplandtl1, 0)
  1612. for i, lppand := range lpplandList {
  1613. n := i + 1
  1614. if lppand.Tmrid == tmrid {
  1615. sort = int64(n) + 1
  1616. lppand.Sort = int64(n)
  1617. updateLpplandList = append(updateLpplandList, lppand)
  1618. } else if sort > 0 {
  1619. lppand.Sort = int64(n) + 2
  1620. updateLpplandList = append(updateLpplandList, lppand)
  1621. }
  1622. }
  1623. tx.Begin()
  1624. for _, lppand := range updateLpplandList {
  1625. _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1626. if err != nil {
  1627. tx.Rollback()
  1628. log.Println("AddLpplandtl-error-2: ", err)
  1629. appG.Response(http.StatusOK, e.ERROR, err)
  1630. return
  1631. }
  1632. }
  1633. if sort < 0 && len(lpplandList) > 0 {
  1634. sort = int64(len(lpplandList) + 2)
  1635. } else if len(lpplandList) == 0 {
  1636. sort = 1
  1637. }
  1638. _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1639. VALUES (?,?,?,?,?,?,?,?,?,
  1640. ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1641. parammaps.Get("pastureid").ValueStr(),
  1642. parammaps.Get("lppid").ValueStr(),
  1643. parammaps.Get("barid").ValueStr(),
  1644. parammaps.Get("barname").ValueStr(),
  1645. parammaps.Get("fpdid").ValueStr(),
  1646. parammaps.Get("lweight").ValueStr(),
  1647. sort,
  1648. parammaps.Get("tmrid").ValueStr(),
  1649. parammaps.Get("tmrname").ValueStr(),
  1650. parammaps.Get("fttype").ValueStr(),
  1651. parammaps.Get("background").ValueStr(),
  1652. parammaps.Get("cowcount").ValueStr(),
  1653. parammaps.Get("cowcount").ValueStr(),
  1654. parammaps.Get("ccountradio").ValueStr(),
  1655. parammaps.Get("ccountradio").ValueStr()).Execute()
  1656. if err != nil {
  1657. tx.Rollback()
  1658. log.Println("AddLpplandtl-error-3: ", err)
  1659. appG.Response(http.StatusOK, e.ERROR, err)
  1660. return
  1661. }
  1662. err = tx.Commit()
  1663. if err != nil {
  1664. tx.Rollback()
  1665. log.Println("AddLpplandtl-error-6: ", err)
  1666. appG.Response(http.StatusOK, e.ERROR, err)
  1667. return
  1668. }
  1669. } else {
  1670. lpplandid := parammaps.Get("id").ValueInt64()
  1671. // updateLpplandList := make([]*lpplandtl1, 0)
  1672. var oldsort int64
  1673. for i, lppand := range lpplandList {
  1674. lppand.Sort = int64(i) + 1
  1675. }
  1676. noup := false
  1677. for i, lppand := range lpplandList {
  1678. if lppand.Id == lpplandid {
  1679. oldsort = lppand.Sort
  1680. b := false
  1681. for _, lppand := range lpplandList {
  1682. if lppand.Tmrid == tmrid {
  1683. noup = false
  1684. b = true
  1685. break
  1686. }
  1687. }
  1688. if !b {
  1689. if i == 0 || i == len(lpplandList)-1 {
  1690. noup = true
  1691. }
  1692. if i != 0 && i < len(lpplandList)-1 {
  1693. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  1694. noup = true
  1695. }
  1696. }
  1697. // if lpplandList[i+1].Tmrid == tmrid {
  1698. // _, 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()
  1699. // if err != nil {
  1700. // tx.Rollback()
  1701. // log.Println("AddLpplandtl-error-2: ", err)
  1702. // appG.Response(http.StatusOK, e.ERROR, err)
  1703. // return
  1704. // }
  1705. // tx.Commit()
  1706. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1707. // return
  1708. // }
  1709. // }
  1710. // break
  1711. // }
  1712. // }
  1713. }
  1714. break
  1715. }
  1716. }
  1717. if noup {
  1718. _, 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()
  1719. if err != nil {
  1720. tx.Rollback()
  1721. log.Println("AddLpplandtl-error-2: ", err)
  1722. appG.Response(http.StatusOK, e.ERROR, err)
  1723. return
  1724. }
  1725. } else {
  1726. for i, lppand := range lpplandList {
  1727. if lppand.Id == lpplandid {
  1728. if i != 0 {
  1729. if lpplandList[i-1].Tmrid == tmrid {
  1730. _, 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()
  1731. if err != nil {
  1732. tx.Rollback()
  1733. log.Println("AddLpplandtl-error-2: ", err)
  1734. appG.Response(http.StatusOK, e.ERROR, err)
  1735. return
  1736. }
  1737. tx.Commit()
  1738. appG.Response(http.StatusOK, e.SUCCESS, true)
  1739. return
  1740. }
  1741. }
  1742. if i < len(lpplandList)-1 {
  1743. if lpplandList[i+1].Tmrid == tmrid {
  1744. _, 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()
  1745. if err != nil {
  1746. tx.Rollback()
  1747. log.Println("AddLpplandtl-error-2: ", err)
  1748. appG.Response(http.StatusOK, e.ERROR, err)
  1749. return
  1750. }
  1751. tx.Commit()
  1752. appG.Response(http.StatusOK, e.SUCCESS, true)
  1753. return
  1754. }
  1755. }
  1756. break
  1757. }
  1758. }
  1759. exist := false
  1760. sort1 := 0
  1761. a := false
  1762. for _, lppand := range lpplandList {
  1763. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  1764. sort1 = int(lppand.Sort)
  1765. if !a {
  1766. for _, lppand1 := range lpplandList {
  1767. if lppand1.Id == lpplandid {
  1768. lppand1.Tmrid = tmrid
  1769. lppand1.Tmrname = lppand.Tmrname
  1770. a = true
  1771. // lppand1.Sort = lppand.Sort
  1772. break
  1773. }
  1774. }
  1775. }
  1776. exist = true
  1777. }
  1778. }
  1779. if exist {
  1780. if oldsort > int64(sort1) {
  1781. for _, lppand := range lpplandList {
  1782. if lppand.Id == lpplandid {
  1783. lppand.Sort = int64(sort1) + 1
  1784. continue
  1785. }
  1786. if lppand.Sort >= int64(sort1)+1 {
  1787. lppand.Sort++
  1788. }
  1789. }
  1790. } else {
  1791. for _, lppand := range lpplandList {
  1792. if lppand.Id == lpplandid {
  1793. lppand.Sort = int64(sort1) + 1
  1794. continue
  1795. }
  1796. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  1797. lppand.Sort--
  1798. } else if lppand.Sort <= int64(sort1)+1 {
  1799. lppand.Sort++
  1800. }
  1801. }
  1802. }
  1803. } else {
  1804. for _, lppand := range lpplandList {
  1805. if lppand.Id == lpplandid {
  1806. lppand.Sort = int64(len(lpplandList)) + 1
  1807. lppand.Tmrid = tmrid
  1808. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  1809. break
  1810. }
  1811. }
  1812. }
  1813. sort.Slice(lpplandList, func(i, j int) bool {
  1814. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  1815. })
  1816. for i, lppand := range lpplandList {
  1817. lppand.Sort = int64(i) + 1
  1818. }
  1819. for _, lppand := range lpplandList {
  1820. _, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? ", lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1821. if err != nil {
  1822. tx.Rollback()
  1823. log.Println("AddLpplandtl-error-2: ", err)
  1824. appG.Response(http.StatusOK, e.ERROR, err)
  1825. return
  1826. }
  1827. }
  1828. }
  1829. tx.Commit()
  1830. }
  1831. appG.Response(http.StatusOK, e.SUCCESS, true)
  1832. }
  1833. func UpdateLpplandtlSort(c *gin.Context) {
  1834. appG := app.Gin{C: c}
  1835. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1836. fsion := gofasion.NewFasion(string(dataByte))
  1837. parammaps := fsion.Get("parammaps")
  1838. tx := restful.Engine.NewSession()
  1839. defer tx.Close()
  1840. var err error
  1841. lpplandList := make([]*lpplandtl1, 0)
  1842. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1843. OrderBy("sort").Find(&lpplandList)
  1844. if err != nil {
  1845. log.Println("UpdateLpplandtlSort-error-1: ", err)
  1846. appG.Response(http.StatusOK, e.ERROR, err)
  1847. return
  1848. }
  1849. var oldsort, lpplandtlsort int64
  1850. tmrid := parammaps.Get("tmrid").ValueInt64()
  1851. sort := parammaps.Get("sort").ValueInt64()
  1852. lpplandtlid := parammaps.Get("id").ValueInt64()
  1853. sort1 := parammaps.Get("sort").ValueInt64()
  1854. for i, lppland := range lpplandList {
  1855. lppland.Sort = int64(i) + 1
  1856. }
  1857. for _, lppland := range lpplandList {
  1858. if tmrid == lppland.Tmrid {
  1859. oldsort = lppland.Sort
  1860. break
  1861. }
  1862. }
  1863. for _, lppland := range lpplandList {
  1864. if lppland.Sort == sort {
  1865. for _, lppland1 := range lpplandList {
  1866. if lppland.Tmrid == lppland1.Tmrid {
  1867. sort = lppland1.Sort
  1868. if oldsort > sort {
  1869. break
  1870. }
  1871. }
  1872. }
  1873. break
  1874. }
  1875. }
  1876. if oldsort == sort {
  1877. appG.Response(http.StatusOK, e.SUCCESS, true)
  1878. return
  1879. }
  1880. tmrLpplandList := make([]*lpplandtl1, 0)
  1881. for _, lppland := range lpplandList {
  1882. if lppland.Tmrid == tmrid {
  1883. tmrLpplandList = append(tmrLpplandList, lppland)
  1884. if lppland.Id == lpplandtlid {
  1885. lpplandtlsort = lppland.Sort
  1886. }
  1887. }
  1888. }
  1889. exist := false
  1890. for _, tmrLppland := range tmrLpplandList {
  1891. if sort1 == tmrLppland.Sort {
  1892. exist = true
  1893. break
  1894. }
  1895. }
  1896. if exist {
  1897. for _, tmrLppland := range tmrLpplandList {
  1898. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  1899. tmrLppland.Sort++
  1900. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  1901. tmrLppland.Sort--
  1902. }
  1903. }
  1904. for _, tmrLppland := range tmrLpplandList {
  1905. if tmrLppland.Id == lpplandtlid {
  1906. tmrLppland.Sort = sort1
  1907. break
  1908. }
  1909. }
  1910. } else {
  1911. if oldsort < sort && len(tmrLpplandList) > 1 {
  1912. for i, tmrLppland := range tmrLpplandList {
  1913. tmrLppland.Sort = sort + int64(i) - 1
  1914. }
  1915. } else {
  1916. for i, tmrLppland := range tmrLpplandList {
  1917. tmrLppland.Sort = sort + int64(i)
  1918. }
  1919. }
  1920. }
  1921. if oldsort > sort {
  1922. for _, lppland := range lpplandList {
  1923. if lppland.Sort == sort {
  1924. // uptmrid = lppland.Tmrid
  1925. sort = lppland.Sort
  1926. break
  1927. }
  1928. }
  1929. } else {
  1930. var uptmrid int64
  1931. for _, lppland := range lpplandList {
  1932. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  1933. uptmrid = lppland.Tmrid
  1934. sort = lppland.Sort
  1935. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  1936. sort = lppland.Sort
  1937. }
  1938. }
  1939. }
  1940. upcount := int64(len(tmrLpplandList))
  1941. if oldsort > sort {
  1942. for _, lppland := range lpplandList {
  1943. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  1944. lppland.Sort += upcount
  1945. tmrLpplandList = append(tmrLpplandList, lppland)
  1946. }
  1947. }
  1948. } else {
  1949. for _, lppland := range lpplandList {
  1950. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  1951. lppland.Sort -= upcount
  1952. tmrLpplandList = append(tmrLpplandList, lppland)
  1953. }
  1954. }
  1955. }
  1956. updateLpplandList := make([]*lpplandtl1, 0)
  1957. for _, lppland := range lpplandList {
  1958. exist := false
  1959. for _, tmrLppland := range tmrLpplandList {
  1960. if lppland.Id == tmrLppland.Id {
  1961. exist = true
  1962. break
  1963. }
  1964. }
  1965. if !exist {
  1966. updateLpplandList = append(updateLpplandList, lppland)
  1967. }
  1968. }
  1969. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  1970. tx.Begin()
  1971. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  1972. var wg sync.WaitGroup
  1973. for _, lppland := range tmrLpplandList {
  1974. wg.Add(1)
  1975. go func(lppland *lpplandtl1) {
  1976. defer wg.Done()
  1977. _, err = tx.SQL(`update lpplandtl1 set sort = ? where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
  1978. if err != nil {
  1979. log.Println("UpdateLpplandtlSort-error-2: ", err)
  1980. tx.Rollback()
  1981. appG.Response(http.StatusOK, e.ERROR, err)
  1982. return
  1983. }
  1984. }(lppland)
  1985. }
  1986. wg.Wait()
  1987. err = tx.Commit()
  1988. if err != nil {
  1989. log.Println("UpdateLpplandtlSort-error-3: ", err)
  1990. tx.Rollback()
  1991. appG.Response(http.StatusOK, e.ERROR, err)
  1992. return
  1993. }
  1994. appG.Response(http.StatusOK, e.SUCCESS, true)
  1995. }
  1996. func AddLpplandtlDate(c *gin.Context) {
  1997. appG := app.Gin{C: c}
  1998. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1999. fsion := gofasion.NewFasion(string(dataByte))
  2000. parammaps := fsion.Get("parammaps")
  2001. tx := restful.Engine.NewSession()
  2002. defer tx.Close()
  2003. exist := false
  2004. var err error
  2005. lpplandList := make([]*lpplandtl1, 0)
  2006. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2007. And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
  2008. if err != nil {
  2009. log.Println("AddLpplandtl-error-1: ", err)
  2010. appG.Response(http.StatusOK, e.ERROR, err)
  2011. return
  2012. }
  2013. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  2014. for _, lppand := range lpplandList {
  2015. fmt.Println(parammaps.Get("id").ValueInt64())
  2016. if lppand.Id == parammaps.Get("id").ValueInt64() {
  2017. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  2018. appG.Response(http.StatusOK, e.SUCCESS, true)
  2019. return
  2020. }
  2021. exist = true
  2022. break
  2023. }
  2024. }
  2025. }
  2026. tmrid := parammaps.Get("tmrid").ValueInt64()
  2027. if !exist {
  2028. var sort int64 = -1
  2029. for _, lppand := range lpplandList {
  2030. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  2031. _, err := tx.SQL(" update lpplandtl1date set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  2032. if err != nil {
  2033. log.Println("AddLpplandtl-error-2: ", err)
  2034. appG.Response(http.StatusOK, e.ERROR, err)
  2035. return
  2036. }
  2037. appG.Response(http.StatusOK, e.SUCCESS, true)
  2038. return
  2039. }
  2040. }
  2041. for i, lppand := range lpplandList {
  2042. lppand.Sort = int64(i) + 1
  2043. }
  2044. updateLpplandList := make([]*lpplandtl1, 0)
  2045. for i, lppand := range lpplandList {
  2046. n := i + 1
  2047. if lppand.Tmrid == tmrid {
  2048. sort = int64(n) + 1
  2049. lppand.Sort = int64(n)
  2050. updateLpplandList = append(updateLpplandList, lppand)
  2051. } else if sort > 0 {
  2052. lppand.Sort = int64(n) + 2
  2053. updateLpplandList = append(updateLpplandList, lppand)
  2054. }
  2055. }
  2056. tx.Begin()
  2057. for _, lppand := range updateLpplandList {
  2058. _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? ", lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2059. if err != nil {
  2060. tx.Rollback()
  2061. log.Println("AddLpplandtl-error-2: ", err)
  2062. appG.Response(http.StatusOK, e.ERROR, err)
  2063. return
  2064. }
  2065. }
  2066. if sort < 0 && len(lpplandList) > 0 {
  2067. sort = int64(len(lpplandList) + 1)
  2068. } else if len(lpplandList) == 0 {
  2069. sort = 1
  2070. }
  2071. _, err = tx.SQL(`INSERT INTO lpplandtl1date(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio,date)
  2072. VALUES (?,?,?,?,?,?,?,?,?,
  2073. ?,?,IF(?='',0,?),IF(?='',0,?),?)`,
  2074. parammaps.Get("pastureid").ValueStr(),
  2075. parammaps.Get("lppid").ValueStr(),
  2076. parammaps.Get("barid").ValueStr(),
  2077. parammaps.Get("barname").ValueStr(),
  2078. parammaps.Get("fpdid").ValueStr(),
  2079. parammaps.Get("lweight").ValueStr(),
  2080. sort,
  2081. parammaps.Get("tmrid").ValueStr(),
  2082. parammaps.Get("tmrname").ValueStr(),
  2083. parammaps.Get("fttype").ValueStr(),
  2084. parammaps.Get("background").ValueStr(),
  2085. parammaps.Get("cowcount").ValueStr(),
  2086. parammaps.Get("cowcount").ValueStr(),
  2087. parammaps.Get("ccountradio").ValueStr(),
  2088. parammaps.Get("ccountradio").ValueStr(),
  2089. parammaps.Get("date").ValueStr()).Execute()
  2090. if err != nil {
  2091. tx.Rollback()
  2092. log.Println("AddLpplandtl-error-3: ", err)
  2093. appG.Response(http.StatusOK, e.ERROR, err)
  2094. return
  2095. }
  2096. err = tx.Commit()
  2097. if err != nil {
  2098. tx.Rollback()
  2099. log.Println("AddLpplandtl-error-6: ", err)
  2100. appG.Response(http.StatusOK, e.ERROR, err)
  2101. return
  2102. }
  2103. } else {
  2104. lpplandid := parammaps.Get("id").ValueInt64()
  2105. // updateLpplandList := make([]*lpplandtl1, 0)
  2106. var oldsort int64
  2107. for i, lppand := range lpplandList {
  2108. lppand.Sort = int64(i) + 1
  2109. }
  2110. noup := false
  2111. for i, lppand := range lpplandList {
  2112. if lppand.Id == lpplandid {
  2113. oldsort = lppand.Sort
  2114. b := false
  2115. for _, lppand := range lpplandList {
  2116. if lppand.Tmrid == tmrid {
  2117. noup = false
  2118. b = true
  2119. break
  2120. }
  2121. }
  2122. if !b {
  2123. if i == 0 || i == len(lpplandList)-1 {
  2124. noup = true
  2125. }
  2126. if i != 0 && i < len(lpplandList)-1 {
  2127. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  2128. noup = true
  2129. }
  2130. }
  2131. }
  2132. break
  2133. }
  2134. }
  2135. if noup {
  2136. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2137. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2138. if err != nil {
  2139. tx.Rollback()
  2140. log.Println("AddLpplandtl-error-2: ", err)
  2141. appG.Response(http.StatusOK, e.ERROR, err)
  2142. return
  2143. }
  2144. } else {
  2145. for i, lppand := range lpplandList {
  2146. if lppand.Id == lpplandid {
  2147. if i != 0 {
  2148. if lpplandList[i-1].Tmrid == tmrid {
  2149. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2150. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2151. if err != nil {
  2152. tx.Rollback()
  2153. log.Println("AddLpplandtl-error-2: ", err)
  2154. appG.Response(http.StatusOK, e.ERROR, err)
  2155. return
  2156. }
  2157. tx.Commit()
  2158. appG.Response(http.StatusOK, e.SUCCESS, true)
  2159. return
  2160. }
  2161. }
  2162. if i < len(lpplandList)-1 {
  2163. if lpplandList[i+1].Tmrid == tmrid {
  2164. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ", parammaps.Get("tmrid").ValueStr(),
  2165. parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2166. if err != nil {
  2167. tx.Rollback()
  2168. log.Println("AddLpplandtl-error-2: ", err)
  2169. appG.Response(http.StatusOK, e.ERROR, err)
  2170. return
  2171. }
  2172. tx.Commit()
  2173. appG.Response(http.StatusOK, e.SUCCESS, true)
  2174. return
  2175. }
  2176. }
  2177. break
  2178. }
  2179. }
  2180. exist := false
  2181. sort1 := 0
  2182. a := false
  2183. for _, lppand := range lpplandList {
  2184. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  2185. sort1 = int(lppand.Sort)
  2186. if !a {
  2187. for _, lppand1 := range lpplandList {
  2188. if lppand1.Id == lpplandid {
  2189. lppand1.Tmrid = tmrid
  2190. lppand1.Tmrname = lppand.Tmrname
  2191. a = true
  2192. // lppand1.Sort = lppand.Sort
  2193. break
  2194. }
  2195. }
  2196. }
  2197. exist = true
  2198. }
  2199. }
  2200. if exist {
  2201. if oldsort > int64(sort1) {
  2202. for _, lppand := range lpplandList {
  2203. if lppand.Id == lpplandid {
  2204. lppand.Sort = int64(sort1) + 1
  2205. continue
  2206. }
  2207. if lppand.Sort >= int64(sort1)+1 {
  2208. lppand.Sort++
  2209. }
  2210. }
  2211. } else {
  2212. for _, lppand := range lpplandList {
  2213. if lppand.Id == lpplandid {
  2214. lppand.Sort = int64(sort1) + 1
  2215. continue
  2216. }
  2217. // if lppand.Sort > oldsort {
  2218. // lppand.Sort--
  2219. // }
  2220. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  2221. lppand.Sort--
  2222. } else if lppand.Sort <= int64(sort1)+1 {
  2223. lppand.Sort++
  2224. }
  2225. }
  2226. }
  2227. } else {
  2228. for _, lppand := range lpplandList {
  2229. if lppand.Id == lpplandid {
  2230. lppand.Sort = int64(len(lpplandList)) + 1
  2231. lppand.Tmrid = tmrid
  2232. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  2233. break
  2234. }
  2235. }
  2236. }
  2237. sort.Slice(lpplandList, func(i, j int) bool {
  2238. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  2239. })
  2240. for i, lppand := range lpplandList {
  2241. lppand.Sort = int64(i) + 1
  2242. }
  2243. for _, lppand := range lpplandList {
  2244. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? and date = ? ",
  2245. lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2246. if err != nil {
  2247. tx.Rollback()
  2248. log.Println("AddLpplandtl-error-2: ", err)
  2249. appG.Response(http.StatusOK, e.ERROR, err)
  2250. return
  2251. }
  2252. }
  2253. }
  2254. tx.Commit()
  2255. // lpplandid := parammaps.Get("id").ValueInt64()
  2256. // updateLpplandList := make([]*lpplandtl1, 0)
  2257. // var oldsort int64
  2258. // for i, lppand := range lpplandList {
  2259. // lppand.Sort = int64(i) + 1
  2260. // }
  2261. // for _, lppand := range lpplandList {
  2262. // if lppand.Id == lpplandid {
  2263. // oldsort = lppand.Sort
  2264. // break
  2265. // }
  2266. // }
  2267. // for _, lppand := range lpplandList {
  2268. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  2269. // sort = lppand.Sort
  2270. // break
  2271. // }
  2272. // }
  2273. // if sort == -1 {
  2274. // sort = 1
  2275. // }
  2276. // if oldsort < sort {
  2277. // for _, lppand := range lpplandList {
  2278. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  2279. // lppand.Sort--
  2280. // updateLpplandList = append(updateLpplandList, lppand)
  2281. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  2282. // lppand.Sort++
  2283. // updateLpplandList = append(updateLpplandList, lppand)
  2284. // }
  2285. // }
  2286. // sort--
  2287. // } else {
  2288. // for _, lppand := range lpplandList {
  2289. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  2290. // lppand.Sort++
  2291. // updateLpplandList = append(updateLpplandList, lppand)
  2292. // }
  2293. // }
  2294. // }
  2295. // var tmrlpplandList []*lpplandtl1
  2296. // for _, lppand := range lpplandList {
  2297. // exist := false
  2298. // for _, lppand1 := range updateLpplandList {
  2299. // if lppand.Id == lppand1.Id {
  2300. // exist = true
  2301. // break
  2302. // }
  2303. // }
  2304. // if !exist {
  2305. // tmrlpplandList = append(tmrlpplandList, lppand)
  2306. // }
  2307. // }
  2308. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  2309. // if len(updateLpplandList) > 0 {
  2310. // tx.Begin()
  2311. // for _, lppand := range updateLpplandList {
  2312. // _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  2313. // if err != nil {
  2314. // tx.Rollback()
  2315. // log.Println("AddLpplandtl-error-2: ", err)
  2316. // appG.Response(http.StatusOK, e.ERROR, err)
  2317. // return
  2318. // }
  2319. // }
  2320. // _, 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()
  2321. // if err != nil {
  2322. // tx.Rollback()
  2323. // log.Println("AddLpplandtl-error-2: ", err)
  2324. // appG.Response(http.StatusOK, e.ERROR, err)
  2325. // return
  2326. // }
  2327. // tx.Commit()
  2328. // }
  2329. }
  2330. appG.Response(http.StatusOK, e.SUCCESS, true)
  2331. }
  2332. func UpdateLpplandtlDateSort(c *gin.Context) {
  2333. appG := app.Gin{C: c}
  2334. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2335. fsion := gofasion.NewFasion(string(dataByte))
  2336. parammaps := fsion.Get("parammaps")
  2337. date := parammaps.Get("date").ValueStr()
  2338. tx := restful.Engine.NewSession()
  2339. defer tx.Close()
  2340. var err error
  2341. lpplandList := make([]*lpplandtl1, 0)
  2342. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2343. And("date = ? ", date).OrderBy("sort").Find(&lpplandList)
  2344. if err != nil {
  2345. log.Println("UpdateLpplandtlSort-error-1: ", err)
  2346. appG.Response(http.StatusOK, e.ERROR, err)
  2347. return
  2348. }
  2349. var oldsort, lpplandtlsort int64
  2350. tmrid := parammaps.Get("tmrid").ValueInt64()
  2351. sort := parammaps.Get("sort").ValueInt64()
  2352. lpplandtlid := parammaps.Get("id").ValueInt64()
  2353. sort1 := parammaps.Get("sort").ValueInt64()
  2354. for i, lppland := range lpplandList {
  2355. lppland.Sort = int64(i) + 1
  2356. }
  2357. for _, lppland := range lpplandList {
  2358. if tmrid == lppland.Tmrid {
  2359. oldsort = lppland.Sort
  2360. break
  2361. }
  2362. }
  2363. for _, lppland := range lpplandList {
  2364. if lppland.Sort == sort {
  2365. for _, lppland1 := range lpplandList {
  2366. if lppland.Tmrid == lppland1.Tmrid {
  2367. sort = lppland1.Sort
  2368. if oldsort > sort {
  2369. break
  2370. }
  2371. }
  2372. }
  2373. break
  2374. }
  2375. }
  2376. if oldsort == sort {
  2377. appG.Response(http.StatusOK, e.SUCCESS, true)
  2378. return
  2379. }
  2380. tmrLpplandList := make([]*lpplandtl1, 0)
  2381. for _, lppland := range lpplandList {
  2382. if lppland.Tmrid == tmrid {
  2383. tmrLpplandList = append(tmrLpplandList, lppland)
  2384. if lppland.Id == lpplandtlid {
  2385. lpplandtlsort = lppland.Sort
  2386. }
  2387. }
  2388. }
  2389. exist := false
  2390. for _, tmrLppland := range tmrLpplandList {
  2391. if sort1 == tmrLppland.Sort {
  2392. exist = true
  2393. break
  2394. }
  2395. }
  2396. if exist {
  2397. for _, tmrLppland := range tmrLpplandList {
  2398. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  2399. tmrLppland.Sort++
  2400. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  2401. tmrLppland.Sort--
  2402. }
  2403. }
  2404. for _, tmrLppland := range tmrLpplandList {
  2405. if tmrLppland.Id == lpplandtlid {
  2406. tmrLppland.Sort = sort1
  2407. break
  2408. }
  2409. }
  2410. } else {
  2411. if oldsort < sort && len(tmrLpplandList) > 1 {
  2412. for i, tmrLppland := range tmrLpplandList {
  2413. tmrLppland.Sort = sort + int64(i) - 1
  2414. }
  2415. } else {
  2416. for i, tmrLppland := range tmrLpplandList {
  2417. tmrLppland.Sort = sort + int64(i)
  2418. }
  2419. }
  2420. }
  2421. if oldsort > sort {
  2422. for _, lppland := range lpplandList {
  2423. if lppland.Sort == sort {
  2424. // uptmrid = lppland.Tmrid
  2425. sort = lppland.Sort
  2426. break
  2427. }
  2428. }
  2429. } else {
  2430. var uptmrid int64
  2431. for _, lppland := range lpplandList {
  2432. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  2433. uptmrid = lppland.Tmrid
  2434. sort = lppland.Sort
  2435. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  2436. sort = lppland.Sort
  2437. }
  2438. }
  2439. }
  2440. upcount := int64(len(tmrLpplandList))
  2441. if oldsort > sort {
  2442. for _, lppland := range lpplandList {
  2443. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  2444. lppland.Sort += upcount
  2445. tmrLpplandList = append(tmrLpplandList, lppland)
  2446. }
  2447. }
  2448. } else {
  2449. for _, lppland := range lpplandList {
  2450. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  2451. lppland.Sort -= upcount
  2452. tmrLpplandList = append(tmrLpplandList, lppland)
  2453. }
  2454. }
  2455. }
  2456. updateLpplandList := make([]*lpplandtl1, 0)
  2457. for _, lppland := range lpplandList {
  2458. exist := false
  2459. for _, tmrLppland := range tmrLpplandList {
  2460. if lppland.Id == tmrLppland.Id {
  2461. exist = true
  2462. break
  2463. }
  2464. }
  2465. if !exist {
  2466. updateLpplandList = append(updateLpplandList, lppland)
  2467. }
  2468. }
  2469. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  2470. tx.Begin()
  2471. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  2472. var wg sync.WaitGroup
  2473. for _, lppland := range tmrLpplandList {
  2474. wg.Add(1)
  2475. go func(lppland *lpplandtl1) {
  2476. defer wg.Done()
  2477. _, err = tx.SQL(`update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? `, lppland.Sort, lppland.Id, lppland.Pastureid, date).Execute()
  2478. if err != nil {
  2479. log.Println("UpdateLpplandtlSort-error-2: ", err)
  2480. tx.Rollback()
  2481. appG.Response(http.StatusOK, e.ERROR, err)
  2482. return
  2483. }
  2484. }(lppland)
  2485. }
  2486. wg.Wait()
  2487. err = tx.Commit()
  2488. if err != nil {
  2489. log.Println("UpdateLpplandtlSort-error-3: ", err)
  2490. tx.Rollback()
  2491. appG.Response(http.StatusOK, e.ERROR, err)
  2492. return
  2493. }
  2494. appG.Response(http.StatusOK, e.SUCCESS, true)
  2495. }
  2496. func GetFeedpUndistributed(c *gin.Context) {
  2497. appG := app.Gin{C: c}
  2498. tx := restful.Engine.NewSession()
  2499. defer tx.Close()
  2500. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2501. fsion := gofasion.NewFasion(string(dataByte))
  2502. timesMax := fsion.Get("times").ValueInt()
  2503. pastureid := fsion.Get("pastureid").ValueStr()
  2504. data := make(map[string]interface{})
  2505. for times := 1; times <= timesMax; times++ {
  2506. 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
  2507. inner join feedtemplet ft
  2508. on ft.id = feedp.ftid and ft.pastureid=?
  2509. WHERE feedp.pastureid = ?
  2510. GROUP BY feedp.ftid
  2511. UNION
  2512. 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
  2513. inner join feedtemplet ft
  2514. on ft.id = feedp.ptsfid and ft.pastureid=?
  2515. WHERE feedp.pastureid = ? and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  2516. GROUP BY feedp.ptsfid`
  2517. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  2518. if err != nil {
  2519. log.Println("GetFeedpUndistributed-error-1: ", err)
  2520. appG.Response(http.StatusOK, e.ERROR, nil)
  2521. return
  2522. }
  2523. 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 ,
  2524. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  2525. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  2526. UNION
  2527. 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 ,
  2528. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  2529. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  2530. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  2531. if err != nil {
  2532. log.Println("GetFeedpUndistributed-error-2: ", err)
  2533. appG.Response(http.StatusOK, e.ERROR, nil)
  2534. return
  2535. }
  2536. for _, ft := range ftList {
  2537. arrList := make([]map[string]interface{}, 0)
  2538. arrid := ft["arrid"].(string)
  2539. for _, detail := range ftDetailList {
  2540. if _, ok := detail["ptid"]; ok {
  2541. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2542. detail["background"] = ft["background"]
  2543. arrList = append(arrList, detail)
  2544. }
  2545. } else if _, ok := detail["ptsid"]; ok {
  2546. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2547. detail["background"] = ft["background"]
  2548. arrList = append(arrList, detail)
  2549. }
  2550. }
  2551. }
  2552. if len(arrList) > 0 {
  2553. ft["arrList"] = arrList
  2554. }
  2555. }
  2556. var key string
  2557. if times == 1 {
  2558. key = "one"
  2559. } else if times == 2 {
  2560. key = "two"
  2561. } else if times == 3 {
  2562. key = "three"
  2563. } else if times == 4 {
  2564. key = "four"
  2565. } else if times == 5 {
  2566. key = "five"
  2567. } else {
  2568. key = "six"
  2569. }
  2570. data[key] = ftList
  2571. }
  2572. appG.Response(http.StatusOK, e.SUCCESS, data)
  2573. }