chart_info.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. continue
  284. }
  285. for _, tmpData := range tmpDataList {
  286. dataList = append(dataList, &data_manage.EdbDataList{
  287. EdbDataId: tmpData.FutureGoodEdbDataId,
  288. EdbInfoId: tmpData.FutureGoodEdbInfoId,
  289. DataTime: tmpData.DataTime.Format(utils.FormatDate),
  290. DataTimestamp: tmpData.DataTimestamp,
  291. Value: tmpData.Close,
  292. })
  293. }
  294. edbDataListMap[v.EdbInfoId] = dataList
  295. v.DataList = dataList
  296. }
  297. xEdbIdValue, yDataList, err = BarChartData(baseEdbInfoMapping, edbInfoMappingList, futureGoodEdbInfoList, edbDataListMap, barChartInfoDateList, regionType, baseEdbInfoMapping.EndDate)
  298. if len(barChartInfoConf.XDataList) > 0 {
  299. xDataList = barChartInfoConf.XDataList
  300. } else {
  301. for _, v := range edbInfoMappingList {
  302. xDataList = append(xDataList, data_manage.XData{
  303. Name: v.EdbName,
  304. NameEn: v.EdbNameEn,
  305. IsHide: 0,
  306. })
  307. }
  308. }
  309. //yDataList =make([]data_manage.YData,0)
  310. //data_manage.YData{
  311. // Date: "",
  312. // Color: "",
  313. // Name: "",
  314. // NameEn: "",
  315. // Value: nil,
  316. // NoDataEdbList: nil,
  317. // XEdbInfoIdList: nil,
  318. //}
  319. futureGoodEdbInfoIndexMap := make(map[int]int)
  320. for index, v := range futureGoodEdbInfoList {
  321. futureGoodEdbInfoIndexMap[v.FutureGoodEdbInfoId] = index
  322. }
  323. nMap := make(map[int]int)
  324. maxIndex := 0 // 最大x轴的下标
  325. for k, tmpYData := range yDataList {
  326. yDataMap := tmpYData.EdbValMap
  327. edbInfoIdList := make([]int, 0)
  328. noDataEdbInfoIdList := make([]int, 0)
  329. valueList := make([]float64, 0)
  330. noDataEdbIdMap := make(map[int]int)
  331. // 所有的N值
  332. for _, n := range tmpYData.M {
  333. nMap[n] = n
  334. }
  335. // 基础指标
  336. baseEdbIds := make([]int, 0)
  337. for _, tmp := range edbInfoMappingList {
  338. edbInfoIdList = append(edbInfoIdList, tmp.EdbInfoId)
  339. baseEdbIds = append(baseEdbIds, tmp.EdbInfoId)
  340. tmpVal, ok := yDataMap[tmp.EdbInfoId]
  341. valueList = append(valueList, tmpVal)
  342. if !ok || tmpVal == 0 {
  343. noDataEdbInfoIdList = append(noDataEdbInfoIdList, tmp.EdbInfoId)
  344. noDataEdbIdMap[tmp.EdbInfoId] = tmp.EdbInfoId
  345. }
  346. }
  347. for _, futureGoodEdbInfo := range futureGoodEdbInfoList {
  348. tmpEdbInfId := futureGoodEdbInfo.FutureGoodEdbInfoId
  349. edbInfoIdList = append(edbInfoIdList, tmpEdbInfId)
  350. tmpVal, ok := yDataMap[tmpEdbInfId]
  351. valueList = append(valueList, tmpVal)
  352. if !ok || tmpVal == 0 {
  353. noDataEdbInfoIdList = append(noDataEdbInfoIdList, tmpEdbInfId)
  354. noDataEdbIdMap[tmpEdbInfId] = tmpEdbInfId
  355. }
  356. }
  357. //tmpYData.Value = valueList
  358. tmpYData.NoDataEdbList = noDataEdbInfoIdList
  359. yDataList[k] = tmpYData
  360. lenEdbId := len(edbInfoIdList)
  361. tmpMaxIndex := lenEdbId - 1 // 当前数据的最大x轴的下标
  362. for i := lenEdbId - 1; i >= 0; i-- {
  363. // 如果没有在无数据的指标列表中找到,那么就找到了最大x轴的下标
  364. if _, ok := noDataEdbIdMap[edbInfoIdList[i]]; !ok || utils.InArrayByInt(baseEdbIds, edbInfoIdList[i]) { //以往的逻辑是碰到第一个无数据的期货指标,那后续的月份都是无数据的,因此需要特殊处理,改成多个现货指标之后需要排除无值的现货指标
  365. // 如果最大x轴的下标 小于 当前下标,那么就重新赋值,
  366. if maxIndex < i-1 {
  367. maxIndex = i - 1
  368. }
  369. break
  370. }
  371. tmpMaxIndex = i - 1
  372. }
  373. // 如果最大x轴的下标 小于 当前下标,那么就重新赋值
  374. if maxIndex < tmpMaxIndex {
  375. maxIndex = tmpMaxIndex
  376. }
  377. }
  378. //xEdbIdValue = xEdbIdValue[0:maxIndex]
  379. // 找出所有的N值,并进行正序排列
  380. nList := make([]int, 0)
  381. for _, n := range nMap {
  382. nList = append(nList, n)
  383. }
  384. sort.Slice(nList, func(i, j int) bool {
  385. return nList[i] < nList[j]
  386. })
  387. for k, v := range yDataList {
  388. if len(v.XEdbInfoIdList) >= maxIndex+1 {
  389. yDataList[k].XEdbInfoIdList = v.XEdbInfoIdList[0 : maxIndex+1]
  390. }
  391. if len(v.Value) >= maxIndex+1 {
  392. yDataList[k].Value = v.Value[0 : maxIndex+1]
  393. }
  394. }
  395. baseEdbLen := len(edbInfoMappingList)
  396. tmpXDataList, newYDataList, err := handleResultData(regionType, futureGoodEdbType, baseEdbLen, yDataList, futureGoodEdbInfoList, maxIndex)
  397. if err != nil {
  398. return
  399. }
  400. if len(barChartInfoConf.XDataList) == 0 {
  401. xDataList = append(xDataList, tmpXDataList...)
  402. }
  403. yDataList = newYDataList
  404. return
  405. }
  406. // BarChartData 获取数据
  407. 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) {
  408. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  409. // 现货指标数据map
  410. baseEdbDataMap := make(map[int]map[string]float64)
  411. edbInfoMappingMap := make(map[int]struct{})
  412. for _, v := range edbInfoMappingList {
  413. edbInfoMappingMap[v.EdbInfoId] = struct{}{}
  414. }
  415. for edbInfoId, edbDataList := range edbDataListMap {
  416. if _, ok1 := edbInfoMappingMap[edbInfoId]; ok1 {
  417. edbDateData := make(map[string]float64)
  418. for _, edbData := range edbDataList {
  419. edbDateData[edbData.DataTime] = edbData.Value
  420. }
  421. baseEdbDataMap[edbInfoId] = edbDateData
  422. }
  423. }
  424. edbDataMap := make(map[int]map[string]float64)
  425. for edbInfoId, edbDataList := range edbDataListMap {
  426. if _, ok := edbInfoMappingMap[edbInfoId]; ok {
  427. continue
  428. }
  429. edbDateData := make(map[string]float64)
  430. for _, edbData := range edbDataList {
  431. edbDateData[edbData.DataTime] = edbData.Value
  432. }
  433. edbDataMap[edbInfoId] = edbDateData
  434. }
  435. // edbIdList 指标展示顺序;x轴的指标顺序
  436. edbIdList = make([]int, 0)
  437. for _, v := range edbInfoMappingList {
  438. edbIdList = append(edbIdList, v.EdbInfoId)
  439. }
  440. for _, v := range futureGoodMappingList {
  441. edbIdList = append(edbIdList, v.FutureGoodEdbInfoId)
  442. }
  443. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  444. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  445. for _, barChartInfoDate := range barChartInfoDateList {
  446. yDataMap := make(map[int]float64)
  447. var maxDate time.Time
  448. var findDateTime time.Time
  449. switch barChartInfoDate.Type {
  450. case 1: //最新值
  451. findDateTime = latestDateTime
  452. case 2: //近期几天
  453. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  454. case 3: // 固定日期
  455. //寻找固定日期的数据
  456. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  457. if tmpErr != nil {
  458. err = tmpErr
  459. return
  460. }
  461. findDateTime = tmpFindDateTime
  462. default:
  463. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  464. return
  465. }
  466. if findDateTime.IsZero() {
  467. err = errors.New("错误的日期")
  468. return
  469. }
  470. findDataList := make([]float64, 0) // 当前日期的数据值
  471. noDataIdList := make([]int, 0) // 没有数据的指标id
  472. noDataIdMap := make(map[int]int, 0) // 没有数据的指标map
  473. xEdbInfoIdList := make([]int, 0) // 当前数据的指标id列表
  474. // 先找到基准日期
  475. var realDateTime time.Time
  476. realDateTime, findDataValue, isFind, tmpErr := GetNeedDateData(findDateTime, edbDataListMap[baseEdbInfoMapping.EdbInfoId], baseEdbDataMap[baseEdbInfoMapping.EdbInfoId], edbDataMap)
  477. if tmpErr != nil {
  478. err = tmpErr
  479. return
  480. }
  481. // 处理其余现货指标
  482. for _, v := range edbInfoMappingList {
  483. if v.EdbInfoId == baseEdbInfoMapping.EdbInfoId {
  484. findDataList = append(findDataList, findDataValue)
  485. yDataMap[baseEdbInfoMapping.EdbInfoId] = findDataValue
  486. if isFind {
  487. maxDate = realDateTime
  488. } else {
  489. noDataIdList = append(noDataIdList, baseEdbInfoMapping.EdbInfoId)
  490. noDataIdMap[baseEdbInfoMapping.EdbInfoId] = baseEdbInfoMapping.EdbInfoId
  491. }
  492. xEdbInfoIdList = append(xEdbInfoIdList, v.EdbInfoId)
  493. continue
  494. }
  495. findDataValueTmp, isFindTmp := baseEdbDataMap[v.EdbInfoId][realDateTime.Format(utils.FormatDate)]
  496. findDataList = append(findDataList, findDataValueTmp)
  497. yDataMap[v.EdbInfoId] = findDataValueTmp
  498. if !isFindTmp {
  499. noDataIdList = append(noDataIdList, v.EdbInfoId)
  500. noDataIdMap[v.EdbInfoId] = v.EdbInfoId
  501. }
  502. xEdbInfoIdList = append(xEdbInfoIdList, v.EdbInfoId)
  503. }
  504. //currMonth := findDateTime.Month() // 当前月份
  505. //currYear := findDateTime.Year() // 当前年份
  506. // 用实际日期的月份作为基准,往前推12个月(2024-5-13 16:26:43修改)
  507. currMonth := realDateTime.Month() // 当前月份
  508. currYear := realDateTime.Year() // 当前年份
  509. mList := make([]int, 0) // 间隔月份
  510. indexList := make([]int, 0)
  511. if regionType == `国内` {
  512. for i := currMonth + 1; i <= 12; i++ {
  513. indexList = append(indexList, int(i))
  514. mList = append(mList, int(i-currMonth))
  515. }
  516. for i := 1; i < int(currMonth); i++ {
  517. indexList = append(indexList, i)
  518. mList = append(mList, 12+i-int(currMonth))
  519. }
  520. } else {
  521. for i, v := range futureGoodMappingList {
  522. if v.FutureGoodEdbType == 2 {
  523. if i == 0 {
  524. continue
  525. }
  526. indexList = append(indexList, i)
  527. mList = append(mList, v.Month)
  528. } else {
  529. if v.Year > currYear || (v.Year == currYear && v.Month > int(currMonth)) {
  530. indexList = append(indexList, i)
  531. mList = append(mList, (v.Year-currYear)*12+v.Month-int(currMonth))
  532. }
  533. }
  534. }
  535. }
  536. for _, i := range indexList {
  537. futureGoodMapping := futureGoodMappingList[i] // 当前的期货指标
  538. //tmpRealDateTime, tmpFindDataValue, tmpIsFind, tmpErr := GetNeedDateData(findDateTime, edbDataListMap[futureGoodMapping.FutureGoodEdbInfoId], edbDataMap[futureGoodMapping.FutureGoodEdbInfoId])
  539. //if tmpErr != nil {
  540. // err = tmpErr
  541. // return
  542. //}
  543. //tmpRealDateTime := findDateTime // 按照配置找到的日期
  544. tmpRealDateTime := realDateTime // 实际现货的日期
  545. tmpFindDataValue, tmpIsFind := edbDataMap[futureGoodMapping.FutureGoodEdbInfoId][tmpRealDateTime.Format(utils.FormatDate)]
  546. yDataMap[futureGoodMapping.FutureGoodEdbInfoId] = tmpFindDataValue
  547. findDataList = append(findDataList, tmpFindDataValue)
  548. if tmpIsFind {
  549. if maxDate.IsZero() || maxDate.Before(tmpRealDateTime) {
  550. maxDate = tmpRealDateTime
  551. }
  552. } else {
  553. noDataIdList = append(noDataIdList, futureGoodMapping.FutureGoodEdbInfoId)
  554. noDataIdMap[futureGoodMapping.FutureGoodEdbInfoId] = futureGoodMapping.FutureGoodEdbInfoId
  555. }
  556. // 当前期货合约的指标
  557. xEdbInfoIdList = append(xEdbInfoIdList, futureGoodMapping.FutureGoodEdbInfoId)
  558. }
  559. yName := barChartInfoDate.Name
  560. yNameEn := barChartInfoDate.Name
  561. if yName == `` {
  562. if barChartInfoDate.Type == 2 {
  563. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  564. if barChartInfoDate.Value == 1 {
  565. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  566. } else {
  567. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  568. }
  569. } else {
  570. yName = maxDate.Format(utils.FormatDate)
  571. yNameEn = maxDate.Format(utils.FormatDate)
  572. }
  573. }
  574. yDate := "0000-00-00"
  575. if !maxDate.IsZero() {
  576. yDate = maxDate.Format(utils.FormatDate)
  577. }
  578. // 数据处理,将没有数据的下标,赋值平均值
  579. {
  580. hasDataIndexList := make([]int, 0)
  581. for dataK, edbInfoId := range xEdbInfoIdList {
  582. if _, ok := noDataIdMap[edbInfoId]; !ok { // 如果是没有数据的指标id
  583. hasDataIndexList = append(hasDataIndexList, dataK)
  584. }
  585. }
  586. lenHasDataIndex := len(hasDataIndexList)
  587. if lenHasDataIndex > 0 {
  588. for lenHasDataI := 1; lenHasDataI < lenHasDataIndex; lenHasDataI++ {
  589. perK := hasDataIndexList[lenHasDataI-1] //上一个有数据的指标下标
  590. currK := hasDataIndexList[lenHasDataI] //当前有数据的指标下标
  591. preVal := findDataList[perK] //上一个有数据的坐标的值
  592. currVal := findDataList[currK] //当前有数据的指标的值
  593. // 环差值
  594. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(currK - perK)))
  595. var tmpI int64
  596. // 将两个中间的数据做平均值补全
  597. for hcI := perK + 1; hcI < currK; hcI++ {
  598. tmpI++
  599. findDataList[hcI], _ = decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(tmpI))).RoundCeil(4).Float64()
  600. }
  601. }
  602. }
  603. }
  604. yDataList = append(yDataList, data_manage.YData{
  605. Date: yDate,
  606. ConfigDate: realDateTime,
  607. Value: findDataList,
  608. NoDataEdbList: noDataIdList,
  609. XEdbInfoIdList: xEdbInfoIdList,
  610. Color: barChartInfoDate.Color,
  611. Name: yName,
  612. NameEn: yNameEn,
  613. EdbValMap: yDataMap,
  614. M: mList,
  615. })
  616. }
  617. return
  618. }
  619. // handleResultData 处理成最终的结果数据
  620. 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) {
  621. xDataList = make([]data_manage.XData, 0)
  622. newYDataList = yDataList
  623. if regionType == `国内` {
  624. for i := 1; i < 12; i++ {
  625. if i > maxIndex {
  626. break
  627. }
  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 []*data_manage.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 := data_manage.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 := data_manage.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.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
  964. if err != nil {
  965. return
  966. }
  967. // 批量刷新期货指标
  968. err = FutureGoodEdbInfoRefreshAllFromBase(futureGoodEdbInfoList, false)
  969. if err != nil {
  970. return
  971. }
  972. resp, err := RefreshEdbRelation(chartInfoId)
  973. if err != nil {
  974. return
  975. }
  976. if resp.Ret != 200 {
  977. err = fmt.Errorf("刷新利润曲线图表数据失败:Err:%s", resp.Msg)
  978. return
  979. }
  980. return
  981. }