edb_data_base.go 13 KB

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