diff --git a/controllers/BookController.go b/controllers/BookController.go index 7975f8c8..36e53866 100644 --- a/controllers/BookController.go +++ b/controllers/BookController.go @@ -171,7 +171,6 @@ func (c *BookController) SaveBook() { book.HistoryCount = historyCount book.IsDownload = 0 - if autoRelease { book.AutoRelease = 1 } else { @@ -194,7 +193,7 @@ func (c *BookController) SaveBook() { } if autoSave { book.AutoSave = 1 - }else{ + } else { book.AutoSave = 0 } if err := book.Update(); err != nil { @@ -329,7 +328,7 @@ func (c *BookController) UploadCover() { fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16) //附件路径按照项目组织 - filePath := filepath.Join("uploads", book.Identify,"images", fileName+ext) + filePath := filepath.Join("uploads", book.Identify, "images", fileName+ext) path := filepath.Dir(filePath) @@ -394,7 +393,7 @@ func (c *BookController) Users() { pageIndex, _ := c.GetInt("page", 1) if key == "" { - c.Abort("404") + c.ShowErrorPage(404, "项目不存在或已删除") } book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId) @@ -407,7 +406,7 @@ func (c *BookController) Users() { c.Data["Model"] = *book - members, totalCount, err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId, pageIndex, 15) + members, totalCount, err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId, pageIndex, conf.PageSize) if totalCount > 0 { pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl()) @@ -819,6 +818,61 @@ func (c *BookController) SaveSort() { c.JsonResult(0, "ok") } +func (c *BookController) Team() { + c.Prepare() + c.TplName = "book/team.tpl" + + key := c.Ctx.Input.Param(":key") + pageIndex, _ := c.GetInt("page", 1) + + if key == "" { + c.ShowErrorPage(404, "项目不存在或已删除") + } + + book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId) + if err != nil { + if err == models.ErrPermissionDenied { + c.ShowErrorPage(403, "权限不足") + } + c.ShowErrorPage(500, "系统错误") + } + + c.Data["Model"] = book + + members, totalCount, err := models.NewTeamRelationship().FindByBookToPager(book.BookId, pageIndex, conf.PageSize) + + if totalCount > 0 { + pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl()) + c.Data["PageHtml"] = pager.HtmlPages() + } else { + c.Data["PageHtml"] = "" + } + b, err := json.Marshal(members) + + if err != nil { + c.Data["Result"] = template.JS("[]") + } else { + c.Data["Result"] = template.JS(string(b)) + } +} + +func (c *BookController) TeamAdd() { + c.Prepare() + + c.JsonResult(0, "OK") +} + +func (c *BookController) TeamDelete() { + c.Prepare() + + c.JsonResult(0, "OK") +} + +func (c *BookController) TeamSearch() { + c.Prepare() + +} + func (c *BookController) IsPermission() (*models.BookResult, error) { identify := c.GetString("identify") diff --git a/controllers/ManagerController.go b/controllers/ManagerController.go index 758fdc26..d359b544 100644 --- a/controllers/ManagerController.go +++ b/controllers/ManagerController.go @@ -982,15 +982,109 @@ func (c *ManagerController) TeamChangeMemberRole() { } +//团队项目列表. func (c *ManagerController) TeamBookList() { c.Prepare() c.TplName = "manager/team_book_list.tpl" -} -func (c *ManagerController) TeamBookAdd() { - c.Prepare() + teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id")) + pageIndex, _ := c.GetInt("page", 0) + + if teamId <= 0 { + c.JsonResult(5002, "团队标识不能为空") + } + + team, err := models.NewTeam().First(teamId) + + if err == orm.ErrNoRows { + c.ShowErrorPage(404, "团队不存在") + } + c.CheckErrorResult(500, err) + c.Data["Model"] = team + + teams, totalCount, err := models.NewTeamRelationship().FindToPager(teamId, pageIndex, conf.PageSize) + + if err != nil && err != orm.ErrNoRows { + c.ShowErrorPage(500, err.Error()) + } + if err == orm.ErrNoRows || len(teams) <= 0 { + c.Data["Result"] = template.JS("[]") + c.Data["PageHtml"] = "" + return + } + + if totalCount > 0 { + pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl()) + c.Data["PageHtml"] = pager.HtmlPages() + } else { + c.Data["PageHtml"] = "" + } + + b, err := json.Marshal(teams) + + if err != nil { + beego.Error("编码 JSON 结果失败 ->", err) + c.Data["Result"] = template.JS("[]") + } else { + c.Data["Result"] = template.JS(string(b)) + } } +//给团队增加项目. +func (c *ManagerController) TeamBookAdd() { + c.Prepare() + + teamId, _ := c.GetInt("teamId") + bookId, _ := c.GetInt("bookId") + + if teamId <= 0 || bookId <= 0 { + c.JsonResult(500, "参数错误") + } + teamRel := models.NewTeamRelationship() + teamRel.BookId = bookId + teamRel.TeamId = teamId + + err := teamRel.Save() + + if err != nil { + c.JsonResult(5001, err.Error()) + } else { + teamRel.Include() + c.JsonResult(0, "OK", teamRel) + } +} + +//搜索未参与的项目. +func (c *ManagerController) TeamSearchBook() { + c.Prepare() + + teamId, _ := c.GetInt("teamId") + keyword := strings.TrimSpace(c.GetString("q")) + + if teamId <= 0 { + c.JsonResult(500, "参数错误") + } + + searchResult, err := models.NewTeamRelationship().FindNotJoinBookByName(teamId, keyword, 10) + + if err != nil { + c.JsonResult(500, err.Error()) + } + c.JsonResult(0, "OK", searchResult) + +} func (c *ManagerController) TeamBookDelete() { c.Prepare() + teamRelationshipId, _ := c.GetInt("teamRelId") + + if teamRelationshipId <= 0 { + c.JsonResult(500, "参数错误") + } + + err := models.NewTeamRelationship().Delete(teamRelationshipId) + + if err != nil { + c.JsonResult(5001, "删除失败") + } + c.JsonResult(0, "OK") } diff --git a/models/Team.go b/models/Team.go index 080446ba..57ed2814 100644 --- a/models/Team.go +++ b/models/Team.go @@ -95,7 +95,7 @@ func (t *Team) FindToPager(pageIndex, pageSize int) (list []*Team, totalCount in offset := (pageIndex - 1) * pageSize - _, err = o.QueryTable(t.TableNameWithPrefix()).Offset(offset).Limit(pageSize).All(&list) + _, err = o.QueryTable(t.TableNameWithPrefix()).OrderBy("-team_id").Offset(offset).Limit(pageSize).All(&list) if err != nil { return diff --git a/models/TeamMember.go b/models/TeamMember.go index 41732b6b..c27b3a8e 100644 --- a/models/TeamMember.go +++ b/models/TeamMember.go @@ -63,7 +63,7 @@ func (m *TeamMember) ChangeRoleId(teamId int, memberId int, roleId conf.BookRole } o := orm.NewOrm() - err = o.QueryTable(m.TableNameWithPrefix()).Filter("team_id", teamId).Filter("member_id", memberId).One(m) + err = o.QueryTable(m.TableNameWithPrefix()).Filter("team_id", teamId).Filter("member_id", memberId).OrderBy("-team_member_id").One(m) if err != nil { beego.Error("查询团队用户时失败 ->", err) @@ -79,6 +79,7 @@ func (m *TeamMember) ChangeRoleId(teamId int, memberId int, roleId conf.BookRole return m, err } +//查询团队中指定的用户. func (m *TeamMember) FindFirst(teamId, memberId int) (*TeamMember, error) { if teamId <= 0 || memberId <= 0 { return nil, ErrInvalidParameter @@ -93,6 +94,7 @@ func (m *TeamMember) FindFirst(teamId, memberId int) (*TeamMember, error) { return m.Include(),nil } +//更新或插入团队用户. func (m *TeamMember) Save(cols ...string) (err error) { if m.TeamId <= 0 { @@ -110,7 +112,11 @@ func (m *TeamMember) Save(cols ...string) (err error) { if !o.QueryTable(NewMember()).Filter("member_id", m.MemberId).Filter("status", 0).Exist() { return errors.New("用户不存在或已禁用") } + if m.TeamMemberId <= 0 { + if o.QueryTable(m.TableNameWithPrefix()).Filter("team_id",m.TeamId).Filter("member_id",m.MemberId).Exist() { + return errors.New("团队中已存在该用户") + } _, err = o.Insert(m) } else { _, err = o.Update(m, cols...) @@ -121,6 +127,7 @@ func (m *TeamMember) Save(cols ...string) (err error) { return } +//删除一个团队用户. func (m *TeamMember) Delete(id int) (err error) { if id <= 0 { @@ -134,6 +141,7 @@ func (m *TeamMember) Delete(id int) (err error) { return } +//分页查询团队用户. func (m *TeamMember) FindToPager(teamId, pageIndex, pageSize int) (list []*TeamMember, totalCount int, err error) { if teamId <= 0 { err = ErrInvalidParameter @@ -165,6 +173,7 @@ func (m *TeamMember) FindToPager(teamId, pageIndex, pageSize int) (list []*TeamM return } +//查询关联数据. func (m *TeamMember) Include() *TeamMember { if member, err := NewMember().Find(m.MemberId, "account", "real_name", "avatar"); err == nil { @@ -184,6 +193,7 @@ func (m *TeamMember) Include() *TeamMember { return m } +//查询未加入团队的用户。 func (m *TeamMember) FindNotJoinMemberByAccount(teamId int, account string, limit int) (*SelectMemberResult, error) { if teamId <= 0 { return nil, ErrInvalidParameter @@ -193,7 +203,7 @@ func (m *TeamMember) FindNotJoinMemberByAccount(teamId int, account string, limi sql := `select member.member_id,member.account from md_members as member left join md_team_member as team on team.team_id = ? and member.member_id != team.member_id - where member.account like ? + where member.account like ? and team.member_id isnull order by member.member_id desc limit ?;` diff --git a/models/TeamRelationship.go b/models/TeamRelationship.go index 8db83153..1342472c 100644 --- a/models/TeamRelationship.go +++ b/models/TeamRelationship.go @@ -3,6 +3,9 @@ package models import ( "github.com/lifei6671/mindoc/conf" "time" + "github.com/astaxie/beego/orm" + "github.com/astaxie/beego" + "errors" ) type TeamRelationship struct { @@ -10,6 +13,9 @@ type TeamRelationship struct { BookId int `orm:"column(book_id)"` TeamId int `orm:"column(team_id)"` CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"` + BookMemberId int `orm:"-" json:"book_member_id"` + BookMemberName string `orm:"-" json:"book_member_name"` + BookName string `orm:"-" json:"book_name"` } // TableName 获取对应数据库表名. @@ -27,9 +33,192 @@ func (m *TeamRelationship) TableEngine() string { // 联合唯一键 func (m *TeamRelationship) TableUnique() [][]string { - return [][]string{{"team_id", "team_id"}} + return [][]string{{"team_id", "book_id"}} +} +func (m *TeamRelationship) QueryTable() orm.QuerySeter { + return orm.NewOrm().QueryTable(m.TableNameWithPrefix()) } func NewTeamRelationship() *TeamRelationship { return &TeamRelationship{} } + +//保存团队项目. +func (m *TeamRelationship) Save(cols ...string) (err error) { + if m.TeamId <= 0 || m.BookId <= 0 { + return ErrInvalidParameter + } + if (m.TeamRelationshipId > 0 && m.QueryTable().Filter("book_id", m.BookId).Filter("team_id", m.TeamId).Filter("team_relationship_id__ne", m.TeamRelationshipId).Exist()) || + m.TeamRelationshipId <= 0 && m.QueryTable().Filter("book_id", m.BookId).Filter("team_id", m.TeamId).Exist() { + return errors.New("当前团队已加入该项目") + } + if m.TeamRelationshipId > 0 { + _, err = orm.NewOrm().Update(m) + } else { + _, err = orm.NewOrm().Insert(m) + } + if err != nil { + beego.Error("保存团队项目时出错 ->", err) + } + return +} + +func (m *TeamRelationship) Delete(teamRelId int) (err error) { + if teamRelId <= 0 { + return ErrInvalidParameter + } + _, err = m.QueryTable().Filter("team_relationship_id", teamRelId).Delete() + + if err != nil { + beego.Error("删除团队项目失败 ->", err) + } + return +} + +//分页查询团队项目. +func (m *TeamRelationship) FindToPager(teamId, pageIndex, pageSize int) (list []*TeamRelationship, totalCount int, err error) { + if teamId <= 0 { + err = ErrInvalidParameter + return + } + offset := (pageIndex - 1) * pageSize + + o := orm.NewOrm() + + _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("team_id", teamId).OrderBy("-team_relationship_id").Offset(offset).Limit(pageSize).All(&list) + + if err != nil { + beego.Error("查询团队项目时出错 ->", err) + return + } + count, err := m.QueryTable().Filter("team_id", teamId).Count() + + if err != nil { + beego.Error("查询团队项目时出错 ->", err) + return + } + totalCount = int(count) + for _, item := range list { + item.Include() + } + return +} + +//加载附加数据. +func (m *TeamRelationship) Include() (*TeamRelationship, error) { + if m.BookId > 0 { + b, err := NewBook().Find(m.BookId, "book_name", "identify", "member_id") + if err != nil { + return m, err + } + m.BookName = b.BookName + m.BookMemberId = b.MemberId + if b.MemberId > 0 { + member, err := NewMember().Find(b.MemberId, "account", "real_name") + if err != nil { + return m, err + } + if member.RealName == "" { + m.BookMemberName = member.Account + } else { + m.BookMemberName = member.RealName + } + } + } + return m, nil +} + +//查询未加入团队的用户。 +func (m *TeamRelationship) FindNotJoinBookByName(teamId int, bookName string, limit int) (*SelectMemberResult, error) { + if teamId <= 0 { + return nil, ErrInvalidParameter + } + o := orm.NewOrm() + + sql := `select book.book_id,book.book_name +from md_books as book +where book.book_id not in (select team.book_id from md_team_relationship as team where team_id=?) +and book.book_name like ? order by book_id desc limit ?;` + + books := make([]*Book, 0) + + _, err := o.Raw(sql, teamId, "%"+bookName+"%", limit).QueryRows(&books) + + if err != nil { + beego.Error("查询团队项目时出错 ->", err) + return nil, err + } + + result := SelectMemberResult{} + items := make([]KeyValueItem, 0) + + for _, book := range books { + item := KeyValueItem{} + item.Id = book.BookId + item.Text = book.BookName + items = append(items, item) + } + result.Result = items + + return &result, err +} + +//查询指定项目的团队. +func (m *TeamRelationship) FindByBookToPager(bookId, pageIndex,pageSize int) (list []*TeamRelationship,totalCount int,err error) { + + if bookId <= 0{ + err = ErrInvalidParameter + return + } + + offset := (pageIndex - 1) * pageSize + + o := orm.NewOrm() + + _, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("-team_relationship_id").Offset(offset).Limit(pageSize).All(&list) + + if err != nil { + beego.Error("查询团队项目时出错 ->", err) + return + } + count, err := m.QueryTable().Filter("book_id", bookId).Count() + + if err != nil { + beego.Error("查询团队项目时出错 ->", err) + return + } + totalCount = int(count) + for _, item := range list { + item.Include() + } + return +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/routers/router.go b/routers/router.go index a2582fb7..732781ac 100644 --- a/routers/router.go +++ b/routers/router.go @@ -55,6 +55,7 @@ func init() { beego.Router("/manager/team/book/list/:id", &controllers.ManagerController{}, "*:TeamBookList") beego.Router("/manager/team/book/add", &controllers.ManagerController{}, "POST:TeamBookAdd") beego.Router("/manager/team/book/delete", &controllers.ManagerController{}, "POST:TeamBookDelete") + beego.Router("/manager/team/book/search", &controllers.ManagerController{}, "*:TeamSearchBook") beego.Router("/setting", &controllers.SettingController{}, "*:Index") @@ -67,6 +68,8 @@ func init() { beego.Router("/book/:key/users", &controllers.BookController{}, "*:Users") beego.Router("/book/:key/release", &controllers.BookController{}, "post:Release") beego.Router("/book/:key/sort", &controllers.BookController{}, "post:SaveSort") + beego.Router("/book/:key/teams", &controllers.BookController{}, "*:Team") + beego.Router("/book/create", &controllers.BookController{}, "*:Create") beego.Router("/book/users/create", &controllers.BookMemberController{}, "post:AddMember") @@ -82,6 +85,11 @@ func init() { beego.Router("/book/setting/token", &controllers.BookController{}, "post:CreateToken") beego.Router("/book/setting/delete", &controllers.BookController{}, "post:Delete") + beego.Router("/book/team/add", &controllers.BookController{}, "POST:TeamAdd") + beego.Router("/book/team/delete", &controllers.BookController{}, "POST:TeamDelete") + beego.Router("/book/team/search", &controllers.BookController{}, "POST:TeamSearch") + + //管理文章的路由 beego.Router("/manage/blogs", &controllers.BlogController{},"*:ManageList") beego.Router("/manage/blogs/setting/?:id", &controllers.BlogController{}, "*:ManageSetting") diff --git a/views/book/dashboard.tpl b/views/book/dashboard.tpl index 8a04ec1d..4a09f6c7 100644 --- a/views/book/dashboard.tpl +++ b/views/book/dashboard.tpl @@ -27,7 +27,8 @@