column_setting.go 13.4 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/columnSetting/command"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/columnSetting/query"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"strconv"
	"time"
)

// 栏目设置
type ColumnSettingService struct {
}

/**
 * @Author SteveChan
 * @Description //新增用户时,初始化栏目设置
 * @Date 23:13 2021/1/26
 * @Param
 * @return
 **/
func (columnSettingService *ColumnSettingService) CreateColumnSetting(createColumnSettingCommand *command.CreateColumnSettingCommand) (interface{}, error) {
	if err := createColumnSettingCommand.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 errStart := transactionContext.StartTransaction(); errStart != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	newColumnSetting := &domain.ColumnSetting{
		Description:  createColumnSettingCommand.Description,
		UserName:     createColumnSettingCommand.UserName,
		CompanyId:    createColumnSettingCommand.CompanyId,
		Uid:          createColumnSettingCommand.Uid,
		Key:          constant.ORDER_BASE,          // 订单栏目模块名称
		Value:        domain.DefaultColumns,        // 栏目值数组,创建用户时使用默认栏目设置
		InvalidValue: domain.DefaultInvalidColumns, // 初始化无效栏目数组
		CreatedAt:    time.Now(),
		UpdatedAt:    time.Now(),
	}
	var columnSettingRepository domain.ColumnSettingRepository
	if value, errFac := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); errFac != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFac.Error())
	} else {
		columnSettingRepository = value
	}
	if columnSettingSaved, errSaved := columnSettingRepository.Save(newColumnSetting); errSaved != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errSaved.Error())
	} else {
		if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
		}
		return columnSettingSaved, nil
	}
}

/**
 * @Author SteveChan
 * @Description // 返回栏目设置
 * @Date 23:14 2021/1/26
 * @Param
 * @return
 **/
func (columnSettingService *ColumnSettingService) GetColumnSetting(getColumnSettingQuery *query.GetColumnSettingQuery) (interface{}, error) {
	if err := getColumnSettingQuery.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 columnSettingRepository domain.ColumnSettingRepository
	if value, errFact := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); errFact != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFact.Error())
	} else {
		columnSettingRepository = value
	}
	columnSettingFound, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": getColumnSettingQuery.Uid, "companyId": getColumnSettingQuery.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSettingFound == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(getColumnSettingQuery.Uid, 10)))
	} else {
		if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
		}

		// 数据mapper
		orderBaseColumns := map[string]interface{}{
			"orderBaseColumns": map[string]interface{}{
				"selected":   columnSettingFound.Value,
				"unselected": columnSettingFound.InvalidValue,
			},
		}

		return orderBaseColumns, nil
	}
}

// 返回栏目设置增删改查列表
func (columnSettingService *ColumnSettingService) ListColumnSetting(listColumnSettingQuery *query.ListColumnSettingQuery) (interface{}, error) {
	if err := listColumnSettingQuery.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 columnSettingRepository domain.ColumnSettingRepository
	if value, err := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		columnSettingRepository = value
	}
	if count, columnSettings, err := columnSettingRepository.Find(domain.ColumnSettingFindQuery{
		Offset: listColumnSettingQuery.Offset,
		Limit:  listColumnSettingQuery.Limit,
	}); 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,
			"column_settings": columnSettings,
		}, nil
	}
}

/**
 * @Author SteveChan
 * @Description // 删除用户时移除栏目设置
 * @Date 23:14 2021/1/26
 * @Param
 * @return
 **/
