customer_value.go 10.6 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/customerValue/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/customerValue/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)

// 客户价值服务
type CustomerValueService struct {
}

// 创建客户价值
func (customerValueService *CustomerValueService) CreateCustomerValue(createCustomerValueCommand *command.CreateCustomerValueCommand) (interface{}, error) {
	if err := createCustomerValueCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	newCustomerValue := &domain.CustomerValue{
		CompanyId:         createCustomerValueCommand.CompanyId,
		CustomerValueName: createCustomerValueCommand.CustomerValueName,
	}
	var customerValueRepository domain.CustomerValueRepository
	if value, err := factory.CreateCustomerValueRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customerValueRepository = value
	}
	if count, _, err := customerValueRepository.Find(map[string]interface{}{
		"customerValueName": createCustomerValueCommand.CustomerValueName,
		"companyId":         createCustomerValueCommand.CompanyId,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称")
		}
	}
	if customerValue, err := customerValueRepository.Save(newCustomerValue); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return customerValue, nil
	}
}

// 返回客户价值
func (customerValueService *CustomerValueService) GetCustomerValue(getCustomerValueQuery *query.GetCustomerValueQuery) (interface{}, error) {
	if err := getCustomerValueQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var customerValueRepository domain.CustomerValueRepository
	if value, err := factory.CreateCustomerValueRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customerValueRepository = value
	}
	customerValue, err := customerValueRepository.FindOne(map[string]interface{}{"customerValueId": getCustomerValueQuery.CustomerValueId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if customerValue == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getCustomerValueQuery.CustomerValueId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return customerValue, nil
	}
}

// 更新客户价值
func (customerValueService *CustomerValueService) UpdateCustomerValue(updateCustomerValueCommand *command.UpdateCustomerValueCommand) (interface{}, error) {
	if err := updateCustomerValueCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var customerValueRepository domain.CustomerValueRepository
	if value, err := factory.CreateCustomerValueRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customerValueRepository = value
	}
	customerValue, err := customerValueRepository.FindOne(map[string]interface{}{"customerValueId": updateCustomerValueCommand.CustomerValueId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if customerValue == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateCustomerValueCommand.CustomerValueId)))
	}
	if count, customerValues, err := customerValueRepository.Find(map[string]interface{}{
		"customerValueName": updateCustomerValueCommand.CustomerValueName,
		"companyId":         customerValue.CompanyId,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 && customerValues[0].CustomerValueId != customerValue.CustomerValueId {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称")
		}
	}
	if err := customerValue.Update(tool_funs.SimpleStructToMap(updateCustomerValueCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if customerValue, err := customerValueRepository.Save(customerValue); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return customerValue, nil
	}
}

// 移除客户价值
func (customerValueService *CustomerValueService) RemoveCustomerValue(removeCustomerValueCommand *command.RemoveCustomerValueCommand) (interface{}, error) {
	if err := removeCustomerValueCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var customerValueRepository domain.CustomerValueRepository
	if value, err := factory.CreateCustomerValueRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customerValueRepository = value
	}
	customerValue, err := customerValueRepository.FindOne(map[string]interface{}{"customerValueId": removeCustomerValueCommand.CustomerValueId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if customerValue == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeCustomerValueCommand.CustomerValueId)))
	}
	var taskRepository domain.TaskRepository
	if value, err := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskRepository = value
	}
	if count, _, err := taskRepository.Find(map[string]interface{}{
		"customerValues": []int{customerValue.CustomerValueId},
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该标签已被使用,不可删除")
		}
	}
	if customerValue, err := customerValueRepository.Remove(customerValue); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return customerValue, nil
	}
}

// 返回客户价值列表
func (customerValueService *CustomerValueService) ListCustomerValue(listCustomerValueQuery *query.ListCustomerValueQuery) (interface{}, error) {
	if err := listCustomerValueQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var customerValueRepository domain.CustomerValueRepository
	if value, err := factory.CreateCustomerValueRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customerValueRepository = value
	}
	if count, customerValues, err := customerValueRepository.Find(tool_funs.SimpleStructToMap(listCustomerValueQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return map[string]interface{}{
			"count":          count,
			"customerValues": customerValues,
		}, nil
	}
}

func NewCustomerValueService(options map[string]interface{}) *CustomerValueService {
	newCustomerValueService := &CustomerValueService{}
	return newCustomerValueService
}