pg_order_good_reponsitory.go 4.0 KB
package repository

import (
	"fmt"

	"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"
)

type OrderGoodRepository struct {
	transactionContext *transaction.TransactionContext
}

var (
	_ domain.OrderGoodRepository = (*OrderGoodRepository)(nil)
)

func NewOrderGoodRepository(transactionContext *transaction.TransactionContext) (*OrderGoodRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &OrderGoodRepository{transactionContext: transactionContext}, nil
}

func (reponsitory OrderGoodRepository) transformPgModelToDomainModel(orderModel *models.OrderGood) (order domain.OrderGood, err error) {
	order = domain.OrderGood{
		Id: orderModel.Id, OrderId: orderModel.OrderId, GoodName: orderModel.GoodName,
		PlanGoodNumber: orderModel.PlanGoodNumber, UseGoodNumber: orderModel.UseGoodNumber,
		Price: orderModel.Price, PartnerBonusPercent: orderModel.PartnerBonusPercent,
		BonusStatus: orderModel.BonusStatus, Remark: orderModel.Remark,
		GoodCompute: domain.GoodCompute{
			PlanAmount:          orderModel.PlanAmount,
			UseAmount:           orderModel.UseAmount,
			PlanPartnerBonus:    orderModel.PlanPartnerBonus,
			UsePartnerBonus:     orderModel.UsePartnerBonus,
			PartnerBonusHas:     orderModel.PartnerBonusHas,
			PartnerBonusNot:     orderModel.PartnerBonusNot,
			PartnerBonusExpense: orderModel.PartnerBonusExpense,
		},
	}
	switch orderModel.BonusStatus {
	case domain.OrderGoodWaitPay:
		order.CurrentBonusStatus = domain.OrderGoodBonusWaitPay{}
	case domain.OrderGoodHasPay:
		order.CurrentBonusStatus = domain.OrderGoodBonusHasPay{}
	}
	return order, nil
}

func (repository OrderGoodRepository) Save(data []domain.OrderGood) error {
	var (
		err error
		tx  = repository.transactionContext.PgTx
	)
	for k, v := range data {
		m := &models.OrderGood{
			Id: v.Id, OrderId: v.OrderId, GoodName: v.GoodName,
			PlanGoodNumber: v.PlanGoodNumber, Price: v.Price,
			PlanAmount: v.GoodCompute.PlanAmount, UseGoodNumber: v.UseGoodNumber,
			UseAmount: v.GoodCompute.UseAmount, PartnerBonusPercent: v.PartnerBonusPercent,
			PlanPartnerBonus: v.GoodCompute.PlanPartnerBonus, UsePartnerBonus: v.GoodCompute.UsePartnerBonus,
			PartnerBonusHas: v.GoodCompute.PartnerBonusHas, PartnerBonusNot: v.GoodCompute.PartnerBonusNot,
			PartnerBonusExpense: v.GoodCompute.PartnerBonusExpense, BonusStatus: v.BonusStatus,
			Remark: v.Remark,
		}
		if v.Id == 0 {
			_, err = tx.Model(m).
				Returning("*").
				Insert()
			if err != nil {
				return err
			}
			data[k].Id = m.Id
		} else {
			_, err = tx.Model(m).
				WherePK().
				Update()
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (repository OrderGoodRepository) Find(queryOption domain.OrderGoodFindQuery) ([]domain.OrderGood, int, error) {
	db := repository.transactionContext.PgDd
	goodModes := []models.OrderGood{}
	query := db.Model(&goodModes)
	if queryOption.OrderId > 0 {
		query = query.Where("order_id=?", queryOption.OrderId)
	}
	if queryOption.Offset > -1 {
		query = query.Offset(queryOption.Offset)
	}
	if queryOption.Limit > 0 {
		query = query.Limit(queryOption.Limit)
	} else {
		query = query.Limit(100)
	}
	var (
		err        error
		orderGoods = make([]domain.OrderGood, 0)
	)
	query = query.Order("order_good.id")
	count, err := query.SelectAndCount()
	if err != nil {
		return orderGoods, 0, err
	}
	for i := range goodModes {
		domainOrderGood, err := repository.transformPgModelToDomainModel(&goodModes[i])
		if err != nil {
			return orderGoods, 0, err
		}
		orderGoods = append(orderGoods, domainOrderGood)
	}
	return orderGoods, count, nil
}

func (repository OrderGoodRepository) Remove(orderId int64, goodids ...int64) error {
	var (
		err error
		tx  = repository.transactionContext.PgTx
	)
	m := &models.OrderGood{}
	query := tx.Model(m).Where("order_id=?", orderId)
	if len(goodids) > 0 {
		query = query.WhereIn("id in(?)", goodids)
	}
	_, err = query.Delete()
	return err
}