trade_guangzhou.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_task/models/data_manage"
  6. "eta/eta_task/utils"
  7. "fmt"
  8. "time"
  9. )
  10. // 广州商品交易所-分类
  11. func SyncFromGuangzhouClassify() (err error) {
  12. data := make(map[string]interface{})
  13. data["Source"] = utils.DATA_SOURCE_GFEX
  14. method := `classify/list`
  15. result, err := HttpPost("SyncFromGuangzhouClassify", method, data)
  16. if err != nil {
  17. fmt.Println("SyncFromGuangzhouClassify HttpPost Err:", err.Error())
  18. return err
  19. }
  20. utils.FileLog.Info("SyncFromGuangzhouClassify:" + result)
  21. fmt.Println("SyncFromGuangzhouClassify result:", result)
  22. respObj := new(data_manage.GuangzhouClassifyResp)
  23. err = json.Unmarshal([]byte(result), &respObj)
  24. if err != nil {
  25. return err
  26. }
  27. //获取所有指标信息 某一天的
  28. allClassify, err := data_manage.GetBaseFromTradeGuangzhouClassifyAll()
  29. if err != nil {
  30. return
  31. }
  32. existIndexMap := make(map[int]*data_manage.BaseFromTradeGuangzhouClassify)
  33. for _, v := range allClassify {
  34. existIndexMap[v.BaseFromTradeGuangzhouClassifyId] = v
  35. }
  36. for _, item := range respObj.Data {
  37. if _, ok := existIndexMap[item.BaseFromTradeGuangzhouClassifyId]; !ok {
  38. newID, err := data_manage.AddBaseFromTradeGuangzhouClassify(item)
  39. if err != nil {
  40. fmt.Println("AddBaseFromTradeGuangzhouClassify error:", err)
  41. }
  42. fmt.Println("AddBaseFromTradeGuangzhouClassify new indexID:", newID)
  43. }
  44. }
  45. return err
  46. }
  47. // 广州商品交易所-指标信息
  48. func SyncFromGuangzhouIndex() (err error) {
  49. method := `index/list`
  50. data := make(map[string]interface{})
  51. data["Source"] = utils.DATA_SOURCE_GFEX
  52. result, err := HttpPost("SyncFromGuangzhou", method, data)
  53. if err != nil {
  54. fmt.Println("SyncFromGuangzhou HttpPost Err:", err.Error())
  55. }
  56. utils.FileLog.Info(result)
  57. fmt.Println("SyncFromGuangzhouIndex result:", result)
  58. respObj := new(data_manage.GuangzhouIndexResp)
  59. err = json.Unmarshal([]byte(result), &respObj)
  60. if err != nil {
  61. return err
  62. }
  63. //获取所有指标信息 某一天的
  64. allIndex, err := data_manage.GetBaseFromTradeGuangzhouIndexAll()
  65. if err != nil {
  66. return
  67. }
  68. existIndexMap := make(map[int]*data_manage.BaseFromTradeGuangzhouIndex)
  69. for _, v := range allIndex {
  70. existIndexMap[v.BaseFromTradeGuangzhouIndexId] = v
  71. }
  72. for _, item := range respObj.Data {
  73. //if _, ok := existIndexMap[item.BaseFromTradeGuangzhouIndexId]; !ok {
  74. newID, err := data_manage.AddBaseFromTradeGuangzhouIndex(item)
  75. if err != nil {
  76. fmt.Println("insert error:", err)
  77. }
  78. fmt.Println("insert new indexID:", newID)
  79. //}
  80. }
  81. return err
  82. }
  83. // 广州商品交易所-合约
  84. func SyncFromGuangzhouContract() (err error) {
  85. data := make(map[string]interface{})
  86. data["Source"] = utils.DATA_SOURCE_GFEX
  87. method := `contract/list`
  88. result, err := HttpPost("SyncFromGuangzhouContract", method, data)
  89. if err != nil {
  90. fmt.Println("SyncFromGuangzhouContract HttpPost Err:", err.Error())
  91. }
  92. utils.FileLog.Info(result)
  93. fmt.Println("SyncFromGuangzhouContract result:", result)
  94. respObj := new(data_manage.GuangzhouContractResp)
  95. err = json.Unmarshal([]byte(result), &respObj)
  96. if err != nil {
  97. return err
  98. }
  99. //获取所有指标信息 某一天的
  100. allClassify, err := data_manage.GetBaseFromTradeGuangzhouContractAll()
  101. if err != nil {
  102. return
  103. }
  104. existContractMap := make(map[int]*data_manage.BaseFromTradeGuangzhouContract)
  105. for _, v := range allClassify {
  106. existContractMap[v.BaseFromTradeGuangzhouContractId] = v
  107. }
  108. obj := new(data_manage.BaseFromTradeGuangzhouIndex)
  109. for _, item := range respObj.Data {
  110. if _, ok := existContractMap[item.BaseFromTradeGuangzhouContractId]; !ok {
  111. err = obj.AddBaseFromTradeGuangzhouContract(item)
  112. if err != nil {
  113. fmt.Println("AddBaseFromTradeGuangzhouContract error:", err)
  114. }
  115. fmt.Println("AddBaseFromTradeGuangzhouContract new indexID:")
  116. }
  117. }
  118. return err
  119. }
  120. // SyncComTradeData 同步UN指标数据
  121. func SyncFromGuangzhouTradeData() (err error) {
  122. startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
  123. //var startDate string
  124. maxDate, err := data_manage.GetBaseFromTradeGuangzhouMaxDate()
  125. if err != nil || maxDate.IsZero() {
  126. startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  127. } else {
  128. startDate = maxDate.Format(utils.FormatDate) + " 00:00:00"
  129. }
  130. method := `index/data/list_page`
  131. existDataMap := make(map[string]*data_manage.BaseFromTradeGuangzhouData)
  132. allData, err1 := data_manage.GetAllBaseFromTradeGuangzhouDataList(startDate)
  133. if err1 != nil {
  134. fmt.Println("get GetAllBaseFromTradeGuangzhouDataList err:" + err1.Error())
  135. return
  136. }
  137. for _, dv := range allData {
  138. tmpKey := dv.IndexCode + "_" + dv.DataTime
  139. existDataMap[tmpKey] = dv
  140. }
  141. //获取所有指标信息 某一天的
  142. maxPage := 1
  143. for currPage := 0; currPage < maxPage; currPage++ {
  144. data := make(map[string]interface{})
  145. data["Source"] = utils.DATA_SOURCE_GFEX
  146. data["StartDate"] = startDate
  147. data["CurrPage"] = currPage
  148. data["PageSize"] = 500 //
  149. var result string
  150. result, err = HttpPost("SyncFromGuangzhouTradeData", method, data)
  151. utils.FileLog.Info(result)
  152. fmt.Println(result)
  153. respObj := new(data_manage.BaseFromTradeGuangzhouDataResp)
  154. err = json.Unmarshal([]byte(result), &respObj)
  155. if err != nil {
  156. fmt.Println("json.Unmarshal err:" + err.Error())
  157. return err
  158. }
  159. if respObj.Ret != 200 {
  160. err = errors.New(respObj.ErrMsg)
  161. return
  162. }
  163. // 总页码数
  164. maxPage = respObj.Data.Paging.Pages
  165. addDataList := make([]*data_manage.BaseFromTradeGuangzhouData, 0)
  166. if respObj.Data.List != nil && len(respObj.Data.List) > 0 {
  167. for _, dv := range respObj.Data.List {
  168. tmpKey := dv.IndexCode + "_" + dv.DataTime
  169. if _, ok := existDataMap[tmpKey]; !ok {
  170. addDataList = append(addDataList, dv)
  171. existDataMap[tmpKey] = dv
  172. }
  173. }
  174. }
  175. // 最后如果还有数据未插入,那么继续插入吧
  176. if len(addDataList) > 0 {
  177. _, err = data_manage.MultiAddBaseFromTradeGuangzhouData(addDataList)
  178. if err != nil {
  179. fmt.Println("MultiAddBaseFromTradeGuangzhouData error:", err)
  180. }
  181. }
  182. }
  183. return err
  184. }