205 lines
7.2 KiB
Go
205 lines
7.2 KiB
Go
// Code generated by sql2gorm. DO NOT EDIT.
|
|
package repo
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
"fmt"
|
|
"github.com/samber/do"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func init(){
|
|
do.Provide[RoleRepo](nil,NewRoleRepo)
|
|
}
|
|
|
|
// 表
|
|
type Role struct {
|
|
ID uint `gorm:"column:id;primary_key;AUTO_INCREMENT"` // id
|
|
Code string `gorm:"column:code;NOT NULL"` // code
|
|
Name string `gorm:"column:name;NOT NULL"` // 名称
|
|
CreatedBy string `gorm:"column:created_by"` // 创建人
|
|
CreatedOn time.Time `gorm:"column:created_on;default:CURRENT_TIMESTAMP;NOT NULL"` // 记录创建时间
|
|
ModifiedBy string `gorm:"column:modified_by"` // 修改人
|
|
ModifiedOn time.Time `gorm:"column:modified_on;default:CURRENT_TIMESTAMP"` // 记录修改时间
|
|
}
|
|
|
|
func (m *Role) TableName() string {
|
|
return "role"
|
|
}
|
|
|
|
type RoleRepo interface{
|
|
ExistCode(ctx context.Context,code string)(exist bool,err error)
|
|
ExistId(ctx context.Context,id uint)(exist bool,err error)
|
|
Create(ctx context.Context,role Role)error
|
|
Save(ctx context.Context,role Role)error
|
|
Search(ctx context.Context,query Query)([]Role,error)
|
|
GetById(ctx context.Context,id uint)(Role,error)
|
|
DelById(ctx context.Context,id uint)error
|
|
|
|
// 用户角色
|
|
CreateUserRole(ctx context.Context,items []UserRole)error
|
|
GetUserRolesByAccount(ctx context.Context,account string)([]Role,error)
|
|
RemoveUserRole(ctx context.Context,items []UserRole)error
|
|
SearchRoleUsers(ctx context.Context,roleCode string,query Query)([]User,error)
|
|
|
|
// 组织角色
|
|
GetOrgRolesByIDs(ctx context.Context,ids []uint)([]Role,error)
|
|
|
|
GetOrgAuthors(ctx context.Context,roleid uint,orgIds []uint)([]OrganizationRoleAuthor,error)
|
|
GetRoleAllOrgAuthorsByRoleId(ctx context.Context,roleid uint)([]OrganizationRoleAuthor,error)
|
|
CreateOrgAuthor(ctx context.Context,authors []OrganizationRoleAuthor)error
|
|
DeleteOrgAuthor(ctx context.Context,roleId uint,orgIds []uint)error
|
|
SearchOrgAuthor(ctx context.Context,roleId uint,query Query)([]Organization,error)
|
|
|
|
CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error
|
|
DeleteOrgRole(ctx context.Context,roleId uint,orgIds []uint)error
|
|
GetOrgRolesByRoleOrgId(ctx context.Context,roleId uint,orgIds []uint)([]OrganizationRole,error)
|
|
}
|
|
|
|
type roleRepo struct{
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewRoleRepo(i *do.Injector)(RoleRepo,error){
|
|
return &roleRepo{
|
|
db :do.MustInvoke[*gorm.DB](i),
|
|
},nil
|
|
}
|
|
|
|
func (r *roleRepo)ExistCode(ctx context.Context,code string)(exist bool,err error){
|
|
var count int64 = 0
|
|
err = r.db.Model(&Role{}).Where("code = ?",code).Count(&count).Error
|
|
if count > 0{
|
|
return true,err
|
|
}
|
|
|
|
return false,err
|
|
}
|
|
|
|
func (r *roleRepo)ExistId(ctx context.Context,id uint)(exist bool,err error){
|
|
var count int64 = 0
|
|
err = r.db.Model(&Role{}).Where("id = ?",id).Count(&count).Error
|
|
if count > 0{
|
|
return true,err
|
|
}
|
|
|
|
return false,err
|
|
}
|
|
|
|
func (r *roleRepo)Create(ctx context.Context,role Role)error{
|
|
return r.db.Create(&role).Error
|
|
}
|
|
|
|
func (r *roleRepo)Save(ctx context.Context,role Role)error{
|
|
return r.db.Model(&Role{}).Where("id = ?",role.ID).Update("name",role.Name).Error
|
|
}
|
|
|
|
func (r *roleRepo)Search(ctx context.Context,query Query)([]Role,error){
|
|
roles := make([]Role, 0)
|
|
|
|
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
|
db := r.db.Model(&Role{})
|
|
if query.Keyword != "" {
|
|
db = db.Where("code like ? or name like ?", keyword, keyword)
|
|
}
|
|
|
|
err := db.Order(query.Sort).Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&roles).Error
|
|
return roles, err
|
|
}
|
|
|
|
func (r *roleRepo)GetById(ctx context.Context,id uint)(Role,error){
|
|
role := Role{}
|
|
err := r.db.Where("id = ?",id).Take(&role).Error
|
|
return role,err
|
|
}
|
|
|
|
func (r *roleRepo)DelById(ctx context.Context,id uint)error{
|
|
return r.db.Where("id = ?",id).Delete(&Role{}).Error
|
|
}
|
|
|
|
func (r *roleRepo)GetUserRolesByAccount(ctx context.Context,account string)([]Role,error){
|
|
roles := make([]Role,0)
|
|
err := r.db.Model(Role{}).Joins("join user_role on user_role.role_code = role.code and user_role.user_account = ?",account).Find(&roles).Error
|
|
return roles,err
|
|
}
|
|
|
|
func (r *roleRepo)GetOrgRolesByIDs(ctx context.Context,ids []uint)([]Role,error){
|
|
roles := make([]Role,0)
|
|
err := r.db.Model(Role{}).Joins("join organization_role on organization_role.role_id = role.id and organization_role.org_id in ?",ids).Find(&roles).Error
|
|
return roles,err
|
|
}
|
|
|
|
func (r *roleRepo)RemoveUserRole(ctx context.Context,items []UserRole)error{
|
|
for _,item := range items{
|
|
if err := r.db.Where("user_account = ? and role_code = ?",item.UserAccount,item.RoleCode).Delete(&UserRole{}).Error;err != nil{
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *roleRepo) GetOrgAuthors(ctx context.Context,roleid uint,orgIds []uint)([]OrganizationRoleAuthor,error){
|
|
authors := make([]OrganizationRoleAuthor,0)
|
|
err := r.db.Where("role_id = ? and org_id in ?",roleid,orgIds).Find(&authors).Error
|
|
return authors,err
|
|
}
|
|
|
|
func (r *roleRepo) CreateOrgAuthor(ctx context.Context,authors []OrganizationRoleAuthor)error{
|
|
err := r.db.Create(&authors).Error
|
|
return err
|
|
}
|
|
|
|
func (r *roleRepo) DeleteOrgAuthor(ctx context.Context,roleId uint,orgIds []uint)error{
|
|
err := r.db.Where("role_id = ? and org_id in ?",roleId,orgIds).Delete(&OrganizationRoleAuthor{}).Error
|
|
return err
|
|
}
|
|
|
|
func (r *roleRepo) CreateOrgRole(ctx context.Context,orgRoles []OrganizationRole)error{
|
|
err := r.db.Create(&orgRoles).Error
|
|
return err
|
|
}
|
|
|
|
func (r *roleRepo)DeleteOrgRole(ctx context.Context,roleId uint,orgIds []uint)error{
|
|
err := r.db.Where("role_id = ? and org_id in ?",roleId,orgIds).Delete(&OrganizationRole{}).Error
|
|
return err
|
|
}
|
|
|
|
func (r *roleRepo)GetOrgRolesByRoleOrgId(ctx context.Context,roleId uint,orgIds []uint)([]OrganizationRole,error){
|
|
orgRoles := make([]OrganizationRole,0)
|
|
err := r.db.Where("role_id = ? and org_id in ?",roleId,orgIds).Find(&orgRoles).Error
|
|
return orgRoles,err
|
|
}
|
|
|
|
func (r *roleRepo)GetRoleAllOrgAuthorsByRoleId(ctx context.Context,roleid uint)([]OrganizationRoleAuthor,error){
|
|
authors := make([]OrganizationRoleAuthor,0)
|
|
err := r.db.Where("role_id = ?",roleid).Find(&authors).Error
|
|
return authors,err
|
|
}
|
|
|
|
func (r *roleRepo)SearchRoleUsers(ctx context.Context,roleCode string,query Query)([]User,error){
|
|
users := make([]User,0)
|
|
db := r.db.Model(User{}).Joins("join user_role on user_role.user_account = user.account user_role.role_code = ?",roleCode)
|
|
|
|
if query.Keyword != "" {
|
|
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
|
db = db.Where("user.account like ? or user.name like ?", keyword, keyword)
|
|
}
|
|
|
|
err := db.Order("user_role.created_on desc").Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&users).Error
|
|
return users,err
|
|
}
|
|
|
|
func (r *roleRepo)SearchOrgAuthor(ctx context.Context,roleId uint,query Query)([]Organization,error){
|
|
orgs := make([]Organization,0)
|
|
db := r.db.Model(Organization{}).Joins("join organization_role_author on organization_role_author.org_id = organization.id and organization_role_author.role_id = ?",roleId)
|
|
|
|
if query.Keyword != "" {
|
|
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
|
db = db.Where("organization.name like ? ", keyword)
|
|
}
|
|
|
|
err := db.Order("organization_role_author.created_on desc,organization.sort").Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&orgs).Error
|
|
return orgs,err
|
|
} |