package pc

import (
	"database/sql"
	_ "github.com/mattn/go-sqlite3"
	"time"
	"math/rand"
	"strings"
	"pkg/app"
)

//拉取在线的算力设备
func ipAddress() []string {
	now := time.Now()
    hour := now.Hour()
	minute := now.Minute()
	t := hour*100 + minute -100
	db, err := sql.Open("sqlite3", "info.db")
	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()
	rows, _ := db.Query(`select average from Average;`)
	defer rows.Close()
	var average int
	rows.Next()
	rows.Scan(&average)
	rows, _ = db.Query(`select ipAddress from Endpoint where times <= ? and isOnLine >= ?;`, average, t)
	defer rows.Close()
	var endpoint Endpoint
	ipAddress := ""
	for rows.Next() {
		rows.Scan(&endpoint.ipAddress)
		ipAddress += endpoint.ipAddress + ","
	}
	rows.Close()
	sep := ","
	IpAddress := strings.Split(ipAddress, sep)
	return IpAddress
}

func init() {
	rand.Seed(time.Now().UnixNano())
}


//洗牌算法
func shuffle(n []string) {
	for i := 0; i < len(n); i++ {
		a := rand.Intn(len(n))
		b := rand.Intn(len(n))
		n[a], n[b] = n[b], n[a]
	}
}

//返回可进行运算的设备地址
func request(c *gin.Contex) []string {
	appG := app.Gin{C: c}
	var ipAddress = ipAddress()
	for i := 0; i < 3; i++ {
		shuffle(ipAddress)
	}
	appG.Response(http.StatusOK, "成功", null)
}