spillage.go 88 KB

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