2017-04-21 18:20:35 +08:00
|
|
|
|
package models
|
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
|
import (
|
2017-04-24 18:25:17 +08:00
|
|
|
|
"errors"
|
|
|
|
|
"github.com/astaxie/beego/logs"
|
2018-01-26 17:17:38 +08:00
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
|
"github.com/lifei6671/mindoc/conf"
|
2017-04-22 17:24:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
type Relationship struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
RelationshipId int `orm:"pk;auto;unique;column(relationship_id)" json:"relationship_id"`
|
|
|
|
|
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
|
|
|
|
|
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// RoleId 角色:0 创始人(创始人不能被移除) / 1 管理员/2 编辑者/3 观察者
|
2018-11-05 18:50:01 +08:00
|
|
|
|
RoleId conf.BookRole `orm:"column(role_id);type(int)" json:"role_id"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
|
func (m *Relationship) TableName() string {
|
|
|
|
|
return "relationship"
|
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
func (m *Relationship) TableNameWithPrefix() string {
|
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
|
func (m *Relationship) TableEngine() string {
|
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 联合唯一键
|
2018-11-13 17:33:13 +08:00
|
|
|
|
func (m *Relationship) TableUnique() [][]string {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
return [][]string{
|
2018-11-05 18:50:01 +08:00
|
|
|
|
{"member_id", "book_id"},
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 17:33:13 +08:00
|
|
|
|
func (m *Relationship) QueryTable() orm.QuerySeter {
|
|
|
|
|
return orm.NewOrm().QueryTable(m.TableNameWithPrefix())
|
2018-11-12 21:01:59 +08:00
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
func NewRelationship() *Relationship {
|
|
|
|
|
return &Relationship{}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) Find(id int) (*Relationship, error) {
|
2017-04-24 18:25:17 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("relationship_id", id).One(m)
|
|
|
|
|
return m, err
|
2017-04-24 18:25:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
|
//查询指定项目的创始人.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) FindFounder(book_id int) (*Relationship, error) {
|
2017-05-11 13:39:34 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("role_id", 0).One(m)
|
2017-05-11 13:39:34 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return m, err
|
2017-05-11 13:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
|
func (m *Relationship) UpdateRoleId(bookId, memberId int, roleId conf.BookRole) (*Relationship, error) {
|
2017-04-24 18:25:17 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
book := NewBook()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
book.BookId = bookId
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
|
|
if err := o.Read(book); err != nil {
|
|
|
|
|
logs.Error("UpdateRoleId => ", err)
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return m, errors.New("项目不存在")
|
2017-04-24 18:25:17 +08:00
|
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("member_id", memberId).Filter("book_id", bookId).One(m)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
|
m = NewRelationship()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
m.BookId = bookId
|
|
|
|
|
m.MemberId = memberId
|
|
|
|
|
m.RoleId = roleId
|
2018-01-26 17:17:38 +08:00
|
|
|
|
} else if err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
} else if m.RoleId == conf.BookFounder {
|
|
|
|
|
return m, errors.New("不能变更创始人的权限")
|
2017-04-24 18:25:17 +08:00
|
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
|
m.RoleId = roleId
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
2017-06-02 09:27:13 +08:00
|
|
|
|
if m.RelationshipId > 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err = o.Update(m)
|
|
|
|
|
} else {
|
|
|
|
|
_, err = o.Insert(m)
|
2017-06-02 09:27:13 +08:00
|
|
|
|
}
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return m, err
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 17:33:13 +08:00
|
|
|
|
func (m *Relationship) FindForRoleId(bookId, memberId int) (conf.BookRole, error) {
|
2017-04-22 17:24:17 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
relationship := NewRelationship()
|
|
|
|
|
|
2018-11-13 17:33:13 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).Filter("member_id", memberId).One(relationship)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return 0, err
|
2017-04-22 17:24:17 +08:00
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return relationship.RoleId, nil
|
2017-04-22 17:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) FindByBookIdAndMemberId(book_id, member_id int) (*Relationship, error) {
|
2017-04-25 20:05:59 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", member_id).One(m)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
return m, err
|
2017-04-25 20:05:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) Insert() error {
|
2017-04-22 17:24:17 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err := o.Insert(m)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) Update() error {
|
2017-04-22 17:24:17 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err := o.Update(m)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) DeleteByBookIdAndMemberId(book_id, member_id int) error {
|
2017-04-24 18:25:17 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", member_id).One(m)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
|
return errors.New("用户未参与该项目")
|
|
|
|
|
}
|
|
|
|
|
if m.RoleId == conf.BookFounder {
|
|
|
|
|
return errors.New("不能删除创始人")
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err = o.Delete(m)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
logs.Error("删除项目参与者 => ", err)
|
2017-04-24 18:25:17 +08:00
|
|
|
|
return errors.New("删除失败")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Relationship) Transfer(book_id, founder_id, receive_id int) error {
|
2017-04-25 20:05:59 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
founder := NewRelationship()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", founder_id).One(founder)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if founder.RoleId != conf.BookFounder {
|
|
|
|
|
return errors.New("转让者不是创始人")
|
|
|
|
|
}
|
|
|
|
|
receive := NewRelationship()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", receive_id).One(receive)
|
2017-04-25 20:05:59 +08:00
|
|
|
|
|
|
|
|
|
if err != orm.ErrNoRows && err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
o.Begin()
|
|
|
|
|
|
|
|
|
|
founder.RoleId = conf.BookAdmin
|
|
|
|
|
|
|
|
|
|
receive.MemberId = receive_id
|
|
|
|
|
receive.RoleId = conf.BookFounder
|
|
|
|
|
receive.BookId = book_id
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if err := founder.Update(); err != nil {
|
2017-04-25 20:05:59 +08:00
|
|
|
|
o.Rollback()
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-06-02 09:27:13 +08:00
|
|
|
|
if receive.RelationshipId > 0 {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if _, err := o.Update(receive); err != nil {
|
2017-06-02 09:27:13 +08:00
|
|
|
|
o.Rollback()
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
} else {
|
|
|
|
|
if _, err := o.Insert(receive); err != nil {
|
2017-06-02 09:27:13 +08:00
|
|
|
|
o.Rollback()
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-04-25 20:05:59 +08:00
|
|
|
|
}
|
2017-06-02 09:27:13 +08:00
|
|
|
|
|
2017-04-25 20:05:59 +08:00
|
|
|
|
return o.Commit()
|
|
|
|
|
}
|