chart_info.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. package future_good
  2. import (
  3. "errors"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/models/data_manage"
  6. "eta/eta_chart_lib/models/data_manage/future_good"
  7. "eta/eta_chart_lib/services/alarm_msg"
  8. "eta/eta_chart_lib/services/data"
  9. "eta/eta_chart_lib/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "sort"
  13. "strconv"
  14. "time"
  15. )
  16. // GetChartEdbData 获取图表的指标数据
  17. func GetChartEdbData(chartInfoId int, startDate, endDate string, edbInfoMapping, futureGoodEdbInfoMapping *models.ChartEdbInfoMapping, barChartInfoDateList []data_manage.BarChartInfoDateReq) (barConfigEdbInfoIdList []data_manage.BarChartInfoEdbItemReq, edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, xDataList []models.XData, yDataList []models.YData, err error) {
  18. edbList = make([]*models.ChartEdbInfoMapping, 0)
  19. if futureGoodEdbInfoMapping == nil {
  20. err = errors.New("商品指标未选取")
  21. return
  22. }
  23. if edbInfoMapping == nil {
  24. err = errors.New("ETA指标未选取")
  25. return
  26. }
  27. // 指标对应的所有数据
  28. edbDataListMap := make(map[int][]*models.EdbDataList)
  29. item := new(data_manage.ChartEdbInfoMapping)
  30. edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
  31. if edbInfoMapping.Unit == `无` {
  32. edbInfoMapping.Unit = ``
  33. }
  34. if futureGoodEdbInfoMapping.Unit == `无` {
  35. futureGoodEdbInfoMapping.Unit = ``
  36. }
  37. if chartInfoId <= 0 {
  38. edbInfoMapping.IsAxis = 1
  39. edbInfoMapping.LeadValue = 0
  40. edbInfoMapping.LeadUnit = ""
  41. edbInfoMapping.ChartEdbMappingId = 0
  42. edbInfoMapping.ChartInfoId = 0
  43. edbInfoMapping.IsOrder = false
  44. edbInfoMapping.EdbInfoType = 1
  45. edbInfoMapping.ChartStyle = ""
  46. edbInfoMapping.ChartColor = ""
  47. edbInfoMapping.ChartWidth = 0
  48. futureGoodEdbInfoMapping.IsAxis = 1
  49. futureGoodEdbInfoMapping.LeadValue = 0
  50. futureGoodEdbInfoMapping.LeadUnit = ""
  51. futureGoodEdbInfoMapping.ChartEdbMappingId = 0
  52. futureGoodEdbInfoMapping.ChartInfoId = 0
  53. futureGoodEdbInfoMapping.IsOrder = false
  54. futureGoodEdbInfoMapping.EdbInfoType = 1
  55. futureGoodEdbInfoMapping.ChartStyle = ""
  56. futureGoodEdbInfoMapping.ChartColor = ""
  57. futureGoodEdbInfoMapping.ChartWidth = 0
  58. } else {
  59. edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
  60. futureGoodEdbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(futureGoodEdbInfoMapping.LeadUnit)
  61. }
  62. // 普通的指标数据
  63. {
  64. dataList := make([]*models.EdbDataList, 0)
  65. dataList, err = models.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, startDate, endDate)
  66. edbDataListMap[edbInfoMapping.EdbInfoId] = dataList
  67. item.DataList = dataList
  68. edbList = append(edbList, edbInfoMapping)
  69. barConfigEdbInfoIdList = append(barConfigEdbInfoIdList, data_manage.BarChartInfoEdbItemReq{
  70. EdbInfoId: edbInfoMapping.EdbInfoId,
  71. //Name: edbInfoMapping.EdbName,
  72. Name: "现货价",
  73. NameEn: "Spot Price",
  74. Source: edbInfoMapping.Source,
  75. })
  76. }
  77. // 获取主力合约和最新日期
  78. var zlFutureGoodEdbInfo *future_good.FutureGoodEdbInfo
  79. var latestDate string // 最新日期是这个
  80. var regionType string // 交易所来源:“国内”,“海外”
  81. var futureGoodEdbType int
  82. {
  83. // 寻找主力合约
  84. zlFutureGoodEdbInfo, err = future_good.GetFutureGoodEdbInfo(futureGoodEdbInfoMapping.EdbInfoId)
  85. if err != nil {
  86. return
  87. }
  88. regionType = zlFutureGoodEdbInfo.RegionType
  89. futureGoodEdbType = zlFutureGoodEdbInfo.FutureGoodEdbType
  90. // 如果当前合约配置的 "画图时,日期来源的指标id" 并不是自己的话,那么就去查找对应配置的合约
  91. if zlFutureGoodEdbInfo.DateSourceId != zlFutureGoodEdbInfo.FutureGoodEdbInfoId {
  92. sourceDateFutureGoodEdbInfo, tmpErr := future_good.GetFutureGoodEdbInfo(zlFutureGoodEdbInfo.DateSourceId)
  93. if tmpErr != nil {
  94. err = tmpErr
  95. return
  96. }
  97. latestDate = sourceDateFutureGoodEdbInfo.EndDate // 最新日期是这个
  98. } else {
  99. latestDate = zlFutureGoodEdbInfo.EndDate // 最新日期是这个
  100. }
  101. if latestDate == `0000-00-00` {
  102. err = errors.New("日期异常")
  103. return
  104. }
  105. }
  106. // 获取期货指标以及期货数据
  107. tmpFutureGoodEdbInfoList, err := future_good.GetFutureGoodEdbInfoListByParentId(futureGoodEdbInfoMapping.EdbInfoId)
  108. if err != nil {
  109. return
  110. }
  111. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  112. _, futureGoodEdbInfoList, err := getFutureGoodEdbInfoList(latestDateTime, tmpFutureGoodEdbInfoList, barChartInfoDateList)
  113. if err != nil {
  114. return
  115. }
  116. futureGoodMappingList := make([]*models.ChartEdbInfoMapping, 0)
  117. for k, v := range futureGoodEdbInfoList {
  118. newMappingInfo := &models.ChartEdbInfoMapping{
  119. EdbInfoId: v.FutureGoodEdbInfoId,
  120. SourceName: v.Exchange,
  121. Source: 0,
  122. EdbCode: v.FutureGoodEdbCode,
  123. EdbName: v.FutureGoodEdbName,
  124. EdbAliasName: v.FutureGoodEdbName,
  125. EdbNameEn: v.FutureGoodEdbNameEn,
  126. EdbType: edbInfoMapping.EdbType,
  127. Frequency: edbInfoMapping.Frequency,
  128. FrequencyEn: edbInfoMapping.FrequencyEn,
  129. Unit: edbInfoMapping.Unit,
  130. UnitEn: edbInfoMapping.UnitEn,
  131. StartDate: v.StartDate,
  132. EndDate: v.EndDate,
  133. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  134. ChartEdbMappingId: v.FutureGoodEdbInfoId,
  135. ChartInfoId: edbInfoMapping.ChartInfoId,
  136. MaxData: v.MaxValue,
  137. MinData: v.MinValue,
  138. IsOrder: edbInfoMapping.IsOrder,
  139. IsAxis: edbInfoMapping.IsAxis,
  140. EdbInfoType: edbInfoMapping.EdbInfoType,
  141. EdbInfoCategoryType: edbInfoMapping.EdbInfoCategoryType,
  142. LeadValue: edbInfoMapping.LeadValue,
  143. LeadUnit: edbInfoMapping.LeadUnit,
  144. LeadUnitEn: edbInfoMapping.LeadUnitEn,
  145. ChartStyle: edbInfoMapping.ChartStyle,
  146. ChartColor: edbInfoMapping.ChartColor,
  147. PredictChartColor: edbInfoMapping.PredictChartColor,
  148. ChartWidth: edbInfoMapping.ChartWidth,
  149. ChartType: edbInfoMapping.ChartType,
  150. LatestDate: v.LatestDate.Format(utils.FormatDateTime),
  151. LatestValue: v.LatestValue,
  152. UniqueCode: futureGoodEdbInfoMapping.UniqueCode + strconv.Itoa(k),
  153. MinValue: v.MinValue,
  154. MaxValue: v.MaxValue,
  155. DataList: nil,
  156. IsNullData: false,
  157. }
  158. futureGoodMappingList = append(futureGoodMappingList, newMappingInfo)
  159. barConfigEdbInfoIdList = append(barConfigEdbInfoIdList, data_manage.BarChartInfoEdbItemReq{
  160. EdbInfoId: newMappingInfo.EdbInfoId,
  161. Name: fmt.Sprint("M+", v.Month),
  162. NameEn: fmt.Sprint("M+", v.Month),
  163. Source: newMappingInfo.Source,
  164. })
  165. }
  166. // 获取数据
  167. for _, v := range futureGoodMappingList {
  168. dataList := make([]*models.EdbDataList, 0)
  169. tmpDataList, tmpErr := future_good.GetFutureGoodEdbDataListByDate(v.EdbInfoId, startDate, endDate)
  170. if tmpErr != nil {
  171. return
  172. }
  173. for _, tmpData := range tmpDataList {
  174. dataList = append(dataList, &models.EdbDataList{
  175. EdbDataId: tmpData.FutureGoodEdbDataId,
  176. EdbInfoId: tmpData.FutureGoodEdbInfoId,
  177. DataTime: tmpData.DataTime.Format(utils.FormatDate),
  178. DataTimestamp: tmpData.DataTimestamp,
  179. Value: tmpData.Close,
  180. })
  181. }
  182. edbDataListMap[v.EdbInfoId] = dataList
  183. v.DataList = dataList
  184. }
  185. edbList = append(edbList, futureGoodMappingList...)
  186. xEdbIdValue, yDataList, err = BarChartData(edbList[0], futureGoodEdbInfoList, edbDataListMap, barChartInfoDateList, regionType, edbInfoMapping.EndDate)
  187. xDataList = []models.XData{
  188. {
  189. Name: "现货价",
  190. NameEn: "Spot Price",
  191. },
  192. }
  193. //yDataList =make([]data_manage.YData,0)
  194. //data_manage.YData{
  195. // Date: "",
  196. // Color: "",
  197. // Name: "",
  198. // NameEn: "",
  199. // Value: nil,
  200. // NoDataEdbList: nil,
  201. // XEdbInfoIdList: nil,
  202. //}
  203. futureGoodEdbInfoIndexMap := make(map[int]int)
  204. for index, v := range futureGoodEdbInfoList {
  205. futureGoodEdbInfoIndexMap[v.FutureGoodEdbInfoId] = index
  206. }
  207. nMap := make(map[int]int)
  208. maxIndex := 0 // 最大x轴的下标
  209. for k, tmpYData := range yDataList {
  210. yDataMap := tmpYData.EdbValMap
  211. edbInfoIdList := make([]int, 0)
  212. noDataEdbInfoIdList := make([]int, 0)
  213. valueList := make([]float64, 0)
  214. noDataEdbIdMap := make(map[int]int)
  215. // 所有的N值
  216. for _, n := range tmpYData.M {
  217. nMap[n] = n
  218. }
  219. // 基础指标
  220. {
  221. baseEdbInfId := edbList[0].EdbInfoId
  222. edbInfoIdList = append(edbInfoIdList, baseEdbInfId)
  223. tmpVal, ok := yDataMap[baseEdbInfId]
  224. valueList = append(valueList, tmpVal)
  225. if !ok || tmpVal == 0 {
  226. noDataEdbInfoIdList = append(noDataEdbInfoIdList, baseEdbInfId)
  227. noDataEdbIdMap[baseEdbInfId] = baseEdbInfId
  228. }
  229. }
  230. for _, futureGoodEdbInfo := range futureGoodEdbInfoList {
  231. tmpEdbInfId := futureGoodEdbInfo.FutureGoodEdbInfoId
  232. edbInfoIdList = append(edbInfoIdList, tmpEdbInfId)
  233. tmpVal, ok := yDataMap[tmpEdbInfId]
  234. valueList = append(valueList, tmpVal)
  235. if !ok || tmpVal == 0 {
  236. noDataEdbInfoIdList = append(noDataEdbInfoIdList, tmpEdbInfId)
  237. noDataEdbIdMap[tmpEdbInfId] = tmpEdbInfId
  238. }
  239. }
  240. //tmpYData.Value = valueList
  241. tmpYData.NoDataEdbList = noDataEdbInfoIdList
  242. yDataList[k] = tmpYData
  243. lenEdbId := len(edbInfoIdList)
  244. tmpMaxIndex := lenEdbId - 1 // 当前数据的最大x轴的下标
  245. for i := lenEdbId - 1; i >= 0; i-- {
  246. // 如果没有在无数据的指标列表中找到,那么就找到了最大x轴的下标
  247. if _, ok := noDataEdbIdMap[edbInfoIdList[i]]; !ok {
  248. // 如果最大x轴的下标 小于 当前下标,那么就重新赋值
  249. if maxIndex < i-1 {
  250. maxIndex = i - 1
  251. }
  252. break
  253. }
  254. tmpMaxIndex = i - 1
  255. }
  256. // 如果最大x轴的下标 小于 当前下标,那么就重新赋值
  257. if maxIndex < tmpMaxIndex {
  258. maxIndex = tmpMaxIndex
  259. }
  260. }
  261. //xEdbIdValue = xEdbIdValue[0:maxIndex]
  262. // 找出所有的N值,并进行正序排列
  263. nList := make([]int, 0)
  264. for _, n := range nMap {
  265. nList = append(nList, n)
  266. }
  267. sort.Slice(nList, func(i, j int) bool {
  268. return nList[i] < nList[j]
  269. })
  270. for k, v := range yDataList {
  271. if len(v.XEdbInfoIdList) >= maxIndex+1 {
  272. yDataList[k].XEdbInfoIdList = v.XEdbInfoIdList[0 : maxIndex+1]
  273. }
  274. if len(v.Value) >= maxIndex+1 {
  275. yDataList[k].Value = v.Value[0 : maxIndex+1]
  276. }
  277. }
  278. tmpXDataList, newYDataList, err := handleResultData(regionType, futureGoodEdbType, yDataList, futureGoodEdbInfoList, maxIndex)
  279. if err != nil {
  280. return
  281. }
  282. xDataList = append(xDataList, tmpXDataList...)
  283. yDataList = newYDataList
  284. return
  285. }
  286. // BarChartData 柱方图的数据处理
  287. func BarChartData(edbInfoMapping *models.ChartEdbInfoMapping, futureGoodMappingList []*future_good.FutureGoodEdbInfo, edbDataListMap map[int][]*models.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, regionType, latestDate string) (edbIdList []int, yDataList []models.YData, err error) {
  288. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  289. // 现货指标数据map
  290. baseEdbDataMap := make(map[string]float64)
  291. for edbInfoId, edbDataList := range edbDataListMap {
  292. if edbInfoId == edbInfoMapping.EdbInfoId {
  293. for _, edbData := range edbDataList {
  294. baseEdbDataMap[edbData.DataTime] = edbData.Value
  295. }
  296. }
  297. }
  298. // 期货指标数据map
  299. futureGoodEdbDataMap := make(map[int]map[string]float64)
  300. for edbInfoId, edbDataList := range edbDataListMap {
  301. if edbInfoId == edbInfoMapping.EdbInfoId {
  302. continue
  303. }
  304. edbDateData := make(map[string]float64)
  305. for _, edbData := range edbDataList {
  306. edbDateData[edbData.DataTime] = edbData.Value
  307. }
  308. futureGoodEdbDataMap[edbInfoId] = edbDateData
  309. }
  310. edbDataMap := make(map[int]map[string]float64)
  311. for edbInfoId, edbDataList := range edbDataListMap {
  312. edbDateData := make(map[string]float64)
  313. for _, edbData := range edbDataList {
  314. edbDateData[edbData.DataTime] = edbData.Value
  315. }
  316. edbDataMap[edbInfoId] = edbDateData
  317. }
  318. // edbIdList 指标展示顺序;x轴的指标顺序
  319. edbIdList = make([]int, 0)
  320. edbIdList = append(edbIdList, edbInfoMapping.EdbInfoId)
  321. for _, v := range futureGoodMappingList {
  322. edbIdList = append(edbIdList, v.FutureGoodEdbInfoId)
  323. }
  324. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  325. yDataList = make([]models.YData, 0) //y轴的数据列表
  326. for _, barChartInfoDate := range barChartInfoDateList {
  327. yDataMap := make(map[int]float64)
  328. var maxDate time.Time
  329. var findDateTime time.Time
  330. switch barChartInfoDate.Type {
  331. case 1: //最新值
  332. findDateTime = latestDateTime
  333. case 2: //近期几天
  334. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  335. case 3: // 固定日期
  336. //寻找固定日期的数据
  337. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  338. if tmpErr != nil {
  339. err = tmpErr
  340. return
  341. }
  342. findDateTime = tmpFindDateTime
  343. default:
  344. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  345. return
  346. }
  347. if findDateTime.IsZero() {
  348. err = errors.New("错误的日期")
  349. return
  350. }
  351. findDataList := make([]float64, 0) // 当前日期的数据值
  352. noDataIdList := make([]int, 0) // 没有数据的指标id
  353. noDataIdMap := make(map[int]int, 0) // 没有数据的指标map
  354. xEdbInfoIdList := make([]int, 0) // 当前数据的指标id列表
  355. // 现货指标
  356. realDateTime, findDataValue, isFind, tmpErr := GetNeedDateData(findDateTime, edbDataListMap[edbInfoMapping.EdbInfoId], baseEdbDataMap, futureGoodEdbDataMap)
  357. if tmpErr != nil {
  358. err = tmpErr
  359. return
  360. }
  361. findDataList = append(findDataList, findDataValue)
  362. yDataMap[edbInfoMapping.EdbInfoId] = findDataValue
  363. if isFind {
  364. maxDate = realDateTime
  365. } else {
  366. noDataIdList = append(noDataIdList, edbInfoMapping.EdbInfoId)
  367. noDataIdMap[edbInfoMapping.EdbInfoId] = edbInfoMapping.EdbInfoId
  368. }
  369. //currMonth := findDateTime.Month() // 当前月份
  370. //currYear := findDateTime.Year() // 当前年份
  371. // 用实际日期的月份作为基准,往前推12个月(2024-5-13 16:26:43修改)
  372. currMonth := realDateTime.Month() // 当前月份
  373. currYear := realDateTime.Year() // 当前年份
  374. xEdbInfoIdList = append(xEdbInfoIdList, edbInfoMapping.EdbInfoId)
  375. mList := make([]int, 0) // 间隔月份
  376. indexList := make([]int, 0)
  377. if regionType == `国内` {
  378. for i := currMonth + 1; i <= 12; i++ {
  379. indexList = append(indexList, int(i))
  380. mList = append(mList, int(i-currMonth))
  381. }
  382. for i := 1; i < int(currMonth); i++ {
  383. indexList = append(indexList, i)
  384. mList = append(mList, 12+i-int(currMonth))
  385. }
  386. } else {
  387. for i, v := range futureGoodMappingList {
  388. if v.FutureGoodEdbType == 2 {
  389. if i == 0 {
  390. continue
  391. }
  392. indexList = append(indexList, i)
  393. mList = append(mList, v.Month)
  394. } else {
  395. if v.Year > currYear || (v.Year == currYear && v.Month > int(currMonth)) {
  396. indexList = append(indexList, i)
  397. mList = append(mList, (v.Year-currYear)*12+v.Month-int(currMonth))
  398. }
  399. }
  400. }
  401. }
  402. for _, i := range indexList {
  403. futureGoodMapping := futureGoodMappingList[i] // 当前的期货指标
  404. //tmpRealDateTime, tmpFindDataValue, tmpIsFind, tmpErr := GetNeedDateData(findDateTime, edbDataListMap[futureGoodMapping.FutureGoodEdbInfoId], edbDataMap[futureGoodMapping.FutureGoodEdbInfoId])
  405. //if tmpErr != nil {
  406. // err = tmpErr
  407. // return
  408. //}
  409. //tmpRealDateTime := findDateTime // 按照配置找到的日期
  410. tmpRealDateTime := realDateTime // 实际现货的日期
  411. tmpFindDataValue, tmpIsFind := futureGoodEdbDataMap[futureGoodMapping.FutureGoodEdbInfoId][tmpRealDateTime.Format(utils.FormatDate)]
  412. yDataMap[futureGoodMapping.FutureGoodEdbInfoId] = tmpFindDataValue
  413. findDataList = append(findDataList, tmpFindDataValue)
  414. if tmpIsFind {
  415. if maxDate.IsZero() || maxDate.Before(tmpRealDateTime) {
  416. maxDate = tmpRealDateTime
  417. }
  418. } else {
  419. noDataIdList = append(noDataIdList, futureGoodMapping.FutureGoodEdbInfoId)
  420. noDataIdMap[futureGoodMapping.FutureGoodEdbInfoId] = futureGoodMapping.FutureGoodEdbInfoId
  421. }
  422. // 当前期货合约的指标
  423. xEdbInfoIdList = append(xEdbInfoIdList, futureGoodMapping.FutureGoodEdbInfoId)
  424. }
  425. yName := barChartInfoDate.Name
  426. yNameEn := barChartInfoDate.Name
  427. if yName == `` {
  428. if barChartInfoDate.Type == 2 {
  429. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  430. if barChartInfoDate.Value == 1 {
  431. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  432. } else {
  433. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  434. }
  435. } else {
  436. yName = maxDate.Format(utils.FormatDate)
  437. yNameEn = maxDate.Format(utils.FormatDate)
  438. }
  439. }
  440. yDate := "0000-00-00"
  441. if !maxDate.IsZero() {
  442. yDate = maxDate.Format(utils.FormatDate)
  443. }
  444. // 数据处理,将没有数据的下标,赋值平均值
  445. {
  446. hasDataIndexList := make([]int, 0)
  447. for dataK, edbInfoId := range xEdbInfoIdList {
  448. if _, ok := noDataIdMap[edbInfoId]; !ok { // 如果是没有数据的指标id
  449. hasDataIndexList = append(hasDataIndexList, dataK)
  450. }
  451. }
  452. lenHasDataIndex := len(hasDataIndexList)
  453. if lenHasDataIndex > 0 {
  454. for lenHasDataI := 1; lenHasDataI < lenHasDataIndex; lenHasDataI++ {
  455. perK := hasDataIndexList[lenHasDataI-1] //上一个有数据的指标下标
  456. currK := hasDataIndexList[lenHasDataI] //当前有数据的指标下标
  457. preVal := findDataList[perK] //上一个有数据的坐标的值
  458. currVal := findDataList[currK] //当前有数据的指标的值
  459. // 环差值
  460. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(currK - perK)))
  461. var tmpI int64
  462. // 将两个中间的数据做平均值补全
  463. for hcI := perK + 1; hcI < currK; hcI++ {
  464. tmpI++
  465. findDataList[hcI], _ = decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(tmpI))).RoundCeil(4).Float64()
  466. }
  467. }
  468. }
  469. }
  470. yDataList = append(yDataList, models.YData{
  471. Date: yDate,
  472. ConfigDate: realDateTime,
  473. Value: findDataList,
  474. NoDataEdbList: noDataIdList,
  475. XEdbInfoIdList: xEdbInfoIdList,
  476. Color: barChartInfoDate.Color,
  477. Name: yName,
  478. NameEn: yNameEn,
  479. EdbValMap: yDataMap,
  480. M: mList,
  481. })
  482. }
  483. return
  484. }
  485. // GetNeedDateData 获取合约内需要的日期数据
  486. func GetNeedDateData(needDateTime time.Time, dataList []*models.EdbDataList, edbDataMap map[string]float64, allEdbDataMap map[int]map[string]float64) (findDateTime time.Time, findDataValue float64, isFind bool, err error) {
  487. //dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  488. if len(dataList) <= 0 {
  489. // 没有数据的指标id
  490. return
  491. }
  492. //最早的日期
  493. minDateTime, err := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  494. if err != nil {
  495. return
  496. }
  497. // 该日期存在数据的期货指标的最小数量,目前是现货和期货各1个,总共2个
  498. maxCount := 1
  499. for tmpDateTime := needDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  500. tmpDate := tmpDateTime.Format(utils.FormatDate)
  501. tmpValue, ok := edbDataMap[tmpDate]
  502. if !ok {
  503. continue
  504. }
  505. // 该日期存在数据的指标数量
  506. count := 0
  507. for _, currEdbDataMap := range allEdbDataMap {
  508. _, tmpIsFind := currEdbDataMap[tmpDate]
  509. if tmpIsFind {
  510. count++
  511. if count >= maxCount {
  512. continue
  513. }
  514. }
  515. }
  516. // 该日期存在数据的期货指标数量小于2个,那么要继续往前找
  517. if count < maxCount {
  518. continue
  519. }
  520. //如果能找到数据,那么就返回
  521. // 数据为0,也直接返回,做无值处理
  522. if tmpValue == 0 {
  523. return
  524. }
  525. findDateTime, _ = time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  526. findDataValue = tmpValue
  527. isFind = true
  528. return
  529. }
  530. return
  531. }
  532. // FutureGoodChartInfoRefresh
  533. // @author Roc
  534. // @datetime 2023-2-2 18:44:46
  535. // @description 商品价格曲线图表刷新
  536. func FutureGoodChartInfoRefresh(chartInfoId int) (err error) {
  537. var errMsg string
  538. defer func() {
  539. if err != nil {
  540. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errMsg, 3)
  541. fmt.Println("ChartInfoRefresh Err:" + errMsg)
  542. }
  543. }()
  544. edbInfoMapping, err := models.GetEtaEdbChartEdbMapping(chartInfoId)
  545. if err != nil {
  546. errMsg = "获取需要刷新的ETA指标失败:Err:" + err.Error()
  547. return
  548. }
  549. // 获取期货指标
  550. futureGoodEdbInfoMapping, err := models.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  551. if err != nil {
  552. errMsg = "获取需要刷新的商品期货指标失败:Err:" + err.Error()
  553. return
  554. }
  555. // 获取期货指标以及期货数据
  556. futureGoodEdbInfoList, err := future_good.GetFutureGoodEdbInfoListByParentId(futureGoodEdbInfoMapping.EdbInfoId)
  557. if err != nil {
  558. return
  559. }
  560. // 批量刷新ETA指标
  561. err, _ = data.EdbInfoRefreshAllFromBase([]int{edbInfoMapping.EdbInfoId}, false)
  562. if err != nil {
  563. return
  564. }
  565. // 批量刷新期货指标
  566. err = FutureGoodEdbInfoRefreshAllFromBase(futureGoodEdbInfoList, false)
  567. if err != nil {
  568. return
  569. }
  570. return
  571. }
  572. // handleResultData 处理成最终的结果数据
  573. func handleResultData(regionType string, futureGoodEdbType int, yDataList []models.YData, futureGoodEdbInfoList []*future_good.FutureGoodEdbInfo, maxIndex int) (xDataList []models.XData, newYDataList []models.YData, err error) {
  574. xDataList = make([]models.XData, 0)
  575. newYDataList = yDataList
  576. if regionType == `国内` {
  577. for i := 1; i < 12; i++ {
  578. if i > maxIndex {
  579. break
  580. }
  581. xDataList = append(xDataList, models.XData{
  582. Name: fmt.Sprint("M+", i),
  583. NameEn: fmt.Sprint("M+", i),
  584. })
  585. }
  586. return
  587. }
  588. futureGoodEdbInfoMap := make(map[int]*future_good.FutureGoodEdbInfo)
  589. for _, v := range futureGoodEdbInfoList {
  590. futureGoodEdbInfoMap[v.FutureGoodEdbInfoId] = v
  591. }
  592. if futureGoodEdbType == 2 { // FutureGoodEdbType int `description:"指标类型,1:年月是固定的合约;2:只有M+N期的合约,未固定年月"`
  593. nList := []int{3, 15, 27}
  594. for _, i := range nList {
  595. xDataList = append(xDataList, models.XData{
  596. Name: fmt.Sprint("M+", i),
  597. NameEn: fmt.Sprint("M+", i),
  598. })
  599. }
  600. for yIndex, yData := range yDataList {
  601. newYDataList[yIndex].XEdbInfoIdList = []int{}
  602. newYDataList[yIndex].Value = []float64{}
  603. tmpNList := nList
  604. //当前的主力合约
  605. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, yData.XEdbInfoIdList[0])
  606. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[0])
  607. xEdbInfoIdList := yData.XEdbInfoIdList[1:]
  608. valIndex := 1
  609. needNum := 0
  610. for _, n := range tmpNList {
  611. if len(xEdbInfoIdList) > 0 {
  612. edbInfoId := xEdbInfoIdList[0]
  613. futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
  614. if !ok {
  615. err = errors.New("找不到指标")
  616. return
  617. }
  618. // 当前距离最早的日期相差的N数
  619. if futureGoodEdbInfo.Month == n {
  620. if needNum > 0 {
  621. currVal := yData.Value[valIndex]
  622. preVal := yData.Value[valIndex-1]
  623. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  624. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  625. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  626. // 赋值平均值
  627. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  628. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  629. }
  630. }
  631. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, edbInfoId)
  632. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  633. valIndex++
  634. needNum = 0
  635. if len(xEdbInfoIdList) > 0 {
  636. xEdbInfoIdList = xEdbInfoIdList[1:]
  637. }
  638. } else {
  639. needNum++
  640. }
  641. }
  642. }
  643. }
  644. maxI := 0
  645. for _, yData := range newYDataList {
  646. lenEdb := len(yData.XEdbInfoIdList)
  647. for i := 0; i < lenEdb; i++ {
  648. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  649. if maxI < i {
  650. maxI = i
  651. }
  652. }
  653. }
  654. }
  655. xDataList = xDataList[0:maxI]
  656. for yIndex, yData := range newYDataList {
  657. if len(yData.XEdbInfoIdList) > maxI+1 {
  658. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  659. }
  660. if len(yData.Value) > maxI+1 {
  661. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  662. }
  663. }
  664. return
  665. }
  666. nMap := make(map[int]int)
  667. for _, v := range yDataList {
  668. findDateTime := v.ConfigDate
  669. currMonth := findDateTime.Month() // 当前月份
  670. currYear := findDateTime.Year() // 当前年份
  671. //v.XEdbInfoIdList
  672. for edbInfoIndex, edbInfoId := range v.XEdbInfoIdList {
  673. // 第一个不处理
  674. if edbInfoIndex == 0 {
  675. continue
  676. }
  677. futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
  678. if !ok {
  679. err = errors.New("找不到指标")
  680. return
  681. }
  682. n := (futureGoodEdbInfo.Year-currYear)*12 + futureGoodEdbInfo.Month - int(currMonth)
  683. nMap[n] = n
  684. }
  685. }
  686. // 找出所有的N值,并进行正序排列
  687. nList := make([]int, 0)
  688. for _, n := range nMap {
  689. nList = append(nList, n)
  690. }
  691. sort.Slice(nList, func(i, j int) bool {
  692. return nList[i] < nList[j]
  693. })
  694. //prevMonth := 1
  695. for _, n := range nList {
  696. xDataList = append(xDataList, models.XData{
  697. Name: fmt.Sprint("M+", n),
  698. NameEn: fmt.Sprint("M+", n),
  699. })
  700. }
  701. for yIndex, yData := range yDataList {
  702. newYDataList[yIndex].XEdbInfoIdList = []int{}
  703. newYDataList[yIndex].Value = []float64{}
  704. tmpNList := nList
  705. //当前的主力合约
  706. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, yData.XEdbInfoIdList[0])
  707. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[0])
  708. xEdbInfoIdList := yData.XEdbInfoIdList[1:]
  709. currDataTime := yData.ConfigDate
  710. valIndex := 1
  711. needNum := 0
  712. for _, n := range tmpNList {
  713. if len(xEdbInfoIdList) > 0 {
  714. edbInfoId := xEdbInfoIdList[0]
  715. futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
  716. if !ok {
  717. err = errors.New("找不到指标")
  718. return
  719. }
  720. // 当前距离最早的日期相差的N数
  721. divMonth := (futureGoodEdbInfo.Year-currDataTime.Year())*12 + (futureGoodEdbInfo.Month - int(currDataTime.Month()))
  722. if divMonth == n {
  723. if needNum > 0 {
  724. currVal := yData.Value[valIndex]
  725. preVal := yData.Value[valIndex-1]
  726. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  727. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  728. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  729. // 赋值平均值
  730. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  731. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  732. }
  733. }
  734. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, edbInfoId)
  735. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  736. valIndex++
  737. needNum = 0
  738. if len(xEdbInfoIdList) > 0 {
  739. xEdbInfoIdList = xEdbInfoIdList[1:]
  740. }
  741. } else {
  742. needNum++
  743. }
  744. }
  745. }
  746. }
  747. maxI := 0
  748. for _, yData := range newYDataList {
  749. lenEdb := len(yData.XEdbInfoIdList)
  750. for i := 0; i < lenEdb; i++ {
  751. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  752. if maxI < i {
  753. maxI = i
  754. }
  755. }
  756. }
  757. }
  758. xDataList = xDataList[0:maxI]
  759. for yIndex, yData := range newYDataList {
  760. if len(yData.XEdbInfoIdList) > maxI+1 {
  761. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  762. }
  763. if len(yData.Value) > maxI+1 {
  764. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  765. }
  766. }
  767. return
  768. }
  769. // getFutureGoodEdbInfoList 获取适用的指标列表
  770. func getFutureGoodEdbInfoList(latestDateTime time.Time, tmpFutureGoodEdbInfoList []*future_good.FutureGoodEdbInfo, barChartInfoDateList []data_manage.BarChartInfoDateReq) (earliestDateTime time.Time, futureGoodEdbInfoList []*future_good.FutureGoodEdbInfo, err error) {
  771. maxM := 36 //最大36期合约
  772. futureGoodEdbInfoList = make([]*future_good.FutureGoodEdbInfo, 0)
  773. earliestDateTime = latestDateTime // 数据的最早日期,目的是为了找出最早的合约
  774. for _, barChartInfoDate := range barChartInfoDateList {
  775. var findDateTime time.Time
  776. switch barChartInfoDate.Type {
  777. case 1: //最新值
  778. findDateTime = latestDateTime
  779. case 2: //近期几天
  780. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  781. case 3: // 固定日期
  782. //寻找固定日期的数据
  783. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  784. if tmpErr != nil {
  785. err = tmpErr
  786. return
  787. }
  788. findDateTime = tmpFindDateTime
  789. default:
  790. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  791. return
  792. }
  793. if findDateTime.IsZero() {
  794. err = errors.New("错误的日期")
  795. return
  796. }
  797. if findDateTime.Before(earliestDateTime) {
  798. earliestDateTime = findDateTime
  799. }
  800. }
  801. for _, v := range tmpFutureGoodEdbInfoList {
  802. if v.RegionType == `国内` {
  803. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  804. continue
  805. }
  806. //海外的连续日期,目前
  807. if v.FutureGoodEdbType == 2 {
  808. if v.Month <= maxM {
  809. //addMonth := int(earliestDateTime.Month()) + v.Month
  810. //v.Year = earliestDateTime.Year() + addMonth/12
  811. //realMonth := addMonth % 12
  812. //if realMonth == 0 {
  813. // realMonth = 12
  814. //}
  815. //v.Month = realMonth
  816. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  817. }
  818. continue
  819. }
  820. if v.Year < earliestDateTime.Year() {
  821. continue
  822. }
  823. // 小于等于当前年,那么就肯定是ok的
  824. if v.Year <= earliestDateTime.Year() {
  825. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  826. continue
  827. }
  828. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  829. if (v.Year-earliestDateTime.Year())*12+(v.Month-int(earliestDateTime.Month())) <= maxM {
  830. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  831. continue
  832. }
  833. }
  834. return
  835. }