Power.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package Power
  2. import (
  3. "github.com/beego/beego/v2/adapter/orm"
  4. orm2 "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type Power struct {
  8. Id int `orm:"column(ID);size(11);auto;pk"`
  9. T_id string `orm:"size(8);null"` // 权限ID
  10. T_name string `orm:"size(256);null"` // 权限名称
  11. T_menu string `orm:"size(256);null"` // Account.Menu.Id 设备通知策略 M1|M2|
  12. T_State int `orm:"size(200);default(1)"` // 0删除 1 正常
  13. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  14. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  15. }
  16. type Power_R struct {
  17. T_id string // 权限ID
  18. T_name string // 权限名称
  19. }
  20. func PowerToPower_R(r Power) (res Power_R) {
  21. res.T_id = r.T_id
  22. res.T_name = r.T_name
  23. return
  24. }
  25. func (t *Power) TableName() string {
  26. return "power" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. func NewPower(orm orm.Ormer) *PowerDaoImpl {
  29. return &PowerDaoImpl{orm: orm}
  30. }
  31. type PowerDaoImpl struct {
  32. orm orm.Ormer
  33. }
  34. // 获取 ById
  35. func (m *PowerDaoImpl) Read_Power_ByT_id(T_id string) (r Power, err error) {
  36. qs := m.orm.QueryTable(new(Power))
  37. err = qs.Filter("T_id", T_id).Filter("T_State", 1).One(&r)
  38. return r, err
  39. }
  40. // 添加
  41. func (m *PowerDaoImpl) Add_Power(r Power) (id int64, err error) {
  42. r.T_State = 1
  43. id, err = m.orm.Insert(&r)
  44. return id, err
  45. }
  46. // 添加
  47. func (m *PowerDaoImpl) InsertMulti_Power(r []Power) (id int64, err error) {
  48. id, err = orm.NewOrm().InsertMulti(len(r), r)
  49. return
  50. }
  51. // 修改
  52. func (m *PowerDaoImpl) Update_Power(r Power) (int64, error) {
  53. return m.orm.Update(&r, "T_name", "T_menu")
  54. }
  55. // 删除
  56. func (m *PowerDaoImpl) Delete_Power(r Power) (int64, error) {
  57. r.T_State = 0
  58. return m.orm.Update(&r, "T_State")
  59. }
  60. // 获取列表
  61. func (m *PowerDaoImpl) Read_Power_List(T_name string, page int, page_z int) (res []Power_R, err error) {
  62. qs := m.orm.QueryTable(new(Power))
  63. var offset int64
  64. if page <= 1 {
  65. offset = 0
  66. } else {
  67. offset = int64((page - 1) * page_z)
  68. }
  69. // 过滤
  70. cond := orm.NewCondition()
  71. cond1 := cond.And("T_State", 1)
  72. if len(T_name) > 0 {
  73. cond1 = cond1.And("T_name__icontains", T_name)
  74. }
  75. // 查询
  76. var r []Power
  77. if page_z == 9999 {
  78. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  79. } else {
  80. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  81. }
  82. for _, v := range r {
  83. res = append(res, PowerToPower_R(v))
  84. }
  85. return res, err
  86. }
  87. // 获取统计
  88. func (m *PowerDaoImpl) Read_Power_Count(T_name string) (cnt int64, err error) {
  89. qs := m.orm.QueryTable(new(Power))
  90. cond := orm.NewCondition()
  91. cond1 := cond.And("T_State", 1)
  92. if len(T_name) > 0 {
  93. cond1 = cond1.And("T_name__icontains", T_name)
  94. }
  95. return qs.SetCond((*orm2.Condition)(cond1)).Count()
  96. }