edb_data_base.go 16 KB

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