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-23 21:55:50 +08:00
"github.com/beego/beego/v2/adapter/orm"
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
}
2018-01-26 17:17:38 +08:00
func ( m * MemberRelationshipResult ) ResolveRoleName ( ) * MemberRelationshipResult {
2017-04-24 18:25:17 +08:00
if m . RoleId == conf . BookAdmin {
m . RoleName = "管理者"
2018-01-26 17:17:38 +08:00
} else if m . RoleId == conf . BookEditor {
2017-04-24 18:25:17 +08:00
m . RoleName = "编辑者"
2018-01-26 17:17:38 +08:00
} else if m . RoleId == conf . BookObserver {
2017-04-24 18:25:17 +08:00
m . RoleName = "观察者"
}
return m
}
2018-11-05 18:50:01 +08:00
2018-02-03 15:25:10 +08:00
// 根据项目ID查询用户
func ( m * MemberRelationshipResult ) FindForUsersByBookId ( 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
sql1 := "SELECT * FROM md_relationship AS rel LEFT JOIN md_members as member ON rel.member_id = member.member_id WHERE rel.book_id = ? ORDER BY rel.relationship_id DESC LIMIT ?,?"
sql2 := "SELECT count(*) AS total_count FROM md_relationship AS rel LEFT JOIN md_members as member ON rel.member_id = member.member_id WHERE rel.book_id = ?"
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
2018-02-03 15:25:10 +08:00
_ , err = o . Raw ( sql1 , bookId , offset , pageSize ) . 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 {
2017-04-24 18:25:17 +08:00
item . ResolveRoleName ( )
}
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-03-23 21:55:50 +08:00
_ , err := o . Raw ( sql , bookId , keyWord , keyWord , limit ) . QueryRows ( & members )
2019-01-16 01:09:09 +08:00
return members , err
}