func (columnSettingService *ColumnSettingService) RemoveColumnSetting(removeColumnSettingCommand *command.RemoveColumnSettingCommand) (interface{}, error) {
	if err := removeColumnSettingCommand.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 errSt := transactionContext.StartTransaction(); errSt != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errSt.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	var columnSettingRepository domain.ColumnSettingRepository
	if value, errFac := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); errFac != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFac.Error())
	} else {
		columnSettingRepository = value
	}
	columnSettingFound, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": removeColumnSettingCommand.Uid, "companyId": removeColumnSettingCommand.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSettingFound == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(removeColumnSettingCommand.Uid, 10)))
	}
	if columnSettingRemoved, _, errRm := columnSettingRepository.Remove(columnSettingFound, nil); errRm != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errRm.Error())
	} else {
		if errTr := transactionContext.CommitTransaction(); errTr != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, errRm.Error())
		}

		// 数据mapper
		orderBaseColumns := map[string]interface{}{
			"orderBaseColumns": map[string]interface{}{
				"selected":   columnSettingRemoved.Value,
				"unselected": columnSettingRemoved.InvalidValue,
			},
		}

		return orderBaseColumns, nil
	}
}

/**
 * @Author SteveChan
 * @Description // 重置栏目设置
 * @Date 23:15 2021/1/26
 * @Param
 * @return
 **/
func (columnSettingService *ColumnSettingService) ResetColumn(resetColumnCommand *command.ResetColumnCommand) (interface{}, error) {
	if err := resetColumnCommand.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 errStart := transactionContext.StartTransaction(); errStart != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	var columnSettingRepository domain.ColumnSettingRepository
	if value, errFact := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); errFact != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFact.Error())
	} else {
		columnSettingRepository = value
	}
	columnSettingFound, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": resetColumnCommand.Uid, "companyId": resetColumnCommand.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSettingFound == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(resetColumnCommand.Uid, 10)))
	}
	if errUpdated := columnSettingFound.Update(tool_funs.SimpleStructToMap(resetColumnCommand)); errUpdated != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, errUpdated.Error())
	}
	if columnSettingSaved, errSaved := columnSettingRepository.Save(columnSettingFound); errSaved != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errSaved.Error())
	} else {
		if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
		}

		// 数据mapper
		orderBaseColumns := map[string]interface{}{
			"orderBaseColumns": map[string]interface{}{
				"selected":   columnSettingSaved.Value,
				"unselected": columnSettingSaved.InvalidValue,
			},
		}

		return orderBaseColumns, nil
	}
}

/**
 * @Author SteveChan
 * @Description // 更新栏目设置
 * @Date 23:15 2021/1/26
 * @Param
 * @return
 **/
func (columnSettingService *ColumnSettingService) UpdateColumnSetting(updateColumnSettingCommand *command.UpdateColumnSettingCommand) (interface{}, error) {
	if err := updateColumnSettingCommand.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 errStart := transactionContext.StartTransaction(); errStart != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	var columnSettingRepository domain.ColumnSettingRepository
	if value, errFact := factory.CreateColumnSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); errFact != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errFact.Error())
	} else {
		columnSettingRepository = value
	}
	updateColumnSettingCommand.Value = updateColumnSettingCommand.Selected
	updateColumnSettingCommand.InvalidValue = updateColumnSettingCommand.UnSelected
	columnSetting, err := columnSettingRepository.FindOne(map[string]interface{}{"uid": updateColumnSettingCommand.Uid, "companyId": updateColumnSettingCommand.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if columnSetting == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(updateColumnSettingCommand.Uid, 10)))
	}
	if errUpdated := columnSetting.Update(tool_funs.SimpleStructToMap(updateColumnSettingCommand)); errUpdated != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, errUpdated.Error())
	}
	if columnSettingSaved, errSaved := columnSettingRepository.Save(columnSetting); errSaved != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, errSaved.Error())
	} else {
		if errCommit := transactionContext.CommitTransaction(); errCommit != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, errCommit.Error())
		}

		// 数据mapper
		orderBaseColumns := map[string]interface{}{
			"orderBaseColumns": map[string]interface{}{
				"selected":   columnSettingSaved.Value,
				"unselected": columnSettingSaved.InvalidValue,
			},
		}

		return orderBaseColumns, nil
	}
}

func NewColumnSettingService(options map[string]interface{}) *ColumnSettingService {
	newColumnSettingService := &ColumnSettingService{}
	return newColumnSettingService
}