package db

import (
	"applet/app/db/model"
	"applet/app/utils/logx"

	"xorm.io/xorm"
)

// GetFinUserFlowByID is 用户流水记录
func GetFinUserFlowByID(Db *xorm.Engine, id interface{}) (*model.FinUserFlow, error) {
	var m model.FinUserFlow
	if has, err := Db.Where("id = ?", id).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// GetFinUserFlowByID is 用户流水记录
func GetFinUserFlowByUIDANDOID(Db *xorm.Engine, types, uid, ordId string) (*model.FinUserFlow, error) {
	var m model.FinUserFlow
	if has, err := Db.Where("uid = ? and ord_id = ? and type = ?", uid, ordId, types).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	return &m, nil
}
func GetFinUserFlowByUIDANDOIDTOORDTYPE(Db *xorm.Engine, types, uid, ordId string) (*model.FinUserFlow, error) {
	var m model.FinUserFlow
	if has, err := Db.Where("uid = ? and ord_id = ? and ord_type = ?", uid, ordId, types).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// GetFinUserFlowByID is 用户流水记录
func GetFinUserFlowByOIDANDORDTYPE(Db *xorm.Engine, types, ordId, ordType string) (*model.FinUserFlow, error) {
	var m model.FinUserFlow
	if has, err := Db.Where("ord_id = ? and ord_action = ? and ord_type= ?", ordId, types, ordType).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

//FinUserFlowInsertOne is 插入一条流水记录
func FinUserFlowInsertOne(Db *xorm.Engine, m *model.FinUserFlow) error {
	_, err := Db.InsertOne(m)
	if err != nil {
		return err
	}
	return nil
}

//FinUserFlowInsertOne is 插入一条流水记录
func FinUserFlowWithSessionInsertOne(session *xorm.Session, m *model.FinUserFlow) error {
	_, err := session.InsertOne(m)
	if err != nil {
		return err
	}
	return nil
}

// FinUserFlowByUID is 用户流水
func FinUserFlowInputByUID(Db *xorm.Engine, uid interface{}, time string, limit, start int) ([]*model.FinUserFlow, error) {
	var m []*model.FinUserFlow
	if err := Db.Where("uid = ? AND create_at like ? and (amount> ? or ord_type=?)", uid, time+"%", "0", "fast_return").In("type", "0").Desc("create_at").Limit(limit, start).Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return m, nil
}
func FinUserFlowInputByUIDWithAmount(Db *xorm.Engine, uid interface{}, types, before_amount, after_amount string) (*model.FinUserFlow, error) {
	var m model.FinUserFlow
	if has, err := Db.Where("uid = ? and ord_type='withdraw' and ord_action = ? and before_amount= ? and after_amount = ?", uid, types, before_amount, after_amount).Get(&m); err != nil || !has {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// FinUserFlowByUIDByOrderAction is 用户流水 by OrderAction
func FinUserFlowInputByUIDByOrderActionByTime(Db *xorm.Engine, uid, oa interface{}, time string, limit, start int) ([]*model.FinUserFlow, error) {
	var m []*model.FinUserFlow
	if err := Db.Where("uid = ?  AND create_at like ? and amount>0", uid, time+"%").In("ord_action", oa).Desc("create_at").Limit(limit, start).Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return m, nil
}

// FinUserFlowByUIDByOrderAction is 用户流水 by OrderAction
func FinUserFlowInputByUIDByTypeByTime(Db *xorm.Engine, uid int, time string, limit, start int) ([]*model.FinUserFlow, error) {
	var m []*model.FinUserFlow
	if err := Db.Where("uid = ? AND type = 1 AND create_at like ? and (amount>0 or ord_type=? or ord_action=?)", uid, time+"%", "fast_return", 101).Desc("create_at").Limit(limit, start).Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return m, nil
}

// 在事务中使用，插入一条流水记录
func FinUserFlowInsertOneWithSession(session *xorm.Session, m *model.FinUserFlow) error {
	_, err := session.InsertOne(m)
	if err != nil {
		return err
	}
	return nil
}
