package services import ( "encoding/json" "errors" "eta/eta_task/models/data_manage" "eta/eta_task/utils" "fmt" "time" ) // 广州商品交易所-分类 func SyncFromGuangzhouClassify() (err error) { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_GFEX method := `classify/list` result, err := HttpPost("SyncFromGuangzhouClassify", method, data) if err != nil { fmt.Println("SyncFromGuangzhouClassify HttpPost Err:", err.Error()) return err } utils.FileLog.Info("SyncFromGuangzhouClassify:" + result) fmt.Println("SyncFromGuangzhouClassify result:", result) respObj := new(data_manage.GuangzhouClassifyResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { return err } //获取所有指标信息 某一天的 allClassify, err := data_manage.GetBaseFromTradeGuangzhouClassifyAll() if err != nil { return } existIndexMap := make(map[int]*data_manage.BaseFromTradeGuangzhouClassify) for _, v := range allClassify { existIndexMap[v.BaseFromTradeGuangzhouClassifyId] = v } for _, item := range respObj.Data { if _, ok := existIndexMap[item.BaseFromTradeGuangzhouClassifyId]; !ok { newID, err := data_manage.AddBaseFromTradeGuangzhouClassify(item) if err != nil { fmt.Println("AddBaseFromTradeGuangzhouClassify error:", err) } fmt.Println("AddBaseFromTradeGuangzhouClassify new indexID:", newID) } } return err } // 广州商品交易所-指标信息 func SyncFromGuangzhouIndex() (err error) { method := `index/list` data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_GFEX result, err := HttpPost("SyncFromGuangzhou", method, data) if err != nil { fmt.Println("SyncFromGuangzhou HttpPost Err:", err.Error()) } utils.FileLog.Info(result) fmt.Println("SyncFromGuangzhouIndex result:", result) respObj := new(data_manage.GuangzhouIndexResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { return err } //获取所有指标信息 某一天的 allIndex, err := data_manage.GetBaseFromTradeGuangzhouIndexAll() if err != nil { return } existIndexMap := make(map[int]*data_manage.BaseFromTradeGuangzhouIndex) for _, v := range allIndex { existIndexMap[v.BaseFromTradeGuangzhouIndexId] = v } for _, item := range respObj.Data { //if _, ok := existIndexMap[item.BaseFromTradeGuangzhouIndexId]; !ok { newID, err := data_manage.AddBaseFromTradeGuangzhouIndex(item) if err != nil { fmt.Println("insert error:", err) } fmt.Println("insert new indexID:", newID) //} } return err } // 广州商品交易所-合约 func SyncFromGuangzhouContract() (err error) { data := make(map[string]interface{}) data["Source"] = utils.DATA_SOURCE_GFEX method := `contract/list` result, err := HttpPost("SyncFromGuangzhouContract", method, data) if err != nil { fmt.Println("SyncFromGuangzhouContract HttpPost Err:", err.Error()) } utils.FileLog.Info(result) fmt.Println("SyncFromGuangzhouContract result:", result) respObj := new(data_manage.GuangzhouContractResp) err = json.Unmarshal([]byte(result), &respObj) if err != nil { return err } //获取所有指标信息 某一天的 allClassify, err := data_manage.GetBaseFromTradeGuangzhouContractAll() if err != nil { return } existContractMap := make(map[int]*data_manage.BaseFromTradeGuangzhouContract) for _, v := range allClassify { existContractMap[v.BaseFromTradeGuangzhouContractId] = v } obj := new(data_manage.BaseFromTradeGuangzhouIndex) for _, item := range respObj.Data { if _, ok := existContractMap[item.BaseFromTradeGuangzhouContractId]; !ok { err = obj.AddBaseFromTradeGuangzhouContract(item) if err != nil { fmt.Println("AddBaseFromTradeGuangzhouContract error:", err) } fmt.Println("AddBaseFromTradeGuangzhouContract new indexID:") } } return err } // SyncComTradeData 同步UN指标数据 func SyncFromGuangzhouTradeData() (err error) { startDate := time.Now().Format(utils.FormatDate) + " 00:00:00" //var startDate string maxDate, err := data_manage.GetBaseFromTradeGuangzhouMaxDate() if err != nil || maxDate.IsZero() { startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00" } else { startDate = maxDate.Format(utils.FormatDate) + " 00:00:00" } method := `index/data/list_page` existDataMap := make(map[string]*data_manage.BaseFromTradeGuangzhouData) allData, err1 := data_manage.GetAllBaseFromTradeGuangzhouDataList(startDate) if err1 != nil { fmt.Println("get GetAllBaseFromTradeGuangzhouDataList 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_GFEX data["StartDate"] = startDate data["CurrPage"] = currPage data["PageSize"] = 500 // var result string result, err = HttpPost("SyncFromGuangzhouTradeData", method, data) utils.FileLog.Info(result) fmt.Println(result) respObj := new(data_manage.BaseFromTradeGuangzhouDataResp) 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.BaseFromTradeGuangzhouData, 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.MultiAddBaseFromTradeGuangzhouData(addDataList) if err != nil { fmt.Println("MultiAddBaseFromTradeGuangzhouData error:", err) } } } return err }