edb_data_base.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // 指标数据->存储表
  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. default:
  167. edbSource := EdbSourceIdMap[source]
  168. if edbSource != nil {
  169. tableName = edbSource.TableName
  170. }
  171. }
  172. return
  173. }
  174. type EdbDataBase struct {
  175. EdbDataId int `orm:"column(edb_data_id);pk"`
  176. EdbInfoId int
  177. EdbCode string
  178. DataTime string
  179. Value string
  180. Status int
  181. CreateTime time.Time
  182. ModifyTime time.Time
  183. DataTimestamp int64
  184. }
  185. func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  186. var pars []interface{}
  187. pars = append(pars, edbCode)
  188. o := orm.NewOrmUsingDB("data")
  189. tableName := GetEdbDataTableName(source, subSource)
  190. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  191. if limit > 0 {
  192. sql += ` LIMIT ? `
  193. pars = append(pars, limit)
  194. }
  195. sql = fmt.Sprintf(sql, tableName)
  196. _, err = o.Raw(sql, pars).QueryRows(&items)
  197. return
  198. }
  199. func GetEdbDataBaseByCodeAndDate(source, subSource int, edbCode string, startDate string) (count int, err error) {
  200. o := orm.NewOrmUsingDB("data")
  201. tableName := GetEdbDataTableName(source, subSource)
  202. sql := ` SELECT COUNT(1) AS count FROM %s WHERE edb_code=? AND data_time=? `
  203. sql = fmt.Sprintf(sql, tableName)
  204. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  205. return
  206. }
  207. func GetEdbDataAllByEdbCodeAndSubSource(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  208. var pars []interface{}
  209. pars = append(pars, edbCode)
  210. o := orm.NewOrmUsingDB("data")
  211. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  212. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  213. if limit > 0 {
  214. sql += ` LIMIT ? `
  215. pars = append(pars, limit)
  216. }
  217. sql = fmt.Sprintf(sql, tableName)
  218. _, err = o.Raw(sql, pars).QueryRows(&items)
  219. return
  220. }
  221. // GetEdbDataTableName 指标数据->存储表
  222. func GetEdbDataTableNameAndSubSource(source, subSource int) (tableName string) {
  223. switch source {
  224. case utils.DATA_SOURCE_WIND:
  225. if subSource == utils.DATA_SUB_SOURCE_EDB {
  226. tableName = "edb_data_wind"
  227. } else {
  228. tableName = "edb_data_wind_wsd"
  229. }
  230. case utils.DATA_SOURCE_THS:
  231. if subSource == utils.DATA_SUB_SOURCE_EDB {
  232. tableName = "edb_data_ths"
  233. } else {
  234. tableName = "edb_data_ths_ds"
  235. }
  236. default:
  237. edbSource := EdbSourceIdMap[source]
  238. if edbSource != nil {
  239. tableName = edbSource.TableName
  240. }
  241. }
  242. return
  243. }
  244. func GetBaseIndexTableName(source int) (tableName string) {
  245. edbSource := EdbSourceIdMap[source]
  246. if edbSource != nil {
  247. tableName = edbSource.IndexTableName
  248. }
  249. return
  250. }
  251. func GetBaseIndexInfoByEdbCode(edbCode string, source int) (item *BaseIndexInfo, err error) {
  252. var pars []interface{}
  253. pars = append(pars, edbCode)
  254. o := orm.NewOrmUsingDB("data")
  255. tableName := GetBaseIndexTableName(source)
  256. if tableName == "" {
  257. err = fmt.Errorf("未找到对应的表")
  258. return
  259. }
  260. sql := ` SELECT * FROM %s WHERE index_code=? `
  261. sql = fmt.Sprintf(sql, tableName)
  262. err = o.Raw(sql, pars).QueryRow(&item)
  263. return
  264. }