project_module.go 7.1 KB
package service

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/application/projectModule/command"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/projectModule/query"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
	"time"
)

type ProjectModuleService struct {
}

func (svr *ProjectModuleService) CreateProjectModule(header *protocol.RequestHeader, request *command.CreateProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _             = factory.CreateTransactionContext(nil)
		ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	)
	rsp = &command.CreateProjectModuleResponse{}
	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()
	}()
	newProjectModule := &domain.ProjectModule{
		ProjectName: request.ProjectName,
		ProjectKey:  request.ProjectKey,
		Description: request.Description,
		Status:      0,
		CreateTime:  time.Now(),
		UpdateTime:  time.Now(),
	}

	var ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
	if _, err = ProjectModuleRepository.FindOne(map[string]interface{}{"projectKey": request.ProjectKey, "status": domain.StatusNormal}); err == nil {
		err = protocol.NewCustomMessage(1, "项目键值已存在:"+request.ProjectKey)
		return
	}
	if m, err := ProjectModuleRepository.Save(newProjectModule); err != nil {
		return nil, err
	} else {
		rsp = m
	}

	if _, err = ProjectModuleVersionRepository.Save(&domain.ProjectModuleVersion{
		ProjectModuleId: newProjectModule.Id,
		Version:         "latest",
		Description:     "latest version",
		Status:          domain.StatusNormal,
		CreateTime:      time.Now(),
		UpdateTime:      time.Now(),
	}); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) UpdateProjectModule(header *protocol.RequestHeader, request *command.UpdateProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.UpdateProjectModuleResponse{}
	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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
	var projectModule *domain.ProjectModule
	if projectModule, err = ProjectModuleRepository.FindOne(map[string]interface{}{"id": request.Id}); err != nil {
		err = protocol.NewCustomMessage(1, "模板项目不存在")
		return
	}
	if projectModule.ProjectKey != request.ProjectKey {
		if _, err = ProjectModuleRepository.FindOne(map[string]interface{}{"projectKey": request.ProjectKey, "status": domain.StatusNormal}); err == nil {
			err = protocol.NewCustomMessage(1, "项目键值已存在:"+request.ProjectKey)
			return
		}
	}
	if err = projectModule.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if projectModule, err = ProjectModuleRepository.Save(projectModule); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) GetProjectModule(header *protocol.RequestHeader, request *query.GetProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &query.GetProjectModuleResponse{}
	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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
	var projectModule *domain.ProjectModule
	if projectModule, err = ProjectModuleRepository.FindOne(common.ObjectToMap(request)); err != nil {
		err = protocol.NewCustomMessage(1, "模板项目不存在")
		return
	}
	rsp = utils.LoadCustomField(projectModule, "Id", "ProjectName", "ProjectKey", "Description", "Status", "CreateTime", "UpdateTime")
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) DeleteProjectModule(header *protocol.RequestHeader, request *command.DeleteProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.DeleteProjectModuleResponse{}
	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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
	var projectModule *domain.ProjectModule
	if projectModule, err = ProjectModuleRepository.FindOne(map[string]interface{}{"id": request.Id}); err != nil {
		return
	}
	if err = projectModule.Update(map[string]interface{}{"status": 2}); err != nil {
		return
	}
	if projectModule, err = ProjectModuleRepository.Save(projectModule); err != nil {
		return
	}
	rsp = projectModule
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) ListProjectModule(header *protocol.RequestHeader, request *query.ListProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	//listProjectModuleResponse := &protocolx.ListProjectModuleResponse{}
	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 ProjectModuleRepository, _ = factory.CreateProjectModuleRepository(transactionContext)
	var projectModule []*domain.ProjectModule
	var total int64
	if total, projectModule, err = ProjectModuleRepository.Find(map[string]interface{}{"offset": (request.PageNumber - 1) * request.PageSize, "limit": request.PageSize, "sortByCreateTime": "DESC", "status": domain.StatusNormal, "searchByText": request.ProjectName}); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"totalRow":   total,
		"pageNumber": request.PageNumber,
		"lists":      utils.LoadCustomField(projectModule, "Id", "ProjectName", "ProjectKey", "Description", "Status", "CreateTime", "UpdateTime"), //listProjectModuleResponse.LoadDto(projectModule),
	}
	err = transactionContext.CommitTransaction()
	return
}

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