package shanghu

import (
	orm "duoduo/database"
	"time"
)

type RechargeCancelLog struct {
	ID             int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
	ClientOpenID   string    `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`
	MerchantOpenID string    `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"`
	QuantityPre    int       `gorm:"column:quantity_pre;type:int(11)" json:"quantity_pre"`     // 核销前数量
	QuantityAfter  int       `gorm:"column:quantity_after;type:int(11)" json:"quantity_after"` // 核销后数量
	Quantity       int       `gorm:"column:quantity;type:int(11)" json:"quantity"`             // 数量
	Operator       string    `gorm:"column:operator;type:varchar(255)" json:"operator"`        // operator 操作人
	RechargeID     int64     `gorm:"column:recharge_id;type:bigint(20)" json:"recharge_id"`
	UpdateBy       int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`
	CreateBy       int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`
	CreatedAt      time.Time `gorm:"column:created_at;type:datetime" json:"created_at"`
	UpdatedAt      time.Time `gorm:"column:updated_at;type:datetime" json:"updated_at"`
	DeletedAt      time.Time `gorm:"column:deleted_at;type:datetime;default:null" json:"deleted_at"`
}

func (m *RechargeCancelLog) TableName() string {
	return "recharge_cancel_log"
}

// list 接口使用
func (m *RechargeCancelLog) RechargeCancelLogByClientList(pageSize int, pageIndex int) ([]RechargeCancelLog, int, error) {
	var doc []RechargeCancelLog

	table := orm.ShMysql.Table(m.TableName())

	table = table.Where("client_open_id = ? ", m.ClientOpenID)
	var count int
	if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Count(&count)
	return doc, count, nil
}

// list 接口使用
func (m *RechargeCancelLog) RechargeCancelLogByMerchantList(pageSize int, pageIndex int) ([]RechargeCancelLog, int, error) {
	var doc []RechargeCancelLog

	table := orm.ShMysql.Table(m.TableName())

	table = table.Where("merchant_open_id = ? ", m.MerchantOpenID)
	var count int
	if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Count(&count)
	return doc, count, nil
}