project_module_files.go 6.2 KB
package project_module_files

import (
	"github.com/tiptok/gocomm/common"
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
	"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"
	"time"
)

type ProjectModuleFilesService struct {
}

func (svr *ProjectModuleFilesService) CreateProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleFilesRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.CreateProjectModuleFilesResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	newProjectModuleFiles := &domain.ProjectModuleFiles{
		ProjectModuleId:        request.ProjectModuleId,
		ProjectModuleVersionId: request.ProjectModuleVersionId,
		ParentId:               request.ProjectModuleVersionId,
		FileType:               request.FileType,
		FileName:               request.FileName,
		FileKey:                request.FileKey,
		CodeBlock:              request.CodeBlock,
		Remark:                 request.Remark,
		CreateTime:             time.Now(),
		UpdateTime:             time.Now(),
	}

	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)
		return
	}
	if m, err := ProjectModuleFilesRepository.Save(newProjectModuleFiles); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleFilesService) UpdateProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.UpdateProjectModuleFilesRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.UpdateProjectModuleFilesResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
	var projectModuleFiles *domain.ProjectModuleFiles
	if projectModuleFiles, err = ProjectModuleFilesRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if err = projectModuleFiles.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if projectModuleFiles, err = ProjectModuleFilesRepository.Save(projectModuleFiles); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleFilesService) GetProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.GetProjectModuleFilesRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.GetProjectModuleFilesResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
	var projectModuleFiles *domain.ProjectModuleFiles
	if projectModuleFiles, err = ProjectModuleFilesRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = projectModuleFiles
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleFilesService) DeleteProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.DeleteProjectModuleFilesRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.DeleteProjectModuleFilesResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
	var projectModuleFiles *domain.ProjectModuleFiles
	if projectModuleFiles, err = ProjectModuleFilesRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if projectModuleFiles, err = ProjectModuleFilesRepository.Remove(projectModuleFiles); err != nil {
		return
	}
	rsp = projectModuleFiles
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleFilesService) ListProjectModuleFiles(header *protocol.RequestHeader, request *protocolx.ListProjectModuleFilesRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ListProjectModuleFilesResponse{}
	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()
	}()

	var ProjectModuleFilesRepository, _ = factory.CreateProjectModuleFilesRepository(transactionContext)
	var projectModuleFiles []*domain.ProjectModuleFiles
	var total int64
	if total, projectModuleFiles, err = ProjectModuleFilesRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  projectModuleFiles,
	}
	err = transactionContext.CommitTransaction()
	return
}

func NewProjectModuleFilesService(options map[string]interface{}) *ProjectModuleFilesService {
	svr := &ProjectModuleFilesService{}
	return svr
}