Power.go 3.5 KB

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