spillage.go 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "../../pkg/app"
  14. "../../pkg/e"
  15. "../../pkg/logging"
  16. "../../pkg/setting"
  17. "../../routers/restful"
  18. "github.com/Anderson-Lu/gofasion/gofasion"
  19. "github.com/gin-gonic/gin"
  20. "github.com/xormplus/xorm"
  21. )
  22. type feedtemplet struct {
  23. Id int64 `xorm:"id"`
  24. Ccname string `xorm:"ccname"`
  25. Cname string `xorm:"tname"`
  26. }
  27. type tmrStruct struct {
  28. Id int64 `xorm:"id"`
  29. Tname string `xorm:"tname"`
  30. Maxstirfeed float64 `xorm:"maxstirfeed"`
  31. }
  32. type lpplanStruct struct {
  33. Id int64 `xorm:"id"`
  34. Tmrname string `xorm:"tmrname"`
  35. Tmrid int64 `xorm:"tmrid"`
  36. Sort int64 `xorm:"sort"`
  37. Sel int64 `xorm:"sel"`
  38. Times int64 `xorm:"times"`
  39. Ftname string `xorm:"ftname"`
  40. Ftid int64 `xorm:"ftid"`
  41. Maxweight float64 `xorm:"maxweight"`
  42. Sumweight float64 `xorm:"sumweight"`
  43. Sumcowcount int64 `xorm:"sumcowcount"`
  44. Pastureid string `xorm:"pastureid"`
  45. Display string `xorm:"display"`
  46. Issplit int64 `xorm:"issplit"`
  47. BeginTime string `xorm:"begintime"`
  48. }
  49. type sysopt struct {
  50. Inforvalue int `xorm:"inforvalue"`
  51. }
  52. type lpplanQueryInfo struct {
  53. Pastureid int64 `xorm:"pastureid"`
  54. Lppid int64 `xorm:"id"`
  55. Maxweight float64 `xorm:"maxweight"`
  56. Lweight float64 `xorm:"lweight"`
  57. Lweighthis float64 `xorm:"lweighthis"`
  58. Barid int64 `xorm:"barid"`
  59. Barname string `xorm:"barname"`
  60. Sort int64 `xorm:"sort"`
  61. Tmrid int64 `xorm:"tmrid"`
  62. Tmrname string `xorm:"tmrname"`
  63. Cowcount int64 `xorm:"cowcount"`
  64. Ccountradio float64 `xorm:"ccountradio"`
  65. Background string `xorm:"background"`
  66. Times int64 `xorm:"times"`
  67. Ftid int64 `xorm:"ftid"`
  68. }
  69. type fpdetailQueryInfo struct {
  70. Fpdid int64 `xorm:"fpdid"`
  71. Ptsrate float64 `xorm:"ptsrate"`
  72. Ptsuse float64 `xorm:"ptsuse"`
  73. Ptuse float64 `xorm:"ptuse"`
  74. Weight float64 `xorm:"weight"`
  75. Barid int64 `xorm:"barid"`
  76. Barname string `xorm:"barname"`
  77. Cowcount int64 `xorm:"cowcount"`
  78. Ccountradio float64 `xorm:"ccountradio"`
  79. Fttypeid int64 `xorm:"fttypeid"`
  80. Times int64 `xorm:"times"`
  81. Ptid int64 `xorm:"ptid"`
  82. Background string `xorm:"background"`
  83. }
  84. type lpplandtl1 struct {
  85. Id int64 `xorm:"id"`
  86. Pastureid int64 `xorm:"pastureid"`
  87. Lppid int64 `xorm:"lppid"`
  88. Barid int64 `xorm:"barid"`
  89. Barname string `xorm:"barname"`
  90. Fpdid int64 `xorm:"fpdid"`
  91. Fttype int64 `xorm:"fttype"`
  92. Lweight float64 `xorm:"lweight"`
  93. Sort int64 `xorm:"sort"`
  94. Tmrid int64 `xorm:"tmrid"`
  95. Tmrname string `xorm:"tmrname"`
  96. Cowcount int64 `xorm:"cowcount"`
  97. Ccountradio float64 `xorm:"ccountradio"`
  98. Background string `xorm:"background"`
  99. Lweighthis float64 `xorm:"lweighthis"`
  100. }
  101. //撒料计划 自动生成
  102. func Autogeneration(c *gin.Context) {
  103. appG := app.Gin{C: c}
  104. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  105. fsion := gofasion.NewFasion(string(dataByte))
  106. pastureid := fsion.Get("pastureid").ValueStr()
  107. typeIN := fsion.Get("type").ValueInt64()
  108. log.Println(time.Now())
  109. tx := restful.Engine.NewSession()
  110. defer tx.Close()
  111. var times int
  112. var wg sync.WaitGroup
  113. var tmrlist *[]*tmrStruct
  114. var feedtempletlist *[]*feedtemplet
  115. var err error
  116. var lpplanlist *[]*lpplanList
  117. var fpdetailWeightList *[]*fpdetailWeight
  118. wg.Add(1)
  119. go func() {
  120. defer wg.Done()
  121. feedtempletlist, err = getfeedtemplet(pastureid, tx)
  122. if err != nil {
  123. appG.Response(http.StatusOK, e.ERROR, false)
  124. return
  125. }
  126. }()
  127. wg.Wait()
  128. wg.Add(1)
  129. go func() {
  130. defer wg.Done()
  131. tmrlist, err = gettmr(pastureid, tx)
  132. if err != nil {
  133. appG.Response(http.StatusOK, e.ERROR, false)
  134. return
  135. }
  136. }()
  137. wg.Wait()
  138. wg.Add(1)
  139. go func() {
  140. defer wg.Done()
  141. times, err = gettimes(pastureid, tx)
  142. if err != nil {
  143. appG.Response(http.StatusOK, e.ERROR, false)
  144. return
  145. }
  146. }()
  147. wg.Wait()
  148. wg.Add(1)
  149. go func() {
  150. defer wg.Done()
  151. lpplanlist, err = getLpplanlist(pastureid, tx)
  152. if err != nil {
  153. appG.Response(http.StatusOK, e.ERROR, false)
  154. return
  155. }
  156. }()
  157. wg.Wait()
  158. wg.Add(1)
  159. go func() {
  160. defer wg.Done()
  161. fpdetailWeightList, err = getFpdetail(pastureid, tx)
  162. if err != nil {
  163. appG.Response(http.StatusOK, e.ERROR, false)
  164. return
  165. }
  166. for _, fpd := range *fpdetailWeightList {
  167. for _, lpp := range *lpplanlist {
  168. if (lpp.Ftid == fpd.Ptid || lpp.Ftid == fpd.Ptsid) && lpp.Times == fpd.Times {
  169. fpd.Weight = fpd.Weight - (lpp.Maxweight - lpp.Lweight)
  170. }
  171. }
  172. }
  173. }()
  174. wg.Wait()
  175. // 避免车次数量为0造成死循环
  176. max := false
  177. for _, tmr := range *tmrlist {
  178. if tmr.Maxstirfeed > 0 {
  179. max = true
  180. break
  181. }
  182. }
  183. for i := 1; i <= times; i++ {
  184. count, err := getLpplanCount(tx, i, pastureid)
  185. if err != nil {
  186. appG.Response(http.StatusOK, e.ERROR, false)
  187. return
  188. }
  189. for _, tem := range *feedtempletlist {
  190. for _, fpd := range *fpdetailWeightList {
  191. if tem.Id == fpd.Temid && i == int(fpd.Times) {
  192. var ftid int64
  193. if fpd.Ptid != 0 {
  194. ftid = fpd.Ptid
  195. } else {
  196. ftid = fpd.Ptsid
  197. }
  198. for fpd.Weight > 0 && max {
  199. for _, tmr := range *tmrlist {
  200. if tmr.Maxstirfeed > 0 {
  201. count++
  202. ids, err := setting.SnowIds.NextId()
  203. if err != nil {
  204. ids = time.Now().UnixNano()
  205. logging.Info("create SnowIds err", err)
  206. }
  207. display := tem.Cname
  208. if i == 1 {
  209. display += "第一班"
  210. } else if i == 2 {
  211. display += "第二班"
  212. } else if i == 3 {
  213. display += "第三班"
  214. } else if i == 4 {
  215. display += "第四班"
  216. }
  217. lpplan := &lpplanStruct{
  218. Id: ids,
  219. Tmrname: tmr.Tname,
  220. Tmrid: tmr.Id,
  221. Sort: count,
  222. Sel: 1,
  223. Times: int64(i),
  224. Ftname: tem.Cname,
  225. Ftid: ftid,
  226. Maxweight: tmr.Maxstirfeed,
  227. Sumweight: 0,
  228. Sumcowcount: 0,
  229. Pastureid: pastureid,
  230. Display: display,
  231. }
  232. err = addLpplan(lpplan, tx)
  233. if err != nil {
  234. appG.Response(http.StatusOK, e.ERROR, false)
  235. return
  236. }
  237. fpd.Weight = fpd.Weight - tmr.Maxstirfeed
  238. }
  239. if fpd.Weight < 0 {
  240. break
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. // wg.Wait()
  249. err = autogeneration(typeIN, pastureid, tx)
  250. if err != nil {
  251. appG.Response(http.StatusOK, e.ERROR, false)
  252. return
  253. }
  254. appG.Response(http.StatusOK, e.SUCCESS, true)
  255. }
  256. //查看配方模板
  257. func getfeedtemplet(pastureid string, tx *xorm.Session) (*[]*feedtemplet, error) {
  258. data := make([]*feedtemplet, 0)
  259. err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
  260. if err != nil {
  261. log.Println("getfeedtemplet-error", err)
  262. return nil, err
  263. }
  264. return &data, nil
  265. }
  266. //查看tmr设备
  267. func gettmr(pastureid string, tx *xorm.Session) (*[]*tmrStruct, error) {
  268. data := make([]*tmrStruct, 0)
  269. session := tx.Table("tmr")
  270. session.Select("TRIM(id) id,tname,maxstirfeed")
  271. session.Where("pastureid = ? ", pastureid)
  272. session.Where("enable = 1")
  273. session.In("tclassid", "1,2,3")
  274. err := session.Find(&data)
  275. if err != nil {
  276. log.Println("gettmr-error", err)
  277. return nil, err
  278. }
  279. return &data, nil
  280. }
  281. //判断计划种是否有该车次
  282. func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
  283. session := tx.Table("lpplan")
  284. session.Where("pastureid = ? ", pastureid)
  285. session.Where("times = ? ", times)
  286. session.Where("tmrid = ? ", tmrid)
  287. session.Where("ftid = ? ", ftid)
  288. exist, err := session.Exist()
  289. if err != nil {
  290. log.Println("getLpplanExist-error", err)
  291. return false, err
  292. }
  293. return exist, nil
  294. }
  295. type fpdetailWeight struct {
  296. Times int64 `xorm:"times"`
  297. Weight float64 `xorm:"weight"`
  298. Pastureid int64 `xorm:"pastureid"`
  299. Ptid int64 `xorm:"ptid"`
  300. Ptsid int64 `xorm:"ptsid"`
  301. Cowcount int64 `xorm:"cowcount"`
  302. Temid int64 `xorm:"Temid"`
  303. }
  304. //获取料数量
  305. func getFpdetail(pastureid string, tx *xorm.Session) (*[]*fpdetailWeight, error) {
  306. fpdetaillist := make([]*fpdetailWeight, 0)
  307. 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
  308. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  309. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  310. WHERE fpd.pastureid = ? AND fpd.ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptid > 0
  311. group by fpd.id,fpd.times) t1
  312. 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 ,
  313. 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 ,
  314. cowcount FROM fpdetail fpd
  315. join feedp fp on fp.ptsfid = fpd.ptsid and fp.pastureid = fpd.pastureid
  316. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  317. WHERE fpd.pastureid = ? AND fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2) and fpd.ptsid > 0
  318. group by fpd.id,fpd.times) t1
  319. group by t1.temid,t1.times) t2 on t2.temid = t1.temid and t2.times = t1.times
  320. group by t1.temid,t1.times
  321. `, pastureid, pastureid).Find(&fpdetaillist)
  322. if err != nil {
  323. log.Println("getFpdetail-error", err)
  324. tx.Rollback()
  325. return nil, err
  326. }
  327. return &fpdetaillist, nil
  328. }
  329. type lpplanList struct {
  330. Tmrid int64 `xorm:"tmrid"`
  331. Times int64 `xorm:"times"`
  332. Ftid int64 `xorm:"ftid"`
  333. Maxweight float64 `xorm:"maxweight"`
  334. Lweight float64 `xorm:"lweight"`
  335. }
  336. func getLpplanlist(patsureid string, tx *xorm.Session) (*[]*lpplanList, error) {
  337. session := tx.SQL(` select t1.times,t1.tmrid,t1.ftid,t1.maxweight ,ifnull(t2.lweight,0)as lweight from lpplan t1
  338. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  339. where t1.pastureid = ? `, patsureid, patsureid)
  340. data := make([]*lpplanList, 0)
  341. err := session.Find(&data)
  342. if err != nil {
  343. log.Println("getLpplanlist-error", err)
  344. return nil, err
  345. }
  346. return &data, nil
  347. }
  348. // 添加车次计划
  349. func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
  350. _, err := tx.Table("lpplan").Insert(lpplan)
  351. if err != nil {
  352. log.Println("addLpplan-error", err)
  353. return err
  354. }
  355. return nil
  356. }
  357. //获取车次数量
  358. func getLpplanCount(tx *xorm.Session, times int, pastureid string) (int64, error) {
  359. count, err := tx.Table("lpplan").Where("times = ?", times).And("pastureid = ?", pastureid).Count()
  360. if err != nil {
  361. log.Println("getLpplanCount-error", err)
  362. return 0, err
  363. }
  364. return count, err
  365. }
  366. //获取班次信息
  367. func gettimes(pastureid string, tx *xorm.Session) (int, error) {
  368. session := tx.Table("sysopt")
  369. session.Where("pastureid = ? ", pastureid)
  370. session.Where("inforname = 'times' ")
  371. data := new(sysopt)
  372. _, err := session.Get(data)
  373. if err != nil {
  374. log.Println("gettimes-error", err)
  375. return 0, err
  376. }
  377. return data.Inforvalue, nil
  378. }
  379. type upfpdetail struct {
  380. insertlweight float64
  381. fttypeid int64
  382. fpdid int64
  383. }
  384. func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
  385. tx.Begin()
  386. if typeIN == 1 {
  387. _, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
  388. if err != nil {
  389. log.Println("autogeneration-error-1", err)
  390. tx.Rollback()
  391. return err
  392. }
  393. _, err = tx.SQL(`update fpdetail
  394. set ptuse = 0,ptsuse=0
  395. WHERE pastureid = ?`, pastureid).Execute()
  396. if err != nil {
  397. log.Println("autogeneration-error-2", err)
  398. tx.Rollback()
  399. return err
  400. }
  401. }
  402. lpplansession := tx.SQL(`select
  403. t1.sort,t1.tmrid,t1.tmrname,t1.ftid,
  404. t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight,t1.times from lpplan t1
  405. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  406. where t1.pastureid = ? and t1.maxweight > ifnull(t2.lweight,0) `, pastureid, pastureid)
  407. lpplanlist := make([]*lpplanQueryInfo, 0)
  408. err := lpplansession.Find(&lpplanlist)
  409. if err != nil {
  410. log.Println("autogeneration-error-3", err)
  411. tx.Rollback()
  412. return err
  413. }
  414. fpdetaillist := make([]*fpdetailQueryInfo, 0)
  415. 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 ,
  416. (select bname from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,cowcount,ccountradio,0 isfill FROM fpdetail fpd
  417. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  418. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  419. WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2)
  420. group by fpd.id , fpd.times `, pastureid).Find(&fpdetaillist)
  421. if err != nil {
  422. log.Println("autogeneration-error-4", err)
  423. tx.Rollback()
  424. return err
  425. }
  426. log.Println(time.Now(), "333333333333")
  427. var wg sync.WaitGroup
  428. for _, lpplan := range lpplanlist {
  429. lpplandtl1List := make([]*lpplandtl1, 0)
  430. upfpdetailList := make([]*upfpdetail, 0)
  431. for _, fpdetail := range fpdetaillist {
  432. if fpdetail.Ptid == lpplan.Ftid && fpdetail.Times == lpplan.Times {
  433. var weight float64
  434. weight = fpdetail.Weight
  435. if lpplan.Maxweight <= lpplan.Lweight || weight <= 0 {
  436. continue
  437. }
  438. var insertlweight float64
  439. if lpplan.Maxweight-lpplan.Lweight > weight {
  440. insertlweight = weight
  441. } else {
  442. insertlweight = lpplan.Maxweight - lpplan.Lweight
  443. }
  444. lpplan.Lweight += insertlweight
  445. lpplan.Lweighthis = insertlweight
  446. exist := false
  447. for _, up := range upfpdetailList {
  448. if up.fpdid == fpdetail.Fpdid {
  449. up.insertlweight += insertlweight
  450. exist = true
  451. break
  452. }
  453. }
  454. if !exist {
  455. upfpdetailList = append(upfpdetailList, &upfpdetail{
  456. fpdid: fpdetail.Fpdid,
  457. insertlweight: insertlweight,
  458. fttypeid: fpdetail.Fttypeid,
  459. })
  460. }
  461. fpdetail.Weight = fpdetail.Weight - insertlweight
  462. exist = false
  463. for _, lpplandtl1 := range lpplandtl1List {
  464. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  465. lpplandtl1.Lweight += insertlweight
  466. lpplandtl1.Lweighthis = insertlweight
  467. exist = true
  468. break
  469. }
  470. }
  471. if !exist {
  472. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  473. Pastureid: lpplan.Pastureid,
  474. Lppid: lpplan.Lppid,
  475. Barid: fpdetail.Barid,
  476. Barname: fpdetail.Barname,
  477. Fpdid: fpdetail.Fpdid,
  478. Fttype: fpdetail.Fttypeid,
  479. Lweight: insertlweight,
  480. Sort: lpplan.Sort,
  481. Tmrid: lpplan.Tmrid,
  482. Tmrname: lpplan.Tmrname,
  483. Cowcount: lpplan.Cowcount,
  484. Ccountradio: lpplan.Ccountradio,
  485. Background: fpdetail.Background,
  486. Lweighthis: insertlweight,
  487. })
  488. }
  489. }
  490. }
  491. for _, lpplpplandtl1 := range lpplandtl1List {
  492. wg.Add(1)
  493. go func(lpplpplandtl1 *lpplandtl1) {
  494. defer wg.Done()
  495. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  496. cowcount,ccountradio,background,lweighthis)
  497. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  498. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  499. lpplpplandtl1.Lppid,
  500. lpplpplandtl1.Barid,
  501. lpplpplandtl1.Barname,
  502. lpplpplandtl1.Fpdid,
  503. lpplpplandtl1.Fttype,
  504. lpplpplandtl1.Lweight,
  505. lpplpplandtl1.Sort,
  506. lpplpplandtl1.Tmrid,
  507. lpplpplandtl1.Tmrname,
  508. lpplpplandtl1.Cowcount,
  509. lpplpplandtl1.Ccountradio,
  510. lpplpplandtl1.Background,
  511. lpplpplandtl1.Lweighthis).Execute()
  512. if err != nil {
  513. log.Println("autogeneration-error-6", err)
  514. tx.Rollback()
  515. return
  516. }
  517. }(lpplpplandtl1)
  518. }
  519. wg.Wait()
  520. for _, up := range upfpdetailList {
  521. wg.Add(1)
  522. go func(up *upfpdetail) {
  523. defer wg.Done()
  524. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  525. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  526. if err != nil {
  527. log.Println("autogeneration-error-5", err)
  528. tx.Rollback()
  529. return
  530. }
  531. }(up)
  532. }
  533. wg.Wait()
  534. }
  535. if typeIN == 1 {
  536. _, err = tx.SQL("update lpplan set sel = 1 where pastureid = ?", pastureid).Execute()
  537. if err != nil {
  538. log.Println("autogeneration-error-10", err)
  539. return err
  540. }
  541. }
  542. supplement := tx.Table("fpdetail").Alias("fpd")
  543. 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")
  544. supplement.Join("inner", []string{"feedp", "fp"}, "fp.ptsfid = fpd.ptsid").And("fp.pastureid = fpd.pastureid")
  545. supplement.Join("inner", []string{"feedtemplet", "ft"}, "ft.id = fp.ftid").And("ft.pastureid = fpd.pastureid")
  546. supplement.Where("fpd.pastureid = ?", pastureid).And("fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2)").And("fpd.ptsid > 0")
  547. supplement.GroupBy("fpd.id,fpd.times")
  548. supplementList := make([]*fpdetailQueryInfo, 0)
  549. err = supplement.Find(&supplementList)
  550. if err != nil {
  551. log.Println("autogeneration-error-11", err)
  552. return err
  553. }
  554. if len(supplementList) > 0 {
  555. for _, lpplan := range lpplanlist {
  556. if lpplan.Maxweight <= lpplan.Lweight {
  557. continue
  558. }
  559. lppids := []string{strconv.FormatInt(lpplan.Lppid, 10)}
  560. for _, lpp := range lpplanlist {
  561. if lpp.Ftid == lpplan.Ftid && lpp.Times == lpplan.Times {
  562. lppids = append(lppids, strconv.FormatInt(lpp.Lppid, 10))
  563. }
  564. }
  565. lpplandtl1List := make([]*lpplandtl1, 0)
  566. upfpdetailList := make([]*upfpdetail, 0)
  567. lpplandtllist := make([]*lpplandtl1, 0)
  568. err = tx.Table("lpplandtl1").Select("barid").Where(fmt.Sprintf("lppid in (%s)", strings.Join(lppids, ","))).And("").Find(&lpplandtllist)
  569. if err != nil {
  570. log.Println("autogeneration-error-11", err)
  571. return err
  572. }
  573. for _, fpdetail := range supplementList {
  574. if lpplan.Times != fpdetail.Times {
  575. continue
  576. }
  577. exist := false
  578. for _, bar := range lpplandtllist {
  579. if bar.Barid == fpdetail.Barid {
  580. exist = true
  581. break
  582. }
  583. }
  584. if !exist {
  585. continue
  586. }
  587. var weight float64
  588. weight = fpdetail.Weight
  589. if weight <= 0 {
  590. continue
  591. }
  592. var insertlweight float64
  593. if lpplan.Maxweight-lpplan.Lweight > weight {
  594. insertlweight = weight
  595. } else {
  596. insertlweight = lpplan.Maxweight - lpplan.Lweight
  597. }
  598. lpplan.Lweight += insertlweight
  599. lpplan.Lweighthis = insertlweight
  600. exist = false
  601. for _, up := range upfpdetailList {
  602. if up.fpdid == fpdetail.Fpdid {
  603. up.insertlweight += insertlweight
  604. exist = true
  605. break
  606. }
  607. }
  608. if !exist {
  609. upfpdetailList = append(upfpdetailList, &upfpdetail{
  610. fpdid: fpdetail.Fpdid,
  611. insertlweight: insertlweight,
  612. fttypeid: 0,
  613. })
  614. }
  615. fpdetail.Weight = fpdetail.Weight - insertlweight
  616. exist = false
  617. for _, lpplandtl1 := range lpplandtl1List {
  618. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  619. lpplandtl1.Lweight += insertlweight
  620. lpplandtl1.Lweighthis = insertlweight
  621. exist = true
  622. break
  623. }
  624. }
  625. if !exist {
  626. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  627. Pastureid: lpplan.Pastureid,
  628. Lppid: lpplan.Lppid,
  629. Barid: fpdetail.Barid,
  630. Barname: fpdetail.Barname,
  631. Fpdid: fpdetail.Fpdid,
  632. Fttype: 0,
  633. Lweight: insertlweight,
  634. Sort: lpplan.Sort,
  635. Tmrid: lpplan.Tmrid,
  636. Tmrname: lpplan.Tmrname,
  637. Cowcount: lpplan.Cowcount,
  638. Ccountradio: lpplan.Ccountradio,
  639. Background: fpdetail.Background,
  640. Lweighthis: insertlweight,
  641. })
  642. }
  643. }
  644. for _, lpplpplandtl1 := range lpplandtl1List {
  645. wg.Add(1)
  646. go func(lpplpplandtl1 *lpplandtl1) {
  647. defer wg.Done()
  648. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  649. cowcount,ccountradio,background,lweighthis)
  650. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  651. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  652. lpplpplandtl1.Lppid,
  653. lpplpplandtl1.Barid,
  654. lpplpplandtl1.Barname,
  655. lpplpplandtl1.Fpdid,
  656. lpplpplandtl1.Fttype,
  657. lpplpplandtl1.Lweight,
  658. lpplpplandtl1.Sort,
  659. lpplpplandtl1.Tmrid,
  660. lpplpplandtl1.Tmrname,
  661. lpplpplandtl1.Cowcount,
  662. lpplpplandtl1.Ccountradio,
  663. lpplpplandtl1.Background,
  664. lpplpplandtl1.Lweighthis).Execute()
  665. if err != nil {
  666. log.Println("autogeneration-error-6", err)
  667. tx.Rollback()
  668. return
  669. }
  670. }(lpplpplandtl1)
  671. }
  672. wg.Wait()
  673. for _, up := range upfpdetailList {
  674. wg.Add(1)
  675. go func(up *upfpdetail) {
  676. defer wg.Done()
  677. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  678. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  679. if err != nil {
  680. log.Println("autogeneration-error-5", err)
  681. tx.Rollback()
  682. // return err
  683. return
  684. }
  685. }(up)
  686. }
  687. wg.Wait()
  688. }
  689. }
  690. err = tx.Commit()
  691. if err != nil {
  692. log.Println("autogeneration-error-11", err)
  693. return err
  694. }
  695. return nil
  696. }
  697. func GetSpillage(c *gin.Context) {
  698. appG := app.Gin{C: c}
  699. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  700. fsion := gofasion.NewFasion(string(dataByte))
  701. parammaps := fsion.Get("parammaps")
  702. pastureid := parammaps.Get("pastureid").ValueInt64()
  703. times := parammaps.Get("times").ValueInt64()
  704. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  705. // refresh 0 全部 , 1 车次信息, 2 班次
  706. refresh := parammaps.Get("refresh").ValueInt64()
  707. tx := restful.Engine.NewSession()
  708. defer tx.Close()
  709. data := make(map[string]interface{})
  710. if refresh == 0 || refresh == 2 {
  711. 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
  712. inner join feedtemplet ft
  713. on ft.id = feedp.ftid and ft.pastureid=?
  714. WHERE feedp.pastureid = ? %s
  715. GROUP BY feedp.ftid
  716. UNION
  717. 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
  718. inner join feedtemplet ft
  719. on ft.id = feedp.ptsfid and ft.pastureid=?
  720. WHERE feedp.pastureid = ? %s and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  721. GROUP BY feedp.ptsfid`
  722. // SELECT TRIM(id) id,times,tratio,ROUND(ptsrate*weight,2)-ptsuse weight,TRIM(barid) barid,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
  723. // (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,
  724. // ? AS background,? AS fttype,cowcount,ccountradio, 1 isfill FROM fpdetaildate fpdetail
  725. // WHERE fpdetail.pastureid = ? AND ( fpdetail.`ptsid`=?) AND times=? AND ptsuse< ROUND(ptsrate*weight,2) and fpdetail.date = ?
  726. // UNION
  727. // 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 ,
  728. // (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,
  729. // ? AS background,? AS fttype,cowcount,ccountradio, 0 isfill FROM fpdetaildate fpdetail
  730. // WHERE fpdetail.pastureid = ? AND (fpdetail.`ptid`=? ) AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2) and fpdetail.date = ?
  731. // ORDER BY barid
  732. if ftid != 0 {
  733. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  734. } else {
  735. sqlStr = fmt.Sprintf(sqlStr, "", "")
  736. }
  737. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  738. if err != nil {
  739. log.Println("GetSpillage-error-1: ", err)
  740. appG.Response(http.StatusOK, e.ERROR, nil)
  741. return
  742. }
  743. 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 ,
  744. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  745. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  746. UNION
  747. 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 ,
  748. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  749. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  750. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  751. if err != nil {
  752. log.Println("GetSpillage-error-2: ", err)
  753. appG.Response(http.StatusOK, e.ERROR, nil)
  754. return
  755. }
  756. for _, ft := range ftList {
  757. arrList := make([]map[string]interface{}, 0)
  758. arrid := ft["arrid"].(string)
  759. for _, detail := range ftDetailList {
  760. if _, ok := detail["ptid"]; ok {
  761. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  762. detail["background"] = ft["background"]
  763. arrList = append(arrList, detail)
  764. }
  765. } else if _, ok := detail["ptsid"]; ok {
  766. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  767. detail["background"] = ft["background"]
  768. arrList = append(arrList, detail)
  769. }
  770. }
  771. }
  772. if len(arrList) > 0 {
  773. ft["arrList"] = arrList
  774. }
  775. }
  776. data["ftlist"] = ftList
  777. }
  778. if refresh == 0 || refresh == 1 {
  779. sqlstr := `SELECT
  780. ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) tmrname,trim(tmrid) tmrid,sort,sel,
  781. CASE times
  782. WHEN 1 THEN
  783. '第一班'
  784. WHEN 2 THEN
  785. '第二班'
  786. WHEN 3 THEN
  787. '第三班'
  788. WHEN 4 THEN
  789. '第四班'
  790. END timesstr,issplit,
  791. times,display,begintime,
  792. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  793. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1
  794. inner join fpdetail
  795. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  796. WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.pastureid =lpplan.pastureid ),2),0) sumweight ,
  797. ifnull((select maxstirfeed from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),100000) maxweight,
  798. TRIM(ftid) ftid,
  799. TRIM(id) id,
  800. TRIM(id) lppid,
  801. TRIM(pastureid) pastureid
  802. FROM
  803. lpplan
  804. WHERE pastureid = ? and times <= (SELECT sysopt.inforvalue FROM sysopt
  805. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  806. `
  807. arrsql := `SELECT
  808. trim(lpplandtl1.lppid) lppid,lweight,
  809. lpplandtl1.lweight weight,lpplandtl1.sort,
  810. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  811. TRIM(lpplandtl1.fpdid) fpdid,
  812. TRIM(lpplandtl1.barid) barid,
  813. TRIM(lpplandtl1.id) id,
  814. TRIM(lpplandtl1.pastureid) pastureid,
  815. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  816. lpplandtl1.fttype,
  817. trim(lpplandtl1.tmrid) tmrid,
  818. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  819. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  820. fpdetail.ptid,
  821. fpdetail.ptsid,
  822. fpdetail.times,
  823. fpdetail.cowcount,
  824. fpdetail.ccountradio
  825. FROM
  826. lpplandtl1
  827. inner join fpdetail
  828. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  829. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0
  830. `
  831. var args []interface{}
  832. args = append(args, pastureid)
  833. if times != 0 {
  834. sqlstr += " and times = ? "
  835. arrsql += " and times = ? "
  836. args = append(args, times)
  837. }
  838. sqlstr += "ORDER BY times,lpplan.sort"
  839. session := tx.SQL(sqlstr, args...)
  840. lppList, err := session.Query().List()
  841. if err != nil {
  842. log.Println("GetSpillage-error-3: ", err)
  843. appG.Response(http.StatusOK, e.ERROR, nil)
  844. return
  845. }
  846. arrsql += " ORDER BY lpplandtl1.sort"
  847. arrList, err := tx.SQL(arrsql, args...).QueryString() //获取
  848. if err != nil {
  849. log.Println("GetSpillage-error-4: ", err)
  850. appG.Response(http.StatusOK, e.ERROR, nil)
  851. return
  852. }
  853. for _, query := range lppList {
  854. list := make([]map[string]string, 0)
  855. for _, arr := range arrList {
  856. if query["lppid"].(string) == arr["lppid"] {
  857. list = append(list, arr)
  858. }
  859. }
  860. query["arrList"] = list
  861. }
  862. data["list"] = lppList
  863. data["total"] = len(lppList)
  864. }
  865. appG.Response(http.StatusOK, e.SUCCESS, data)
  866. }
  867. type lpplanTrains struct {
  868. Id string `xorm:"id"`
  869. Sort int64 `xorm:"sort"`
  870. Pastureid string `xorm:"pastureid"`
  871. }
  872. //修改车次顺序
  873. func UpdateTrains(c *gin.Context) {
  874. appG := app.Gin{C: c}
  875. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  876. lppList := make([]*lpplanTrains, 0)
  877. err := json.Unmarshal(dataByte, &lppList)
  878. if err != nil {
  879. log.Println("UpdateTrains-error-1: ", err)
  880. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  881. return
  882. }
  883. tx := restful.Engine.NewSession()
  884. defer tx.Close()
  885. tx.Begin()
  886. for _, item := range lppList {
  887. _, err = tx.Table("lpplan").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  888. if err != nil {
  889. log.Println("UpdateTrains-error-2: ", err)
  890. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  891. tx.Rollback()
  892. return
  893. }
  894. }
  895. err = tx.Commit()
  896. if err != nil {
  897. log.Println("UpdateTrains-error-2: ", err)
  898. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  899. tx.Rollback()
  900. return
  901. }
  902. appG.Response(http.StatusOK, e.SUCCESS, true)
  903. }
  904. func UpdateTrainsDay(c *gin.Context) {
  905. appG := app.Gin{C: c}
  906. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  907. lppList := make([]*lpplanTrains, 0)
  908. err := json.Unmarshal(dataByte, &lppList)
  909. if err != nil {
  910. log.Println("UpdateTrainsDay-error-1: ", err)
  911. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  912. return
  913. }
  914. tx := restful.Engine.NewSession()
  915. defer tx.Close()
  916. tx.Begin()
  917. for _, item := range lppList {
  918. _, err = tx.Table("lpplandate").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  919. if err != nil {
  920. log.Println("UpdateTrainsDay-error-2: ", err)
  921. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  922. tx.Rollback()
  923. return
  924. }
  925. }
  926. err = tx.Commit()
  927. if err != nil {
  928. log.Println("UpdateTrainsDay-error-3: ", err)
  929. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  930. tx.Rollback()
  931. return
  932. }
  933. appG.Response(http.StatusOK, e.SUCCESS, true)
  934. }
  935. type ReqAddLpplan struct {
  936. Id string `xorm:"id"`
  937. Tmrid string `xorm:"tmrid"`
  938. TmrName string `xorm:"tmrname"`
  939. Sort int64 `xorm:"sort"`
  940. Sel int64 `xorm:"sel"`
  941. Times int64 `xorm:"times"`
  942. Ftname string `xorm:"ftname"`
  943. Ftid string `xorm:"ftid"`
  944. Maxweight float64 `xorm:"maxweight"`
  945. // Sumweight int64 `xorm:"sumweight"`
  946. Sumcowcount int64 `xorm:"sumcowcount"`
  947. Pastureid string `xorm:"pastureid"`
  948. Display string `xorm:"display"`
  949. Issplit int64 `xorm:"issplit"`
  950. BeginTime string `xorm:"begintime"`
  951. }
  952. //添加车次并返回添加的车次信息
  953. func AddLpplan(c *gin.Context) {
  954. appG := app.Gin{C: c}
  955. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  956. fsion := gofasion.NewFasion(string(dataByte))
  957. parammaps := fsion.Get("parammaps")
  958. req := new(ReqAddLpplan)
  959. err := json.Unmarshal([]byte(parammaps.Json()), req)
  960. if err != nil {
  961. log.Println("AddLpplan-error-1: ", err)
  962. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  963. return
  964. }
  965. if req.Times == 0 {
  966. log.Println("AddLpplan-error-1: ", err)
  967. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  968. return
  969. }
  970. tx := restful.Engine.NewSession()
  971. defer tx.Close()
  972. ids, err := setting.SnowIds.NextId()
  973. if err != nil {
  974. ids = time.Now().UnixNano()
  975. logging.Info("AddLpplan-error-1: ", err)
  976. }
  977. req.Id = strconv.FormatInt(ids, 10)
  978. datacount, err := tx.Table("lpplan").Select("max(sort) as sort").Where("pastureid = ?", req.Pastureid).And(" times = ?", req.Times).QueryString()
  979. if err != nil {
  980. log.Println("AddLpplan-error-2: ", err)
  981. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  982. return
  983. }
  984. var count int64 = 0
  985. for _, d := range datacount {
  986. if _, ok := d["sort"]; ok {
  987. count, _ = strconv.ParseInt(d["sort"], 10, 64)
  988. }
  989. }
  990. req.Sort = count + 1
  991. _, err = tx.Table("lpplan").Insert(req)
  992. if err != nil {
  993. log.Println("AddLpplan-error-3: ", err)
  994. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  995. return
  996. }
  997. appG.Response(http.StatusOK, e.SUCCESS, req)
  998. }
  999. type ReqAddLpplanDay struct {
  1000. Id string `xorm:"id"`
  1001. Tmrid string `xorm:"tmrid"`
  1002. TmrName string `xorm:"tmrname"`
  1003. Sort int64 `xorm:"sort"`
  1004. Sel int64 `xorm:"sel"`
  1005. Times int64 `xorm:"times"`
  1006. Ftname string `xorm:"ftname"`
  1007. Ftid string `xorm:"ftid"`
  1008. Maxweight float64 `xorm:"maxweight"`
  1009. // Sumweight int64 `xorm:"sumweight"`
  1010. Sumcowcount int64 `xorm:"sumcowcount"`
  1011. Pastureid string `xorm:"pastureid"`
  1012. Display string `xorm:"display"`
  1013. Issplit int64 `xorm:"issplit"`
  1014. BeginTime string `xorm:"begintime"`
  1015. Date string `xorm:"date"`
  1016. }
  1017. func AddLpplanDay(c *gin.Context) {
  1018. appG := app.Gin{C: c}
  1019. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1020. fsion := gofasion.NewFasion(string(dataByte))
  1021. parammaps := fsion.Get("parammaps")
  1022. req := new(ReqAddLpplanDay)
  1023. err := json.Unmarshal([]byte(parammaps.Json()), req)
  1024. if err != nil {
  1025. log.Println("AddLpplanDay-error-1: ", err)
  1026. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1027. return
  1028. }
  1029. if req.Times == 0 {
  1030. log.Println("AddLpplanDay-error-1: ", err)
  1031. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  1032. return
  1033. }
  1034. tx := restful.Engine.NewSession()
  1035. defer tx.Close()
  1036. ids, err := setting.SnowIds.NextId()
  1037. if err != nil {
  1038. ids = time.Now().UnixNano()
  1039. logging.Info("AddLpplanDay-error-1: ", err)
  1040. }
  1041. req.Id = strconv.FormatInt(ids, 10)
  1042. count, err := tx.Table("lpplandate").Where("pastureid = ?", req.Pastureid).And(" date = ?", req.Date).And(" times = ?", req.Times).Count()
  1043. if err != nil {
  1044. log.Println("AddLpplanDay-error-2: ", err)
  1045. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1046. return
  1047. }
  1048. req.Sort = count + 1
  1049. _, err = tx.Table("lpplandate").Insert(req)
  1050. if err != nil {
  1051. log.Println("AddLpplanDay-error-3: ", err)
  1052. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1053. return
  1054. }
  1055. appG.Response(http.StatusOK, e.SUCCESS, req)
  1056. }
  1057. //修改车次信息,如果修改tmrid信息则删除栏舍信息
  1058. func UpdateLpplan(c *gin.Context) {
  1059. appG := app.Gin{C: c}
  1060. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1061. fsion := gofasion.NewFasion(string(dataByte))
  1062. parammaps := fsion.Get("parammaps")
  1063. lpplanid := parammaps.Get("id").ValueStr()
  1064. pastureid := parammaps.Get("pastureid").ValueStr()
  1065. tmrid := parammaps.Get("tmrid").ValueStr()
  1066. tmrname := parammaps.Get("tmrname").ValueStr()
  1067. begintime := parammaps.Get("begintime").ValueStr()
  1068. display := parammaps.Get("display").ValueStr()
  1069. times := parammaps.Get("times").ValueInt64()
  1070. ftid := parammaps.Get("ftid").ValueStr()
  1071. ftname := parammaps.Get("ftname").ValueStr()
  1072. sel := parammaps.Get("sel").ValueInt64()
  1073. issplit := parammaps.Get("issplit").ValueInt64()
  1074. lpplan := new(ReqAddLpplan)
  1075. lpplan.Tmrid = tmrid
  1076. lpplan.TmrName = tmrname
  1077. lpplan.BeginTime = begintime
  1078. lpplan.Sel = sel
  1079. lpplan.Issplit = issplit
  1080. lpplan.Times = times
  1081. lpplan.Ftid = ftid
  1082. lpplan.Ftname = ftname
  1083. lpplan.Display = display
  1084. tx := restful.Engine.NewSession()
  1085. defer tx.Close()
  1086. tx.Begin()
  1087. lpplanData := new(lpplanStruct)
  1088. _, err := tx.Table("lpplan").Select("tmrid").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).Get(lpplanData)
  1089. if err != nil {
  1090. log.Println("UpdateLpplan-error-1: ", err)
  1091. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1092. tx.Rollback()
  1093. return
  1094. }
  1095. _, err = tx.Table("lpplan").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).
  1096. MustCols("display,tmrid,tmrname,begintime,sel,issplit,times,ftid,ftname").Update(lpplan)
  1097. if err != nil {
  1098. log.Println("UpdateLpplan-error-1: ", err)
  1099. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1100. tx.Rollback()
  1101. return
  1102. }
  1103. // if tmrid != lpplan.Tmrid {
  1104. // _, err = tx.SQL(`delete from lpplandtl1 where lppid = ? and pastureid = ? `, lpplanid, pastureid).Execute()
  1105. // if err != nil {
  1106. // log.Println("UpdateLpplan-error-2: ", err)
  1107. // appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1108. // tx.Rollback()
  1109. // return
  1110. // }
  1111. // }
  1112. err = tx.Commit()
  1113. if err != nil {
  1114. log.Println("UpdateLpplan-error-3: ", err)
  1115. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1116. tx.Rollback()
  1117. return
  1118. }
  1119. appG.Response(http.StatusOK, e.SUCCESS, true)
  1120. }
  1121. func GetSpillageDay(c *gin.Context) {
  1122. appG := app.Gin{C: c}
  1123. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1124. fsion := gofasion.NewFasion(string(dataByte))
  1125. parammaps := fsion.Get("parammaps")
  1126. pastureid := parammaps.Get("pastureid").ValueInt64()
  1127. times := parammaps.Get("times").ValueInt64()
  1128. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  1129. // refresh 0 全部 , 1 车次信息, 2 班次
  1130. refresh := parammaps.Get("refresh").ValueInt64()
  1131. date := parammaps.Get("date").ValueStr()
  1132. tx := restful.Engine.NewSession()
  1133. defer tx.Close()
  1134. data := make(map[string]interface{})
  1135. if refresh == 0 || refresh == 2 {
  1136. // 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
  1137. // inner join feedtemplet ft
  1138. // on ft.id = feedp.ftid and ft.pastureid=?
  1139. // WHERE feedp.pastureid = ? %s
  1140. // GROUP BY feedp.ftid
  1141. // UNION
  1142. // 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
  1143. // inner join feedtemplet ft
  1144. // on ft.id = feedp.ptsfid and ft.pastureid=?
  1145. // WHERE feedp.pastureid = ? %s and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1146. // GROUP BY feedp.ptsfid`
  1147. 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
  1148. FROM feedpdate feedp
  1149. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1150. INNER JOIN feedtemplet ft
  1151. ON ft.id = fpd.ptid AND ft.pastureid= feedp.pastureid
  1152. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1153. GROUP BY fpd.ptid
  1154. UNION
  1155. 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
  1156. FROM feedpdate feedp
  1157. INNER JOIN fpdetaildate fpd ON fpd.date = feedp.date AND fpd.barid = feedp.barid AND fpd.pastureid = feedp.pastureid
  1158. INNER JOIN feedtemplet ft
  1159. ON ft.id = fpd.ptsid AND ft.pastureid=feedp.pastureid
  1160. WHERE feedp.pastureid = ? AND feedp.date=? %s
  1161. AND (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1162. GROUP BY fpd.ptsid`
  1163. if ftid != 0 {
  1164. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  1165. } else {
  1166. sqlStr = fmt.Sprintf(sqlStr, "", "")
  1167. }
  1168. ftList, err := tx.SQL(sqlStr, times, pastureid, date, times, pastureid, date).Query().List()
  1169. if err != nil {
  1170. log.Println("GetSpillage-error-1: ", err)
  1171. appG.Response(http.StatusOK, e.ERROR, nil)
  1172. return
  1173. }
  1174. // AND ( fpdetail.ptsid=?)
  1175. // AND (fpdetail.ptid=? )
  1176. 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 ,
  1177. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 1 isfill FROM fpdetaildate fpdetail
  1178. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2) and fpdetail.date = ?
  1179. UNION
  1180. 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 ,
  1181. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,cowcount,ccountradio, 0 isfill FROM fpdetaildate fpdetail
  1182. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2) and fpdetail.date = ?
  1183. ORDER BY barid`, pastureid, times, date, pastureid, times, date).Query().List()
  1184. if err != nil {
  1185. log.Println("GetSpillage-error-2: ", err)
  1186. appG.Response(http.StatusOK, e.ERROR, nil)
  1187. return
  1188. }
  1189. for _, ft := range ftList {
  1190. arrList := make([]map[string]interface{}, 0)
  1191. arrid := ft["arrid"].(string)
  1192. for _, detail := range ftDetailList {
  1193. if _, ok := detail["ptid"]; ok {
  1194. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1195. detail["background"] = ft["background"]
  1196. detail["fttype"] = ft["type"].(int64)
  1197. arrList = append(arrList, detail)
  1198. }
  1199. } else if _, ok := detail["ptsid"]; ok {
  1200. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1201. detail["background"] = ft["background"]
  1202. detail["fttype"] = ft["type"].(int64)
  1203. arrList = append(arrList, detail)
  1204. }
  1205. }
  1206. }
  1207. if len(arrList) > 0 {
  1208. ft["arrList"] = arrList
  1209. }
  1210. }
  1211. data["ftlist"] = ftList
  1212. }
  1213. if refresh == 0 || refresh == 1 {
  1214. sqlstr := `SELECT
  1215. lpplan.date,
  1216. -- ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname)
  1217. lpplan.tmrname tmrname,trim(lpplan.tmrid) tmrid,lpplan.sort,lpplan.sel,
  1218. CASE lpplan.times
  1219. WHEN 1 THEN
  1220. '第一班'
  1221. WHEN 2 THEN
  1222. '第二班'
  1223. WHEN 3 THEN
  1224. '第三班'
  1225. WHEN 4 THEN
  1226. '第四班'
  1227. END timesstr,issplit,
  1228. lpplan.times,display,begintime,
  1229. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  1230. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1date lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id and lpplandtl1.date= ?),2),0) sumweight ,
  1231. maxweight,
  1232. TRIM(ftid) ftid,
  1233. TRIM(lpplan.id) id,
  1234. TRIM(lpplan.id) lppid,
  1235. TRIM(lpplan.pastureid) pastureid,
  1236. ifnull(dpl.havebutton,0) as havebutton
  1237. FROM
  1238. lpplandate lpplan
  1239. left join downloadedplan dpl on dpl.pid = lpplan.id and DATE_FORMAT(dpl.mydate,'%Y-%m-%e') = lpplan.date
  1240. WHERE lpplan.pastureid = ? and lpplan.date = ? and lpplan.times <= (SELECT sysopt.inforvalue FROM sysopt
  1241. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  1242. `
  1243. arrsql := `SELECT
  1244. trim(lpplandtl1.lppid) lppid,lweight,
  1245. lpplandtl1.lweight weight,lpplandtl1.sort,
  1246. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  1247. TRIM(lpplandtl1.fpdid) fpdid,
  1248. TRIM(lpplandtl1.barid) barid,
  1249. TRIM(lpplandtl1.id) id,
  1250. TRIM(lpplandtl1.pastureid) pastureid,
  1251. lpplandtl1.background,
  1252. lpplandtl1.fttype,
  1253. trim(lpplandtl1.tmrid) tmrid,
  1254. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  1255. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  1256. fpdetail.ptid,
  1257. fpdetail.ptsid,
  1258. fpdetail.times,
  1259. fpdetail.cowcount,
  1260. fpdetail.ccountradio
  1261. FROM
  1262. lpplandtl1date lpplandtl1
  1263. inner join fpdetaildate fpdetail
  1264. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid and lpplandtl1.date = fpdetail.date
  1265. WHERE lpplandtl1.pastureid = ? and lpplandtl1.date = ? and lpplandtl1.lweight>0 `
  1266. var args []interface{}
  1267. args = append(args, date, pastureid, date)
  1268. if times != 0 {
  1269. sqlstr += " and lpplan.times = ? "
  1270. // arrsql += " and times = ? "
  1271. args = append(args, times)
  1272. }
  1273. sqlstr += " group by lpplan.id ORDER BY times,lpplan.sort"
  1274. session := tx.SQL(sqlstr, args...)
  1275. lppList, err := session.Query().List()
  1276. if err != nil {
  1277. log.Println("GetSpillage-error-3: ", err)
  1278. appG.Response(http.StatusOK, e.ERROR, nil)
  1279. return
  1280. }
  1281. arrsql += " ORDER BY lpplandtl1.sort"
  1282. arrList, err := tx.SQL(arrsql, pastureid, date).QueryString() //获取
  1283. if err != nil {
  1284. log.Println("GetSpillage-error-4: ", err)
  1285. appG.Response(http.StatusOK, e.ERROR, nil)
  1286. return
  1287. }
  1288. for _, query := range lppList {
  1289. list := make([]map[string]string, 0)
  1290. for _, arr := range arrList {
  1291. if query["lppid"].(string) == arr["lppid"] {
  1292. list = append(list, arr)
  1293. }
  1294. }
  1295. query["arrList"] = list
  1296. }
  1297. data["list"] = lppList
  1298. data["total"] = len(lppList)
  1299. }
  1300. appG.Response(http.StatusOK, e.SUCCESS, data)
  1301. }
  1302. func AddLpplandtl(c *gin.Context) {
  1303. appG := app.Gin{C: c}
  1304. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1305. fsion := gofasion.NewFasion(string(dataByte))
  1306. parammaps := fsion.Get("parammaps")
  1307. tx := restful.Engine.NewSession()
  1308. defer tx.Close()
  1309. exist := false
  1310. var err error
  1311. lpplandList := make([]*lpplandtl1, 0)
  1312. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,barid,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1313. OrderBy("sort").Find(&lpplandList)
  1314. if err != nil {
  1315. log.Println("AddLpplandtl-error-1: ", err)
  1316. appG.Response(http.StatusOK, e.ERROR, err)
  1317. return
  1318. }
  1319. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1320. for _, lppand := range lpplandList {
  1321. if lppand.Id == parammaps.Get("id").ValueInt64() {
  1322. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1323. appG.Response(http.StatusOK, e.SUCCESS, true)
  1324. return
  1325. }
  1326. exist = true
  1327. break
  1328. }
  1329. }
  1330. }
  1331. tmrid := parammaps.Get("tmrid").ValueInt64()
  1332. var sort int64 = -1
  1333. if !exist {
  1334. for _, lppand := range lpplandList {
  1335. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1336. _, err := tx.SQL(" update lpplandtl1 set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1337. if err != nil {
  1338. log.Println("AddLpplandtl-error-2: ", err)
  1339. appG.Response(http.StatusOK, e.ERROR, err)
  1340. return
  1341. }
  1342. appG.Response(http.StatusOK, e.SUCCESS, true)
  1343. return
  1344. }
  1345. }
  1346. for i, lppand := range lpplandList {
  1347. lppand.Sort = int64(i) + 1
  1348. }
  1349. updateLpplandList := make([]*lpplandtl1, 0)
  1350. for i, lppand := range lpplandList {
  1351. n := i + 1
  1352. if lppand.Tmrid == tmrid {
  1353. sort = int64(n)
  1354. lppand.Sort = int64(n) + 1
  1355. updateLpplandList = append(updateLpplandList, lppand)
  1356. } else if sort > 0 {
  1357. lppand.Sort = int64(n) + 1
  1358. updateLpplandList = append(updateLpplandList, lppand)
  1359. }
  1360. }
  1361. tx.Begin()
  1362. for _, lppand := range updateLpplandList {
  1363. _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1364. if err != nil {
  1365. tx.Rollback()
  1366. log.Println("AddLpplandtl-error-2: ", err)
  1367. appG.Response(http.StatusOK, e.ERROR, err)
  1368. return
  1369. }
  1370. }
  1371. if sort < 0 && len(lpplandList) > 0 {
  1372. sort = int64(len(lpplandList) + 1)
  1373. } else if len(lpplandList) == 0 {
  1374. sort = 1
  1375. }
  1376. _, err = tx.SQL(`INSERT INTO lpplandtl1(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio)
  1377. VALUES (?,?,?,?,?,?,?,?,?,
  1378. ?,?,IF(?='',0,?),IF(?='',0,?))`,
  1379. parammaps.Get("pastureid").ValueStr(),
  1380. parammaps.Get("lppid").ValueStr(),
  1381. parammaps.Get("barid").ValueStr(),
  1382. parammaps.Get("barname").ValueStr(),
  1383. parammaps.Get("fpdid").ValueStr(),
  1384. parammaps.Get("lweight").ValueStr(),
  1385. sort,
  1386. parammaps.Get("tmrid").ValueStr(),
  1387. parammaps.Get("tmrname").ValueStr(),
  1388. parammaps.Get("fttype").ValueStr(),
  1389. parammaps.Get("background").ValueStr(),
  1390. parammaps.Get("cowcount").ValueStr(),
  1391. parammaps.Get("cowcount").ValueStr(),
  1392. parammaps.Get("ccountradio").ValueStr(),
  1393. parammaps.Get("ccountradio").ValueStr()).Execute()
  1394. if err != nil {
  1395. tx.Rollback()
  1396. log.Println("AddLpplandtl-error-3: ", err)
  1397. appG.Response(http.StatusOK, e.ERROR, err)
  1398. return
  1399. }
  1400. err = tx.Commit()
  1401. if err != nil {
  1402. tx.Rollback()
  1403. log.Println("AddLpplandtl-error-6: ", err)
  1404. appG.Response(http.StatusOK, e.ERROR, err)
  1405. return
  1406. }
  1407. } else {
  1408. lpplandid := parammaps.Get("id").ValueInt64()
  1409. updateLpplandList := make([]*lpplandtl1, 0)
  1410. var oldsort int64
  1411. for i, lppand := range lpplandList {
  1412. lppand.Sort = int64(i) + 1
  1413. }
  1414. for _, lppand := range lpplandList {
  1415. if lppand.Id == lpplandid {
  1416. oldsort = lppand.Sort
  1417. break
  1418. }
  1419. }
  1420. for _, lppand := range lpplandList {
  1421. if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  1422. sort = lppand.Sort
  1423. break
  1424. }
  1425. }
  1426. if sort == -1 {
  1427. sort = 1
  1428. }
  1429. if oldsort < sort {
  1430. for _, lppand := range lpplandList {
  1431. if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  1432. lppand.Sort--
  1433. updateLpplandList = append(updateLpplandList, lppand)
  1434. } else if lppand.Sort > sort && lppand.Id != lpplandid {
  1435. lppand.Sort++
  1436. updateLpplandList = append(updateLpplandList, lppand)
  1437. }
  1438. }
  1439. sort--
  1440. } else {
  1441. for _, lppand := range lpplandList {
  1442. if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  1443. lppand.Sort++
  1444. updateLpplandList = append(updateLpplandList, lppand)
  1445. }
  1446. }
  1447. }
  1448. var tmrlpplandList []*lpplandtl1
  1449. for _, lppand := range lpplandList {
  1450. exist := false
  1451. for _, lppand1 := range updateLpplandList {
  1452. if lppand.Id == lppand1.Id {
  1453. exist = true
  1454. break
  1455. }
  1456. }
  1457. if !exist {
  1458. tmrlpplandList = append(tmrlpplandList, lppand)
  1459. }
  1460. }
  1461. updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  1462. if len(updateLpplandList) > 0 {
  1463. tx.Begin()
  1464. for _, lppand := range updateLpplandList {
  1465. _, err := tx.SQL(" update lpplandtl1 set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1466. if err != nil {
  1467. tx.Rollback()
  1468. log.Println("AddLpplandtl-error-2: ", err)
  1469. appG.Response(http.StatusOK, e.ERROR, err)
  1470. return
  1471. }
  1472. }
  1473. _, 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()
  1474. if err != nil {
  1475. tx.Rollback()
  1476. log.Println("AddLpplandtl-error-2: ", err)
  1477. appG.Response(http.StatusOK, e.ERROR, err)
  1478. return
  1479. }
  1480. tx.Commit()
  1481. }
  1482. }
  1483. appG.Response(http.StatusOK, e.SUCCESS, true)
  1484. }
  1485. func UpdateLpplandtlSort(c *gin.Context) {
  1486. appG := app.Gin{C: c}
  1487. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1488. fsion := gofasion.NewFasion(string(dataByte))
  1489. parammaps := fsion.Get("parammaps")
  1490. tx := restful.Engine.NewSession()
  1491. defer tx.Close()
  1492. var err error
  1493. lpplandList := make([]*lpplandtl1, 0)
  1494. err = tx.Table("lpplandtl1").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1495. OrderBy("sort").Find(&lpplandList)
  1496. if err != nil {
  1497. log.Println("UpdateLpplandtlSort-error-1: ", err)
  1498. appG.Response(http.StatusOK, e.ERROR, err)
  1499. return
  1500. }
  1501. var oldsort int64
  1502. tmrid := parammaps.Get("tmrid").ValueInt64()
  1503. sort := parammaps.Get("sort").ValueInt64()
  1504. for i, lppland := range lpplandList {
  1505. lppland.Sort = int64(i) + 1
  1506. }
  1507. for _, lppland := range lpplandList {
  1508. if tmrid == lppland.Tmrid {
  1509. oldsort = lppland.Sort
  1510. break
  1511. }
  1512. }
  1513. for _, lppland := range lpplandList {
  1514. if lppland.Sort == sort {
  1515. for _, lppland1 := range lpplandList {
  1516. if lppland.Tmrid == lppland1.Tmrid {
  1517. sort = lppland1.Sort
  1518. if oldsort > sort {
  1519. break
  1520. }
  1521. }
  1522. }
  1523. break
  1524. }
  1525. }
  1526. if oldsort == sort {
  1527. appG.Response(http.StatusOK, e.SUCCESS, true)
  1528. return
  1529. }
  1530. tmrLpplandList := make([]*lpplandtl1, 0)
  1531. for _, lppland := range lpplandList {
  1532. if lppland.Tmrid == tmrid {
  1533. tmrLpplandList = append(tmrLpplandList, lppland)
  1534. }
  1535. }
  1536. for i, tmrLppland := range tmrLpplandList {
  1537. tmrLppland.Sort = sort + int64(i)
  1538. }
  1539. if oldsort > sort {
  1540. for _, lppland := range lpplandList {
  1541. if lppland.Sort == sort {
  1542. // uptmrid = lppland.Tmrid
  1543. sort = lppland.Sort
  1544. break
  1545. }
  1546. }
  1547. } else {
  1548. var uptmrid int64
  1549. for _, lppland := range lpplandList {
  1550. if lppland.Sort == sort {
  1551. uptmrid = lppland.Tmrid
  1552. sort = lppland.Sort
  1553. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  1554. sort = lppland.Sort
  1555. }
  1556. }
  1557. }
  1558. upcount := int64(len(tmrLpplandList))
  1559. if oldsort > sort {
  1560. for _, lppland := range lpplandList {
  1561. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  1562. lppland.Sort += upcount
  1563. tmrLpplandList = append(tmrLpplandList, lppland)
  1564. }
  1565. }
  1566. } else {
  1567. for _, lppland := range lpplandList {
  1568. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  1569. lppland.Sort -= upcount
  1570. tmrLpplandList = append(tmrLpplandList, lppland)
  1571. }
  1572. }
  1573. }
  1574. updateLpplandList := make([]*lpplandtl1, 0)
  1575. for _, lppland := range lpplandList {
  1576. exist := false
  1577. for _, tmrLppland := range tmrLpplandList {
  1578. if lppland.Id == tmrLppland.Id {
  1579. exist = true
  1580. break
  1581. }
  1582. }
  1583. if !exist {
  1584. updateLpplandList = append(updateLpplandList, lppland)
  1585. }
  1586. }
  1587. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  1588. tx.Begin()
  1589. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  1590. var wg sync.WaitGroup
  1591. for _, lppland := range tmrLpplandList {
  1592. wg.Add(1)
  1593. go func(lppland *lpplandtl1) {
  1594. defer wg.Done()
  1595. _, err = tx.SQL(`update lpplandtl1 set sort = ? where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
  1596. if err != nil {
  1597. log.Println("UpdateLpplandtlSort-error-2: ", err)
  1598. tx.Rollback()
  1599. appG.Response(http.StatusOK, e.ERROR, err)
  1600. return
  1601. }
  1602. }(lppland)
  1603. }
  1604. wg.Wait()
  1605. err = tx.Commit()
  1606. if err != nil {
  1607. log.Println("UpdateLpplandtlSort-error-3: ", err)
  1608. tx.Rollback()
  1609. appG.Response(http.StatusOK, e.ERROR, err)
  1610. return
  1611. }
  1612. appG.Response(http.StatusOK, e.SUCCESS, true)
  1613. }
  1614. func AddLpplandtlDate(c *gin.Context) {
  1615. appG := app.Gin{C: c}
  1616. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1617. fsion := gofasion.NewFasion(string(dataByte))
  1618. parammaps := fsion.Get("parammaps")
  1619. tx := restful.Engine.NewSession()
  1620. defer tx.Close()
  1621. exist := false
  1622. var err error
  1623. lpplandList := make([]*lpplandtl1, 0)
  1624. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,barid,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1625. And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
  1626. if err != nil {
  1627. log.Println("AddLpplandtl-error-1: ", err)
  1628. appG.Response(http.StatusOK, e.ERROR, err)
  1629. return
  1630. }
  1631. if parammaps.Get("id").ValueStr() != "" || parammaps.Get("id").ValueStr() != "0" {
  1632. for _, lppand := range lpplandList {
  1633. fmt.Println(parammaps.Get("id").ValueInt64())
  1634. if lppand.Id == parammaps.Get("id").ValueInt64() {
  1635. if lppand.Tmrid == parammaps.Get("tmrid").ValueInt64() {
  1636. appG.Response(http.StatusOK, e.SUCCESS, true)
  1637. return
  1638. }
  1639. exist = true
  1640. break
  1641. }
  1642. }
  1643. }
  1644. tmrid := parammaps.Get("tmrid").ValueInt64()
  1645. var sort int64 = -1
  1646. if !exist {
  1647. for _, lppand := range lpplandList {
  1648. if lppand.Barid == parammaps.Get("barid").ValueInt64() {
  1649. _, err := tx.SQL(" update lpplandtl1date set lweight =lweight + ? where id = ? and pastureid = ? ", parammaps.Get("lweight").ValueStr(), lppand.Id, parammaps.Get("pastureid").ValueStr()).Execute()
  1650. if err != nil {
  1651. log.Println("AddLpplandtl-error-2: ", err)
  1652. appG.Response(http.StatusOK, e.ERROR, err)
  1653. return
  1654. }
  1655. appG.Response(http.StatusOK, e.SUCCESS, true)
  1656. return
  1657. }
  1658. }
  1659. for i, lppand := range lpplandList {
  1660. lppand.Sort = int64(i) + 1
  1661. }
  1662. updateLpplandList := make([]*lpplandtl1, 0)
  1663. for i, lppand := range lpplandList {
  1664. n := i + 1
  1665. if lppand.Tmrid == tmrid {
  1666. sort = int64(n)
  1667. lppand.Sort = int64(n) + 1
  1668. updateLpplandList = append(updateLpplandList, lppand)
  1669. } else if sort > 0 {
  1670. lppand.Sort = int64(n) + 1
  1671. updateLpplandList = append(updateLpplandList, lppand)
  1672. }
  1673. }
  1674. tx.Begin()
  1675. for _, lppand := range updateLpplandList {
  1676. _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? and date = ? ", lppand.Sort, lppand.Id, lppand.Pastureid, parammaps.Get("date").ValueStr()).Execute()
  1677. if err != nil {
  1678. tx.Rollback()
  1679. log.Println("AddLpplandtl-error-2: ", err)
  1680. appG.Response(http.StatusOK, e.ERROR, err)
  1681. return
  1682. }
  1683. }
  1684. if sort < 0 && len(lpplandList) > 0 {
  1685. sort = int64(len(lpplandList) + 1)
  1686. } else if len(lpplandList) == 0 {
  1687. sort = 1
  1688. }
  1689. _, err = tx.SQL(`INSERT INTO lpplandtl1date(pastureid,lppid,barid,barname,fpdid,lweight,sort,tmrid,tmrname,fttype,background,cowcount,ccountradio,date)
  1690. VALUES (?,?,?,?,?,?,?,?,?,
  1691. ?,?,IF(?='',0,?),IF(?='',0,?),?)`,
  1692. parammaps.Get("pastureid").ValueStr(),
  1693. parammaps.Get("lppid").ValueStr(),
  1694. parammaps.Get("barid").ValueStr(),
  1695. parammaps.Get("barname").ValueStr(),
  1696. parammaps.Get("fpdid").ValueStr(),
  1697. parammaps.Get("lweight").ValueStr(),
  1698. sort,
  1699. parammaps.Get("tmrid").ValueStr(),
  1700. parammaps.Get("tmrname").ValueStr(),
  1701. parammaps.Get("fttype").ValueStr(),
  1702. parammaps.Get("background").ValueStr(),
  1703. parammaps.Get("cowcount").ValueStr(),
  1704. parammaps.Get("cowcount").ValueStr(),
  1705. parammaps.Get("ccountradio").ValueStr(),
  1706. parammaps.Get("ccountradio").ValueStr(),
  1707. parammaps.Get("date").ValueStr()).Execute()
  1708. if err != nil {
  1709. tx.Rollback()
  1710. log.Println("AddLpplandtl-error-3: ", err)
  1711. appG.Response(http.StatusOK, e.ERROR, err)
  1712. return
  1713. }
  1714. err = tx.Commit()
  1715. if err != nil {
  1716. tx.Rollback()
  1717. log.Println("AddLpplandtl-error-6: ", err)
  1718. appG.Response(http.StatusOK, e.ERROR, err)
  1719. return
  1720. }
  1721. } else {
  1722. lpplandid := parammaps.Get("id").ValueInt64()
  1723. updateLpplandList := make([]*lpplandtl1, 0)
  1724. var oldsort int64
  1725. for i, lppand := range lpplandList {
  1726. lppand.Sort = int64(i) + 1
  1727. }
  1728. for _, lppand := range lpplandList {
  1729. if lppand.Id == lpplandid {
  1730. oldsort = lppand.Sort
  1731. break
  1732. }
  1733. }
  1734. for _, lppand := range lpplandList {
  1735. if lppand.Tmrid == tmrid && lppand.Id != lpplandid && sort < 0 {
  1736. sort = lppand.Sort
  1737. break
  1738. }
  1739. }
  1740. if sort == -1 {
  1741. sort = 1
  1742. }
  1743. if oldsort < sort {
  1744. for _, lppand := range lpplandList {
  1745. if lppand.Id != lpplandid && lppand.Sort > oldsort && lppand.Sort < sort {
  1746. lppand.Sort--
  1747. updateLpplandList = append(updateLpplandList, lppand)
  1748. } else if lppand.Sort > sort && lppand.Id != lpplandid {
  1749. lppand.Sort++
  1750. updateLpplandList = append(updateLpplandList, lppand)
  1751. }
  1752. }
  1753. sort--
  1754. } else {
  1755. for _, lppand := range lpplandList {
  1756. if lppand.Sort < oldsort && lppand.Sort >= sort && lppand.Id != lpplandid {
  1757. lppand.Sort++
  1758. updateLpplandList = append(updateLpplandList, lppand)
  1759. }
  1760. }
  1761. }
  1762. var tmrlpplandList []*lpplandtl1
  1763. for _, lppand := range lpplandList {
  1764. exist := false
  1765. for _, lppand1 := range updateLpplandList {
  1766. if lppand.Id == lppand1.Id {
  1767. exist = true
  1768. break
  1769. }
  1770. }
  1771. if !exist {
  1772. tmrlpplandList = append(tmrlpplandList, lppand)
  1773. }
  1774. }
  1775. updateLpplandList = append(updateLpplandList, tmrlpplandList...)
  1776. if len(updateLpplandList) > 0 {
  1777. tx.Begin()
  1778. for _, lppand := range updateLpplandList {
  1779. _, err := tx.SQL(" update lpplandtl1date set sort = ? where id = ? and pastureid = ? ", lppand.Sort, lppand.Id, lppand.Pastureid).Execute()
  1780. if err != nil {
  1781. tx.Rollback()
  1782. log.Println("AddLpplandtl-error-2: ", err)
  1783. appG.Response(http.StatusOK, e.ERROR, err)
  1784. return
  1785. }
  1786. }
  1787. _, 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()
  1788. if err != nil {
  1789. tx.Rollback()
  1790. log.Println("AddLpplandtl-error-2: ", err)
  1791. appG.Response(http.StatusOK, e.ERROR, err)
  1792. return
  1793. }
  1794. tx.Commit()
  1795. }
  1796. }
  1797. appG.Response(http.StatusOK, e.SUCCESS, true)
  1798. }
  1799. func UpdateLpplandtlDateSort(c *gin.Context) {
  1800. appG := app.Gin{C: c}
  1801. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1802. fsion := gofasion.NewFasion(string(dataByte))
  1803. parammaps := fsion.Get("parammaps")
  1804. tx := restful.Engine.NewSession()
  1805. defer tx.Close()
  1806. var err error
  1807. lpplandList := make([]*lpplandtl1, 0)
  1808. err = tx.Table("lpplandtl1date").Select("id,pastureid,sort,tmrid").Where("lppid = ? ", parammaps.Get("lppid").ValueStr()).And("pastureid = ?", parammaps.Get("pastureid").ValueStr()).
  1809. And("date = ? ", parammaps.Get("date").ValueStr()).OrderBy("sort").Find(&lpplandList)
  1810. if err != nil {
  1811. log.Println("UpdateLpplandtlSort-error-1: ", err)
  1812. appG.Response(http.StatusOK, e.ERROR, err)
  1813. return
  1814. }
  1815. var oldsort int64
  1816. tmrid := parammaps.Get("tmrid").ValueInt64()
  1817. sort := parammaps.Get("sort").ValueInt64()
  1818. for i, lppland := range lpplandList {
  1819. lppland.Sort = int64(i) + 1
  1820. }
  1821. for _, lppland := range lpplandList {
  1822. if tmrid == lppland.Tmrid {
  1823. oldsort = lppland.Sort
  1824. break
  1825. }
  1826. }
  1827. for _, lppland := range lpplandList {
  1828. if lppland.Sort == sort {
  1829. for _, lppland1 := range lpplandList {
  1830. if lppland.Tmrid == lppland1.Tmrid {
  1831. sort = lppland1.Sort
  1832. if oldsort > sort {
  1833. break
  1834. }
  1835. }
  1836. }
  1837. break
  1838. }
  1839. }
  1840. if oldsort == sort {
  1841. appG.Response(http.StatusOK, e.SUCCESS, true)
  1842. return
  1843. }
  1844. tmrLpplandList := make([]*lpplandtl1, 0)
  1845. for _, lppland := range lpplandList {
  1846. if lppland.Tmrid == tmrid {
  1847. tmrLpplandList = append(tmrLpplandList, lppland)
  1848. }
  1849. }
  1850. for i, tmrLppland := range tmrLpplandList {
  1851. tmrLppland.Sort = sort + int64(i)
  1852. }
  1853. if oldsort > sort {
  1854. for _, lppland := range lpplandList {
  1855. if lppland.Sort == sort {
  1856. // uptmrid = lppland.Tmrid
  1857. sort = lppland.Sort
  1858. break
  1859. }
  1860. }
  1861. } else {
  1862. var uptmrid int64
  1863. for _, lppland := range lpplandList {
  1864. if lppland.Sort == sort {
  1865. uptmrid = lppland.Tmrid
  1866. sort = lppland.Sort
  1867. } else if uptmrid != 0 && uptmrid == lppland.Tmrid {
  1868. sort = lppland.Sort
  1869. }
  1870. }
  1871. }
  1872. upcount := int64(len(tmrLpplandList))
  1873. if oldsort > sort {
  1874. for _, lppland := range lpplandList {
  1875. if lppland.Sort >= sort && lppland.Tmrid != tmrid {
  1876. lppland.Sort += upcount
  1877. tmrLpplandList = append(tmrLpplandList, lppland)
  1878. }
  1879. }
  1880. } else {
  1881. for _, lppland := range lpplandList {
  1882. if lppland.Sort <= sort && lppland.Sort >= oldsort && lppland.Tmrid != tmrid {
  1883. lppland.Sort -= upcount
  1884. tmrLpplandList = append(tmrLpplandList, lppland)
  1885. }
  1886. }
  1887. }
  1888. updateLpplandList := make([]*lpplandtl1, 0)
  1889. for _, lppland := range lpplandList {
  1890. exist := false
  1891. for _, tmrLppland := range tmrLpplandList {
  1892. if lppland.Id == tmrLppland.Id {
  1893. exist = true
  1894. break
  1895. }
  1896. }
  1897. if !exist {
  1898. updateLpplandList = append(updateLpplandList, lppland)
  1899. }
  1900. }
  1901. tmrLpplandList = append(tmrLpplandList, updateLpplandList...)
  1902. tx.Begin()
  1903. // _, err = tx.Table("lpplandtl1").Update(&tmrLpplandList)
  1904. var wg sync.WaitGroup
  1905. for _, lppland := range tmrLpplandList {
  1906. wg.Add(1)
  1907. go func(lppland *lpplandtl1) {
  1908. defer wg.Done()
  1909. _, err = tx.SQL(`update lpplandtl1date set sort = ? where id = ? and pastureid = ? `, lppland.Sort, lppland.Id, lppland.Pastureid).Execute()
  1910. if err != nil {
  1911. log.Println("UpdateLpplandtlSort-error-2: ", err)
  1912. tx.Rollback()
  1913. appG.Response(http.StatusOK, e.ERROR, err)
  1914. return
  1915. }
  1916. }(lppland)
  1917. }
  1918. wg.Wait()
  1919. err = tx.Commit()
  1920. if err != nil {
  1921. log.Println("UpdateLpplandtlSort-error-3: ", err)
  1922. tx.Rollback()
  1923. appG.Response(http.StatusOK, e.ERROR, err)
  1924. return
  1925. }
  1926. appG.Response(http.StatusOK, e.SUCCESS, true)
  1927. }
  1928. func GetFeedpUndistributed(c *gin.Context) {
  1929. appG := app.Gin{C: c}
  1930. tx := restful.Engine.NewSession()
  1931. defer tx.Close()
  1932. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1933. fsion := gofasion.NewFasion(string(dataByte))
  1934. timesMax := fsion.Get("times").ValueInt()
  1935. pastureid := fsion.Get("pastureid").ValueStr()
  1936. data := make(map[string]interface{})
  1937. for times := 1; times <= timesMax; times++ {
  1938. 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
  1939. inner join feedtemplet ft
  1940. on ft.id = feedp.ftid and ft.pastureid=?
  1941. WHERE feedp.pastureid = ?
  1942. GROUP BY feedp.ftid
  1943. UNION
  1944. 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
  1945. inner join feedtemplet ft
  1946. on ft.id = feedp.ptsfid and ft.pastureid=?
  1947. WHERE feedp.pastureid = ? and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  1948. GROUP BY feedp.ptsfid`
  1949. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  1950. if err != nil {
  1951. log.Println("GetFeedpUndistributed-error-1: ", err)
  1952. appG.Response(http.StatusOK, e.ERROR, nil)
  1953. return
  1954. }
  1955. 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 ,
  1956. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  1957. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  1958. UNION
  1959. 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 ,
  1960. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  1961. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  1962. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  1963. if err != nil {
  1964. log.Println("GetFeedpUndistributed-error-2: ", err)
  1965. appG.Response(http.StatusOK, e.ERROR, nil)
  1966. return
  1967. }
  1968. for _, ft := range ftList {
  1969. arrList := make([]map[string]interface{}, 0)
  1970. arrid := ft["arrid"].(string)
  1971. for _, detail := range ftDetailList {
  1972. if _, ok := detail["ptid"]; ok {
  1973. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1974. detail["background"] = ft["background"]
  1975. arrList = append(arrList, detail)
  1976. }
  1977. } else if _, ok := detail["ptsid"]; ok {
  1978. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  1979. detail["background"] = ft["background"]
  1980. arrList = append(arrList, detail)
  1981. }
  1982. }
  1983. }
  1984. if len(arrList) > 0 {
  1985. ft["arrList"] = arrList
  1986. }
  1987. }
  1988. var key string
  1989. if times == 1 {
  1990. key = "one"
  1991. } else if times == 2 {
  1992. key = "two"
  1993. } else if times == 3 {
  1994. key = "three"
  1995. } else if times == 4 {
  1996. key = "four"
  1997. } else if times == 5 {
  1998. key = "five"
  1999. } else {
  2000. key = "six"
  2001. }
  2002. data[key] = ftList
  2003. }
  2004. appG.Response(http.StatusOK, e.SUCCESS, data)
  2005. }