package Warning import ( "Cold_Api/controllers/lib" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "time" ) var ( WarningRateDay = "Day" WarningRateMonth = "Month" ) // 报警率 type WarningRate struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uuid string `orm:"index;size(64);null"` // 用户uuid T_date string `orm:"size(64);null"` // 记薪日期,2023-03-01 T_device_num int64 `orm:"size(20);default(0)"` // 探头数量 T_warning_num int64 `orm:"size(20);default(0)"` // 报警数量 T_type string `orm:"size(20);default(0)"` // 类型 Day-每日 Month-每月 T_rate float32 `orm:"digits(12);decimals(2)"` // 报警率 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } type WarningRate_R struct { Id int T_uuid string // 用户uuid T_date string // 记薪日期,2023-03-01 T_device_num int64 // 探头数量 T_warning_num int64 // 报警数量 Value float32 // 报警率 } type WarningRate_User struct { T_uuid string // 用户uuid T_name string // 用户名 } func WarningRateToWarningRate_R(t WarningRate) (r WarningRate_R) { r.Id = t.Id r.T_uuid = t.T_uuid r.T_date = t.T_date r.T_device_num = t.T_device_num r.T_warning_num = t.T_warning_num r.Value = t.T_rate return r } func (t *WarningRate) TableName() string { return "warning_rate" } func init() { //注册模型 orm.RegisterModel(new(WarningRate)) } func Add_WarningRate(m WarningRate) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } func Read_WarningRate_List(T_uuid, T_type string, startTime, endTime string) (r []map[string]interface{}) { o := orm.NewOrm() qs := o.QueryTable(new(WarningRate)) cond := orm.NewCondition() var maps []WarningRate cond = cond.And("T_uuid", T_uuid).And("T_type", T_type) if len(startTime) > 0 { cond = cond.And("T_date__gte", startTime) } if len(endTime) > 0 { cond = cond.And("T_date__lte", endTime) } _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_date").All(&maps) if err != nil { logs.Error(lib.FuncName(), err) } for _, v := range maps { rate := map[string]interface{}{ "T_uuid": v.T_uuid, "T_date": v.T_date, "T_device_num": v.T_device_num, "T_warning_num": v.T_warning_num, "value": v.T_rate, } r = append(r, rate) } return r } // 获取每月平均值 func Read_WarningRateAvg_Month(startTime, endTime string) (r []WarningRate_R) { o := orm.NewOrm() 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 + "'" + " GROUP BY t_uuid" _, err := o.Raw(sql).QueryRows(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } // 获取昨天的设备报警率 func Read_WarningRate_Yesterday(T_uuid string) float32 { o := orm.NewOrm() qs := o.QueryTable(new(WarningRate)) var r WarningRate T_date := time.Now().AddDate(0, -1, 0).Format("2006-01-02") err := qs.Filter("T_uuid", T_uuid).Filter("T_date", T_date).Filter("T_type", WarningRateDay).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return r.T_rate }