package twi

import (
	"time"

	"courseSign/server/global"
	"courseSign/server/model/common/request"
	"courseSign/server/model/twi"
	twiReq "courseSign/server/model/twi/request"
)

type TwiMetamaskService struct {
}

// CreateTwiMetamask 创建TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService) CreateTwiMetamask(twiMetamask twi.TwiMetamask) (err error) {
	err = global.GVA_DB.Create(&twiMetamask).Error
	return err
}

// DeleteTwiMetamask 删除TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)DeleteTwiMetamask(twiMetamask twi.TwiMetamask) (err error) {
	err = global.GVA_DB.Delete(&twiMetamask).Error
	return err
}

// DeleteTwiMetamaskByIds 批量删除TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)DeleteTwiMetamaskByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]twi.TwiMetamask{},"id in ?",ids.Ids).Error
	return err
}

// UpdateTwiMetamask 更新TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)UpdateTwiMetamask(twiMetamask twi.TwiMetamask) (err error) {
	err = global.GVA_DB.Save(&twiMetamask).Error
	return err
}

// GetTwiMetamask 根据id获取TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)GetTwiMetamask(id uint) (twiMetamask twi.TwiMetamask, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&twiMetamask).Error
	return
}

// GetTwiMetamaskInfoList 分页获取TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)GetTwiMetamaskInfoList(info twiReq.TwiMetamaskSearch) (list []twi.TwiMetamask, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&twi.TwiMetamask{})
    var twiMetamasks []twi.TwiMetamask
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Address != "" {
        db = db.Where("address = ?",info.Address)
    }
    if info.TwiName != "" {
        db = db.Where("twi_name LIKE ?","%"+ info.TwiName+"%")
    }
    if info.TwiId != "" {
        db = db.Where("twi_id = ?",info.TwiId)
    }
    if info.TwiUsername != "" {
        db = db.Where("twi_username LIKE ?","%"+ info.TwiUsername+"%")
    }
        if info.StartBindTime != nil && info.EndBindTime != nil {
            db = db.Where("bind_time BETWEEN ? AND ? ",info.StartBindTime,info.EndBindTime)
        }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&twiMetamasks).Error
	return  twiMetamasks, total, err
}

// GetTwiMetamask 根据id获取TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)GetTwiMetamaskByAddr(addr string) (twiMetamask twi.TwiMetamask, err error) {
	err = global.GVA_DB.Where("address = ?", addr).First(&twiMetamask).Error
	return
}

// GetTwiMetamaskByName 根据id获取TwiMetamask记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)GetTwiMetamaskByName(name string) (twiMetamask twi.TwiMetamask, err error) {
	err = global.GVA_DB.Where("twi_username = ?", name).First(&twiMetamask).Error
	return
}

// SetUserBindTwi 设置地址为绑定状态
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)SetUserBindTwi(addr string,me twiReq.TwiUserMe) (err error) {
	err = global.GVA_DB.Model(&twi.TwiMetamask{}).Where("address = ?", addr).UpdateColumns(map[string]interface{}{
		"is_twi_auth":true,
		"bind_time":time.Now(),
		"twi_id":me.Id,
		"twi_name":me.Name,
		"twi_username":me.Username,
		"twi_photo_url":me.ProfileImageUrl,
	}).Error
	return
}

// GetTelegramByName 根据name查询tele信息
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)GetTelegramByName(name string) (twiMetamask twi.TwiMetamask, err error) {
	err = global.GVA_DB.Where("tele_username = ?", name).First(&twiMetamask).Error
	return
}

// UnbindTwiMetamaskTwitter 解绑twitter 相关信息
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)UnbindTwiMetamaskTwitter(id uint) (err error) {
	err = global.GVA_DB.Model(&twi.TwiMetamask{}).Where("id = ?",id).UpdateColumns(map[string]interface{}{
		"is_twi_auth":false,
		"bind_time":nil,
		"twi_id":"",
		"twi_name":"",
		"twi_username":"",
		"twi_photo_url":"",
	}).Error
	return err
}

// UnbindTwiMetamaskTelegram 解绑tele 相关信息
// Author [piexlmax](https://github.com/piexlmax)
func (twiMetamaskService *TwiMetamaskService)UnbindTwiMetamaskTelegram(id uint) (err error) {
	return global.GVA_DB.Model(&twi.TwiMetamask{}).Where("id = ?",id).UpdateColumns(map[string]interface{}{
		"is_tele_auth":false,
		"tele_id":"",
		"tele_first_name":"",
		"tele_last_name":"",
		"tele_username":"",
		"tele_photo_url":"",
	}).Error
}

