company.go 6.1 KB
package service

import (
	"github.com/emirpasic/gods/sets/hashset"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/company/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/company/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/background/company/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
)

// 企业
type CompanyService struct {
}

// 创建企业
// func (companyService *CompanyService) CreateCompany(createCompanyCommand *command.CreateCompanyCommand) (interface{}, error) {
// 	return nil, nil
// }

// 返回企业
func (companyService *CompanyService) GetCompany(getCompanyQuery *query.GetCompanyQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	result, err := creationUserGateway.CompanyGet(allied_creation_user.ReqCompanyGet{
		CompanyId: getCompanyQuery.CompanyId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	userResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
		InCompanyIds: []interface{}{result.CompanyId},
		UserType:     domain.UserTypeCompanyAdmin,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var user allied_creation_user.UserDetail
	if len(userResult.Users) > 0 {
		user = userResult.Users[0]
	}
	companyDto := &dto.CompanyGetItemDto{}
	companyDto.LoadDto(allied_creation_user.CompanyItem(*result), user)
	return companyDto, nil
}

func (companyService *CompanyService) AuditCompany(cmd *command.AuditCompanyCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	_, err := creationUserGateway.AuditCompany(allied_creation_user.ReqCompanyAudit{
		CompanyId: int(cmd.CompanyId),
		Status:    cmd.Status,
		Remark:    cmd.Remark,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	smsServe := sms_serve.NewHttplibHttplibSmsServe()
	if cmd.Status == domain.CompanyAuthenticated {
		var tmpPhone []rune = []rune(cmd.Phone)
		for i := 0; i < 4; i++ {
			tmpPhone[i+3] = rune('*')
		}
		err = smsServe.SendNoticeSms(cmd.Phone, 5007040, map[string]interface{}{"phone": string(tmpPhone)})
	} else if cmd.Status == domain.CompanyUnauthenticated {
		err = smsServe.SendNoticeSms(cmd.Phone, 5006964, map[string]interface{}{"reason": cmd.Remark})
	}
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return struct{}{}, nil
}

func (companyService *CompanyService) EnableCompany(cmd *command.EnableCompanyCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	_, err := creationUserGateway.EnableCompany(allied_creation_user.ReqCompanyEnable{
		CompanyId: int(cmd.CompanyId),
		Status:    cmd.Status,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 返回企业列表
func (companyService *CompanyService) ListCompany(listCompanyQuery *query.ListCompanyQuery) (int64, interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	result, err := creationUserGateway.CompanySearch(allied_creation_user.ReqCompanySearch{
		Offset:      (listCompanyQuery.PageNumber - 1) * listCompanyQuery.PageSize,
		Limit:       listCompanyQuery.PageSize,
		Status:      listCompanyQuery.Status,
		CompanyName: listCompanyQuery.CompanyName,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	companySet := hashset.New()
	for i := range result.Companys {
		companySet.Add(result.Companys[i].CompanyId)
	}
	var users = make([]allied_creation_user.UserDetail, 0)
	if companySet.Size() > 0 {
		userResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
			InCompanyIds: companySet.Values(),
			UserType:     domain.UserTypeCompanyAdmin,
		})
		if err != nil {
			return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}
		users = userResult.Users
	}
	companyListItemsDto := dto.CompanyListItemsDto{}
	companyListItemsDto.LoadDto(result.Companys, users)
	return int64(result.Count), companyListItemsDto, nil
}

// 返回自定义菜单列表(匹配有设置的菜单)
// func (companyService *CompanyService) ListCompanyCustomizeMenus(listCompanyCustomizeMenusCommand *command.ListCompanyCustomizeMenusCommand) (interface{}, error) {
// 	return nil, nil
// }

// 移除企业
// func (companyService *CompanyService) RemoveCompany(removeCompanyCommand *command.RemoveCompanyCommand) (interface{}, error) {
// 	return nil, nil
// }

// 更新企业
func (companyService *CompanyService) UpdateCompany(updateCompanyCommand *command.UpdateCompanyCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(updateCompanyCommand.Operator)
	_, err := creationUserGateway.CompanyUpdate(allied_creation_user.ReqCompanyUpdate{
		CompanyId:   updateCompanyCommand.Operator.CompanyId,
		CompanyName: updateCompanyCommand.CompanyName,
		Address:     updateCompanyCommand.Address,
		SystemName:  updateCompanyCommand.SystemName,
		Logo:        updateCompanyCommand.Logo,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return updateCompanyCommand, nil
}

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