edb_data_base.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. default:
  175. edbSource := EdbSourceIdMap[source]
  176. if edbSource != nil {
  177. tableName = edbSource.TableName
  178. }
  179. }
  180. return
  181. }
  182. //计算指标->关联的基础指标表
  183. //func GetEdbInfoCalculateTableName(source int) (tableName string) {
  184. // switch source {
  185. // case utils.DATA_SOURCE_CALCULATE:
  186. // tableName = "edb_info_calculate"
  187. // case utils.DATA_SOURCE_CALCULATE_LJZZY:
  188. // tableName = "edb_info_calculate_ljzzy"
  189. // case utils.DATA_SOURCE_CALCULATE_TBZ:
  190. // tableName = "edb_info_calculate_tbz"
  191. // case utils.DATA_SOURCE_CALCULATE_TCZ:
  192. // tableName = "edb_info_calculate_tcz"
  193. // case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  194. // tableName = "edb_info_calculate_nszydpjjs"
  195. // case utils.DATA_SOURCE_CALCULATE_HBZ:
  196. // tableName = "edb_info_calculate_hbz"
  197. // case utils.DATA_SOURCE_CALCULATE_HCZ:
  198. // tableName = "edb_info_calculate_hcz"
  199. // case utils.DATA_SOURCE_CALCULATE_BP:
  200. // tableName = "edb_info_calculate_bp"
  201. // default:
  202. // tableName = ""
  203. // }
  204. // return
  205. //}
  206. type EdbDataBase struct {
  207. EdbDataId int `orm:"column(edb_data_id);pk"`
  208. EdbInfoId int
  209. EdbCode string
  210. DataTime string
  211. Value string
  212. Status int
  213. CreateTime time.Time
  214. ModifyTime time.Time
  215. DataTimestamp int64
  216. }
  217. func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  218. // 自有数据需要额外处理(从mongo获取)
  219. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  220. return GetEdbDataAllByEdbCodeByMongo(edbCode, source, subSource, limit)
  221. }
  222. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  223. return GetThsHfEdbDataAllByEdbCodeByMongo(edbCode, source, subSource, limit)
  224. }
  225. return GetEdbDataAllByEdbCodeByMysql(edbCode, source, subSource, limit)
  226. }
  227. func GetEdbDataAllByEdbCodeByMysql(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  228. var pars []interface{}
  229. pars = append(pars, edbCode)
  230. o := orm.NewOrmUsingDB("data")
  231. tableName := GetEdbDataTableName(source, subSource)
  232. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  233. if limit > 0 {
  234. sql += ` LIMIT ? `
  235. pars = append(pars, limit)
  236. }
  237. sql = fmt.Sprintf(sql, tableName)
  238. _, err = o.Raw(sql, pars).QueryRows(&items)
  239. return
  240. }
  241. func GetEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) {
  242. list = make([]*EdbInfoSearchData, 0)
  243. mogDataObj := mgo.EdbDataBusiness{}
  244. // 构建查询条件
  245. queryConditions := bson.M{
  246. "edb_code": edbCode,
  247. }
  248. var tmpDataList []*mgo.EdbDataBusiness
  249. // 获取列表数据
  250. if limit > 0 {
  251. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  252. } else {
  253. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  254. }
  255. if err != nil {
  256. return
  257. }
  258. for _, v := range tmpDataList {
  259. list = append(list, &EdbInfoSearchData{
  260. DataTime: v.DataTime.Format(utils.FormatDate),
  261. Value: v.Value,
  262. })
  263. }
  264. return
  265. }
  266. func GetBaseIndexInfoByEdbCode(edbCode string, source int) (item *BaseIndexInfo, err error) {
  267. var pars []interface{}
  268. pars = append(pars, edbCode)
  269. o := orm.NewOrmUsingDB("data")
  270. tableName := GetBaseIndexTableName(source)
  271. if tableName == "" {
  272. err = fmt.Errorf("未找到对应的表")
  273. return
  274. }
  275. sql := ` SELECT * FROM %s WHERE index_code=? `
  276. sql = fmt.Sprintf(sql, tableName)
  277. err = o.Raw(sql, pars).QueryRow(&item)
  278. return
  279. }
  280. func GetBaseIndexTableName(source int) (tableName string) {
  281. edbSource := EdbSourceIdMap[source]
  282. if edbSource != nil {
  283. tableName = edbSource.IndexTableName
  284. }
  285. return
  286. }
  287. func GetEdbDataAllByEdbCodeAndSubSource(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  288. var pars []interface{}
  289. pars = append(pars, edbCode)
  290. o := orm.NewOrmUsingDB("data")
  291. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  292. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  293. if limit > 0 {
  294. sql += ` LIMIT ? `
  295. pars = append(pars, limit)
  296. }
  297. sql = fmt.Sprintf(sql, tableName)
  298. _, err = o.Raw(sql, pars).QueryRows(&items)
  299. return
  300. }
  301. // GetEdbDataTableNameAndSubSource 指标数据->存储表
  302. func GetEdbDataTableNameAndSubSource(source, subSource int) (tableName string) {
  303. switch source {
  304. case utils.DATA_SOURCE_WIND:
  305. if subSource == utils.DATA_SUB_SOURCE_EDB {
  306. tableName = "edb_data_wind"
  307. } else {
  308. tableName = "edb_data_wind_wsd"
  309. }
  310. case utils.DATA_SOURCE_THS:
  311. if subSource == utils.DATA_SUB_SOURCE_EDB {
  312. tableName = "edb_data_ths"
  313. } else {
  314. tableName = "edb_data_ths_ds"
  315. }
  316. case utils.DATA_SOURCE_YS:
  317. tableName = "edb_data_ys"
  318. default:
  319. edbSource := EdbSourceIdMap[source]
  320. if edbSource != nil {
  321. tableName = edbSource.TableName
  322. }
  323. }
  324. return
  325. }
  326. func GetEdbDataAllByEdbCodes(edbCodes []string, limit int) (items []*EdbInfoSearchData, err error) {
  327. var pars []interface{}
  328. pars = append(pars, edbCodes)
  329. o := orm.NewOrmUsingDB("data")
  330. sql := ` SELECT * FROM edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) ORDER BY data_time DESC`
  331. if limit > 0 {
  332. sql += ` LIMIT ? `
  333. pars = append(pars, limit)
  334. }
  335. _, err = o.Raw(sql, pars).QueryRows(&items)
  336. return
  337. }
  338. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  339. nowStr := time.Now().Format(utils.FormatDateTime)
  340. addSql += "("
  341. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  342. "," + "'" + nowStr + "'" + "," + "1"
  343. addSql += "," + "'" + timestampStr + "'"
  344. addSql += "),"
  345. return
  346. }
  347. func GetThsHfEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) {
  348. list = make([]*EdbInfoSearchData, 0)
  349. mogDataObj := mgo.EdbDataThsHf{}
  350. // 构建查询条件
  351. queryConditions := bson.M{
  352. "edb_code": edbCode,
  353. }
  354. var tmpDataList []*mgo.EdbDataThsHf
  355. // 获取列表数据
  356. if limit > 0 {
  357. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  358. } else {
  359. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  360. }
  361. if err != nil {
  362. return
  363. }
  364. for _, v := range tmpDataList {
  365. list = append(list, &EdbInfoSearchData{
  366. DataTime: v.DataTime.Format(utils.FormatDate),
  367. Value: v.Value,
  368. })
  369. }
  370. return
  371. }