Power.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. T_menu string
  20. }
  21. func PowerToPower_R(r Power) (res Power_R) {
  22. res.T_id = r.T_id
  23. res.T_name = r.T_name
  24. res.T_menu = r.T_menu
  25. return
  26. }
  27. func (t *Power) TableName() string {
  28. return "power" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. func NewPower(orm orm.Ormer) *PowerDaoImpl {
  31. return &PowerDaoImpl{orm: orm}
  32. }
  33. type PowerDaoImpl struct {
  34. orm orm.Ormer
  35. }
  36. // 获取 ById
  37. func (m *PowerDaoImpl) Read_Power_ByT_id(T_id string) (r Power, err error) {
  38. qs := m.orm.QueryTable(new(Power))
  39. err = qs.Filter("T_id", T_id).Filter("T_State", 1).One(&r)
  40. return r, err
  41. }
  42. // 添加
  43. func (m *PowerDaoImpl) Add_Power(r Power) (id int64, err error) {
  44. r.T_State = 1
  45. id, err = m.orm.Insert(&r)
  46. return id, err
  47. }
  48. // 添加
  49. func (m *PowerDaoImpl) InsertMulti_Power(r []Power) (id int64, err error) {
  50. id, err = orm.NewOrm().InsertMulti(len(r), r)
  51. return
  52. }
  53. // 修改
  54. func (m *PowerDaoImpl) Update_Power(r Power) (int64, error) {
  55. return m.orm.Update(&r, "T_name", "T_menu")
  56. }
  57. // 删除
  58. func (m *PowerDaoImpl) Delete_Power(r Power) (int64, error) {
  59. r.T_State = 0
  60. return m.orm.Update(&r, "T_State")
  61. }
  62. // 获取列表
  63. func (m *PowerDaoImpl) Read_Power_List(T_name string, page int, page_z int) (res []Power_R, err error) {
  64. qs := m.orm.QueryTable(new(Power))
  65. var offset int64
  66. if page <= 1 {
  67. offset = 0
  68. } else {
  69. offset = int64((page - 1) * page_z)
  70. }
  71. // 过滤
  72. cond := orm.NewCondition()
  73. cond1 := cond.And("T_State", 1)
  74. if len(T_name) > 0 {
  75. cond1 = cond1.And("T_name__icontains", T_name)
  76. }
  77. // 查询
  78. var r []Power
  79. if page_z == 9999 {
  80. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  81. } else {
  82. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  83. }
  84. for _, v := range r {
  85. res = append(res, PowerToPower_R(v))
  86. }
  87. return res, err
  88. }
  89. // 获取统计
  90. func (m *PowerDaoImpl) Read_Power_Count(T_name string) (cnt int64, err error) {
  91. qs := m.orm.QueryTable(new(Power))
  92. cond := orm.NewCondition()
  93. cond1 := cond.And("T_State", 1)
  94. if len(T_name) > 0 {
  95. cond1 = cond1.And("T_name__icontains", T_name)
  96. }
  97. return qs.SetCond((*orm2.Condition)(cond1)).Count()
  98. }