project_module.go 6.0 KB
package project_module

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"
	"time"
)

type ProjectModuleService struct {
}

func (svr *ProjectModuleService) CreateProjectModule(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _             = factory.CreateTransactionContext(nil)
		ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	)
	rsp = &protocolx.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}); 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:          0,
		CreateTime:      time.Now(),
		UpdateTime:      time.Now(),
	}); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) UpdateProjectModule(header *protocol.RequestHeader, request *protocolx.UpdateProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.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(common.ObjectToMap(request)); err != nil {
		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 *protocolx.GetProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.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 = projectModule
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) DeleteProjectModule(header *protocol.RequestHeader, request *protocolx.DeleteProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.DeleteProjectModuleResponse{}
	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(common.ObjectToMap(request)); err != nil {
		return
	}
	if projectModule, err = ProjectModuleRepository.Remove(projectModule); err != nil {
		return
	}
	rsp = projectModule
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleService) ListProjectModule(header *protocol.RequestHeader, request *protocolx.ListProjectModuleRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &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(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  projectModule,
	}
	err = transactionContext.CommitTransaction()
	return
}

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