|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type AdminDb struct {
- Db *xorm.Engine `json:"db"`
- }
-
- func (adminDb *AdminDb) Set() { // set方法
- adminDb.Db = Db
- }
-
- func (adminDb *AdminDb) CreateAdminId() (admId int, err error) {
- m := new(model.Admin)
- has, err := adminDb.Db.Desc("adm_id").Get(m)
- if err != nil {
- return 0, logx.Error(err)
- }
- if has == false {
- return 0, nil
- }
- admId = m.AdmId + 1
- return admId, nil
- }
-
- func (adminDb *AdminDb) AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error) {
- if reflect.TypeOf(admId).Kind() == reflect.Slice {
- return session.In("adm_id", admId).Delete(model.Admin{})
- } else {
- return session.Where("id = ?", admId).Delete(model.Admin{})
- }
- }
-
- func (adminDb *AdminDb) GetAdmin(id int) (m *model.Admin, err error) {
- m = new(model.Admin)
- has, err := adminDb.Db.Where("adm_id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (adminDb *AdminDb) GetAdminByUserName(userName string) (m *model.Admin, err error) {
- m = new(model.Admin)
- has, err := adminDb.Db.Where("username =?", userName).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (adminDb *AdminDb) GetAdminRolePermission(admId int) (list []*AdminRolePermission, total int64, err error) {
- total, err = adminDb.Db.Where("admin.adm_id =?", admId).
- Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
- Join("LEFT", "role", "admin_role.role_id = role.id").
- Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
- Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
- Join("LEFT", "permission_group_permission", "permission_group.id = permission_group_permission.group_id").
- Join("LEFT", "permission", "permission_group_permission.permission_id = permission.id").
- FindAndCount(&list)
- return
- }
-
- func (adminDb *AdminDb) FindAdminRolePermissionGroup(admId int) (list []*AdminRolePermissionGroup, total int64, err error) {
- total, err = adminDb.Db.Where("admin.adm_id =?", admId).
- Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
- Join("LEFT", "role", "admin_role.role_id = role.id").
- //Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
- //Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
- FindAndCount(&list)
- return
- }
-
- func (adminDb *AdminDb) FindAdminWithEnterprise(admId int) (list []*AdminEnterprise, total int64, err error) {
- total, err = adminDb.Db.Where("admin.adm_id =?", admId).
- Join("LEFT", "admin_with_enterprise", "admin.adm_id = admin_with_enterprise.adm_id").
- Join("LEFT", "enterprise", "admin_with_enterprise.enterprise_id = enterprise.id").
- FindAndCount(&list)
- return
- }
-
- func (adminDb *AdminDb) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) {
- sess := adminDb.Db.Desc("adm_id").Limit(limit, (page-1)*limit)
- if username != "" {
- sess.And("username like ?", "%"+username+"%")
- }
- if state != 0 {
- sess.And("state = ?", state)
- }
- total, err = sess.FindAndCount(&list)
- if err != nil {
- return nil, 0, err
- }
- return
- }
-
- func (adminDb *AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) {
- affected, err := adminDb.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (adminDb *AdminDb) AdminInsert(m *model.Admin) (int64, error) {
- insertAffected, err := adminDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return insertAffected, nil
- }
-
- func GetAllAdmin(eg *xorm.Engine) *[]model.Admin {
- var data []model.Admin
- err := eg.Where("is_super_administrator=?", 0).Find(&data)
- if err != nil {
- return nil
- }
- return &data
- }
- func GetAdminMore(eg *xorm.Engine, ids []int) map[int]model.Admin {
-
- data := make(map[int]model.Admin)
- if len(ids) > 0 {
- list := make([]model.Admin, 0)
- eg.In("adm_id", ids).Find(&list)
- for _, v := range list {
- data[v.AdmId] = v
- }
- }
- return data
- }
|