ControlGroup.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package Device
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/adapter/orm"
  5. _ "github.com/go-sql-driver/mysql"
  6. "time"
  7. )
  8. // 控制组
  9. type ControlGroup struct {
  10. Id int `orm:"column(ID);size(11);auto;pk"`
  11. T_pid int `orm:"index;size(256);"` // Account.Company 绑定公司
  12. T_name string `orm:"size(256);"` // 标题
  13. T_text string `orm:"type(text);default('')"` // 设备控制json
  14. /*T_text json
  15. [{
  16. "T_sn": "xxxxxxxxx",
  17. "T_name": "一楼空调01",
  18. "T_ProductID": "lkhqwZjQ",
  19. "d_switch": 1
  20. },
  21. {
  22. "T_sn": "xxxxxxxxx",
  23. "T_name": "一楼空调02",
  24. "T_ProductID": "lkhqwZjQ",
  25. "d_switch": 1
  26. }
  27. ]
  28. */
  29. CreateTime time.Time `orm:"column(create_time);type(timestamp);auto_now_add"` //auto_now_add 第一次保存时才设置时间
  30. UpdateTime time.Time `orm:"column(update_time);type(timestamp);auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  31. }
  32. func (t *ControlGroup) TableName() string {
  33. return "device_sensor" // 数据库名称 // ************** 替换 FormulaList **************
  34. }
  35. func init() {
  36. //注册模型
  37. orm.RegisterModel(new(ControlGroup))
  38. }
  39. // ---------------- 特殊方法 -------------------
  40. // 获取 ById
  41. func Read_ControlGroup_ById(id int) (r ControlGroup, err error) {
  42. o := orm.NewOrm()
  43. r = ControlGroup{Id: id}
  44. err = o.Read(&r)
  45. if err != nil {
  46. fmt.Println(err)
  47. }
  48. return r, err
  49. }
  50. // 添加
  51. func Add_ControlGroup(m ControlGroup) (id int64, err error) {
  52. o := orm.NewOrm()
  53. id, err = o.Insert(&m)
  54. if err != nil {
  55. fmt.Println(err)
  56. }
  57. return id, err
  58. }
  59. // 修改
  60. func Update_ControlGroup(r ControlGroup, cols ...string) bool {
  61. o := orm.NewOrm()
  62. if num, err := o.Update(&r, cols...); err == nil {
  63. fmt.Println("Number of records updated in database:", num)
  64. return true
  65. }
  66. return false
  67. }
  68. // 删除
  69. func Delete_ControlGroup(m ControlGroup) bool {
  70. o := orm.NewOrm()
  71. if num, err := o.Delete(&m); err == nil {
  72. fmt.Println("Number of records deleted in database:", num)
  73. return true
  74. }
  75. return false
  76. }
  77. // 获取列表
  78. func Read_ControlGroup_ByT_pid(T_pid int) []ControlGroup {
  79. o := orm.NewOrm()
  80. // 也可以直接使用 Model 结构体作为表名
  81. qs := o.QueryTable(new(ControlGroup))
  82. var r []ControlGroup
  83. qs.Filter("T_pid", T_pid).OrderBy("Id").All(&r)
  84. return r
  85. }