spillage.go 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822
  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. if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1128. updateStatus = false
  1129. break
  1130. }
  1131. }
  1132. if updateStatus {
  1133. now := time.Now()
  1134. for _, data := range dataList {
  1135. _, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  1136. background,cowcount,ccountradio,lweighthis,createdate)
  1137. values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  1138. data["pastureid"], data["lppid"], data["barid"],
  1139. data["barname"], data["fpdid"], data["fttype"],
  1140. data["lweight"], data["sort"], data["tmrid"],
  1141. data["tmrname"], data["background"], data["cowcount"],
  1142. data["ccountradio"], data["lweighthis"], now).Execute()
  1143. if err != nil {
  1144. log.Println("UpdateLpplan-error-5: ", err)
  1145. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1146. return
  1147. }
  1148. }
  1149. break
  1150. }
  1151. }
  1152. } else {
  1153. for _, old := range oldList {
  1154. updateStatus := true
  1155. for _, data := range dataList {
  1156. if (old.Get("lweight").ValueStr() == data["lweight"].(string)) && old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1157. updateStatus = false
  1158. break
  1159. }
  1160. }
  1161. if updateStatus {
  1162. now := time.Now()
  1163. for _, old := range oldList {
  1164. exist := false
  1165. var lweight, tmrid, tmrname interface{}
  1166. for _, data := range dataList {
  1167. if old.Get("barid").ValueInt64() == data["barid"].(int64) {
  1168. lweight = data["lweight"]
  1169. tmrid = data["tmrid"]
  1170. tmrname = data["tmrname"]
  1171. exist = true
  1172. break
  1173. }
  1174. }
  1175. if !exist {
  1176. tmrid = old.Get("tmrid").ValueInt64()
  1177. tmrname = old.Get("tmrname").ValueStr()
  1178. lweight = 0
  1179. }
  1180. _, err = tx.SQL(` insert into lpplandtl1history(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  1181. background,cowcount,ccountradio,lweighthis,createdate) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  1182. old.Get("pastureid").ValueStr(), old.Get("lppid").ValueStr(), old.Get("barid").ValueStr(),
  1183. old.Get("barname").ValueStr(), old.Get("fpdid").ValueStr(), old.Get("fttype").ValueStr(),
  1184. lweight, old.Get("sort").ValueStr(), tmrid,
  1185. tmrname, old.Get("background").ValueStr(), old.Get("cowcount").ValueStr(),
  1186. old.Get("ccountradio").ValueStr(), old.Get("lweighthis").ValueInt64(), now).Execute()
  1187. if err != nil {
  1188. log.Println("UpdateLpplan-error-6: ", err)
  1189. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1190. return
  1191. }
  1192. }
  1193. break
  1194. }
  1195. }
  1196. }
  1197. appG.Response(http.StatusOK, e.SUCCESS, true)
  1198. }
  1199. func GetSpillageDay(c *gin.Context) {
  1200. appG := app.Gin{C: c}
  1201. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1202. fsion := gofasion.NewFasion(string(dataByte))
  1203. parammaps := fsion.Get("parammaps")
  1204. pastureid := parammaps.Get("pastureid").ValueInt64()
  1205. times := parammaps.Get("times").ValueInt64()
  1206. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  1207. // refresh 0 全部 , 1 车次信息, 2 班次
  1208. refresh := parammaps.Get("refresh").ValueInt64()
  1209. date := parammaps.Get("date").ValueStr()
  1210. tx := restful.Engine.NewSession()
  1211. defer tx.Close()
  1212. data := make(map[string]interface{})
  1213. if refresh == 0 || refresh == 2 {
  1214. 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
  1215. FROM feedpdate feedp
  1216. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1217. INNER JOIN feedtemplet ft
  1218. ON ft.id = fpd.ptid AND ft.pastureid= feedp.pastureid
  1219. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1220. GROUP BY fpd.ptid
  1221. UNION
  1222. 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
  1223. FROM feedpdate feedp
  1224. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1225. INNER JOIN feedtemplet ft
  1226. ON ft.id = fpd.ptsid AND ft.pastureid=feedp.pastureid
  1227. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1228. AND (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1229. GROUP BY fpd.ptsid`
  1230. if ftid != 0 {
  1231. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  1232. } else {
  1233. sqlStr = fmt.Sprintf(sqlStr, "", "")
  1234. }
  1235. ftList, err := tx.SQL(sqlStr, times, pastureid, date, times, pastureid, date).Query().List()
  1236. if err != nil {
  1237. log.Println("GetSpillageDay-error-1: ", err)
  1238. appG.Response(http.StatusOK, e.ERROR, nil)
  1239. return
  1240. }
  1241. // AND ( fpdetail.ptsid=?)
  1242. // AND (fpdetail.ptid=? )
  1243. 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 ,
  1244. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 1 isfill FROM fpdetaildate fpdetail
  1245. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ifnull(ptsrate,0)*weight,2) and fpdetail.date = ?
  1246. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid ) is not null
  1247. UNION
  1248. 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 ,
  1249. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 0 isfill FROM fpdetaildate fpdetail
  1250. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ifnull(ptsrate,0))*weight,2) and fpdetail.date = ?
  1251. and (select id from bar where id = fpdetail.barid and pastureid = fpdetail.pastureid) is not null
  1252. ORDER BY barid`, pastureid, times, date, pastureid, times, date).Query().List()
  1253. if err != nil {
  1254. log.Println("GetSpillageDay-error-2: ", err)
  1255. appG.Response(http.StatusOK, e.ERROR, nil)
  1256. return
  1257. }
  1258. for _, ft := range ftList {
  1259. arrList := make([]map[string]interface{}, 0)
  1260. arrid := ft["arrid"].(string)
  1261. for _, detail := range ftDetailList {
  1262. if _, ok := detail["ptid"]; ok {
  1263. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1264. detail["background"] = ft["background"]
  1265. detail["fttype"] = ft["type"].(int64)
  1266. arrList = append(arrList, detail)
  1267. }
  1268. } else if _, ok := detail["ptsid"]; ok {
  1269. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1270. detail["background"] = ft["background"]
  1271. detail["fttype"] = ft["type"].(int64)
  1272. arrList = append(arrList, detail)
  1273. }
  1274. }
  1275. }
  1276. if len(arrList) > 0 {
  1277. ft["arrList"] = arrList
  1278. }
  1279. }
  1280. data["ftlist"] = ftList
  1281. }
  1282. if refresh == 0 || refresh == 1 {
  1283. sqlstr := `SELECT
  1284. lpplan.date,
  1285. -- ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname)
  1286. lpplan.tmrname tmrname,trim(lpplan.tmrid) tmrid,lpplan.sort,lpplan.sel,lpplan.times timesInt,
  1287. CASE lpplan.times
  1288. WHEN 1 THEN
  1289. '第一班'
  1290. WHEN 2 THEN
  1291. '第二班'
  1292. WHEN 3 THEN
  1293. '第三班'
  1294. WHEN 4 THEN
  1295. '第四班'
  1296. WHEN 5 THEN
  1297. '第五班'
  1298. WHEN 6 THEN
  1299. '第六班'
  1300. END timesstr,issplit,
  1301. lpplan.times,display,begintime,
  1302. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  1303. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1date lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.date= ?),2),0) sumweight ,
  1304. maxweight,
  1305. TRIM(ftid) ftid,
  1306. TRIM(lpplan.id) id,
  1307. TRIM(lpplan.id) lppid,
  1308. TRIM(lpplan.pastureid) pastureid,
  1309. ifnull(dpl.havebutton,0) as havebutton,
  1310. ifnull((select volume from tmr where id = lpplan.tmrid ),0) tmrvolume,
  1311. (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,
  1312. (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,
  1313. ifnull((select volume from feedtemplet where id = lpplan.ftid ),0) ftvolume,
  1314. (select inforvalue from sysopt where pastureid = lpplan.pastureid and inforname = 'tmrUpper') tmrUpper,
  1315. (select inforvalue from sysopt where pastureid = lpplan.pastureid and inforname = 'tmrUnder') tmrUnder
  1316. FROM
  1317. lpplandate lpplan
  1318. left join downloadedplan dpl on dpl.pid = lpplan.id and DATE_FORMAT(dpl.mydate,'%Y-%m-%e') = lpplan.date
  1319. WHERE lpplan.pastureid = ? and lpplan.date = ? and lpplan.times <= (SELECT sysopt.inforvalue FROM sysopt
  1320. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  1321. `
  1322. arrsql := `SELECT
  1323. trim(lpplandtl1.lppid) lppid,lweight,
  1324. lpplandtl1.lweight weight,lpplandtl1.sort,
  1325. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  1326. TRIM(lpplandtl1.fpdid) fpdid,
  1327. TRIM(lpplandtl1.barid) barid,
  1328. TRIM(lpplandtl1.id) id,
  1329. TRIM(lpplandtl1.pastureid) pastureid,
  1330. lpplandtl1.background,
  1331. lpplandtl1.fttype,
  1332. trim(lpplandtl1.tmrid) tmrid,
  1333. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  1334. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  1335. fpdetail.ptid,
  1336. fpdetail.ptsid,
  1337. fpdetail.times,
  1338. fpdetail.cowcount,
  1339. fpdetail.ccountradio
  1340. FROM
  1341. lpplandtl1date lpplandtl1
  1342. inner join fpdetaildate fpdetail
  1343. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid and lpplandtl1.date = fpdetail.date
  1344. WHERE lpplandtl1.pastureid = ? and lpplandtl1.date = ? and lpplandtl1.lweight>0 `
  1345. var args []interface{}
  1346. args = append(args, date, pastureid, date)
  1347. if times != 0 {
  1348. sqlstr += " and lpplan.times = ? "
  1349. // arrsql += " and times = ? "
  1350. args = append(args, times)
  1351. }
  1352. sqlstr += " group by lpplan.id "
  1353. 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)
  1354. sqlstr += " ORDER BY qq.timesInt,qq.sort "
  1355. session := tx.SQL(sqlstr, args...)
  1356. lppList, err := session.Query().List()
  1357. if err != nil {
  1358. log.Println("GetSpillageDay-error-3: ", err)
  1359. appG.Response(http.StatusOK, e.ERROR, nil)
  1360. return
  1361. }
  1362. arrsql += " ORDER BY lpplandtl1.sort"
  1363. arrList, err := tx.SQL(arrsql, pastureid, date).QueryString() //获取
  1364. if err != nil {
  1365. log.Println("GetSpillage-error-4: ", err)
  1366. appG.Response(http.StatusOK, e.ERROR, nil)
  1367. return
  1368. }
  1369. for _, query := range lppList {
  1370. list := make([]map[string]string, 0)
  1371. for _, arr := range arrList {
  1372. if query["lppid"].(string) == arr["lppid"] {
  1373. list = append(list, arr)
  1374. }
  1375. }
  1376. query["arrList"] = list
  1377. }
  1378. data["list"] = lppList
  1379. data["total"] = len(lppList)
  1380. }
  1381. appG.Response(http.StatusOK, e.SUCCESS, data)
  1382. }
  1383. // func AddLpplandtl(c *gin.Context) {
  1384. // appG := app.Gin{C: c}
  1385. // dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1386. // fsion := gofasion.NewFasion(string(dataByte))
  1387. // parammaps := fsion.Get("parammaps")
  1388. // tx := restful.Engine.NewSession()
  1389. // defer tx.Close()
  1390. // exist := false
  1391. // var err error
  1392. // lpplandList := make([]*lpplandtl1, 0)
  1393. // err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1394. // OrderBy("sort").Find(&lpplandList)
  1395. // if err != nil {
  1396. // log.Println("AddLpplandtl-error-1: ", err)
  1397. // appG.Response(http.StatusOK, e.ERROR, err)
  1398. // return
  1399. // }
  1400. // if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1401. // for _, lppand := range lpplandList {
  1402. // if lppand.Id == parammaps.Get("id").ValueInt64() {
  1403. // if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1404. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1405. // return
  1406. // }
  1407. // exist = true
  1408. // break
  1409. // }
  1410. // }
  1411. // }
  1412. // tmrid := parammaps.Get("tmrid").ValueInt64()
  1413. // var sort int64 = -1
  1414. // if !exist {
  1415. // for _, lppand := range lpplandList {
  1416. // if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1417. // _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1418. // if err != nil {
  1419. // log.Println("AddLpplandtl-error-2: ", err)
  1420. // appG.Response(http.StatusOK, e.ERROR, err)
  1421. // return
  1422. // }
  1423. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1424. // return
  1425. // }
  1426. // }
  1427. // for i, lppand := range lpplandList {
  1428. // lppand.Sort = int64(i) + 1
  1429. // }
  1430. // updateLpplandList := make([]*lpplandtl1, 0)
  1431. // for i, lppand := range lpplandList {
  1432. // n := i + 1
  1433. // if lppand.Tmrid == tmrid {
  1434. // sort = int64(n) + 1
  1435. // lppand.Sort = int64(n)
  1436. // updateLpplandList = append(updateLpplandList, lppand)
  1437. // } else if sort > 0 {
  1438. // lppand.Sort = int64(n) + 2
  1439. // updateLpplandList = append(updateLpplandList, lppand)
  1440. // }
  1441. // }
  1442. // tx.Begin()
  1443. // for _, lppand := range updateLpplandList {
  1444. // _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1445. // if err != nil {
  1446. // tx.Rollback()
  1447. // log.Println("AddLpplandtl-error-2: ", err)
  1448. // appG.Response(http.StatusOK, e.ERROR, err)
  1449. // return
  1450. // }
  1451. // }
  1452. // if sort < 0 && len(lpplandList) > 0 {
  1453. // sort = int64(len(lpplandList) + 2)
  1454. // } else if len(lpplandList) == 0 {
  1455. // sort = 1
  1456. // }
  1457. // _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1458. // VALUES (?,?,?,?,?,?,?,?,?,
  1459. // ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1460. // parammaps.Get("pastureid").ValueStr(),
  1461. // parammaps.Get("lppid").ValueStr(),
  1462. // parammaps.Get("barid").ValueStr(),
  1463. // parammaps.Get("barname").ValueStr(),
  1464. // parammaps.Get("fpdid").ValueStr(),
  1465. // parammaps.Get("lweight").ValueStr(),
  1466. // sort,
  1467. // parammaps.Get("tmrid").ValueStr(),
  1468. // parammaps.Get("tmrname").ValueStr(),
  1469. // parammaps.Get("fttype").ValueStr(),
  1470. // parammaps.Get("background").ValueStr(),
  1471. // parammaps.Get("cowcount").ValueStr(),
  1472. // parammaps.Get("cowcount").ValueStr(),
  1473. // parammaps.Get("ccountradio").ValueStr(),
  1474. // parammaps.Get("ccountradio").ValueStr()).Execute()
  1475. // if err != nil {
  1476. // tx.Rollback()
  1477. // log.Println("AddLpplandtl-error-3: ", err)
  1478. // appG.Response(http.StatusOK, e.ERROR, err)
  1479. // return
  1480. // }
  1481. // err = tx.Commit()
  1482. // if err != nil {
  1483. // tx.Rollback()
  1484. // log.Println("AddLpplandtl-error-6: ", err)
  1485. // appG.Response(http.StatusOK, e.ERROR, err)
  1486. // return
  1487. // }
  1488. // } else {
  1489. // lpplandid := parammaps.Get("id").ValueInt64()
  1490. // updateLpplandList := make([]*lpplandtl1, 0)
  1491. // var oldsort int64
  1492. // for i, lppand := range lpplandList {
  1493. // lppand.Sort = int64(i) + 1
  1494. // }
  1495. // for _, lppand := range lpplandList {
  1496. // if lppand.Id == lpplandid {
  1497. // oldsort = lppand.Sort
  1498. // break
  1499. // }
  1500. // }
  1501. // for _, lppand := range lpplandList {
  1502. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  1503. // sort = lppand.Sort
  1504. // break
  1505. // }
  1506. // }
  1507. // if sort == -1 {
  1508. // sort = 1
  1509. // }
  1510. // if oldsort < sort {
  1511. // for _, lppand := range lpplandList {
  1512. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  1513. // lppand.Sort--
  1514. // updateLpplandList = append(updateLpplandList, lppand)
  1515. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  1516. // lppand.Sort++
  1517. // updateLpplandList = append(updateLpplandList, lppand)
  1518. // }
  1519. // }
  1520. // sort--
  1521. // } else {
  1522. // for _, lppand := range lpplandList {
  1523. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  1524. // lppand.Sort++
  1525. // updateLpplandList = append(updateLpplandList, lppand)
  1526. // }
  1527. // }
  1528. // }
  1529. // var tmrlpplandList []*lpplandtl1
  1530. // for _, lppand := range lpplandList {
  1531. // exist := false
  1532. // for _, lppand1 := range updateLpplandList {
  1533. // if lppand.Id == lppand1.Id {
  1534. // exist = true
  1535. // break
  1536. // }
  1537. // }
  1538. // if !exist {
  1539. // tmrlpplandList = append(tmrlpplandList, lppand)
  1540. // }
  1541. // }
  1542. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  1543. // if len(updateLpplandList) > 0 {
  1544. // tx.Begin()
  1545. // for _, lppand := range updateLpplandList {
  1546. // _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1547. // if err != nil {
  1548. // tx.Rollback()
  1549. // log.Println("AddLpplandtl-error-2: ", err)
  1550. // appG.Response(http.StatusOK, e.ERROR, err)
  1551. // return
  1552. // }
  1553. // }
  1554. // _, 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()
  1555. // if err != nil {
  1556. // tx.Rollback()
  1557. // log.Println("AddLpplandtl-error-2: ", err)
  1558. // appG.Response(http.StatusOK, e.ERROR, err)
  1559. // return
  1560. // }
  1561. // tx.Commit()
  1562. // }
  1563. // }
  1564. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1565. // }
  1566. func AddLpplandtl(c *gin.Context) {
  1567. appG := app.Gin{C: c}
  1568. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1569. fsion := gofasion.NewFasion(string(dataByte))
  1570. parammaps := fsion.Get("parammaps")
  1571. tx := restful.Engine.NewSession()
  1572. defer tx.Close()
  1573. exist := false
  1574. var err error
  1575. lpplandList := make([]*lpplandtl1, 0)
  1576. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1577. OrderBy("sort").Find(&lpplandList)
  1578. if err != nil {
  1579. log.Println("AddLpplandtl-error-1: ", err)
  1580. appG.Response(http.StatusOK, e.ERROR, err)
  1581. return
  1582. }
  1583. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1584. for _, lppand := range lpplandList {
  1585. if lppand.Id == parammaps.Get("id").ValueInt64() {
  1586. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1587. appG.Response(http.StatusOK, e.SUCCESS, true)
  1588. return
  1589. }
  1590. exist = true
  1591. break
  1592. }
  1593. }
  1594. }
  1595. tmrid := parammaps.Get("tmrid").ValueInt64()
  1596. if !exist {
  1597. var sort int64 = -1
  1598. var maxsort int64
  1599. for _, lppand := range lpplandList {
  1600. if maxsort <= lppand.Sort {
  1601. maxsort = lppand.Sort
  1602. }
  1603. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1604. _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1605. if err != nil {
  1606. log.Println("AddLpplandtl-error-2: ", err)
  1607. appG.Response(http.StatusOK, e.ERROR, err)
  1608. return
  1609. }
  1610. appG.Response(http.StatusOK, e.SUCCESS, true)
  1611. return
  1612. }
  1613. }
  1614. for i, lppand := range lpplandList {
  1615. lppand.Sort = int64(i) + 1
  1616. }
  1617. updateLpplandList := make([]*lpplandtl1, 0)
  1618. for i, lppand := range lpplandList {
  1619. n := i + 1
  1620. if lppand.Tmrid == tmrid {
  1621. sort = int64(n) + 1
  1622. lppand.Sort = int64(n)
  1623. updateLpplandList = append(updateLpplandList, lppand)
  1624. } else if sort > 0 {
  1625. lppand.Sort = int64(n) + 2
  1626. updateLpplandList = append(updateLpplandList, lppand)
  1627. }
  1628. }
  1629. tx.Begin()
  1630. for _, lppand := range updateLpplandList {
  1631. _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1632. if err != nil {
  1633. tx.Rollback()
  1634. log.Println("AddLpplandtl-error-2: ", err)
  1635. appG.Response(http.StatusOK, e.ERROR, err)
  1636. return
  1637. }
  1638. }
  1639. if sort < 0 && len(lpplandList) > 0 {
  1640. sort = maxsort + 1
  1641. } else if len(lpplandList) == 0 {
  1642. sort = 1
  1643. }
  1644. _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1645. VALUES (?,?,?,?,?,?,?,?,?,
  1646. ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1647. parammaps.Get("pastureid").ValueStr(),
  1648. parammaps.Get("lppid").ValueStr(),
  1649. parammaps.Get("barid").ValueStr(),
  1650. parammaps.Get("barname").ValueStr(),
  1651. parammaps.Get("fpdid").ValueStr(),
  1652. parammaps.Get("lweight").ValueStr(),
  1653. sort,
  1654. parammaps.Get("tmrid").ValueStr(),
  1655. parammaps.Get("tmrname").ValueStr(),
  1656. parammaps.Get("fttype").ValueStr(),
  1657. parammaps.Get("background").ValueStr(),
  1658. parammaps.Get("cowcount").ValueStr(),
  1659. parammaps.Get("cowcount").ValueStr(),
  1660. parammaps.Get("ccountradio").ValueStr(),
  1661. parammaps.Get("ccountradio").ValueStr()).Execute()
  1662. if err != nil {
  1663. tx.Rollback()
  1664. log.Println("AddLpplandtl-error-3: ", err)
  1665. appG.Response(http.StatusOK, e.ERROR, err)
  1666. return
  1667. }
  1668. err = tx.Commit()
  1669. if err != nil {
  1670. tx.Rollback()
  1671. log.Println("AddLpplandtl-error-6: ", err)
  1672. appG.Response(http.StatusOK, e.ERROR, err)
  1673. return
  1674. }
  1675. } else {
  1676. lpplandid := parammaps.Get("id").ValueInt64()
  1677. // updateLpplandList := make([]*lpplandtl1, 0)
  1678. var oldsort int64
  1679. for i, lppand := range lpplandList {
  1680. lppand.Sort = int64(i) + 1
  1681. }
  1682. noup := false
  1683. for i, lppand := range lpplandList {
  1684. if lppand.Id == lpplandid {
  1685. oldsort = lppand.Sort
  1686. b := false
  1687. for _, lppand := range lpplandList {
  1688. if lppand.Tmrid == tmrid {
  1689. noup = false
  1690. b = true
  1691. break
  1692. }
  1693. }
  1694. if !b {
  1695. if i == 0 || i == len(lpplandList)-1 {
  1696. noup = true
  1697. }
  1698. if i != 0 && i < len(lpplandList)-1 {
  1699. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  1700. noup = true
  1701. }
  1702. }
  1703. // if lpplandList[i+1].Tmrid == tmrid {
  1704. // _, 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()
  1705. // if err != nil {
  1706. // tx.Rollback()
  1707. // log.Println("AddLpplandtl-error-2: ", err)
  1708. // appG.Response(http.StatusOK, e.ERROR, err)
  1709. // return
  1710. // }
  1711. // tx.Commit()
  1712. // appG.Response(http.StatusOK, e.SUCCESS, true)
  1713. // return
  1714. // }
  1715. // }
  1716. // break
  1717. // }
  1718. // }
  1719. }
  1720. break
  1721. }
  1722. }
  1723. if noup {
  1724. _, 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()
  1725. if err != nil {
  1726. tx.Rollback()
  1727. log.Println("AddLpplandtl-error-2: ", err)
  1728. appG.Response(http.StatusOK, e.ERROR, err)
  1729. return
  1730. }
  1731. } else {
  1732. for i, lppand := range lpplandList {
  1733. if lppand.Id == lpplandid {
  1734. if i != 0 {
  1735. if lpplandList[i-1].Tmrid == tmrid {
  1736. _, 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()
  1737. if err != nil {
  1738. tx.Rollback()
  1739. log.Println("AddLpplandtl-error-2: ", err)
  1740. appG.Response(http.StatusOK, e.ERROR, err)
  1741. return
  1742. }
  1743. tx.Commit()
  1744. appG.Response(http.StatusOK, e.SUCCESS, true)
  1745. return
  1746. }
  1747. }
  1748. if i < len(lpplandList)-1 {
  1749. if lpplandList[i+1].Tmrid == tmrid {
  1750. _, 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()
  1751. if err != nil {
  1752. tx.Rollback()
  1753. log.Println("AddLpplandtl-error-2: ", err)
  1754. appG.Response(http.StatusOK, e.ERROR, err)
  1755. return
  1756. }
  1757. tx.Commit()
  1758. appG.Response(http.StatusOK, e.SUCCESS, true)
  1759. return
  1760. }
  1761. }
  1762. break
  1763. }
  1764. }
  1765. exist := false
  1766. sort1 := 0
  1767. a := false
  1768. for _, lppand := range lpplandList {
  1769. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  1770. sort1 = int(lppand.Sort)
  1771. if !a {
  1772. for _, lppand1 := range lpplandList {
  1773. if lppand1.Id == lpplandid {
  1774. lppand1.Tmrid = tmrid
  1775. lppand1.Tmrname = lppand.Tmrname
  1776. a = true
  1777. // lppand1.Sort = lppand.Sort
  1778. break
  1779. }
  1780. }
  1781. }
  1782. exist = true
  1783. }
  1784. }
  1785. if exist {
  1786. if oldsort > int64(sort1) {
  1787. for _, lppand := range lpplandList {
  1788. if lppand.Id == lpplandid {
  1789. lppand.Sort = int64(sort1) + 1
  1790. continue
  1791. }
  1792. if lppand.Sort >= int64(sort1)+1 {
  1793. lppand.Sort++
  1794. }
  1795. }
  1796. } else {
  1797. for _, lppand := range lpplandList {
  1798. if lppand.Id == lpplandid {
  1799. lppand.Sort = int64(sort1) + 1
  1800. continue
  1801. }
  1802. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  1803. lppand.Sort--
  1804. } else if lppand.Sort <= int64(sort1)+1 {
  1805. lppand.Sort++
  1806. }
  1807. }
  1808. }
  1809. } else {
  1810. for _, lppand := range lpplandList {
  1811. if lppand.Id == lpplandid {
  1812. lppand.Sort = int64(len(lpplandList)) + 1
  1813. lppand.Tmrid = tmrid
  1814. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  1815. break
  1816. }
  1817. }
  1818. }
  1819. sort.Slice(lpplandList, func(i, j int) bool {
  1820. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  1821. })
  1822. for i, lppand := range lpplandList {
  1823. lppand.Sort = int64(i) + 1
  1824. }
  1825. for _, lppand := range lpplandList {
  1826. _, err := tx.SQL(" update lpplandtl1 set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? ", lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1827. if err != nil {
  1828. tx.Rollback()
  1829. log.Println("AddLpplandtl-error-2: ", err)
  1830. appG.Response(http.StatusOK, e.ERROR, err)
  1831. return
  1832. }
  1833. }
  1834. }
  1835. tx.Commit()
  1836. }
  1837. appG.Response(http.StatusOK, e.SUCCESS, true)
  1838. }
  1839. func UpdateLpplandtlSort(c *gin.Context) {
  1840. appG := app.Gin{C: c}
  1841. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1842. fsion := gofasion.NewFasion(string(dataByte))
  1843. parammaps := fsion.Get("parammaps")
  1844. tx := restful.Engine.NewSession()
  1845. defer tx.Close()
  1846. var err error
  1847. lpplandList := make([]*lpplandtl1, 0)
  1848. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1849. OrderBy("sort").Find(&lpplandList)
  1850. if err != nil {
  1851. log.Println("UpdateLpplandtlSort-error-1: ", err)
  1852. appG.Response(http.StatusOK, e.ERROR, err)
  1853. return
  1854. }
  1855. var oldsort, lpplandtlsort int64
  1856. tmrid := parammaps.Get("tmrid").ValueInt64()
  1857. sort := parammaps.Get("sort").ValueInt64()
  1858. lpplandtlid := parammaps.Get("id").ValueInt64()
  1859. sort1 := parammaps.Get("sort").ValueInt64()
  1860. for i, lppland := range lpplandList {
  1861. lppland.Sort = int64(i) + 1
  1862. }
  1863. for _, lppland := range lpplandList {
  1864. if tmrid == lppland.Tmrid {
  1865. oldsort = lppland.Sort
  1866. break
  1867. }
  1868. }
  1869. for _, lppland := range lpplandList {
  1870. if lppland.Sort == sort {
  1871. for _, lppland1 := range lpplandList {
  1872. if lppland.Tmrid == lppland1.Tmrid {
  1873. sort = lppland1.Sort
  1874. if oldsort > sort {
  1875. break
  1876. }
  1877. }
  1878. }
  1879. break
  1880. }
  1881. }
  1882. if oldsort == sort {
  1883. appG.Response(http.StatusOK, e.SUCCESS, true)
  1884. return
  1885. }
  1886. tmrLpplandList := make([]*lpplandtl1, 0)
  1887. for _, lppland := range lpplandList {
  1888. if lppland.Tmrid == tmrid {
  1889. tmrLpplandList = append(tmrLpplandList, lppland)
  1890. if lppland.Id == lpplandtlid {
  1891. lpplandtlsort = lppland.Sort
  1892. }
  1893. }
  1894. }
  1895. exist := false
  1896. for _, tmrLppland := range tmrLpplandList {
  1897. if sort1 == tmrLppland.Sort {
  1898. exist = true
  1899. break
  1900. }
  1901. }
  1902. if exist {
  1903. for _, tmrLppland := range tmrLpplandList {
  1904. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  1905. tmrLppland.Sort++
  1906. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  1907. tmrLppland.Sort--
  1908. }
  1909. }
  1910. for _, tmrLppland := range tmrLpplandList {
  1911. if tmrLppland.Id == lpplandtlid {
  1912. tmrLppland.Sort = sort1
  1913. break
  1914. }
  1915. }
  1916. } else {
  1917. if oldsort < sort && len(tmrLpplandList) > 1 {
  1918. for i, tmrLppland := range tmrLpplandList {
  1919. tmrLppland.Sort = sort + int64(i) - 1
  1920. }
  1921. } else {
  1922. for i, tmrLppland := range tmrLpplandList {
  1923. tmrLppland.Sort = sort + int64(i)
  1924. }
  1925. }
  1926. }
  1927. if oldsort > sort {
  1928. for _, lppland := range lpplandList {
  1929. if lppland.Sort == sort {
  1930. // uptmrid = lppland.Tmrid
  1931. sort = lppland.Sort
  1932. break
  1933. }
  1934. }
  1935. } else {
  1936. var uptmrid int64
  1937. for _, lppland := range lpplandList {
  1938. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  1939. uptmrid = lppland.Tmrid
  1940. sort = lppland.Sort
  1941. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  1942. sort = lppland.Sort
  1943. }
  1944. }
  1945. }
  1946. upcount := int64(len(tmrLpplandList))
  1947. if oldsort > sort {
  1948. for _, lppland := range lpplandList {
  1949. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  1950. lppland.Sort += upcount
  1951. tmrLpplandList = append(tmrLpplandList, lppland)
  1952. }
  1953. }
  1954. } else {
  1955. for _, lppland := range lpplandList {
  1956. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  1957. lppland.Sort -= upcount
  1958. tmrLpplandList = append(tmrLpplandList, lppland)
  1959. }
  1960. }
  1961. }
  1962. updateLpplandList := make([]*lpplandtl1, 0)
  1963. for _, lppland := range lpplandList {
  1964. exist := false
  1965. for _, tmrLppland := range tmrLpplandList {
  1966. if lppland.Id == tmrLppland.Id {
  1967. exist = true
  1968. break
  1969. }
  1970. }
  1971. if !exist {
  1972. updateLpplandList = append(updateLpplandList, lppland)
  1973. }
  1974. }
  1975. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  1976. // tmrLpplandMap := make(map[int64]bool)
  1977. // for _, item := range tmrLpplandList {
  1978. // tmrLpplandMap[item.Sort] = false
  1979. // }
  1980. for _, item := range tmrLpplandList {
  1981. exist := false
  1982. for _, item1 := range tmrLpplandList {
  1983. if item.Sort == item1.Sort && item.Id != item1.Id {
  1984. exist = true
  1985. }
  1986. }
  1987. if exist {
  1988. appG.Response(http.StatusOK, e.ERROR, "顺序调整有误,请重新调整!!!")
  1989. return
  1990. }
  1991. }
  1992. tx.Begin()
  1993. var wg sync.WaitGroup
  1994. for _, lppland := range tmrLpplandList {
  1995. wg.Add(1)
  1996. go func(lppland *lpplandtl1) {
  1997. defer wg.Done()
  1998. _, err = tx.SQL(`update lpplandtl1 set sort = ? where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
  1999. if err != nil {
  2000. log.Println("UpdateLpplandtlSort-error-2: ", err)
  2001. tx.Rollback()
  2002. appG.Response(http.StatusOK, e.ERROR, err)
  2003. return
  2004. }
  2005. }(lppland)
  2006. }
  2007. wg.Wait()
  2008. err = tx.Commit()
  2009. if err != nil {
  2010. log.Println("UpdateLpplandtlSort-error-3: ", err)
  2011. tx.Rollback()
  2012. appG.Response(http.StatusOK, e.ERROR, err)
  2013. return
  2014. }
  2015. appG.Response(http.StatusOK, e.SUCCESS, true)
  2016. }
  2017. func AddLpplandtlDate(c *gin.Context) {
  2018. appG := app.Gin{C: c}
  2019. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2020. fsion := gofasion.NewFasion(string(dataByte))
  2021. parammaps := fsion.Get("parammaps")
  2022. tx := restful.Engine.NewSession()
  2023. defer tx.Close()
  2024. exist := false
  2025. var err error
  2026. lpplandList := make([]*lpplandtl1, 0)
  2027. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,barid,tmrid,tmrname").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2028. And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
  2029. if err != nil {
  2030. log.Println("AddLpplandtl-error-1: ", err)
  2031. appG.Response(http.StatusOK, e.ERROR, err)
  2032. return
  2033. }
  2034. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  2035. for _, lppand := range lpplandList {
  2036. fmt.Println(parammaps.Get("id").ValueInt64())
  2037. if lppand.Id == parammaps.Get("id").ValueInt64() {
  2038. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  2039. appG.Response(http.StatusOK, e.SUCCESS, true)
  2040. return
  2041. }
  2042. exist = true
  2043. break
  2044. }
  2045. }
  2046. }
  2047. tmrid := parammaps.Get("tmrid").ValueInt64()
  2048. if !exist {
  2049. var sort int64 = -1
  2050. for _, lppand := range lpplandList {
  2051. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  2052. _, err := tx.SQL(" update lpplandtl1date set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  2053. if err != nil {
  2054. log.Println("AddLpplandtl-error-2: ", err)
  2055. appG.Response(http.StatusOK, e.ERROR, err)
  2056. return
  2057. }
  2058. appG.Response(http.StatusOK, e.SUCCESS, true)
  2059. return
  2060. }
  2061. }
  2062. for i, lppand := range lpplandList {
  2063. lppand.Sort = int64(i) + 1
  2064. }
  2065. updateLpplandList := make([]*lpplandtl1, 0)
  2066. for i, lppand := range lpplandList {
  2067. n := i + 1
  2068. if lppand.Tmrid == tmrid {
  2069. sort = int64(n) + 1
  2070. lppand.Sort = int64(n)
  2071. updateLpplandList = append(updateLpplandList, lppand)
  2072. } else if sort > 0 {
  2073. lppand.Sort = int64(n) + 2
  2074. updateLpplandList = append(updateLpplandList, lppand)
  2075. }
  2076. }
  2077. tx.Begin()
  2078. for _, lppand := range updateLpplandList {
  2079. _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? ", lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2080. if err != nil {
  2081. tx.Rollback()
  2082. log.Println("AddLpplandtl-error-2: ", err)
  2083. appG.Response(http.StatusOK, e.ERROR, err)
  2084. return
  2085. }
  2086. }
  2087. if sort < 0 && len(lpplandList) > 0 {
  2088. sort = int64(len(lpplandList) + 1)
  2089. } else if len(lpplandList) == 0 {
  2090. sort = 1
  2091. }
  2092. _, err = tx.SQL(`INSERT INTO lpplandtl1date(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio,date)
  2093. VALUES (?,?,?,?,?,?,?,?,?,
  2094. ?,?,IF(?='',0,?),IF(?='',0,?),?)`,
  2095. parammaps.Get("pastureid").ValueStr(),
  2096. parammaps.Get("lppid").ValueStr(),
  2097. parammaps.Get("barid").ValueStr(),
  2098. parammaps.Get("barname").ValueStr(),
  2099. parammaps.Get("fpdid").ValueStr(),
  2100. parammaps.Get("lweight").ValueStr(),
  2101. sort,
  2102. parammaps.Get("tmrid").ValueStr(),
  2103. parammaps.Get("tmrname").ValueStr(),
  2104. parammaps.Get("fttype").ValueStr(),
  2105. parammaps.Get("background").ValueStr(),
  2106. parammaps.Get("cowcount").ValueStr(),
  2107. parammaps.Get("cowcount").ValueStr(),
  2108. parammaps.Get("ccountradio").ValueStr(),
  2109. parammaps.Get("ccountradio").ValueStr(),
  2110. parammaps.Get("date").ValueStr()).Execute()
  2111. if err != nil {
  2112. tx.Rollback()
  2113. log.Println("AddLpplandtl-error-3: ", err)
  2114. appG.Response(http.StatusOK, e.ERROR, err)
  2115. return
  2116. }
  2117. err = tx.Commit()
  2118. if err != nil {
  2119. tx.Rollback()
  2120. log.Println("AddLpplandtl-error-6: ", err)
  2121. appG.Response(http.StatusOK, e.ERROR, err)
  2122. return
  2123. }
  2124. } else {
  2125. lpplandid := parammaps.Get("id").ValueInt64()
  2126. // updateLpplandList := make([]*lpplandtl1, 0)
  2127. var oldsort int64
  2128. for i, lppand := range lpplandList {
  2129. lppand.Sort = int64(i) + 1
  2130. }
  2131. noup := false
  2132. for i, lppand := range lpplandList {
  2133. if lppand.Id == lpplandid {
  2134. oldsort = lppand.Sort
  2135. b := false
  2136. for _, lppand := range lpplandList {
  2137. if lppand.Tmrid == tmrid {
  2138. noup = false
  2139. b = true
  2140. break
  2141. }
  2142. }
  2143. if !b {
  2144. if i == 0 || i == len(lpplandList)-1 {
  2145. noup = true
  2146. }
  2147. if i != 0 && i < len(lpplandList)-1 {
  2148. if lpplandList[i-1].Tmrid != lppand.Tmrid || lpplandList[i+1].Tmrid != lppand.Tmrid {
  2149. noup = true
  2150. }
  2151. }
  2152. }
  2153. break
  2154. }
  2155. }
  2156. if noup {
  2157. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2158. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2159. if err != nil {
  2160. tx.Rollback()
  2161. log.Println("AddLpplandtl-error-2: ", err)
  2162. appG.Response(http.StatusOK, e.ERROR, err)
  2163. return
  2164. }
  2165. } else {
  2166. for i, lppand := range lpplandList {
  2167. if lppand.Id == lpplandid {
  2168. if i != 0 {
  2169. if lpplandList[i-1].Tmrid == tmrid {
  2170. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ",
  2171. parammaps.Get("tmrid").ValueStr(), parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2172. if err != nil {
  2173. tx.Rollback()
  2174. log.Println("AddLpplandtl-error-2: ", err)
  2175. appG.Response(http.StatusOK, e.ERROR, err)
  2176. return
  2177. }
  2178. tx.Commit()
  2179. appG.Response(http.StatusOK, e.SUCCESS, true)
  2180. return
  2181. }
  2182. }
  2183. if i < len(lpplandList)-1 {
  2184. if lpplandList[i+1].Tmrid == tmrid {
  2185. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ? where id = ? and pastureid = ? and date = ? ", parammaps.Get("tmrid").ValueStr(),
  2186. parammaps.Get("tmrname").ValueStr(), lpplandid, parammaps.Get("pastureid").ValueStr(), parammaps.Get("date").ValueStr()).Execute()
  2187. if err != nil {
  2188. tx.Rollback()
  2189. log.Println("AddLpplandtl-error-2: ", err)
  2190. appG.Response(http.StatusOK, e.ERROR, err)
  2191. return
  2192. }
  2193. tx.Commit()
  2194. appG.Response(http.StatusOK, e.SUCCESS, true)
  2195. return
  2196. }
  2197. }
  2198. break
  2199. }
  2200. }
  2201. exist := false
  2202. sort1 := 0
  2203. a := false
  2204. for _, lppand := range lpplandList {
  2205. if lppand.Tmrid == tmrid && lppand.Id != lpplandid {
  2206. sort1 = int(lppand.Sort)
  2207. if !a {
  2208. for _, lppand1 := range lpplandList {
  2209. if lppand1.Id == lpplandid {
  2210. lppand1.Tmrid = tmrid
  2211. lppand1.Tmrname = lppand.Tmrname
  2212. a = true
  2213. // lppand1.Sort = lppand.Sort
  2214. break
  2215. }
  2216. }
  2217. }
  2218. exist = true
  2219. }
  2220. }
  2221. if exist {
  2222. if oldsort > int64(sort1) {
  2223. for _, lppand := range lpplandList {
  2224. if lppand.Id == lpplandid {
  2225. lppand.Sort = int64(sort1) + 1
  2226. continue
  2227. }
  2228. if lppand.Sort >= int64(sort1)+1 {
  2229. lppand.Sort++
  2230. }
  2231. }
  2232. } else {
  2233. for _, lppand := range lpplandList {
  2234. if lppand.Id == lpplandid {
  2235. lppand.Sort = int64(sort1) + 1
  2236. continue
  2237. }
  2238. // if lppand.Sort > oldsort {
  2239. // lppand.Sort--
  2240. // }
  2241. if lppand.Sort > oldsort && lppand.Sort < int64(sort1)+1 {
  2242. lppand.Sort--
  2243. } else if lppand.Sort <= int64(sort1)+1 {
  2244. lppand.Sort++
  2245. }
  2246. }
  2247. }
  2248. } else {
  2249. for _, lppand := range lpplandList {
  2250. if lppand.Id == lpplandid {
  2251. lppand.Sort = int64(len(lpplandList)) + 1
  2252. lppand.Tmrid = tmrid
  2253. lppand.Tmrname = parammaps.Get("tmrname").ValueStr()
  2254. break
  2255. }
  2256. }
  2257. }
  2258. sort.Slice(lpplandList, func(i, j int) bool {
  2259. return lpplandList[i].Sort < lpplandList[j].Sort // 升序
  2260. })
  2261. for i, lppand := range lpplandList {
  2262. lppand.Sort = int64(i) + 1
  2263. }
  2264. for _, lppand := range lpplandList {
  2265. _, err := tx.SQL(" update lpplandtl1date set tmrid = ? , tmrname = ?,sort = ? where id = ? and pastureid = ? and date = ? ",
  2266. lppand.Tmrid, lppand.Tmrname, lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  2267. if err != nil {
  2268. tx.Rollback()
  2269. log.Println("AddLpplandtl-error-2: ", err)
  2270. appG.Response(http.StatusOK, e.ERROR, err)
  2271. return
  2272. }
  2273. }
  2274. }
  2275. tx.Commit()
  2276. // lpplandid := parammaps.Get("id").ValueInt64()
  2277. // updateLpplandList := make([]*lpplandtl1, 0)
  2278. // var oldsort int64
  2279. // for i, lppand := range lpplandList {
  2280. // lppand.Sort = int64(i) + 1
  2281. // }
  2282. // for _, lppand := range lpplandList {
  2283. // if lppand.Id == lpplandid {
  2284. // oldsort = lppand.Sort
  2285. // break
  2286. // }
  2287. // }
  2288. // for _, lppand := range lpplandList {
  2289. // if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  2290. // sort = lppand.Sort
  2291. // break
  2292. // }
  2293. // }
  2294. // if sort == -1 {
  2295. // sort = 1
  2296. // }
  2297. // if oldsort < sort {
  2298. // for _, lppand := range lpplandList {
  2299. // if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  2300. // lppand.Sort--
  2301. // updateLpplandList = append(updateLpplandList, lppand)
  2302. // } else if lppand.Sort > sort && lppand.Id != lpplandid {
  2303. // lppand.Sort++
  2304. // updateLpplandList = append(updateLpplandList, lppand)
  2305. // }
  2306. // }
  2307. // sort--
  2308. // } else {
  2309. // for _, lppand := range lpplandList {
  2310. // if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  2311. // lppand.Sort++
  2312. // updateLpplandList = append(updateLpplandList, lppand)
  2313. // }
  2314. // }
  2315. // }
  2316. // var tmrlpplandList []*lpplandtl1
  2317. // for _, lppand := range lpplandList {
  2318. // exist := false
  2319. // for _, lppand1 := range updateLpplandList {
  2320. // if lppand.Id == lppand1.Id {
  2321. // exist = true
  2322. // break
  2323. // }
  2324. // }
  2325. // if !exist {
  2326. // tmrlpplandList = append(tmrlpplandList, lppand)
  2327. // }
  2328. // }
  2329. // updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  2330. // if len(updateLpplandList) > 0 {
  2331. // tx.Begin()
  2332. // for _, lppand := range updateLpplandList {
  2333. // _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  2334. // if err != nil {
  2335. // tx.Rollback()
  2336. // log.Println("AddLpplandtl-error-2: ", err)
  2337. // appG.Response(http.StatusOK, e.ERROR, err)
  2338. // return
  2339. // }
  2340. // }
  2341. // _, 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()
  2342. // if err != nil {
  2343. // tx.Rollback()
  2344. // log.Println("AddLpplandtl-error-2: ", err)
  2345. // appG.Response(http.StatusOK, e.ERROR, err)
  2346. // return
  2347. // }
  2348. // tx.Commit()
  2349. // }
  2350. }
  2351. appG.Response(http.StatusOK, e.SUCCESS, true)
  2352. }
  2353. func UpdateLpplandtlDateSort(c *gin.Context) {
  2354. appG := app.Gin{C: c}
  2355. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2356. fsion := gofasion.NewFasion(string(dataByte))
  2357. parammaps := fsion.Get("parammaps")
  2358. date := parammaps.Get("date").ValueStr()
  2359. tx := restful.Engine.NewSession()
  2360. defer tx.Close()
  2361. var err error
  2362. lpplandList := make([]*lpplandtl1, 0)
  2363. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  2364. And("date = ? ", date).OrderBy("sort").Find(&lpplandList)
  2365. if err != nil {
  2366. log.Println("UpdateLpplandtlSort-error-1: ", err)
  2367. appG.Response(http.StatusOK, e.ERROR, err)
  2368. return
  2369. }
  2370. var oldsort, lpplandtlsort int64
  2371. tmrid := parammaps.Get("tmrid").ValueInt64()
  2372. sort := parammaps.Get("sort").ValueInt64()
  2373. lpplandtlid := parammaps.Get("id").ValueInt64()
  2374. sort1 := parammaps.Get("sort").ValueInt64()
  2375. for i, lppland := range lpplandList {
  2376. lppland.Sort = int64(i) + 1
  2377. }
  2378. for _, lppland := range lpplandList {
  2379. if tmrid == lppland.Tmrid {
  2380. oldsort = lppland.Sort
  2381. break
  2382. }
  2383. }
  2384. for _, lppland := range lpplandList {
  2385. if lppland.Sort == sort {
  2386. for _, lppland1 := range lpplandList {
  2387. if lppland.Tmrid == lppland1.Tmrid {
  2388. sort = lppland1.Sort
  2389. if oldsort > sort {
  2390. break
  2391. }
  2392. }
  2393. }
  2394. break
  2395. }
  2396. }
  2397. if oldsort == sort {
  2398. appG.Response(http.StatusOK, e.SUCCESS, true)
  2399. return
  2400. }
  2401. tmrLpplandList := make([]*lpplandtl1, 0)
  2402. for _, lppland := range lpplandList {
  2403. if lppland.Tmrid == tmrid {
  2404. tmrLpplandList = append(tmrLpplandList, lppland)
  2405. if lppland.Id == lpplandtlid {
  2406. lpplandtlsort = lppland.Sort
  2407. }
  2408. }
  2409. }
  2410. exist := false
  2411. for _, tmrLppland := range tmrLpplandList {
  2412. if sort1 == tmrLppland.Sort {
  2413. exist = true
  2414. break
  2415. }
  2416. }
  2417. if exist {
  2418. for _, tmrLppland := range tmrLpplandList {
  2419. if sort1 <= tmrLppland.Sort && lpplandtlsort > tmrLppland.Sort {
  2420. tmrLppland.Sort++
  2421. } else if sort1 >= tmrLppland.Sort && lpplandtlsort <= tmrLppland.Sort {
  2422. tmrLppland.Sort--
  2423. }
  2424. }
  2425. for _, tmrLppland := range tmrLpplandList {
  2426. if tmrLppland.Id == lpplandtlid {
  2427. tmrLppland.Sort = sort1
  2428. break
  2429. }
  2430. }
  2431. } else {
  2432. if oldsort < sort && len(tmrLpplandList) > 1 {
  2433. for i, tmrLppland := range tmrLpplandList {
  2434. tmrLppland.Sort = sort + int64(i) - 1
  2435. }
  2436. } else {
  2437. for i, tmrLppland := range tmrLpplandList {
  2438. tmrLppland.Sort = sort + int64(i)
  2439. }
  2440. }
  2441. }
  2442. if oldsort > sort {
  2443. for _, lppland := range lpplandList {
  2444. if lppland.Sort == sort {
  2445. // uptmrid = lppland.Tmrid
  2446. sort = lppland.Sort
  2447. break
  2448. }
  2449. }
  2450. } else {
  2451. var uptmrid int64
  2452. for _, lppland := range lpplandList {
  2453. if lppland.Sort == sort && lppland.Tmrid != tmrid {
  2454. uptmrid = lppland.Tmrid
  2455. sort = lppland.Sort
  2456. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  2457. sort = lppland.Sort
  2458. }
  2459. }
  2460. }
  2461. upcount := int64(len(tmrLpplandList))
  2462. if oldsort > sort {
  2463. for _, lppland := range lpplandList {
  2464. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  2465. lppland.Sort += upcount
  2466. tmrLpplandList = append(tmrLpplandList, lppland)
  2467. }
  2468. }
  2469. } else {
  2470. for _, lppland := range lpplandList {
  2471. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  2472. lppland.Sort -= upcount
  2473. tmrLpplandList = append(tmrLpplandList, lppland)
  2474. }
  2475. }
  2476. }
  2477. updateLpplandList := make([]*lpplandtl1, 0)
  2478. for _, lppland := range lpplandList {
  2479. exist := false
  2480. for _, tmrLppland := range tmrLpplandList {
  2481. if lppland.Id == tmrLppland.Id {
  2482. exist = true
  2483. break
  2484. }
  2485. }
  2486. if !exist {
  2487. updateLpplandList = append(updateLpplandList, lppland)
  2488. }
  2489. }
  2490. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  2491. tx.Begin()
  2492. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  2493. var wg sync.WaitGroup
  2494. for _, lppland := range tmrLpplandList {
  2495. wg.Add(1)
  2496. go func(lppland *lpplandtl1) {
  2497. defer wg.Done()
  2498. _, err = tx.SQL(`update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? `, lppland.Sort, lppland.Id, lppland.Pastureid, date).Execute()
  2499. if err != nil {
  2500. log.Println("UpdateLpplandtlSort-error-2: ", err)
  2501. tx.Rollback()
  2502. appG.Response(http.StatusOK, e.ERROR, err)
  2503. return
  2504. }
  2505. }(lppland)
  2506. }
  2507. wg.Wait()
  2508. err = tx.Commit()
  2509. if err != nil {
  2510. log.Println("UpdateLpplandtlSort-error-3: ", err)
  2511. tx.Rollback()
  2512. appG.Response(http.StatusOK, e.ERROR, err)
  2513. return
  2514. }
  2515. appG.Response(http.StatusOK, e.SUCCESS, true)
  2516. }
  2517. func GetFeedpUndistributed(c *gin.Context) {
  2518. appG := app.Gin{C: c}
  2519. tx := restful.Engine.NewSession()
  2520. defer tx.Close()
  2521. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2522. fsion := gofasion.NewFasion(string(dataByte))
  2523. timesMax := fsion.Get("times").ValueInt()
  2524. pastureid := fsion.Get("pastureid").ValueStr()
  2525. data := make(map[string]interface{})
  2526. for times := 1; times <= timesMax; times++ {
  2527. 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
  2528. inner join feedtemplet ft
  2529. on ft.id = feedp.ftid and ft.pastureid=?
  2530. WHERE feedp.pastureid = ?
  2531. GROUP BY feedp.ftid
  2532. UNION
  2533. 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
  2534. inner join feedtemplet ft
  2535. on ft.id = feedp.ptsfid and ft.pastureid=?
  2536. WHERE feedp.pastureid = ? and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  2537. GROUP BY feedp.ptsfid`
  2538. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  2539. if err != nil {
  2540. log.Println("GetFeedpUndistributed-error-1: ", err)
  2541. appG.Response(http.StatusOK, e.ERROR, nil)
  2542. return
  2543. }
  2544. 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 ,
  2545. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  2546. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  2547. UNION
  2548. 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 ,
  2549. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  2550. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  2551. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  2552. if err != nil {
  2553. log.Println("GetFeedpUndistributed-error-2: ", err)
  2554. appG.Response(http.StatusOK, e.ERROR, nil)
  2555. return
  2556. }
  2557. for _, ft := range ftList {
  2558. arrList := make([]map[string]interface{}, 0)
  2559. arrid := ft["arrid"].(string)
  2560. for _, detail := range ftDetailList {
  2561. if _, ok := detail["ptid"]; ok {
  2562. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2563. detail["background"] = ft["background"]
  2564. arrList = append(arrList, detail)
  2565. }
  2566. } else if _, ok := detail["ptsid"]; ok {
  2567. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  2568. detail["background"] = ft["background"]
  2569. arrList = append(arrList, detail)
  2570. }
  2571. }
  2572. }
  2573. if len(arrList) > 0 {
  2574. ft["arrList"] = arrList
  2575. }
  2576. }
  2577. var key string
  2578. if times == 1 {
  2579. key = "one"
  2580. } else if times == 2 {
  2581. key = "two"
  2582. } else if times == 3 {
  2583. key = "three"
  2584. } else if times == 4 {
  2585. key = "four"
  2586. } else if times == 5 {
  2587. key = "five"
  2588. } else {
  2589. key = "six"
  2590. }
  2591. data[key] = ftList
  2592. }
  2593. appG.Response(http.StatusOK, e.SUCCESS, data)
  2594. }