package system

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

const (
	AdminVerifyCodeRecordTypeMobile = 1 // 验证方式-手机号
	AdminVerifyCodeRecordTypeEmail  = 2 // 验证方式-邮箱

	AdminVerifyCodeRecordSourceLogin    = 1 // 验证码来源-登录
	AdminVerifyCodeRecordSourceAbnormal = 2 // 验证码来源-异常登录校验
	AdminVerifyCodeRecordSourceForget   = 3 // 验证码来源-忘记密码

	AdminVerifyCodeRecordStatusSuccess = 1 // 验证码发送状态-已发送
	AdminVerifyCodeRecordStatusFail    = 2 // 验证码发送状态-发送失败
)

// AdminVerifyCodeRecord 短信邮箱验证码记录表
type AdminVerifyCodeRecord struct {
	Id          int       `orm:"column(id);pk"`
	VerifyType  int       `description:"验证方式：1-手机号；2-邮箱"`
	Source      int       `description:"来源：1-登录；2-异常登录校验；3-忘记密码"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱"`
	Code        string    `description:"验证码"`
	ExpiredTime time.Time `description:"验证码过期时间"`
	SendResult  string    `description:"发送结果"`
	SendStatus  int       `description:"发送状态：0-待发送；1-已发送；2-发送失败"`
	RequestId   string    `description:"请求ID"`
	CreateTime  time.Time `description:"创建时间"`
	ModifyTime  time.Time `description:"更新时间"`
}

func (m *AdminVerifyCodeRecord) TableName() string {
	return "admin_verify_code_record"
}

func (m *AdminVerifyCodeRecord) PrimaryId() string {
	return "id"
}

func (m *AdminVerifyCodeRecord) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.Id = int(id)
	return
}

func (m *AdminVerifyCodeRecord) CreateMulti(items []*AdminVerifyCodeRecord) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *AdminVerifyCodeRecord) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *AdminVerifyCodeRecord) Del() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.Id).Exec()
	return
}

func (m *AdminVerifyCodeRecord) GetItemById(id int) (item *AdminVerifyCodeRecord, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *AdminVerifyCodeRecord) GetItemByCondition(condition string, pars []interface{}) (item *AdminVerifyCodeRecord, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *AdminVerifyCodeRecord) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *AdminVerifyCodeRecord) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AdminVerifyCodeRecord, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *AdminVerifyCodeRecord) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AdminVerifyCodeRecord, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}
