pg_order_base_dao.go 2.4 KB
package dao

import (
	"fmt"

	"github.com/go-pg/pg/v10/orm"

	"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 OrderBaseDao struct {
	transactionContext *transaction.TransactionContext
}

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

func (dao OrderBaseDao) OrderCodeExist(code string, notId ...int64) (bool, error) {
	tx := dao.transactionContext.GetDB()
	m := &models.OrderBase{}
	query := tx.Model(m).Where("order_code=?", code)
	if len(notId) > 0 {
		query = query.WhereIn("id not in(?)", notId)
	}
	ok, err := query.Exists()
	return ok, err
}

func (dao OrderBaseDao) DeliveryCodeExist(code string, companyId int64, notId ...int64) (bool, error) {
	tx := dao.transactionContext.GetDB()
	m := &models.OrderBase{}
	query := tx.Model(m).Where("delivery_code=?", code).Where("company_id=?", companyId)
	if len(notId) > 0 {
		query = query.WhereIn("id not in(?)", notId)
	}
	ok, err := query.Exists()
	return ok, err
}

//OrderListByCondition 根据条件获取订单分红列表
//orderType 订单类型
//partnerOrCode 合伙人姓名或订单号或发货单号
func (dao OrderBaseDao) OrderBonusListByCondition(companyId int64, orderType int, partnerOrCode string, limit, offset int) ([]models.OrderBase, int, error) {
	tx := dao.transactionContext.GetDB()
	var orders []models.OrderBase
	query := tx.Model(&orders).Where("order_base.company_id=?", companyId)
	if orderType > 0 {
		query = query.Where("order_base.order_type=?", orderType)
	} else {
		query = query.Where("order_base.order_type<>?", domain.OrderIntention)
	}
	if len(partnerOrCode) > 0 {
		query = query.Join("LEFT JOIN partner_info as p ON order_base.partner_id=p.id").
			WhereGroup(func(q *orm.Query) (*orm.Query, error) {
				q = q.WhereOr("order_base.order_code like ? ", "%"+partnerOrCode+"%").
					WhereOr("order_base.delivery_code like ? ", "%"+partnerOrCode+"%").
					WhereOr("p.partner_name like ? ", "%"+partnerOrCode+"%")
				return q, nil
			})
	}
	query = query.Offset(offset).Limit(limit)
	cnt, err := query.SelectAndCount()
	return orders, cnt, err
}