group.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package module
  2. import (
  3. "tmr-watch/http/handle/restful"
  4. "tmr-watch/models"
  5. )
  6. // MixedFodderData 混料准确率和撒料准确率
  7. func MixedFodderData(req *models.AnalysisAccuracyRequest) ([]*models.MixedFodderDataList, error) {
  8. res := make([]*models.MixedFodderDataList, 0)
  9. newSql := restful.Engine.NewSession().Table("downloadedplan").Alias("dp").
  10. Select("dp.oweight,dp.lweight,dp.iweight,dp.mydate").
  11. Join("LEFT", []string{"feedtemplet", "ft"}, "dp.tempid = ft.id").
  12. Where("dp.pastureid = ?", req.PastureId)
  13. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  14. newSql.And("dp.mydate >= ? and dp.mydate <= ?", req.StartDate, req.EndDate)
  15. }
  16. if req.CattleParentCategoryId > 0 {
  17. newSql.And("ft.ccid = ?", req.CattleParentCategoryId)
  18. }
  19. if req.FeedFormulaId > 0 {
  20. newSql.And("dp.tempid = ?", req.FeedFormulaId)
  21. }
  22. if err := newSql.GroupBy("dp.mydate").OrderBy("dp.mydate").Find(&res); err != nil {
  23. return nil, err
  24. }
  25. return res, nil
  26. }
  27. // mixedFodderCorrectData 混料正确率
  28. // SELECT
  29. //SUM(de.`havebuttom`) AS "已混料操作数",de.date,
  30. //IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`feedallowratio` AND de.`actualweightminus`<>0,1,0)),0) AS "混料正确数"
  31. //FROM downloadplandtl1 de LEFT JOIN feedtemplet ft ON ft.id = de.fid
  32. //WHERE de.pastureid = 1653271339
  33. //AND ft.id =20 AND ft.ccid = ?
  34. //GROUP BY de.date ORDER BY de.date
  35. func MixedFodderCorrectData(req *models.AnalysisAccuracyRequest) ([]*models.MixedFodderCorrectDataList, error) {
  36. res := make([]*models.MixedFodderCorrectDataList, 0)
  37. newSql := restful.Engine.NewSession().Table("downloadplandtl1").Alias("de").
  38. Select(`SUM(de.havebuttom) AS "use_mixed_fodder_option_number",de.date,IFNULL(SUM(IF(ABS(de.actualweightminus-de.lweight)<=de.feedallowratio AND de.actualweightminus<>0,1,0)),0) AS "mixed_correct_number"`).
  39. Join("LEFT", []string{"feedtemplet", "ft"}, "ft.id = de.fid").
  40. Where("de.pastureid = ?", req.PastureId)
  41. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  42. newSql.And("de.date >= ? and de.date <= ?", req.StartDate, req.EndDate)
  43. }
  44. if req.CattleParentCategoryId > 0 {
  45. newSql.And("ft.ccid = ?", req.CattleParentCategoryId)
  46. }
  47. if req.FeedFormulaId > 0 {
  48. newSql.And("de.tempid = ?", req.FeedFormulaId)
  49. }
  50. if err := newSql.GroupBy("de.date").OrderBy("de.date").Find(&res); err != nil {
  51. return nil, err
  52. }
  53. return res, nil
  54. }
  55. func SprinkleFodderCorrectData(req *models.AnalysisAccuracyRequest) ([]*models.SprinkleFodderCorrectDataList, error) {
  56. res := make([]*models.SprinkleFodderCorrectDataList, 0)
  57. newSql := restful.Engine.NewSession().Table("downloadplandtl2").Alias("de").
  58. Select(`SUM(de.havebuttom) AS "use_sprinkle_option_number",de.date,IFNULL(SUM(IF(ABS(de.actualweightminus-de.lweight)<=de.allowratio AND de.actualweightminus<>0,1,0)),0) AS "sprinkle_correct_number"`).
  59. Join("LEFT", []string{"feedtemplet", "ft"}, "ft.id = de.feedtempletid").
  60. Where("de.pastureid = ?", req.PastureId)
  61. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  62. newSql.And("de.date >= ? and de.date <= ?", req.StartDate, req.EndDate)
  63. }
  64. if req.CattleParentCategoryId > 0 {
  65. newSql.And("ft.ccid = ?", req.CattleParentCategoryId)
  66. }
  67. if req.FeedFormulaId > 0 {
  68. newSql.And("de.tempid = ?", req.FeedFormulaId)
  69. }
  70. if err := newSql.GroupBy("de.date").OrderBy("de.date").Find(&res); err != nil {
  71. return nil, err
  72. }
  73. return res, nil
  74. }
  75. func ProcessAnalysisData(req *models.AnalysisAccuracyRequest) ([]*models.ProcessData, error) {
  76. res := make([]*models.ProcessData, 0)
  77. newSql := restful.Engine.Table("downloadplandtl1_exec").Alias("e").
  78. Select("e.id,e.begintime as exec_begin_time,e.intime as exec_end_time,e.processtime as exec_process_time,e.stirdelay as exec_stir_delay ,l2.begintime as l2_begin_time,l2.intime as l2_end_time,l2.processtime as l2_process_time").
  79. Join("LEFT", []string{"downloadplandtl2", "l2"}, "e.pastureid = l2.pastureid and e.pid = l2.pid").
  80. Where("e.pastureid = ?", req.PastureId)
  81. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  82. newSql.And("e.date >= ? and e.date <= ?", req.StartDate, req.EndDate)
  83. }
  84. if req.CattleParentCategoryId > 0 {
  85. newSql.And("l2.cowclassid = ?", req.CattleParentCategoryId)
  86. }
  87. if req.FeedFormulaId > 0 {
  88. newSql.And("l2.feedtempletid = ?", req.FeedFormulaId)
  89. }
  90. if err := newSql.GroupBy("e.date").OrderBy("e.date").Find(&res); err != nil {
  91. return nil, err
  92. }
  93. return res, nil
  94. }
  95. func SprinkleStatistics(req *models.SprinkleStatisticsRequest) ([]*models.SprinkleStatisticsDataList, error) {
  96. res := make([]*models.SprinkleStatisticsDataList, 0)
  97. newSql := restful.Engine.Table("downloadplandtl2").Alias("a").Select("a.fbarid,a.fname,a.intime,a.processtime,b.times").
  98. Join("LEFT", []string{"downloadedplan", "b"}, "a.pastureid = b.pastureid AND b.pid = a.pid").
  99. Where("a.pastureid = ?", req.PastureId).And("b.times > 0 ").And("a.fbarid > 0")
  100. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  101. newSql.And("a.date >= ? and a.date <= ?", req.StartDate, req.EndDate)
  102. }
  103. if req.FeedFormulaId > 0 {
  104. newSql.And("a.feedtempletid = ?", req.FeedFormulaId)
  105. }
  106. if err := newSql.GroupBy("a.fbarid ,b.intime").Find(&res); err != nil {
  107. return nil, err
  108. }
  109. return res, nil
  110. }