package data

import (
	"PaymentCenter/app/http/entities"
	"PaymentCenter/app/models/paychannelmodel"
	"database/sql"
	"xorm.io/builder"
	"xorm.io/xorm"
)

type PayChannelRepo struct {
	repo xorm.Interface
}

func NewPayChannelRepo(repo xorm.Interface) *PayChannelRepo {
	return &PayChannelRepo{
		repo: repo,
	}
}

func (m *PayChannelRepo) PayChannelList(conn builder.Cond, pageFilter entities.PageRequest, payChannelList *[]paychannelmodel.PayChannel) (int64, error) {
	repo := m.repo.Where(conn)
	if pageFilter.Page > 0 {
		repo = repo.Limit(pageFilter.PageSize, pageFilter.PageSize*(pageFilter.Page-1))
	}
	return repo.Desc("create_time").FindAndCount(payChannelList)
}

func (m *PayChannelRepo) PayChannelInsertOne(payChannel *paychannelmodel.PayChannel) (int64, error) {
	return m.repo.InsertOne(payChannel)
}

func (m *PayChannelRepo) PayChannelDelete(payChannel *paychannelmodel.PayChannel, conn builder.Cond) (int64, error) {
	return m.repo.Where(conn).Delete(payChannel)
}

// columns 参数为要更新的字段,即使为空
func (m *PayChannelRepo) PayChannelUpdate(payChannel *paychannelmodel.PayChannel, conn builder.Cond, columns ...string) (int64, error) {
	return m.repo.Where(conn).MustCols(columns...).Update(payChannel)
}

func (m *PayChannelRepo) PayChannelGet(payChannel *paychannelmodel.PayChannel, conn builder.Cond) (bool, error) {
	return m.repo.Where(conn).Get(payChannel)
}

func (m *PayChannelRepo) PayChannelFindOne(payChannel *paychannelmodel.PayChannel, conn builder.Cond, columns ...string) (*paychannelmodel.PayChannel, error) {
	has, err := m.repo.Where(conn).Get(payChannel)
	if !has {
		return nil, sql.ErrNoRows
	}
	return payChannel, err
}