DeviceClass.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package Device
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/models/Admin"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. _ "github.com/go-sql-driver/mysql"
  8. "time"
  9. )
  10. type DeviceClass struct {
  11. Id int `orm:"column(ID);size(11);auto;pk"`
  12. T_uuid string `orm:"size(256);null"` //
  13. T_name string `orm:"size(256);null"` // 分类
  14. T_Notice_wx string `orm:"type(text);null"` //微信公众号 appid/名字|
  15. T_Notice_phone string `orm:"type(text);null"` //手机 1111111|
  16. T_Notice_message string `orm:"type(text);null"` //短信 1111111|
  17. T_Notice_mailbox string `orm:"type(text);null"` //邮箱 1111111|
  18. T_State int `orm:"size(2);1"` // 0 删除 1 正常
  19. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  20. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  21. }
  22. func (t *DeviceClass) TableName() string {
  23. return "DeviceClass" // 数据库名称 // ************** 替换 DesignClass **************
  24. }
  25. func init() {
  26. //注册模型
  27. orm.RegisterModel(new(DeviceClass))
  28. }
  29. // ---------------- 特殊方法 -------------------
  30. // 获取 ById
  31. func Read_Class_ById(id int) (r DeviceClass, err error) {
  32. o := orm.NewOrm()
  33. r = DeviceClass{Id: id}
  34. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  35. if err != nil {
  36. fmt.Println(err)
  37. }
  38. return r,err
  39. }
  40. // 添加
  41. func Add_Class(m DeviceClass) (id int64, err error) {
  42. o := orm.NewOrm()
  43. id, err = o.Insert(&m)
  44. if err != nil {
  45. fmt.Println(err)
  46. }
  47. return
  48. }
  49. // 修改
  50. func Update_Class_ById(m DeviceClass) (err error) {
  51. o := orm.NewOrm()
  52. v := DeviceClass{Id: m.Id}
  53. // ascertain id exists in the database
  54. if err = o.Read(&v); err == nil {
  55. var num int64
  56. if num, err = o.Update(&m, "T_name","T_Notice_wx","T_Notice_phone","T_Notice_message","T_Notice_mailbox"); err == nil {
  57. fmt.Println("Number of records updated in database:", num)
  58. }
  59. }
  60. return
  61. }
  62. // 删除
  63. func Delete_Class_ById(id int) (DeviceClass) {
  64. o := orm.NewOrm()
  65. v := DeviceClass{Id: id}
  66. // ascertain id exists in the database
  67. if err := o.Read(&v); err == nil {
  68. var num int64
  69. v.T_State = 0
  70. if num, err = o.Update(&v, "T_State"); err == nil {
  71. fmt.Println("Number of records updated in database:", num)
  72. }
  73. }
  74. return v
  75. }
  76. // 删除
  77. func Delete_Class_ByUuid_All(user_ Admin.Admin) {
  78. o := orm.NewOrm()
  79. qs := o.QueryTable(new(DeviceClass))
  80. var r []DeviceClass
  81. qs.Filter("T_State", 1).All(&r)
  82. for _,v := range r{
  83. v.T_State = 0
  84. o.Update(&v, "T_State");
  85. }
  86. }
  87. // 获取全部
  88. func Read_Class_All_1() (r []DeviceClass) {
  89. o := orm.NewOrm()
  90. qs := o.QueryTable(new(DeviceClass))
  91. qs.Filter("T_State", 1).All(&r)
  92. return r
  93. }
  94. // 获取列表
  95. func Read_DeviceClass_ALL_1(T_uuid string,page int,T_name string) (r []DeviceClass,cnt int64) {
  96. o := orm.NewOrm()
  97. // 也可以直接使用 Model 结构体作为表名
  98. qs := o.QueryTable(new(DeviceClass))
  99. var offset int64
  100. if page <= 1 {
  101. offset = 0
  102. } else {
  103. offset = int64((page - 1) * conf.Page_size)
  104. }
  105. 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)
  106. cnt, _ = qs.Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
  107. return r,cnt
  108. }
  109. // 获取列表
  110. func Read_DeviceClass_ALL_T_uuid_1(T_uuid string) (r []DeviceClass) {
  111. o := orm.NewOrm()
  112. // 也可以直接使用 Model 结构体作为表名
  113. qs := o.QueryTable(new(DeviceClass))
  114. qs.Filter("T_uuid", T_uuid).OrderBy("-Id").Filter("T_State", 1).All(&r)
  115. return r
  116. }