spillage.go 89 KB

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