operator_log.go 5.3 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/operatorLog/command"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/operatorLog/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"
)

type OperatorLogService struct {
}

func (svr *OperatorLogService) CreateOperatorLog(header *protocol.RequestHeader, request *command.CreateOperatorLogRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.CreateOperatorLogResponse{}
	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()
	}()
	newOperatorLog := &domain.OperatorLog{
		Operator: request.Commiter,
		Code:     request.Code,
		Content:  request.Content,
	}

	var OperatorLogRepository, _ = factory.CreateOperatorLogRepository(transactionContext)
	if m, err := OperatorLogRepository.Save(newOperatorLog); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *OperatorLogService) UpdateOperatorLog(header *protocol.RequestHeader, request *command.UpdateOperatorLogRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.UpdateOperatorLogResponse{}
	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 OperatorLogRepository, _ = factory.CreateOperatorLogRepository(transactionContext)
	var operatorLog *domain.OperatorLog
	if operatorLog, err = OperatorLogRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if err = operatorLog.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if operatorLog, err = OperatorLogRepository.Save(operatorLog); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *OperatorLogService) GetOperatorLog(header *protocol.RequestHeader, request *query.GetOperatorLogRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &query.GetOperatorLogResponse{}
	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 OperatorLogRepository, _ = factory.CreateOperatorLogRepository(transactionContext)
	var operatorLog *domain.OperatorLog
	if operatorLog, err = OperatorLogRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = operatorLog
	err = transactionContext.CommitTransaction()
	return
}

func (svr *OperatorLogService) DeleteOperatorLog(header *protocol.RequestHeader, request *command.DeleteOperatorLogRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.DeleteOperatorLogResponse{}
	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 OperatorLogRepository, _ = factory.CreateOperatorLogRepository(transactionContext)
	var operatorLog *domain.OperatorLog
	if operatorLog, err = OperatorLogRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if operatorLog, err = OperatorLogRepository.Remove(operatorLog); err != nil {
		return
	}
	rsp = operatorLog
	err = transactionContext.CommitTransaction()
	return
}

func (svr *OperatorLogService) ListOperatorLog(header *protocol.RequestHeader, request *query.ListOperatorLogRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &query.ListOperatorLogResponse{}
	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 OperatorLogRepository, _ = factory.CreateOperatorLogRepository(transactionContext)
	var operatorLog []*domain.OperatorLog
	var total int64
	if total, operatorLog, err = OperatorLogRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  utils.LoadCustomField(operatorLog, "Id", "Operator", "Content", "CreateTime"),
	}
	err = transactionContext.CommitTransaction()
	return
}

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