profit_chart_info.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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, 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()-realDateTime.Year())*12 + int(currDate.Month()-realDateTime.Month())
  299. if tmpN <= 0 {
  300. continue
  301. }
  302. // 如果期数大于最大期数,那么就退出当前匹配
  303. if tmpN >= maxN {
  304. break
  305. }
  306. zlAndChildEdbId := make(map[int]int)
  307. childFutureGoodEdbInfoIdList := make([]int, 0)
  308. for zlFutureGoodEdbInfoId, futureGoodEdbInfoList := range futureGoodEdbInfoMap {
  309. // 判断是否特殊合约
  310. if childFutureGoodEdbInfoIdMap, ok := specialFutureGoodEdbInfoMap[zlFutureGoodEdbInfoId]; ok {
  311. if childFutureGoodEdbInfo, ok2 := childFutureGoodEdbInfoIdMap[tmpN]; ok2 {
  312. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  313. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  314. }
  315. } else {
  316. if childFutureGoodEdbInfo, ok2 := futureGoodEdbInfoList[date]; ok2 {
  317. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  318. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  319. }
  320. }
  321. }
  322. // 合约不全,不参与计算
  323. //if len(childFutureGoodEdbInfoIdList) != lenZlFutureGoodEdbInfo {
  324. // continue
  325. //}
  326. calculateMap := make(map[int]float64)
  327. for _, childFutureGoodEdbInfoId := range childFutureGoodEdbInfoIdList {
  328. tmpFindDataValue, tmpIsFind := edbDataMap[childFutureGoodEdbInfoId][realDateTime.Format(utils.FormatDate)]
  329. if tmpIsFind && tmpFindDataValue != 0 {
  330. calculateMap[childFutureGoodEdbInfoId] = tmpFindDataValue
  331. }
  332. }
  333. // 合约的数据不全,不参与计算
  334. //if len(calculateMap) != lenZlFutureGoodEdbInfo {
  335. // continue
  336. //}
  337. newTagEdbIdMap := make(map[string]int)
  338. for tag, zlEdbId := range tagEdbIdMap {
  339. if tag == "A" {
  340. nameTmp := strings.Split(futureGoodEdbInfoMap[zlEdbId][date].FutureGoodEdbName, "(")
  341. nameTmpEn := strings.Split(futureGoodEdbInfoMap[zlEdbId][date].FutureGoodEdbNameEn, "(")
  342. if len(nameTmp) > 1 && len(nameTmpEn) > 1 {
  343. nameTmp[1] = strings.Trim(nameTmp[1], ")")
  344. nameTmpEn[1] = strings.Trim(nameTmpEn[1], ")")
  345. tmpNameMap[tmpN+1] = nameTmp[1] + "-" + nameTmpEn[1]
  346. }
  347. }
  348. newTagEdbIdMap[tag] = zlAndChildEdbId[zlEdbId]
  349. }
  350. //, formulaStr string, tagEdbIdMap map[string]int
  351. formulaFormStr := ReplaceFormula(newTagEdbIdMap, calculateMap, formulaStr)
  352. //计算公式异常,那么就移除该指标
  353. if formulaFormStr == `` {
  354. //removeDateList = append(removeDateList, sk)
  355. fmt.Println("异常了")
  356. continue
  357. }
  358. expression := formula.NewExpression(formulaFormStr)
  359. calResult, evaluateErr := expression.Evaluate()
  360. if evaluateErr != nil {
  361. // 分母为0的报错
  362. if strings.Contains(evaluateErr.Error(), "divide by zero") {
  363. //removeDateList = append(removeDateList, sk)
  364. continue
  365. }
  366. err = errors.New("计算失败:Err:" + evaluateErr.Error() + ";formulaStr:" + formulaFormStr)
  367. fmt.Println(err)
  368. continue
  369. }
  370. // 如果计算结果是NAN,那么就退出当前循环
  371. if calResult.IsNan() {
  372. continue
  373. }
  374. calVal, tmpErr := calResult.Float64()
  375. if tmpErr != nil {
  376. err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  377. fmt.Println(err)
  378. continue
  379. }
  380. //yDataMap[n] = calVal
  381. //xEdbInfoIdList = append(xEdbInfoIdList, n)
  382. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  383. yDataMap[tmpN] = calVal
  384. xEdbInfoIdList = append(xEdbInfoIdList, tmpN)
  385. findDataList = append(findDataList, calVal)
  386. }
  387. yName := barChartInfoDate.Name
  388. yNameEn := barChartInfoDate.Name
  389. if yName == `` {
  390. if barChartInfoDate.Type == 2 {
  391. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  392. if barChartInfoDate.Value == 1 {
  393. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  394. } else {
  395. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  396. }
  397. } else {
  398. yName = maxDate.Format(utils.FormatDate)
  399. yNameEn = maxDate.Format(utils.FormatDate)
  400. }
  401. }
  402. yDate := "0000-00-00"
  403. if !maxDate.IsZero() {
  404. yDate = maxDate.Format(utils.FormatDate)
  405. }
  406. {
  407. hasDataIndexList := make([]int, 0)
  408. for dataK, edbInfoId := range xEdbInfoIdList {
  409. if _, ok := noDataIdMap[edbInfoId]; !ok { // 如果是没有数据的指标id
  410. hasDataIndexList = append(hasDataIndexList, dataK)
  411. }
  412. }
  413. lenHasDataIndex := len(hasDataIndexList)
  414. if lenHasDataIndex > 0 {
  415. for lenHasDataI := 1; lenHasDataI < lenHasDataIndex; lenHasDataI++ {
  416. perK := hasDataIndexList[lenHasDataI-1] //上一个有数据的指标下标
  417. currK := hasDataIndexList[lenHasDataI] //当前有数据的指标下标
  418. preVal := findDataList[perK] //上一个有数据的坐标的值
  419. currVal := findDataList[currK] //当前有数据的指标的值
  420. // 环差值
  421. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(currK - perK)))
  422. var tmpI int64
  423. // 将两个中间的数据做平均值补全
  424. for hcI := perK + 1; hcI < currK; hcI++ {
  425. tmpI++
  426. findDataList[hcI], _ = decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(tmpI))).RoundCeil(4).Float64()
  427. }
  428. }
  429. }
  430. }
  431. futureGoodNameMap[tmpk] = tmpNameMap
  432. yDataList = append(yDataList, data_manage.YData{
  433. Date: yDate,
  434. ConfigDate: realDateTime,
  435. Value: findDataList,
  436. NoDataEdbList: noDataIdList,
  437. XEdbInfoIdList: xEdbInfoIdList,
  438. Color: barChartInfoDate.Color,
  439. Name: yName,
  440. NameEn: yNameEn,
  441. EdbValMap: yDataMap,
  442. M: mList,
  443. })
  444. }
  445. return
  446. }
  447. // getFutureGoodEdbInfoList 获取适用的指标列表
  448. 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) {
  449. maxN := 36 //最大36期合约
  450. futureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
  451. futureGoodEdbInfoDateMap = make(map[string]*future_good.FutureGoodEdbInfo)
  452. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  453. if zlFutureGoodEdbInfo.RegionType == "国内" {
  454. startMonth := int(earliestDateTime.Month())
  455. if startMonth == 1 {
  456. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList
  457. } else {
  458. // 因为是下标,所以对应想下标是需要-1的
  459. index := startMonth - 1
  460. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList[index:]
  461. futureGoodEdbInfoList = append(futureGoodEdbInfoList, tmpFutureGoodEdbInfoList[:index]...)
  462. }
  463. lenFutureGoodEdbInfoList := len(futureGoodEdbInfoList)
  464. //futureGoodEdbInfoList
  465. //if isAllChina {
  466. //}
  467. // 如果全是国内指标,那么只需要拼上多出的几期合约即可
  468. maxN = lenFutureGoodEdbInfoList + monthNum
  469. for i := 1; i < maxN; i++ {
  470. k := i % lenFutureGoodEdbInfoList
  471. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, i, 0).Format(utils.FormatYearMonthDate)] = futureGoodEdbInfoList[k]
  472. }
  473. //需求池604,只要是国内合约,最大必须是12期
  474. newMaxN = 12
  475. return
  476. }
  477. for _, v := range tmpFutureGoodEdbInfoList {
  478. //海外的连续日期,目前
  479. if v.FutureGoodEdbType == 2 {
  480. if v.Month <= maxN {
  481. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, v.Month, 0).Format(utils.FormatYearMonthDate)] = v
  482. if v.Month > newMaxN {
  483. newMaxN = v.Month
  484. }
  485. }
  486. continue
  487. }
  488. if v.Year < earliestDateTime.Year() {
  489. continue
  490. }
  491. // 小于等于当前年,那么就肯定是ok的
  492. if v.Year == earliestDateTime.Year() && v.Month <= int(earliestDateTime.Month()) {
  493. continue
  494. }
  495. subYear := v.Year - earliestDateTime.Year()
  496. subMonth := v.Month - int(earliestDateTime.Month())
  497. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  498. tmpN := subYear*12 + subMonth
  499. if tmpN < maxN {
  500. tmpDateTime := time.Date(v.Year, time.Month(v.Month), 0, 0, 0, 0, 0, time.Local)
  501. futureGoodEdbInfoDateMap[tmpDateTime.Format(utils.FormatYearMonthDate)] = v
  502. if tmpN > newMaxN {
  503. newMaxN = tmpN
  504. }
  505. continue
  506. }
  507. }
  508. return
  509. }
  510. // handleProfitResultData 处理成最终的结果数据
  511. 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) {
  512. newYDataList = yDataList
  513. xDataList = xDataListInit
  514. nMap := make(map[int]int)
  515. nList := make([]int, 0)
  516. nListEdbMap := make(map[int]struct{})
  517. for _, v := range yDataList {
  518. for _, n := range v.XEdbInfoIdList {
  519. if utils.InArrayByInt(allEdbInfoIds, n) {
  520. if _, ok := nListEdbMap[n]; !ok {
  521. nList = append(nList, n)
  522. nListEdbMap[n] = struct{}{}
  523. }
  524. } else {
  525. nMap[n] = n
  526. }
  527. }
  528. }
  529. // 找出所有的N值,并进行正序排列
  530. nListTmp := make([]int, 0)
  531. for _, n := range nMap {
  532. nListTmp = append(nListTmp, n)
  533. }
  534. sort.Slice(nListTmp, func(i, j int) bool {
  535. return nListTmp[i] < nListTmp[j]
  536. })
  537. nList = append(nList, nListTmp...)
  538. for _, n := range nList {
  539. if utils.InArrayByInt(allEdbInfoIds, n) {
  540. continue
  541. }
  542. xDataList = append(xDataList, data_manage.XData{
  543. Name: fmt.Sprint("M+", n),
  544. NameEn: fmt.Sprint("M+", n),
  545. })
  546. }
  547. for yIndex, yData := range yDataList {
  548. newYDataList[yIndex].XEdbInfoIdList = []int{}
  549. newYDataList[yIndex].Value = []float64{}
  550. tmpNList := nList
  551. xEdbInfoIdList := yData.XEdbInfoIdList
  552. valIndex := 0
  553. needNum := 0
  554. for _, n := range tmpNList {
  555. if len(xEdbInfoIdList) > 0 {
  556. currN := xEdbInfoIdList[0]
  557. // 当前距离最早的日期相差的N数
  558. if n == currN { // todo 改成所有的基础现货指标
  559. if needNum > 0 {
  560. currVal := yData.Value[valIndex]
  561. preVal := yData.Value[valIndex-1]
  562. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  563. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  564. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  565. // 赋值平均值
  566. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  567. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  568. }
  569. }
  570. if utils.InArrayByInt(allEdbInfoIds, currN) {
  571. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN)
  572. } else {
  573. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN+1)
  574. }
  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. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  599. xDataList = xDataList[0 : maxI+1]
  600. for yIndex, yData := range newYDataList {
  601. if len(yData.XEdbInfoIdList) > maxI+1 {
  602. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  603. }
  604. if len(yData.Value) > maxI+1 {
  605. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  606. }
  607. nameList := make([]string, 0)
  608. enNameList := make([]string, 0)
  609. for k1, n := range newYDataList[yIndex].XEdbInfoIdList {
  610. if utils.InArrayByInt(allEdbInfoIds, n) { // 现货价不处理
  611. tmpItem := xDataListInit[k1]
  612. nameList = append(nameList, tmpItem.Name)
  613. enNameList = append(enNameList, tmpItem.NameEn)
  614. continue
  615. }
  616. if n <= 0 {
  617. nameList = append(nameList, `无合约`)
  618. enNameList = append(enNameList, `no contract`)
  619. } else {
  620. nameTmp := futureNameMap[yIndex][n]
  621. nameTmpSlice := strings.Split(nameTmp, "-")
  622. nameList = append(nameList, nameTmpSlice[0])
  623. enNameList = append(enNameList, nameTmpSlice[1])
  624. }
  625. }
  626. newYDataList[yIndex].NameList = nameList
  627. newYDataList[yIndex].EnNameList = enNameList
  628. }
  629. return
  630. }
  631. func CheckFormula(formula string) map[string]string {
  632. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  633. str := strings.ToUpper(formula)
  634. for _, v := range mathFormula {
  635. str = strings.Replace(str, v, "", -1)
  636. }
  637. str = strings.Replace(str, "(", "", -1)
  638. str = strings.Replace(str, ")", "", -1)
  639. byteMap := make(map[string]string)
  640. for i := 0; i < len(str); i++ {
  641. byteInt := str[i]
  642. if byteInt >= 65 && byteInt <= 90 {
  643. byteStr := string(byteInt)
  644. if _, ok := byteMap[byteStr]; !ok {
  645. byteMap[byteStr] = byteStr
  646. }
  647. }
  648. }
  649. return byteMap
  650. }
  651. func ReplaceFormula(tagEdbIdMap map[string]int, valArr map[int]float64, formulaStr string) string {
  652. funMap := GetFormulaMap()
  653. for k, v := range funMap {
  654. formulaStr = strings.Replace(formulaStr, k, v, -1)
  655. }
  656. replaceCount := 0
  657. for tag, edbInfoId := range tagEdbIdMap {
  658. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  659. dvStr := fmt.Sprintf("%v", val)
  660. formulaStr = strings.Replace(formulaStr, tag, dvStr, -1)
  661. replaceCount++
  662. }
  663. }
  664. for k, v := range funMap {
  665. formulaStr = strings.Replace(formulaStr, v, k, -1)
  666. }
  667. fmt.Println(formulaStr)
  668. if replaceCount == len(tagEdbIdMap) {
  669. return formulaStr
  670. } else {
  671. return ""
  672. }
  673. }
  674. func GetFormulaMap() map[string]string {
  675. funMap := make(map[string]string)
  676. funMap["MAX"] = "[@@]"
  677. funMap["MIN"] = "[@!]"
  678. funMap["ABS"] = "[@#]"
  679. funMap["CEIL"] = "[@$]"
  680. funMap["COS"] = "[@%]"
  681. funMap["FLOOR"] = "[@^]"
  682. funMap["MOD"] = "[@&]"
  683. funMap["POW"] = "[@*]"
  684. funMap["ROUND"] = "[@(]"
  685. return funMap
  686. }