query.go 11 KB

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