package Account import ( "ColdVerify_server/lib" "ColdVerify_server/logs" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "time" ) type Admin struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uuid string `orm:"size(256);null"` // T_power int `orm:"size(2);;default(0)"` // 权限 0 T_name string `orm:"size(256);null"` // 姓名 T_user string `orm:"size(256);null"` // 用户名 T_pass string `orm:"size(256);null"` // 密码 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 Admin_R struct { T_uuid string `orm:"column(t_uuid)"` T_power int `orm:"column(t_power)"` // 权限 T_power_name string `orm:"column(t_power_name)"` // 权限 T_name string `orm:"column(t_name)"` // 姓名 T_user string `orm:"column(t_user)"` // 用户名 } func (t *Admin) TableName() string { return "admin" // 数据库名称 // ************** 替换 FormulaList ************** } func init() { //注册模型 orm.RegisterModel(new(Admin)) } func AdminToAdmin_R(T Admin, powerMap map[int]string) (T_r Admin_R) { T_r.T_uuid = T.T_uuid T_r.T_power = T.T_power T_r.T_name = T.T_name T_r.T_user = T.T_user T_r.T_power_name = powerMap[T.T_power] return T_r } // 获取 ByT_user func Read_Admin_ByT_user(T_user string) (e error, r Admin) { o := orm.NewOrm() qs := o.QueryTable(new(Admin)) e = qs.Filter("T_user", T_user).Filter("T_State", 1).One(&r) return e, r } // 获取 ByT_uuid func Read_Admin_ByT_uuid(T_uuid string) (e error, r Admin) { o := orm.NewOrm() qs := o.QueryTable(new(Admin)) e = qs.Filter("T_uuid", T_uuid).Filter("T_State", 1).One(&r) return e, r } // 添加 func Add_Admin(m Admin) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } func Read_Admin_verification(T_user string, T_pass string) (error, Admin) { o := orm.NewOrm() r := Admin{T_user: T_user, T_pass: T_pass, T_State: 1} err := o.Read(&r, "T_user", "T_pass", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { logs.Error(lib.FuncName(), err) } return err, r } // 获取列表 func Read_Admin_List(T_name string, page int, page_z int, powerMap map[int]string) ([]Admin_R, int) { o := orm.NewOrm() var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } qs := o.QueryTable(new(Admin)) var r []Admin cond := orm.NewCondition() cond1 := cond.And("T_State", 1).AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name)) qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count() // 转换 var Admin_r []Admin_R for _, v := range r { Admin_r = append(Admin_r, AdminToAdmin_R(v, powerMap)) } return Admin_r, int(cnt) } // 修改 func Update_Admin(m Admin, cols ...string) bool { o := orm.NewOrm() if num, err := o.Update(&m, cols...); err == nil { logs.Println("Number of records updated in database:", num) return true } return false } // 删除 func Delete_Admin(v Admin) bool { o := orm.NewOrm() if num, err := o.Delete(&v); err == nil { logs.Println("Number of records deleted in database:", num) } else { return false } return true } // 删除 func Delete_Admin_(v Admin) bool { o := orm.NewOrm() v.T_State = 0 if num, err := o.Update(&v, "T_State"); err == nil { logs.Println("Number of records updated in database:", num) } else { return false } return true } // 获取全部列表 func Read_Admin_List_ALL() (maps []Admin) { o := orm.NewOrm() qs := o.QueryTable(new(Admin)) qs.Filter("T_State", 1).OrderBy("Id").All(&maps) return maps } // 获取全部列表-包括软删除数据 func Read_Admin_List_ALL_1() (maps []Admin) { o := orm.NewOrm() qs := o.QueryTable(new(Admin)) qs.OrderBy("Id").All(&maps) return maps } // 获取全部列表 func Read_Admin_List_ALL_Power(PowerMaps map[int]string) (maps []AdminPower_R) { o := orm.NewOrm() var r []Admin qs := o.QueryTable(new(Admin)) qs.Filter("T_State", 1).OrderBy("Id").All(&r) // 转换 var Admin_Power_r []AdminPower_R for _, v := range r { Admin_Power_r = append(Admin_Power_r, AdminToAdminPower_R(v, PowerMaps)) } return Admin_Power_r } // {"uuid":"name"} func AdminListToMap(A []Admin) map[string]string { maps := make(map[string]string, len(A)) for _, v := range A { maps[v.T_uuid] = v.T_name } return maps } type AdminPower_R struct { T_uuid string `orm:"column(t_uuid)"` T_name string `orm:"column(t_name)"` // 姓名 T_power_name string `orm:"column(t_power_name)"` // 权限 } // PowerMaps {1:'管理员'} func AdminToAdminPower_R(T Admin, PowerMaps map[int]string) (T_r AdminPower_R) { T_power_name, ok := PowerMaps[T.T_power] if !ok { T_power_name = "" } T_r.T_uuid = T.T_uuid T_r.T_name = T.T_name T_r.T_power_name = T_power_name return T_r }