access.go 4.9 KB
package access

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/access"
)

type AccessService struct {
}

func (svr *AccessService) CreateAccess(header *protocol.RequestHeader, request *protocolx.CreateAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.CreateAccessResponse{}
	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()
	}()
	newAccess := &domain.Access{
		AccessName: request.AccessName,
		AccessCode: request.AccessCode,
		AccessType: request.AccessType,
		Sort:       request.Sort,
		Object:     request.Object,
		Action:     request.Action,
		Module:     request.Module,
	}

	var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	if m, err := AccessRepository.Save(newAccess); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) UpdateAccess(header *protocol.RequestHeader, request *protocolx.UpdateAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.UpdateAccessResponse{}
	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 AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access *domain.Access
	if access, err = AccessRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if err = access.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if access, err = AccessRepository.Save(access); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) GetAccess(header *protocol.RequestHeader, request *protocolx.GetAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.GetAccessResponse{}
	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 AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access *domain.Access
	if access, err = AccessRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = access
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) DeleteAccess(header *protocol.RequestHeader, request *protocolx.DeleteAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.DeleteAccessResponse{}
	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 AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access *domain.Access
	if access, err = AccessRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if access, err = AccessRepository.Remove(access); err != nil {
		return
	}
	rsp = access
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) ListAccess(header *protocol.RequestHeader, request *protocolx.ListAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.ListAccessResponse{}
	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 AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access []*domain.Access
	var total int64
	if total, access, err = AccessRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  access,
	}
	err = transactionContext.CommitTransaction()
	return
}

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