...
|
...
|
@@ -7,6 +7,8 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
|
|
|
protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/project_module_files"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
...
|
...
|
@@ -15,7 +17,8 @@ type ProjectModuleFilesService struct { |
|
|
|
|
|
func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleFilesRequest) (rsp interface{}, err error) {
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
ProjectModuleVersion, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
|
|
|
)
|
|
|
rsp = &protocolx.CreateProjectModuleFilesResponse{}
|
|
|
if err = request.ValidateCommand(); err != nil {
|
...
|
...
|
@@ -31,7 +34,7 @@ func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol. |
|
|
newProjectModuleFiles := &domain.ProjectModuleFiles{
|
|
|
ProjectModuleId: request.ProjectModuleId,
|
|
|
ProjectModuleVersionId: request.ProjectModuleVersionId,
|
|
|
ParentId: request.ProjectModuleVersionId,
|
|
|
ParentId: request.ParentId,
|
|
|
FileType: request.FileType,
|
|
|
FileName: request.FileName,
|
|
|
FileKey: request.FileKey,
|
...
|
...
|
@@ -39,17 +42,16 @@ func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol. |
|
|
Remark: request.Remark,
|
|
|
CreateTime: time.Now(),
|
|
|
UpdateTime: time.Now(),
|
|
|
Path: request.FileKey,
|
|
|
}
|
|
|
|
|
|
var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
|
|
|
if m, e := ProjectModuleFilesRepository.FindOne(map[string]interface{}{"fileKey": request.FileKey, "projectModuleId": request.ProjectModuleId, "projectModuleVersionId": request.ProjectModuleVersionId}); e == nil && m != nil {
|
|
|
err = protocol.NewCustomMessage(1, "已存在 filekey:"+request.FileKey)
|
|
|
if _, err = ProjectModuleVersion.FindOne(map[string]interface{}{"id": request.ProjectModuleVersionId, "projectModuleId": request.ProjectModuleId}); err != nil {
|
|
|
err = protocol.NewCustomMessage(1, "项目版本不存在")
|
|
|
return
|
|
|
}
|
|
|
if m, err := ProjectModuleFilesRepository.Save(newProjectModuleFiles); err != nil {
|
|
|
return nil, err
|
|
|
} else {
|
|
|
rsp = m
|
|
|
var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
|
|
|
if rsp, err = svr.save(newProjectModuleFiles, ProjectModuleFilesRepository); err != nil {
|
|
|
return
|
|
|
}
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
return
|
...
|
...
|
@@ -172,6 +174,130 @@ func (svr *ProjectModuleFilesService) ListProjectModuleFiles(header *protocol.Re |
|
|
return
|
|
|
}
|
|
|
|
|
|
func (svr *ProjectModuleFilesService) Import(header *protocol.RequestHeader, request *protocolx.ImportRequest) (rsp *protocolx.ImportResponse, err error) {
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
ProjectModuleVersion, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
|
|
|
)
|
|
|
rsp = &protocolx.ImportResponse{}
|
|
|
if err = request.ValidateCommand(); err != nil {
|
|
|
err = protocol.NewCustomMessage(2, err.Error())
|
|
|
return
|
|
|
}
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
|
|
log.Error(err)
|
|
|
return nil, err
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
if _, err = ProjectModuleVersion.FindOne(map[string]interface{}{"id": request.ProjectModuleVersionId, "projectModuleId": request.ProjectModuleId}); err != nil {
|
|
|
err = protocol.NewCustomMessage(1, "项目版本不存在")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
|
|
|
var mapParent map[string]*domain.ProjectModuleFiles = make(map[string]*domain.ProjectModuleFiles)
|
|
|
|
|
|
for _, f := range request.ImportFiles {
|
|
|
fileDir := filepath.Dir(f.FileName)
|
|
|
fileBase := filepath.Base(f.FileName)
|
|
|
filename := strings.Split(fileBase, ".")[0]
|
|
|
var parentNode *domain.ProjectModuleFiles = &domain.ProjectModuleFiles{Path: "", ProjectModuleId: request.ProjectModuleId, ProjectModuleVersionId: request.ProjectModuleVersionId, Id: 0}
|
|
|
var ok bool
|
|
|
if len(fileDir) > 0 {
|
|
|
// create dir node
|
|
|
if parentNode, ok = mapParent[fileDir]; !ok {
|
|
|
parentNodes, _ := svr.mkdirF(fileDir, request.ProjectModuleId, request.ProjectModuleVersionId, request.ParentId, ProjectModuleFilesRepository)
|
|
|
for i := range parentNodes {
|
|
|
mapParent[parentNodes[i].Path] = parentNodes[i]
|
|
|
}
|
|
|
// a/b/c/test.go test.go 的parent node 就是 c (a/b/c 里面的最后一个)
|
|
|
if len(parentNodes) > 0 {
|
|
|
parentNode = parentNodes[len(parentNodes)-1]
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//save new node
|
|
|
newItem := &domain.ProjectModuleFiles{
|
|
|
ProjectModuleId: parentNode.ProjectModuleId,
|
|
|
ProjectModuleVersionId: parentNode.ProjectModuleVersionId,
|
|
|
ParentId: parentNode.Id,
|
|
|
FileType: domain.File,
|
|
|
FileName: filename,
|
|
|
FileKey: filename,
|
|
|
CodeBlock: string(f.FileContent),
|
|
|
Remark: "",
|
|
|
CreateTime: time.Now(),
|
|
|
UpdateTime: time.Now(),
|
|
|
Path: filename,
|
|
|
}
|
|
|
if _, e := svr.save(newItem, ProjectModuleFilesRepository); e != nil {
|
|
|
log.Error(e)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
|
|
|
log.Info("Receive files:", len(request.ImportFiles), request.ProjectModuleId, request.ProjectModuleVersionId, request.ParentId)
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func (svr *ProjectModuleFilesService) save(request *domain.ProjectModuleFiles, ProjectModuleFilesRepository domain.ProjectModuleFilesRepository) (rsp *domain.ProjectModuleFiles, err error) {
|
|
|
if request.ParentId > 0 {
|
|
|
if pfile, e := ProjectModuleFilesRepository.FindOne(map[string]interface{}{"id": request.ParentId, "projectModuleId": request.ProjectModuleId, "projectModuleVersionId": request.ProjectModuleVersionId}); e != nil {
|
|
|
err = protocol.NewCustomMessage(1, "父节点不存在")
|
|
|
return
|
|
|
} else {
|
|
|
request.Path = filepath.Join(pfile.Path, request.Path)
|
|
|
}
|
|
|
}
|
|
|
if m, e := ProjectModuleFilesRepository.FindOne(map[string]interface{}{"fileKey": request.FileKey, "projectModuleId": request.ProjectModuleId, "projectModuleVersionId": request.ProjectModuleVersionId, "parentId": request.ParentId}); e == nil && m != nil {
|
|
|
err = protocol.NewCustomMessage(1, "已存在 filekey:"+request.FileKey)
|
|
|
rsp = m
|
|
|
return
|
|
|
}
|
|
|
if m, e := ProjectModuleFilesRepository.Save(request); e != nil {
|
|
|
err = e
|
|
|
return
|
|
|
} else {
|
|
|
rsp = m
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
func (svr *ProjectModuleFilesService) mkdirF(fileDir string, mid, vid, pid int64, repository domain.ProjectModuleFilesRepository) (files []*domain.ProjectModuleFiles, err error) {
|
|
|
fileDir = strings.ReplaceAll(fileDir, "/", "\\")
|
|
|
dirs := strings.Split(fileDir, "\\")
|
|
|
for _, dir := range dirs {
|
|
|
//save new dir
|
|
|
newItem := &domain.ProjectModuleFiles{
|
|
|
ProjectModuleId: mid,
|
|
|
ProjectModuleVersionId: vid,
|
|
|
ParentId: pid,
|
|
|
FileType: domain.Dir,
|
|
|
FileName: dir,
|
|
|
FileKey: dir,
|
|
|
CodeBlock: "",
|
|
|
Remark: "",
|
|
|
CreateTime: time.Now(),
|
|
|
UpdateTime: time.Now(),
|
|
|
Path: dir,
|
|
|
}
|
|
|
if file, e := svr.save(newItem, repository); e != nil {
|
|
|
if file != nil { //已存在
|
|
|
pid = file.Id
|
|
|
files = append(files, file)
|
|
|
}
|
|
|
continue
|
|
|
} else {
|
|
|
pid = file.Id
|
|
|
files = append(files, file)
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func NewProjectModuleFilesService(options map[string]interface{}) *ProjectModuleFilesService {
|
|
|
svr := &ProjectModuleFilesService{}
|
|
|
return svr
|
...
|
...
|
|