order_log_subscriber.go 6.4 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{
					GoodId: currentEvent.OrderId,
					Title:  "编辑备注",
					Item:   currentEvent.NewRemark,
					Action: []string{},
				},
			},
		}
		err = orderLogRepository.Add(&orderLog)
		break
	case event.PAY_ORDER_GOOD_BONUS_EVENT:
		currentEvent := domainEvent.(event.PayOrderGoodBonus)
		orderLog := domain.OrderLog{
			OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
			OperatorId:   currentEvent.Admin.Id,
			Operator:     adminUser.Name,
			AlterTime:    time.Now(),
			DataFrom:     domain.ORDER_LOG_FROM,
			LogAction:    "支付",
			OrderId:      currentEvent.OrderBase.Id,
			Descript:     []domain.OrderLogDescript{},
		}
		for i := range currentEvent.Goods {
			m := domain.OrderLogDescript{
				GoodId: currentEvent.Goods[i].Id,
				Title:  "支付分红",
				Item:   currentEvent.Goods[i].GoodName,
				Action: []string{
					fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.Goods[i].GetCurrentPartnerBonus()),
				},
			}
			orderLog.Descript = append(orderLog.Descript, m)
		}
		err = orderLogRepository.Add(&orderLog)
		break
	case event.BATCH_PAY_ORDER_GOOD_BONUS_EVENT:
		currentEvent := domainEvent.(event.BatchPayOrderGoodBonus)
		for i := range currentEvent {
			orderLog := domain.OrderLog{
				OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
				OperatorId:   currentEvent[i].Admin.Id,
				Operator:     adminUser.Name,
				AlterTime:    time.Now(),
				DataFrom:     domain.ORDER_LOG_FROM,
				LogAction:    "支付",
				OrderId:      currentEvent[i].OrderBase.Id,
				Descript:     []domain.OrderLogDescript{},
			}
			for ii := range currentEvent[i].Goods {
				m := domain.OrderLogDescript{
					GoodId: currentEvent[i].Goods[ii].Id,
					Title:  "支付分红",
					Item:   currentEvent[i].Goods[ii].GoodName,
					Action: []string{
						fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent[i].Goods[ii].GetCurrentPartnerBonus()),
					},
				}
				orderLog.Descript = append(orderLog.Descript, m)
			}
			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.BATCH_PAY_ORDER_GOOD_BONUS_EVENT,
		event.PAY_ORDER_GOOD_BONUS_EVENT,
	}
}