query.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package edb_data
  2. import (
  3. "errors"
  4. "fmt"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/models/mgo"
  8. "hongze/hongze_yb/models/tables/edb_source"
  9. "hongze/hongze_yb/utils"
  10. "strconv"
  11. "time"
  12. )
  13. // GetEdbDataTableName 指标数据->存储表
  14. func GetEdbDataTableName(source, subSource int) (tableName string) {
  15. switch source {
  16. case utils.DATA_SOURCE_THS:
  17. switch subSource {
  18. case utils.DATA_SUB_SOURCE_DATE:
  19. tableName = "edb_data_ths_ds"
  20. case utils.DATA_SUB_SOURCE_HIGH_FREQUENCY:
  21. tableName = "edb_data_ths_hf"
  22. default:
  23. tableName = "edb_data_ths"
  24. }
  25. case utils.DATA_SOURCE_WIND:
  26. if subSource == utils.DATA_SUB_SOURCE_DATE {
  27. tableName = "edb_data_wind_wsd"
  28. } else {
  29. tableName = "edb_data_wind"
  30. }
  31. case utils.DATA_SOURCE_PB, utils.DATA_SOURCE_PB_FINANCE: //彭博经济数据、彭博财务数据
  32. tableName = "edb_data_pb"
  33. case utils.DATA_SOURCE_CALCULATE:
  34. tableName = "edb_data_calculate"
  35. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  36. tableName = "edb_data_calculate_ljzzy"
  37. case utils.DATA_SOURCE_CALCULATE_TBZ:
  38. tableName = "edb_data_calculate_tbz"
  39. case utils.DATA_SOURCE_CALCULATE_TCZ:
  40. tableName = "edb_data_calculate_tcz"
  41. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  42. tableName = "edb_data_calculate_nszydpjjs"
  43. case utils.DATA_SOURCE_MANUAL:
  44. tableName = "edb_data_manual"
  45. case utils.DATA_SOURCE_LZ:
  46. tableName = "edb_data_lz"
  47. case utils.DATA_SOURCE_YS:
  48. tableName = "edb_data_ys"
  49. case utils.DATA_SOURCE_CALCULATE_HBZ:
  50. tableName = "edb_data_calculate_hbz"
  51. case utils.DATA_SOURCE_CALCULATE_HCZ:
  52. tableName = "edb_data_calculate_hcz"
  53. case utils.DATA_SOURCE_CALCULATE_BP:
  54. tableName = "edb_data_calculate_bp"
  55. case utils.DATA_SOURCE_GL:
  56. tableName = "edb_data_gl"
  57. case utils.DATA_SOURCE_ZZ:
  58. tableName = "edb_data_zz"
  59. case utils.DATA_SOURCE_DL:
  60. tableName = "edb_data_dl"
  61. case utils.DATA_SOURCE_SH:
  62. tableName = "edb_data_sh"
  63. case utils.DATA_SOURCE_CFFEX:
  64. tableName = "edb_data_cffex"
  65. case utils.DATA_SOURCE_SHFE:
  66. tableName = "edb_data_ine"
  67. case utils.DATA_SOURCE_GIE:
  68. tableName = "edb_data_gie"
  69. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  70. tableName = "edb_data_calculate_zjpj"
  71. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  72. tableName = "edb_data_calculate_time_shift"
  73. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
  74. tableName = "edb_data_calculate_ljztbpj"
  75. case utils.DATA_SOURCE_LT:
  76. tableName = "edb_data_lt"
  77. case utils.DATA_SOURCE_COAL:
  78. tableName = "edb_data_coal"
  79. case utils.DATA_SOURCE_PYTHON:
  80. tableName = "edb_data_python"
  81. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  82. tableName = "edb_data_google_travel"
  83. case utils.DATA_SOURCE_PREDICT_CALCULATE:
  84. tableName = "edb_data_predict_calculate"
  85. case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  86. tableName = "edb_data_predict_calculate_tbz"
  87. case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  88. tableName = "edb_data_predict_calculate_tcz"
  89. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  90. tableName = "edb_data_mysteel_chemical"
  91. case utils.DATA_SOURCE_CALCULATE_CJJX:
  92. tableName = "edb_data_calculate_cjjx"
  93. case utils.DATA_SOURCE_EIA_STEO:
  94. tableName = "edb_data_eia_steo"
  95. case utils.DATA_SOURCE_CALCULATE_NHCC:
  96. tableName = "edb_data_calculate_nhcc"
  97. case utils.DATA_SOURCE_COM_TRADE:
  98. tableName = "edb_data_com_trade"
  99. case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  100. tableName = "edb_data_predict_calculate_nszydpjjs"
  101. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  102. tableName = "edb_data_calculate_adjust"
  103. case utils.DATA_SOURCE_SCI:
  104. tableName = "edb_data_sci"
  105. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  106. tableName = "edb_data_predict_calculate_ljzzy"
  107. case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  108. tableName = "edb_data_predict_calculate_time_shift"
  109. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ:
  110. tableName = "edb_data_predict_calculate_zjpj"
  111. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ:
  112. tableName = "edb_data_predict_calculate_ljztbpj"
  113. case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC:
  114. tableName = "edb_data_predict_calculate_nhcc"
  115. case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  116. tableName = "edb_data_predict_calculate_cjjx"
  117. case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  118. tableName = "edb_data_predict_calculate_hbz"
  119. case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  120. tableName = "edb_data_predict_calculate_hcz"
  121. case utils.DATA_SOURCE_PREDICT_CALCULATE_BP:
  122. tableName = "edb_data_predict_calculate_bp"
  123. case utils.DATA_SOURCE_CALCULATE_JP:
  124. tableName = "edb_data_calculate_jp"
  125. case utils.DATA_SOURCE_CALCULATE_NH:
  126. tableName = "edb_data_calculate_nh"
  127. case utils.DATA_SOURCE_CALCULATE_KSZS:
  128. tableName = "edb_data_calculate_kszs"
  129. case utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  130. tableName = "edb_data_predict_calculate_jp"
  131. case utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  132. tableName = "edb_data_predict_calculate_nh"
  133. case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS:
  134. tableName = "edb_data_predict_calculate_kszs"
  135. case utils.DATA_SOURCE_BAIINFO:
  136. tableName = "edb_data_baiinfo"
  137. case utils.DATA_SOURCE_STOCK_PLANT:
  138. tableName = "edb_data_stock_plant"
  139. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  140. tableName = "edb_data_calculate_correlation"
  141. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  142. tableName = "edb_data_national_statistics"
  143. case utils.DATA_SOURCE_CALCULATE_LJZZJ: //累计值转季 -> 61
  144. tableName = "edb_data_calculate_ljzzj"
  145. case utils.DATA_SOURCE_CALCULATE_LJZ: //累计值 -> 62
  146. tableName = "edb_data_calculate_ljz"
  147. case utils.DATA_SOURCE_CALCULATE_LJZNCZJ: //累计值(年初至今) -> 63
  148. tableName = "edb_data_calculate_ljznczj"
  149. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZJ: // 预测指标 - 累计值 -> 65
  150. tableName = "edb_data_predict_calculate_ljzzj"
  151. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZ: //预测指标 - 累计值转季->64
  152. tableName = "edb_data_predict_calculate_ljz"
  153. case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZNCZJ: //预测指标 - 累计值(年初至今) -> 66
  154. tableName = "edb_data_predict_calculate_ljznczj"
  155. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION: //标准差->67
  156. tableName = "edb_data_calculate_standard_deviation"
  157. case utils.DATA_SOURCE_CALCULATE_PERCENTILE: //百分位->68
  158. tableName = "edb_data_calculate_percentile"
  159. case utils.DATA_SOURCE_PREDICT_CALCULATE_STANDARD_DEVIATION: //预测标准差->69
  160. tableName = "edb_data_predict_ccalculate_standard_deviation"
  161. case utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE: //预测百分位->70
  162. tableName = "edb_data_predict_ccalculate_percentile"
  163. case utils.DATA_SOURCE_FUBAO: //富宝 -> 71
  164. tableName = "edb_data_fubao"
  165. case utils.DATA_SOURCE_CALCULATE_ZSXY:
  166. tableName = "edb_data_calculate_zsxy" // 指数修匀->72
  167. case utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  168. tableName = "edb_data_predict_calculate_zsxy" // 预测指数修匀->73
  169. case utils.DATA_SOURCE_CALCULATE_ZDYFX:
  170. tableName = "edb_data_calculate_zdyfx" // 自定义分析->74
  171. case utils.DATA_SOURCE_CALCULATE_RJZ: //日均值->75
  172. tableName = "edb_data_calculate_rjz"
  173. case utils.DATA_SOURCE_PREDICT: // 基础预测指标->30
  174. tableName = "edb_data_predict_base"
  175. default:
  176. edbSource, _ := edb_source.GetEdbSourceBySource(source)
  177. if edbSource != nil {
  178. tableName = edbSource.TbName
  179. }
  180. }
  181. return
  182. }
  183. type EdbDataList struct {
  184. EdbDataId int `description:" 指标数据ID"`
  185. EdbInfoId int `description:"指标ID"`
  186. DataTime string `json:"-" description:"数据日期"`
  187. DataTimestamp int64 `description:"数据日期"`
  188. Value float64 `description:"数据值"`
  189. }
  190. type EdbDataItems struct {
  191. Items []*EdbDataList
  192. Year int
  193. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  194. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  195. }
  196. type EdbDataResult struct {
  197. List []*EdbDataItems
  198. }
  199. type QuarterData struct {
  200. Year string
  201. DataList []*EdbDataList
  202. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  203. ChartLegend string
  204. Years string
  205. }
  206. type QuarterXDateItem struct {
  207. StartDate time.Time
  208. EndDate time.Time
  209. ShowName string
  210. ChartLegend string
  211. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  212. }
  213. type SeasonExtraItem struct {
  214. ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
  215. XStartDate string `description:"横坐标显示的起始日"`
  216. XEndDate string `description:"横坐标显示的截止日"`
  217. JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
  218. }
  219. type SeasonChartLegend struct {
  220. Name string
  221. Value string
  222. }
  223. type QuarterDataList []*QuarterData
  224. func (m QuarterDataList) Len() int {
  225. return len(m)
  226. }
  227. func (m QuarterDataList) Less(i, j int) bool {
  228. return m[i].Years < m[j].Years
  229. }
  230. func (m QuarterDataList) Swap(i, j int) {
  231. m[i], m[j] = m[j], m[i]
  232. }
  233. // GetEdbDataList 获取指标数据
  234. func GetEdbDataList(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  235. // 自有数据需要额外处理(从mongo获取)
  236. if source == utils.DATA_SOURCE_BUSINESS && global.CONFIG.Serve.UseMongo {
  237. return getEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  238. }
  239. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && global.CONFIG.Serve.UseMongo {
  240. return getThsHfEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  241. }
  242. return getEdbDataListByMysql(source, subSource, edbInfoId, startDate, endDate)
  243. }
  244. // getEdbDataListByMysql
  245. // @Description: 通过mysql查询指标数据
  246. // @author: Roc
  247. // @datetime 2024-05-08 17:52:21
  248. // @param source int
  249. // @param subSource int
  250. // @param edbInfoId int
  251. // @param startDate string
  252. // @param endDate string
  253. // @return list []*EdbDataList
  254. // @return err error
  255. func getEdbDataListByMysql(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  256. tableName := GetEdbDataTableName(source, subSource)
  257. if tableName == "" {
  258. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  259. list = make([]*EdbDataList, 0)
  260. return list, err
  261. }
  262. var pars []interface{}
  263. pars = append(pars, edbInfoId)
  264. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id = ? `
  265. if startDate != "" {
  266. sql += ` AND data_time >= ? `
  267. pars = append(pars, startDate)
  268. }
  269. if endDate != "" {
  270. sql += ` AND data_time <= ? `
  271. pars = append(pars, endDate)
  272. }
  273. sql += ` ORDER BY data_time ASC `
  274. sql = fmt.Sprintf(sql, tableName)
  275. err = global.MYSQL["data"].Raw(sql, pars...).Scan(&list).Error
  276. // 格式化日期
  277. if len(list) > 0 {
  278. for _, row := range list {
  279. if row.DataTime != "" {
  280. row.DataTime = row.DataTime[:10] // 此处获取的字符串row.DataTime长度有长有短,截取年月日
  281. //tempTime, _ := time.Parse("2006-01-02T00:00:00+08:00", row.DataTime)
  282. //row.DataTime = tempTime.Format(utils.FormatDate)
  283. }
  284. }
  285. }
  286. return
  287. }
  288. // getEdbDataListByMongo
  289. // @Description: 通过mongo查询指标数据
  290. // @author: Roc
  291. // @datetime 2024-05-07 10:40:43
  292. // @param source int
  293. // @param subSource int
  294. // @param endInfoId int
  295. // @param startDate string
  296. // @param endDate string
  297. // @return list []*EdbDataList
  298. // @return err error
  299. func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  300. list = make([]*EdbDataList, 0)
  301. mogDataObj := mgo.EdbDataBusiness{}
  302. // 构建查询条件
  303. queryConditions := bson.M{
  304. "edb_info_id": edbInfoId,
  305. }
  306. // 日期
  307. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  308. if err != nil {
  309. return
  310. }
  311. if len(dateCondition) > 0 {
  312. queryConditions["data_time"] = dateCondition
  313. }
  314. // 获取列表数据
  315. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  316. if tmpErr != nil {
  317. err = tmpErr
  318. return
  319. }
  320. for k, v := range tmpDataList {
  321. list = append(list, &EdbDataList{
  322. EdbDataId: k + 1,
  323. EdbInfoId: v.EdbInfoId,
  324. DataTime: v.DataTime.Format(utils.FormatDate),
  325. DataTimestamp: v.DataTimestamp,
  326. Value: v.Value,
  327. })
  328. }
  329. return
  330. }
  331. func getThsHfEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  332. list = make([]*EdbDataList, 0)
  333. mogDataObj := mgo.EdbDataThsHf{}
  334. // 构建查询条件
  335. queryConditions := bson.M{
  336. "edb_info_id": edbInfoId,
  337. }
  338. // 日期
  339. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  340. if err != nil {
  341. return
  342. }
  343. if len(dateCondition) > 0 {
  344. queryConditions["data_time"] = dateCondition
  345. }
  346. // 获取列表数据
  347. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  348. if tmpErr != nil {
  349. err = tmpErr
  350. return
  351. }
  352. for k, v := range tmpDataList {
  353. list = append(list, &EdbDataList{
  354. EdbDataId: k + 1,
  355. EdbInfoId: v.EdbInfoId,
  356. DataTime: v.DataTime.Format(utils.FormatDate),
  357. DataTimestamp: v.DataTimestamp,
  358. Value: v.Value,
  359. })
  360. }
  361. return
  362. }