chart_info.go 31 KB

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