profit_chart_info.go 26 KB

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