goploy/model/ProjectUserModel.go
2020-09-29 10:29:44 +08:00

125 lines
3.2 KiB
Go

package model
import (
"fmt"
"strings"
sq "github.com/Masterminds/squirrel"
)
const projectUserTable = "`project_user`"
// ProjectUser -
type ProjectUser struct {
ID int64 `json:"id"`
NamespaceID int64 `json:"namespaceId,omitempty"`
ProjectID int64 `json:"projectId"`
ProjectName string `json:"projectName"`
UserID int64 `json:"userId"`
UserName string `json:"userName"`
Role string `json:"role,omitempty"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
// ProjectUsers -
type ProjectUsers []ProjectUser
// GetBindUserListByProjectID -
func (pu ProjectUser) GetBindUserListByProjectID() (ProjectUsers, error) {
rows, err := sq.
Select("project_user.id, project_id, project_user.user_id, user.name, namespace_user.role, project_user.insert_time, project_user.update_time").
From(projectUserTable).
LeftJoin(userTable + " ON project_user.user_id = user.id").
LeftJoin(namespaceUserTable + " ON namespace_user.user_id = user.id").
Where(sq.Eq{
"project_id": pu.ProjectID,
"namespace_id": pu.NamespaceID,
"role": []string{"group-manager", "member"},
}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
projectUsers := ProjectUsers{}
for rows.Next() {
var projectUser ProjectUser
if err := rows.Scan(&projectUser.ID, &projectUser.ProjectID, &projectUser.UserID, &projectUser.UserName, &projectUser.Role, &projectUser.InsertTime, &projectUser.UpdateTime); err != nil {
return nil, err
}
projectUsers = append(projectUsers, projectUser)
}
return projectUsers, nil
}
// AddMany -
func (pu ProjectUsers) AddMany() error {
if len(pu) == 0 {
return nil
}
builder := sq.
Replace(projectUserTable).
Columns("project_id", "user_id")
for _, row := range pu {
builder = builder.Values(row.ProjectID, row.UserID)
}
_, err := builder.RunWith(DB).Exec()
return err
}
// AddAdminByUserID add admin to table project_user
func (pu ProjectUser) AddAdminByUserID() error {
builder := sq.
Replace(projectUserTable).
Columns("project_id", "user_id").
Select(sq.
Select(fmt.Sprintf("id as project_id, %d as user_id", pu.UserID)).
From(projectTable))
_, err := builder.RunWith(DB).Exec()
return err
}
// AddNamespaceProjectInUserID add all project with namespace in user id
func (pu ProjectUser) AddNamespaceProjectInUserID(namespaceID int64, userIDs []int64) error {
userIDSQL := ""
for _, userID := range userIDs {
userIDSQL += fmt.Sprintf("SELECT %d as user_id UNION ", userID)
}
userIDSQL = "(" + strings.TrimRight(userIDSQL, " UNION ") + ") as t1"
builder := sq.
Replace(projectUserTable).
Columns("project_id", "user_id").
Select(sq.
Select("project.id as project_id, t1.user_id as user_id").
From(userIDSQL).
Join(projectTable).
Where(sq.Eq{"namespace_id": namespaceID}))
_, err := builder.RunWith(DB).Exec()
return err
}
// DeleteRow -
func (pu ProjectUser) DeleteRow() error {
_, err := sq.
Delete(projectUserTable).
Where(sq.Eq{"id": pu.ID}).
RunWith(DB).
Exec()
return err
}
// DeleteByUserID -
func (pu ProjectUser) DeleteByUserID() error {
_, err := sq.
Delete(projectUserTable).
Where(sq.Eq{"user_id": pu.UserID}).
RunWith(DB).
Exec()
return err
}