|
|
package main
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"crypto/md5"
|
|
|
"dsBaseRpc/Utils/CommonUtil"
|
|
|
"dsBaseRpc/Utils/ConfigUtil"
|
|
|
"dsBaseRpc/Utils/DbUtil"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
var db = DbUtil.Engine
|
|
|
|
|
|
//每次获取的条数
|
|
|
var limit = 200
|
|
|
|
|
|
//默认开始时间
|
|
|
var defaultStartTs = "1970-01-01 00:00:00"
|
|
|
|
|
|
// 日志文件路径
|
|
|
var progressFilePath = "/usr/local/SyncDataLogs/"
|
|
|
|
|
|
//建立与汇集中心的主题映射关系结构
|
|
|
type tableStruct struct {
|
|
|
TableName string `json:"table_name"`
|
|
|
PrimaryKey string `json:"primary_key"`
|
|
|
DataSource string `json:"data_source"`
|
|
|
}
|
|
|
|
|
|
//有同步哪些表(增量),之所以不遍历文件的名称进行上报,是因为需要控制上传的顺序,如果只是文件名,就丢失了顺序
|
|
|
var IncrSqlDict = []tableStruct{
|
|
|
{TableName: "t_base_organization", PrimaryKey: "org_id", DataSource: "org_school"},
|
|
|
{TableName: "t_base_class", PrimaryKey: "class_id", DataSource: "org_class"},
|
|
|
{TableName: "t_base_teacher", PrimaryKey: "teacher_id", DataSource: "user_teacher"},
|
|
|
{TableName: "t_base_student", PrimaryKey: "student_id", DataSource: "user_student"},
|
|
|
{TableName: "t_sys_loginperson_log", PrimaryKey: "id_int", DataSource: "log_login"},
|
|
|
}
|
|
|
|
|
|
// 全量数据上报
|
|
|
var FullSqlDict = []tableStruct{
|
|
|
{TableName: "t_base_organization", PrimaryKey: "org_id", DataSource: "org_school"},
|
|
|
{TableName: "t_sys_dict", PrimaryKey: "dict_id", DataSource: "sys_dic"},
|
|
|
{TableName: "t_gov_area", PrimaryKey: "area_code", DataSource: "org_area"},
|
|
|
}
|
|
|
|
|
|
// 数据上报的结构体
|
|
|
type postStruct struct {
|
|
|
AuthToken string `json:"auth_token"`
|
|
|
DataSource string `json:"data_source"`
|
|
|
SystemId string `json:"system_id"`
|
|
|
OrgId string `json:"org_id"`
|
|
|
QueryPage int `json:"query_page"`
|
|
|
QueryTime string `json:"query_time"`
|
|
|
}
|
|
|
|
|
|
//系统token
|
|
|
var SystemToken = ""
|
|
|
|
|
|
//是否成功
|
|
|
var success = false
|
|
|
|
|
|
// 获取签名用的结构体
|
|
|
type authStruct struct {
|
|
|
AuthTime string `json:"auth_time"`
|
|
|
SystemId string `json:"system_id"`
|
|
|
SystemToken string `json:"system_token"`
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:获取系统token
|
|
|
作者:黄海
|
|
|
时间:2020-07-22
|
|
|
*/
|
|
|
func getSystemToken() (bool, string) {
|
|
|
//(1)计算出system_token=MD5.hash(MD5.hash(system_id+auth_time)+system_key)
|
|
|
var as authStruct
|
|
|
as.AuthTime = CommonUtil.GetCurrentTime()
|
|
|
as.SystemId = ConfigUtil.DataExchangeSystemId
|
|
|
//计算 md5
|
|
|
w := md5.New()
|
|
|
io.WriteString(w, as.SystemId+as.AuthTime)
|
|
|
//将str写入到w中
|
|
|
md5str := fmt.Sprintf("%x", w.Sum(nil))
|
|
|
w = md5.New()
|
|
|
io.WriteString(w, md5str+ConfigUtil.DataExchangeSystemKey)
|
|
|
//将str写入到w中
|
|
|
md5str = fmt.Sprintf("%x", w.Sum(nil))
|
|
|
//系统token
|
|
|
as.SystemToken = md5str
|
|
|
|
|
|
//(2)根据system_token换取authToken
|
|
|
jsonBytes, _ := json.Marshal(as)
|
|
|
|
|
|
p := httpDo("POST", ConfigUtil.DataExchangeSystemAuthUrl, string(jsonBytes))
|
|
|
if !p.Success {
|
|
|
fmt.Println(CommonUtil.GetCurrentTime() + "获取认证签名失败!")
|
|
|
return false, "获取认证签名失败!"
|
|
|
}
|
|
|
return true, p.Message
|
|
|
}
|
|
|
|
|
|
type ResultStruct struct {
|
|
|
Message string `json:"message"`
|
|
|
Success bool `json:"success"`
|
|
|
}
|
|
|
|
|
|
// 基础方法,这里多用于访问webapi,配合上json转换。
|
|
|
func httpDo(method string, url string, msg string) ResultStruct {
|
|
|
var p ResultStruct
|
|
|
p.Success = false
|
|
|
p.Message = "上报到汇集系统失败!请检查是否SystemToken是有效的,或者有两个及以上客户端同时在上报。"
|
|
|
client := &http.Client{}
|
|
|
body := bytes.NewBuffer([]byte(msg))
|
|
|
req, err := http.NewRequest(method,
|
|
|
url,
|
|
|
body)
|
|
|
if err != nil {
|
|
|
// handle error
|
|
|
}
|
|
|
req.Header.Set("Content-Type", "application/json;charset=utf-8")
|
|
|
resp, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return p
|
|
|
}
|
|
|
defer resp.Body.Close()
|
|
|
resultBody, err := ioutil.ReadAll(resp.Body)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return p
|
|
|
}
|
|
|
json.Unmarshal(resultBody, &p)
|
|
|
return p
|
|
|
}
|
|
|
|
|
|
func main() {
|
|
|
//获取系统token
|
|
|
success, SystemToken = getSystemToken()
|
|
|
// 访问地址
|
|
|
url := `http://fort.edusoa.com:7777/v1/dataex/DataexPage`
|
|
|
var a postStruct
|
|
|
a.AuthToken=SystemToken
|
|
|
a.DataSource="org_school"
|
|
|
a.OrgId="-1"
|
|
|
a.QueryPage=0
|
|
|
a.QueryTime=CommonUtil.GetCurrentTime()
|
|
|
a.SystemId=ConfigUtil.DataExchangeSystemId
|
|
|
|
|
|
//(2)根据system_token换取authToken
|
|
|
jsonBytes, _ := json.Marshal(a)
|
|
|
p := httpDo("POST", url, string(jsonBytes))
|
|
|
if !p.Success {
|
|
|
fmt.Println(p)
|
|
|
}
|
|
|
fmt.Println(p)
|
|
|
}
|