profit_chart_info.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. edbIdList, yDataList, err = ProfitChartChartData(baseEdbInfo, baseDataListMap, futureGoodEdbInfoDateMap, futureGoodDataListMap, chartInfoDateList, baseEdbInfo.EndDate, specialFutureGoodEdbInfoMap, formulaStr, tagEdbIdMap, dateList, maxN, reqEdbInfoIds)
  173. // todo 最后处理数据
  174. tmpXDataList, newYDataList, err := handleProfitResultData(xDataList, baseEdbInfo, yDataList, earliestDateTime, edbIdList)
  175. if err != nil {
  176. return
  177. }
  178. if len(reqXDataList) == 0 {
  179. xDataList = tmpXDataList
  180. } else {
  181. xDataList = reqXDataList
  182. }
  183. yDataList = newYDataList
  184. return
  185. }
  186. // ProfitChartChartData 获取数据
  187. 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, err error) {
  188. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  189. //earliestDateTime time.Time
  190. // ETA指标数据
  191. allBaseEdbDateDataMap := make(map[int]map[string]float64)
  192. for edbInfoId, baseDataList := range baseDataListMap {
  193. baseEdbDateData := make(map[string]float64)
  194. for _, edbData := range baseDataList {
  195. baseEdbDateData[edbData.DataTime] = edbData.Value
  196. }
  197. allBaseEdbDateDataMap[edbInfoId] = baseEdbDateData
  198. }
  199. // 商品指标数据
  200. edbDataMap := make(map[int]map[string]float64)
  201. for edbInfoId, edbDataList := range futureGoodEdbDataListMap {
  202. edbDateData := make(map[string]float64)
  203. for _, edbData := range edbDataList {
  204. edbDateData[edbData.DataTime] = edbData.Value
  205. }
  206. edbDataMap[edbInfoId] = edbDateData
  207. }
  208. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  209. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  210. // 将计算公式中的字母转大写
  211. formulaStr = strings.ToUpper(formulaStr)
  212. for _, barChartInfoDate := range chartInfoDateList {
  213. yDataMap := make(map[int]float64)
  214. var maxDate time.Time
  215. var findDateTime time.Time
  216. switch barChartInfoDate.Type {
  217. case 1: //最新值
  218. findDateTime = latestDateTime
  219. case 2: //近期几天
  220. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  221. case 3: // 固定日期
  222. //寻找固定日期的数据
  223. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  224. if tmpErr != nil {
  225. err = tmpErr
  226. return
  227. }
  228. findDateTime = tmpFindDateTime
  229. default:
  230. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  231. return
  232. }
  233. if findDateTime.IsZero() {
  234. err = errors.New("错误的日期")
  235. return
  236. }
  237. findDataList := make([]float64, 0) // 当前日期的数据值
  238. noDataIdList := make([]int, 0) // 没有数据的指标id
  239. noDataIdMap := make(map[int]int, 0) // 没有数据的指标map
  240. xEdbInfoIdList := make([]int, 0) // 当前数据的指标id列表
  241. // 现货指标
  242. index := 0
  243. var realDateTime time.Time
  244. // 现货指标
  245. baseEdbDateData, ok := allBaseEdbDateDataMap[baseEdbInfo.EdbInfoId]
  246. if !ok {
  247. err = fmt.Errorf("指标id: %d 没有数据", baseEdbInfo.EdbInfoId)
  248. return
  249. }
  250. realDateTime, findDataValue, isFind, tmpErr := GetNeedDateData(findDateTime, baseDataListMap[baseEdbInfo.EdbInfoId], baseEdbDateData, edbDataMap)
  251. if tmpErr != nil {
  252. err = tmpErr
  253. return
  254. }
  255. if isFind {
  256. maxDate = realDateTime
  257. }
  258. edbIdList = make([]int, 0) //普通指标ID
  259. for _, edbInfoId := range reqEdbInfoIds {
  260. if edbInfoId == baseEdbInfo.EdbInfoId {
  261. findDataList = append(findDataList, findDataValue)
  262. yDataMap[index] = findDataValue
  263. xEdbInfoIdList = append(xEdbInfoIdList, edbInfoId)
  264. edbIdList = append(edbIdList, edbInfoId)
  265. index += 1
  266. continue
  267. }
  268. baseEdbDateDataTmp, ok := allBaseEdbDateDataMap[edbInfoId]
  269. if !ok {
  270. err = fmt.Errorf("指标id: %d 没有数据", edbInfoId)
  271. return
  272. }
  273. findDataValueTmp, isFindTmp := baseEdbDateDataTmp[realDateTime.Format(utils.FormatDate)]
  274. if !isFindTmp {
  275. noDataIdList = append(noDataIdList, edbInfoId)
  276. noDataIdMap[edbInfoId] = edbInfoId
  277. }
  278. findDataList = append(findDataList, findDataValueTmp)
  279. yDataMap[index] = findDataValueTmp
  280. xEdbInfoIdList = append(xEdbInfoIdList, edbInfoId)
  281. edbIdList = append(edbIdList, edbInfoId)
  282. index += 1
  283. }
  284. mList := make([]int, 0) // 间隔月份
  285. // 最小开始的n值
  286. //minN := (findDateTime.Year()-earliestDateTime.Year())*12 + int(findDateTime.Month()-earliestDateTime.Month())
  287. for _, date := range dateList {
  288. currDate, _ := time.ParseInLocation(utils.FormatYearMonthDate, date, time.Local)
  289. // 如果当前的n值小于最小开始的n值,那么就不处理
  290. //if n < minN {
  291. // continue
  292. //}
  293. //findDateTime
  294. // 获取当前日期相对开始日期的期数
  295. tmpN := (currDate.Year()-realDateTime.Year())*12 + int(currDate.Month()-realDateTime.Month())
  296. if tmpN <= 0 {
  297. continue
  298. }
  299. // 如果期数大于最大期数,那么就退出当前匹配
  300. if tmpN >= maxN {
  301. break
  302. }
  303. zlAndChildEdbId := make(map[int]int)
  304. childFutureGoodEdbInfoIdList := make([]int, 0)
  305. for zlFutureGoodEdbInfoId, futureGoodEdbInfoList := range futureGoodEdbInfoMap {
  306. // 判断是否特殊合约
  307. if childFutureGoodEdbInfoIdMap, ok := specialFutureGoodEdbInfoMap[zlFutureGoodEdbInfoId]; ok {
  308. if childFutureGoodEdbInfo, ok2 := childFutureGoodEdbInfoIdMap[tmpN]; ok2 {
  309. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  310. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  311. }
  312. } else {
  313. if childFutureGoodEdbInfo, ok2 := futureGoodEdbInfoList[date]; ok2 {
  314. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  315. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  316. }
  317. }
  318. }
  319. // 合约不全,不参与计算
  320. //if len(childFutureGoodEdbInfoIdList) != lenZlFutureGoodEdbInfo {
  321. // continue
  322. //}
  323. calculateMap := make(map[int]float64)
  324. for _, childFutureGoodEdbInfoId := range childFutureGoodEdbInfoIdList {
  325. tmpFindDataValue, tmpIsFind := edbDataMap[childFutureGoodEdbInfoId][realDateTime.Format(utils.FormatDate)]
  326. if tmpIsFind && tmpFindDataValue != 0 {
  327. calculateMap[childFutureGoodEdbInfoId] = tmpFindDataValue
  328. }
  329. }
  330. // 合约的数据不全,不参与计算
  331. //if len(calculateMap) != lenZlFutureGoodEdbInfo {
  332. // continue
  333. //}
  334. newTagEdbIdMap := make(map[string]int)
  335. for tag, zlEdbId := range tagEdbIdMap {
  336. newTagEdbIdMap[tag] = zlAndChildEdbId[zlEdbId]
  337. }
  338. //, formulaStr string, tagEdbIdMap map[string]int
  339. formulaFormStr := ReplaceFormula(newTagEdbIdMap, calculateMap, formulaStr)
  340. //计算公式异常,那么就移除该指标
  341. if formulaFormStr == `` {
  342. //removeDateList = append(removeDateList, sk)
  343. fmt.Println("异常了")
  344. continue
  345. }
  346. expression := formula.NewExpression(formulaFormStr)
  347. calResult, evaluateErr := expression.Evaluate()
  348. if evaluateErr != nil {
  349. // 分母为0的报错
  350. if strings.Contains(evaluateErr.Error(), "divide by zero") {
  351. //removeDateList = append(removeDateList, sk)
  352. continue
  353. }
  354. err = errors.New("计算失败:Err:" + evaluateErr.Error() + ";formulaStr:" + formulaFormStr)
  355. fmt.Println(err)
  356. continue
  357. }
  358. // 如果计算结果是NAN,那么就退出当前循环
  359. if calResult.IsNan() {
  360. continue
  361. }
  362. calVal, tmpErr := calResult.Float64()
  363. if tmpErr != nil {
  364. err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  365. fmt.Println(err)
  366. continue
  367. }
  368. //yDataMap[n] = calVal
  369. //xEdbInfoIdList = append(xEdbInfoIdList, n)
  370. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  371. yDataMap[tmpN] = calVal
  372. xEdbInfoIdList = append(xEdbInfoIdList, tmpN)
  373. findDataList = append(findDataList, calVal)
  374. }
  375. yName := barChartInfoDate.Name
  376. yNameEn := barChartInfoDate.Name
  377. if yName == `` {
  378. if barChartInfoDate.Type == 2 {
  379. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  380. if barChartInfoDate.Value == 1 {
  381. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  382. } else {
  383. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  384. }
  385. } else {
  386. yName = maxDate.Format(utils.FormatDate)
  387. yNameEn = maxDate.Format(utils.FormatDate)
  388. }
  389. }
  390. yDate := "0000-00-00"
  391. if !maxDate.IsZero() {
  392. yDate = maxDate.Format(utils.FormatDate)
  393. }
  394. {
  395. hasDataIndexList := make([]int, 0)
  396. for dataK, edbInfoId := range xEdbInfoIdList {
  397. if _, ok := noDataIdMap[edbInfoId]; !ok { // 如果是没有数据的指标id
  398. hasDataIndexList = append(hasDataIndexList, dataK)
  399. }
  400. }
  401. lenHasDataIndex := len(hasDataIndexList)
  402. if lenHasDataIndex > 0 {
  403. for lenHasDataI := 1; lenHasDataI < lenHasDataIndex; lenHasDataI++ {
  404. perK := hasDataIndexList[lenHasDataI-1] //上一个有数据的指标下标
  405. currK := hasDataIndexList[lenHasDataI] //当前有数据的指标下标
  406. preVal := findDataList[perK] //上一个有数据的坐标的值
  407. currVal := findDataList[currK] //当前有数据的指标的值
  408. // 环差值
  409. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(currK - perK)))
  410. var tmpI int64
  411. // 将两个中间的数据做平均值补全
  412. for hcI := perK + 1; hcI < currK; hcI++ {
  413. tmpI++
  414. findDataList[hcI], _ = decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(tmpI))).RoundCeil(4).Float64()
  415. }
  416. }
  417. }
  418. }
  419. yDataList = append(yDataList, data_manage.YData{
  420. Date: yDate,
  421. ConfigDate: realDateTime,
  422. Value: findDataList,
  423. NoDataEdbList: noDataIdList,
  424. XEdbInfoIdList: xEdbInfoIdList,
  425. Color: barChartInfoDate.Color,
  426. Name: yName,
  427. NameEn: yNameEn,
  428. EdbValMap: yDataMap,
  429. M: mList,
  430. })
  431. }
  432. return
  433. }
  434. // getFutureGoodEdbInfoList 获取适用的指标列表
  435. 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) {
  436. maxN := 36 //最大36期合约
  437. futureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
  438. futureGoodEdbInfoDateMap = make(map[string]*future_good.FutureGoodEdbInfo)
  439. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  440. if zlFutureGoodEdbInfo.RegionType == "国内" {
  441. startMonth := int(earliestDateTime.Month())
  442. if startMonth == 1 {
  443. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList
  444. } else {
  445. // 因为是下标,所以对应想下标是需要-1的
  446. index := startMonth - 1
  447. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList[index:]
  448. futureGoodEdbInfoList = append(futureGoodEdbInfoList, tmpFutureGoodEdbInfoList[:index]...)
  449. }
  450. lenFutureGoodEdbInfoList := len(futureGoodEdbInfoList)
  451. //futureGoodEdbInfoList
  452. //if isAllChina {
  453. //}
  454. // 如果全是国内指标,那么只需要拼上多出的几期合约即可
  455. maxN = lenFutureGoodEdbInfoList + monthNum
  456. for i := 1; i < maxN; i++ {
  457. k := i % lenFutureGoodEdbInfoList
  458. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, i, 0).Format(utils.FormatYearMonthDate)] = futureGoodEdbInfoList[k]
  459. }
  460. //需求池604,只要是国内合约,最大必须是12期
  461. newMaxN = 12
  462. return
  463. }
  464. for _, v := range tmpFutureGoodEdbInfoList {
  465. //海外的连续日期,目前
  466. if v.FutureGoodEdbType == 2 {
  467. if v.Month <= maxN {
  468. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, v.Month, 0).Format(utils.FormatYearMonthDate)] = v
  469. if v.Month > newMaxN {
  470. newMaxN = v.Month
  471. }
  472. }
  473. continue
  474. }
  475. if v.Year < earliestDateTime.Year() {
  476. continue
  477. }
  478. // 小于等于当前年,那么就肯定是ok的
  479. if v.Year == earliestDateTime.Year() && v.Month <= int(earliestDateTime.Month()) {
  480. continue
  481. }
  482. subYear := v.Year - earliestDateTime.Year()
  483. subMonth := v.Month - int(earliestDateTime.Month())
  484. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  485. tmpN := subYear*12 + subMonth
  486. if tmpN < maxN {
  487. tmpDateTime := time.Date(v.Year, time.Month(v.Month), 0, 0, 0, 0, 0, time.Local)
  488. futureGoodEdbInfoDateMap[tmpDateTime.Format(utils.FormatYearMonthDate)] = v
  489. if tmpN > newMaxN {
  490. newMaxN = tmpN
  491. }
  492. continue
  493. }
  494. }
  495. return
  496. }
  497. // handleProfitResultData 处理成最终的结果数据
  498. func handleProfitResultData(xDataListInit []data_manage.XData, baseEdbInfo *data_manage.EdbInfo, yDataList []data_manage.YData, earliestDateTime time.Time, allEdbInfoIds []int) (xDataList []data_manage.XData, newYDataList []data_manage.YData, err error) {
  499. newYDataList = yDataList
  500. xDataList = xDataListInit
  501. nMap := make(map[int]int)
  502. nList := make([]int, 0)
  503. nListEdbMap := make(map[int]struct{})
  504. for _, v := range yDataList {
  505. for _, n := range v.XEdbInfoIdList {
  506. if utils.InArrayByInt(allEdbInfoIds, n) {
  507. if _, ok := nListEdbMap[n]; !ok {
  508. nList = append(nList, n)
  509. nListEdbMap[n] = struct{}{}
  510. }
  511. } else {
  512. nMap[n] = n
  513. }
  514. }
  515. }
  516. // 找出所有的N值,并进行正序排列
  517. nListTmp := make([]int, 0)
  518. for _, n := range nMap {
  519. nListTmp = append(nListTmp, n)
  520. }
  521. sort.Slice(nListTmp, func(i, j int) bool {
  522. return nListTmp[i] < nListTmp[j]
  523. })
  524. nList = append(nList, nListTmp...)
  525. for _, n := range nList {
  526. if utils.InArrayByInt(allEdbInfoIds, n) {
  527. continue
  528. }
  529. xDataList = append(xDataList, data_manage.XData{
  530. Name: fmt.Sprint("M+", n),
  531. NameEn: fmt.Sprint("M+", n),
  532. })
  533. }
  534. for yIndex, yData := range yDataList {
  535. newYDataList[yIndex].XEdbInfoIdList = []int{}
  536. newYDataList[yIndex].Value = []float64{}
  537. tmpNList := nList
  538. xEdbInfoIdList := yData.XEdbInfoIdList
  539. valIndex := 0
  540. needNum := 0
  541. for _, n := range tmpNList {
  542. if len(xEdbInfoIdList) > 0 {
  543. currN := xEdbInfoIdList[0]
  544. // 当前距离最早的日期相差的N数
  545. if n == currN { // todo 改成所有的基础现货指标
  546. if needNum > 0 {
  547. currVal := yData.Value[valIndex]
  548. preVal := yData.Value[valIndex-1]
  549. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  550. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  551. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  552. // 赋值平均值
  553. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
  554. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  555. }
  556. }
  557. if utils.InArrayByInt(allEdbInfoIds, currN) {
  558. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN)
  559. } else {
  560. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN+1)
  561. }
  562. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  563. valIndex++
  564. needNum = 0
  565. if len(xEdbInfoIdList) > 0 {
  566. xEdbInfoIdList = xEdbInfoIdList[1:]
  567. }
  568. } else {
  569. needNum++
  570. }
  571. }
  572. }
  573. }
  574. maxI := 0
  575. for _, yData := range newYDataList {
  576. lenEdb := len(yData.XEdbInfoIdList)
  577. for i := 0; i < lenEdb; i++ {
  578. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  579. if maxI < i {
  580. maxI = i
  581. }
  582. }
  583. }
  584. }
  585. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  586. xDataList = xDataList[0 : maxI+1]
  587. for yIndex, yData := range newYDataList {
  588. if len(yData.XEdbInfoIdList) > maxI+1 {
  589. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  590. }
  591. if len(yData.Value) > maxI+1 {
  592. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  593. }
  594. currDate, _ := time.ParseInLocation(utils.FormatDate, yData.Date, time.Local)
  595. nameList := make([]string, 0)
  596. enNameList := make([]string, 0)
  597. for k1, n := range newYDataList[yIndex].XEdbInfoIdList {
  598. if utils.InArrayByInt(allEdbInfoIds, n) { // 现货价不处理
  599. tmpItem := xDataListInit[k1]
  600. nameList = append(nameList, tmpItem.Name)
  601. enNameList = append(enNameList, tmpItem.NameEn)
  602. continue
  603. }
  604. if n <= 0 {
  605. nameList = append(nameList, `无合约`)
  606. enNameList = append(enNameList, `no contract`)
  607. } else {
  608. var date string
  609. currDateTime := currDate.AddDate(0, n-1, 0)
  610. month := int(currDateTime.Month())
  611. date = fmt.Sprintf("%d%d", currDateTime.Year(), month)
  612. if month < 10 {
  613. date = fmt.Sprintf("%d0%d", currDateTime.Year(), month)
  614. }
  615. nameList = append(nameList, date)
  616. enNameList = append(enNameList, date)
  617. }
  618. }
  619. newYDataList[yIndex].NameList = nameList
  620. newYDataList[yIndex].EnNameList = enNameList
  621. }
  622. return
  623. }
  624. func CheckFormula(formula string) map[string]string {
  625. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  626. str := strings.ToUpper(formula)
  627. for _, v := range mathFormula {
  628. str = strings.Replace(str, v, "", -1)
  629. }
  630. str = strings.Replace(str, "(", "", -1)
  631. str = strings.Replace(str, ")", "", -1)
  632. byteMap := make(map[string]string)
  633. for i := 0; i < len(str); i++ {
  634. byteInt := str[i]
  635. if byteInt >= 65 && byteInt <= 90 {
  636. byteStr := string(byteInt)
  637. if _, ok := byteMap[byteStr]; !ok {
  638. byteMap[byteStr] = byteStr
  639. }
  640. }
  641. }
  642. return byteMap
  643. }
  644. func ReplaceFormula(tagEdbIdMap map[string]int, valArr map[int]float64, formulaStr string) string {
  645. funMap := GetFormulaMap()
  646. for k, v := range funMap {
  647. formulaStr = strings.Replace(formulaStr, k, v, -1)
  648. }
  649. replaceCount := 0
  650. for tag, edbInfoId := range tagEdbIdMap {
  651. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  652. dvStr := fmt.Sprintf("%v", val)
  653. formulaStr = strings.Replace(formulaStr, tag, dvStr, -1)
  654. replaceCount++
  655. }
  656. }
  657. for k, v := range funMap {
  658. formulaStr = strings.Replace(formulaStr, v, k, -1)
  659. }
  660. fmt.Println(formulaStr)
  661. if replaceCount == len(tagEdbIdMap) {
  662. return formulaStr
  663. } else {
  664. return ""
  665. }
  666. }
  667. func GetFormulaMap() map[string]string {
  668. funMap := make(map[string]string)
  669. funMap["MAX"] = "[@@]"
  670. funMap["MIN"] = "[@!]"
  671. funMap["ABS"] = "[@#]"
  672. funMap["CEIL"] = "[@$]"
  673. funMap["COS"] = "[@%]"
  674. funMap["FLOOR"] = "[@^]"
  675. funMap["MOD"] = "[@&]"
  676. funMap["POW"] = "[@*]"
  677. funMap["ROUND"] = "[@(]"
  678. return funMap
  679. }