spillage.go 83 KB

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