package Device import ( "Cold_Api/conf" "Cold_Api/models/Admin" "fmt" "github.com/beego/beego/v2/adapter/orm" _ "github.com/go-sql-driver/mysql" "time" ) type DeviceClass struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uuid string `orm:"size(256);null"` // T_name string `orm:"size(256);null"` // 分类 T_Notice_wx string `orm:"type(text);null"` //微信公众号 appid/名字| T_Notice_phone string `orm:"type(text);null"` //手机 1111111| T_Notice_message string `orm:"type(text);null"` //短信 1111111| T_Notice_mailbox string `orm:"type(text);null"` //邮箱 1111111| T_State int `orm:"size(2);1"` // 0 删除 1 正常 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 保存时都会对时间自动更新 } func (t *DeviceClass) TableName() string { return "DeviceClass" // 数据库名称 // ************** 替换 DesignClass ************** } func init() { //注册模型 orm.RegisterModel(new(DeviceClass)) } // ---------------- 特殊方法 ------------------- // 获取 ById func Read_Class_ById(id int) (r DeviceClass, err error) { o := orm.NewOrm() r = DeviceClass{Id: id} err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { fmt.Println(err) } return r,err } // 添加 func Add_Class(m DeviceClass) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { fmt.Println(err) } return } // 修改 func Update_Class_ById(m DeviceClass) (err error) { o := orm.NewOrm() v := DeviceClass{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { var num int64 if num, err = o.Update(&m, "T_name","T_Notice_wx","T_Notice_phone","T_Notice_message","T_Notice_mailbox"); err == nil { fmt.Println("Number of records updated in database:", num) } } return } // 删除 func Delete_Class_ById(id int) (DeviceClass) { o := orm.NewOrm() v := DeviceClass{Id: id} // ascertain id exists in the database if err := o.Read(&v); err == nil { var num int64 v.T_State = 0 if num, err = o.Update(&v, "T_State"); err == nil { fmt.Println("Number of records updated in database:", num) } } return v } // 删除 func Delete_Class_ByUuid_All(user_ Admin.Admin) { o := orm.NewOrm() qs := o.QueryTable(new(DeviceClass)) var r []DeviceClass qs.Filter("T_State", 1).All(&r) for _,v := range r{ v.T_State = 0 o.Update(&v, "T_State"); } } // 获取全部 func Read_Class_All_1() (r []DeviceClass) { o := orm.NewOrm() qs := o.QueryTable(new(DeviceClass)) qs.Filter("T_State", 1).All(&r) return r } // 获取列表 func Read_DeviceClass_ALL_1(T_uuid string,page int,T_name string) (r []DeviceClass,cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceClass)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * conf.Page_size) } qs.Limit(conf.Page_size, offset).Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).OrderBy("-Id").Filter("T_State", 1).All(&r) cnt, _ = qs.Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count() return r,cnt } // 获取列表 func Read_DeviceClass_ALL_T_uuid_1(T_uuid string) (r []DeviceClass) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceClass)) qs.Filter("T_uuid", T_uuid).OrderBy("-Id").Filter("T_State", 1).All(&r) return r }