package aduserdb

//
//import (
//	"errors"
//	"go-admin/app/admin/models"
//	"go-admin/app/admin/models/sysmodel"
//	"time"
//
//	"go.uber.org/zap"
//	"gorm.io/gorm"
//
//	log "github.com/go-admin-team/go-admin-core/logger"
//)
//
//// 是否重复发送
//// ∵ now - send < 60 时，不能重复发送
//// ∴ send > now - 60 时，不能重复发送
//func IsRepeatSend(orm *gorm.DB, receive string, businessType int) (verifyCode models.AdVerifyCode, err error) {
//	// sqlSe := `
//	// 	SELECT id, sendtime, verifytime
//	// 	FROM ad_verifycode
//	// 	WHERE businesstype = $1 AND phone = $2 AND sendtime > $3 AND verifyflag = $4
//	// 	LIMIT 1
//	// `
//	// Now - 60
//	nowSub60 := time.Now().Add(-time.Minute)
//	err = orm.Model(verifyCode).Where("business_type =? AND phone =? AND send_time >? AND verify_flag =?", businessType, receive, nowSub60, 0).
//		First(&verifyCode).
//		Error
//
//	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
//		log.Error("IsRepeatSend", zap.Error(err))
//		return
//	}
//
//	return
//}
//
//// GetVerifyCode 获取验证码
//func GetVerifyCode(orm *gorm.DB, req sysmodel.CheckCaptcha) (verifyCode models.AdVerifyCode) {
//	req.Now = time.Now()
//	req.Flag = 0
//
//	//sql := `
//	//	SELECT id FROM ad_verifycode
//	//	WHERE phone = :phone AND code = :code AND sendtime < :now
//	//	AND verifytime > :now AND businesstype = :businesstype AND verifyflag = :verifyflag
//	//`
//	// 查询符合业务的未被验证的验证码
//	//if err = dbhelper.MasterPgdb.GetNameQuery(&verifyCode, sql, req); err != nil {
//	//	log.Error("GetVerifyCode", zap.Error(err))
//	//	return
//	//}
//
//	// sqlSe := `SELECT id,phone,code FROM ad_verifycode WHERE phone=$1 AND code=$2 AND businesstype=$3 AND verifyflag=0 AND sendtime<$4 AND verifytime>$4 ORDER BY id DESC LIMIT 1`
//	// err := dbhelper.MasterPgdb.Get(&verifyCode, sqlSe, req.Receive, req.Captcha, req.BusinessType, req.Now)
//	err := orm.Model(verifyCode).Where("phone =? AND code=? AND business_type=? AND verify_flag=0 AND send_time <? AND verify_time >?",
//		req.Receive, req.Captcha, req.BusinessType, req.Now, req.Now).
//		Order("id desc").
//		First(&verifyCode).
//		Error
//	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
//		log.Error("GetVerifyCode", zap.Error(err))
//	}
//	return
//}
//
//// // UpdateVerifyCode 更新验证码状态
//// func UpdateVerifyCode(cc sysmodel.CheckCaptcha, tx *sqlx.Tx) error {
//// 	cc.Flag = 1
//// 	cc.Now = time.Now()
//
//// 	sqlUp := `
//// 		UPDATE ad_verifycode
//// 		SET validtime = :now, verifyflag = :verifyflag
//// 		WHERE phone = :phone AND code = :code AND sendtime < :now AND verifytime > :now AND businesstype = :businesstype AND verifyflag = 0
//// 	`
//// 	var re sql.Result
//// 	var err error
//// 	if tx == nil {
//// 		re, err = dbhelper.MasterPgdb.NamedExec(sqlUp, cc)
//// 	} else {
//// 		// 更新该验证码为已验证
//// 		re, err = dbhelper.MasterPgdb.NamedExecTx(sqlUp, cc, tx)
//// 	}
//// 	if err != nil {
//// 		log.Error("UpdateVerifyCode", zap.Error(err))
//// 		return err
//// 	}
//// 	if row, _ := re.RowsAffected(); row == 0 {
//// 		return errors.New("验证码无效")
//// 	}
//
//// 	return nil
//// }
//
//// UpdateVerifyFlag 更新验证码状态,改为已经使用过该验证码
//func UpdateVerifyFlag(orm *gorm.DB, id int) error {
//	// sqlUp := `UPDATE ad_verifycode	SET validtime =$1, verifyflag =1 WHERE id=$2`
//	verifyCode := models.AdVerifyCode{}
//	verifyCode.Id = id
//
//	res := orm.Model(verifyCode).Updates(map[string]interface{}{"valid_time": time.Now(), "verify_flag": 1})
//	// re, err := dbhelper.MasterPgdb.ExecTx(sqlUp, tx, time.Now(), id)
//	if res.Error != nil {
//		log.Error("UpdateVerifyFlag", zap.Error(res.Error))
//		return res.Error
//	}
//	if res.RowsAffected == 0 {
//		return errors.New("验证码无效")
//	}
//	return nil
//
//	//cc.Flag = 1
//	//cc.Now = time.Now()
//	//sqlUp := `
//	//	UPDATE ad_verifycode
//	//	SET validtime = :now, verifyflag = :verifyflag
//	//	WHERE phone = :phone AND code = :code AND sendtime < :now AND verifytime > :now AND businesstype = :businesstype AND verifyflag = 0
//	//`
//	//var re sql.Result
//	//var err error
//	//if tx == nil {
//	//	re, err = dbhelper.MasterPgdb.NamedExec(sqlUp, cc)
//	//} else {
//	//	// 更新该验证码为已验证
//	//	re, err = dbhelper.MasterPgdb.NamedExecTx(sqlUp, cc, tx)
//	//}
//	//if err != nil {
//	//	log.Error("UpdateVerifyCode", zap.Error(err))
//	//	return err
//	//}
//	//if row, _ := re.RowsAffected(); row == 0 {
//	//	return errors.New("验证码无效")
//	//}
//	//return nil
//}
