79 lines
2.8 KiB
Go
79 lines
2.8 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[UserOrganizationRepo](nil,NewUserOrganizationRepo)
|
|
}
|
|
|
|
// 用户组织表
|
|
type UserOrganization struct {
|
|
ID uint `gorm:"column:id;primary_key;AUTO_INCREMENT"` // id
|
|
UserID uint `gorm:"column:user_id;NOT NULL"` // 用户id
|
|
OrgID uint `gorm:"column:org_id;NOT NULL"` // 组织id
|
|
Leader int `gorm:"column:leader;default:0"` // 是否是组织领导:一个组织只能有一个领导人
|
|
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 *UserOrganization) TableName() string {
|
|
return "user_organization"
|
|
}
|
|
|
|
type UserOrganizationRepo interface{
|
|
Create(ctx context.Context,userOrgs []UserOrganization)error
|
|
Delete(ctx context.Context,orgId uint,userIds []uint)error
|
|
SearchOrgUser(ctx context.Context,orgId uint,query Query)([]User,error)
|
|
GetOrgUsers(ctx context.Context,orgId uint,userIds []uint)([]UserOrganization,error)
|
|
}
|
|
|
|
type userOrganizationRepo struct{
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserOrganizationRepo(i *do.Injector)(UserOrganizationRepo,error){
|
|
return &userOrganizationRepo{
|
|
db:do.MustInvoke[*gorm.DB](i),
|
|
},nil
|
|
}
|
|
|
|
func (u *userOrganizationRepo)Create(ctx context.Context,userOrgs []UserOrganization)error{
|
|
if len(userOrgs) == 0{
|
|
return nil
|
|
}
|
|
|
|
return u.db.Create(userOrgs).Error
|
|
}
|
|
|
|
func (u *userOrganizationRepo)Delete(ctx context.Context,orgId uint,userIds []uint)error{
|
|
return u.db.Where("org_id = ? and user_id in ?",orgId,userIds).Delete(&UserOrganization{}).Error
|
|
}
|
|
|
|
func (u *userOrganizationRepo)SearchOrgUser(ctx context.Context,orgId uint,query Query)([]User,error){
|
|
users := make([]User,0)
|
|
db := u.db.Model(User{}).Joins("join user_organization on user_organization.user_id = user.id and user_organization.org_id = ?",orgId)
|
|
|
|
if query.Keyword != "" {
|
|
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
|
db = db.Where("user.name like ? or user.account like ? ", keyword,keyword)
|
|
}
|
|
|
|
err := db.Order("user_organization.created_on desc").Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&users).Error
|
|
return users,err
|
|
}
|
|
|
|
func (u *userOrganizationRepo)GetOrgUsers(ctx context.Context,orgId uint,userIds []uint)([]UserOrganization,error){
|
|
uorgs := make([]UserOrganization,0)
|
|
err := u.db.Where("org_id = ? and user_id in ?",orgId,userIds).Find(&uorgs).Error
|
|
return uorgs,err
|
|
} |