system_setting.go 7.3 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/systemSetting/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/systemSetting/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/domain"
)

// 用户的系统设置
type SystemSettingService struct {
}

// 返回用户的系统设置
func (systemSettingService *SystemSettingService) GetSystemSetting(getSystemSettingQuery *query.GetSystemSettingQuery) (interface{}, error) {
	if err := getSystemSettingQuery.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 systemSettingRepository domain.SystemSettingRepository
	if value, err := factory.CreateSystemSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		systemSettingRepository = value
	}
	_, systemSettings, err := systemSettingRepository.Find(map[string]interface{}{
		"companyId":   getSystemSettingQuery.CompanyId,
		"settingCode": getSystemSettingQuery.SettingCode,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var systemSetting domain.SystemSetting
	if len(systemSettings) == 0 {
		//获取默认设置项
		setting := domain.SystemSetting{}
		defaultSet := setting.GetDefaultSetting(getSystemSettingQuery.SettingCode)
		if len(defaultSet) == 0 {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取设置项失败,settingCode="+getSystemSettingQuery.SettingCode)
		}
		defaultSet[0].CompanyId = getSystemSettingQuery.CompanyId
		systemSetting = defaultSet[0]
	} else {
		systemSetting = *systemSettings[0]
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return systemSetting, nil
}

// 返回用户的系统设置列表
func (systemSettingService *SystemSettingService) ListSystemSetting(listSystemSettingQuery *query.ListSystemSettingQuery) (interface{}, error) {
	if err := listSystemSettingQuery.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 systemSettingRepository domain.SystemSettingRepository
	if value, err := factory.CreateSystemSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		systemSettingRepository = value
	}
	queryOption := map[string]interface{}{
		"companyId": listSystemSettingQuery.CompanyId,
	}
	if len(listSystemSettingQuery.SettingCode) > 0 {
		queryOption["settingCode"] = listSystemSettingQuery.SettingCode
	}
	_, systemSettings, err := systemSettingRepository.Find(queryOption)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	settingMap := map[string]*domain.SystemSetting{}
	for i := range systemSettings {
		code := systemSettings[i].SettingCode
		settingMap[code] = systemSettings[i]
	}
	defaultSettings := new(domain.SystemSetting).GetDefaultSetting("")
	for i := range defaultSettings {
		if v, ok := settingMap[defaultSettings[i].SettingCode]; ok {
			defaultSettings[i].Value = v.Value
			defaultSettings[i].SystemSettingId = v.SystemSettingId
		}
		defaultSettings[i].CompanyId = listSystemSettingQuery.CompanyId
	}
	return map[string]interface{}{
		"grid": map[string]interface{}{
			"list":  defaultSettings,
			"total": len(defaultSettings),
		},
	}, nil
}

// 更新用户的系统设置
func (systemSettingService *SystemSettingService) UpdateSystemSetting(updateSystemSettingCommand *command.UpdateSystemSettingCommand) (interface{}, error) {
	if err := updateSystemSettingCommand.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 systemSettingRepository domain.SystemSettingRepository
	if value, err := factory.CreateSystemSettingRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		systemSettingRepository = value
	}
	_, systemSettings, err := systemSettingRepository.Find(map[string]interface{}{
		"settingCode": updateSystemSettingCommand.SettingCode,
		"companyId":   updateSystemSettingCommand.CompanyId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var systemSetting *domain.SystemSetting
	if len(systemSettings) == 0 {
		//未找到历史数据,使用默认配置
		defaultSet := new(domain.SystemSetting).GetDefaultSetting(updateSystemSettingCommand.SettingCode)
		if len(defaultSet) > 0 {
			systemSetting = &domain.SystemSetting{}
			*systemSetting = defaultSet[0]
			systemSetting.CompanyId = updateSystemSettingCommand.CompanyId
		}
	} else {
		systemSetting = systemSettings[0]
	}
	if systemSetting == nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "settingCode错误,settingCode="+updateSystemSettingCommand.SettingCode)
	}
	if err := systemSetting.Update(map[string]interface{}{
		"value": updateSystemSettingCommand.Value,
	}); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if systemSetting, err := systemSettingRepository.Save(systemSetting); 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 systemSetting, nil
	}
}

func NewSystemSettingService(options map[string]interface{}) *SystemSettingService {
	newSystemSettingService := &SystemSettingService{}
	return newSystemSettingService
}