You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.6 KiB
Go
162 lines
3.6 KiB
Go
package containers
|
|
|
|
import (
|
|
"joybuywork/internal/app"
|
|
"joybuywork/internal/consts/ckey"
|
|
"joybuywork/internal/plugins/encrypt"
|
|
"joybuywork/internal/repository/dbcheck"
|
|
"joybuywork/internal/repository/model"
|
|
"joybuywork/internal/repository/mtype"
|
|
"joybuywork/internal/repository/withdefault"
|
|
"joybuywork/internal/service/cmm"
|
|
"joybuywork/internal/utils"
|
|
"joybuywork/internal/utils/logic"
|
|
pb "joybuywork/proto/go_proto/w_project"
|
|
"joybuywork/proto/go_proto/workerr"
|
|
|
|
oprojectpb "gits.branchcn.com/backend/oplatform/proto/go_proto/oproject"
|
|
"gorm.io/gen"
|
|
)
|
|
|
|
type ProjectStore struct {
|
|
logic.Logic
|
|
|
|
Req *pb.StoreReq
|
|
Res *pb.StoreRes
|
|
}
|
|
|
|
func (s *ProjectStore) Run() (err error) {
|
|
return logic.Run(s)
|
|
}
|
|
|
|
func (s *ProjectStore) Exec() (err error) {
|
|
domain, err := s.domain()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return s.create(domain)
|
|
}
|
|
|
|
func (s *ProjectStore) create(domain string) (err error) {
|
|
m := s.GetDao().Project
|
|
|
|
project := withdefault.Project(&model.Project{
|
|
CompanyID: s.Req.GetCompanyId(),
|
|
Title: s.Req.GetTitle(),
|
|
Domain: domain,
|
|
IsAll: utils.Ternary(s.Req.GetIsAll(), int8(mtype.Yes), int8(mtype.No)),
|
|
Scene: int8(s.Req.GetScene()),
|
|
LogoURL: s.Req.GetLogoUrl(),
|
|
FissionCertType: mtype.Yes,
|
|
})
|
|
|
|
err = m.WithContext(s.Ctx).Create(project)
|
|
if err != nil {
|
|
err = s.BE().MysqlError(err)
|
|
return
|
|
}
|
|
|
|
err = s._userProjectRelate(project)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
//生成默认密钥
|
|
psm := s.GetDao().ProjectSecret
|
|
err = psm.WithContext(s.Ctx).Create(withdefault.ProjectSecret(&model.ProjectSecret{
|
|
ProjectID: project.ID,
|
|
CreatorID: s.Req.GetUserId(),
|
|
AccessSecret: encrypt.ProjectSecret(project.ID),
|
|
}))
|
|
if err != nil {
|
|
return s.BE().MysqlError(err)
|
|
}
|
|
|
|
return s._sync(project)
|
|
}
|
|
|
|
func (s *ProjectStore) _sync(project *model.Project) (err error) {
|
|
// 创建项目第三方配置
|
|
_, err = app.NewOProject().CreateWithSecret(s.Ctx, &oprojectpb.CreateWithSecretReq{
|
|
ProjectId: project.ID,
|
|
Secret: project.ThirdSecret,
|
|
ProjectHash: project.HashID,
|
|
})
|
|
return
|
|
}
|
|
|
|
func (s *ProjectStore) _userProjectRelate(project *model.Project) (err error) {
|
|
// 项目没有选择成员
|
|
if !s.Req.GetIsAll() && len(s.Req.GetUserIds()) == 0 {
|
|
return
|
|
}
|
|
|
|
// 关联项目成员
|
|
userM := s.GetDao().User
|
|
var cond = []gen.Condition{
|
|
userM.IsDel.Eq(mtype.No),
|
|
}
|
|
|
|
//不同的查询条件
|
|
if s.Req.GetIsAll() {
|
|
cond = append(cond, userM.CompanyID.Eq(s.Req.GetCompanyId()))
|
|
} else {
|
|
cond = append(cond, userM.HashID.In(s.Req.GetUserIds()...))
|
|
}
|
|
|
|
//找到员工
|
|
var userIds []int64
|
|
err = userM.WithContext(s.Ctx).Where(cond...).Pluck(userM.ID, &userIds)
|
|
if err != nil {
|
|
return s.BE().MysqlError(err)
|
|
}
|
|
|
|
var adds []*model.UserProjectRelate
|
|
for _, v := range userIds {
|
|
adds = append(adds, withdefault.UserProjectRelate(&model.UserProjectRelate{
|
|
UserID: v,
|
|
ProjectID: project.ID,
|
|
}))
|
|
}
|
|
|
|
if len(adds) > 0 {
|
|
err = s.GetDao().UserProjectRelate.WithContext(s.Ctx).Create(adds...)
|
|
if err != nil {
|
|
return s.BE().MysqlError(err)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (s *ProjectStore) domain() (domain string, err error) {
|
|
|
|
if s.Req.GetDomain() == "" {
|
|
domain = cmm.NewCMM(s.Ctx, s.GetDao()).GetDomain()
|
|
return
|
|
}
|
|
|
|
domain = s.Req.GetDomain()
|
|
lc := app.NewRedisLock(s.Ctx, ckey.ProjectStoreOnce(domain))
|
|
defer lc.Release()
|
|
if !lc.SoftLock() {
|
|
err = workerr.OnceLockError
|
|
return
|
|
}
|
|
|
|
m := s.GetDao().Project
|
|
exist, err := m.WithContext(s.Ctx).Where(m.Domain.Eq(domain), m.CompanyID.Neq(s.Req.GetCompanyId())).Take()
|
|
if err = dbcheck.DbError(err); err != nil {
|
|
err = s.BE().MysqlError(err)
|
|
return
|
|
}
|
|
|
|
if exist != nil {
|
|
err = workerr.DomainExistError
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|