Power.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package Account
  2. import (
  3. "bzd_server/conf"
  4. "fmt"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. "time"
  7. )
  8. type UserPower struct {
  9. Id int `orm:"column(ID);size(11);auto;pk"`
  10. T_name string `orm:"size(256);null"` // 权限名称
  11. Logs_Sys_r int `orm:"size(2);0"` // 系统日志
  12. Logs_User_r int `orm:"size(2);0"` // 用户日志
  13. T_State int `orm:"size(200);1"` // 0删除 1 正常
  14. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  15. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  16. }
  17. func (t *UserPower) TableName() string {
  18. return "UserPower" // 数据库名称 // ************** 替换 FormulaList **************
  19. }
  20. func init() {
  21. //注册模型
  22. orm.RegisterModel(new(UserPower))
  23. }
  24. // 修改
  25. func Update_UserPower_ById_Basics(m UserPower) (err error) {
  26. o := orm.NewOrm()
  27. v := UserPower{Id: m.Id}
  28. // ascertain id exists in the database
  29. if err = o.Read(&v); err == nil {
  30. var num int64
  31. m.Id = v.Id
  32. if num, err = o.Update(&m, "Power_name", "Power_Administration", "Power_User_r", "Power_User_entry", "Power_Device_r", "Power_Device_a", "Power_Device_Parameter_e", "Power_DeviceSensor_Class", "Power_DeviceSensor_Parameter_e", "Power_DeviceSensor_Compensate_e", "Power_Device_Bind_e", "Power_DataScreen_r", "Logs_Sys_r", "Logs_User_r"); err == nil {
  33. fmt.Println("Number of records updated in database:", num)
  34. }
  35. }
  36. return err
  37. }
  38. // 添加
  39. func Add_UserPower(m UserPower) (id int64, err error) {
  40. o := orm.NewOrm()
  41. id, err = o.Insert(&m)
  42. if err != nil {
  43. fmt.Println(err)
  44. }
  45. return id, err
  46. }
  47. // 获取 ById
  48. func Read_UserPower_ById(Id int) (r UserPower, err error) {
  49. o := orm.NewOrm()
  50. r = UserPower{Id: Id}
  51. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  52. if err != nil {
  53. fmt.Println(err)
  54. }
  55. return r, err
  56. }
  57. // 获取列表
  58. func Read_Power_List_ALL_1() (maps []UserPower) {
  59. o := orm.NewOrm()
  60. qs := o.QueryTable(new(UserPower))
  61. qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
  62. return maps
  63. }
  64. // 获取列表
  65. func Read_Power_List_ALL_Power_Administration(Power_Administration int) (maps []UserPower) {
  66. o := orm.NewOrm()
  67. qs := o.QueryTable(new(UserPower))
  68. qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
  69. return maps
  70. }
  71. // 获取列表
  72. func Read_Power_List_ALL(page int, Power_name string) (maps []UserPower, cnt int64) {
  73. o := orm.NewOrm()
  74. // 也可以直接使用 Model 结构体作为表名
  75. qs := o.QueryTable(new(UserPower))
  76. var offset int64
  77. if page <= 1 {
  78. offset = 0
  79. } else {
  80. offset = int64((page - 1) * conf.Page_size)
  81. }
  82. qs.Limit(conf.Page_size, offset).Filter("Power_name__icontains", Power_name).Filter("T_State", 1).OrderBy("Id").All(&maps)
  83. cnt, _ = qs.Filter("Power_name__icontains", Power_name).Filter("T_State", 1).Count()
  84. return maps, cnt
  85. }
  86. // 修改
  87. func Update_Delete_UserPower_Byid(Id int) (err error) {
  88. o := orm.NewOrm()
  89. v := UserPower{Id: Id}
  90. // ascertain id exists in the database
  91. if err = o.Read(&v); err == nil {
  92. var num int64
  93. v.T_State = 0
  94. if num, err = o.Update(&v, "T_State"); err == nil {
  95. fmt.Println("Number of records updated in database:", num)
  96. }
  97. }
  98. return err
  99. }