chart_info.go 31 KB

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