edb_data_base.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // GetEdbDataTableName 指标数据->存储表
  9. func GetEdbDataTableName(source, subSource int) (tableName string) {
  10. switch source {
  11. case utils.DATA_SOURCE_THS:
  12. tableName = "edb_data_ths"
  13. if subSource == utils.DATA_SUB_SOURCE_DATE {
  14. tableName = "edb_data_ths_ds"
  15. } else {
  16. tableName = "edb_data_ths"
  17. }
  18. case utils.DATA_SOURCE_WIND:
  19. if subSource == utils.DATA_SUB_SOURCE_DATE {
  20. tableName = "edb_data_wind_wsd"
  21. } else {
  22. tableName = "edb_data_wind"
  23. }
  24. case utils.DATA_SOURCE_PB, utils.DATA_SOURCE_PB_FINANCE: //彭博经济数据、彭博财务数据
  25. tableName = "edb_data_pb"
  26. case utils.DATA_SOURCE_CALCULATE:
  27. tableName = "edb_data_calculate"
  28. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  29. tableName = "edb_data_calculate_ljzzy"
  30. case utils.DATA_SOURCE_CALCULATE_TBZ:
  31. tableName = "edb_data_calculate_tbz"
  32. case utils.DATA_SOURCE_CALCULATE_TCZ:
  33. tableName = "edb_data_calculate_tcz"
  34. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  35. tableName = "edb_data_calculate_nszydpjjs"
  36. case utils.DATA_SOURCE_MANUAL:
  37. tableName = "edb_data_manual"
  38. case utils.DATA_SOURCE_LZ:
  39. tableName = "edb_data_lz"
  40. case utils.DATA_SOURCE_YS:
  41. tableName = "edb_data_ys"
  42. case utils.DATA_SOURCE_CALCULATE_HBZ:
  43. tableName = "edb_data_calculate_hbz"
  44. case utils.DATA_SOURCE_CALCULATE_HCZ:
  45. tableName = "edb_data_calculate_hcz"
  46. case utils.DATA_SOURCE_CALCULATE_BP:
  47. tableName = "edb_data_calculate_bp"
  48. case utils.DATA_SOURCE_GL:
  49. tableName = "edb_data_gl"
  50. case utils.DATA_SOURCE_ZZ:
  51. tableName = "edb_data_zz"
  52. case utils.DATA_SOURCE_DL:
  53. tableName = "edb_data_dl"
  54. case utils.DATA_SOURCE_SH:
  55. tableName = "edb_data_sh"
  56. case utils.DATA_SOURCE_CFFEX:
  57. tableName = "edb_data_cffex"
  58. case utils.DATA_SOURCE_SHFE:
  59. tableName = "edb_data_ine"
  60. case utils.DATA_SOURCE_GIE:
  61. tableName = "edb_data_gie"
  62. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  63. tableName = "edb_data_calculate_zjpj"
  64. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  65. tableName = "edb_data_calculate_time_shift"
  66. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
  67. tableName = "edb_data_calculate_ljztbpj"
  68. case utils.DATA_SOURCE_LT:
  69. tableName = "edb_data_lt"
  70. case utils.DATA_SOURCE_COAL:
  71. tableName = "edb_data_coal"
  72. case utils.DATA_SOURCE_PYTHON:
  73. tableName = "edb_data_python"
  74. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  75. tableName = "edb_data_google_travel"
  76. case utils.DATA_SOURCE_PREDICT_CALCULATE:
  77. tableName = "edb_data_predict_calculate"
  78. case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  79. tableName = "edb_data_predict_calculate_tbz"
  80. case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  81. tableName = "edb_data_predict_calculate_tcz"
  82. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  83. tableName = "edb_data_mysteel_chemical"
  84. case utils.DATA_SOURCE_CALCULATE_CJJX:
  85. tableName = "edb_data_calculate_cjjx"
  86. case utils.DATA_SOURCE_EIA_STEO:
  87. tableName = "edb_data_eia_steo"
  88. case utils.DATA_SOURCE_CALCULATE_NHCC:
  89. tableName = "edb_data_calculate_nhcc"
  90. case utils.DATA_SOURCE_COM_TRADE:
  91. tableName = "edb_data_com_trade"
  92. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  93. tableName = "edb_data_predict_calculate_nszydpjjs"
  94. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  95. tableName = "edb_data_calculate_adjust"
  96. case utils.DATA_SOURCE_SCI:
  97. tableName = "edb_data_sci"
  98. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  99. tableName = "edb_data_predict_calculate_ljzzy"
  100. case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  101. tableName = "edb_data_predict_calculate_time_shift"
  102. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ:
  103. tableName = "edb_data_predict_calculate_zjpj"
  104. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ:
  105. tableName = "edb_data_predict_calculate_ljztbpj"
  106. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC:
  107. tableName = "edb_data_predict_calculate_nhcc"
  108. case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  109. tableName = "edb_data_predict_calculate_cjjx"
  110. case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  111. tableName = "edb_data_predict_calculate_hbz"
  112. case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  113. tableName = "edb_data_predict_calculate_hcz"
  114. case utils.DATA_SOURCE_PREDICT_CALCULATE_BP:
  115. tableName = "edb_data_predict_calculate_bp"
  116. case utils.DATA_SOURCE_CALCULATE_JP:
  117. tableName = "edb_data_calculate_jp"
  118. case utils.DATA_SOURCE_CALCULATE_NH:
  119. tableName = "edb_data_calculate_nh"
  120. case utils.DATA_SOURCE_CALCULATE_KSZS:
  121. tableName = "edb_data_calculate_kszs"
  122. case utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  123. tableName = "edb_data_predict_calculate_jp"
  124. case utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  125. tableName = "edb_data_predict_calculate_nh"
  126. case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS:
  127. tableName = "edb_data_predict_calculate_kszs"
  128. case utils.DATA_SOURCE_BAIINFO:
  129. tableName = "edb_data_baiinfo"
  130. case utils.DATA_SOURCE_STOCK_PLANT:
  131. tableName = "edb_data_stock_plant"
  132. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  133. tableName = "edb_data_calculate_correlation"
  134. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  135. tableName = "edb_data_national_statistics"
  136. case utils.DATA_SOURCE_CALCULATE_LJZZJ: //累计值转季 -> 61
  137. tableName = "edb_data_calculate_ljzzj"
  138. case utils.DATA_SOURCE_CALCULATE_LJZ: //累计值 -> 62
  139. tableName = "edb_data_calculate_ljz"
  140. case utils.DATA_SOURCE_CALCULATE_LJZNCZJ: //累计值(年初至今) -> 63
  141. tableName = "edb_data_calculate_ljznczj"
  142. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZJ: // 预测指标 - 累计值 -> 65
  143. tableName = "edb_data_predict_calculate_ljzzj"
  144. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZ: //预测指标 - 累计值转季->64
  145. tableName = "edb_data_predict_calculate_ljz"
  146. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZNCZJ: //预测指标 - 累计值(年初至今) -> 66
  147. tableName = "edb_data_predict_calculate_ljznczj"
  148. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION: //标准差->67
  149. tableName = "edb_data_calculate_standard_deviation"
  150. case utils.DATA_SOURCE_CALCULATE_PERCENTILE: //百分位->68
  151. tableName = "edb_data_calculate_percentile"
  152. case utils.DATA_SOURCE_PREDICT_CALCULATE_STANDARD_DEVIATION: //预测标准差->69
  153. tableName = "edb_data_predict_ccalculate_standard_deviation"
  154. case utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE: //预测百分位->70
  155. tableName = "edb_data_predict_ccalculate_percentile"
  156. case utils.DATA_SOURCE_FUBAO: //富宝-71
  157. tableName = "edb_data_fubao"
  158. case utils.DATA_SOURCE_CALCULATE_ZSXY:
  159. tableName = "edb_data_calculate_zsxy" // 指数修匀->72
  160. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  161. tableName = "edb_data_predict_calculate_zsxy" // 预测指数修匀->73
  162. case utils.DATA_SOURCE_CALCULATE_ZDYFX:
  163. tableName = "edb_data_calculate_zdyfx" // 自定义分析->74
  164. case utils.DATA_SOURCE_CALCULATE_RJZ: //日均值->75
  165. tableName = "edb_data_calculate_rjz"
  166. case utils.DATA_SOURCE_GFEX: //广州期货交易所->78
  167. tableName = "edb_data_gz"
  168. case utils.DATA_SOURCE_ICPI: //ICPI消费价格指数->79
  169. tableName = "edb_data_icpi"
  170. default:
  171. edbSource := EdbSourceIdMap[source]
  172. if edbSource != nil {
  173. tableName = edbSource.TableName
  174. }
  175. }
  176. return
  177. }
  178. //计算指标->关联的基础指标表
  179. //func GetEdbInfoCalculateTableName(source int) (tableName string) {
  180. // switch source {
  181. // case utils.DATA_SOURCE_CALCULATE:
  182. // tableName = "edb_info_calculate"
  183. // case utils.DATA_SOURCE_CALCULATE_LJZZY:
  184. // tableName = "edb_info_calculate_ljzzy"
  185. // case utils.DATA_SOURCE_CALCULATE_TBZ:
  186. // tableName = "edb_info_calculate_tbz"
  187. // case utils.DATA_SOURCE_CALCULATE_TCZ:
  188. // tableName = "edb_info_calculate_tcz"
  189. // case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  190. // tableName = "edb_info_calculate_nszydpjjs"
  191. // case utils.DATA_SOURCE_CALCULATE_HBZ:
  192. // tableName = "edb_info_calculate_hbz"
  193. // case utils.DATA_SOURCE_CALCULATE_HCZ:
  194. // tableName = "edb_info_calculate_hcz"
  195. // case utils.DATA_SOURCE_CALCULATE_BP:
  196. // tableName = "edb_info_calculate_bp"
  197. // default:
  198. // tableName = ""
  199. // }
  200. // return
  201. //}
  202. type EdbDataBase struct {
  203. EdbDataId int `orm:"column(edb_data_id);pk"`
  204. EdbInfoId int
  205. EdbCode string
  206. DataTime string
  207. Value string
  208. Status int
  209. CreateTime time.Time
  210. ModifyTime time.Time
  211. DataTimestamp int64
  212. }
  213. func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  214. var pars []interface{}
  215. pars = append(pars, edbCode)
  216. o := orm.NewOrmUsingDB("data")
  217. tableName := GetEdbDataTableName(source, subSource)
  218. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  219. if limit > 0 {
  220. sql += ` LIMIT ? `
  221. pars = append(pars, limit)
  222. }
  223. sql = fmt.Sprintf(sql, tableName)
  224. _, err = o.Raw(sql, pars).QueryRows(&items)
  225. return
  226. }
  227. func GetBaseIndexInfoByEdbCode(edbCode string, source int) (item *BaseIndexInfo, err error) {
  228. var pars []interface{}
  229. pars = append(pars, edbCode)
  230. o := orm.NewOrmUsingDB("data")
  231. tableName := GetBaseIndexTableName(source)
  232. if tableName == "" {
  233. err = fmt.Errorf("未找到对应的表")
  234. return
  235. }
  236. sql := ` SELECT * FROM %s WHERE index_code=? `
  237. sql = fmt.Sprintf(sql, tableName)
  238. err = o.Raw(sql, pars).QueryRow(&item)
  239. return
  240. }
  241. func GetEdbDataBaseByCodeAndDate(source, subSource int, edbCode string, startDate string) (count int, err error) {
  242. o := orm.NewOrmUsingDB("data")
  243. tableName := GetEdbDataTableName(source, subSource)
  244. sql := ` SELECT COUNT(1) AS count FROM %s WHERE edb_code=? AND data_time=? `
  245. sql = fmt.Sprintf(sql, tableName)
  246. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  247. return
  248. }
  249. func GetBaseIndexTableName(source int) (tableName string) {
  250. edbSource := EdbSourceIdMap[source]
  251. if edbSource != nil {
  252. tableName = edbSource.IndexTableName
  253. }
  254. return
  255. }
  256. func GetEdbDataAllByEdbCodeAndSubSource(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  257. var pars []interface{}
  258. pars = append(pars, edbCode)
  259. o := orm.NewOrmUsingDB("data")
  260. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  261. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  262. if limit > 0 {
  263. sql += ` LIMIT ? `
  264. pars = append(pars, limit)
  265. }
  266. sql = fmt.Sprintf(sql, tableName)
  267. _, err = o.Raw(sql, pars).QueryRows(&items)
  268. return
  269. }
  270. // GetEdbDataTableName 指标数据->存储表
  271. func GetEdbDataTableNameAndSubSource(source, subSource int) (tableName string) {
  272. switch source {
  273. case utils.DATA_SOURCE_WIND:
  274. if subSource == utils.DATA_SUB_SOURCE_EDB {
  275. tableName = "edb_data_wind"
  276. } else {
  277. tableName = "edb_data_wind_wsd"
  278. }
  279. case utils.DATA_SOURCE_THS:
  280. if subSource == utils.DATA_SUB_SOURCE_EDB {
  281. tableName = "edb_data_ths"
  282. } else {
  283. tableName = "edb_data_ths_ds"
  284. }
  285. case utils.DATA_SOURCE_YS:
  286. tableName = "edb_data_ys"
  287. default:
  288. edbSource := EdbSourceIdMap[source]
  289. if edbSource != nil {
  290. tableName = edbSource.TableName
  291. }
  292. }
  293. return
  294. }
  295. func GetEdbDataAllByEdbCodes(edbCodes []string, limit int) (items []*EdbInfoSearchData, err error) {
  296. var pars []interface{}
  297. pars = append(pars, edbCodes)
  298. o := orm.NewOrmUsingDB("data")
  299. sql := ` SELECT * FROM edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) ORDER BY data_time DESC`
  300. if limit > 0 {
  301. sql += ` LIMIT ? `
  302. pars = append(pars, limit)
  303. }
  304. _, err = o.Raw(sql, pars).QueryRows(&items)
  305. return
  306. }