package api_proxy

import (
	"DataShare/global"
	"DataShare/model"
	"DataShare/util"
	"encoding/json"
	"fmt"
	"github.com/sirupsen/logrus"
	"strings"
	"sync"
)
//类成员
type CQ119 struct{
	Xftoken  string  	`json:"xftoken"`
	UserName string     `json:"userName"` //yzzcptcs
	Password string     `json:"password"` //ADmin123456!
	LoginUrl string     `json:"loginUrl"` //http://dam.cq119.gov.cn:9528/admin-ht/api/external/stoken
}
//请求结构体
type CQ119RequestBody struct{
	Page      int      `json:"page"`
	Size      int      `json:"size"`
	Search    string   `json:"search"`
	RowIdOnly      int    `json:"rowIdOnly"`
}
//xftoken
type CQ119_Xftoken_Resp struct{
	Code   int    `json:"code"`
	Xftoken   string    `json:"xftoken"`

}
var histance_119 *CQ119
var once_119 sync.Once

func CQ119_GetInstance() *CQ119{
	once_119.Do(func() {
		histance_119 = &CQ119{
			UserName: global.SystemConfig.Cq119.Username,
			Password: global.SystemConfig.Cq119.Password,
			LoginUrl: global.SystemConfig.Cq119.LoginUrl,
			Xftoken: "",
		}
	})

	return histance_119
}

func(this *CQ119) CQ119_OnAuth(url string, username string, password string)([]byte, error){

	type User struct{
		UserName  string   `json:"username"`
		Password  string   `json:"password"`
	}
	authReq := model.Auth119Info{
		UserName: username,
		Password: password,
	}

	jsonData,err := json.Marshal(authReq)
	if err != nil {
		return nil,err
	}

	byteData,err := util.HttpPost(url,jsonData,"")
	fmt.Println(string(byteData))

	return byteData,err
}

func(this *CQ119) CQ119_ApiRequest(url string, req *CQ119RequestBody)([]byte, error){

	jsonData,err := json.Marshal(req)
	if err != nil {
		return nil,err
	}
	byteData,err := util.HttpPost(url,jsonData,this.Xftoken)
	//fmt.Println(string(byteData))
	if strings.Contains(string(byteData),"请申请合法的token后再调用此接口") {
		authInfo, err := this.CQ119_OnAuth(this.LoginUrl,this.UserName,this.Password)
		if err != nil {
			return nil, err
		}

		var xftoken_resp CQ119_Xftoken_Resp
		err = json.NewDecoder(strings.NewReader(string(authInfo))).Decode(&xftoken_resp)
		if err != nil{
			global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error()))
			return nil,err
		}

		this.Xftoken = xftoken_resp.Xftoken
		//再来请求一遍
		byteData,err = util.HttpPost(url,jsonData,this.Xftoken)
		if err != nil{
			return nil,err
		}
	}

	//var tempMap map[string]interface{}
	//err = json.Unmarshal(byteData, &tempMap)
	//if err != nil {
	//	panic(err)
	//}
	//for k, values := range tempMap {
	//	fmt.Printf("%s: %s\n", k, values) // ... and this line
	//}

	return byteData,err
}

func(this *CQ119) CQ119_ApiCall(url string, param []byte)(map[string]interface{},[]byte, error){

	//jsonData,err := json.Marshal(param)
	//if err != nil {
	//	return nil,err
	//}
	byteData,err := util.HttpPost(url,param,this.Xftoken)
	//fmt.Println(string(byteData))
	if strings.Contains(string(byteData),"请申请合法的token后再调用此接口") {
		authInfo, err := this.CQ119_OnAuth(this.LoginUrl,this.UserName,this.Password)
		if err != nil {
			return nil,nil, err
		}

		var xftoken_resp CQ119_Xftoken_Resp
		err = json.NewDecoder(strings.NewReader(string(authInfo))).Decode(&xftoken_resp)
		if err != nil{
			global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error()))
			return nil,nil,err
		}

		this.Xftoken = xftoken_resp.Xftoken
		//再来请求一遍
		byteData,err = util.HttpPost(url,param,this.Xftoken)
		if err != nil{
			return nil,nil,err
		}
	}

	var tempMap map[string]interface{}
	err = json.Unmarshal(byteData, &tempMap)
	if err != nil {
		panic(err)
	}
	//for k, values := range tempMap {
	//	fmt.Printf("%s: %s\n", k, values) // ... and this line
	//}

	return tempMap,byteData,err
}

