query.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. default:
  174. edbSource, _ := edb_source.GetEdbSourceBySource(source)
  175. if edbSource != nil {
  176. tableName = edbSource.TbName
  177. }
  178. }
  179. return
  180. }
  181. type EdbDataList struct {
  182. EdbDataId int `description:" 指标数据ID"`
  183. EdbInfoId int `description:"指标ID"`
  184. DataTime string `json:"-" description:"数据日期"`
  185. DataTimestamp int64 `description:"数据日期"`
  186. Value float64 `description:"数据值"`
  187. }
  188. type EdbDataItems struct {
  189. Items []*EdbDataList
  190. Year int
  191. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  192. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  193. }
  194. type EdbDataResult struct {
  195. List []*EdbDataItems
  196. }
  197. type QuarterData struct {
  198. Year string
  199. DataList []*EdbDataList
  200. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  201. ChartLegend string
  202. Years string
  203. }
  204. type QuarterXDateItem struct {
  205. StartDate time.Time
  206. EndDate time.Time
  207. ShowName string
  208. ChartLegend string
  209. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  210. }
  211. type SeasonExtraItem struct {
  212. ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
  213. XStartDate string `description:"横坐标显示的起始日"`
  214. XEndDate string `description:"横坐标显示的截止日"`
  215. JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
  216. }
  217. type SeasonChartLegend struct {
  218. Name string
  219. Value string
  220. }
  221. type QuarterDataList []*QuarterData
  222. func (m QuarterDataList) Len() int {
  223. return len(m)
  224. }
  225. func (m QuarterDataList) Less(i, j int) bool {
  226. return m[i].Years < m[j].Years
  227. }
  228. func (m QuarterDataList) Swap(i, j int) {
  229. m[i], m[j] = m[j], m[i]
  230. }
  231. // GetEdbDataList 获取指标数据
  232. func GetEdbDataList(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  233. // 自有数据需要额外处理(从mongo获取)
  234. if source == utils.DATA_SOURCE_BUSINESS && global.CONFIG.Serve.UseMongo {
  235. return getEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  236. }
  237. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && global.CONFIG.Serve.UseMongo {
  238. return getThsHfEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
  239. }
  240. return getEdbDataListByMysql(source, subSource, edbInfoId, startDate, endDate)
  241. }
  242. // getEdbDataListByMysql
  243. // @Description: 通过mysql查询指标数据
  244. // @author: Roc
  245. // @datetime 2024-05-08 17:52:21
  246. // @param source int
  247. // @param subSource int
  248. // @param edbInfoId int
  249. // @param startDate string
  250. // @param endDate string
  251. // @return list []*EdbDataList
  252. // @return err error
  253. func getEdbDataListByMysql(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  254. tableName := GetEdbDataTableName(source, subSource)
  255. if tableName == "" {
  256. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  257. list = make([]*EdbDataList, 0)
  258. return list, err
  259. }
  260. var pars []interface{}
  261. pars = append(pars, edbInfoId)
  262. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id = ? `
  263. if startDate != "" {
  264. sql += ` AND data_time >= ? `
  265. pars = append(pars, startDate)
  266. }
  267. if endDate != "" {
  268. sql += ` AND data_time <= ? `
  269. pars = append(pars, endDate)
  270. }
  271. sql += ` ORDER BY data_time ASC `
  272. sql = fmt.Sprintf(sql, tableName)
  273. err = global.MYSQL["data"].Raw(sql, pars...).Scan(&list).Error
  274. // 格式化日期
  275. if len(list) > 0 {
  276. for _, row := range list {
  277. if row.DataTime != "" {
  278. row.DataTime = row.DataTime[:10] // 此处获取的字符串row.DataTime长度有长有短,截取年月日
  279. //tempTime, _ := time.Parse("2006-01-02T00:00:00+08:00", row.DataTime)
  280. //row.DataTime = tempTime.Format(utils.FormatDate)
  281. }
  282. }
  283. }
  284. return
  285. }
  286. // getEdbDataListByMongo
  287. // @Description: 通过mongo查询指标数据
  288. // @author: Roc
  289. // @datetime 2024-05-07 10:40:43
  290. // @param source int
  291. // @param subSource int
  292. // @param endInfoId int
  293. // @param startDate string
  294. // @param endDate string
  295. // @return list []*EdbDataList
  296. // @return err error
  297. func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  298. list = make([]*EdbDataList, 0)
  299. mogDataObj := mgo.EdbDataBusiness{}
  300. // 构建查询条件
  301. queryConditions := bson.M{
  302. "edb_info_id": edbInfoId,
  303. }
  304. // 日期
  305. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  306. if err != nil {
  307. return
  308. }
  309. if len(dateCondition) > 0 {
  310. queryConditions["data_time"] = dateCondition
  311. }
  312. // 获取列表数据
  313. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  314. if tmpErr != nil {
  315. err = tmpErr
  316. return
  317. }
  318. for k, v := range tmpDataList {
  319. list = append(list, &EdbDataList{
  320. EdbDataId: k + 1,
  321. EdbInfoId: v.EdbInfoId,
  322. DataTime: v.DataTime.Format(utils.FormatDate),
  323. DataTimestamp: v.DataTimestamp,
  324. Value: v.Value,
  325. })
  326. }
  327. return
  328. }
  329. func getThsHfEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  330. list = make([]*EdbDataList, 0)
  331. mogDataObj := mgo.EdbDataThsHf{}
  332. // 构建查询条件
  333. queryConditions := bson.M{
  334. "edb_info_id": edbInfoId,
  335. }
  336. // 日期
  337. dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
  338. if err != nil {
  339. return
  340. }
  341. if len(dateCondition) > 0 {
  342. queryConditions["data_time"] = dateCondition
  343. }
  344. // 获取列表数据
  345. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  346. if tmpErr != nil {
  347. err = tmpErr
  348. return
  349. }
  350. for k, v := range tmpDataList {
  351. list = append(list, &EdbDataList{
  352. EdbDataId: k + 1,
  353. EdbInfoId: v.EdbInfoId,
  354. DataTime: v.DataTime.Format(utils.FormatDate),
  355. DataTimestamp: v.DataTimestamp,
  356. Value: v.Value,
  357. })
  358. }
  359. return
  360. }