service.go 4.0 KB
package service

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

type CompanyService struct{}

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

//AllowCompany 公司启用合伙人功能
func (service CompanyService) AllowCompany(companyId int64) error {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		companyRespository domain.CompanyRepository
		oldCompanyData     domain.Company
	)
	if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
		"Id": companyId,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
	}
	err = oldCompanyData.Update(map[string]interface{}{
		"Enable": domain.CompanyEnableYes,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	//更新公司数据
	err = companyRespository.Edit(&oldCompanyData)
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新公司(id=%d)的数据失败:%s", companyId, err.Error()))
	}
	err = transactionContext.CommitTransaction()
	return nil
}

//ForbidCompany 公司禁用合伙人功能
func (service CompanyService) ForbidCompany(companyId int64) error {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		companyRespository domain.CompanyRepository
		oldCompanyData     domain.Company
	)
	if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
		"Id": companyId,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
	}
	err = oldCompanyData.Update(map[string]interface{}{
		"Enable": domain.CompanyEnableNo,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	//更新公司数据
	err = companyRespository.Edit(&oldCompanyData)
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新公司(id=%d)的数据失败:%s", companyId, err.Error()))
	}
	err = transactionContext.CommitTransaction()
	return nil
}

//GetCompanyData 获取公司数据
func (service CompanyService) GetCompanyData(companyId int64) (*domain.Company, error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		err                   error
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var (
		companyRespository domain.CompanyRepository
		companyData        domain.Company
	)
	if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	companyData, err = companyRespository.FindOne(map[string]interface{}{
		"Id": companyId,
	})
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
	}
	err = transactionContext.CommitTransaction()
	return &companyData, nil
}