package Power import ( "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "time" ) type Power struct { Id int `orm:"column(ID);size(11);auto;pk"` T_id string `orm:"size(8);null"` // 权限ID T_name string `orm:"size(256);null"` // 权限名称 T_menu string `orm:"size(256);null"` // Account.Menu.Id 设备通知策略 M1|M2| T_State int `orm:"size(200);default(1)"` // 0删除 1 正常 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间 } type Power_R struct { T_id string // 权限ID T_name string // 权限名称 T_menu string } func PowerToPower_R(r Power) (res Power_R) { res.T_id = r.T_id res.T_name = r.T_name res.T_menu = r.T_menu return } func (t *Power) TableName() string { return "power" // 数据库名称 // ************** 替换 FormulaList ************** } func NewPower(orm orm.Ormer) *PowerDaoImpl { return &PowerDaoImpl{orm: orm} } type PowerDaoImpl struct { orm orm.Ormer } // 获取 ById func (m *PowerDaoImpl) Read_Power_ByT_id(T_id string) (r Power, err error) { qs := m.orm.QueryTable(new(Power)) err = qs.Filter("T_id", T_id).Filter("T_State", 1).One(&r) return r, err } // 添加 func (m *PowerDaoImpl) Add_Power(r Power) (id int64, err error) { r.T_State = 1 id, err = m.orm.Insert(&r) return id, err } // 添加 func (m *PowerDaoImpl) InsertMulti_Power(r []Power) (id int64, err error) { id, err = orm.NewOrm().InsertMulti(len(r), r) return } // 修改 func (m *PowerDaoImpl) Update_Power(r Power) (int64, error) { return m.orm.Update(&r, "T_name", "T_menu") } // 删除 func (m *PowerDaoImpl) Delete_Power(r Power) (int64, error) { r.T_State = 0 return m.orm.Update(&r, "T_State") } // 获取列表 func (m *PowerDaoImpl) Read_Power_List(T_name string, page int, page_z int) (res []Power_R, err error) { qs := m.orm.QueryTable(new(Power)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } // 过滤 cond := orm.NewCondition() cond1 := cond.And("T_State", 1) if len(T_name) > 0 { cond1 = cond1.And("T_name__icontains", T_name) } // 查询 var r []Power if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) } for _, v := range r { res = append(res, PowerToPower_R(v)) } return res, err } // 获取统计 func (m *PowerDaoImpl) Read_Power_Count(T_name string) (cnt int64, err error) { qs := m.orm.QueryTable(new(Power)) cond := orm.NewCondition() cond1 := cond.And("T_State", 1) if len(T_name) > 0 { cond1 = cond1.And("T_name__icontains", T_name) } return qs.SetCond((*orm2.Condition)(cond1)).Count() }