作者 唐旭辉

更新

package factory
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/domainService"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
func CreateBusinessBonusService(options map[string]interface{}) (service.BusinessBonusService, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return domainService.NewBusinessBonusService(transactionContext), nil
}
... ...
... ... @@ -8,6 +8,7 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
... ... @@ -91,6 +92,17 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
if err = partnerInfoRepository.Save(&newPartnerInfo); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var businessBonusSrv service.BusinessBonusService
if businessBonusSrv, err = factory.CreateBusinessBonusService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = businessBonusSrv.EnableOrDisable(newPartnerInfo.Partner.Id)
if err != nil {
e := fmt.Sprintf("更新业务分红(partner_id=%d)数据失败:%s", newPartnerInfo.Partner.Id, err)
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
err = transactionContext.CommitTransaction()
return newPartnerInfo, nil
}
... ... @@ -202,6 +214,17 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.Upd
if err = partnerInfoRepository.Save(partnerInfo); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var businessBonusSrv service.BusinessBonusService
if businessBonusSrv, err = factory.CreateBusinessBonusService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = businessBonusSrv.EnableOrDisable(partnerInfo.Partner.Id)
if err != nil {
e := fmt.Sprintf("更新业务分红(partner_id=%d)数据失败:%s", partnerInfo.Partner.Id, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
transactionContext.CommitTransaction()
return
}
... ...
... ... @@ -6,6 +6,7 @@ import (
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
... ... @@ -93,3 +94,11 @@ func (dao BusinessBonusDao) CountBusinessBonus(partnerId int64, partnerNameMatch
_, err = tx.Query(&result, sql, allParam...)
return result, err
}
func (dao BusinessBonusDao) ExistBusinessBonus(userId int64) (bool, error) {
tx := dao.transactionContext.PgTx
ok, err := tx.Model(&models.BusinessBonus{}).
Where("user_id=?", userId).
Exists()
return ok, err
}
... ...
package domainService
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/repository"
)
type BusinessBonusService struct {
transactionContext *transaction.TransactionContext
}
func NewBusinessBonusService(tcx *transaction.TransactionContext) *BusinessBonusService {
return &BusinessBonusService{
transactionContext: tcx,
}
}
func (srv BusinessBonusService) EnableOrDisable(parntnerId int64) error {
var (
bonusDao, _ = dao.NewBusinessBonusDao(srv.transactionContext)
bonusReponsitory, _ = repository.NewBusinessBonusRepository(srv.transactionContext)
partnerReponsitory, _ = repository.NewPartnerInfoRepository(srv.transactionContext)
err error
partnerHasInBonus bool
partnerData *domain.PartnerInfo
partnerHasBusinessCategory bool
)
partnerHasInBonus, err = bonusDao.ExistBusinessBonus(parntnerId)
if err != nil {
return err
}
partnerData, err = partnerReponsitory.FindOne(domain.PartnerFindOneQuery{UserId: parntnerId})
if err != nil {
return err
}
for _, v := range partnerData.PartnerCategoryInfos {
if v.Id == 2 {
partnerHasBusinessCategory = true
break
}
}
if partnerHasInBonus && partnerHasBusinessCategory {
//已存在业务分红数据况 && 是业务合伙人类型
var bonusData *domain.BusinessBonus
bonusData, err = bonusReponsitory.FindOne(domain.BusinessBonusFindOneQuery{
PartnerId: parntnerId,
})
if bonusData.IsDisable == domain.BUSINESS_BONUS_ENABLE {
return nil
}
bonusData.IsDisable = domain.BUSINESS_BONUS_ENABLE
err = bonusReponsitory.Edit(bonusData)
return err
}
if partnerHasInBonus && !partnerHasBusinessCategory {
//已存在业务分红数据况 && 不是业务合伙人类型
var bonusData *domain.BusinessBonus
bonusData, err = bonusReponsitory.FindOne(domain.BusinessBonusFindOneQuery{
PartnerId: parntnerId,
})
if bonusData.IsDisable == domain.BUSINESS_BONUS_DISABLE {
return nil
}
bonusData.IsDisable = domain.BUSINESS_BONUS_DISABLE
err = bonusReponsitory.Edit(bonusData)
return err
}
if !partnerHasInBonus && partnerHasBusinessCategory {
//不存在业务分红数据况 && 是业务合伙人类型
newBonus := &domain.BusinessBonus{
PartnerInfoId: partnerData.Partner.Id,
IsDisable: domain.BUSINESS_BONUS_ENABLE,
CompanyId: partnerData.CompanyId,
Bonus: 0,
}
err = bonusReponsitory.Add(newBonus)
return err
}
return nil
}
... ...
... ... @@ -11,23 +11,23 @@ import (
type BusinessBonus struct {
tableName struct{} `pg:"business_bonus"`
// 唯一标识
Id int64
Id int64 `pg:",pk"`
// 公司编号
CompanyId int64
// 合伙人信息Id
PartnerInfoId int64
// 应收分红
Bonus float64
Bonus float64 `pg:",use_zero"`
// 未收分红
BonusNot float64
// 分红支出
BonusExpense float64
//已收分红
BonusHas float64
// 是否关闭【0;否】【1:是】
IsDisable int8
//是否有效【0;无效】【1:有效】
IsDisable int8 `pg:",use_zero"`
// 分红状态 1:待支付分红 2:已支付分红
BonusStatus int8
BonusStatus int8 `pg:",use_zero"`
// 创建时间
CreateAt time.Time
// 更新时间
... ... @@ -47,6 +47,5 @@ var _ pg.BeforeInsertHook = (*BusinessBonus)(nil)
func (bonus *BusinessBonus) BeforeInsert(ctx context.Context) (context.Context, error) {
bonus.CreateAt = time.Now()
bonus.UpdateAt = time.Now()
return ctx, nil
}
... ...
... ... @@ -56,19 +56,7 @@ func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) error {
err error
tx = repository.transactionContext.PgTx
)
m := &models.PartnerInfo{
Id: dm.Partner.Id,
PartnerName: dm.Partner.PartnerName,
Account: dm.Partner.Account,
Password: dm.Password,
Status: dm.Status,
PartnerCategory: dm.PartnerCategory,
Salesman: dm.Salesman,
RegionInfo: dm.RegionInfo,
CooperateTime: dm.CooperateTime,
CompanyId: dm.CompanyId,
// PartnerCategoryInfos: dm.PartnerCategoryInfos,
}
categorys := []struct{ Id int64 }{}
for _, v := range dm.PartnerCategoryInfos {
temp := struct{ Id int64 }{
... ... @@ -76,7 +64,19 @@ func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) error {
}
categorys = append(categorys, temp)
}
m.PartnerCategoryInfos = categorys
m := &models.PartnerInfo{
Id: dm.Partner.Id,
PartnerName: dm.Partner.PartnerName,
Account: dm.Partner.Account,
Password: dm.Password,
Status: dm.Status,
PartnerCategory: dm.PartnerCategory,
Salesman: dm.Salesman,
RegionInfo: dm.RegionInfo,
CooperateTime: dm.CooperateTime,
CompanyId: dm.CompanyId,
PartnerCategoryInfos: categorys,
}
if m.Id == 0 {
err = tx.Insert(m)
dm.Partner.Id = m.Id
... ...