client_version.go 5.7 KB
package client_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/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
	protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/client_version"
	"time"
)

type ClientVersionService struct {
}

func (svr *ClientVersionService) CreateClientVersion(header *protocol.RequestHeader, request *protocolx.CreateClientVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.CreateClientVersionResponse{}
	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()
	}()
	newClientVersion := &domain.ClientVersion{
		Commiter:          header.UserId,
		ProjectName:       request.ProjectName,
		Version:           request.Version,
		Title:             request.Title,
		Remark:            request.Remark,
		ClientPackageInfo: request.ClientPackageInfo,
		CreateTime:        time.Now(),
	}

	var ClientVersionRepository, _ = factory.CreateClientVersionRepository(transactionContext)
	if m, err := ClientVersionRepository.Save(newClientVersion); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ClientVersionService) UpdateClientVersion(header *protocol.RequestHeader, request *protocolx.UpdateClientVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.UpdateClientVersionResponse{}
	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 ClientVersionRepository, _ = factory.CreateClientVersionRepository(transactionContext)
	var clientVersion *domain.ClientVersion
	if clientVersion, err = ClientVersionRepository.FindOne(map[string]interface{}{"id": request.Id}); err != nil {
		return
	}
	if err = clientVersion.Update((utils.LoadCustomField(request, header.BodyKeys...)).(map[string]interface{})); err != nil {
		return
	}
	if clientVersion, err = ClientVersionRepository.Save(clientVersion); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ClientVersionService) GetClientVersion(header *protocol.RequestHeader, request *protocolx.GetClientVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.GetClientVersionResponse{}
	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 ClientVersionRepository, _ = factory.CreateClientVersionRepository(transactionContext)
	var clientVersion *domain.ClientVersion
	if clientVersion, err = ClientVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = clientVersion
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ClientVersionService) DeleteClientVersion(header *protocol.RequestHeader, request *protocolx.DeleteClientVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.DeleteClientVersionResponse{}
	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 ClientVersionRepository, _ = factory.CreateClientVersionRepository(transactionContext)
	var clientVersion *domain.ClientVersion
	if clientVersion, err = ClientVersionRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if clientVersion, err = ClientVersionRepository.Remove(clientVersion); err != nil {
		return
	}
	rsp = clientVersion
	err = transactionContext.CommitTransaction()
	return
}

func (svr *ClientVersionService) ListClientVersion(header *protocol.RequestHeader, request *protocolx.ListClientVersionRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ListClientVersionResponse{}
	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 ClientVersionRepository, _ = factory.CreateClientVersionRepository(transactionContext)
	var clientVersion []*domain.ClientVersion
	var total int64
	if total, clientVersion, err = ClientVersionRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"totalRow":   total,
		"pageNumber": (request.Offset + request.Limit) / request.Limit,
		"lists":      utils.LoadCustomField(clientVersion, "Id", "ProjectName", "Version", "Title", "Remark", "CreateTime"),
	}
	err = transactionContext.CommitTransaction()
	return
}

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