package services import ( "encoding/json" "errors" "eta/eta_task/models/data_manage" "eta/eta_task/utils" "fmt" "time" ) // UsdaFasGPR地缘风险指数指标数据同步 func SyncBaseFromGprRisk() (err error) { var startDate string maxDate, err := data_manage.GetBaseFromGprRiskIndexMaxDate() if err != nil || maxDate.IsZero() { startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00" } else { startDate = maxDate.Format(utils.FormatDateTime) } method := `index/list` data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_GPR_RISK data["StartDate"] = startDate //data["EndDate"] = endDate result, err := HttpPost("SyncRankingFromGprRisk", method, data) utils.FileLog.Info(result) fmt.Println(result) respObj := new(data_manage.GprRiskIndexResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { utils.FileLog.Info("err:", err.Error()) fmt.Println("err:", err.Error()) return err } GprRiskObj := new(data_manage.BaseFromGprRiskIndex) for _, zv := range respObj.Data { newID, err := GprRiskObj.InsertOrUpdateBaseFromGprRiskIndex(zv) if err != nil { fmt.Println("InsertOrUpdateBaseFromGprRiskIndex error:", err) } fmt.Println("InsertOrUpdateBaseFromGprRiskIndex new indexID:", newID) } return err } // GprRiskGPR地缘风险指数-分类 func SyncBaseFromGprRiskClassify() (err error) { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_USDA_FAS method := `classify/list` result, err := HttpPost("SyncBaseFromGprRiskClassify", method, data) if err != nil { fmt.Println("SyncBaseFromGprRiskClassify HttpPost Err:", err.Error()) } utils.FileLog.Info(result) fmt.Println("SyncBaseFromGprRiskClassify result:", result) respObj := new(data_manage.GprRiskClassifyResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { return err } //获取所有分类 /* allClassify, err := data_manage.GetBaseFromGprRiskClassifyAll() if err != nil { return } existIndexMap := make(map[int]*data_manage.BaseFromGprRiskClassify) for _, v := range allClassify { existIndexMap[v.BaseFromGprRiskClassifyId] = v }*/ for _, item := range respObj.Data { //if _, ok := existIndexMap[item.BaseFromGprRiskClassifyId]; !ok { newID, err := data_manage.InsertOrUpdateBaseFromGprRiskClassify(item) if err != nil { fmt.Println("InsertOrUpdateBaseFromGprRiskClassify error:", err) } fmt.Println("InsertOrUpdateBaseFromGprRiskClassify new indexID:", newID) //} } return err } // GprRiskGPR地缘风险指数-数据 func SyncBaseFromGprRiskData() (err error) { startDate := time.Now().Format(utils.FormatDate) + " 00:00:00" //var startDate string maxDate, err := data_manage.GetBaseFromGprRiskMaxDate() if err != nil || maxDate.IsZero() { startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00" } else { startDate = maxDate.Format(utils.FormatDateTime) } method := `index/data/list_page` existDataMap := make(map[string]*data_manage.BaseFromGprRiskData) allData, err1 := data_manage.GetAllBaseFromGprRiskDataList(startDate) if err1 != nil { fmt.Println("get GetAllBaseFromGprRiskDataList err:" + err1.Error()) return } for _, dv := range allData { tmpKey := dv.IndexCode + "_" + dv.DataTime existDataMap[tmpKey] = dv } //获取所有指标信息 某一天的 maxPage := 1 for currPage := 0; currPage < maxPage; currPage++ { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_GPR_RISK data["StartDate"] = startDate data["CurrPage"] = currPage data["PageSize"] = 500 // var result string result, err = HttpPost("SyncBaseFromGprRiskData", method, data) utils.FileLog.Info(result) fmt.Println(result) respObj := new(data_manage.BaseFromGprRiskDataResp) 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.BaseFromGprRiskData, 0) if respObj.Data.List != nil && len(respObj.Data.List) > 0 { for _, dv := range respObj.Data.List { tmpKey := dv.IndexCode + "_" + dv.DataTime if _, ok := existDataMap[tmpKey]; !ok { addDataList = append(addDataList, dv) existDataMap[tmpKey] = dv } } } // 最后如果还有数据未插入,那么继续插入吧 if len(addDataList) > 0 { _, err = data_manage.MultiAddBaseFromGprRiskDataIndex(addDataList) if err != nil { fmt.Println("MultiAddBaseFromGprRiskDataIndex error:", err) } } } return err }