spillage.go 90 KB

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