pg_order_dao.go 2.9 KB
package dao

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	"time"
)

type OrderDao struct {
	transactionContext *transaction.TransactionContext
}

//订单统计
func (dao *OrderDao) OrderStatics(option *domain.OrderStaticQuery) (count int, amount float64, err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.Order)
	q := tx.Model(order)
	q.ColumnExpr("count(*) count")
	q.ColumnExpr("sum(order_actual_amount) total_order_amount")
	if option.PartnerId > 0 {
		q.Where(`"order".partner_id =?`, option.PartnerId)
	}
	if option.OrderType > 0 {
		q.Where(`"order".order_type =?`, option.OrderType)
	}
	if option.OrderStatus > 0 {
		q.Where(`"order".order_status =?`, option.OrderStatus)
	}
	if option.BeginTime > 0 {
		q.Where(`"order".create_at >=?`, time.Unix(option.BeginTime/1000, 0))
	}
	if option.EndTime > 0 {
		q.Where(`"order".create_at <?`, time.Unix(option.EndTime/1000, 0))
	}
	err = q.Select(&count, &amount)
	return
}

//分红订单
func (dao *OrderDao) DividendOrders(option *domain.DividendOrdersQueryOption) (count int, orders []*models.Order, err error) {
	tx := dao.transactionContext.PgTx
	q := tx.Model(&orders)
	q.Column(`order.id`, `order.order_code`, `order.order_actual_amount`, `order.partner_bonus_percent`)
	q.Where(`"order".order_status >=?`, domain.OrderStatusDeliverSome) //已经发货
	if option.PartnerId > 0 {
		q.Where(`"order".partner_id=?`, option.PartnerId)
	}
	if option.StartTime > 0 {
		q.Where(`"order".create_at >=?`, time.Unix(option.StartTime/1000, 0))
	}
	if option.EndTime > 0 {
		q.Where(`"order".create_at <?`, time.Unix(option.EndTime/1000, 0))
	}
	if option.DividendAction == 0 { //累计分红
		if option.DetailAction == 0 { //已收明细
			q.Join(`JOIN order_payment as a ON a.order_id="order".id`)
			q.Where(`"a".bonus_status=?`, domain.BonusPaid)
		} else if option.DetailAction == 1 { //未收明细
			q.Where(`"order".order_actual_amount>"order".order_payment_amount`)
		}
	} else if option.DividendAction == 1 { //分红支出
		q.Where(`"order".order_amount>"order".order_actual_amount`)
	}
	if option.Limit > 0 {
		q.Limit(option.Limit)
	}
	if option.Offset > 0 {
		q.Offset(option.Offset)
	}
	count, err = q.Distinct().SelectAndCount()
	return
}

func (dao *OrderDao) UpdateLastViewTime(id int64, lastViewTime time.Time) (err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.Order)
	q := tx.Model(order).Set("last_view_time = ?", lastViewTime)
	q.Where("id=?", id)
	_, err = q.Update()
	return
}

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