|
|
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/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
|
|
|
)
|
|
|
|
|
|
// 企业
|
|
|
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()
|
|
|
}()
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, 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()
|
|
|
}()
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func NewCompanyService(options map[string]interface{}) *CompanyService {
|
|
|
newCompanyService := &CompanyService{}
|
|
|
return newCompanyService
|
|
|
} |
...
|
...
|
|