WarningRate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package Warning
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "github.com/beego/beego/v2/adapter/orm"
  5. orm2 "github.com/beego/beego/v2/client/orm"
  6. "github.com/beego/beego/v2/core/logs"
  7. "time"
  8. )
  9. var (
  10. WarningRateDay = "Day"
  11. WarningRateMonth = "Month"
  12. )
  13. // 报警率
  14. type WarningRate struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_uuid string `orm:"index;size(64);null"` // 用户uuid
  17. T_date string `orm:"size(64);null"` // 记薪日期,2023-03-01
  18. T_device_num int64 `orm:"size(20);default(0)"` // 探头数量
  19. T_warning_num int64 `orm:"size(20);default(0)"` // 报警数量
  20. T_type string `orm:"size(20);default(0)"` // 类型 Day-每日 Month-每月
  21. T_rate float32 `orm:"digits(12);decimals(2)"` // 报警率
  22. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  23. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  24. }
  25. type WarningRate_R struct {
  26. Id int
  27. T_uuid string // 用户uuid
  28. T_date string // 记薪日期,2023-03-01
  29. T_device_num int64 // 探头数量
  30. T_warning_num int64 // 报警数量
  31. Value float32 // 报警率
  32. }
  33. type WarningRate_User struct {
  34. T_uuid string // 用户uuid
  35. T_name string // 用户名
  36. }
  37. func WarningRateToWarningRate_R(t WarningRate) (r WarningRate_R) {
  38. r.Id = t.Id
  39. r.T_uuid = t.T_uuid
  40. r.T_date = t.T_date
  41. r.T_device_num = t.T_device_num
  42. r.T_warning_num = t.T_warning_num
  43. r.Value = t.T_rate
  44. return r
  45. }
  46. func (t *WarningRate) TableName() string {
  47. return "warning_rate"
  48. }
  49. func init() {
  50. //注册模型
  51. orm.RegisterModel(new(WarningRate))
  52. }
  53. func Add_WarningRate(m WarningRate) (id int64, err error) {
  54. o := orm.NewOrm()
  55. id, err = o.Insert(&m)
  56. if err != nil {
  57. logs.Error(lib.FuncName(), err)
  58. }
  59. return id, err
  60. }
  61. func Read_WarningRate_List(T_uuid, T_type string, startTime, endTime string) (r []map[string]interface{}) {
  62. o := orm.NewOrm()
  63. qs := o.QueryTable(new(WarningRate))
  64. cond := orm.NewCondition()
  65. var maps []WarningRate
  66. cond = cond.And("T_uuid", T_uuid).And("T_type", T_type)
  67. if len(startTime) > 0 {
  68. cond = cond.And("T_date__gte", startTime)
  69. }
  70. if len(endTime) > 0 {
  71. cond = cond.And("T_date__lte", endTime)
  72. }
  73. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_date").All(&maps)
  74. if err != nil {
  75. logs.Error(lib.FuncName(), err)
  76. }
  77. for _, v := range maps {
  78. rate := map[string]interface{}{
  79. "T_uuid": v.T_uuid,
  80. "T_date": v.T_date,
  81. "T_device_num": v.T_device_num,
  82. "T_warning_num": v.T_warning_num,
  83. "value": v.T_rate,
  84. }
  85. r = append(r, rate)
  86. }
  87. return r
  88. }
  89. // 获取每月平均值
  90. func Read_WarningRateAvg_Month(startTime, endTime string) (r []WarningRate_R) {
  91. o := orm.NewOrm()
  92. sql := "SELECT t_uuid, ROUND(avg(t_device_num)) as t_device_num ,ROUND(avg(t_warning_num)) as t_warning_num FROM warning_rate WHERE `t_type` = '" + WarningRateDay + "' AND T_date >= '" + startTime + "' AND T_date <= '" + endTime + "'" +
  93. " GROUP BY t_uuid"
  94. _, err := o.Raw(sql).QueryRows(&r)
  95. if err != nil {
  96. logs.Error(lib.FuncName(), err)
  97. return
  98. }
  99. return r
  100. }
  101. // 获取昨天的设备报警率
  102. func Read_WarningRate_Yesterday(T_uuid string) float32 {
  103. o := orm.NewOrm()
  104. qs := o.QueryTable(new(WarningRate))
  105. var r WarningRate
  106. T_date := time.Now().AddDate(0, -1, 0).Format("2006-01-02")
  107. err := qs.Filter("T_uuid", T_uuid).Filter("T_date", T_date).Filter("T_type", WarningRateDay).One(&r)
  108. if err != nil {
  109. logs.Error(lib.FuncName(), err)
  110. }
  111. return r.T_rate
  112. }