spillage.go 89 KB

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