spillage.go 88 KB

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