eia_steo.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_task/models/data_manage"
  5. "eta/eta_task/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // SyncEiaSteoIndex EIA STEO报告 指标
  10. func SyncEiaSteoIndex() (err error) {
  11. var startDate string
  12. maxDate, err := data_manage.GetBaseFromEiaSteoIndexMaxCreateDate()
  13. if err != nil || maxDate.IsZero() {
  14. startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  15. } else {
  16. startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
  17. }
  18. method := `index/list`
  19. data := make(map[string]interface{})
  20. data["Source"] = utils.DATA_SOURCE_EIA_STEO
  21. data["StartDate"] = startDate
  22. //data["EndDate"] = endDate
  23. result, err := HttpPost("SyncEiaSteoIndex", method, data)
  24. if err != nil {
  25. fmt.Println("HttpPost err:", err)
  26. utils.FileLog.Info("HttpPost err:", err)
  27. return
  28. }
  29. utils.FileLog.Info(result)
  30. fmt.Println(result)
  31. respObj := new(data_manage.EiaSteoIndexResp)
  32. err = json.Unmarshal([]byte(result), &respObj)
  33. if err != nil {
  34. fmt.Println("json.Unmarshal err:" + err.Error())
  35. return err
  36. }
  37. //获取所有指标信息 某一天的
  38. /*allIndex, err := data_manage.GetBaseFromEiaSteoIndexAll(startDate)
  39. if err != nil {
  40. fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error())
  41. return
  42. }
  43. existIndexMap := make(map[string]*data_manage.BaseFromEiaSteoIndex)
  44. for _, v := range allIndex {
  45. existIndexMap[v.IndexCode] = v
  46. }*/
  47. for _, zv := range respObj.Data {
  48. //if _, ok := existIndexMap[zv.IndexCode]; !ok {
  49. if zv.BaseFromEiaSteoIndexId <= 0 {
  50. continue
  51. }
  52. newID, err := data_manage.InsertOrUpdateBaseFromEiaSteoIndex(zv)
  53. if err != nil {
  54. fmt.Println("InsertOrUpdateBaseFromEiaSteoIndex error:", err)
  55. }
  56. fmt.Println("InsertOrUpdateBaseFromEiaSteoIndex new indexID:", newID)
  57. //}
  58. }
  59. return err
  60. }
  61. // SyncEiaSteoIndexData EIA STEO报告 指标
  62. func SyncEiaSteoIndexData() (err error) {
  63. startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
  64. method := `index/data/list`
  65. //获取所有指标信息 某一天的
  66. allIndex, err := data_manage.GetBaseFromEiaSteoIndexAll(startDate)
  67. if err != nil {
  68. fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error())
  69. return
  70. }
  71. for _, zv := range allIndex {
  72. data := make(map[string]interface{})
  73. data["Source"] = utils.DATA_SOURCE_EIA_STEO
  74. data["StartDate"] = startDate
  75. data["IndexCode"] = zv.IndexCode
  76. var result string
  77. result, err = HttpPost("SyncEiaSteoIndexData", method, data)
  78. if err != nil {
  79. fmt.Println("HttpPost err:", err)
  80. utils.FileLog.Info("HttpPost err:", err)
  81. return
  82. }
  83. utils.FileLog.Info(result)
  84. fmt.Println(result)
  85. respObj := new(data_manage.EiaSteoIndexDataResp)
  86. err = json.Unmarshal([]byte(result), &respObj)
  87. if err != nil {
  88. fmt.Println("json.Unmarshal err:" + err.Error())
  89. return err
  90. }
  91. existDataMap := make(map[int]*data_manage.BaseFromEiaSteoData)
  92. allData, err1 := data_manage.GetBaseFromEiaSteoIndexDataAll(startDate)
  93. if err1 != nil {
  94. fmt.Println("get GetBaseFromEiaSteoIndexDataAll err:" + err1.Error())
  95. return
  96. }
  97. for _, dv := range allData {
  98. existDataMap[dv.BaseFromEiaSteoDataId] = dv
  99. }
  100. for _, dv := range respObj.Data {
  101. if _, ok := existDataMap[dv.BaseFromEiaSteoDataId]; !ok {
  102. newID, err := data_manage.AddBaseFromEiaSteoData(dv)
  103. if err != nil {
  104. fmt.Println("insert error:", err)
  105. }
  106. fmt.Println("insert new indexID:", newID)
  107. }
  108. }
  109. }
  110. return err
  111. }
  112. // EIA STEO报告-分类
  113. func SyncEiaSteoClassify() (err error) {
  114. data := make(map[string]interface{})
  115. data["Source"] = utils.DATA_SOURCE_EIA_STEO
  116. method := `classify/list`
  117. result, err := HttpPost("SyncEiaSteoClassify", method, data)
  118. if err != nil {
  119. fmt.Println("SyncEiaSteoClassify HttpPost Err:", err.Error())
  120. return err
  121. }
  122. utils.FileLog.Info("SyncEiaSteoClassify:" + result)
  123. fmt.Println("SyncEiaSteoClassify result:", result)
  124. respObj := new(data_manage.EiaSteoClassifyDataResp)
  125. err = json.Unmarshal([]byte(result), &respObj)
  126. if err != nil {
  127. return err
  128. }
  129. //获取所有指标信息 某一天的
  130. allClassify, err := data_manage.GetBaseFromEiaSteoClassifyAll()
  131. if err != nil {
  132. return
  133. }
  134. existClassifyMap := make(map[int]*data_manage.BaseFromEiaSteoClassify)
  135. for _, v := range allClassify {
  136. existClassifyMap[v.BaseFromEiaSteoClassifyId] = v
  137. }
  138. for _, item := range respObj.Data {
  139. if oldClassify, ok := existClassifyMap[item.BaseFromEiaSteoClassifyId]; !ok {
  140. newID, err := data_manage.AddBaseEiaSteoClassify(item)
  141. if err != nil {
  142. fmt.Println("InsertOrUpdateBaseEiaSteoClassify error:", err)
  143. }
  144. fmt.Println("InsertOrUpdateBaseEiaSteoClassify new indexID:", newID)
  145. } else {
  146. var upDateCols []string
  147. if oldClassify.ClassifyName != item.ClassifyName {
  148. oldClassify.ClassifyName = item.ClassifyName
  149. upDateCols = append(upDateCols, "ClassifyName")
  150. }
  151. if oldClassify.ClassifyNameOriginal != item.ClassifyNameOriginal {
  152. oldClassify.ClassifyNameOriginal = item.ClassifyNameOriginal
  153. upDateCols = append(upDateCols, "ClassifyNameOriginal")
  154. }
  155. if oldClassify.ParentId != item.ParentId {
  156. oldClassify.ParentId = item.ParentId
  157. upDateCols = append(upDateCols, "ParentId")
  158. }
  159. if oldClassify.Level != item.Level {
  160. oldClassify.Level = item.Level
  161. upDateCols = append(upDateCols, "Level")
  162. }
  163. if len(upDateCols) > 0 {
  164. oldClassify.ModifyTime = time.Now()
  165. upDateCols = append(upDateCols, "ModifyTime")
  166. err := oldClassify.Update(upDateCols)
  167. if err != nil {
  168. fmt.Println("UpdateBaseFromEiaSteoClassify error:", err)
  169. }
  170. }
  171. delete(existClassifyMap, oldClassify.BaseFromEiaSteoClassifyId)
  172. }
  173. }
  174. var deleteId []int
  175. for k, _ := range existClassifyMap {
  176. deleteId = append(deleteId, k)
  177. }
  178. err = data_manage.BatchDeleteBaseFromEiaSteoClassify(deleteId)
  179. if err != nil {
  180. fmt.Println("BatchDeleteBaseFromEiaSteoClassify error:", err)
  181. }
  182. return err
  183. }
  184. func SyncEiaSteoIndexDataV2() (err error) {
  185. startDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  186. method := `index/data/list`
  187. //获取所有指标信息 某一天的
  188. allIndexCode, err := data_manage.GetBaseFromEiaSteoIndexCodeListByDate(startDate)
  189. if err != nil {
  190. fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error())
  191. utils.FileLog.Info("get GetBaseFromEiaSteoIndexCodeList err:", err)
  192. return
  193. }
  194. for _, indexCode := range allIndexCode {
  195. data := make(map[string]interface{})
  196. data["Source"] = utils.DATA_SOURCE_EIA_STEO
  197. data["StartDate"] = startDate
  198. data["IndexCode"] = indexCode
  199. var result string
  200. result, err = HttpPost("SyncEiaSteoIndexData", method, data)
  201. if err != nil {
  202. fmt.Println("HttpPost err:", err)
  203. utils.FileLog.Info("HttpPost err:", err)
  204. return
  205. }
  206. utils.FileLog.Info(result)
  207. fmt.Println(result)
  208. respObj := new(data_manage.EiaSteoIndexDataResp)
  209. err = json.Unmarshal([]byte(result), &respObj)
  210. if err != nil {
  211. fmt.Println("json.Unmarshal err:" + err.Error())
  212. return err
  213. }
  214. existDataMap := make(map[int]*data_manage.BaseFromEiaSteoData)
  215. allData, err1 := data_manage.GetBaseFromEiaSteoIndexDataByIndexCode(indexCode, startDate)
  216. if err1 != nil {
  217. utils.FileLog.Error("get GetBaseFromEiaSteoIndexDataByDate err:", err1.Error())
  218. return
  219. }
  220. for _, dv := range allData {
  221. existDataMap[dv.BaseFromEiaSteoDataId] = dv
  222. }
  223. for _, dv := range respObj.Data {
  224. if v, ok := existDataMap[dv.BaseFromEiaSteoDataId]; !ok {
  225. newID, err := data_manage.AddBaseFromEiaSteoData(dv)
  226. if err != nil {
  227. utils.FileLog.Error("add error:", err)
  228. }
  229. fmt.Println("insert new indexID:", newID)
  230. } else {
  231. upDateCols := make([]string, 0)
  232. if !v.DataTime.Equal(dv.DataTime) {
  233. v.DataTime = dv.DataTime
  234. upDateCols = append(upDateCols, "data_time")
  235. }
  236. if v.Value != dv.Value {
  237. v.Value = dv.Value
  238. v.ModifyTime = dv.ModifyTime
  239. upDateCols = append(upDateCols, []string{"value", "modify_time"}...)
  240. }
  241. if len(upDateCols) > 0 {
  242. err = v.Update(upDateCols)
  243. if err != nil {
  244. utils.FileLog.Error("update error:", err)
  245. }
  246. }
  247. }
  248. }
  249. }
  250. return err
  251. }