Power.go 3.5 KB

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