spillage.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package api
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "time"
  7. "../../pkg/app"
  8. "../../pkg/e"
  9. "../../pkg/logging"
  10. "../../pkg/setting"
  11. "../../routers/restful"
  12. "github.com/Anderson-Lu/gofasion/gofasion"
  13. "github.com/gin-gonic/gin"
  14. "github.com/xormplus/xorm"
  15. )
  16. type feedtemplet struct {
  17. Id int64 `xorm:"id"`
  18. Ccname string `xorm:"ccname"`
  19. Cname string `xorm:"tname"`
  20. }
  21. type tmrStruct struct {
  22. Id int64 `xorm:"id"`
  23. Tname string `xorm:"tname"`
  24. Maxstirfeed float64 `xorm:"maxstirfeed"`
  25. }
  26. type lpplanStruct struct {
  27. Id int64 `xorm:"id"`
  28. Tmrname string `xorm:"tmrname"`
  29. Tmrid int64 `xorm:"tmrid"`
  30. Sort int64 `xorm:"sort"`
  31. Sel int64 `xorm:"sel"`
  32. Times int64 `xorm:"times"`
  33. Ftname string `xorm:"ftname"`
  34. Ftid int64 `xorm:"ftid"`
  35. Maxweight float64 `xorm:"maxweight"`
  36. Sumweight float64 `xorm:"sumweight"`
  37. Sumcowcount int64 `xorm:"sumcowcount"`
  38. Pastureid string `xorm:"pastureid"`
  39. }
  40. type sysopt struct {
  41. Inforvalue int `xorm:"inforvalue"`
  42. }
  43. type lpplanQueryInfo struct {
  44. Pastureid int64 `xorm:"pastureid"`
  45. Lppid int64 `xorm:"id"`
  46. Maxweight float64 `xorm:"maxweight"`
  47. Lweight float64 `xorm:"lweight"`
  48. Lweighthis float64 `xorm:"lweighthis"`
  49. Barid int64 `xorm:"barid"`
  50. Barname string `xorm:"barname"`
  51. Sort int64 `xorm:"sort"`
  52. Tmrid int64 `xorm:"tmrid"`
  53. Tmrname string `xorm:"tmrname"`
  54. Cowcount int64 `xorm:"cowcount"`
  55. Ccountradio float64 `xorm:"ccountradio"`
  56. Background string `xorm:"background"`
  57. Times int64 `xorm:"times"`
  58. Ftid int64 `xorm:"ftid"`
  59. }
  60. type fpdetailQueryInfo struct {
  61. Fpdid int64 `xorm:"fpdid"`
  62. Ptsuse float64 `xorm:"ptsuse"`
  63. Ptuse float64 `xorm:"ptuse"`
  64. Weight float64 `xorm:"weight"`
  65. Barid int64 `xorm:"barid"`
  66. Barname string `xorm:"barname"`
  67. Cowcount int64 `xorm:"cowcount"`
  68. Ccountradio float64 `xorm:"ccountradio"`
  69. }
  70. type lpplandtl1 struct {
  71. Pastureid int64 `xorm:"pastureid"`
  72. Lppid int64 `xorm:"lppid"`
  73. Barid int64 `xorm:"barid"`
  74. Barname string `xorm:"barname"`
  75. Fpdid int64 `xorm:"fpdid"`
  76. Fttype int64 `xorm:"fttype"`
  77. Lweight float64 `xorm:"lweight"`
  78. Sort int64 `xorm:"sort"`
  79. Tmrid int64 `xorm:"tmrid"`
  80. Tmrname string `xorm:"tmrname"`
  81. Cowcount int64 `xorm:"cowcount"`
  82. Ccountradio float64 `xorm:"ccountradio"`
  83. Background string `xorm:"background"`
  84. Lweighthis float64 `xorm:"lweighthis"`
  85. }
  86. //撒料计划 自动生成
  87. func Autogeneration(c *gin.Context) {
  88. appG := app.Gin{C: c}
  89. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  90. fsion := gofasion.NewFasion(string(dataByte))
  91. pastureid := fsion.Get("pastureid").ValueStr()
  92. typeIN := fsion.Get("type").ValueInt64()
  93. tx := restful.Engine.NewSession()
  94. feedtempletlist, err := getfeedtemplet(pastureid, tx)
  95. if err != nil {
  96. appG.Response(http.StatusOK, e.ERROR, false)
  97. return
  98. }
  99. tmrlist, err := gettmr(pastureid, tx)
  100. if err != nil {
  101. appG.Response(http.StatusOK, e.ERROR, false)
  102. return
  103. }
  104. times, err := gettimes(pastureid, tx)
  105. if err != nil {
  106. appG.Response(http.StatusOK, e.ERROR, false)
  107. return
  108. }
  109. lpplanlist, err := getLpplanlist(pastureid, tx)
  110. if err != nil {
  111. appG.Response(http.StatusOK, e.ERROR, false)
  112. return
  113. }
  114. // var wg sync.WaitGroup
  115. // wg.Add(len(feedtempletlist))
  116. for _, tem := range feedtempletlist {
  117. // go func(tem *feedtemplet) {
  118. // defer wg.Done()
  119. count, err := getLpplanCount(tx)
  120. if err != nil {
  121. appG.Response(http.StatusOK, e.ERROR, false)
  122. return
  123. }
  124. for _, tmr := range tmrlist {
  125. for i := 1; i <= times; i++ {
  126. exist := false
  127. for _, lpp := range lpplanlist {
  128. // session.Where("pastureid = ? ", pastureid)
  129. // session.Where("times = ? ", times)
  130. // session.Where("tmrid = ? ", tmrid)
  131. // session.Where("ftid = ? ", ftid)
  132. if lpp.Tmrid == tmr.Id && lpp.Times == int64(i) && lpp.Ftid == tem.Id {
  133. exist = true
  134. break
  135. }
  136. }
  137. // exist, err := getLpplanExist(tmr.Id, tem.Id, i, pastureid, tx)
  138. // if err != nil {
  139. // appG.Response(http.StatusOK, e.ERROR, false)
  140. // return
  141. // }
  142. if !exist {
  143. ids, err := setting.SnowIds.NextId()
  144. if err != nil {
  145. ids = time.Now().UnixNano()
  146. logging.Info("create SnowIds err", err)
  147. }
  148. count++
  149. lpplan := &lpplanStruct{
  150. Id: ids,
  151. Tmrname: tmr.Tname,
  152. Tmrid: tmr.Id,
  153. Sort: count,
  154. Sel: 1,
  155. Times: int64(i),
  156. Ftname: tem.Cname,
  157. Ftid: tem.Id,
  158. Maxweight: tmr.Maxstirfeed,
  159. Sumweight: 0,
  160. Sumcowcount: 0,
  161. Pastureid: pastureid,
  162. }
  163. err = addLpplan(lpplan, tx)
  164. if err != nil {
  165. appG.Response(http.StatusOK, e.ERROR, false)
  166. return
  167. }
  168. }
  169. }
  170. }
  171. // }(tem)
  172. }
  173. go func(typeIN int64, pastureid string, tx *xorm.Session) {
  174. err = autogeneration(typeIN, pastureid, tx)
  175. if err != nil {
  176. appG.Response(http.StatusOK, e.ERROR, false)
  177. return
  178. }
  179. }(typeIN, pastureid, tx)
  180. appG.Response(http.StatusOK, e.SUCCESS, true)
  181. }
  182. //查看配方模板
  183. func getfeedtemplet(pastureid string, tx *xorm.Session) ([]*feedtemplet, error) {
  184. data := make([]*feedtemplet, 0)
  185. err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
  186. if err != nil {
  187. log.Println("getfeedtemplet-error", err)
  188. return nil, err
  189. }
  190. return data, nil
  191. }
  192. //查看tmr设备
  193. func gettmr(pastureid string, tx *xorm.Session) ([]*tmrStruct, error) {
  194. data := make([]*tmrStruct, 0)
  195. session := tx.Table("tmr")
  196. session.Select("TRIM(id) id,tname,maxstirfeed")
  197. session.Where("pastureid = ? ", pastureid)
  198. session.Where("enable = 1")
  199. session.In("tclassid", "1,2,3")
  200. err := session.Find(&data)
  201. if err != nil {
  202. log.Println("gettmr-error", err)
  203. return nil, err
  204. }
  205. return data, nil
  206. }
  207. //判断计划种是否有该车次
  208. func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
  209. session := tx.Table("lpplan")
  210. session.Where("pastureid = ? ", pastureid)
  211. session.Where("times = ? ", times)
  212. session.Where("tmrid = ? ", tmrid)
  213. session.Where("ftid = ? ", ftid)
  214. exist, err := session.Exist()
  215. if err != nil {
  216. log.Println("getLpplanExist-error", err)
  217. return false, err
  218. }
  219. return exist, nil
  220. }
  221. type lpplanList struct {
  222. Tmrid int64 `xorm:"tmrid"`
  223. Times int64 `xorm:"times"`
  224. Ftid int64 `xorm:"ftid"`
  225. }
  226. func getLpplanlist(patsureid string, tx *xorm.Session) ([]*lpplanList, error) {
  227. session := tx.Table("lpplan")
  228. session.Select("times,tmrid,ftid")
  229. session.Where("pastureid = ? ", patsureid)
  230. data := make([]*lpplanList, 0)
  231. err := session.Find(&data)
  232. if err != nil {
  233. log.Println("getLpplanlist-error", err)
  234. return nil, err
  235. }
  236. return data, nil
  237. }
  238. // 添加车次计划
  239. func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
  240. _, err := tx.Table("lpplan").Insert(lpplan)
  241. if err != nil {
  242. log.Println("addLpplan-error", err)
  243. return err
  244. }
  245. return nil
  246. }
  247. //获取车次数量
  248. func getLpplanCount(tx *xorm.Session) (int64, error) {
  249. count, err := tx.Table("lpplan").Count()
  250. if err != nil {
  251. log.Println("getLpplanCount-error", err)
  252. return 0, err
  253. }
  254. return count, err
  255. }
  256. //获取班次信息
  257. func gettimes(pastureid string, tx *xorm.Session) (int, error) {
  258. session := tx.Table("sysopt")
  259. session.Where("pastureid = ? ", pastureid)
  260. session.Where("inforname = 'times' ")
  261. data := new(sysopt)
  262. _, err := session.Get(data)
  263. if err != nil {
  264. log.Println("gettimes-error", err)
  265. return 0, err
  266. }
  267. return data.Inforvalue, nil
  268. }
  269. func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
  270. tx.Begin()
  271. if typeIN == 1 {
  272. _, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
  273. if err != nil {
  274. log.Println("autogeneration-error-1", err)
  275. tx.Rollback()
  276. return err
  277. }
  278. _, err = tx.SQL(`update fpdetail
  279. set ptuse = 0,ptsuse=0
  280. WHERE pastureid = ?`, pastureid).Execute()
  281. if err != nil {
  282. log.Println("autogeneration-error-2", err)
  283. tx.Rollback()
  284. return err
  285. }
  286. }
  287. lpplansession := tx.SQL(`select
  288. t1.sort,t1.tmrid,t1.tmrname,
  289. t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight from lpplan t1
  290. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  291. where t1.pastureid = ? and t1.maxweight > ifnull(t2.lweight,0) `, pastureid, pastureid)
  292. lpplanlist := make([]*lpplanQueryInfo, 0)
  293. err := lpplansession.Find(&lpplanlist)
  294. if err != nil {
  295. log.Println("autogeneration-error-3", err)
  296. tx.Rollback()
  297. return err
  298. }
  299. for _, lpplan := range lpplanlist {
  300. fpdetailsession := tx.Table("fpdetail").Alias("fpd")
  301. fpdetailsession.Select("fpd.barid,fpd.barname,fpd.id as fpdid ,fpd.weight as weight,ifnull(fpd.ptuse,0) as ptuse,ifnull(fpd.ptsuse,0) as ptsuse,fpd.barid,fpd.barname,fpd.cowcount,fpd.ccountradio")
  302. fpdetailsession.Join("inner", []string{"lpplan", "lpp"},
  303. "lpp.pastureid = fpd.pastureid ").And("lpp.ftid = fpd.ptid").And(
  304. "lpp.times = fpd.times").And("lpp.id = ?", lpplan.Lppid)
  305. fpdetailsession.Where("fpd.pastureid = ? ", pastureid)
  306. fpdetailsession.GroupBy("fpd.id")
  307. fpdetaillist := make([]*fpdetailQueryInfo, 0)
  308. err = fpdetailsession.Find(&fpdetaillist)
  309. if err != nil {
  310. log.Println("autogeneration-error-4", err)
  311. tx.Rollback()
  312. return err
  313. }
  314. for _, fpdetail := range fpdetaillist {
  315. // //剩余料
  316. weight := fpdetail.Weight*(1-fpdetail.Ptsuse) - fpdetail.Ptuse
  317. if lpplan.Maxweight <= lpplan.Lweight || weight == 0 {
  318. continue
  319. }
  320. var insertlweight float64
  321. if lpplan.Maxweight-lpplan.Lweight > weight {
  322. insertlweight = weight
  323. } else {
  324. insertlweight = lpplan.Maxweight - lpplan.Lweight
  325. }
  326. lpplan.Lweight += insertlweight
  327. lpplan.Lweighthis = insertlweight
  328. fpdetail.Ptuse += insertlweight
  329. // updatefpd := tx.Table("fpdetail").Alias("fpd")
  330. // updatefpd.Join("INNER", []string{"lpplan", "lpp"}, "lpp.pastureid = fpd.pastureid")
  331. // updatefpd.Where("lpp.ftid = fpd.ptid").And("lpp.times = fpd.times")
  332. // _, err = updatefpd.Update(fpdetail)
  333. _, err = tx.SQL(` update fpdetail fpd join lpplan lpp on lpp.pastureid = fpd.pastureid and lpp.ftid = fpd.ptid and lpp.times = fpd.times
  334. set fpd.ptuse = ? where fpd.id = ? `, fpdetail.Ptuse, fpdetail.Fpdid).Execute()
  335. if err != nil {
  336. log.Println("autogeneration-error-5", err)
  337. tx.Rollback()
  338. return err
  339. }
  340. lpplandtlExecute, err := tx.SQL(`select id from lpplandtl1 where pastureid= ?
  341. and lppid = ? and fpdid =? and fttype = ? `, pastureid, lpplan.Lppid, fpdetail.Fpdid, 1).Exist()
  342. if err != nil {
  343. log.Println("autogeneration-error-6", err)
  344. tx.Rollback()
  345. return err
  346. }
  347. if lpplandtlExecute {
  348. lpplandtl1query := new(lpplandtl1)
  349. query := tx.Table("lpplandtl1")
  350. query.Where("pastureid = ?", pastureid)
  351. query.Where("lppid = ?", lpplan.Lppid)
  352. query.Where("fpdid = ?", fpdetail.Fpdid)
  353. query.Where("fttype = 1")
  354. _, err = query.Get(lpplandtl1query)
  355. if err != nil {
  356. log.Println("autogeneration-error-7", err)
  357. tx.Rollback()
  358. return err
  359. }
  360. upLpplandtl1 := tx.Table("lpplandtl1")
  361. data := &lpplandtl1{
  362. Pastureid: lpplan.Pastureid,
  363. Lppid: lpplan.Lppid,
  364. Barid: fpdetail.Barid,
  365. Barname: fpdetail.Barname,
  366. Fpdid: fpdetail.Fpdid,
  367. Fttype: 1,
  368. Lweight: lpplandtl1query.Lweight + insertlweight,
  369. Sort: lpplan.Sort,
  370. Tmrid: lpplan.Tmrid,
  371. Tmrname: lpplan.Tmrname,
  372. Cowcount: fpdetail.Cowcount,
  373. Ccountradio: fpdetail.Ccountradio,
  374. // Background: lpplan.Background,
  375. Lweighthis: insertlweight,
  376. }
  377. upLpplandtl1.Where("pastureid = ?", pastureid)
  378. upLpplandtl1.Where("lppid = ?", lpplan.Lppid)
  379. upLpplandtl1.Where("fpdid = ?", fpdetail.Fpdid)
  380. upLpplandtl1.Where("fttype = 1")
  381. _, err = upLpplandtl1.Update(data)
  382. if err != nil {
  383. log.Println("autogeneration-error-8", err)
  384. tx.Rollback()
  385. return err
  386. }
  387. } else {
  388. data := &lpplandtl1{
  389. Pastureid: lpplan.Pastureid,
  390. Lppid: lpplan.Lppid,
  391. Barid: lpplan.Barid,
  392. Barname: lpplan.Barname,
  393. Fpdid: fpdetail.Fpdid,
  394. Fttype: 1,
  395. Lweight: insertlweight,
  396. Sort: lpplan.Sort,
  397. Tmrid: lpplan.Tmrid,
  398. Tmrname: lpplan.Tmrname,
  399. Cowcount: lpplan.Cowcount,
  400. Ccountradio: lpplan.Ccountradio,
  401. Background: lpplan.Background,
  402. Lweighthis: insertlweight,
  403. }
  404. addLpplandtl1 := tx.Table("lpplandtl1")
  405. _, err = addLpplandtl1.Insert(data)
  406. if err != nil {
  407. log.Println("autogeneration-error-9", err)
  408. tx.Rollback()
  409. return err
  410. }
  411. }
  412. }
  413. }
  414. err = tx.Commit()
  415. if err != nil {
  416. log.Println("autogeneration-error-10", err)
  417. return err
  418. }
  419. return nil
  420. }