package services import ( "encoding/json" "eta/eta_task/models/data_manage" "eta/eta_task/utils" "fmt" "time" ) // SyncEiaSteoIndex EIA STEO报告 指标 func SyncEiaSteoIndex() (err error) { var startDate string maxDate, err := data_manage.GetBaseFromEiaSteoIndexMaxCreateDate() 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_EIA_STEO data["StartDate"] = startDate //data["EndDate"] = endDate result, err := HttpPost("SyncEiaSteoIndex", method, data) if err != nil { fmt.Println("HttpPost err:", err) utils.FileLog.Info("HttpPost err:", err) return } utils.FileLog.Info(result) fmt.Println(result) respObj := new(data_manage.EiaSteoIndexResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { fmt.Println("json.Unmarshal err:" + err.Error()) return err } //获取所有指标信息 某一天的 /*allIndex, err := data_manage.GetBaseFromEiaSteoIndexAll(startDate) if err != nil { fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error()) return } existIndexMap := make(map[string]*data_manage.BaseFromEiaSteoIndex) for _, v := range allIndex { existIndexMap[v.IndexCode] = v }*/ for _, zv := range respObj.Data { //if _, ok := existIndexMap[zv.IndexCode]; !ok { if zv.BaseFromEiaSteoIndexId <= 0 { continue } newID, err := data_manage.InsertOrUpdateBaseFromEiaSteoIndex(zv) if err != nil { fmt.Println("InsertOrUpdateBaseFromEiaSteoIndex error:", err) } fmt.Println("InsertOrUpdateBaseFromEiaSteoIndex new indexID:", newID) //} } return err } // SyncEiaSteoIndexData EIA STEO报告 指标 func SyncEiaSteoIndexData() (err error) { startDate := time.Now().Format(utils.FormatDate) + " 00:00:00" method := `index/data/list` //获取所有指标信息 某一天的 allIndex, err := data_manage.GetBaseFromEiaSteoIndexAll(startDate) if err != nil { fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error()) return } for _, zv := range allIndex { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_EIA_STEO data["StartDate"] = startDate data["IndexCode"] = zv.IndexCode var result string result, err = HttpPost("SyncEiaSteoIndexData", method, data) if err != nil { fmt.Println("HttpPost err:", err) utils.FileLog.Info("HttpPost err:", err) return } utils.FileLog.Info(result) fmt.Println(result) respObj := new(data_manage.EiaSteoIndexDataResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { fmt.Println("json.Unmarshal err:" + err.Error()) return err } existDataMap := make(map[int]*data_manage.BaseFromEiaSteoData) allData, err1 := data_manage.GetBaseFromEiaSteoIndexDataAll(startDate) if err1 != nil { fmt.Println("get GetBaseFromEiaSteoIndexDataAll err:" + err1.Error()) return } for _, dv := range allData { existDataMap[dv.BaseFromEiaSteoDataId] = dv } for _, dv := range respObj.Data { if _, ok := existDataMap[dv.BaseFromEiaSteoDataId]; !ok { newID, err := data_manage.AddBaseFromEiaSteoData(dv) if err != nil { fmt.Println("insert error:", err) } fmt.Println("insert new indexID:", newID) } } } return err } // EIA STEO报告-分类 func SyncEiaSteoClassify() (err error) { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_EIA_STEO method := `classify/list` result, err := HttpPost("SyncEiaSteoClassify", method, data) if err != nil { fmt.Println("SyncEiaSteoClassify HttpPost Err:", err.Error()) return err } utils.FileLog.Info("SyncEiaSteoClassify:" + result) fmt.Println("SyncEiaSteoClassify result:", result) respObj := new(data_manage.EiaSteoClassifyDataResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { return err } //获取所有指标信息 某一天的 allClassify, err := data_manage.GetBaseFromEiaSteoClassifyAll() if err != nil { return } existClassifyMap := make(map[int]*data_manage.BaseFromEiaSteoClassify) for _, v := range allClassify { existClassifyMap[v.BaseFromEiaSteoClassifyId] = v } for _, item := range respObj.Data { if oldClassify, ok := existClassifyMap[item.BaseFromEiaSteoClassifyId]; !ok { newID, err := data_manage.AddBaseEiaSteoClassify(item) if err != nil { fmt.Println("InsertOrUpdateBaseEiaSteoClassify error:", err) } fmt.Println("InsertOrUpdateBaseEiaSteoClassify new indexID:", newID) } else { var upDateCols []string if oldClassify.ClassifyName != item.ClassifyName { oldClassify.ClassifyName = item.ClassifyName upDateCols = append(upDateCols, "ClassifyName") } if oldClassify.ClassifyNameOriginal != item.ClassifyNameOriginal { oldClassify.ClassifyNameOriginal = item.ClassifyNameOriginal upDateCols = append(upDateCols, "ClassifyNameOriginal") } if oldClassify.ParentId != item.ParentId { oldClassify.ParentId = item.ParentId upDateCols = append(upDateCols, "ParentId") } if oldClassify.Level != item.Level { oldClassify.Level = item.Level upDateCols = append(upDateCols, "Level") } if len(upDateCols) > 0 { oldClassify.ModifyTime = time.Now() upDateCols = append(upDateCols, "ModifyTime") err := oldClassify.Update(upDateCols) if err != nil { fmt.Println("UpdateBaseFromEiaSteoClassify error:", err) } } delete(existClassifyMap, oldClassify.BaseFromEiaSteoClassifyId) } } var deleteId []int for k, _ := range existClassifyMap { deleteId = append(deleteId, k) } err = data_manage.BatchDeleteBaseFromEiaSteoClassify(deleteId) if err != nil { fmt.Println("BatchDeleteBaseFromEiaSteoClassify error:", err) } return err } func SyncEiaSteoIndexDataV2() (err error) { startDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00" method := `index/data/list` //获取所有指标信息 某一天的 allIndexCode, err := data_manage.GetBaseFromEiaSteoIndexCodeListByDate(startDate) if err != nil { fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error()) utils.FileLog.Info("get GetBaseFromEiaSteoIndexCodeList err:", err) return } for _, indexCode := range allIndexCode { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_EIA_STEO data["StartDate"] = startDate data["IndexCode"] = indexCode var result string result, err = HttpPost("SyncEiaSteoIndexData", method, data) if err != nil { fmt.Println("HttpPost err:", err) utils.FileLog.Info("HttpPost err:", err) return } utils.FileLog.Info(result) fmt.Println(result) respObj := new(data_manage.EiaSteoIndexDataResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { fmt.Println("json.Unmarshal err:" + err.Error()) return err } existDataMap := make(map[int]*data_manage.BaseFromEiaSteoData) allData, err1 := data_manage.GetBaseFromEiaSteoIndexDataByIndexCode(indexCode, startDate) if err1 != nil { utils.FileLog.Error("get GetBaseFromEiaSteoIndexDataByDate err:", err1.Error()) return } for _, dv := range allData { existDataMap[dv.BaseFromEiaSteoDataId] = dv } for _, dv := range respObj.Data { if v, ok := existDataMap[dv.BaseFromEiaSteoDataId]; !ok { newID, err := data_manage.AddBaseFromEiaSteoData(dv) if err != nil { utils.FileLog.Error("add error:", err) } fmt.Println("insert new indexID:", newID) } else { upDateCols := make([]string, 0) if !v.DataTime.Equal(dv.DataTime) { v.DataTime = dv.DataTime upDateCols = append(upDateCols, "data_time") } if v.Value != dv.Value { v.Value = dv.Value v.ModifyTime = dv.ModifyTime upDateCols = append(upDateCols, []string{"value", "modify_time"}...) } if len(upDateCols) > 0 { err = v.Update(upDateCols) if err != nil { utils.FileLog.Error("update error:", err) } } } } } return err }