edb_data_base.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. return GetEdbDataAllByEdbCodeByMysql(edbCode, source, subSource, limit)
  223. }
  224. func GetEdbDataAllByEdbCodeByMysql(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  225. var pars []interface{}
  226. pars = append(pars, edbCode)
  227. o := orm.NewOrmUsingDB("data")
  228. tableName := GetEdbDataTableName(source, subSource)
  229. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  230. if limit > 0 {
  231. sql += ` LIMIT ? `
  232. pars = append(pars, limit)
  233. }
  234. sql = fmt.Sprintf(sql, tableName)
  235. _, err = o.Raw(sql, pars).QueryRows(&items)
  236. return
  237. }
  238. func GetEdbDataAllByEdbCodeByMongo(edbCode string, source, subSource, limit int) (list []*EdbInfoSearchData, err error) {
  239. list = make([]*EdbInfoSearchData, 0)
  240. mogDataObj := mgo.EdbDataBusiness{}
  241. // 构建查询条件
  242. queryConditions := bson.M{
  243. "edb_code": edbCode,
  244. }
  245. var tmpDataList []*mgo.EdbDataBusiness
  246. // 获取列表数据
  247. if limit > 0 {
  248. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  249. } else {
  250. tmpDataList, err = mogDataObj.GetLimitDataList(queryConditions, int64(limit), []string{"-data_time"})
  251. }
  252. if err != nil {
  253. return
  254. }
  255. for _, v := range tmpDataList {
  256. list = append(list, &EdbInfoSearchData{
  257. DataTime: v.DataTime.Format(utils.FormatDate),
  258. Value: v.Value,
  259. })
  260. }
  261. return
  262. }
  263. func GetBaseIndexInfoByEdbCode(edbCode string, source int) (item *BaseIndexInfo, err error) {
  264. var pars []interface{}
  265. pars = append(pars, edbCode)
  266. o := orm.NewOrmUsingDB("data")
  267. tableName := GetBaseIndexTableName(source)
  268. if tableName == "" {
  269. err = fmt.Errorf("未找到对应的表")
  270. return
  271. }
  272. sql := ` SELECT * FROM %s WHERE index_code=? `
  273. sql = fmt.Sprintf(sql, tableName)
  274. err = o.Raw(sql, pars).QueryRow(&item)
  275. return
  276. }
  277. func GetBaseIndexTableName(source int) (tableName string) {
  278. edbSource := EdbSourceIdMap[source]
  279. if edbSource != nil {
  280. tableName = edbSource.IndexTableName
  281. }
  282. return
  283. }
  284. func GetEdbDataAllByEdbCodeAndSubSource(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  285. var pars []interface{}
  286. pars = append(pars, edbCode)
  287. o := orm.NewOrmUsingDB("data")
  288. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  289. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  290. if limit > 0 {
  291. sql += ` LIMIT ? `
  292. pars = append(pars, limit)
  293. }
  294. sql = fmt.Sprintf(sql, tableName)
  295. _, err = o.Raw(sql, pars).QueryRows(&items)
  296. return
  297. }
  298. // GetEdbDataTableNameAndSubSource 指标数据->存储表
  299. func GetEdbDataTableNameAndSubSource(source, subSource int) (tableName string) {
  300. switch source {
  301. case utils.DATA_SOURCE_WIND:
  302. if subSource == utils.DATA_SUB_SOURCE_EDB {
  303. tableName = "edb_data_wind"
  304. } else {
  305. tableName = "edb_data_wind_wsd"
  306. }
  307. case utils.DATA_SOURCE_THS:
  308. if subSource == utils.DATA_SUB_SOURCE_EDB {
  309. tableName = "edb_data_ths"
  310. } else {
  311. tableName = "edb_data_ths_ds"
  312. }
  313. case utils.DATA_SOURCE_YS:
  314. tableName = "edb_data_ys"
  315. default:
  316. edbSource := EdbSourceIdMap[source]
  317. if edbSource != nil {
  318. tableName = edbSource.TableName
  319. }
  320. }
  321. return
  322. }
  323. func GetEdbDataAllByEdbCodes(edbCodes []string, limit int) (items []*EdbInfoSearchData, err error) {
  324. var pars []interface{}
  325. pars = append(pars, edbCodes)
  326. o := orm.NewOrmUsingDB("data")
  327. sql := ` SELECT * FROM edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) ORDER BY data_time DESC`
  328. if limit > 0 {
  329. sql += ` LIMIT ? `
  330. pars = append(pars, limit)
  331. }
  332. _, err = o.Raw(sql, pars).QueryRows(&items)
  333. return
  334. }
  335. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  336. nowStr := time.Now().Format(utils.FormatDateTime)
  337. addSql += "("
  338. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  339. "," + "'" + nowStr + "'" + "," + "1"
  340. addSql += "," + "'" + timestampStr + "'"
  341. addSql += "),"
  342. return
  343. }