123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package Device
- import (
- "Cold_Api/conf"
- "Cold_Api/models/Admin"
- "fmt"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "strings"
- "time"
- )
- // 模板
- type DeviceWarning struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_Bind string `orm:"size(256);null"` //设备绑定 Uid
- //T_class int `orm:"size(200);null"` // 分类
- //T_img string `orm:"size(256);null"` // 图片
- T_Id int `orm:"size(200);null"` // 传感器 ID
- T_Name string `orm:"size(256);null"` // 传感器 温度探头1
- T_Msid int `orm:"size(256);null"` //
- T_T float32 `orm:"size(20);null"` // 湿度下限
- T_RH float32 `orm:"size(20);null"` // 湿度上限
- T_Title string `orm:"size(256);null"` // 报警标题 温度超上限报警
- T_Addr string `orm:"size(256);null"` // 地址 车载环境监测仪
- T_Remark string `orm:"size(256);null"` // 备注
- T_Ut time.Time `orm:"type(timestamp);null;"` // 采集时间
- T_Text string `orm:"size(256);null"` // 备注
- T_Log string `orm:"type(text);null"` // 处理日志
- T_State int `orm:"size(2);1"` // 0 删除 1 已处理 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 DeviceWarning_R struct {
- T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_Id int // 传感器 ID
- T_Name string // 传感器 温度探头1
- T_T float32 // 湿度下限
- T_RH float32 // 湿度上限
- T_Title string // 报警标题 温度超上限报警
- T_Addr string // 地址 车载环境监测仪
- T_Remark string // 备注
- T_Ut time.Time // 采集时间
- T_Text string // 备注
- T_Log []string // 处理日志
- T_State int // 0 删除 1 未处理 2 已处理
- }
- func (t *DeviceWarning) TableName() string {
- return "DeviceWarning" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(DeviceWarning))
- orm.Debug = true
- }
- // ---------------- 特殊方法 -------------------
- func DeviceWarningToDeviceWarning_R(t DeviceWarning) (r DeviceWarning_R) {
- r.T_sn = t.T_sn
- r.T_Id = t.T_Id
- r.T_Name = t.T_Name
- r.T_T = t.T_T
- r.T_RH = t.T_RH
- r.T_Title = t.T_Title
- r.T_Addr = t.T_Addr
- r.T_Remark = t.T_Remark
- r.T_Ut = t.T_Ut
- r.T_Text = t.T_Text
- r.T_State = t.T_State
- r.T_Log = strings.Split(t.T_Log, "\n")
- return r
- }
- // 获取 ById
- func Read_DeviceWarning_ById(id int) (r DeviceWarning) {
- o := orm.NewOrm()
- r = DeviceWarning{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- return r
- }
- // 添加
- func Add_DeviceWarning(m DeviceWarning) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- //if err != nil {
- // fmt.Println(err)
- //}
- return id, err
- }
- // 修改
- func Update_DeviceWarning(r DeviceWarning, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&r, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- return true
- }
- return false
- }
- // 修改
- func Update_DeviceWarning_ById(m DeviceWarning) (err error) {
- o := orm.NewOrm()
- v := DeviceWarning{Id: m.Id}
- // ascertain id exists in the database
- if err = o.Read(&v); err == nil {
- var num int64
- m.T_State = 2
- if num, err = o.Update(&m, "T_Text"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- }
- }
- return
- }
- // 修改
- func Update_DeviceWarning_Delete(m DeviceWarning) (err error) {
- o := orm.NewOrm()
- v := DeviceWarning{Id: m.Id}
- // ascertain id exists in the database
- if err = o.Read(&v); err == nil {
- v.T_State = 0
- if num, err := o.Update(&v, "T_State"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- }
- }
- return
- }
- // 获取列表
- func Read_DeviceWarning_1(user_ Admin.Admin, page int, T_sn string, T_Name string, Time_start_ string, Time_end_ string) (r []DeviceWarning, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceWarning))
- var offset int64
- var page_z int64
- page_z = int64(conf.Page_size)
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1)) * page_z
- }
- if page == 9999 {
- page_z = 9999
- offset = 0
- }
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- cond := orm.NewCondition()
- cond1 := cond.And("T_State__gt", 0)
- if len(T_Bind) > 1 {
- cond1 = cond.AndCond(cond1).And("T_Bind__icontains", T_Bind)
- }
- if len(T_Name) > 1 {
- cond1 = cond.AndCond(cond1).AndCond(cond.Or("T_Name__icontains", T_Name).Or("T_Title__icontains", T_Name).Or("T_Addr__icontains", T_Name).Or("T_sn__icontains", T_Name))
- }
- if len(T_sn) > 1 {
- cond1 = cond.AndCond(cond1).And("T_sn__icontains", T_sn)
- }
- if len(Time_start_) > 1 {
- cond1 = cond.AndCond(cond1).And("T_Ut__gte", Time_start_)
- }
- if len(Time_end_) > 1 {
- cond1 = cond.AndCond(cond1).And("T_Ut__lte", Time_end_)
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- return r, cnt
- //if len(Time_start_) > 1 && len(Time_end_) > 1 {
- // fmt.Println(Time_start_, Time_end_)
- // qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Ut__lte", Time_end_).Filter("T_Ut__gte", Time_start_).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).OrderBy("-Id").All(&r)
- // cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Ut__lte", Time_end_).Filter("T_Ut__gte", Time_start_).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).Count()
- // return r, cnt
- //}
- //
- //if len(Time_start_) > 1 {
- // qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Ut__gte", Time_start_).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).OrderBy("-Id").All(&r)
- // cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Ut__gte", Time_start_).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).Count()
- // return r, cnt
- //} else if len(Time_end_) > 1 {
- // qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Ut__lte", Time_end_).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).OrderBy("-Id").All(&r)
- // cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Ut__lte", Time_end_).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).Count()
- // return r, cnt
- //}
- //
- //qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).OrderBy("-Id").All(&r)
- //cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Name__icontains", T_Name).Filter("T_Addr__icontains", T_Name).Filter("T_sn__icontains", T_Name).Filter("T_Title__icontains", T_Name).Count()
- //
- //return r, cnt
- }
- // 获取列表
- func Read_DeviceWarning(user_ Admin.Admin, T_sn string, T_id string, T_title string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int) (r []DeviceWarning, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceWarning))
- var offset int64
- if page_z == 0 {
- page_z = conf.Page_size
- }
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- cond := orm.NewCondition()
- cond1 := cond.And("T_State__gt", 0)
- if len(T_Bind) > 0 {
- cond1 = cond1.And("T_Bind__icontains", T_Bind)
- }
- if len(T_title) > 0 {
- cond1 = cond1.And("T_Title", T_title)
- }
- if len(T_sn) > 0 {
- cond1 = cond1.And("T_sn", T_sn)
- }
- if len(T_id) > 0 {
- cond1 = cond1.And("T_Id", T_id)
- }
- if len(Time_start_) > 0 {
- cond1 = cond1.And("T_Ut__gte", Time_start_)
- }
- if len(Time_end_) > 0 {
- cond1 = cond1.And("T_Ut__lte", Time_end_)
- }
- //不填或0:所有 1:已处理 2:未处理
- if T_handle == 1 {
- cond1 = cond1.And("T_Text__isnull", true)
- }
- if T_handle == 2 {
- cond1 = cond1.And("T_Text__isnull", false).And("T_State", 2)
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- return r, cnt
- }
- //
- func Read_Warning_ALL_T_Bind_TIME_1d_Count(user_ Admin.Admin, T_sn string) int {
- o := orm.NewOrm()
- var maps_z []orm2.ParamsList
- now := time.Now()
- // 一天前
- //d, _ := time.ParseDuration("-24h")
- //now = now.Add(d)
- timeStr := now.Format("2006-01-02") + " 00:00:00"
- timeStr1 := now.Format("2006-01-02") + " 23:59:59"
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- sql := "SELECT * FROM DeviceWarning WHERE t__title NOT LIKE '%恢复%' AND t__title NOT LIKE '%任务%' AND t__ut > '" + timeStr + "' AND t__ut < '" + timeStr1 + "' AND t__bind LIKE '%" + T_Bind + "%' AND t_sn LIKE '%" + T_sn + "%' group by t_sn"
- fmt.Println(sql)
- _, err := o.Raw(sql).ValuesList(&maps_z)
- if err != nil {
- return 0
- }
- if len(maps_z) == 0 {
- return 0
- }
- //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
- return len(maps_z)
- }
|