chart_info.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. err = fmt.Errorf("查不到期货指标,GetFutureGoodEdbInfo err: %v", err)
  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 = fmt.Errorf("获取期货指标 %d 的日期来源的指标信息失败,错误:%s", zlFutureGoodEdbInfo.DateSourceId, tmpErr.Error())
  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. err = fmt.Errorf("获取期货指标列表失败,getFutureGoodEdbInfoList 错误:%s", err.Error())
  137. return
  138. }
  139. futureGoodMappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  140. for k, v := range futureGoodEdbInfoList {
  141. newMappingInfo := &data_manage.ChartEdbInfoMapping{
  142. EdbInfoId: v.FutureGoodEdbInfoId,
  143. SourceName: v.Exchange,
  144. Source: 0,
  145. EdbCode: v.FutureGoodEdbCode,
  146. EdbName: v.FutureGoodEdbName,
  147. EdbAliasName: v.FutureGoodEdbName,
  148. EdbNameEn: v.FutureGoodEdbNameEn,
  149. EdbType: baseEdbInfoMapping.EdbType,
  150. Frequency: baseEdbInfoMapping.Frequency,
  151. FrequencyEn: baseEdbInfoMapping.FrequencyEn,
  152. Unit: baseEdbInfoMapping.Unit,
  153. UnitEn: baseEdbInfoMapping.UnitEn,
  154. StartDate: v.StartDate,
  155. EndDate: v.EndDate,
  156. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  157. ChartEdbMappingId: v.FutureGoodEdbInfoId,
  158. ChartInfoId: baseEdbInfoMapping.ChartInfoId,
  159. MaxData: v.MaxValue,
  160. MinData: v.MinValue,
  161. IsOrder: baseEdbInfoMapping.IsOrder,
  162. IsAxis: baseEdbInfoMapping.IsAxis,
  163. EdbInfoType: baseEdbInfoMapping.EdbInfoType,
  164. EdbInfoCategoryType: baseEdbInfoMapping.EdbInfoCategoryType,
  165. LeadValue: baseEdbInfoMapping.LeadValue,
  166. LeadUnit: baseEdbInfoMapping.LeadUnit,
  167. LeadUnitEn: baseEdbInfoMapping.LeadUnitEn,
  168. ChartStyle: baseEdbInfoMapping.ChartStyle,
  169. ChartColor: baseEdbInfoMapping.ChartColor,
  170. PredictChartColor: baseEdbInfoMapping.PredictChartColor,
  171. ChartWidth: baseEdbInfoMapping.ChartWidth,
  172. ChartType: baseEdbInfoMapping.ChartType,
  173. LatestDate: v.LatestDate.Format(utils.FormatDateTime),
  174. LatestValue: v.LatestValue,
  175. UniqueCode: futureGoodEdbInfoMapping.UniqueCode + strconv.Itoa(k),
  176. MinValue: v.MinValue,
  177. MaxValue: v.MaxValue,
  178. DataList: nil,
  179. IsNullData: false,
  180. }
  181. futureGoodMappingList = append(futureGoodMappingList, newMappingInfo)
  182. tmp := data_manage.BarChartInfoEdbItemReq{
  183. EdbInfoId: newMappingInfo.EdbInfoId,
  184. Name: fmt.Sprint("M+", v.Month),
  185. NameEn: fmt.Sprint("M+", v.Month),
  186. Source: newMappingInfo.Source,
  187. }
  188. // 判断如果有配置,需要替换为配置内容
  189. xItem, ok := xDataItemMap[k]
  190. if ok && xItem.Name != "" {
  191. tmp.Name = xItem.Name
  192. tmp.NameEn = xItem.NameEn
  193. tmp.IsHide = xItem.IsHide
  194. }
  195. barConfigEdbInfoIdList = append(barConfigEdbInfoIdList, tmp)
  196. }
  197. if regionType == `海外` {
  198. zlFutureGoodEdbMapping := &data_manage.ChartEdbInfoMapping{
  199. EdbInfoId: zlFutureGoodEdbInfo.FutureGoodEdbInfoId,
  200. SourceName: zlFutureGoodEdbInfo.Exchange,
  201. Source: 0,
  202. EdbCode: zlFutureGoodEdbInfo.FutureGoodEdbCode,
  203. EdbName: zlFutureGoodEdbInfo.FutureGoodEdbName,
  204. EdbAliasName: zlFutureGoodEdbInfo.FutureGoodEdbName,
  205. EdbNameEn: zlFutureGoodEdbInfo.FutureGoodEdbNameEn,
  206. EdbType: baseEdbInfoMapping.EdbType,
  207. Frequency: baseEdbInfoMapping.Frequency,
  208. FrequencyEn: baseEdbInfoMapping.FrequencyEn,
  209. Unit: baseEdbInfoMapping.Unit,
  210. UnitEn: baseEdbInfoMapping.UnitEn,
  211. StartDate: zlFutureGoodEdbInfo.StartDate,
  212. EndDate: zlFutureGoodEdbInfo.EndDate,
  213. ModifyTime: zlFutureGoodEdbInfo.ModifyTime.Format(utils.FormatDateTime),
  214. ChartEdbMappingId: zlFutureGoodEdbInfo.FutureGoodEdbInfoId,
  215. ChartInfoId: baseEdbInfoMapping.ChartInfoId,
  216. MaxData: zlFutureGoodEdbInfo.MaxValue,
  217. MinData: zlFutureGoodEdbInfo.MinValue,
  218. IsOrder: baseEdbInfoMapping.IsOrder,
  219. IsAxis: baseEdbInfoMapping.IsAxis,
  220. EdbInfoType: baseEdbInfoMapping.EdbInfoType,
  221. EdbInfoCategoryType: baseEdbInfoMapping.EdbInfoCategoryType,
  222. LeadValue: baseEdbInfoMapping.LeadValue,
  223. LeadUnit: baseEdbInfoMapping.LeadUnit,
  224. LeadUnitEn: baseEdbInfoMapping.LeadUnitEn,
  225. ChartStyle: baseEdbInfoMapping.ChartStyle,
  226. ChartColor: baseEdbInfoMapping.ChartColor,
  227. PredictChartColor: baseEdbInfoMapping.PredictChartColor,
  228. ChartWidth: baseEdbInfoMapping.ChartWidth,
  229. ChartType: baseEdbInfoMapping.ChartType,
  230. LatestDate: zlFutureGoodEdbInfo.LatestDate.Format(utils.FormatDateTime),
  231. LatestValue: zlFutureGoodEdbInfo.LatestValue,
  232. UniqueCode: futureGoodEdbInfoMapping.UniqueCode + strconv.Itoa(0),
  233. MinValue: zlFutureGoodEdbInfo.MinValue,
  234. MaxValue: zlFutureGoodEdbInfo.MaxValue,
  235. DataList: nil,
  236. IsNullData: false,
  237. }
  238. edbList = append(edbList, zlFutureGoodEdbMapping)
  239. edbList = append(edbList, futureGoodMappingList...)
  240. } else {
  241. edbList = append(edbList, futureGoodMappingList...)
  242. }
  243. if !needData {
  244. return
  245. }
  246. // 获取数据
  247. // 现货数据
  248. for _, edbInfoMapping := range edbInfoMappingList {
  249. dataList := make([]*data_manage.EdbDataList, 0)
  250. dataList, err = data_manage.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, startDate, endDate)
  251. if err != nil {
  252. return
  253. }
  254. edbDataListMap[edbInfoMapping.EdbInfoId] = dataList
  255. // todo item
  256. //item.DataList = dataList
  257. }
  258. futureEdbInfoIds := make([]int, 0)
  259. for _, v := range futureGoodMappingList {
  260. futureEdbInfoIds = append(futureEdbInfoIds, v.EdbInfoId)
  261. }
  262. tmpDataListMap := make(map[int][]*future_good2.FutureGoodEdbData)
  263. if len(futureEdbInfoIds) > 0 {
  264. // 期货数据
  265. tmpDataList, tmpErr := future_good2.GetFutureGoodEdbDataListByIdsAndDate(futureEdbInfoIds, startDate, endDate)
  266. if tmpErr != nil {
  267. err = tmpErr
  268. return
  269. }
  270. for _, v := range tmpDataList {
  271. if _, ok := tmpDataListMap[v.FutureGoodEdbInfoId]; !ok {
  272. tmpDataListMap[v.FutureGoodEdbInfoId] = make([]*future_good2.FutureGoodEdbData, 0)
  273. }
  274. tmpDataListMap[v.FutureGoodEdbInfoId] = append(tmpDataListMap[v.FutureGoodEdbInfoId], v)
  275. }
  276. }
  277. for _, v := range futureGoodMappingList {
  278. dataList := make([]*data_manage.EdbDataList, 0)
  279. tmpDataList, ok := tmpDataListMap[v.EdbInfoId]
  280. if !ok {
  281. err = fmt.Errorf("期货数据不存在 FutureGoodEdbInfoId: %d", v.EdbInfoId)
  282. return
  283. }
  284. for _, tmpData := range tmpDataList {
  285. dataList = append(dataList, &data_manage.EdbDataList{
  286. EdbDataId: tmpData.FutureGoodEdbDataId,
  287. EdbInfoId: tmpData.FutureGoodEdbInfoId,
  288. DataTime: tmpData.DataTime.Format(utils.FormatDate),
  289. DataTimestamp: tmpData.DataTimestamp,
  290. Value: tmpData.Close,
  291. })
  292. }
  293. edbDataListMap[v.EdbInfoId] = dataList
  294. v.DataList = dataList
  295. }
  296. xEdbIdValue, yDataList, err = BarChartData(baseEdbInfoMapping, edbInfoMappingList, futureGoodEdbInfoList, edbDataListMap, barChartInfoDateList, regionType, baseEdbInfoMapping.EndDate)
  297. if len(barChartInfoConf.XDataList) > 0 {
  298. xDataList = barChartInfoConf.XDataList
  299. } else {
  300. for _, v := range edbInfoMappingList {
  301. xDataList = append(xDataList, data_manage.XData{
  302. Name: v.EdbName,
  303. NameEn: v.EdbNameEn,
  304. IsHide: 0,
  305. })
  306. }
  307. }
  308. //yDataList =make([]data_manage.YData,0)
  309. //data_manage.YData{
  310. // Date: "",
  311. // Color: "",
  312. // Name: "",
  313. // NameEn: "",
  314. // Value: nil,
  315. // NoDataEdbList: nil,
  316. // XEdbInfoIdList: nil,
  317. //}
  318. futureGoodEdbInfoIndexMap := make(map[int]int)
  319. for index, v := range futureGoodEdbInfoList {
  320. futureGoodEdbInfoIndexMap[v.FutureGoodEdbInfoId] = index
  321. }
  322. nMap := make(map[int]int)
  323. maxIndex := 0 // 最大x轴的下标
  324. for k, tmpYData := range yDataList {
  325. yDataMap := tmpYData.EdbValMap
  326. edbInfoIdList := make([]int, 0)
  327. noDataEdbInfoIdList := make([]int, 0)
  328. valueList := make([]float64, 0)
  329. noDataEdbIdMap := make(map[int]int)
  330. // 所有的N值
  331. for _, n := range tmpYData.M {
  332. nMap[n] = n
  333. }
  334. // 基础指标
  335. baseEdbIds := make([]int, 0)
  336. for _, tmp := range edbInfoMappingList {
  337. edbInfoIdList = append(edbInfoIdList, tmp.EdbInfoId)
  338. baseEdbIds = append(baseEdbIds, tmp.EdbInfoId)
  339. tmpVal, ok := yDataMap[tmp.EdbInfoId]
  340. valueList = append(valueList, tmpVal)
  341. if !ok || tmpVal == 0 {
  342. noDataEdbInfoIdList = append(noDataEdbInfoIdList, tmp.EdbInfoId)
  343. noDataEdbIdMap[tmp.EdbInfoId] = tmp.EdbInfoId
  344. }
  345. }
  346. for _, futureGoodEdbInfo := range futureGoodEdbInfoList {
  347. tmpEdbInfId := futureGoodEdbInfo.FutureGoodEdbInfoId
  348. edbInfoIdList = append(edbInfoIdList, tmpEdbInfId)
  349. tmpVal, ok := yDataMap[tmpEdbInfId]
  350. valueList = append(valueList, tmpVal)
  351. if !ok || tmpVal == 0 {
  352. noDataEdbInfoIdList = append(noDataEdbInfoIdList, tmpEdbInfId)
  353. noDataEdbIdMap[tmpEdbInfId] = tmpEdbInfId
  354. }
  355. }
  356. //tmpYData.Value = valueList
  357. tmpYData.NoDataEdbList = noDataEdbInfoIdList
  358. yDataList[k] = tmpYData
  359. lenEdbId := len(edbInfoIdList)
  360. tmpMaxIndex := lenEdbId - 1 // 当前数据的最大x轴的下标
  361. for i := lenEdbId - 1; i >= 0; i-- {
  362. // 如果没有在无数据的指标列表中找到,那么就找到了最大x轴的下标
  363. if _, ok := noDataEdbIdMap[edbInfoIdList[i]]; !ok || utils.InArrayByInt(baseEdbIds, edbInfoIdList[i]) { //以往的逻辑是碰到第一个无数据的期货指标,那后续的月份都是无数据的,因此需要特殊处理,改成多个现货指标之后需要排除无值的现货指标
  364. // 如果最大x轴的下标 小于 当前下标,那么就重新赋值,
  365. if maxIndex < i-1 {
  366. maxIndex = i - 1
  367. }
  368. break
  369. }
  370. tmpMaxIndex = i - 1
  371. }
  372. // 如果最大x轴的下标 小于 当前下标,那么就重新赋值
  373. if maxIndex < tmpMaxIndex {
  374. maxIndex = tmpMaxIndex
  375. }
  376. }
  377. //xEdbIdValue = xEdbIdValue[0:maxIndex]
  378. // 找出所有的N值,并进行正序排列
  379. nList := make([]int, 0)
  380. for _, n := range nMap {
  381. nList = append(nList, n)
  382. }
  383. sort.Slice(nList, func(i, j int) bool {
  384. return nList[i] < nList[j]
  385. })
  386. for k, v := range yDataList {
  387. if len(v.XEdbInfoIdList) >= maxIndex+1 {
  388. yDataList[k].XEdbInfoIdList = v.XEdbInfoIdList[0 : maxIndex+1]
  389. }
  390. if len(v.Value) >= maxIndex+1 {
  391. yDataList[k].Value = v.Value[0 : maxIndex+1]
  392. }
  393. }
  394. baseEdbLen := len(edbInfoMappingList)
  395. tmpXDataList, newYDataList, err := handleResultData(regionType, futureGoodEdbType, baseEdbLen, yDataList, futureGoodEdbInfoList, maxIndex)
  396. if err != nil {
  397. return
  398. }
  399. if len(barChartInfoConf.XDataList) == 0 {
  400. xDataList = append(xDataList, tmpXDataList...)
  401. }
  402. yDataList = newYDataList
  403. return
  404. }
  405. // BarChartData 获取数据
  406. 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) {
  407. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  408. // 现货指标数据map
  409. baseEdbDataMap := make(map[int]map[string]float64)
  410. edbInfoMappingMap := make(map[int]struct{})
  411. for _, v := range edbInfoMappingList {
  412. edbInfoMappingMap[v.EdbInfoId] = struct{}{}
  413. }
  414. for edbInfoId, edbDataList := range edbDataListMap {
  415. if _, ok1 := edbInfoMappingMap[edbInfoId]; ok1 {
  416. edbDateData := make(map[string]float64)
  417. for _, edbData := range edbDataList {
  418. edbDateData[edbData.DataTime] = edbData.Value
  419. }
  420. baseEdbDataMap[edbInfoId] = edbDateData
  421. }
  422. }
  423. edbDataMap := 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. edbDataMap[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([]data_manage.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], edbDataMap)
  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 := edbDataMap[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, data_manage.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 []data_manage.YData, futureGoodEdbInfoList []*future_good2.FutureGoodEdbInfo, maxIndex int) (xDataList []data_manage.XData, newYDataList []data_manage.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. xDataList = append(xDataList, data_manage.XData{
  628. Name: fmt.Sprint("M+", i),
  629. NameEn: fmt.Sprint("M+", i),
  630. })
  631. }
  632. return
  633. }
  634. futureGoodEdbInfoMap := make(map[int]*future_good2.FutureGoodEdbInfo)
  635. for _, v := range futureGoodEdbInfoList {
  636. futureGoodEdbInfoMap[v.FutureGoodEdbInfoId] = v
  637. }
  638. if futureGoodEdbType == 2 { // FutureGoodEdbType int `description:"指标类型,1:年月是固定的合约;2:只有M+N期的合约,未固定年月"`
  639. nList := []int{3, 15, 27}
  640. for _, i := range nList {
  641. xDataList = append(xDataList, data_manage.XData{
  642. Name: fmt.Sprint("M+", i),
  643. NameEn: fmt.Sprint("M+", i),
  644. })
  645. }
  646. for yIndex, yData := range yDataList {
  647. newYDataList[yIndex].XEdbInfoIdList = []int{}
  648. newYDataList[yIndex].Value = []float64{}
  649. tmpNList := nList
  650. //当前的主力合约
  651. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, yData.XEdbInfoIdList[0:baseEdbLen]...)
  652. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[0:baseEdbLen]...)
  653. xEdbInfoIdList := yData.XEdbInfoIdList[baseEdbLen:]
  654. valIndex := baseEdbLen
  655. needNum := 0
  656. for _, n := range tmpNList {
  657. if len(xEdbInfoIdList) > 0 {
  658. edbInfoId := xEdbInfoIdList[0]
  659. futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
  660. if !ok {
  661. err = errors.New("找不到指标")
  662. return
  663. }
  664. // 当前距离最早的日期相差的N数
  665. if futureGoodEdbInfo.Month == n {
  666. if needNum > 0 {
  667. currVal := yData.Value[valIndex]
  668. preVal := yData.Value[valIndex-1]
  669. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  670. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  671. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  672. // 赋值平均值
  673. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  674. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  675. }
  676. }
  677. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, edbInfoId)
  678. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  679. valIndex++
  680. needNum = 0
  681. if len(xEdbInfoIdList) > 0 {
  682. xEdbInfoIdList = xEdbInfoIdList[1:]
  683. }
  684. } else {
  685. needNum++
  686. }
  687. }
  688. }
  689. }
  690. maxI := 0
  691. for _, yData := range newYDataList {
  692. lenEdb := len(yData.XEdbInfoIdList)
  693. for i := 0; i < lenEdb; i++ {
  694. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  695. if maxI < i {
  696. maxI = i
  697. }
  698. }
  699. }
  700. }
  701. xDataList = xDataList[0:maxI]
  702. for yIndex, yData := range newYDataList {
  703. if len(yData.XEdbInfoIdList) > maxI+1 {
  704. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  705. }
  706. if len(yData.Value) > maxI+1 {
  707. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  708. }
  709. }
  710. return
  711. }
  712. nMap := make(map[int]int)
  713. for _, v := range yDataList {
  714. findDateTime := v.ConfigDate
  715. currMonth := findDateTime.Month() // 当前月份
  716. currYear := findDateTime.Year() // 当前年份
  717. //v.XEdbInfoIdList
  718. for edbInfoIndex, edbInfoId := range v.XEdbInfoIdList {
  719. // 现货指标不处理
  720. if edbInfoIndex <= baseEdbLen-1 {
  721. continue
  722. }
  723. futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
  724. if !ok {
  725. err = errors.New("找不到指标")
  726. return
  727. }
  728. n := (futureGoodEdbInfo.Year-currYear)*12 + futureGoodEdbInfo.Month - int(currMonth)
  729. nMap[n] = n
  730. }
  731. }
  732. // 找出所有的N值,并进行正序排列
  733. nList := make([]int, 0)
  734. for _, n := range nMap {
  735. nList = append(nList, n)
  736. }
  737. sort.Slice(nList, func(i, j int) bool {
  738. return nList[i] < nList[j]
  739. })
  740. //prevMonth := 1
  741. for _, n := range nList {
  742. xDataList = append(xDataList, data_manage.XData{
  743. Name: fmt.Sprint("M+", n),
  744. NameEn: fmt.Sprint("M+", n),
  745. })
  746. }
  747. for yIndex, yData := range yDataList {
  748. newYDataList[yIndex].XEdbInfoIdList = []int{}
  749. newYDataList[yIndex].Value = []float64{}
  750. tmpNList := nList
  751. //当前的主力合约
  752. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, yData.XEdbInfoIdList[0:baseEdbLen]...)
  753. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[0:baseEdbLen]...)
  754. xEdbInfoIdList := yData.XEdbInfoIdList[baseEdbLen:]
  755. currDataTime := yData.ConfigDate
  756. valIndex := baseEdbLen
  757. needNum := 0
  758. for _, n := range tmpNList {
  759. if len(xEdbInfoIdList) > 0 {
  760. edbInfoId := xEdbInfoIdList[0]
  761. futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
  762. if !ok {
  763. err = errors.New("找不到指标")
  764. return
  765. }
  766. // 当前距离最早的日期相差的N数
  767. divMonth := (futureGoodEdbInfo.Year-currDataTime.Year())*12 + (futureGoodEdbInfo.Month - int(currDataTime.Month()))
  768. if divMonth == n {
  769. if needNum > 0 {
  770. currVal := yData.Value[valIndex]
  771. preVal := yData.Value[valIndex-1]
  772. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  773. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  774. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  775. // 赋值平均值
  776. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  777. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  778. }
  779. }
  780. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, edbInfoId)
  781. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  782. valIndex++
  783. needNum = 0
  784. if len(xEdbInfoIdList) > 0 {
  785. xEdbInfoIdList = xEdbInfoIdList[1:]
  786. }
  787. } else {
  788. needNum++
  789. }
  790. }
  791. }
  792. }
  793. maxI := 0
  794. for _, yData := range newYDataList {
  795. lenEdb := len(yData.XEdbInfoIdList)
  796. for i := 0; i < lenEdb; i++ {
  797. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  798. if maxI < i {
  799. maxI = i
  800. }
  801. }
  802. }
  803. }
  804. xDataList = xDataList[0:maxI]
  805. for yIndex, yData := range newYDataList {
  806. if len(yData.XEdbInfoIdList) > maxI+1 {
  807. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  808. }
  809. if len(yData.Value) > maxI+1 {
  810. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  811. }
  812. }
  813. return
  814. }
  815. // getFutureGoodEdbInfoList 获取适用的指标列表
  816. func getFutureGoodEdbInfoList(latestDateTime time.Time, tmpFutureGoodEdbInfoList []*future_good2.FutureGoodEdbInfo, barChartInfoDateList []data_manage.BarChartInfoDateReq) (earliestDateTime time.Time, futureGoodEdbInfoList []*future_good2.FutureGoodEdbInfo, err error) {
  817. maxM := 36 //最大36期合约
  818. futureGoodEdbInfoList = make([]*future_good2.FutureGoodEdbInfo, 0)
  819. earliestDateTime = latestDateTime // 数据的最早日期,目的是为了找出最早的合约
  820. for _, barChartInfoDate := range barChartInfoDateList {
  821. var findDateTime time.Time
  822. switch barChartInfoDate.Type {
  823. case 1: //最新值
  824. findDateTime = latestDateTime
  825. case 2: //近期几天
  826. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  827. case 3: // 固定日期
  828. //寻找固定日期的数据
  829. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  830. if tmpErr != nil {
  831. err = tmpErr
  832. return
  833. }
  834. findDateTime = tmpFindDateTime
  835. default:
  836. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  837. return
  838. }
  839. if findDateTime.IsZero() {
  840. err = errors.New("错误的日期")
  841. return
  842. }
  843. if findDateTime.Before(earliestDateTime) {
  844. earliestDateTime = findDateTime
  845. }
  846. }
  847. for _, v := range tmpFutureGoodEdbInfoList {
  848. if v.RegionType == `国内` {
  849. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  850. continue
  851. }
  852. //海外的连续日期,目前
  853. if v.FutureGoodEdbType == 2 {
  854. if v.Month <= maxM {
  855. //addMonth := int(earliestDateTime.Month()) + v.Month
  856. //v.Year = earliestDateTime.Year() + addMonth/12
  857. //realMonth := addMonth % 12
  858. //if realMonth == 0 {
  859. // realMonth = 12
  860. //}
  861. //v.Month = realMonth
  862. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  863. }
  864. continue
  865. }
  866. if v.Year < earliestDateTime.Year() {
  867. continue
  868. }
  869. // 小于等于当前年,那么就肯定是ok的
  870. if v.Year <= earliestDateTime.Year() {
  871. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  872. continue
  873. }
  874. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  875. if (v.Year-earliestDateTime.Year())*12+(v.Month-int(earliestDateTime.Month())) <= maxM {
  876. futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
  877. continue
  878. }
  879. }
  880. return
  881. }
  882. // GetNeedDateData 获取合约内需要的日期数据
  883. 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) {
  884. //dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  885. if len(dataList) <= 0 {
  886. // 没有数据的指标id
  887. return
  888. }
  889. //最早的日期
  890. minDateTime, err := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  891. if err != nil {
  892. return
  893. }
  894. // 该日期存在数据的期货指标的最小数量,目前是现货和期货各1个,总共2个
  895. maxCount := 1
  896. for tmpDateTime := needDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  897. tmpDate := tmpDateTime.Format(utils.FormatDate)
  898. tmpValue, ok := edbDataMap[tmpDate]
  899. if !ok {
  900. continue
  901. }
  902. // 该日期存在数据的指标数量
  903. count := 0
  904. for _, currEdbDataMap := range allEdbDataMap {
  905. _, tmpIsFind := currEdbDataMap[tmpDate]
  906. if tmpIsFind {
  907. count++
  908. if count >= maxCount {
  909. continue
  910. }
  911. }
  912. }
  913. // 该日期存在数据的期货指标数量小于2个,那么要继续往前找
  914. if count < maxCount {
  915. continue
  916. }
  917. //如果能找到数据,那么就返回
  918. // 数据为0,也直接返回,做无值处理
  919. if tmpValue == 0 {
  920. return
  921. }
  922. findDateTime, _ = time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  923. findDataValue = tmpValue
  924. isFind = true
  925. return
  926. }
  927. return
  928. }
  929. // FutureGoodChartInfoRefresh
  930. // @author Roc
  931. // @datetime 2023-2-2 18:44:46
  932. // @description 商品价格曲线图表刷新
  933. func FutureGoodChartInfoRefresh(chartInfoId int) (err error) {
  934. var errMsg string
  935. defer func() {
  936. if err != nil {
  937. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errMsg, 3)
  938. fmt.Println("ChartInfoRefresh Err:" + errMsg)
  939. }
  940. }()
  941. edbInfoMappingList, err := data_manage.GetEtaEdbChartEdbMappingList(chartInfoId)
  942. if err != nil {
  943. errMsg = "获取需要刷新的ETA指标失败:Err:" + err.Error()
  944. return
  945. }
  946. edbInfoIds := make([]int, 0)
  947. for _, edbInfoMapping := range edbInfoMappingList {
  948. edbInfoIds = append(edbInfoIds, edbInfoMapping.EdbInfoId)
  949. }
  950. // 获取期货指标
  951. futureGoodEdbInfoMapping, err := data_manage.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  952. if err != nil {
  953. errMsg = "获取需要刷新的商品期货指标失败:Err:" + err.Error()
  954. return
  955. }
  956. // 获取期货指标以及期货数据
  957. futureGoodEdbInfoList, err := future_good2.GetFutureGoodEdbInfoListByParentId(futureGoodEdbInfoMapping.EdbInfoId)
  958. if err != nil {
  959. return
  960. }
  961. // 批量刷新ETA指标
  962. err, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
  963. if err != nil {
  964. return
  965. }
  966. // 批量刷新期货指标
  967. err = FutureGoodEdbInfoRefreshAllFromBase(futureGoodEdbInfoList, false)
  968. if err != nil {
  969. return
  970. }
  971. resp, err := RefreshEdbRelation(chartInfoId)
  972. if err != nil {
  973. return
  974. }
  975. if resp.Ret != 200 {
  976. err = fmt.Errorf("刷新利润曲线图表数据失败:Err:%s", resp.Msg)
  977. return
  978. }
  979. return
  980. }