pg_partner_dao.go 2.3 KB
package dao

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"github.com/go-pg/pg/v10/orm"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)

type PartnerInfoDao struct {
	transactionContext *transaction.TransactionContext
}

func (dao *PartnerInfoDao) Update(queryOptions map[string]interface{}) error {
	tx := dao.transactionContext.PgTx
	m := new(models.PartnerInfo)
	query := NewQuery(tx.Model(m), queryOptions)
	query.SetUpdate(`account=?`, "Account")
	query.SetUpdate(`password=?`, "Password")
	query.SetWhere(`id=?`, "Id")
	if _, ok := queryOptions["Id"]; !ok {
		return fmt.Errorf(`error: miss param "Id"`)
	}
	_, err := query.Update()
	return err
}

func (dao *PartnerInfoDao) PartnerStatic(queryOptions map[string]interface{}) (count int, err error) {
	tx := dao.transactionContext.PgTx
	m := new(models.PartnerInfo)
	query := NewQuery(tx.Model(m), queryOptions)

	query.ColumnExpr("count(*) count")
	if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
		query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
	}
	if inPartnerCategory, ok := queryOptions["inPartnerCategory"]; ok {
		query.Where(`partner_info.partner_category_infos @>'[{"id":?}]'`, inPartnerCategory)
	}
	// 多个合伙人类型
	if inPartnerCategory, ok := queryOptions["inPartnerCategories"]; ok {
		query.Query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
			if arrayInPartnerCategory, ok := inPartnerCategory.([]int64); ok {
				for i := range arrayInPartnerCategory {
					q.WhereOr(`partner_info.partner_category_infos @>'[{"id":?}]')`, arrayInPartnerCategory[i])
				}
			}
			return q, nil
		})
	}
	err = query.Select(&count)
	return
}

//func(dao *PartnerInfoDao)PartnerCompanyInfo(queryOptions map[string]interface{})(values map[string]interface{},err error){
////	tx := dao.transactionContext.PgTx
////	m := new(models.PartnerInfo)
////	query := NewQuery(tx.Model(m), queryOptions)
////	query.SetWhere("status=?","status")
////}

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