pg_order_dao.go 2.5 KB
package dao

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type OrderDao struct {
	transactionContext *transaction.TransactionContext
}

func (dao *OrderDao) Update(options map[string]interface{}) (err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.Order)
	q := tx.Model(order)
	if v, ok := options["orderPaymentAmount"]; ok {
		q.Set("order_payment_amount = ?", v)
	}
	if v, ok := options["bonusStatus"]; ok {
		q.Set("bonus_status = ?", v)
	}
	if v, ok := options["id"]; ok {
		q.Where("id = ?", v)
	}
	_, err = q.Update()
	return
}

func (dao *OrderDao) OrderCodeIsExist(code string, notId int64) (bool, error) {
	tx := dao.transactionContext.PgDd
	ok, err := tx.Model(new(models.Order)).
		Where("order_code=?", code).
		Where("id<>?", notId).
		Exists()
	return ok, err
}

func (dao *OrderDao) GetOrderBaseInfo(id int64) (data map[string]interface{}, err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.Order)

	data = make(map[string]interface{})
	q := tx.Model(order)
	q.Column("partner_id", "partner_bonus_percent", "order_payment_amount", "buyer")
	q.Where("id = ?", id)
	err = q.Select()
	if err == nil {
		data["PartnerId"] = order.PartnerId
		data["PartnerBonusPercent"] = order.PartnerBonusPercent
		data["OrderPaymentAmount"] = order.OrderPaymentAmount
		data["Buyer"] = order.Buyer
	}
	return
}

func (dao *OrderDao) GetDividendOrders(options map[string]interface{}) (count int, orders []*models.Order, err error) {
	tx := dao.transactionContext.PgTx
	//Order:=new(models.Order)
	q := tx.Model(&orders)
	q.ColumnExpr(`"order".*`, `partner_info.partner_name`)
	q.Relation("PartnerInfo")
	if v, ok := options["orderCode"]; ok && len(v.(string)) > 0 {
		q.Where(`"order".order_code like ?`, fmt.Sprintf("%%%v%%", v))
	}
	if v, ok := options["orderType"]; ok {
		q.Where(`"order".order_type=?`, v)
	}
	if v, ok := options["partnerId"]; ok && v.(int) > 0 {
		q.Where(`"order".partner_id =?`, v)
	}
	if v, ok := options["offset"]; ok {
		q.Offset(v.(int))
	}
	if v, ok := options["limit"]; ok {
		q.Limit(v.(int))
	}
	q.Order(`order.id DESC`)
	count, err = q.SelectAndCount()
	return
}

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