company.go 14.3 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	coreDomain "github.com/linmadan/egglib-go/core/domain"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/command"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/company/query"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/event/subscriber"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/event"
	"time"
)

// 公司服务
type CompanyService struct {
	coreDomain.BaseEventPublisher
}

// 公司状态转换  禁用、启用
func (companyService *CompanyService) ConvertCompanyStatus(convertCompanyStatusCommand *command.ConvertCompanyStatusCommand) (interface{}, error) {
	if err := convertCompanyStatusCommand.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": convertCompanyStatusCommand.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(convertCompanyStatusCommand.CompanyId)))
	}
	if err := company.Update(tool_funs.SimpleStructToMap(convertCompanyStatusCommand)); 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) 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: &domain.CompanyInfo{
			Name:         createCompanyCommand.Name,         // 公司名称
			Abbreviation: createCompanyCommand.Abbreviation, // 公司简称
			Logo:         "",                                // 公司logo
			Phone:        "",                                // 公司联系电话
			Status:       createCompanyCommand.Status,       // 状态 1正常 2禁用
			Remarks:      createCompanyCommand.Remarks,      // 备注
			Enable:       1,                                 // 是否有效【1:有效】【2:无效】
			Admin: &domain.CompanyAdmin{
				UserAccount: createCompanyCommand.UserAccount, // 用户手机号
				UserName:    createCompanyCommand.UserName,    // 用户名称
			}, // 公司主管理员
		},
		CreateAt: time.Now(),
		UpdateAt: time.Now(),
	}
	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 {
		// todo 新增公司管理员
		companyService.Subscribe(&subscriber.UsersServiceSubscriber{TransactionContext: transactionContext.(*pgTransaction.TransactionContext)})
		companyService.Publish(&event.CompanyCreated{
			Company: company,
		})
		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, companies, 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,
			"companies": companies,
		}, 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) SetCompanyAdmin(setCompanyAdminCommand *command.SetCompanyAdminCommand) (interface{}, error) {
	if err := setCompanyAdminCommand.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": setCompanyAdminCommand.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(setCompanyAdminCommand.CompanyId)))
	}

	newCompanyAdmin := &domain.CompanyAdmin{
		UserAccount: setCompanyAdminCommand.UserAccount, // 用户手机号
		UserName:    setCompanyAdminCommand.UserName,    // 用户名称
	}
	// 更改公司管理员信息
	if err := company.SetCompanyAdmin(newCompanyAdmin); 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 {
		// todo 更改公司管理员
		companyService.Subscribe(&subscriber.UsersServiceSubscriber{TransactionContext: transactionContext.(*pgTransaction.TransactionContext)})
		companyService.Publish(&event.CompanyAdminAssigned{
			CompanyId: company.CompanyId,
			Admin:     company.CompanyInfo.Admin,
		})
		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 updateCompanyCommand.UserAccount != "" || updateCompanyCommand.UserName != "" {
		companyAdmin := company.CompanyInfo.Admin
		if updateCompanyCommand.UserAccount != "" {
			companyAdmin.UserAccount = updateCompanyCommand.UserAccount // 用户手机号
		}
		if updateCompanyCommand.UserName != "" {
			companyAdmin.UserName = updateCompanyCommand.UserName // 用户名称
		}
		if err := company.SetCompanyAdmin(companyAdmin); err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}

		// todo 更改公司管理员
	}

	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 NewCompanyService(options map[string]interface{}) *CompanyService {
	newCompanyService := &CompanyService{}
	return newCompanyService
}