profit_chart_info.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. package future_good
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/models/data_manage/future_good"
  6. "eta/eta_api/models/data_manage/future_good/request"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "github.com/yidane/formula"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // GetProfitChartEdbData 获取利润图表的指标数据
  18. func GetProfitChartEdbData(baseEdbInfo *data_manage.EdbInfo, zlFutureGoodEdbInfoList []*future_good.FutureGoodEdbInfo, chartInfoDateList []request.ChartInfoDateReq, formulaStr string, edbInfoFromTagList []data_manage.EdbInfoFromTag) (barConfigEdbInfoIdList []data_manage.BarChartInfoEdbItemReq, edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, xDataList []data_manage.XData, yDataList []data_manage.YData, err error) {
  19. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  20. if baseEdbInfo == nil {
  21. err = errors.New("ETA指标未选取")
  22. return
  23. }
  24. if len(zlFutureGoodEdbInfoList) <= 0 {
  25. err = errors.New("商品指标未选取")
  26. return
  27. }
  28. // 标签与期货商品指标的关联关系
  29. tagEdbIdMap := make(map[string]int)
  30. // 有效的期货商品指标
  31. futureGoodEdbInfoIdMap := make(map[int]int)
  32. {
  33. tmpTagEdbIdMap := make(map[string]int)
  34. for _, v := range edbInfoFromTagList {
  35. tmpTagEdbIdMap[v.FromTag] = v.EdbInfoId
  36. }
  37. formulaMap := CheckFormula(formulaStr)
  38. for _, tag := range formulaMap {
  39. tagEdbIdMap[tag] = tmpTagEdbIdMap[tag]
  40. futureGoodEdbInfoIdMap[tmpTagEdbIdMap[tag]] = tmpTagEdbIdMap[tag]
  41. }
  42. }
  43. // 指标对应的所有数据
  44. //edbDataListMap := make(map[int][]*data_manage.EdbDataList)
  45. item := new(data_manage.ChartEdbInfoMapping)
  46. item.FrequencyEn = data.GetFrequencyEn(baseEdbInfo.Frequency)
  47. if baseEdbInfo.Unit == `无` {
  48. item.Unit = ``
  49. }
  50. // 普通的指标数据
  51. baseDataList := make([]*data_manage.EdbDataList, 0)
  52. baseDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, "", "")
  53. if err != nil {
  54. return
  55. }
  56. latestDate := zlFutureGoodEdbInfoList[0].EndDate
  57. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  58. earliestDateTime := latestDateTime // 数据的最早日期,目的是为了找出最早的合约
  59. for _, barChartInfoDate := range chartInfoDateList {
  60. var findDateTime time.Time
  61. switch barChartInfoDate.Type {
  62. case 1: //最新值
  63. findDateTime = latestDateTime
  64. case 2: //近期几天
  65. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  66. case 3: // 固定日期
  67. //寻找固定日期的数据
  68. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  69. if tmpErr != nil {
  70. err = tmpErr
  71. return
  72. }
  73. findDateTime = tmpFindDateTime
  74. default:
  75. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  76. return
  77. }
  78. if findDateTime.IsZero() {
  79. err = errors.New("错误的日期")
  80. return
  81. }
  82. if findDateTime.Before(earliestDateTime) {
  83. earliestDateTime = findDateTime
  84. }
  85. }
  86. monthNum := (latestDateTime.Year()-earliestDateTime.Year())*12 + int(latestDateTime.Month()-earliestDateTime.Month())
  87. // 存储主力合约下的所有月份合约
  88. futureGoodEdbInfoDateMap := make(map[int]map[string]*future_good.FutureGoodEdbInfo)
  89. futureGoodDataListMap := make(map[int][]*data_manage.EdbDataList, 0)
  90. // 特殊的商品期货合约(只有M+N的合约,没有固定日期的合约)
  91. specialFutureGoodEdbInfoMap := make(map[int]map[int]*future_good.FutureGoodEdbInfo, 0)
  92. isAllChina := true // 是否都是国内的期货合约
  93. for _, v := range zlFutureGoodEdbInfoList {
  94. if v.RegionType != "国内" {
  95. isAllChina = false
  96. break
  97. }
  98. }
  99. nMap := make(map[string]string)
  100. var maxN int // 最大N值
  101. for _, v := range zlFutureGoodEdbInfoList {
  102. // 如果不是有效的商品期货指标,那么就过滤掉,不做数据查询处理,避免没必要的请求
  103. if _, ok := futureGoodEdbInfoIdMap[v.FutureGoodEdbInfoId]; !ok {
  104. continue
  105. }
  106. // 获取期货指标以及期货数据
  107. tmpFutureGoodEdbInfoList, tmpErr := future_good.GetChildFutureGoodEdbInfoListByParentId(v.FutureGoodEdbInfoId)
  108. if tmpErr != nil {
  109. err = tmpErr
  110. return
  111. }
  112. childFutureGoodEdbInfoMap, tmpMaxN, tmpErr := getProfitFutureGoodEdbInfoList(earliestDateTime, v, tmpFutureGoodEdbInfoList, isAllChina, monthNum)
  113. if tmpErr != nil {
  114. err = tmpErr
  115. return
  116. }
  117. if maxN < tmpMaxN {
  118. maxN = tmpMaxN
  119. }
  120. futureGoodEdbInfoDateMap[v.FutureGoodEdbInfoId] = childFutureGoodEdbInfoMap
  121. if v.FutureGoodEdbType == 2 {
  122. specialFutureGoodEdbInfoMap[v.FutureGoodEdbInfoId] = make(map[int]*future_good.FutureGoodEdbInfo)
  123. }
  124. // 获取数据
  125. for date, childFutureGoodEdbInfo := range childFutureGoodEdbInfoMap {
  126. nMap[date] = date
  127. dataList := make([]*data_manage.EdbDataList, 0)
  128. tmpDataList, tmpErr := future_good.GetFutureGoodEdbDataListByDate(childFutureGoodEdbInfo.FutureGoodEdbInfoId, "", "")
  129. if tmpErr != nil {
  130. return
  131. }
  132. for _, tmpData := range tmpDataList {
  133. dataList = append(dataList, &data_manage.EdbDataList{
  134. EdbDataId: tmpData.FutureGoodEdbDataId,
  135. EdbInfoId: tmpData.FutureGoodEdbInfoId,
  136. DataTime: tmpData.DataTime.Format(utils.FormatDate),
  137. DataTimestamp: tmpData.DataTimestamp,
  138. Value: tmpData.Close,
  139. })
  140. }
  141. futureGoodDataListMap[childFutureGoodEdbInfo.FutureGoodEdbInfoId] = dataList
  142. if childFutureGoodEdbInfo.FutureGoodEdbType == 2 {
  143. specialFutureGoodEdbInfoMap[v.FutureGoodEdbInfoId][childFutureGoodEdbInfo.Month] = childFutureGoodEdbInfo
  144. }
  145. }
  146. }
  147. // 找出所有的N值,并进行正序排列
  148. dateList := make([]string, 0)
  149. for _, n := range nMap {
  150. dateList = append(dateList, n)
  151. }
  152. sort.Slice(dateList, func(i, j int) bool {
  153. return dateList[i] < dateList[j]
  154. })
  155. _, yDataList, err = ProfitChartChartData(baseDataList, futureGoodEdbInfoDateMap, futureGoodDataListMap, chartInfoDateList, baseEdbInfo.EndDate, specialFutureGoodEdbInfoMap, formulaStr, tagEdbIdMap, dateList, maxN)
  156. tmpXDataList, newYDataList, err := handleProfitResultData(baseEdbInfo, yDataList, earliestDateTime)
  157. if err != nil {
  158. return
  159. }
  160. xDataList = []data_manage.XData{
  161. {
  162. Name: "现货利润",
  163. NameEn: "Spot Price",
  164. },
  165. }
  166. xDataList = append(xDataList, tmpXDataList...)
  167. yDataList = newYDataList
  168. return
  169. }
  170. // ProfitChartChartData 获取数据
  171. func ProfitChartChartData(baseDataList []*data_manage.EdbDataList, futureGoodEdbInfoMap map[int]map[string]*future_good.FutureGoodEdbInfo, futureGoodEdbDataListMap map[int][]*data_manage.EdbDataList, chartInfoDateList []request.ChartInfoDateReq, latestDate string, specialFutureGoodEdbInfoMap map[int]map[int]*future_good.FutureGoodEdbInfo, formulaStr string, tagEdbIdMap map[string]int, dateList []string, maxN int) (edbIdList []int, yDataList []data_manage.YData, err error) {
  172. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  173. //earliestDateTime time.Time
  174. // ETA指标数据
  175. baseEdbDateData := make(map[string]float64)
  176. for _, edbData := range baseDataList {
  177. baseEdbDateData[edbData.DataTime] = edbData.Value
  178. }
  179. // 商品指标数据
  180. edbDataMap := make(map[int]map[string]float64)
  181. for edbInfoId, edbDataList := range futureGoodEdbDataListMap {
  182. edbDateData := make(map[string]float64)
  183. for _, edbData := range edbDataList {
  184. edbDateData[edbData.DataTime] = edbData.Value
  185. }
  186. edbDataMap[edbInfoId] = edbDateData
  187. }
  188. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  189. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  190. // 将计算公式中的字母转大写
  191. formulaStr = strings.ToUpper(formulaStr)
  192. for _, barChartInfoDate := range chartInfoDateList {
  193. yDataMap := make(map[int]float64)
  194. var maxDate time.Time
  195. var findDateTime time.Time
  196. switch barChartInfoDate.Type {
  197. case 1: //最新值
  198. findDateTime = latestDateTime
  199. case 2: //近期几天
  200. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  201. case 3: // 固定日期
  202. //寻找固定日期的数据
  203. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  204. if tmpErr != nil {
  205. err = tmpErr
  206. return
  207. }
  208. findDateTime = tmpFindDateTime
  209. default:
  210. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  211. return
  212. }
  213. if findDateTime.IsZero() {
  214. err = errors.New("错误的日期")
  215. return
  216. }
  217. findDataList := make([]float64, 0) // 当前日期的数据值
  218. noDataIdList := make([]int, 0) // 没有数据的指标id
  219. xEdbInfoIdList := make([]int, 0) // 当前数据的指标id列表
  220. // 现货指标
  221. realDateTime, findDataValue, isFind, tmpErr := GetNeedDateData(findDateTime, baseDataList, baseEdbDateData)
  222. if tmpErr != nil {
  223. err = tmpErr
  224. return
  225. }
  226. findDataList = append(findDataList, findDataValue)
  227. yDataMap[0] = findDataValue
  228. if isFind {
  229. maxDate = realDateTime
  230. }
  231. xEdbInfoIdList = append(xEdbInfoIdList, 0)
  232. mList := make([]int, 0) // 间隔月份
  233. // 最小开始的n值
  234. //minN := (findDateTime.Year()-earliestDateTime.Year())*12 + int(findDateTime.Month()-earliestDateTime.Month())
  235. for _, date := range dateList {
  236. currDate, _ := time.ParseInLocation(utils.FormatYearMonthDate, date, time.Local)
  237. // 如果当前的n值小于最小开始的n值,那么就不处理
  238. //if n < minN {
  239. // continue
  240. //}
  241. //findDateTime
  242. // 获取当前日期相对开始日期的期数
  243. tmpN := (currDate.Year()-findDateTime.Year())*12 + int(currDate.Month()-findDateTime.Month())
  244. if tmpN <= 0 {
  245. continue
  246. }
  247. // 如果期数大于最大期数,那么就退出当前匹配
  248. if tmpN >= maxN {
  249. break
  250. }
  251. zlAndChildEdbId := make(map[int]int)
  252. childFutureGoodEdbInfoIdList := make([]int, 0)
  253. for zlFutureGoodEdbInfoId, futureGoodEdbInfoList := range futureGoodEdbInfoMap {
  254. // 判断是否特殊合约
  255. if childFutureGoodEdbInfoIdMap, ok := specialFutureGoodEdbInfoMap[zlFutureGoodEdbInfoId]; ok {
  256. if childFutureGoodEdbInfo, ok2 := childFutureGoodEdbInfoIdMap[tmpN]; ok2 {
  257. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  258. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  259. }
  260. } else {
  261. if childFutureGoodEdbInfo, ok2 := futureGoodEdbInfoList[date]; ok2 {
  262. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  263. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  264. }
  265. }
  266. }
  267. // 合约不全,不参与计算
  268. //if len(childFutureGoodEdbInfoIdList) != lenZlFutureGoodEdbInfo {
  269. // continue
  270. //}
  271. calculateMap := make(map[int]float64)
  272. for _, childFutureGoodEdbInfoId := range childFutureGoodEdbInfoIdList {
  273. tmpFindDataValue, tmpIsFind := edbDataMap[childFutureGoodEdbInfoId][realDateTime.Format(utils.FormatDate)]
  274. if tmpIsFind && tmpFindDataValue != 0 {
  275. calculateMap[childFutureGoodEdbInfoId] = tmpFindDataValue
  276. }
  277. }
  278. // 合约的数据不全,不参与计算
  279. //if len(calculateMap) != lenZlFutureGoodEdbInfo {
  280. // continue
  281. //}
  282. newTagEdbIdMap := make(map[string]int)
  283. for tag, zlEdbId := range tagEdbIdMap {
  284. newTagEdbIdMap[tag] = zlAndChildEdbId[zlEdbId]
  285. }
  286. //, formulaStr string, tagEdbIdMap map[string]int
  287. formulaFormStr := ReplaceFormula(newTagEdbIdMap, calculateMap, formulaStr)
  288. //计算公式异常,那么就移除该指标
  289. if formulaFormStr == `` {
  290. //removeDateList = append(removeDateList, sk)
  291. //fmt.Println("异常了")
  292. continue
  293. }
  294. expression := formula.NewExpression(formulaFormStr)
  295. calResult, evaluateErr := expression.Evaluate()
  296. if evaluateErr != nil {
  297. // 分母为0的报错
  298. if strings.Contains(evaluateErr.Error(), "divide by zero") {
  299. //removeDateList = append(removeDateList, sk)
  300. continue
  301. }
  302. err = errors.New("计算失败:Err:" + evaluateErr.Error() + ";formulaStr:" + formulaFormStr)
  303. fmt.Println(err)
  304. continue
  305. }
  306. // 如果计算结果是NAN,那么就退出当前循环
  307. if calResult.IsNan() {
  308. continue
  309. }
  310. calVal, tmpErr := calResult.Float64()
  311. if tmpErr != nil {
  312. err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  313. fmt.Println(err)
  314. continue
  315. }
  316. //yDataMap[n] = calVal
  317. //xEdbInfoIdList = append(xEdbInfoIdList, n)
  318. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  319. yDataMap[tmpN] = calVal
  320. xEdbInfoIdList = append(xEdbInfoIdList, tmpN)
  321. findDataList = append(findDataList, calVal)
  322. }
  323. yName := barChartInfoDate.Name
  324. yNameEn := barChartInfoDate.Name
  325. if yName == `` {
  326. if barChartInfoDate.Type == 2 {
  327. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  328. if barChartInfoDate.Value == 1 {
  329. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  330. } else {
  331. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  332. }
  333. } else {
  334. yName = maxDate.Format(utils.FormatDate)
  335. yNameEn = maxDate.Format(utils.FormatDate)
  336. }
  337. }
  338. yDate := "0000-00-00"
  339. if !maxDate.IsZero() {
  340. yDate = maxDate.Format(utils.FormatDate)
  341. }
  342. yDataList = append(yDataList, data_manage.YData{
  343. Date: yDate,
  344. ConfigDate: realDateTime,
  345. Value: findDataList,
  346. NoDataEdbList: noDataIdList,
  347. XEdbInfoIdList: xEdbInfoIdList,
  348. Color: barChartInfoDate.Color,
  349. Name: yName,
  350. NameEn: yNameEn,
  351. EdbValMap: yDataMap,
  352. M: mList,
  353. })
  354. }
  355. return
  356. }
  357. // getFutureGoodEdbInfoList 获取适用的指标列表
  358. func getProfitFutureGoodEdbInfoList(earliestDateTime time.Time, zlFutureGoodEdbInfo *future_good.FutureGoodEdbInfo, tmpFutureGoodEdbInfoList []*future_good.FutureGoodEdbInfo, isAllChina bool, monthNum int) (futureGoodEdbInfoDateMap map[string]*future_good.FutureGoodEdbInfo, newMaxN int, err error) {
  359. maxN := 36 //最大36期合约
  360. futureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
  361. futureGoodEdbInfoDateMap = make(map[string]*future_good.FutureGoodEdbInfo)
  362. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  363. if zlFutureGoodEdbInfo.RegionType == "国内" {
  364. startMonth := int(earliestDateTime.Month())
  365. if startMonth == 1 {
  366. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList
  367. } else {
  368. // 因为是下标,所以对应想下标是需要-1的
  369. index := startMonth - 1
  370. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList[index:]
  371. futureGoodEdbInfoList = append(futureGoodEdbInfoList, tmpFutureGoodEdbInfoList[:index]...)
  372. }
  373. lenFutureGoodEdbInfoList := len(futureGoodEdbInfoList)
  374. //futureGoodEdbInfoList
  375. //if isAllChina {
  376. //}
  377. // 如果全是国内指标,那么只需要拼上多出的几期合约即可
  378. maxN = lenFutureGoodEdbInfoList + monthNum
  379. for i := 1; i < maxN; i++ {
  380. k := i % lenFutureGoodEdbInfoList
  381. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, i, 0).Format(utils.FormatYearMonthDate)] = futureGoodEdbInfoList[k]
  382. }
  383. //需求池604,只要是国内合约,最大必须是12期
  384. newMaxN = 12
  385. return
  386. }
  387. for _, v := range tmpFutureGoodEdbInfoList {
  388. //海外的连续日期,目前
  389. if v.FutureGoodEdbType == 2 {
  390. if v.Month <= maxN {
  391. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, v.Month, 0).Format(utils.FormatYearMonthDate)] = v
  392. if v.Month > newMaxN {
  393. newMaxN = v.Month
  394. }
  395. }
  396. continue
  397. }
  398. if v.Year < earliestDateTime.Year() {
  399. continue
  400. }
  401. // 小于等于当前年,那么就肯定是ok的
  402. if v.Year == earliestDateTime.Year() && v.Month <= int(earliestDateTime.Month()) {
  403. continue
  404. }
  405. subYear := v.Year - earliestDateTime.Year()
  406. subMonth := v.Month - int(earliestDateTime.Month())
  407. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  408. tmpN := subYear*12 + subMonth
  409. if tmpN < maxN {
  410. tmpDateTime := time.Date(v.Year, time.Month(v.Month), 0, 0, 0, 0, 0, time.Local)
  411. futureGoodEdbInfoDateMap[tmpDateTime.Format(utils.FormatYearMonthDate)] = v
  412. if tmpN > newMaxN {
  413. newMaxN = tmpN
  414. }
  415. continue
  416. }
  417. }
  418. return
  419. }
  420. // handleProfitResultData 处理成最终的结果数据
  421. func handleProfitResultData(baseEdbInfo *data_manage.EdbInfo, yDataList []data_manage.YData, earliestDateTime time.Time) (xDataList []data_manage.XData, newYDataList []data_manage.YData, err error) {
  422. xDataList = make([]data_manage.XData, 0)
  423. newYDataList = yDataList
  424. nMap := make(map[int]int)
  425. for _, v := range yDataList {
  426. for _, n := range v.XEdbInfoIdList {
  427. nMap[n] = n
  428. }
  429. }
  430. // 找出所有的N值,并进行正序排列
  431. nList := make([]int, 0)
  432. for _, n := range nMap {
  433. nList = append(nList, n)
  434. }
  435. sort.Slice(nList, func(i, j int) bool {
  436. return nList[i] < nList[j]
  437. })
  438. for _, n := range nList {
  439. if n == 0 {
  440. continue
  441. }
  442. xDataList = append(xDataList, data_manage.XData{
  443. Name: fmt.Sprint("M+", n),
  444. NameEn: fmt.Sprint("M+", n),
  445. })
  446. }
  447. for yIndex, yData := range yDataList {
  448. newYDataList[yIndex].XEdbInfoIdList = []int{}
  449. newYDataList[yIndex].Value = []float64{}
  450. tmpNList := nList
  451. xEdbInfoIdList := yData.XEdbInfoIdList
  452. valIndex := 0
  453. needNum := 0
  454. for _, n := range tmpNList {
  455. if len(xEdbInfoIdList) > 0 {
  456. currN := xEdbInfoIdList[0]
  457. // 当前距离最早的日期相差的N数
  458. if n == currN {
  459. if needNum > 0 {
  460. currVal := yData.Value[valIndex]
  461. preVal := yData.Value[valIndex-1]
  462. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  463. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  464. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  465. // 赋值平均值
  466. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  467. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  468. }
  469. }
  470. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN+1)
  471. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  472. valIndex++
  473. needNum = 0
  474. if len(xEdbInfoIdList) > 0 {
  475. xEdbInfoIdList = xEdbInfoIdList[1:]
  476. }
  477. } else {
  478. needNum++
  479. }
  480. }
  481. }
  482. }
  483. maxI := 0
  484. for _, yData := range newYDataList {
  485. lenEdb := len(yData.XEdbInfoIdList)
  486. for i := 0; i < lenEdb; i++ {
  487. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  488. if maxI < i {
  489. maxI = i
  490. }
  491. }
  492. }
  493. }
  494. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  495. xDataList = xDataList[0:maxI]
  496. for yIndex, yData := range newYDataList {
  497. if len(yData.XEdbInfoIdList) > maxI+1 {
  498. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  499. }
  500. if len(yData.Value) > maxI+1 {
  501. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  502. }
  503. currDate, _ := time.ParseInLocation(utils.FormatDate, yData.Date, time.Local)
  504. nameList := make([]string, 0)
  505. enNameList := make([]string, 0)
  506. for _, n := range newYDataList[yIndex].XEdbInfoIdList {
  507. if n == 1 { // 现货价不处理
  508. nameList = append(nameList, baseEdbInfo.EdbName)
  509. enNameList = append(enNameList, baseEdbInfo.EdbNameEn)
  510. continue
  511. }
  512. if n <= 0 {
  513. nameList = append(nameList, `无合约`)
  514. enNameList = append(enNameList, `no contract`)
  515. } else {
  516. var date string
  517. currDateTime := currDate.AddDate(0, n-1, 0)
  518. month := int(currDateTime.Month())
  519. date = fmt.Sprintf("%d%d", currDateTime.Year(), month)
  520. if month < 10 {
  521. date = fmt.Sprintf("%d0%d", currDateTime.Year(), month)
  522. }
  523. nameList = append(nameList, date)
  524. enNameList = append(enNameList, date)
  525. }
  526. }
  527. newYDataList[yIndex].NameList = nameList
  528. newYDataList[yIndex].EnNameList = enNameList
  529. }
  530. return
  531. }
  532. func CheckFormula(formula string) map[string]string {
  533. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  534. str := strings.ToUpper(formula)
  535. for _, v := range mathFormula {
  536. str = strings.Replace(str, v, "", -1)
  537. }
  538. str = strings.Replace(str, "(", "", -1)
  539. str = strings.Replace(str, ")", "", -1)
  540. byteMap := make(map[string]string)
  541. for i := 0; i < len(str); i++ {
  542. byteInt := str[i]
  543. if byteInt >= 65 && byteInt <= 90 {
  544. byteStr := string(byteInt)
  545. if _, ok := byteMap[byteStr]; !ok {
  546. byteMap[byteStr] = byteStr
  547. }
  548. }
  549. }
  550. return byteMap
  551. }
  552. func ReplaceFormula(tagEdbIdMap map[string]int, valArr map[int]float64, formulaStr string) string {
  553. funMap := GetFormulaMap()
  554. for k, v := range funMap {
  555. formulaStr = strings.Replace(formulaStr, k, v, -1)
  556. }
  557. replaceCount := 0
  558. for tag, edbInfoId := range tagEdbIdMap {
  559. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  560. dvStr := fmt.Sprintf("%v", val)
  561. formulaStr = strings.Replace(formulaStr, tag, dvStr, -1)
  562. replaceCount++
  563. }
  564. }
  565. for k, v := range funMap {
  566. formulaStr = strings.Replace(formulaStr, v, k, -1)
  567. }
  568. if replaceCount == len(tagEdbIdMap) {
  569. return formulaStr
  570. } else {
  571. return ""
  572. }
  573. }
  574. func GetFormulaMap() map[string]string {
  575. funMap := make(map[string]string)
  576. funMap["MAX"] = "[@@]"
  577. funMap["MIN"] = "[@!]"
  578. funMap["ABS"] = "[@#]"
  579. funMap["CEIL"] = "[@$]"
  580. funMap["COS"] = "[@%]"
  581. funMap["FLOOR"] = "[@^]"
  582. funMap["MOD"] = "[@&]"
  583. funMap["POW"] = "[@*]"
  584. funMap["ROUND"] = "[@(]"
  585. return funMap
  586. }