|
@@ -0,0 +1,164 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "hongze/hongze_task_trial/models/data_manage"
|
|
|
+ "hongze/hongze_task_trial/utils"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// SyncComTradeIndex 同步UN指标
|
|
|
+func SyncComTradeIndex() (err error) {
|
|
|
+ var startDate string
|
|
|
+ maxDate, err := data_manage.GetBaseFromComTradeMaxDate()
|
|
|
+ if err != nil || maxDate.IsZero() {
|
|
|
+ startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
|
|
|
+ } else {
|
|
|
+ startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
|
|
|
+ }
|
|
|
+
|
|
|
+ method := `index/list`
|
|
|
+ data := make(map[string]interface{})
|
|
|
+ data["Source"] = utils.DATA_SOURCE_COM_TRADE
|
|
|
+ data["StartDate"] = startDate
|
|
|
+ //data["EndDate"] = endDate
|
|
|
+ result, err := HttpPost(method, data)
|
|
|
+ utils.FileLog.Info(result)
|
|
|
+ fmt.Println(result)
|
|
|
+
|
|
|
+ respObj := new(data_manage.ComTradeIndexMappingResp)
|
|
|
+ err = json.Unmarshal([]byte(result), &respObj)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("json.Unmarshal err:" + err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指标处理
|
|
|
+ {
|
|
|
+ //获取所有指标信息 某一天的
|
|
|
+ allComTradeIndex, tmpErr := data_manage.GetAllComTradeIndexList(startDate)
|
|
|
+ if tmpErr != nil {
|
|
|
+ fmt.Println("get GetAllComTradeCodeMappingList err:" + tmpErr.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ existCodeMappingMap := make(map[string]*data_manage.ComTradeIndex)
|
|
|
+ for _, v := range allComTradeIndex {
|
|
|
+ existCodeMappingMap[v.IndexCode] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, zv := range respObj.Data.IndexList {
|
|
|
+ if _, ok := existCodeMappingMap[zv.IndexCode]; !ok {
|
|
|
+ newID, tmpErr := data_manage.AddBaseFromComTradeIndex(&zv)
|
|
|
+ if tmpErr != nil {
|
|
|
+ fmt.Println("insert error:", tmpErr)
|
|
|
+ }
|
|
|
+ fmt.Println("insert new indexID:", newID)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // mapping处理
|
|
|
+ {
|
|
|
+ //获取所有指标信息 某一天的
|
|
|
+ allCodeMappingIndex, tmpErr := data_manage.GetAllComTradeCodeMappingList(startDate)
|
|
|
+ if tmpErr != nil {
|
|
|
+ fmt.Println("get GetAllComTradeCodeMappingList err:" + tmpErr.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ existCodeMappingMap := make(map[string]*data_manage.ComTradeCodeMapping)
|
|
|
+ for _, v := range allCodeMappingIndex {
|
|
|
+ existCodeMappingMap[v.Code] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, zv := range respObj.Data.MappingList {
|
|
|
+ if _, ok := existCodeMappingMap[zv.Code]; !ok {
|
|
|
+ _, err = data_manage.AddBaseFromComTradeCodeMapping(&zv)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("AddBaseFromComTradeCodeMapping error:", err)
|
|
|
+ }
|
|
|
+ fmt.Println("AddBaseFromComTradeCodeMapping new Code:", zv.Code)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+// SyncComTradeData 同步UN指标数据
|
|
|
+func SyncComTradeData() (err error) {
|
|
|
+ startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
|
|
|
+ //var startDate string
|
|
|
+ maxDate, err := data_manage.GetBaseFromComTradeMaxDate()
|
|
|
+ if err != nil || maxDate.IsZero() {
|
|
|
+ startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
|
|
|
+ } else {
|
|
|
+ startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
|
|
|
+ }
|
|
|
+
|
|
|
+ method := `index/data/list_page`
|
|
|
+
|
|
|
+ existDataMap := make(map[string]*data_manage.ComTradeData)
|
|
|
+ allData, err1 := data_manage.GetAllComTradeDataList(startDate)
|
|
|
+ if err1 != nil {
|
|
|
+ fmt.Println("get GetBaseFromEiaSteoIndexDataAll err:" + err1.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, dv := range allData {
|
|
|
+ tmpKey := dv.IndexTradeCode + "_" + dv.DataTime
|
|
|
+ existDataMap[tmpKey] = dv
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取所有指标信息 某一天的
|
|
|
+ maxPage := 1
|
|
|
+
|
|
|
+ for currPage := 0; currPage < maxPage; currPage++ {
|
|
|
+ data := make(map[string]interface{})
|
|
|
+ data["Source"] = utils.DATA_SOURCE_COM_TRADE
|
|
|
+ data["StartDate"] = startDate
|
|
|
+ data["CurrPage"] = currPage
|
|
|
+ data["PageSize"] = 500 //
|
|
|
+
|
|
|
+ var result string
|
|
|
+ result, err = HttpPost(method, data)
|
|
|
+ utils.FileLog.Info(result)
|
|
|
+ fmt.Println(result)
|
|
|
+
|
|
|
+ respObj := new(data_manage.ComTradeIndexDataResp)
|
|
|
+ err = json.Unmarshal([]byte(result), &respObj)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("json.Unmarshal err:" + err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if respObj.Ret != 200 {
|
|
|
+ err = errors.New(respObj.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 总页码数
|
|
|
+ maxPage = respObj.Data.Paging.Pages
|
|
|
+
|
|
|
+ addDataList := make([]*data_manage.ComTradeData, 0)
|
|
|
+ if respObj.Data.List != nil && len(respObj.Data.List) > 0 {
|
|
|
+ for _, dv := range respObj.Data.List {
|
|
|
+ tmpKey := dv.IndexTradeCode + "_" + dv.DataTime
|
|
|
+ if _, ok := existDataMap[tmpKey]; !ok {
|
|
|
+ addDataList = append(addDataList, dv)
|
|
|
+ existDataMap[tmpKey] = dv
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 最后如果还有数据未插入,那么继续插入吧
|
|
|
+ if len(addDataList) > 0 {
|
|
|
+ _, err = data_manage.MultiAddBaseFromComTradeData(addDataList)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("AddBaseFromComTradeData error:", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|