|
|
package service
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/command"
|
...
|
...
|
@@ -19,12 +21,12 @@ func NewPartnerInfoService(options map[string]interface{}) *PartnerInfoService { |
|
|
return newPartnerInfoService
|
|
|
}
|
|
|
|
|
|
// 创建客户价值
|
|
|
func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command.CreatePartnerInfoCommand) (data interface{}, err error) {
|
|
|
// CreatePartnerInfo 创建合伙人
|
|
|
func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.CreatePartnerInfoCommand) (data interface{}, err error) {
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
)
|
|
|
if err = command.ValidateCommand(); err != nil {
|
|
|
if err = cmd.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
...
|
...
|
@@ -37,14 +39,12 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command |
|
|
var (
|
|
|
partnerinfoDao *dao.PartnerInfoDao
|
|
|
)
|
|
|
if v, err := factory.CreatePartnerInfoDao(map[string]interface{}{
|
|
|
if partnerinfoDao, err = factory.CreatePartnerInfoDao(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
} else {
|
|
|
partnerinfoDao = v
|
|
|
}
|
|
|
ok, err := partnerinfoDao.PartnerAccountExist(command.Account)
|
|
|
ok, err := partnerinfoDao.PartnerAccountExist(cmd.Account)
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
...
|
...
|
@@ -52,37 +52,55 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command |
|
|
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "账号已存在")
|
|
|
}
|
|
|
|
|
|
var PartnerInfoRepository domain.PartnerInfoRepository
|
|
|
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
var (
|
|
|
partnerInfoRepository domain.PartnerInfoRepository
|
|
|
categoryRepository domain.PartnerCategoryRepository
|
|
|
categorys []domain.PartnerCategory
|
|
|
)
|
|
|
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, categorys, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
|
|
|
Ids: cmd.PartnerCategory,
|
|
|
})
|
|
|
if err != nil {
|
|
|
e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
}
|
|
|
newPartnerInfo := domain.PartnerInfo{
|
|
|
Partner: domain.Partner{
|
|
|
Account: command.Account,
|
|
|
PartnerName: command.PartnerName,
|
|
|
Account: cmd.Account,
|
|
|
PartnerName: cmd.PartnerName,
|
|
|
},
|
|
|
PartnerCategory: command.PartnerCategory,
|
|
|
Password: command.Password,
|
|
|
Status: command.Status,
|
|
|
RegionInfo: command.RegionInfo,
|
|
|
Salesman: command.Salesman,
|
|
|
CooperateTime: command.CooperateTime,
|
|
|
}
|
|
|
if data, err = PartnerInfoRepository.Save(newPartnerInfo); err != nil {
|
|
|
PartnerCategory: 0,
|
|
|
Password: cmd.Password,
|
|
|
Status: cmd.Status,
|
|
|
RegionInfo: cmd.RegionInfo,
|
|
|
Salesman: cmd.Salesman,
|
|
|
CooperateTime: cmd.CooperateTime,
|
|
|
CompanyId: cmd.CompanyId,
|
|
|
PartnerCategoryInfos: categorys,
|
|
|
}
|
|
|
if err = partnerInfoRepository.Save(&newPartnerInfo); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
return
|
|
|
return newPartnerInfo, nil
|
|
|
}
|
|
|
|
|
|
// GetPartnerInfo 返回合伙人
|
|
|
func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command query.GetPartnerInfoQuery) (data *domain.PartnerInfo, err error) {
|
|
|
func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(q query.GetPartnerInfoQuery) (data *domain.PartnerInfo, err error) {
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
)
|
|
|
if err = command.ValidateQuery(); err != nil {
|
|
|
if err = q.ValidateQuery(); err != nil {
|
|
|
return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
...
|
...
|
@@ -91,26 +109,52 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command query.GetPa |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var PartnerInfoRepository domain.PartnerInfoRepository
|
|
|
var (
|
|
|
PartnerInfoRepository domain.PartnerInfoRepository
|
|
|
categoryRepository domain.PartnerCategoryRepository
|
|
|
categorys []domain.PartnerCategory
|
|
|
partnerData *domain.PartnerInfo
|
|
|
)
|
|
|
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
data, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.Id})
|
|
|
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: q.Id})
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if !partnerData.IsCompany(q.CompanyId) {
|
|
|
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "企业信息异常操作")
|
|
|
}
|
|
|
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
categoryIds := []int64{}
|
|
|
for _, v := range partnerData.PartnerCategoryInfos {
|
|
|
categoryIds = append(categoryIds, v.Id)
|
|
|
}
|
|
|
if len(categoryIds) > 0 {
|
|
|
_, categorys, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
|
|
|
Ids: categoryIds,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
partnerData.PartnerCategoryInfos = categorys
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
return
|
|
|
return partnerData, nil
|
|
|
}
|
|
|
|
|
|
//UpdatePartnerInfo 更新合伙人
|
|
|
func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInfoCommand *command.UpdatePartnerInfoCommand) (err error) {
|
|
|
func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.UpdatePartnerInfoCommand) (err error) {
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
)
|
|
|
if err = updatePartnerInfoCommand.ValidateCommand(); err != nil {
|
|
|
if err = cmd.ValidateCommand(); err != nil {
|
|
|
return application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
...
|
...
|
@@ -119,24 +163,43 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInf |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var partnerInfoRepository domain.PartnerInfoRepository
|
|
|
var (
|
|
|
partnerInfoRepository domain.PartnerInfoRepository
|
|
|
categoryRepository domain.PartnerCategoryRepository
|
|
|
categorys []domain.PartnerCategory
|
|
|
)
|
|
|
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, categorys, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
|
|
|
Ids: cmd.PartnerCategory,
|
|
|
})
|
|
|
if err != nil {
|
|
|
e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
}
|
|
|
partnerInfo, err := partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
|
|
|
UserId: updatePartnerInfoCommand.Id,
|
|
|
UserId: cmd.Id,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
partnerInfo.PartnerCategory = updatePartnerInfoCommand.PartnerCategory
|
|
|
partnerInfo.Salesman = updatePartnerInfoCommand.Salesman
|
|
|
partnerInfo.Status = updatePartnerInfoCommand.Status
|
|
|
partnerInfo.RegionInfo = updatePartnerInfoCommand.RegionInfo
|
|
|
partnerInfo.CooperateTime = updatePartnerInfoCommand.CooperateTime
|
|
|
if _, err = partnerInfoRepository.Save(*partnerInfo); err != nil {
|
|
|
if !partnerInfo.IsCompany(cmd.CompanyId) {
|
|
|
return lib.ThrowError(lib.BUSINESS_ERROR, "异常操作")
|
|
|
}
|
|
|
partnerInfo.Salesman = cmd.Salesman
|
|
|
partnerInfo.Status = cmd.Status
|
|
|
partnerInfo.RegionInfo = cmd.RegionInfo
|
|
|
partnerInfo.CooperateTime = cmd.CooperateTime
|
|
|
partnerInfo.PartnerCategoryInfos = categorys
|
|
|
if err = partnerInfoRepository.Save(partnerInfo); err != nil {
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext.CommitTransaction()
|
...
|
...
|
@@ -162,16 +225,24 @@ func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQue |
|
|
transactionContext.RollbackTransaction()
|
|
|
}
|
|
|
}()
|
|
|
var partnerInfoRepository domain.PartnerInfoRepository
|
|
|
var (
|
|
|
partnerInfoRepository domain.PartnerInfoRepository
|
|
|
categoryRepository domain.PartnerCategoryRepository
|
|
|
categorys []domain.PartnerCategory
|
|
|
)
|
|
|
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
queryOption := domain.PartnerFindQuery{
|
|
|
Offset: listPartnerInfoQuery.Offset,
|
|
|
Limit: listPartnerInfoQuery.Limit,
|
|
|
|
|
|
PartnerName: listPartnerInfoQuery.PartnerName,
|
|
|
}
|
|
|
if listPartnerInfoQuery.Partnertype > 0 {
|
...
|
...
|
@@ -187,17 +258,37 @@ func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQue |
|
|
if count, err = partnerInfoRepository.CountAll(queryOption); err != nil {
|
|
|
return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, categorys, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{})
|
|
|
if err != nil {
|
|
|
return count, partnerInfos, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
categorysMap := make(map[int64]domain.PartnerCategory)
|
|
|
for i := range categorys {
|
|
|
categorysMap[categorys[i].Id] = categorys[i]
|
|
|
}
|
|
|
for i := range partnerInfos {
|
|
|
categoryInPartner := []domain.PartnerCategory{}
|
|
|
for _, vv := range partnerInfos[i].PartnerCategoryInfos {
|
|
|
if categoryData, ok := categorysMap[vv.Id]; ok {
|
|
|
categoryInPartner = append(categoryInPartner, categoryData)
|
|
|
}
|
|
|
}
|
|
|
partnerInfos[i].PartnerCategoryInfos = categoryInPartner
|
|
|
}
|
|
|
if err = transactionContext.CommitTransaction(); err != nil {
|
|
|
return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return count, partnerInfos, nil
|
|
|
}
|
|
|
|
|
|
func (PartnerInfoService *PartnerInfoService) UpdateStatus(command command.StatusPartnerInfoCommand) (err error) {
|
|
|
func (PartnerInfoService *PartnerInfoService) UpdateStatus(cmd command.StatusPartnerInfoCommand) (err error) {
|
|
|
if len(cmd.Ids) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
)
|
|
|
if err = command.ValidateCommand(); err != nil {
|
|
|
if err = cmd.ValidateCommand(); err != nil {
|
|
|
return application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
...
|
...
|
@@ -206,21 +297,18 @@ func (PartnerInfoService *PartnerInfoService) UpdateStatus(command command.Statu |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var partnerInfoRepository domain.PartnerInfoRepository
|
|
|
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
var (
|
|
|
partnerinfoDao *dao.PartnerInfoDao
|
|
|
)
|
|
|
if partnerinfoDao, err = factory.CreatePartnerInfoDao(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
partnerInfo, err := partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
|
|
|
UserId: command.Id,
|
|
|
})
|
|
|
err = partnerinfoDao.UpdatePartnerStatus(cmd.Ids, cmd.CompanyId, cmd.Status)
|
|
|
if err != nil {
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
partnerInfo.Status = command.Status
|
|
|
if _, err = partnerInfoRepository.Save(*partnerInfo); err != nil {
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
e := fmt.Sprintf("更新合伙人(id=%v)的数据失败;%s", cmd.Ids, err)
|
|
|
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
}
|
|
|
transactionContext.CommitTransaction()
|
|
|
return
|
...
|
...
|
|