company.go 13.6 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/company/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/common"
	"strconv"
)

// 企业
type CompanyService struct {
}

// 创建企业
func (companyService *CompanyService) CreateCompany(createCompanyCommand *command.CreateCompanyCommand) (interface{}, error) {
	if err := createCompanyCommand.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()
	}()
	newCompany := &domain.Company{
		//CompanyInfo: createCompanyCommand.CompanyInfo,
	}
	var companyRepository domain.CompanyRepository
	if value, err := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyRepository = value
	}
	if company, err := companyRepository.Save(newCompany); 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 company, nil
	}
}

// 返回企业
func (companyService *CompanyService) GetCompany(getCompanyQuery *query.GetCompanyQuery) (interface{}, error) {
	if err := getCompanyQuery.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 companyRepository domain.CompanyRepository
	if value, err := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyRepository = value
	}
	company, err := companyRepository.FindOne(map[string]interface{}{"companyId": getCompanyQuery.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if company == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getCompanyQuery.CompanyId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return company, nil
	}
}

// 返回企业列表
func (companyService *CompanyService) ListCompany(listCompanyQuery *query.ListCompanyQuery) (interface{}, error) {
	if err := listCompanyQuery.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 companyRepository domain.CompanyRepository
	if value, err := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyRepository = value
	}
	if count, companys, err := companyRepository.Find(tool_funs.SimpleStructToMap(listCompanyQuery)); 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,
			"companys": companys,
		}, nil
	}
}

// 返回自定义菜单列表(匹配有设置的菜单)
func (companyService *CompanyService) ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand *command.ListCompanyCustomizeMenusCommand) (interface{}, error) {
	if err := listCompanyCustomizeMenusCommand.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 menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	queryOptions := common.SimpleStructToMap(listCompanyCustomizeMenusCommand)
	if m, e := menuRepository.FindOne(map[string]interface{}{"code": listCompanyCustomizeMenusCommand.MenuCategory}); e == nil && m != nil {
		queryOptions["category"] = strconv.Itoa(int(m.MenuId))
	}
	_, menus, err := menuRepository.Find(queryOptions)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var customizeMenuRepository domain.CustomizeMenuRepository
	if value, err := factory.CreateCustomizeMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customizeMenuRepository = value
	}
	_, customizeMenus, err := customizeMenuRepository.Find(queryOptions)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	customizeMenuDto := dto.CustomizeMenuDto{}
	ret, _ := customizeMenuDto.LoadDto(menus, customizeMenus)

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{"menus": ret}, nil
}

// 移除企业
func (companyService *CompanyService) RemoveCompany(removeCompanyCommand *command.RemoveCompanyCommand) (interface{}, error) {
	if err := removeCompanyCommand.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 companyRepository domain.CompanyRepository
	if value, err := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyRepository = value
	}
	company, err := companyRepository.FindOne(map[string]interface{}{"companyId": removeCompanyCommand.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if company == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeCompanyCommand.CompanyId)))
	}
	if company, err := companyRepository.Remove(company); 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 company, nil
	}
}

// 更新企业
func (companyService *CompanyService) UpdateCompany(updateCompanyCommand *command.UpdateCompanyCommand) (interface{}, error) {
	if err := updateCompanyCommand.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 companyRepository domain.CompanyRepository
	if value, err := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyRepository = value
	}
	company, err := companyRepository.FindOne(map[string]interface{}{"companyId": updateCompanyCommand.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if company == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateCompanyCommand.CompanyId)))
	}
	if err := company.Update(tool_funs.SimpleStructToMap(updateCompanyCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if company, err := companyRepository.Save(company); 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 company, nil
	}
}

// 更新自定义菜单
func (companyService *CompanyService) UpdateCompanyCustomizeMenus(updateCompanyCustomizeMenusCommand *command.UpdateCompanyCustomizeMenusCommand) (interface{}, error) {
	if err := updateCompanyCustomizeMenusCommand.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 menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	var menuName string
	if menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": updateCompanyCustomizeMenusCommand.MenuId}); err != nil || (menu != nil && menu.IsPublish != domain.MenuPublic) {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "菜单不存在")
	} else {
		menuName = menu.MenuName
	}

	var customizeMenuRepository domain.CustomizeMenuRepository
	if value, err := factory.CreateCustomizeMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		customizeMenuRepository = value
	}
	customizeMenu, e := customizeMenuRepository.FindOne(map[string]interface{}{
		"companyId": updateCompanyCustomizeMenusCommand.CompanyId,
		"menuId":    updateCompanyCustomizeMenusCommand.MenuId,
		"isPublish": domain.MenuPublic,
	})
	if e == domain.ErrorNotFound {
		customizeMenu = &domain.CustomizeMenu{
			CompanyId: updateCompanyCustomizeMenusCommand.CompanyId,
			MenuId:    updateCompanyCustomizeMenusCommand.MenuId,
			MenuName:  menuName,
			MenuAlias: updateCompanyCustomizeMenusCommand.MenuAlias,
			Sort:      updateCompanyCustomizeMenusCommand.Sort,
		}
	} else if customizeMenu != nil {
		data := tool_funs.SimpleStructToMap(updateCompanyCustomizeMenusCommand)
		data["menuName"] = menuName
		if err := customizeMenu.Update(data); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	} else {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, e.Error())
	}
	if customizeMenu, err = customizeMenuRepository.Save(customizeMenu); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, e.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return customizeMenu, nil
}

func NewCompanyService(options map[string]interface{}) *CompanyService {
	newCompanyService := &CompanyService{}
	return newCompanyService
}