作者 yangfu

增加: 1.模板导入功能

@@ -7,6 +7,8 @@ import ( @@ -7,6 +7,8 @@ import (
7 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" 7 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol" 8 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
9 protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/project_module_files" 9 protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/project_module_files"
  10 + "path/filepath"
  11 + "strings"
10 "time" 12 "time"
11 ) 13 )
12 14
@@ -15,7 +17,8 @@ type ProjectModuleFilesService struct { @@ -15,7 +17,8 @@ type ProjectModuleFilesService struct {
15 17
16 func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleFilesRequest) (rsp interface{}, err error) { 18 func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleFilesRequest) (rsp interface{}, err error) {
17 var ( 19 var (
18 - transactionContext, _ = factory.CreateTransactionContext(nil) 20 + transactionContext, _ = factory.CreateTransactionContext(nil)
  21 + ProjectModuleVersion, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
19 ) 22 )
20 rsp = &protocolx.CreateProjectModuleFilesResponse{} 23 rsp = &protocolx.CreateProjectModuleFilesResponse{}
21 if err = request.ValidateCommand(); err != nil { 24 if err = request.ValidateCommand(); err != nil {
@@ -31,7 +34,7 @@ func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol. @@ -31,7 +34,7 @@ func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol.
31 newProjectModuleFiles := &domain.ProjectModuleFiles{ 34 newProjectModuleFiles := &domain.ProjectModuleFiles{
32 ProjectModuleId: request.ProjectModuleId, 35 ProjectModuleId: request.ProjectModuleId,
33 ProjectModuleVersionId: request.ProjectModuleVersionId, 36 ProjectModuleVersionId: request.ProjectModuleVersionId,
34 - ParentId: request.ProjectModuleVersionId, 37 + ParentId: request.ParentId,
35 FileType: request.FileType, 38 FileType: request.FileType,
36 FileName: request.FileName, 39 FileName: request.FileName,
37 FileKey: request.FileKey, 40 FileKey: request.FileKey,
@@ -39,17 +42,16 @@ func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol. @@ -39,17 +42,16 @@ func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol.
39 Remark: request.Remark, 42 Remark: request.Remark,
40 CreateTime: time.Now(), 43 CreateTime: time.Now(),
41 UpdateTime: time.Now(), 44 UpdateTime: time.Now(),
  45 + Path: request.FileKey,
42 } 46 }
43 47
44 - var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)  
45 - if m, e := ProjectModuleFilesRepository.FindOne(map[string]interface{}{"fileKey": request.FileKey, "projectModuleId": request.ProjectModuleId, "projectModuleVersionId": request.ProjectModuleVersionId}); e == nil && m != nil {  
46 - err = protocol.NewCustomMessage(1, "已存在 filekey:"+request.FileKey) 48 + if _, err = ProjectModuleVersion.FindOne(map[string]interface{}{"id": request.ProjectModuleVersionId, "projectModuleId": request.ProjectModuleId}); err != nil {
  49 + err = protocol.NewCustomMessage(1, "项目版本不存在")
47 return 50 return
48 } 51 }
49 - if m, err := ProjectModuleFilesRepository.Save(newProjectModuleFiles); err != nil {  
50 - return nil, err  
51 - } else {  
52 - rsp = m 52 + var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
  53 + if rsp, err = svr.save(newProjectModuleFiles, ProjectModuleFilesRepository); err != nil {
  54 + return
53 } 55 }
54 err = transactionContext.CommitTransaction() 56 err = transactionContext.CommitTransaction()
55 return 57 return
@@ -172,6 +174,130 @@ func (svr *ProjectModuleFilesService) ListProjectModuleFiles(header *protocol.Re @@ -172,6 +174,130 @@ func (svr *ProjectModuleFilesService) ListProjectModuleFiles(header *protocol.Re
172 return 174 return
173 } 175 }
174 176
  177 +func (svr *ProjectModuleFilesService) Import(header *protocol.RequestHeader, request *protocolx.ImportRequest) (rsp *protocolx.ImportResponse, err error) {
  178 + var (
  179 + transactionContext, _ = factory.CreateTransactionContext(nil)
  180 + ProjectModuleVersion, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
  181 + )
  182 + rsp = &protocolx.ImportResponse{}
  183 + if err = request.ValidateCommand(); err != nil {
  184 + err = protocol.NewCustomMessage(2, err.Error())
  185 + return
  186 + }
  187 + if err = transactionContext.StartTransaction(); err != nil {
  188 + log.Error(err)
  189 + return nil, err
  190 + }
  191 + defer func() {
  192 + transactionContext.RollbackTransaction()
  193 + }()
  194 + if _, err = ProjectModuleVersion.FindOne(map[string]interface{}{"id": request.ProjectModuleVersionId, "projectModuleId": request.ProjectModuleId}); err != nil {
  195 + err = protocol.NewCustomMessage(1, "项目版本不存在")
  196 + return
  197 + }
  198 +
  199 + var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
  200 + var mapParent map[string]*domain.ProjectModuleFiles = make(map[string]*domain.ProjectModuleFiles)
  201 +
  202 + for _, f := range request.ImportFiles {
  203 + fileDir := filepath.Dir(f.FileName)
  204 + fileBase := filepath.Base(f.FileName)
  205 + filename := strings.Split(fileBase, ".")[0]
  206 + var parentNode *domain.ProjectModuleFiles = &domain.ProjectModuleFiles{Path: "", ProjectModuleId: request.ProjectModuleId, ProjectModuleVersionId: request.ProjectModuleVersionId, Id: 0}
  207 + var ok bool
  208 + if len(fileDir) > 0 {
  209 + // create dir node
  210 + if parentNode, ok = mapParent[fileDir]; !ok {
  211 + parentNodes, _ := svr.mkdirF(fileDir, request.ProjectModuleId, request.ProjectModuleVersionId, request.ParentId, ProjectModuleFilesRepository)
  212 + for i := range parentNodes {
  213 + mapParent[parentNodes[i].Path] = parentNodes[i]
  214 + }
  215 + // a/b/c/test.go test.go 的parent node 就是 c (a/b/c 里面的最后一个)
  216 + if len(parentNodes) > 0 {
  217 + parentNode = parentNodes[len(parentNodes)-1]
  218 + }
  219 + }
  220 + }
  221 +
  222 + //save new node
  223 + newItem := &domain.ProjectModuleFiles{
  224 + ProjectModuleId: parentNode.ProjectModuleId,
  225 + ProjectModuleVersionId: parentNode.ProjectModuleVersionId,
  226 + ParentId: parentNode.Id,
  227 + FileType: domain.File,
  228 + FileName: filename,
  229 + FileKey: filename,
  230 + CodeBlock: string(f.FileContent),
  231 + Remark: "",
  232 + CreateTime: time.Now(),
  233 + UpdateTime: time.Now(),
  234 + Path: filename,
  235 + }
  236 + if _, e := svr.save(newItem, ProjectModuleFilesRepository); e != nil {
  237 + log.Error(e)
  238 + continue
  239 + }
  240 + }
  241 +
  242 + log.Info("Receive files:", len(request.ImportFiles), request.ProjectModuleId, request.ProjectModuleVersionId, request.ParentId)
  243 + err = transactionContext.CommitTransaction()
  244 + return
  245 +}
  246 +
  247 +func (svr *ProjectModuleFilesService) save(request *domain.ProjectModuleFiles, ProjectModuleFilesRepository domain.ProjectModuleFilesRepository) (rsp *domain.ProjectModuleFiles, err error) {
  248 + if request.ParentId > 0 {
  249 + if pfile, e := ProjectModuleFilesRepository.FindOne(map[string]interface{}{"id": request.ParentId, "projectModuleId": request.ProjectModuleId, "projectModuleVersionId": request.ProjectModuleVersionId}); e != nil {
  250 + err = protocol.NewCustomMessage(1, "父节点不存在")
  251 + return
  252 + } else {
  253 + request.Path = filepath.Join(pfile.Path, request.Path)
  254 + }
  255 + }
  256 + if m, e := ProjectModuleFilesRepository.FindOne(map[string]interface{}{"fileKey": request.FileKey, "projectModuleId": request.ProjectModuleId, "projectModuleVersionId": request.ProjectModuleVersionId, "parentId": request.ParentId}); e == nil && m != nil {
  257 + err = protocol.NewCustomMessage(1, "已存在 filekey:"+request.FileKey)
  258 + rsp = m
  259 + return
  260 + }
  261 + if m, e := ProjectModuleFilesRepository.Save(request); e != nil {
  262 + err = e
  263 + return
  264 + } else {
  265 + rsp = m
  266 + }
  267 + return
  268 +}
  269 +func (svr *ProjectModuleFilesService) mkdirF(fileDir string, mid, vid, pid int64, repository domain.ProjectModuleFilesRepository) (files []*domain.ProjectModuleFiles, err error) {
  270 + fileDir = strings.ReplaceAll(fileDir, "/", "\\")
  271 + dirs := strings.Split(fileDir, "\\")
  272 + for _, dir := range dirs {
  273 + //save new dir
  274 + newItem := &domain.ProjectModuleFiles{
  275 + ProjectModuleId: mid,
  276 + ProjectModuleVersionId: vid,
  277 + ParentId: pid,
  278 + FileType: domain.Dir,
  279 + FileName: dir,
  280 + FileKey: dir,
  281 + CodeBlock: "",
  282 + Remark: "",
  283 + CreateTime: time.Now(),
  284 + UpdateTime: time.Now(),
  285 + Path: dir,
  286 + }
  287 + if file, e := svr.save(newItem, repository); e != nil {
  288 + if file != nil { //已存在
  289 + pid = file.Id
  290 + files = append(files, file)
  291 + }
  292 + continue
  293 + } else {
  294 + pid = file.Id
  295 + files = append(files, file)
  296 + }
  297 + }
  298 + return
  299 +}
  300 +
175 func NewProjectModuleFilesService(options map[string]interface{}) *ProjectModuleFilesService { 301 func NewProjectModuleFilesService(options map[string]interface{}) *ProjectModuleFilesService {
176 svr := &ProjectModuleFilesService{} 302 svr := &ProjectModuleFilesService{}
177 return svr 303 return svr
@@ -7,7 +7,7 @@ var POSTGRESQL_USER = "postgres" @@ -7,7 +7,7 @@ var POSTGRESQL_USER = "postgres"
7 var POSTGRESQL_PASSWORD = "123456" 7 var POSTGRESQL_PASSWORD = "123456"
8 var POSTGRESQL_HOST = "127.0.0.1" 8 var POSTGRESQL_HOST = "127.0.0.1"
9 var POSTGRESQL_PORT = "5432" 9 var POSTGRESQL_PORT = "5432"
10 -var DISABLE_CREATE_TABLE = false 10 +var DISABLE_CREATE_TABLE = true
11 var DISABLE_SQL_GENERATE_PRINT = false 11 var DISABLE_SQL_GENERATE_PRINT = false
12 12
13 func init() { 13 func init() {
@@ -2,6 +2,11 @@ package domain @@ -2,6 +2,11 @@ package domain
2 2
3 import "time" 3 import "time"
4 4
  5 +const (
  6 + Dir = iota + 1
  7 + File
  8 +)
  9 +
5 // ProjectModuleFiles 10 // ProjectModuleFiles
6 type ProjectModuleFiles struct { 11 type ProjectModuleFiles struct {
7 // 唯一标识 12 // 唯一标识
@@ -28,6 +33,8 @@ type ProjectModuleFiles struct { @@ -28,6 +33,8 @@ type ProjectModuleFiles struct {
28 CreateTime time.Time `json:"createTime"` 33 CreateTime time.Time `json:"createTime"`
29 // 更新时间 34 // 更新时间
30 UpdateTime time.Time `json:"updateTime"` 35 UpdateTime time.Time `json:"updateTime"`
  36 + // 当前文件相对路径 a/b/c
  37 + Path string `json:"path"`
31 } 38 }
32 39
33 type ProjectModuleFilesRepository interface { 40 type ProjectModuleFilesRepository interface {
@@ -11,8 +11,8 @@ type ProjectModuleDao struct { @@ -11,8 +11,8 @@ type ProjectModuleDao struct {
11 11
12 func (dao *ProjectModuleDao) DuplicateVersion(srcProjectId, srcVersionId int64, curVersionId int64) error { 12 func (dao *ProjectModuleDao) DuplicateVersion(srcProjectId, srcVersionId int64, curVersionId int64) error {
13 tx := dao.transactionContext.PgTx 13 tx := dao.transactionContext.PgTx
14 - _, err := tx.Exec(`insert into project_module_files(project_module_id,project_module_version_id,file_type,file_name,file_key,code_block,parent_id,sort,remark,create_time,update_time)  
15 -select ?,?,file_type,file_name,file_key,code_block,parent_id,sort,remark,now(),now() from project_module_files where project_module_id=? and project_module_version_id=?`, 14 + _, err := tx.Exec(`insert into project_module_files(project_module_id,project_module_version_id,file_type,file_name,file_key,code_block,parent_id,sort,remark,create_time,update_time,path)
  15 +select ?,?,file_type,file_name,file_key,code_block,parent_id,sort,remark,now(),now(),path from project_module_files where project_module_id=? and project_module_version_id=?`,
16 srcProjectId, curVersionId, srcProjectId, srcVersionId) 16 srcProjectId, curVersionId, srcProjectId, srcVersionId)
17 return err 17 return err
18 } 18 }
@@ -29,4 +29,6 @@ type ProjectModuleFiles struct { @@ -29,4 +29,6 @@ type ProjectModuleFiles struct {
29 CreateTime time.Time 29 CreateTime time.Time
30 // 更新时间 30 // 更新时间
31 UpdateTime time.Time 31 UpdateTime time.Time
  32 + // 当前文件相对路径 a/b/c
  33 + Path string
32 } 34 }
@@ -53,6 +53,9 @@ func (repository *ProjectModuleFilesRepository) FindOne(queryOptions map[string] @@ -53,6 +53,9 @@ func (repository *ProjectModuleFilesRepository) FindOne(queryOptions map[string]
53 query.SetWhere("id = ?", "id") 53 query.SetWhere("id = ?", "id")
54 query.SetWhere("project_module_id = ?", "projectModuleId") 54 query.SetWhere("project_module_id = ?", "projectModuleId")
55 query.SetWhere("project_module_version_id = ?", "projectModuleVersionId") 55 query.SetWhere("project_module_version_id = ?", "projectModuleVersionId")
  56 + query.SetWhere("parent_id = ?", "parentId")
  57 + query.SetWhere("file_key = ?", "fileKey")
  58 + query.SetWhere("path = ?", "path")
56 if err := query.First(); err != nil { 59 if err := query.First(); err != nil {
57 return nil, fmt.Errorf("query row not found") 60 return nil, fmt.Errorf("query row not found")
58 } 61 }
@@ -3,7 +3,6 @@ package beego @@ -3,7 +3,6 @@ package beego
3 import ( 3 import (
4 "github.com/astaxie/beego" 4 "github.com/astaxie/beego"
5 "github.com/astaxie/beego/plugins/cors" 5 "github.com/astaxie/beego/plugins/cors"
6 - "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/middleware"  
7 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers" 6 _ "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/port/beego/routers"
8 ) 7 )
9 8
@@ -16,10 +15,10 @@ func init() { @@ -16,10 +15,10 @@ func init() {
16 AllowCredentials: true, 15 AllowCredentials: true,
17 })) 16 }))
18 17
19 - beego.InsertFilter("/user/*", beego.BeforeExec, middleware.InspectRoleAccess(""))  
20 - beego.InsertFilter("/role/*", beego.BeforeExec, middleware.InspectRoleAccess(""))  
21 - beego.InsertFilter("/project_module/*", beego.BeforeExec, middleware.InspectRoleAccess(""))  
22 - beego.InsertFilter("/project_module_version/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))  
23 - beego.InsertFilter("/project_module_files/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))  
24 - beego.InsertFilter("/rbac/*", beego.BeforeExec, middleware.InspectRoleAccess("/role/*")) 18 + //beego.InsertFilter("/user/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
  19 + //beego.InsertFilter("/role/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
  20 + //beego.InsertFilter("/project_module/*", beego.BeforeExec, middleware.InspectRoleAccess(""))
  21 + //beego.InsertFilter("/project_module_version/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
  22 + //beego.InsertFilter("/project_module_files/*", beego.BeforeExec, middleware.InspectRoleAccess("/project_module/*"))
  23 + //beego.InsertFilter("/rbac/*", beego.BeforeExec, middleware.InspectRoleAccess("/role/*"))
25 } 24 }
@@ -5,6 +5,9 @@ import ( @@ -5,6 +5,9 @@ import (
5 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/project_module_files" 5 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/project_module_files"
6 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol" 6 "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
7 protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/project_module_files" 7 protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/project_module_files"
  8 + "io/ioutil"
  9 + "mime/multipart"
  10 + "sort"
8 ) 11 )
9 12
10 type ProjectModuleFilesController struct { 13 type ProjectModuleFilesController struct {
@@ -13,20 +16,20 @@ type ProjectModuleFilesController struct { @@ -13,20 +16,20 @@ type ProjectModuleFilesController struct {
13 16
14 // CreateProjectModuleFiles 17 // CreateProjectModuleFiles
15 // CreateProjectModuleFiles execute command create ProjectModuleFiles 18 // CreateProjectModuleFiles execute command create ProjectModuleFiles
16 -func (this *ProjectModuleFilesController) CreateProjectModuleFiles() { 19 +func (controller *ProjectModuleFilesController) CreateProjectModuleFiles() {
17 var ( 20 var (
18 msg *protocol.ResponseMessage 21 msg *protocol.ResponseMessage
19 svr = project_module_files.NewProjectModuleFilesService(nil) 22 svr = project_module_files.NewProjectModuleFilesService(nil)
20 request *protocolx.CreateProjectModuleFilesRequest 23 request *protocolx.CreateProjectModuleFilesRequest
21 ) 24 )
22 defer func() { 25 defer func() {
23 - this.Resp(msg) 26 + controller.Resp(msg)
24 }() 27 }()
25 - if err := this.JsonUnmarshal(&request); err != nil { 28 + if err := controller.JsonUnmarshal(&request); err != nil {
26 msg = protocol.NewResponseMessage(2, "") 29 msg = protocol.NewResponseMessage(2, "")
27 return 30 return
28 } 31 }
29 - header := this.GetRequestHeader(this.Ctx) 32 + header := controller.GetRequestHeader(controller.Ctx)
30 data, err := svr.CreateProjectModuleFiles(header, request) 33 data, err := svr.CreateProjectModuleFiles(header, request)
31 if err != nil { 34 if err != nil {
32 log.Error(err) 35 log.Error(err)
@@ -36,20 +39,20 @@ func (this *ProjectModuleFilesController) CreateProjectModuleFiles() { @@ -36,20 +39,20 @@ func (this *ProjectModuleFilesController) CreateProjectModuleFiles() {
36 39
37 // UpdateProjectModuleFiles 40 // UpdateProjectModuleFiles
38 // UpdateProjectModuleFiles execute command update ProjectModuleFiles 41 // UpdateProjectModuleFiles execute command update ProjectModuleFiles
39 -func (this *ProjectModuleFilesController) UpdateProjectModuleFiles() { 42 +func (controller *ProjectModuleFilesController) UpdateProjectModuleFiles() {
40 var ( 43 var (
41 msg *protocol.ResponseMessage 44 msg *protocol.ResponseMessage
42 svr = project_module_files.NewProjectModuleFilesService(nil) 45 svr = project_module_files.NewProjectModuleFilesService(nil)
43 request *protocolx.UpdateProjectModuleFilesRequest 46 request *protocolx.UpdateProjectModuleFilesRequest
44 ) 47 )
45 defer func() { 48 defer func() {
46 - this.Resp(msg) 49 + controller.Resp(msg)
47 }() 50 }()
48 - if err := this.JsonUnmarshal(&request); err != nil { 51 + if err := controller.JsonUnmarshal(&request); err != nil {
49 msg = protocol.NewResponseMessage(2, "") 52 msg = protocol.NewResponseMessage(2, "")
50 return 53 return
51 } 54 }
52 - header := this.GetRequestHeader(this.Ctx) 55 + header := controller.GetRequestHeader(controller.Ctx)
53 data, err := svr.UpdateProjectModuleFiles(header, request) 56 data, err := svr.UpdateProjectModuleFiles(header, request)
54 if err != nil { 57 if err != nil {
55 log.Error(err) 58 log.Error(err)
@@ -59,20 +62,20 @@ func (this *ProjectModuleFilesController) UpdateProjectModuleFiles() { @@ -59,20 +62,20 @@ func (this *ProjectModuleFilesController) UpdateProjectModuleFiles() {
59 62
60 // GetProjectModuleFiles 63 // GetProjectModuleFiles
61 // GetProjectModuleFiles execute query get ProjectModuleFiles 64 // GetProjectModuleFiles execute query get ProjectModuleFiles
62 -func (this *ProjectModuleFilesController) GetProjectModuleFiles() { 65 +func (controller *ProjectModuleFilesController) GetProjectModuleFiles() {
63 var ( 66 var (
64 msg *protocol.ResponseMessage 67 msg *protocol.ResponseMessage
65 svr = project_module_files.NewProjectModuleFilesService(nil) 68 svr = project_module_files.NewProjectModuleFilesService(nil)
66 request *protocolx.GetProjectModuleFilesRequest 69 request *protocolx.GetProjectModuleFilesRequest
67 ) 70 )
68 defer func() { 71 defer func() {
69 - this.Resp(msg) 72 + controller.Resp(msg)
70 }() 73 }()
71 - if err := this.JsonUnmarshal(&request); err != nil { 74 + if err := controller.JsonUnmarshal(&request); err != nil {
72 msg = protocol.NewResponseMessage(2, "") 75 msg = protocol.NewResponseMessage(2, "")
73 return 76 return
74 } 77 }
75 - header := this.GetRequestHeader(this.Ctx) 78 + header := controller.GetRequestHeader(controller.Ctx)
76 data, err := svr.GetProjectModuleFiles(header, request) 79 data, err := svr.GetProjectModuleFiles(header, request)
77 if err != nil { 80 if err != nil {
78 log.Error(err) 81 log.Error(err)
@@ -82,20 +85,20 @@ func (this *ProjectModuleFilesController) GetProjectModuleFiles() { @@ -82,20 +85,20 @@ func (this *ProjectModuleFilesController) GetProjectModuleFiles() {
82 85
83 // DeleteProjectModuleFiles 86 // DeleteProjectModuleFiles
84 // DeleteProjectModuleFiles execute command delete ProjectModuleFiles 87 // DeleteProjectModuleFiles execute command delete ProjectModuleFiles
85 -func (this *ProjectModuleFilesController) DeleteProjectModuleFiles() { 88 +func (controller *ProjectModuleFilesController) DeleteProjectModuleFiles() {
86 var ( 89 var (
87 msg *protocol.ResponseMessage 90 msg *protocol.ResponseMessage
88 svr = project_module_files.NewProjectModuleFilesService(nil) 91 svr = project_module_files.NewProjectModuleFilesService(nil)
89 request *protocolx.DeleteProjectModuleFilesRequest 92 request *protocolx.DeleteProjectModuleFilesRequest
90 ) 93 )
91 defer func() { 94 defer func() {
92 - this.Resp(msg) 95 + controller.Resp(msg)
93 }() 96 }()
94 - if err := this.JsonUnmarshal(&request); err != nil { 97 + if err := controller.JsonUnmarshal(&request); err != nil {
95 msg = protocol.NewResponseMessage(2, "") 98 msg = protocol.NewResponseMessage(2, "")
96 return 99 return
97 } 100 }
98 - header := this.GetRequestHeader(this.Ctx) 101 + header := controller.GetRequestHeader(controller.Ctx)
99 data, err := svr.DeleteProjectModuleFiles(header, request) 102 data, err := svr.DeleteProjectModuleFiles(header, request)
100 if err != nil { 103 if err != nil {
101 log.Error(err) 104 log.Error(err)
@@ -105,21 +108,68 @@ func (this *ProjectModuleFilesController) DeleteProjectModuleFiles() { @@ -105,21 +108,68 @@ func (this *ProjectModuleFilesController) DeleteProjectModuleFiles() {
105 108
106 // ListProjectModuleFiles 109 // ListProjectModuleFiles
107 // ListProjectModuleFiles execute query list ProjectModuleFiles 110 // ListProjectModuleFiles execute query list ProjectModuleFiles
108 -func (this *ProjectModuleFilesController) ListProjectModuleFiles() { 111 +func (controller *ProjectModuleFilesController) ListProjectModuleFiles() {
109 var ( 112 var (
110 msg *protocol.ResponseMessage 113 msg *protocol.ResponseMessage
111 svr = project_module_files.NewProjectModuleFilesService(nil) 114 svr = project_module_files.NewProjectModuleFilesService(nil)
112 request = &protocolx.ListProjectModuleFilesRequest{} 115 request = &protocolx.ListProjectModuleFilesRequest{}
113 ) 116 )
114 defer func() { 117 defer func() {
115 - this.Resp(msg) 118 + controller.Resp(msg)
116 }() 119 }()
117 - request.ProjectModuleVersionId, _ = this.GetInt64("projectModuleVersionId")  
118 - request.ProjectModuleId, _ = this.GetInt64("projectModuleId")  
119 - header := this.GetRequestHeader(this.Ctx) 120 + request.ProjectModuleVersionId, _ = controller.GetInt64("projectModuleVersionId")
  121 + request.ProjectModuleId, _ = controller.GetInt64("projectModuleId")
  122 + header := controller.GetRequestHeader(controller.Ctx)
120 data, err := svr.ListProjectModuleFiles(header, request) 123 data, err := svr.ListProjectModuleFiles(header, request)
121 if err != nil { 124 if err != nil {
122 log.Error(err) 125 log.Error(err)
123 } 126 }
124 msg = protocol.NewResponseMessageData(data, err) 127 msg = protocol.NewResponseMessageData(data, err)
125 } 128 }
  129 +
  130 +// Import
  131 +// 权限列表
  132 +func (controller *ProjectModuleFilesController) Import() {
  133 + var (
  134 + msg *protocol.ResponseMessage
  135 + svr = project_module_files.NewProjectModuleFilesService(nil)
  136 + request = &protocolx.ImportRequest{}
  137 + )
  138 + defer func() {
  139 + controller.Resp(msg)
  140 + }()
  141 + header := controller.GetRequestHeader(controller.Ctx)
  142 + request.ProjectModuleId, _ = controller.GetInt64("projectModuleId")
  143 + request.ProjectModuleVersionId, _ = controller.GetInt64("projectModuleVersionId")
  144 + request.ParentId, _ = controller.GetInt64("parentId")
  145 + for _, k := range getSortFileKeys(controller.Ctx.Request.MultipartForm.File) {
  146 + v := controller.Ctx.Request.MultipartForm.File[k]
  147 + log.Info(k, v[0].Filename, v[0].Size)
  148 + for i := range v {
  149 + if v[i].Size > 50*1024 {
  150 + continue
  151 + }
  152 + f, _ := v[i].Open()
  153 + data, _ := ioutil.ReadAll(f)
  154 + f.Close()
  155 + request.ImportFiles = append(request.ImportFiles, protocolx.ImportFile{
  156 + FileName: v[i].Filename,
  157 + FileContent: data,
  158 + })
  159 + }
  160 + }
  161 + data, err := svr.Import(header, request)
  162 + if err != nil {
  163 + log.Error(err)
  164 + }
  165 + msg = protocol.NewResponseMessageData(data, err)
  166 +}
  167 +
  168 +//获取排序键值
  169 +func getSortFileKeys(files map[string][]*multipart.FileHeader) (keys []string) {
  170 + for k, _ := range files {
  171 + keys = append(keys, k)
  172 + }
  173 + sort.Strings(keys)
  174 + return
  175 +}
@@ -11,4 +11,6 @@ func init() { @@ -11,4 +11,6 @@ func init() {
11 beego.Router("/project_module_files/:projectModuleFilesId", &controllers.ProjectModuleFilesController{}, "GET:GetProjectModuleFiles") 11 beego.Router("/project_module_files/:projectModuleFilesId", &controllers.ProjectModuleFilesController{}, "GET:GetProjectModuleFiles")
12 beego.Router("/project_module_files/:projectModuleFilesId", &controllers.ProjectModuleFilesController{}, "DELETE:DeleteProjectModuleFiles") 12 beego.Router("/project_module_files/:projectModuleFilesId", &controllers.ProjectModuleFilesController{}, "DELETE:DeleteProjectModuleFiles")
13 beego.Router("/project_module_files/", &controllers.ProjectModuleFilesController{}, "GET:ListProjectModuleFiles") 13 beego.Router("/project_module_files/", &controllers.ProjectModuleFilesController{}, "GET:ListProjectModuleFiles")
  14 +
  15 + beego.Router("/project_module_files/import", &controllers.ProjectModuleFilesController{}, "post:Import")
14 } 16 }
  1 +package project_module_files
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +type ImportRequest struct {
  9 + // 项目模块编号
  10 + ProjectModuleId int64 `json:"projectModuleId,omitempty"`
  11 + // 项目模块版本编号
  12 + ProjectModuleVersionId int64 `json:"projectModuleVersionId,omitempty"`
  13 + // 父节点编号
  14 + ParentId int64 `json:"parentId,omitempty"`
  15 + // 导入文件列表
  16 + ImportFiles []ImportFile
  17 +}
  18 +
  19 +type ImportFile struct {
  20 + FileName string
  21 + FileContent []byte
  22 +}
  23 +
  24 +func (ImportRequest *ImportRequest) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(ImportRequest)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + for _, validErr := range valid.Errors {
  32 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  33 + }
  34 + }
  35 + return nil
  36 +}
  1 +package project_module_files
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +type ImportResponse struct {
  9 +}
  10 +
  11 +func (ImportResponse *ImportResponse) ValidateCommand() error {
  12 + valid := validation.Validation{}
  13 + b, err := valid.Valid(ImportResponse)
  14 + if err != nil {
  15 + return err
  16 + }
  17 + if !b {
  18 + for _, validErr := range valid.Errors {
  19 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  20 + }
  21 + }
  22 + return nil
  23 +}