package main

import (
	"encoding/json"
	"fmt"
	"io"
	"jiujunet.com/alipay"
	"net/http"
	"time"
)

type (
	IpRes struct {
		Ret  string  `json:"ret"`
		Data *IpInfo `json:"data"`
	}

	IpInfo struct {
		Ip       string   `json:"ip"`
		Location []string `json:"location"`
	}

	GroupRole struct {
		Desc    string `json:"desc"`
		GroupId string `json:"groupId"`
		RuleId  string `json:"ruleId"`
	}
)

var (
	lastIp        = ""
	groupRoleList = []*GroupRole{
		&GroupRole{
			Desc:    "收银系统正式-宝塔",
			GroupId: "sg-bp1csfr2wcn7ujwoabkk",
			RuleId:  "sgr-bp1adiq57mnoe3asdpoh",
		},
		&GroupRole{
			Desc:    "收银系统正式-22",
			GroupId: "sg-bp1csfr2wcn7ujwoabkk",
			RuleId:  "sgr-bp10yy3lg5lw9g6qhv8o",
		},
		&GroupRole{
			Desc:    "支付系统-22",
			GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
			RuleId:  "sgr-bp1dwhn7mi8mlf60xcgo",
		},
		&GroupRole{
			Desc:    "支付系统-支付宝他",
			GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
			RuleId:  "sgr-bp1653xk85wnusza1nvh",
		},
		&GroupRole{
			Desc:    "支付系统-预发布宝塔",
			GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
			RuleId:  "sgr-bp1idleb69yzsb69yluz",
		},
		&GroupRole{
			Desc:    "支付系统-预发布宝塔",
			GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
			RuleId:  "sgr-bp1idleb69yzsb69yluz",
		},
		&GroupRole{
			Desc:    "银收客-22",
			GroupId: "sg-bp1hnylmydz8bi7gqtgj",
			RuleId:  "sgr-bp1flbqxvwq6f7ffu2qm",
		},
	}
)

func main() {
	for {
		updateServerSgIp()

		duration := time.Hour
		//duration := 5 * time.Second
		ticker := time.NewTicker(duration)
		<-ticker.C
		fmt.Println("定时结束!")
		ticker.Stop()
	}
}

func updateServerSgIp() {
	fmt.Println("updateServerSgIp start")
	fmt.Println("lastIp: ", lastIp)
	fmt.Println("start time: ", time.Now().Format("2006-01-02 15:04:05"))
	ipInfo := getSelfIp()

	if ipInfo == nil {
		fmt.Println("get self ip err")
		return
	}

	fmt.Println("ipInfo:", ipInfo)

	if !StrArrIsContain(ipInfo.Location, "西安") {
		fmt.Println("not in xian")
		return
	}

	if lastIp == ipInfo.Ip {
		fmt.Println("ip not change")
		return
	}

	lastIp = ipInfo.Ip

	fmt.Println("ip change, go to update aliyun")

	for _, groupRole := range groupRoleList {
		err := alipay.UpdateAliIp(groupRole.GroupId, groupRole.RuleId, ipInfo.Ip)
		if err != nil {
			fmt.Printf("UpdateAliIp err: %s, Desc: %s\n", err.Error(), groupRole.Desc)
		} else {
			fmt.Println("UpdateAliIp success: ", groupRole.Desc)
		}
	}

	err := alipay.UpdateAliIp("sg-bp1csfr2wcn7ujwoabkk", "sgr-bp1adiq57mnoe3asdpoh", ipInfo.Ip)
	if err != nil {
		fmt.Println("UpdateAliIp err:", err)
	}
}

func getSelfIp() *IpInfo {
	url := "https://myip.ipip.net/json"

	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("http.Get err:", err)
		return nil
	}

	defer func(Body io.ReadCloser) {
		e := Body.Close()
		if e != nil {

		}
	}(resp.Body)

	// 读取内容
	buf := make([]byte, 1024)
	n, _ := resp.Body.Read(buf)
	fmt.Println(string(buf[:n]))

	result := &IpRes{}
	err = json.Unmarshal(buf[:n], result)
	if err != nil {
		fmt.Println("json.Unmarshal err:", err)
		return nil
	}

	if result.Ret != "ok" {
		fmt.Println("result.Ret err:", result.Ret)
		return nil
	}

	return result.Data
}

func StrArrIsContain(arr []string, str string) bool {
	for _, v := range arr {
		if v == str {
			return true
		}
	}

	return false
}
