spillage.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. package api
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "../../pkg/app"
  12. "../../pkg/e"
  13. "../../pkg/logging"
  14. "../../pkg/setting"
  15. "../../routers/restful"
  16. "github.com/Anderson-Lu/gofasion/gofasion"
  17. "github.com/gin-gonic/gin"
  18. "github.com/xormplus/xorm"
  19. )
  20. type feedtemplet struct {
  21. Id int64 `xorm:"id"`
  22. Ccname string `xorm:"ccname"`
  23. Cname string `xorm:"tname"`
  24. }
  25. type tmrStruct struct {
  26. Id int64 `xorm:"id"`
  27. Tname string `xorm:"tname"`
  28. Maxstirfeed float64 `xorm:"maxstirfeed"`
  29. }
  30. type lpplanStruct struct {
  31. Id int64 `xorm:"id"`
  32. Tmrname string `xorm:"tmrname"`
  33. Tmrid int64 `xorm:"tmrid"`
  34. Sort int64 `xorm:"sort"`
  35. Sel int64 `xorm:"sel"`
  36. Times int64 `xorm:"times"`
  37. Ftname string `xorm:"ftname"`
  38. Ftid int64 `xorm:"ftid"`
  39. Maxweight float64 `xorm:"maxweight"`
  40. Sumweight float64 `xorm:"sumweight"`
  41. Sumcowcount int64 `xorm:"sumcowcount"`
  42. Pastureid string `xorm:"pastureid"`
  43. }
  44. type sysopt struct {
  45. Inforvalue int `xorm:"inforvalue"`
  46. }
  47. type lpplanQueryInfo struct {
  48. Pastureid int64 `xorm:"pastureid"`
  49. Lppid int64 `xorm:"id"`
  50. Maxweight float64 `xorm:"maxweight"`
  51. Lweight float64 `xorm:"lweight"`
  52. Lweighthis float64 `xorm:"lweighthis"`
  53. Barid int64 `xorm:"barid"`
  54. Barname string `xorm:"barname"`
  55. Sort int64 `xorm:"sort"`
  56. Tmrid int64 `xorm:"tmrid"`
  57. Tmrname string `xorm:"tmrname"`
  58. Cowcount int64 `xorm:"cowcount"`
  59. Ccountradio float64 `xorm:"ccountradio"`
  60. Background string `xorm:"background"`
  61. Times int64 `xorm:"times"`
  62. Ftid int64 `xorm:"ftid"`
  63. }
  64. type fpdetailQueryInfo struct {
  65. Fpdid int64 `xorm:"fpdid"`
  66. Ptsrate float64 `xorm:"ptsrate"`
  67. Ptsuse float64 `xorm:"ptsuse"`
  68. Ptuse float64 `xorm:"ptuse"`
  69. Weight float64 `xorm:"weight"`
  70. Barid int64 `xorm:"barid"`
  71. Barname string `xorm:"barname"`
  72. Cowcount int64 `xorm:"cowcount"`
  73. Ccountradio float64 `xorm:"ccountradio"`
  74. Fttypeid int64 `xorm:"fttypeid"`
  75. Times int64 `xorm:"times"`
  76. Ptid int64 `xorm:"ptid"`
  77. Background string `xorm:"background"`
  78. }
  79. type lpplandtl1 struct {
  80. Pastureid int64 `xorm:"pastureid"`
  81. Lppid int64 `xorm:"lppid"`
  82. Barid int64 `xorm:"barid"`
  83. Barname string `xorm:"barname"`
  84. Fpdid int64 `xorm:"fpdid"`
  85. Fttype int64 `xorm:"fttype"`
  86. Lweight float64 `xorm:"lweight"`
  87. Sort int64 `xorm:"sort"`
  88. Tmrid int64 `xorm:"tmrid"`
  89. Tmrname string `xorm:"tmrname"`
  90. Cowcount int64 `xorm:"cowcount"`
  91. Ccountradio float64 `xorm:"ccountradio"`
  92. Background string `xorm:"background"`
  93. Lweighthis float64 `xorm:"lweighthis"`
  94. }
  95. //撒料计划 自动生成
  96. func Autogeneration(c *gin.Context) {
  97. appG := app.Gin{C: c}
  98. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  99. fsion := gofasion.NewFasion(string(dataByte))
  100. pastureid := fsion.Get("pastureid").ValueStr()
  101. typeIN := fsion.Get("type").ValueInt64()
  102. log.Println(time.Now())
  103. tx := restful.Engine.NewSession()
  104. // defer tx.Close()
  105. var times int
  106. var wg sync.WaitGroup
  107. var tmrlist *[]*tmrStruct
  108. var feedtempletlist *[]*feedtemplet
  109. var err error
  110. var lpplanlist *[]*lpplanList
  111. var fpdetailWeightList *[]*fpdetailWeight
  112. wg.Add(1)
  113. go func() {
  114. defer wg.Done()
  115. feedtempletlist, err = getfeedtemplet(pastureid, tx)
  116. if err != nil {
  117. appG.Response(http.StatusOK, e.ERROR, false)
  118. return
  119. }
  120. }()
  121. wg.Wait()
  122. wg.Add(1)
  123. go func() {
  124. defer wg.Done()
  125. tmrlist, err = gettmr(pastureid, tx)
  126. if err != nil {
  127. appG.Response(http.StatusOK, e.ERROR, false)
  128. return
  129. }
  130. }()
  131. wg.Wait()
  132. wg.Add(1)
  133. go func() {
  134. defer wg.Done()
  135. times, err = gettimes(pastureid, tx)
  136. if err != nil {
  137. appG.Response(http.StatusOK, e.ERROR, false)
  138. return
  139. }
  140. }()
  141. wg.Wait()
  142. wg.Add(1)
  143. go func() {
  144. defer wg.Done()
  145. lpplanlist, err = getLpplanlist(pastureid, tx)
  146. if err != nil {
  147. appG.Response(http.StatusOK, e.ERROR, false)
  148. return
  149. }
  150. }()
  151. wg.Wait()
  152. wg.Add(1)
  153. go func() {
  154. defer wg.Done()
  155. fpdetailWeightList, err = getFpdetail(pastureid, tx)
  156. if err != nil {
  157. appG.Response(http.StatusOK, e.ERROR, false)
  158. return
  159. }
  160. for _, fpd := range *fpdetailWeightList {
  161. for _, lpp := range *lpplanlist {
  162. if (lpp.Ftid == fpd.Ptid || lpp.Ftid == fpd.Ptsid) && lpp.Times == fpd.Times {
  163. fpd.Weight = fpd.Weight - (lpp.Maxweight - lpp.Lweight)
  164. }
  165. }
  166. }
  167. }()
  168. wg.Wait()
  169. // 避免车次数量为0造成死循环
  170. max := false
  171. for _, tmr := range *tmrlist {
  172. if tmr.Maxstirfeed > 0 {
  173. max = true
  174. break
  175. }
  176. }
  177. for _, tem := range *feedtempletlist {
  178. count, err := getLpplanCount(tx)
  179. if err != nil {
  180. appG.Response(http.StatusOK, e.ERROR, false)
  181. return
  182. }
  183. for i := 1; i <= times; i++ {
  184. for _, fpd := range *fpdetailWeightList {
  185. if tem.Id == fpd.Temid && i == int(fpd.Times) {
  186. var ftid int64
  187. if fpd.Ptid != 0 {
  188. ftid = fpd.Ptid
  189. } else {
  190. ftid = fpd.Ptsid
  191. }
  192. for fpd.Weight > 0 && max {
  193. for _, tmr := range *tmrlist {
  194. if tmr.Maxstirfeed > 0 {
  195. count++
  196. ids, err := setting.SnowIds.NextId()
  197. if err != nil {
  198. ids = time.Now().UnixNano()
  199. logging.Info("create SnowIds err", err)
  200. }
  201. wg.Add(1)
  202. go func(ids int64, tmr *tmrStruct, count, i, ftid int64, tem *feedtemplet) {
  203. defer wg.Done()
  204. lpplan := &lpplanStruct{
  205. Id: ids,
  206. Tmrname: tmr.Tname,
  207. Tmrid: tmr.Id,
  208. Sort: count,
  209. Sel: 1,
  210. Times: int64(i),
  211. Ftname: tem.Cname,
  212. Ftid: ftid,
  213. Maxweight: tmr.Maxstirfeed,
  214. Sumweight: 0,
  215. Sumcowcount: 0,
  216. Pastureid: pastureid,
  217. }
  218. err = addLpplan(lpplan, tx)
  219. if err != nil {
  220. appG.Response(http.StatusOK, e.ERROR, false)
  221. return
  222. }
  223. }(ids, tmr, count, int64(i), ftid, tem)
  224. fpd.Weight = fpd.Weight - tmr.Maxstirfeed
  225. }
  226. if fpd.Weight < 0 {
  227. break
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. wg.Wait()
  236. err = autogeneration(typeIN, pastureid, tx)
  237. if err != nil {
  238. appG.Response(http.StatusOK, e.ERROR, false)
  239. return
  240. }
  241. appG.Response(http.StatusOK, e.SUCCESS, true)
  242. }
  243. //查看配方模板
  244. func getfeedtemplet(pastureid string, tx *xorm.Session) (*[]*feedtemplet, error) {
  245. data := make([]*feedtemplet, 0)
  246. err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
  247. if err != nil {
  248. log.Println("getfeedtemplet-error", err)
  249. return nil, err
  250. }
  251. return &data, nil
  252. }
  253. //查看tmr设备
  254. func gettmr(pastureid string, tx *xorm.Session) (*[]*tmrStruct, error) {
  255. data := make([]*tmrStruct, 0)
  256. session := tx.Table("tmr")
  257. session.Select("TRIM(id) id,tname,maxstirfeed")
  258. session.Where("pastureid = ? ", pastureid)
  259. session.Where("enable = 1")
  260. session.In("tclassid", "1,2,3")
  261. err := session.Find(&data)
  262. if err != nil {
  263. log.Println("gettmr-error", err)
  264. return nil, err
  265. }
  266. return &data, nil
  267. }
  268. //判断计划种是否有该车次
  269. func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
  270. session := tx.Table("lpplan")
  271. session.Where("pastureid = ? ", pastureid)
  272. session.Where("times = ? ", times)
  273. session.Where("tmrid = ? ", tmrid)
  274. session.Where("ftid = ? ", ftid)
  275. exist, err := session.Exist()
  276. if err != nil {
  277. log.Println("getLpplanExist-error", err)
  278. return false, err
  279. }
  280. return exist, nil
  281. }
  282. type fpdetailWeight struct {
  283. Times int64 `xorm:"times"`
  284. Weight float64 `xorm:"weight"`
  285. Pastureid int64 `xorm:"pastureid"`
  286. Ptid int64 `xorm:"ptid"`
  287. Ptsid int64 `xorm:"ptsid"`
  288. Cowcount int64 `xorm:"cowcount"`
  289. Temid int64 `xorm:"Temid"`
  290. }
  291. //获取料数量
  292. func getFpdetail(pastureid string, tx *xorm.Session) (*[]*fpdetailWeight, error) {
  293. fpdetaillist := make([]*fpdetailWeight, 0)
  294. 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
  295. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  296. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  297. WHERE fpd.pastureid = ? AND fpd.ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptid > 0
  298. group by fpd.id,fpd.times) t1
  299. 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 ,
  300. 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 ,
  301. cowcount FROM fpdetail fpd
  302. join feedp fp on fp.ptsfid = fpd.ptsid and fp.pastureid = fpd.pastureid
  303. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  304. WHERE fpd.pastureid = ? AND fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2) and fpd.ptsid > 0
  305. group by fpd.id,fpd.times) t1
  306. group by t1.temid,t1.times) t2 on t2.temid = t1.temid and t2.times = t1.times
  307. group by t1.temid,t1.times
  308. `, pastureid, pastureid).Find(&fpdetaillist)
  309. if err != nil {
  310. log.Println("getFpdetail-error", err)
  311. tx.Rollback()
  312. return nil, err
  313. }
  314. return &fpdetaillist, nil
  315. }
  316. type lpplanList struct {
  317. Tmrid int64 `xorm:"tmrid"`
  318. Times int64 `xorm:"times"`
  319. Ftid int64 `xorm:"ftid"`
  320. Maxweight float64 `xorm:"maxweight"`
  321. Lweight float64 `xorm:"lweight"`
  322. }
  323. func getLpplanlist(patsureid string, tx *xorm.Session) (*[]*lpplanList, error) {
  324. session := tx.SQL(` select t1.times,t1.tmrid,t1.ftid,t1.maxweight ,ifnull(t2.lweight,0)as lweight from lpplan t1
  325. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  326. where t1.pastureid = ? `, patsureid, patsureid)
  327. data := make([]*lpplanList, 0)
  328. err := session.Find(&data)
  329. if err != nil {
  330. log.Println("getLpplanlist-error", err)
  331. return nil, err
  332. }
  333. return &data, nil
  334. }
  335. // 添加车次计划
  336. func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
  337. _, err := tx.Table("lpplan").Insert(lpplan)
  338. if err != nil {
  339. log.Println("addLpplan-error", err)
  340. return err
  341. }
  342. return nil
  343. }
  344. //获取车次数量
  345. func getLpplanCount(tx *xorm.Session) (int64, error) {
  346. count, err := tx.Table("lpplan").Count()
  347. if err != nil {
  348. log.Println("getLpplanCount-error", err)
  349. return 0, err
  350. }
  351. return count, err
  352. }
  353. //获取班次信息
  354. func gettimes(pastureid string, tx *xorm.Session) (int, error) {
  355. session := tx.Table("sysopt")
  356. session.Where("pastureid = ? ", pastureid)
  357. session.Where("inforname = 'times' ")
  358. data := new(sysopt)
  359. _, err := session.Get(data)
  360. if err != nil {
  361. log.Println("gettimes-error", err)
  362. return 0, err
  363. }
  364. return data.Inforvalue, nil
  365. }
  366. type upfpdetail struct {
  367. insertlweight float64
  368. fttypeid int64
  369. fpdid int64
  370. }
  371. func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
  372. tx.Begin()
  373. if typeIN == 1 {
  374. _, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
  375. if err != nil {
  376. log.Println("autogeneration-error-1", err)
  377. tx.Rollback()
  378. return err
  379. }
  380. _, err = tx.SQL(`update fpdetail
  381. set ptuse = 0,ptsuse=0
  382. WHERE pastureid = ?`, pastureid).Execute()
  383. if err != nil {
  384. log.Println("autogeneration-error-2", err)
  385. tx.Rollback()
  386. return err
  387. }
  388. }
  389. lpplansession := tx.SQL(`select
  390. t1.sort,t1.tmrid,t1.tmrname,t1.ftid,
  391. t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight,t1.times from lpplan t1
  392. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  393. where t1.pastureid = ? and t1.maxweight > ifnull(t2.lweight,0) `, pastureid, pastureid)
  394. lpplanlist := make([]*lpplanQueryInfo, 0)
  395. err := lpplansession.Find(&lpplanlist)
  396. if err != nil {
  397. log.Println("autogeneration-error-3", err)
  398. tx.Rollback()
  399. return err
  400. }
  401. fpdetaillist := make([]*fpdetailQueryInfo, 0)
  402. 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 ,
  403. (select bname from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,cowcount,ccountradio,0 isfill FROM fpdetail fpd
  404. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  405. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  406. WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2)
  407. group by fpd.id , fpd.times `, pastureid).Find(&fpdetaillist)
  408. if err != nil {
  409. log.Println("autogeneration-error-4", err)
  410. tx.Rollback()
  411. return err
  412. }
  413. log.Println(time.Now(), "333333333333")
  414. var wg sync.WaitGroup
  415. for _, lpplan := range lpplanlist {
  416. lpplandtl1List := make([]*lpplandtl1, 0)
  417. upfpdetailList := make([]*upfpdetail, 0)
  418. for _, fpdetail := range fpdetaillist {
  419. if fpdetail.Ptid == lpplan.Ftid && fpdetail.Times == lpplan.Times {
  420. var weight float64
  421. weight = fpdetail.Weight
  422. if lpplan.Maxweight <= lpplan.Lweight || weight <= 0 {
  423. continue
  424. }
  425. var insertlweight float64
  426. if lpplan.Maxweight-lpplan.Lweight > weight {
  427. insertlweight = weight
  428. } else {
  429. insertlweight = lpplan.Maxweight - lpplan.Lweight
  430. }
  431. lpplan.Lweight += insertlweight
  432. lpplan.Lweighthis = insertlweight
  433. exist := false
  434. for _, up := range upfpdetailList {
  435. if up.fpdid == fpdetail.Fpdid {
  436. up.insertlweight += insertlweight
  437. exist = true
  438. break
  439. }
  440. }
  441. if !exist {
  442. upfpdetailList = append(upfpdetailList, &upfpdetail{
  443. fpdid: fpdetail.Fpdid,
  444. insertlweight: insertlweight,
  445. fttypeid: fpdetail.Fttypeid,
  446. })
  447. }
  448. fpdetail.Weight = fpdetail.Weight - insertlweight
  449. exist = false
  450. for _, lpplandtl1 := range lpplandtl1List {
  451. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  452. lpplandtl1.Lweight += insertlweight
  453. lpplandtl1.Lweighthis = insertlweight
  454. exist = true
  455. break
  456. }
  457. }
  458. if !exist {
  459. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  460. Pastureid: lpplan.Pastureid,
  461. Lppid: lpplan.Lppid,
  462. Barid: fpdetail.Barid,
  463. Barname: fpdetail.Barname,
  464. Fpdid: fpdetail.Fpdid,
  465. Fttype: fpdetail.Fttypeid,
  466. Lweight: insertlweight,
  467. Sort: lpplan.Sort,
  468. Tmrid: lpplan.Tmrid,
  469. Tmrname: lpplan.Tmrname,
  470. Cowcount: lpplan.Cowcount,
  471. Ccountradio: lpplan.Ccountradio,
  472. Background: fpdetail.Background,
  473. Lweighthis: insertlweight,
  474. })
  475. }
  476. }
  477. }
  478. for _, lpplpplandtl1 := range lpplandtl1List {
  479. wg.Add(1)
  480. go func(lpplpplandtl1 *lpplandtl1) {
  481. defer wg.Done()
  482. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  483. cowcount,ccountradio,background,lweighthis)
  484. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  485. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  486. lpplpplandtl1.Lppid,
  487. lpplpplandtl1.Barid,
  488. lpplpplandtl1.Barname,
  489. lpplpplandtl1.Fpdid,
  490. lpplpplandtl1.Fttype,
  491. lpplpplandtl1.Lweight,
  492. lpplpplandtl1.Sort,
  493. lpplpplandtl1.Tmrid,
  494. lpplpplandtl1.Tmrname,
  495. lpplpplandtl1.Cowcount,
  496. lpplpplandtl1.Ccountradio,
  497. lpplpplandtl1.Background,
  498. lpplpplandtl1.Lweighthis).Execute()
  499. if err != nil {
  500. log.Println("autogeneration-error-6", err)
  501. tx.Rollback()
  502. return
  503. }
  504. }(lpplpplandtl1)
  505. }
  506. wg.Wait()
  507. for _, up := range upfpdetailList {
  508. wg.Add(1)
  509. go func(up *upfpdetail) {
  510. defer wg.Done()
  511. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  512. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  513. if err != nil {
  514. log.Println("autogeneration-error-5", err)
  515. tx.Rollback()
  516. return
  517. }
  518. }(up)
  519. }
  520. wg.Wait()
  521. }
  522. if typeIN == 1 {
  523. _, err = tx.SQL("update lpplan set sel = 1 where pastureid = ?", pastureid).Execute()
  524. if err != nil {
  525. log.Println("autogeneration-error-10", err)
  526. return err
  527. }
  528. }
  529. supplement := tx.Table("fpdetail").Alias("fpd")
  530. 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")
  531. supplement.Join("inner", []string{"feedp", "fp"}, "fp.ptsfid = fpd.ptsid").And("fp.pastureid = fpd.pastureid")
  532. supplement.Join("inner", []string{"feedtemplet", "ft"}, "ft.id = fp.ftid").And("ft.pastureid = fpd.pastureid")
  533. supplement.Where("fpd.pastureid = ?", pastureid).And("fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2)").And("fpd.ptsid > 0")
  534. supplement.GroupBy("fpd.id,fpd.times")
  535. supplementList := make([]*fpdetailQueryInfo, 0)
  536. err = supplement.Find(&supplementList)
  537. if err != nil {
  538. log.Println("autogeneration-error-11", err)
  539. return err
  540. }
  541. if len(supplementList) > 0 {
  542. for _, lpplan := range lpplanlist {
  543. if lpplan.Maxweight <= lpplan.Lweight {
  544. continue
  545. }
  546. lppids := []string{strconv.FormatInt(lpplan.Lppid, 10)}
  547. for _, lpp := range lpplanlist {
  548. if lpp.Ftid == lpplan.Ftid && lpp.Times == lpplan.Times {
  549. lppids = append(lppids, strconv.FormatInt(lpp.Lppid, 10))
  550. }
  551. }
  552. lpplandtl1List := make([]*lpplandtl1, 0)
  553. upfpdetailList := make([]*upfpdetail, 0)
  554. lpplandtllist := make([]*lpplandtl1, 0)
  555. err = tx.Table("lpplandtl1").Select("barid").Where(fmt.Sprintf("lppid in (%s)", strings.Join(lppids, ","))).And("").Find(&lpplandtllist)
  556. if err != nil {
  557. log.Println("autogeneration-error-11", err)
  558. return err
  559. }
  560. for _, fpdetail := range supplementList {
  561. if lpplan.Times != fpdetail.Times {
  562. continue
  563. }
  564. exist := false
  565. for _, bar := range lpplandtllist {
  566. if bar.Barid == fpdetail.Barid {
  567. exist = true
  568. break
  569. }
  570. }
  571. if !exist {
  572. continue
  573. }
  574. var weight float64
  575. weight = fpdetail.Weight
  576. if weight <= 0 {
  577. continue
  578. }
  579. var insertlweight float64
  580. if lpplan.Maxweight-lpplan.Lweight > weight {
  581. insertlweight = weight
  582. } else {
  583. insertlweight = lpplan.Maxweight - lpplan.Lweight
  584. }
  585. lpplan.Lweight += insertlweight
  586. lpplan.Lweighthis = insertlweight
  587. exist = false
  588. for _, up := range upfpdetailList {
  589. if up.fpdid == fpdetail.Fpdid {
  590. up.insertlweight += insertlweight
  591. exist = true
  592. break
  593. }
  594. }
  595. if !exist {
  596. upfpdetailList = append(upfpdetailList, &upfpdetail{
  597. fpdid: fpdetail.Fpdid,
  598. insertlweight: insertlweight,
  599. fttypeid: 0,
  600. })
  601. }
  602. fpdetail.Weight = fpdetail.Weight - insertlweight
  603. exist = false
  604. for _, lpplandtl1 := range lpplandtl1List {
  605. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  606. lpplandtl1.Lweight += insertlweight
  607. lpplandtl1.Lweighthis = insertlweight
  608. exist = true
  609. break
  610. }
  611. }
  612. if !exist {
  613. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  614. Pastureid: lpplan.Pastureid,
  615. Lppid: lpplan.Lppid,
  616. Barid: fpdetail.Barid,
  617. Barname: fpdetail.Barname,
  618. Fpdid: fpdetail.Fpdid,
  619. Fttype: 0,
  620. Lweight: insertlweight,
  621. Sort: lpplan.Sort,
  622. Tmrid: lpplan.Tmrid,
  623. Tmrname: lpplan.Tmrname,
  624. Cowcount: lpplan.Cowcount,
  625. Ccountradio: lpplan.Ccountradio,
  626. Background: fpdetail.Background,
  627. Lweighthis: insertlweight,
  628. })
  629. }
  630. }
  631. for _, lpplpplandtl1 := range lpplandtl1List {
  632. wg.Add(1)
  633. go func(lpplpplandtl1 *lpplandtl1) {
  634. defer wg.Done()
  635. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  636. cowcount,ccountradio,background,lweighthis)
  637. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  638. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  639. lpplpplandtl1.Lppid,
  640. lpplpplandtl1.Barid,
  641. lpplpplandtl1.Barname,
  642. lpplpplandtl1.Fpdid,
  643. lpplpplandtl1.Fttype,
  644. lpplpplandtl1.Lweight,
  645. lpplpplandtl1.Sort,
  646. lpplpplandtl1.Tmrid,
  647. lpplpplandtl1.Tmrname,
  648. lpplpplandtl1.Cowcount,
  649. lpplpplandtl1.Ccountradio,
  650. lpplpplandtl1.Background,
  651. lpplpplandtl1.Lweighthis).Execute()
  652. if err != nil {
  653. log.Println("autogeneration-error-6", err)
  654. tx.Rollback()
  655. return
  656. }
  657. }(lpplpplandtl1)
  658. }
  659. wg.Wait()
  660. for _, up := range upfpdetailList {
  661. wg.Add(1)
  662. go func(up *upfpdetail) {
  663. defer wg.Done()
  664. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  665. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  666. if err != nil {
  667. log.Println("autogeneration-error-5", err)
  668. tx.Rollback()
  669. // return err
  670. return
  671. }
  672. }(up)
  673. }
  674. wg.Wait()
  675. }
  676. }
  677. err = tx.Commit()
  678. if err != nil {
  679. log.Println("autogeneration-error-11", err)
  680. return err
  681. }
  682. return nil
  683. }
  684. func GetSpillage(c *gin.Context) {
  685. appG := app.Gin{C: c}
  686. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  687. fsion := gofasion.NewFasion(string(dataByte))
  688. parammaps := fsion.Get("parammaps")
  689. pastureid := parammaps.Get("pastureid").ValueInt64()
  690. times := parammaps.Get("times").ValueInt64()
  691. // refresh 0 全部 , 1 车次信息, 2 班次
  692. refresh := parammaps.Get("refresh").ValueInt64()
  693. tx := restful.Engine.NewSession()
  694. defer tx.Close()
  695. data := make(map[string]interface{})
  696. if refresh == 0 || refresh == 2 {
  697. ftList, err := tx.SQL(`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
  698. inner join feedtemplet ft
  699. on ft.id = feedp.ftid and ft.pastureid=?
  700. WHERE feedp.pastureid = ?
  701. GROUP BY feedp.ftid
  702. UNION
  703. 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
  704. inner join feedtemplet ft
  705. on ft.id = feedp.ptsfid and ft.pastureid=?
  706. WHERE feedp.pastureid = ? and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  707. GROUP BY feedp.ptsfid`, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  708. if err != nil {
  709. log.Println("GetSpillage-error-1: ", err)
  710. appG.Response(http.StatusOK, e.ERROR, nil)
  711. return
  712. }
  713. 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 ,
  714. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  715. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  716. UNION
  717. 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 ,
  718. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  719. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  720. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  721. if err != nil {
  722. log.Println("GetSpillage-error-2: ", err)
  723. appG.Response(http.StatusOK, e.ERROR, nil)
  724. return
  725. }
  726. for _, ft := range ftList {
  727. arrList := make([]map[string]interface{}, 0)
  728. arrid := ft["arrid"].(string)
  729. for _, detail := range ftDetailList {
  730. if _, ok := detail["ptid"]; ok {
  731. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  732. detail["background"] = ft["background"]
  733. arrList = append(arrList, detail)
  734. }
  735. } else if _, ok := detail["ptsid"]; ok {
  736. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  737. detail["background"] = ft["background"]
  738. arrList = append(arrList, detail)
  739. }
  740. }
  741. }
  742. if len(arrList) > 0 {
  743. ft["arrList"] = arrList
  744. }
  745. }
  746. data["ftlist"] = ftList
  747. }
  748. if refresh == 0 || refresh == 1 {
  749. lppList, err := tx.SQL(`SELECT
  750. ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) tmrname,trim(tmrid) tmrid,sort,sel,
  751. CASE times
  752. WHEN 1 THEN
  753. '第一班'
  754. WHEN 2 THEN
  755. '第二班'
  756. WHEN 3 THEN
  757. '第三班'
  758. WHEN 4 THEN
  759. '第四班'
  760. END timesstr,issplit,
  761. times,display,begintime,
  762. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  763. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id),2),0) sumweight ,
  764. ifnull((select maxstirfeed from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),100000) maxweight,
  765. TRIM(ftid) ftid,
  766. TRIM(id) id,
  767. TRIM(id) lppid,
  768. TRIM(pastureid) pastureid
  769. FROM
  770. lpplan
  771. WHERE pastureid = ? and times <= (SELECT sysopt.inforvalue FROM sysopt
  772. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  773. ORDER BY lpplan.sort`, pastureid).Query().List()
  774. if err != nil {
  775. log.Println("GetSpillage-error-3: ", err)
  776. appG.Response(http.StatusOK, e.ERROR, nil)
  777. return
  778. }
  779. arrList, err := tx.SQL(`SELECT
  780. trim(lpplandtl1.lppid) lppid,lweight,
  781. lpplandtl1.lweight weight,lpplandtl1.sort,
  782. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  783. TRIM(lpplandtl1.fpdid) fpdid,
  784. TRIM(lpplandtl1.barid) barid,
  785. TRIM(lpplandtl1.id) id,
  786. TRIM(lpplandtl1.pastureid) pastureid,
  787. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  788. lpplandtl1.fttype,
  789. trim(lpplandtl1.tmrid) tmrid,
  790. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  791. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  792. fpdetail.ptid,
  793. fpdetail.ptsid,
  794. fpdetail.times,
  795. fpdetail.cowcount,
  796. fpdetail.ccountradio
  797. FROM
  798. lpplandtl1
  799. inner join fpdetail
  800. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  801. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0
  802. ORDER BY lpplandtl1.sort`, pastureid).QueryString() //获取
  803. if err != nil {
  804. log.Println("GetSpillage-error-2: ", err)
  805. appG.Response(http.StatusOK, e.ERROR, nil)
  806. return
  807. }
  808. for _, query := range lppList {
  809. list := make([]map[string]string, 0)
  810. for _, arr := range arrList {
  811. if query["lppid"].(string) == arr["lppid"] {
  812. list = append(list, arr)
  813. }
  814. }
  815. query["arrList"] = list
  816. }
  817. data["list"] = lppList
  818. data["total"] = len(lppList)
  819. }
  820. appG.Response(http.StatusOK, e.SUCCESS, data)
  821. }