chart_info.go 35 KB

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