profit_chart_info.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package future_good
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. "github.com/yidane/formula"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/models/data_manage/future_good"
  9. "hongze/hz_crm_api/models/data_manage/future_good/request"
  10. "hongze/hz_crm_api/services/data"
  11. "hongze/hz_crm_api/utils"
  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.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, latestDate, 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. tmpRealDateTime := findDateTime
  274. tmpFindDataValue, tmpIsFind := edbDataMap[childFutureGoodEdbInfoId][findDateTime.Format(utils.FormatDate)]
  275. if tmpIsFind && tmpFindDataValue != 0 {
  276. calculateMap[childFutureGoodEdbInfoId] = tmpFindDataValue
  277. if maxDate.IsZero() || maxDate.Before(tmpRealDateTime) {
  278. maxDate = tmpRealDateTime
  279. }
  280. }
  281. }
  282. // 合约的数据不全,不参与计算
  283. //if len(calculateMap) != lenZlFutureGoodEdbInfo {
  284. // continue
  285. //}
  286. newTagEdbIdMap := make(map[string]int)
  287. for tag, zlEdbId := range tagEdbIdMap {
  288. newTagEdbIdMap[tag] = zlAndChildEdbId[zlEdbId]
  289. }
  290. //, formulaStr string, tagEdbIdMap map[string]int
  291. formulaFormStr := ReplaceFormula(newTagEdbIdMap, calculateMap, formulaStr)
  292. //计算公式异常,那么就移除该指标
  293. if formulaFormStr == `` {
  294. //removeDateList = append(removeDateList, sk)
  295. //fmt.Println("异常了")
  296. continue
  297. }
  298. expression := formula.NewExpression(formulaFormStr)
  299. calResult, evaluateErr := expression.Evaluate()
  300. if evaluateErr != nil {
  301. // 分母为0的报错
  302. if strings.Contains(evaluateErr.Error(), "divide by zero") {
  303. //removeDateList = append(removeDateList, sk)
  304. continue
  305. }
  306. err = errors.New("计算失败:Err:" + evaluateErr.Error() + ";formulaStr:" + formulaFormStr)
  307. fmt.Println(err)
  308. continue
  309. }
  310. // 如果计算结果是NAN,那么就退出当前循环
  311. if calResult.IsNan() {
  312. continue
  313. }
  314. calVal, tmpErr := calResult.Float64()
  315. if tmpErr != nil {
  316. err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  317. fmt.Println(err)
  318. continue
  319. }
  320. //yDataMap[n] = calVal
  321. //xEdbInfoIdList = append(xEdbInfoIdList, n)
  322. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  323. yDataMap[tmpN] = calVal
  324. xEdbInfoIdList = append(xEdbInfoIdList, tmpN)
  325. findDataList = append(findDataList, calVal)
  326. }
  327. yName := barChartInfoDate.Name
  328. yNameEn := barChartInfoDate.Name
  329. if yName == `` {
  330. if barChartInfoDate.Type == 2 {
  331. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  332. if barChartInfoDate.Value == 1 {
  333. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  334. } else {
  335. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  336. }
  337. } else {
  338. yName = maxDate.Format(utils.FormatDate)
  339. yNameEn = maxDate.Format(utils.FormatDate)
  340. }
  341. }
  342. yDate := "0000-00-00"
  343. if !maxDate.IsZero() {
  344. yDate = maxDate.Format(utils.FormatDate)
  345. }
  346. yDataList = append(yDataList, data_manage.YData{
  347. Date: yDate,
  348. ConfigDate: findDateTime,
  349. Value: findDataList,
  350. NoDataEdbList: noDataIdList,
  351. XEdbInfoIdList: xEdbInfoIdList,
  352. Color: barChartInfoDate.Color,
  353. Name: yName,
  354. NameEn: yNameEn,
  355. EdbValMap: yDataMap,
  356. M: mList,
  357. })
  358. }
  359. return
  360. }
  361. // getFutureGoodEdbInfoList 获取适用的指标列表
  362. 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) {
  363. maxN := 36 //最大36期合约
  364. futureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
  365. futureGoodEdbInfoDateMap = make(map[string]*future_good.FutureGoodEdbInfo)
  366. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  367. if zlFutureGoodEdbInfo.RegionType == "国内" {
  368. startMonth := int(earliestDateTime.Month())
  369. if startMonth == 1 {
  370. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList
  371. } else {
  372. // 因为是下标,所以对应想下标是需要-1的
  373. index := startMonth - 1
  374. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList[index:]
  375. futureGoodEdbInfoList = append(futureGoodEdbInfoList, tmpFutureGoodEdbInfoList[:index]...)
  376. }
  377. lenFutureGoodEdbInfoList := len(futureGoodEdbInfoList)
  378. //futureGoodEdbInfoList
  379. //if isAllChina {
  380. //}
  381. // 如果全是国内指标,那么只需要拼上多出的几期合约即可
  382. maxN = lenFutureGoodEdbInfoList + monthNum
  383. for i := 1; i < maxN; i++ {
  384. k := i % lenFutureGoodEdbInfoList
  385. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, i, 0).Format(utils.FormatYearMonthDate)] = futureGoodEdbInfoList[k]
  386. }
  387. //需求池604,只要是国内合约,最大必须是12期
  388. newMaxN = 12
  389. return
  390. }
  391. for _, v := range tmpFutureGoodEdbInfoList {
  392. //海外的连续日期,目前
  393. if v.FutureGoodEdbType == 2 {
  394. if v.Month <= maxN {
  395. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, v.Month, 0).Format(utils.FormatYearMonthDate)] = v
  396. if v.Month > newMaxN {
  397. newMaxN = v.Month
  398. }
  399. }
  400. continue
  401. }
  402. if v.Year < earliestDateTime.Year() {
  403. continue
  404. }
  405. // 小于等于当前年,那么就肯定是ok的
  406. if v.Year == earliestDateTime.Year() && v.Month <= int(earliestDateTime.Month()) {
  407. continue
  408. }
  409. subYear := v.Year - earliestDateTime.Year()
  410. subMonth := v.Month - int(earliestDateTime.Month())
  411. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  412. tmpN := subYear*12 + subMonth
  413. if tmpN < maxN {
  414. tmpDateTime := time.Date(v.Year, time.Month(v.Month), 0, 0, 0, 0, 0, time.Local)
  415. futureGoodEdbInfoDateMap[tmpDateTime.Format(utils.FormatYearMonthDate)] = v
  416. if tmpN > newMaxN {
  417. newMaxN = tmpN
  418. }
  419. continue
  420. }
  421. }
  422. return
  423. }
  424. // handleProfitResultData 处理成最终的结果数据
  425. func handleProfitResultData(baseEdbInfo *data_manage.EdbInfo, yDataList []data_manage.YData, earliestDateTime time.Time) (xDataList []data_manage.XData, newYDataList []data_manage.YData, err error) {
  426. xDataList = make([]data_manage.XData, 0)
  427. newYDataList = yDataList
  428. nMap := make(map[int]int)
  429. for _, v := range yDataList {
  430. for _, n := range v.XEdbInfoIdList {
  431. nMap[n] = n
  432. }
  433. }
  434. // 找出所有的N值,并进行正序排列
  435. nList := make([]int, 0)
  436. for _, n := range nMap {
  437. nList = append(nList, n)
  438. }
  439. sort.Slice(nList, func(i, j int) bool {
  440. return nList[i] < nList[j]
  441. })
  442. for _, n := range nList {
  443. if n == 0 {
  444. continue
  445. }
  446. xDataList = append(xDataList, data_manage.XData{
  447. Name: fmt.Sprint("M+", n),
  448. NameEn: fmt.Sprint("M+", n),
  449. })
  450. }
  451. for yIndex, yData := range yDataList {
  452. newYDataList[yIndex].XEdbInfoIdList = []int{}
  453. newYDataList[yIndex].Value = []float64{}
  454. tmpNList := nList
  455. xEdbInfoIdList := yData.XEdbInfoIdList
  456. valIndex := 0
  457. needNum := 0
  458. for _, n := range tmpNList {
  459. if len(xEdbInfoIdList) > 0 {
  460. currN := xEdbInfoIdList[0]
  461. // 当前距离最早的日期相差的N数
  462. if n == currN {
  463. if needNum > 0 {
  464. currVal := yData.Value[valIndex]
  465. preVal := yData.Value[valIndex-1]
  466. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  467. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  468. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  469. // 赋值平均值
  470. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  471. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  472. }
  473. }
  474. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN+1)
  475. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  476. valIndex++
  477. needNum = 0
  478. if len(xEdbInfoIdList) > 0 {
  479. xEdbInfoIdList = xEdbInfoIdList[1:]
  480. }
  481. } else {
  482. needNum++
  483. }
  484. }
  485. }
  486. }
  487. maxI := 0
  488. for _, yData := range newYDataList {
  489. lenEdb := len(yData.XEdbInfoIdList)
  490. for i := 0; i < lenEdb; i++ {
  491. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  492. if maxI < i {
  493. maxI = i
  494. }
  495. }
  496. }
  497. }
  498. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  499. xDataList = xDataList[0:maxI]
  500. for yIndex, yData := range newYDataList {
  501. if len(yData.XEdbInfoIdList) > maxI+1 {
  502. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  503. }
  504. if len(yData.Value) > maxI+1 {
  505. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  506. }
  507. currDate, _ := time.ParseInLocation(utils.FormatDate, yData.Date, time.Local)
  508. nameList := make([]string, 0)
  509. enNameList := make([]string, 0)
  510. for _, n := range newYDataList[yIndex].XEdbInfoIdList {
  511. if n == 1 { // 现货价不处理
  512. nameList = append(nameList, baseEdbInfo.EdbName)
  513. enNameList = append(enNameList, baseEdbInfo.EdbNameEn)
  514. continue
  515. }
  516. if n <= 0 {
  517. nameList = append(nameList, `无合约`)
  518. enNameList = append(enNameList, `no contract`)
  519. } else {
  520. var date string
  521. currDateTime := currDate.AddDate(0, n-1, 0)
  522. month := int(currDateTime.Month())
  523. date = fmt.Sprintf("%d%d", currDateTime.Year(), month)
  524. if month < 10 {
  525. date = fmt.Sprintf("%d0%d", currDateTime.Year(), month)
  526. }
  527. nameList = append(nameList, date)
  528. enNameList = append(enNameList, date)
  529. }
  530. }
  531. newYDataList[yIndex].NameList = nameList
  532. newYDataList[yIndex].EnNameList = enNameList
  533. }
  534. return
  535. }
  536. func CheckFormula(formula string) map[string]string {
  537. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  538. str := strings.ToUpper(formula)
  539. for _, v := range mathFormula {
  540. str = strings.Replace(str, v, "", -1)
  541. }
  542. str = strings.Replace(str, "(", "", -1)
  543. str = strings.Replace(str, ")", "", -1)
  544. byteMap := make(map[string]string)
  545. for i := 0; i < len(str); i++ {
  546. byteInt := str[i]
  547. if byteInt >= 65 && byteInt <= 90 {
  548. byteStr := string(byteInt)
  549. if _, ok := byteMap[byteStr]; !ok {
  550. byteMap[byteStr] = byteStr
  551. }
  552. }
  553. }
  554. return byteMap
  555. }
  556. func ReplaceFormula(tagEdbIdMap map[string]int, valArr map[int]float64, formulaStr string) string {
  557. funMap := GetFormulaMap()
  558. for k, v := range funMap {
  559. formulaStr = strings.Replace(formulaStr, k, v, -1)
  560. }
  561. replaceCount := 0
  562. for tag, edbInfoId := range tagEdbIdMap {
  563. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  564. dvStr := fmt.Sprintf("%v", val)
  565. formulaStr = strings.Replace(formulaStr, tag, dvStr, -1)
  566. replaceCount++
  567. }
  568. }
  569. for k, v := range funMap {
  570. formulaStr = strings.Replace(formulaStr, v, k, -1)
  571. }
  572. if replaceCount == len(tagEdbIdMap) {
  573. return formulaStr
  574. } else {
  575. return ""
  576. }
  577. }
  578. func GetFormulaMap() map[string]string {
  579. funMap := make(map[string]string)
  580. funMap["MAX"] = "[@@]"
  581. funMap["MIN"] = "[@!]"
  582. funMap["ABS"] = "[@#]"
  583. funMap["CEIL"] = "[@$]"
  584. funMap["COS"] = "[@%]"
  585. funMap["FLOOR"] = "[@^]"
  586. funMap["MOD"] = "[@&]"
  587. funMap["POW"] = "[@*]"
  588. funMap["ROUND"] = "[@(]"
  589. return funMap
  590. }