spillage.go 88 KB

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