123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package Device
- import (
- "fmt"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 模板
- type Device struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_pid int `orm:"index;size(256);"` // Account.Company 绑定公司
- T_sn string `orm:"size(256);"` // 设备序列号
- T_tab string `orm:"size(256);"` // 设备标签
- 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 保存时都会对时间自动更新
- }
- type Device_R struct {
- T_tab string // 设备标签
- T_sn []string // 设备序列号
- }
- func (t *Device) TableName() string {
- return "device" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(Device))
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_Device_ById(id int) (r Device, err error) {
- o := orm.NewOrm()
- r = Device{Id: id}
- err = o.Read(&r)
- if err != nil {
- fmt.Println(err)
- }
- return r, err
- }
- // 添加
- func Add_Device(m Device) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- fmt.Println(err)
- }
- return id, err
- }
- // 修改
- func Update_Device(r Device, 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_Device(m Device) 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_Device_BT_pid(T_pid int) []Device_R {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- var r []Device
- qs.Filter("T_pid", T_pid).OrderBy("T_tab").All(&r)
- var Device_list map[string][]string // 泛型
- Device_list = make(map[string][]string)
- for _, v := range r {
- Device_list[v.T_tab] = append(Device_list[v.T_tab], v.T_sn)
- }
- Device_R_list := []Device_R{}
- for k, v := range Device_list {
- Device_R_list = append(Device_R_list, Device_R{T_tab: k, T_sn: v})
- }
- return Device_R_list
- }
|