123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package Device
- import (
- "fmt"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 控制组
- type ControlGroup struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_pid int `orm:"index;size(256);"` // Account.Company 绑定公司
- T_name string `orm:"size(256);"` // 标题
- T_text string `orm:"type(text);default('')"` // 设备控制json
- /*T_text json
- [{
- "T_sn": "xxxxxxxxx",
- "T_name": "一楼空调01",
- "T_ProductID": "lkhqwZjQ",
- "d_switch": 1
- },
- {
- "T_sn": "xxxxxxxxx",
- "T_name": "一楼空调02",
- "T_ProductID": "lkhqwZjQ",
- "d_switch": 1
- }
- ]
- */
- CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- func (t *ControlGroup) TableName() string {
- return "device_sensor" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(ControlGroup))
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_ControlGroup_ById(id int) (r ControlGroup, err error) {
- o := orm.NewOrm()
- r = ControlGroup{Id: id}
- err = o.Read(&r)
- if err != nil {
- fmt.Println(err)
- }
- return r, err
- }
- // 添加
- func Add_ControlGroup(m ControlGroup) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- fmt.Println(err)
- }
- return id, err
- }
- // 修改
- func Update_ControlGroup(r ControlGroup, 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 Delete_ControlGroup(m ControlGroup) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&m); err == nil {
- fmt.Println("Number of records deleted in database:", num)
- return true
- }
- return false
- }
- // 获取列表
- func Read_ControlGroup_ByT_pid(T_pid int) []ControlGroup {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ControlGroup))
- var r []ControlGroup
- qs.Filter("T_pid", T_pid).OrderBy("Id").All(&r)
- return r
- }
|