2017-04-22 17:24:17 +08:00
package models
import (
2018-01-26 17:17:38 +08:00
"time"
2021-03-23 15:09:17 +08:00
2021-03-26 11:34:02 +08:00
"github.com/beego/beego/v2/client/orm"
2021-04-12 21:12:26 +08:00
"github.com/beego/i18n"
2021-03-23 15:09:17 +08:00
"github.com/mindoc-org/mindoc/conf"
2017-04-22 17:24:17 +08:00
)
type MemberRelationshipResult struct {
2018-11-05 18:50:01 +08:00
MemberId int ` json:"member_id" `
Account string ` json:"account" `
RealName string ` json:"real_name" `
Description string ` json:"description" `
Email string ` json:"email" `
Phone string ` json:"phone" `
Avatar string ` json:"avatar" `
Role conf . SystemRole ` json:"role" ` //用户角色: 0 管理员/ 1 普通用户
Status int ` json:"status" ` //用户状态: 0 正常/1 禁用
CreateTime time . Time ` json:"create_time" `
CreateAt int ` json:"create_at" `
RelationshipId int ` json:"relationship_id" `
BookId int ` json:"book_id" `
2017-04-22 17:24:17 +08:00
// RoleId 角色: 0 创始人(创始人不能被移除) / 1 管理员/2 编辑者/3 观察者
2018-11-05 18:50:01 +08:00
RoleId conf . BookRole ` json:"role_id" `
RoleName string ` json:"role_name" `
2017-04-22 17:24:17 +08:00
}
2018-02-03 15:25:10 +08:00
type SelectMemberResult struct {
Result [ ] KeyValueItem ` json:"results" `
}
type KeyValueItem struct {
2018-11-05 18:50:01 +08:00
Id int ` json:"id" `
2018-02-03 15:25:10 +08:00
Text string ` json:"text" `
}
2017-04-22 17:24:17 +08:00
func NewMemberRelationshipResult ( ) * MemberRelationshipResult {
return & MemberRelationshipResult { }
}
2017-04-24 18:25:17 +08:00
func ( m * MemberRelationshipResult ) FromMember ( member * Member ) * MemberRelationshipResult {
m . MemberId = member . MemberId
m . Account = member . Account
m . Description = member . Description
m . Email = member . Email
m . Phone = member . Phone
m . Avatar = member . Avatar
m . Role = member . Role
m . Status = member . Status
m . CreateTime = member . CreateTime
m . CreateAt = member . CreateAt
2018-02-28 15:47:00 +08:00
m . RealName = member . RealName
2017-04-24 18:25:17 +08:00
return m
}
2021-04-12 21:12:26 +08:00
func ( m * MemberRelationshipResult ) ResolveRoleName ( lang string ) * MemberRelationshipResult {
2017-04-24 18:25:17 +08:00
if m . RoleId == conf . BookAdmin {
2021-04-12 21:12:26 +08:00
m . RoleName = i18n . Tr ( lang , "common.administrator" )
2018-01-26 17:17:38 +08:00
} else if m . RoleId == conf . BookEditor {
2021-04-12 21:12:26 +08:00
m . RoleName = i18n . Tr ( lang , "common.editor" )
2018-01-26 17:17:38 +08:00
} else if m . RoleId == conf . BookObserver {
2021-04-12 21:12:26 +08:00
m . RoleName = i18n . Tr ( lang , "common.obverser" )
2017-04-24 18:25:17 +08:00
}
return m
}
2018-11-05 18:50:01 +08:00
2018-02-03 15:25:10 +08:00
// 根据项目ID查询用户
2021-04-12 21:12:26 +08:00
func ( m * MemberRelationshipResult ) FindForUsersByBookId ( lang string , bookId , pageIndex , pageSize int ) ( [ ] * MemberRelationshipResult , int , error ) {
2017-04-22 17:24:17 +08:00
o := orm . NewOrm ( )
2017-04-24 18:25:17 +08:00
var members [ ] * MemberRelationshipResult
2017-04-22 17:24:17 +08:00
2023-04-14 11:09:53 +08:00
sql1 := "SELECT * FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ? ORDER BY rel.relationship_id DESC limit ? offset ?"
2017-04-22 17:24:17 +08:00
2022-03-03 11:38:11 +08:00
sql2 := "SELECT count(*) AS total_count FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ?"
2017-04-22 17:24:17 +08:00
var total_count int
2018-02-03 15:25:10 +08:00
err := o . Raw ( sql2 , bookId ) . QueryRow ( & total_count )
2017-04-22 17:24:17 +08:00
if err != nil {
2018-01-26 17:17:38 +08:00
return members , 0 , err
2017-04-22 17:24:17 +08:00
}
2018-01-26 17:17:38 +08:00
offset := ( pageIndex - 1 ) * pageSize
2017-04-22 17:24:17 +08:00
2023-04-14 11:09:53 +08:00
_ , err = o . Raw ( sql1 , bookId , pageSize , offset ) . QueryRows ( & members )
2017-04-22 17:24:17 +08:00
if err != nil {
2018-01-26 17:17:38 +08:00
return members , 0 , err
2017-04-22 17:24:17 +08:00
}
2018-01-26 17:17:38 +08:00
for _ , item := range members {
2021-04-12 21:12:26 +08:00
item . ResolveRoleName ( lang )
2017-04-24 18:25:17 +08:00
}
2018-01-26 17:17:38 +08:00
return members , total_count , nil
2017-04-22 17:24:17 +08:00
}
2018-02-03 15:25:10 +08:00
// 查询指定文档中不存在的用户列表
2018-11-05 18:50:01 +08:00
func ( m * MemberRelationshipResult ) FindNotJoinUsersByAccount ( bookId , limit int , account string ) ( [ ] * Member , error ) {
2018-02-03 15:25:10 +08:00
o := orm . NewOrm ( )
2018-02-03 17:19:47 +08:00
sql := "SELECT m.* FROM md_members as m LEFT JOIN md_relationship as rel ON m.member_id=rel.member_id AND rel.book_id = ? WHERE rel.relationship_id IS NULL AND m.account LIKE ? LIMIT 0,?;"
2018-02-03 15:25:10 +08:00
var members [ ] * Member
2018-11-05 18:50:01 +08:00
_ , err := o . Raw ( sql , bookId , account , limit ) . QueryRows ( & members )
2018-02-03 15:25:10 +08:00
2018-11-05 18:50:01 +08:00
return members , err
2018-02-03 15:25:10 +08:00
}
2019-01-16 01:09:09 +08:00
// 根据姓名以及用户名模糊查询指定文档中不存在的用户列表
func ( m * MemberRelationshipResult ) FindNotJoinUsersByAccountOrRealName ( bookId , limit int , keyWord string ) ( [ ] * Member , error ) {
o := orm . NewOrm ( )
2019-03-11 15:15:48 +08:00
sql := "SELECT m.* FROM md_members as m LEFT JOIN md_relationship as rel ON rel.member_id = m.member_id AND rel.book_id = ? WHERE rel.relationship_id IS NULL AND (m.real_name LIKE ? OR m.account LIKE ?) LIMIT 0,?;"
2019-01-16 01:09:09 +08:00
var members [ ] * Member
2021-04-12 21:12:26 +08:00
_ , err := o . Raw ( sql , bookId , keyWord , keyWord , limit ) . QueryRows ( & members )
2019-01-16 01:09:09 +08:00
return members , err
}