chart_info.go 36 KB

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