order_log_subscriber.go 5.5 KB
package subscriber

import (
	"fmt"
	"time"

	coreDomain "github.com/linmadan/egglib-go/core/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/event"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

//订单数据修改触发的订阅事件
type OrderLogSubscriber struct {
	TransactionContext *transaction.TransactionContext
}

var _ coreDomain.DomainEventSubscriber = (*OrderLogSubscriber)(nil)

func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent coreDomain.DomainEvent) error {
	var (
		orderLogRepository domain.OrderLogRepository
		userRepository     domain.UsersRepository
		adminUser          domain.Users
		err                error
	)

	if orderLogRepository, err = repository.NewOrderLogRepository(subscriber.TransactionContext); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if userRepository, err = repository.NewUsersRepository(subscriber.TransactionContext); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}

	switch domainEvent.EventType() {
	//订单分红因为货品的数量变动而发送改变
	case event.UPDATE_BONUS_BY_GOOD_NUMBER_EVENT:
		currentEvent := domainEvent.(event.UpdateBonusByGoodNumber)
		adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
		if err != nil {
			return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		}
		orderLog := domain.OrderLog{
			OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
			OperatorId:   currentEvent.AdminId,
			Operator:     adminUser.Name,
			AlterTime:    time.Now(),
			DataFrom:     domain.ORDER_LOG_FROM,
			LogAction:    "修改",
			OrderId:      currentEvent.OrderId,
			GoodId:       currentEvent.GoodId,
			Descript: []domain.OrderLogDescript{
				domain.OrderLogDescript{
					Title: "调整商品数量",
					Item:  currentEvent.GoodName,
					Action: []string{
						fmt.Sprintf("购买数量由%s调整为%s", currentEvent.FormerNumber, currentEvent.NewNumber),
						fmt.Sprintf("商品总价由¥%s调整为¥%s", currentEvent.FormerAmount, currentEvent.NewAmount),
					},
				},
			},
		}
		err = orderLogRepository.Add(&orderLog)
		break
	//订单分红因为合伙人分红比例变动而发送改变
	case event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT:
		currentEvent := domainEvent.(event.UpdateBounsByPartnerBonusPercent)
		adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
		if err != nil {
			return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		}
		orderLog := domain.OrderLog{
			OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
			OperatorId:   currentEvent.AdminId,
			Operator:     adminUser.Name,
			AlterTime:    time.Now(),
			DataFrom:     domain.ORDER_LOG_FROM,
			LogAction:    "修改",
			OrderId:      currentEvent.OrderId,
			GoodId:       currentEvent.GoodId,
			Descript: []domain.OrderLogDescript{
				domain.OrderLogDescript{
					Title: "合伙人分红比例",
					Item:  currentEvent.GoodName,
					Action: []string{
						fmt.Sprintf("分红比例由%s调整为%s", currentEvent.FormerPartnerBonusPercent, currentEvent.NewPartnerBonusPercent),
						fmt.Sprintf("应收分红由¥%s调整为¥%s", currentEvent.FormerPartnerBonus, currentEvent.NewPartnerBonus),
					},
				},
			},
		}
		err = orderLogRepository.Add(&orderLog)
		break
	//更新订单的备注
	case event.UPDATE_ORDER_REMARK:
		currentEvent := domainEvent.(event.UpdateOrderRemark)
		adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
		if err != nil {
			return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		}
		orderLog := domain.OrderLog{
			OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
			OperatorId:   currentEvent.AdminId,
			Operator:     adminUser.Name,
			AlterTime:    time.Now(),
			DataFrom:     domain.ORDER_LOG_FROM,
			LogAction:    "修改",
			OrderId:      currentEvent.OrderId,
			GoodId:       0,
			Descript: []domain.OrderLogDescript{
				domain.OrderLogDescript{
					Title:  "编辑备注",
					Item:   currentEvent.NewRemark,
					Action: []string{},
				},
			},
		}
		err = orderLogRepository.Add(&orderLog)
		break
	// 支付订单中货品的分红
	case event.PAY_ORDER_GOOD_BONUS_EVENT:
		currentEvent := domainEvent.(event.PayOrderGoodBonus)
		adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
		if err != nil {
			return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		}
		orderLog := domain.OrderLog{
			OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
			OperatorId:   currentEvent.AdminId,
			Operator:     adminUser.Name,
			AlterTime:    time.Now(),
			DataFrom:     domain.ORDER_LOG_FROM,
			LogAction:    "支付",
			OrderId:      currentEvent.OrderId,
			GoodId:       currentEvent.GoodId,
			Descript: []domain.OrderLogDescript{
				domain.OrderLogDescript{
					Title: "支付分红",
					Item:  currentEvent.GoodName,
					Action: []string{
						fmt.Sprintf("支付分红¥%.2f", currentEvent.PartnerBonus),
					},
				},
			},
		}
		err = orderLogRepository.Add(&orderLog)
		break
	}
	return err
}

func (subscriber *OrderLogSubscriber) SubscribedToEventTypes() []string {
	return []string{
		event.UPDATE_BONUS_BY_GOOD_NUMBER_EVENT,
		event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT,
		event.UPDATE_ORDER_REMARK,
		event.PAY_ORDER_GOOD_BONUS_EVENT,
	}
}