group.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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("1 = 1")
  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.PastureId > 0 {
  20. newSql.And("dp.pastureid = ?", req.PastureId)
  21. }
  22. if req.FeedFormulaId > 0 {
  23. newSql.And("dp.tempid = ?", req.FeedFormulaId)
  24. }
  25. if err := newSql.GroupBy("dp.mydate").OrderBy("dp.mydate").Find(&res); err != nil {
  26. return nil, err
  27. }
  28. return res, nil
  29. }
  30. // mixedFodderCorrectData 混料正确率
  31. // SELECT
  32. //SUM(de.`havebuttom`) AS "已混料操作数",de.date,
  33. //IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`feedallowratio` AND de.`actualweightminus`<>0,1,0)),0) AS "混料正确数"
  34. //FROM downloadplandtl1 de LEFT JOIN feedtemplet ft ON ft.id = de.fid
  35. //WHERE de.pastureid = 1653271339
  36. //AND ft.id =20 AND ft.ccid = ?
  37. //GROUP BY de.date ORDER BY de.date
  38. func MixedFodderCorrectData(req *models.AnalysisAccuracyRequest) ([]*models.MixedFodderCorrectDataList, error) {
  39. res := make([]*models.MixedFodderCorrectDataList, 0)
  40. newSql := restful.Engine.NewSession().Table("downloadplandtl1").Alias("de").
  41. 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"`).
  42. Join("LEFT", []string{"feedtemplet", "ft"}, "ft.id = de.fid").
  43. Where("1 = 1")
  44. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  45. newSql.And("de.date >= ? and de.date <= ?", req.StartDate, req.EndDate)
  46. }
  47. if req.CattleParentCategoryId > 0 {
  48. newSql.And("ft.ccid = ?", req.CattleParentCategoryId)
  49. }
  50. if req.PastureId > 0 {
  51. newSql.And("de.pastureid = ?", req.PastureId)
  52. }
  53. if req.FeedFormulaId > 0 {
  54. newSql.And("de.tempid = ?", req.FeedFormulaId)
  55. }
  56. if err := newSql.GroupBy("de.date").OrderBy("de.date").Find(&res); err != nil {
  57. return nil, err
  58. }
  59. return res, nil
  60. }
  61. func SprinkleFodderCorrectData(req *models.AnalysisAccuracyRequest) ([]*models.SprinkleFodderCorrectDataList, error) {
  62. res := make([]*models.SprinkleFodderCorrectDataList, 0)
  63. newSql := restful.Engine.NewSession().Table("downloadplandtl2").Alias("de").
  64. 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"`).
  65. Join("LEFT", []string{"feedtemplet", "ft"}, "ft.id = de.feedtempletid").
  66. Where("1 = 1")
  67. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  68. newSql.And("de.date >= ? and de.date <= ?", req.StartDate, req.EndDate)
  69. }
  70. if req.CattleParentCategoryId > 0 {
  71. newSql.And("ft.ccid = ?", req.CattleParentCategoryId)
  72. }
  73. if req.PastureId > 0 {
  74. newSql.And("de.pastureid = ?", req.PastureId)
  75. }
  76. if req.FeedFormulaId > 0 {
  77. newSql.And("de.tempid = ?", req.FeedFormulaId)
  78. }
  79. if err := newSql.GroupBy("de.date").OrderBy("de.date").Find(&res); err != nil {
  80. return nil, err
  81. }
  82. return res, nil
  83. }
  84. func ProcessAnalysisData(req *models.AnalysisAccuracyRequest) ([]*models.ProcessData, error) {
  85. res := make([]*models.ProcessData, 0)
  86. newSql := restful.Engine.Table("downloadplandtl1_exec").Alias("e").
  87. Select("e.id,e.begintime as exec_begin_time,e.intime as exec_end_time,e.processtime as exec_process_time,l2.begintime as l2_begin_time,l2.intime as l2_end_time,l2.processtime as l2_process_time").
  88. Join("LEFT", []string{"downloadplandtl2", "l2"}, "e.pastureid = l2.pastureid and e.pid = l2.pid").
  89. Where("1 = 1")
  90. if len(req.StartDate) > 0 && len(req.EndDate) > 0 {
  91. newSql.And("e.date >= ? and e.date <= ?", req.StartDate, req.EndDate)
  92. }
  93. if req.CattleParentCategoryId > 0 {
  94. newSql.And("l2.cowclassid = ?", req.CattleParentCategoryId)
  95. }
  96. if req.PastureId > 0 {
  97. newSql.And("e.pastureid = ?", req.PastureId)
  98. }
  99. if req.FeedFormulaId > 0 {
  100. newSql.And("l2.feedtempletid = ?", req.FeedFormulaId)
  101. }
  102. if err := newSql.GroupBy("e.date").OrderBy("e.date").Find(&res); err != nil {
  103. return nil, err
  104. }
  105. return res, nil
  106. }