chart_info.go 37 KB

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