project_module_version.go 5.7 KB
package project_module_version

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

type ProjectModuleVersionService struct {
}

func (svr *ProjectModuleVersionService) CreateProjectModuleVersion(header *protocol.RequestHeader, request *protocolx.CreateProjectModuleVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.CreateProjectModuleVersionResponse{}
	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()
	}()
	newProjectModuleVersion := &domain.ProjectModuleVersion{
		Version:     request.Version,
		Description: request.Description,
		Status:      request.Status,
		CreateTime:  time.Now(),
		UpdateTime:  time.Now(),
	}

	var ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	if m, err := ProjectModuleVersionRepository.Save(newProjectModuleVersion); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleVersionService) UpdateProjectModuleVersion(header *protocol.RequestHeader, request *protocolx.UpdateProjectModuleVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.UpdateProjectModuleVersionResponse{}
	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 ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	var projectModuleVersion *domain.ProjectModuleVersion
	if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if err = projectModuleVersion.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if projectModuleVersion, err = ProjectModuleVersionRepository.Save(projectModuleVersion); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleVersionService) GetProjectModuleVersion(header *protocol.RequestHeader, request *protocolx.GetProjectModuleVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.GetProjectModuleVersionResponse{}
	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 ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	var projectModuleVersion *domain.ProjectModuleVersion
	if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = projectModuleVersion
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleVersionService) DeleteProjectModuleVersion(header *protocol.RequestHeader, request *protocolx.DeleteProjectModuleVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.DeleteProjectModuleVersionResponse{}
	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 ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	var projectModuleVersion *domain.ProjectModuleVersion
	if projectModuleVersion, err = ProjectModuleVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if projectModuleVersion, err = ProjectModuleVersionRepository.Remove(projectModuleVersion); err != nil {
		return
	}
	rsp = projectModuleVersion
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ProjectModuleVersionService) ListProjectModuleVersion(header *protocol.RequestHeader, request *protocolx.ListProjectModuleVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ListProjectModuleVersionResponse{}
	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 ProjectModuleVersionRepository, _ = factory.CreateProjectModuleVersionRepository(transactionContext)
	var projectModuleVersion []*domain.ProjectModuleVersion
	var total int64
	if total, projectModuleVersion, err = ProjectModuleVersionRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  projectModuleVersion,
	}
	err = transactionContext.CommitTransaction()
	return
}

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