profit_chart_info.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/models/future_good"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "github.com/dengsgo/math-engine/engine"
  10. "github.com/shopspring/decimal"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // ChartInfoDateReq 图表的日期数据(日期相关)
  17. type ChartInfoDateReq struct {
  18. Type int `description:"配置类型"`
  19. Date string `description:"固定日期"`
  20. Value int `description:"N天的值"`
  21. Color string `description:"颜色"`
  22. Name string `description:"别名"`
  23. }
  24. // SectionScatterReq 截面散点请求
  25. type SectionScatterReq struct {
  26. XName string `description:"x轴名称"`
  27. XNameEn string `description:"x轴名称(英文)"`
  28. XUnitName string `description:"x轴单位名称"`
  29. XUnitNameEn string `description:"x轴单位名称(英文)"`
  30. YName string `description:"y轴名称"`
  31. YNameEn string `description:"y轴名称(英文)"`
  32. YUnitName string `description:"y轴单位名称"`
  33. YUnitNameEn string `description:"y轴单位名称(英文)"`
  34. XMinValue string `description:"X轴的最小值"`
  35. XMaxValue string `description:"X轴的最大值"`
  36. YMinValue string `description:"Y轴的最小值"`
  37. YMaxValue string `description:"Y轴的最大值"`
  38. //EdbList []SectionScatterEdbItemReq `description:"指标数据"`
  39. SeriesList []SectionScatterSeriesItemReq `description:"系列数据"`
  40. }
  41. // SectionScatterSeriesItemReq 系列的请求
  42. type SectionScatterSeriesItemReq struct {
  43. Name string `description:"系列名"`
  44. NameEn string `description:"系列名(英文名)"`
  45. Color string `description:"颜色"`
  46. EdbInfoList []SectionScatterEdbItemReq
  47. ShowTrendLine bool `description:"是否展示趋势线"`
  48. ShowFitEquation bool `description:"是否展示方程式"`
  49. ShowRSquare bool `description:"是否展示R平方"`
  50. }
  51. // SectionScatterEdbItemReq 截面散点请求的指标
  52. type SectionScatterEdbItemReq struct {
  53. XEdbInfoId int `description:"X轴的指标ID"`
  54. YEdbInfoId int `description:"Y轴的指标ID"`
  55. Name string `description:"别名"`
  56. NameEn string `description:"英文别名"`
  57. XDateType int `description:"X轴的日期配置类型"`
  58. XDate string `description:"X轴的日期固定日期"`
  59. XDateValue int `description:"X轴的日期N天的值"`
  60. YDateType int `description:"Y轴的日期配置类型"`
  61. YDate string `description:"Y轴的日期固定日期"`
  62. YDateValue int `description:"Y轴的日期N天的值"`
  63. IsShow bool `description:"是否展示"`
  64. }
  65. // XData 商品价格曲线的的x轴数据
  66. type XData struct {
  67. Name string `description:"别名"`
  68. NameEn string `description:"英文别名"`
  69. }
  70. // YData 柱方图的y轴数据
  71. type YData struct {
  72. Date string `description:"数据日期"`
  73. ConfigDate time.Time `description:"配置的日期" json:"-"`
  74. Color string `description:"数据颜色"`
  75. Name string `description:"别名"`
  76. NameEn string `description:"英文别名"`
  77. Value []float64 `description:"每个指标的值"`
  78. NoDataEdbList []int `description:"没有数据的指标列表"`
  79. XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
  80. NameList []string `description:"每个值对应的名称"`
  81. EnNameList []string `description:"每个值对应的英文名称"`
  82. EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
  83. M []int `description:"对应开始日期的间隔值" json:"-"`
  84. }
  85. // BarChartInfoEdbItemReq 柱方图预览请求数据(指标相关)
  86. type BarChartInfoEdbItemReq struct {
  87. EdbInfoId int `description:"指标ID"`
  88. Name string `description:"别名"`
  89. NameEn string `description:"英文别名"`
  90. Source int `description:"1:ETA图库;2:商品价格"`
  91. }
  92. // ChartInfoReq 图表预览请求数据
  93. type ChartInfoReq struct {
  94. FutureGoodEdbInfoIdList []models.EdbInfoFromTag `description:"指标信息"`
  95. CalculateFormula string `description:"计算公式"`
  96. BaseEdbInfoId int `description:"基础的指标id"`
  97. DateList []ChartInfoDateReq `description:"日期配置"`
  98. ProfitNameEn string `description:"利润英文名称"`
  99. }
  100. // RefreshByChartId 根据图表id刷新图表
  101. func RefreshByChartId(chartInfoId int) (err error, errMsg string) {
  102. // 查找图表
  103. chartInfo, err := models.GetChartInfoById(chartInfoId)
  104. if err != nil {
  105. if err.Error() == utils.ErrNoRow() {
  106. err = nil
  107. }
  108. return
  109. }
  110. if chartInfo.Source != utils.CHART_SOURCE_FUTURE_GOOD_PROFIT {
  111. return
  112. }
  113. var extraConf ChartInfoReq
  114. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  115. if err != nil {
  116. errMsg = "商品利润曲线图配置异常"
  117. return
  118. }
  119. // 查找商品利润图表的扩展信息
  120. chartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
  121. if err = chartInfoFutureGoodProfit.GetItemById(chartInfoId); err != nil {
  122. errMsg = "获取失败"
  123. if err.Error() == utils.ErrNoRow() {
  124. err = nil
  125. }
  126. return
  127. }
  128. baseEdbInfo, err := models.GetEdbInfoById(extraConf.BaseEdbInfoId)
  129. if err != nil {
  130. errMsg = "获取失败"
  131. return
  132. }
  133. // 商品数据库指标
  134. futureGoodEdbInfoMap := make(map[int]*future_good.FutureGoodEdbInfo)
  135. zlFutureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
  136. for _, v := range extraConf.FutureGoodEdbInfoIdList {
  137. if _, ok := futureGoodEdbInfoMap[v.EdbInfoId]; ok {
  138. continue
  139. }
  140. zlFutureGoodEdbInfo, tmpErr := future_good.GetFutureGoodEdbInfo(v.EdbInfoId)
  141. if tmpErr != nil {
  142. err = tmpErr
  143. errMsg = "获取失败"
  144. return
  145. }
  146. futureGoodEdbInfoMap[v.EdbInfoId] = zlFutureGoodEdbInfo
  147. zlFutureGoodEdbInfoList = append(zlFutureGoodEdbInfoList, zlFutureGoodEdbInfo)
  148. }
  149. xDataList, yDataList, err := GetProfitChartEdbData(baseEdbInfo, zlFutureGoodEdbInfoList, extraConf.DateList, extraConf.CalculateFormula, extraConf.FutureGoodEdbInfoIdList)
  150. xDataListByte, err := json.Marshal(xDataList)
  151. if err != nil {
  152. errMsg = "保存失败"
  153. err = errors.New("X轴数据转换失败,ERR:" + err.Error())
  154. return
  155. }
  156. yDataListByte, err := json.Marshal(yDataList)
  157. if err != nil {
  158. errMsg = "保存失败"
  159. err = errors.New("Y轴数据转换失败,ERR:" + err.Error())
  160. return
  161. }
  162. extraUpdateCol := make([]string, 0)
  163. chartInfoFutureGoodProfit.XValue = string(xDataListByte)
  164. chartInfoFutureGoodProfit.YValue = string(yDataListByte)
  165. chartInfoFutureGoodProfit.ProfitName = zlFutureGoodEdbInfoList[0].FutureGoodEdbName + "盘面利润"
  166. chartInfoFutureGoodProfit.ModifyTime = time.Now()
  167. extraUpdateCol = []string{"XValue", "YValue", "ProfitName", "ModifyTime"}
  168. err = chartInfoFutureGoodProfit.Update(extraUpdateCol)
  169. return
  170. }
  171. // GetProfitChartEdbData 获取利润图表的指标数据
  172. func GetProfitChartEdbData(baseEdbInfo *models.EdbInfo, zlFutureGoodEdbInfoList []*future_good.FutureGoodEdbInfo, chartInfoDateList []ChartInfoDateReq, formulaStr string, edbInfoFromTagList []models.EdbInfoFromTag) (xDataList []XData, yDataList []YData, err error) {
  173. if baseEdbInfo == nil {
  174. err = errors.New("ETA指标未选取")
  175. return
  176. }
  177. if len(zlFutureGoodEdbInfoList) <= 0 {
  178. err = errors.New("商品指标未选取")
  179. return
  180. }
  181. // 标签与期货商品指标的关联关系
  182. tagEdbIdMap := make(map[string]int)
  183. // 有效的期货商品指标
  184. futureGoodEdbInfoIdMap := make(map[int]int)
  185. {
  186. tmpTagEdbIdMap := make(map[string]int)
  187. for _, v := range edbInfoFromTagList {
  188. tmpTagEdbIdMap[v.FromTag] = v.EdbInfoId
  189. }
  190. formulaMap := CheckFormula(formulaStr)
  191. for _, tag := range formulaMap {
  192. tagEdbIdMap[tag] = tmpTagEdbIdMap[tag]
  193. futureGoodEdbInfoIdMap[tmpTagEdbIdMap[tag]] = tmpTagEdbIdMap[tag]
  194. }
  195. }
  196. // 普通的指标数据
  197. baseDataList := make([]*models.EdbDataList, 0)
  198. baseDataList, err = models.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, "", "")
  199. if err != nil {
  200. return
  201. }
  202. latestDate := zlFutureGoodEdbInfoList[0].EndDate
  203. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  204. earliestDateTime := latestDateTime // 数据的最早日期,目的是为了找出最早的合约
  205. for _, barChartInfoDate := range chartInfoDateList {
  206. var findDateTime time.Time
  207. switch barChartInfoDate.Type {
  208. case 1: //最新值
  209. findDateTime = latestDateTime
  210. case 2: //近期几天
  211. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  212. case 3: // 固定日期
  213. //寻找固定日期的数据
  214. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  215. if tmpErr != nil {
  216. err = tmpErr
  217. return
  218. }
  219. findDateTime = tmpFindDateTime
  220. default:
  221. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  222. return
  223. }
  224. if findDateTime.IsZero() {
  225. err = errors.New("错误的日期")
  226. return
  227. }
  228. if findDateTime.Before(earliestDateTime) {
  229. earliestDateTime = findDateTime
  230. }
  231. }
  232. monthNum := (latestDateTime.Year()-earliestDateTime.Year())*12 + int(latestDateTime.Month()-earliestDateTime.Month())
  233. // 存储主力合约下的所有月份合约
  234. futureGoodEdbInfoDateMap := make(map[int]map[string]*future_good.FutureGoodEdbInfo)
  235. futureGoodDataListMap := make(map[int][]*models.EdbDataList, 0)
  236. // 特殊的商品期货合约(只有M+N的合约,没有固定日期的合约)
  237. specialFutureGoodEdbInfoMap := make(map[int]map[int]*future_good.FutureGoodEdbInfo, 0)
  238. isAllChina := true // 是否都是国内的期货合约
  239. for _, v := range zlFutureGoodEdbInfoList {
  240. if v.RegionType != "国内" {
  241. isAllChina = false
  242. break
  243. }
  244. }
  245. nMap := make(map[string]string)
  246. var maxN int // 最大N值
  247. for _, v := range zlFutureGoodEdbInfoList {
  248. // 如果不是有效的商品期货指标,那么就过滤掉,不做数据查询处理,避免没必要的请求
  249. if _, ok := futureGoodEdbInfoIdMap[v.FutureGoodEdbInfoId]; !ok {
  250. continue
  251. }
  252. // 获取期货指标以及期货数据
  253. tmpFutureGoodEdbInfoList, tmpErr := future_good.GetChildFutureGoodEdbInfoListByParentId(v.FutureGoodEdbInfoId)
  254. if tmpErr != nil {
  255. err = tmpErr
  256. return
  257. }
  258. childFutureGoodEdbInfoMap, tmpMaxN, tmpErr := getProfitFutureGoodEdbInfoList(earliestDateTime, v, tmpFutureGoodEdbInfoList, isAllChina, monthNum)
  259. if tmpErr != nil {
  260. err = tmpErr
  261. return
  262. }
  263. if maxN < tmpMaxN {
  264. maxN = tmpMaxN
  265. }
  266. futureGoodEdbInfoDateMap[v.FutureGoodEdbInfoId] = childFutureGoodEdbInfoMap
  267. if v.FutureGoodEdbType == 2 {
  268. specialFutureGoodEdbInfoMap[v.FutureGoodEdbInfoId] = make(map[int]*future_good.FutureGoodEdbInfo)
  269. }
  270. // 获取数据
  271. for date, childFutureGoodEdbInfo := range childFutureGoodEdbInfoMap {
  272. nMap[date] = date
  273. dataList := make([]*models.EdbDataList, 0)
  274. tmpDataList, tmpErr := future_good.GetFutureGoodEdbDataListByDate(childFutureGoodEdbInfo.FutureGoodEdbInfoId, "", "")
  275. if tmpErr != nil {
  276. return
  277. }
  278. for _, tmpData := range tmpDataList {
  279. dataList = append(dataList, &models.EdbDataList{
  280. EdbDataId: tmpData.FutureGoodEdbDataId,
  281. EdbInfoId: tmpData.FutureGoodEdbInfoId,
  282. DataTime: tmpData.DataTime.Format(utils.FormatDate),
  283. DataTimestamp: tmpData.DataTimestamp,
  284. Value: tmpData.Close,
  285. })
  286. }
  287. futureGoodDataListMap[childFutureGoodEdbInfo.FutureGoodEdbInfoId] = dataList
  288. if childFutureGoodEdbInfo.FutureGoodEdbType == 2 {
  289. specialFutureGoodEdbInfoMap[v.FutureGoodEdbInfoId][childFutureGoodEdbInfo.Month] = childFutureGoodEdbInfo
  290. }
  291. }
  292. }
  293. // 找出所有的N值,并进行正序排列
  294. dateList := make([]string, 0)
  295. for _, n := range nMap {
  296. dateList = append(dateList, n)
  297. }
  298. sort.Slice(dateList, func(i, j int) bool {
  299. return dateList[i] < dateList[j]
  300. })
  301. _, yDataList, err = ProfitChartChartData(baseDataList, futureGoodEdbInfoDateMap, futureGoodDataListMap, chartInfoDateList, baseEdbInfo.EndDate, specialFutureGoodEdbInfoMap, formulaStr, tagEdbIdMap, dateList, maxN)
  302. if err != nil {
  303. return
  304. }
  305. tmpXDataList, newYDataList, err := handleProfitResultData(baseEdbInfo, yDataList, earliestDateTime)
  306. if err != nil {
  307. return
  308. }
  309. xDataList = []XData{
  310. {
  311. Name: "现货利润",
  312. NameEn: "Spot Price",
  313. },
  314. }
  315. xDataList = append(xDataList, tmpXDataList...)
  316. yDataList = newYDataList
  317. return
  318. }
  319. // ProfitChartChartData 获取数据
  320. func ProfitChartChartData(baseDataList []*models.EdbDataList, futureGoodEdbInfoMap map[int]map[string]*future_good.FutureGoodEdbInfo, futureGoodEdbDataListMap map[int][]*models.EdbDataList, chartInfoDateList []ChartInfoDateReq, latestDate string, specialFutureGoodEdbInfoMap map[int]map[int]*future_good.FutureGoodEdbInfo, formulaStr string, tagEdbIdMap map[string]int, dateList []string, maxN int) (edbIdList []int, yDataList []YData, err error) {
  321. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  322. //earliestDateTime time.Time
  323. // ETA指标数据
  324. baseEdbDateData := make(map[string]float64)
  325. for _, edbData := range baseDataList {
  326. baseEdbDateData[edbData.DataTime] = edbData.Value
  327. }
  328. // 商品指标数据
  329. edbDataMap := make(map[int]map[string]float64)
  330. for edbInfoId, edbDataList := range futureGoodEdbDataListMap {
  331. edbDateData := make(map[string]float64)
  332. for _, edbData := range edbDataList {
  333. edbDateData[edbData.DataTime] = edbData.Value
  334. }
  335. edbDataMap[edbInfoId] = edbDateData
  336. }
  337. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
  338. yDataList = make([]YData, 0) //y轴的数据列表
  339. // 将计算公式中的字母转大写
  340. formulaStr = strings.ToUpper(formulaStr)
  341. for _, barChartInfoDate := range chartInfoDateList {
  342. yDataMap := make(map[int]float64)
  343. var maxDate time.Time
  344. var findDateTime time.Time
  345. switch barChartInfoDate.Type {
  346. case 1: //最新值
  347. findDateTime = latestDateTime
  348. case 2: //近期几天
  349. findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  350. case 3: // 固定日期
  351. //寻找固定日期的数据
  352. tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  353. if tmpErr != nil {
  354. err = tmpErr
  355. return
  356. }
  357. findDateTime = tmpFindDateTime
  358. default:
  359. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  360. return
  361. }
  362. if findDateTime.IsZero() {
  363. err = errors.New("错误的日期")
  364. return
  365. }
  366. findDataList := make([]float64, 0) // 当前日期的数据值
  367. noDataIdList := make([]int, 0) // 没有数据的指标id
  368. xEdbInfoIdList := make([]int, 0) // 当前数据的指标id列表
  369. // 现货指标
  370. realDateTime, findDataValue, isFind, tmpErr := GetNeedDateData(findDateTime, baseDataList, baseEdbDateData, edbDataMap)
  371. if tmpErr != nil {
  372. err = tmpErr
  373. return
  374. }
  375. findDataList = append(findDataList, findDataValue)
  376. yDataMap[0] = findDataValue
  377. if isFind {
  378. maxDate = realDateTime
  379. }
  380. xEdbInfoIdList = append(xEdbInfoIdList, 0)
  381. mList := make([]int, 0) // 间隔月份
  382. // 最小开始的n值
  383. //minN := (findDateTime.Year()-earliestDateTime.Year())*12 + int(findDateTime.Month()-earliestDateTime.Month())
  384. for _, date := range dateList {
  385. currDate, _ := time.ParseInLocation(utils.FormatYearMonthDate, date, time.Local)
  386. // 如果当前的n值小于最小开始的n值,那么就不处理
  387. //if n < minN {
  388. // continue
  389. //}
  390. //findDateTime
  391. // 获取当前日期相对开始日期的期数
  392. //tmpN := (currDate.Year()-findDateTime.Year())*12 + int(currDate.Month()-findDateTime.Month())
  393. // 用实际日期的月份作为基准,往前推12个月(2024-5-13 16:26:43修改)
  394. tmpN := (currDate.Year()-realDateTime.Year())*12 + int(currDate.Month()-realDateTime.Month())
  395. if tmpN <= 0 {
  396. continue
  397. }
  398. // 如果期数大于最大期数,那么就退出当前匹配
  399. if tmpN >= maxN {
  400. break
  401. }
  402. zlAndChildEdbId := make(map[int]int)
  403. childFutureGoodEdbInfoIdList := make([]int, 0)
  404. for zlFutureGoodEdbInfoId, futureGoodEdbInfoList := range futureGoodEdbInfoMap {
  405. // 判断是否特殊合约
  406. if childFutureGoodEdbInfoIdMap, ok := specialFutureGoodEdbInfoMap[zlFutureGoodEdbInfoId]; ok {
  407. if childFutureGoodEdbInfo, ok2 := childFutureGoodEdbInfoIdMap[tmpN]; ok2 {
  408. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  409. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  410. }
  411. } else {
  412. if childFutureGoodEdbInfo, ok2 := futureGoodEdbInfoList[date]; ok2 {
  413. childFutureGoodEdbInfoIdList = append(childFutureGoodEdbInfoIdList, childFutureGoodEdbInfo.FutureGoodEdbInfoId)
  414. zlAndChildEdbId[zlFutureGoodEdbInfoId] = childFutureGoodEdbInfo.FutureGoodEdbInfoId
  415. }
  416. }
  417. }
  418. // 合约不全,不参与计算
  419. //if len(childFutureGoodEdbInfoIdList) != lenZlFutureGoodEdbInfo {
  420. // continue
  421. //}
  422. calculateMap := make(map[int]float64)
  423. for _, childFutureGoodEdbInfoId := range childFutureGoodEdbInfoIdList {
  424. tmpFindDataValue, tmpIsFind := edbDataMap[childFutureGoodEdbInfoId][realDateTime.Format(utils.FormatDate)]
  425. if tmpIsFind && tmpFindDataValue != 0 {
  426. calculateMap[childFutureGoodEdbInfoId] = tmpFindDataValue
  427. }
  428. }
  429. // 合约的数据不全,不参与计算
  430. //if len(calculateMap) != lenZlFutureGoodEdbInfo {
  431. // continue
  432. //}
  433. newTagEdbIdMap := make(map[string]int)
  434. for tag, zlEdbId := range tagEdbIdMap {
  435. newTagEdbIdMap[tag] = zlAndChildEdbId[zlEdbId]
  436. }
  437. //, formulaStr string, tagEdbIdMap map[string]int
  438. formulaFormStr := ReplaceFormula(newTagEdbIdMap, calculateMap, formulaStr)
  439. //计算公式异常,那么就移除该指标
  440. if formulaFormStr == `` {
  441. //removeDateList = append(removeDateList, sk)
  442. //fmt.Println("异常了")
  443. continue
  444. }
  445. //expression := formula.NewExpression(formulaFormStr)
  446. //calResult, evaluateErr := expression.Evaluate()
  447. //if evaluateErr != nil {
  448. // // 分母为0的报错
  449. // if strings.Contains(evaluateErr.Error(), "divide by zero") {
  450. // //removeDateList = append(removeDateList, sk)
  451. // continue
  452. // }
  453. // err = errors.New("计算失败:Err:" + evaluateErr.Error() + ";formulaStr:" + formulaFormStr)
  454. // fmt.Println(err)
  455. // continue
  456. //}
  457. //// 如果计算结果是NAN,那么就退出当前循环
  458. //if calResult.IsNan() {
  459. // continue
  460. //}
  461. //calVal, tmpErr := calResult.Float64()
  462. //if tmpErr != nil {
  463. // err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  464. // fmt.Println(err)
  465. // continue
  466. //}
  467. calVal, err := engine.ParseAndExec(formulaFormStr)
  468. //calVal, err := calResult.Float64()
  469. if err != nil {
  470. // 分母为0的报错,忽略该循环
  471. if utils.IsDivideZero(err) {
  472. //removeDateList = append(removeDateList, sk)
  473. continue
  474. }
  475. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  476. fmt.Println(err)
  477. return nil, nil, err
  478. }
  479. //nanCheck := fmt.Sprintf("%0.f", calVal)
  480. //yDataMap[n] = calVal
  481. //xEdbInfoIdList = append(xEdbInfoIdList, n)
  482. nanCheck := fmt.Sprintf("%0.f", calVal)
  483. // 分母为0.0的报错
  484. if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
  485. continue
  486. }
  487. calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
  488. yDataMap[tmpN] = calVal
  489. xEdbInfoIdList = append(xEdbInfoIdList, tmpN)
  490. findDataList = append(findDataList, calVal)
  491. }
  492. yName := barChartInfoDate.Name
  493. yNameEn := barChartInfoDate.Name
  494. if yName == `` {
  495. if barChartInfoDate.Type == 2 {
  496. yName = strconv.Itoa(barChartInfoDate.Value) + "天前"
  497. if barChartInfoDate.Value == 1 {
  498. yNameEn = strconv.Itoa(barChartInfoDate.Value) + "day ago"
  499. } else {
  500. yNameEn = strconv.Itoa(barChartInfoDate.Value) + " days ago"
  501. }
  502. } else {
  503. yName = maxDate.Format(utils.FormatDate)
  504. yNameEn = maxDate.Format(utils.FormatDate)
  505. }
  506. }
  507. yDate := "0000-00-00"
  508. if !maxDate.IsZero() {
  509. yDate = maxDate.Format(utils.FormatDate)
  510. }
  511. yDataList = append(yDataList, YData{
  512. Date: yDate,
  513. ConfigDate: realDateTime,
  514. Value: findDataList,
  515. NoDataEdbList: noDataIdList,
  516. XEdbInfoIdList: xEdbInfoIdList,
  517. Color: barChartInfoDate.Color,
  518. Name: yName,
  519. NameEn: yNameEn,
  520. EdbValMap: yDataMap,
  521. M: mList,
  522. })
  523. }
  524. return
  525. }
  526. // getFutureGoodEdbInfoList 获取适用的指标列表
  527. 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) {
  528. maxN := 36 //最大36期合约
  529. futureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
  530. futureGoodEdbInfoDateMap = make(map[string]*future_good.FutureGoodEdbInfo)
  531. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  532. if zlFutureGoodEdbInfo.RegionType == "国内" {
  533. startMonth := int(earliestDateTime.Month())
  534. if startMonth == 1 {
  535. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList
  536. } else {
  537. // 因为是下标,所以对应想下标是需要-1的
  538. index := startMonth - 1
  539. futureGoodEdbInfoList = tmpFutureGoodEdbInfoList[index:]
  540. futureGoodEdbInfoList = append(futureGoodEdbInfoList, tmpFutureGoodEdbInfoList[:index]...)
  541. }
  542. lenFutureGoodEdbInfoList := len(futureGoodEdbInfoList)
  543. //futureGoodEdbInfoList
  544. //if isAllChina {
  545. // // 如果全是国内指标,那么只需要拼上多出的几期合约即可
  546. // maxN = lenFutureGoodEdbInfoList + monthNum
  547. //}
  548. // 如果全是国内指标,那么只需要拼上多出的几期合约即可
  549. maxN = lenFutureGoodEdbInfoList + monthNum
  550. for i := 1; i < maxN; i++ {
  551. k := i % lenFutureGoodEdbInfoList
  552. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, i, 0).Format(utils.FormatYearMonthDate)] = futureGoodEdbInfoList[k]
  553. if i > newMaxN {
  554. newMaxN = i
  555. }
  556. }
  557. //需求池604,只要是国内合约,最大必须是12期
  558. newMaxN = 12
  559. return
  560. }
  561. for _, v := range tmpFutureGoodEdbInfoList {
  562. //海外的连续日期,目前
  563. if v.FutureGoodEdbType == 2 {
  564. if v.Month <= newMaxN {
  565. futureGoodEdbInfoDateMap[earliestDateTime.AddDate(0, v.Month, 0).Format(utils.FormatYearMonthDate)] = v
  566. if v.Month > newMaxN {
  567. newMaxN = v.Month
  568. }
  569. }
  570. continue
  571. }
  572. if v.Year < earliestDateTime.Year() {
  573. continue
  574. }
  575. // 小于等于当前年,那么就肯定是ok的
  576. if v.Year == earliestDateTime.Year() && v.Month <= int(earliestDateTime.Month()) {
  577. continue
  578. }
  579. subYear := v.Year - earliestDateTime.Year()
  580. subMonth := v.Month - int(earliestDateTime.Month())
  581. // 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
  582. tmpN := subYear*12 + subMonth
  583. if tmpN < newMaxN {
  584. tmpDateTime := time.Date(v.Year, time.Month(v.Month), 0, 0, 0, 0, 0, time.Local)
  585. futureGoodEdbInfoDateMap[tmpDateTime.Format(utils.FormatYearMonthDate)] = v
  586. if tmpN > newMaxN {
  587. newMaxN = tmpN
  588. }
  589. continue
  590. }
  591. }
  592. return
  593. }
  594. // handleProfitResultData 处理成最终的结果数据
  595. func handleProfitResultData(baseEdbInfo *models.EdbInfo, yDataList []YData, earliestDateTime time.Time) (xDataList []XData, newYDataList []YData, err error) {
  596. xDataList = make([]XData, 0)
  597. newYDataList = yDataList
  598. nMap := make(map[int]int)
  599. for _, v := range yDataList {
  600. for _, n := range v.XEdbInfoIdList {
  601. nMap[n] = n
  602. }
  603. }
  604. // 找出所有的N值,并进行正序排列
  605. nList := make([]int, 0)
  606. for _, n := range nMap {
  607. nList = append(nList, n)
  608. }
  609. sort.Slice(nList, func(i, j int) bool {
  610. return nList[i] < nList[j]
  611. })
  612. for _, n := range nList {
  613. if n == 0 {
  614. continue
  615. }
  616. xDataList = append(xDataList, XData{
  617. Name: fmt.Sprint("M+", n),
  618. NameEn: fmt.Sprint("M+", n),
  619. })
  620. }
  621. for yIndex, yData := range yDataList {
  622. newYDataList[yIndex].XEdbInfoIdList = []int{}
  623. newYDataList[yIndex].Value = []float64{}
  624. tmpNList := nList
  625. xEdbInfoIdList := yData.XEdbInfoIdList
  626. valIndex := 0
  627. needNum := 0
  628. for _, n := range tmpNList {
  629. if len(xEdbInfoIdList) > 0 {
  630. currN := xEdbInfoIdList[0]
  631. // 当前距离最早的日期相差的N数
  632. if n == currN {
  633. if needNum > 0 {
  634. currVal := yData.Value[valIndex]
  635. preVal := yData.Value[valIndex-1]
  636. hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
  637. for tmpNum := 0; tmpNum < needNum; tmpNum++ {
  638. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
  639. // 赋值平均值
  640. tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).Round(4).Float64()
  641. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
  642. }
  643. }
  644. newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, currN+1)
  645. newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
  646. valIndex++
  647. needNum = 0
  648. if len(xEdbInfoIdList) > 0 {
  649. xEdbInfoIdList = xEdbInfoIdList[1:]
  650. }
  651. } else {
  652. needNum++
  653. }
  654. }
  655. }
  656. }
  657. maxI := 0
  658. for _, yData := range newYDataList {
  659. lenEdb := len(yData.XEdbInfoIdList)
  660. for i := 0; i < lenEdb; i++ {
  661. if yData.XEdbInfoIdList[i] != 0 && !utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
  662. if maxI < i {
  663. maxI = i
  664. }
  665. }
  666. }
  667. }
  668. earliestDateTime = time.Date(earliestDateTime.Year(), earliestDateTime.Month(), 1, 0, 0, 0, 0, time.Local)
  669. xDataList = xDataList[0:maxI]
  670. for yIndex, yData := range newYDataList {
  671. if len(yData.XEdbInfoIdList) > maxI+1 {
  672. newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0 : maxI+1]
  673. }
  674. if len(yData.Value) > maxI+1 {
  675. newYDataList[yIndex].Value = yData.Value[0 : maxI+1]
  676. }
  677. currDate, _ := time.ParseInLocation(utils.FormatDate, yData.Date, time.Local)
  678. nameList := make([]string, 0)
  679. enNameList := make([]string, 0)
  680. for _, n := range newYDataList[yIndex].XEdbInfoIdList {
  681. if n == 1 { // 现货价不处理
  682. nameList = append(nameList, baseEdbInfo.EdbName)
  683. enNameList = append(enNameList, baseEdbInfo.EdbNameEn)
  684. continue
  685. }
  686. if n <= 0 {
  687. nameList = append(nameList, `无合约`)
  688. enNameList = append(enNameList, `no contract`)
  689. } else {
  690. var date string
  691. currDateTime := currDate.AddDate(0, n-1, 0)
  692. month := int(currDateTime.Month())
  693. date = fmt.Sprintf("%d%d", currDateTime.Year(), month)
  694. if month < 10 {
  695. date = fmt.Sprintf("%d0%d", currDateTime.Year(), month)
  696. }
  697. nameList = append(nameList, date)
  698. enNameList = append(enNameList, date)
  699. }
  700. }
  701. newYDataList[yIndex].NameList = nameList
  702. newYDataList[yIndex].EnNameList = enNameList
  703. }
  704. return
  705. }
  706. func CheckFormula(formula string) map[string]string {
  707. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  708. str := strings.ToUpper(formula)
  709. for _, v := range mathFormula {
  710. str = strings.Replace(str, v, "", -1)
  711. }
  712. str = strings.Replace(str, "(", "", -1)
  713. str = strings.Replace(str, ")", "", -1)
  714. byteMap := make(map[string]string)
  715. for i := 0; i < len(str); i++ {
  716. byteInt := str[i]
  717. if byteInt >= 65 && byteInt <= 90 {
  718. byteStr := string(byteInt)
  719. if _, ok := byteMap[byteStr]; !ok {
  720. byteMap[byteStr] = byteStr
  721. }
  722. }
  723. }
  724. return byteMap
  725. }
  726. func ReplaceFormula(tagEdbIdMap map[string]int, valArr map[int]float64, formulaStr string) string {
  727. funMap := GetFormulaMap()
  728. for k, v := range funMap {
  729. formulaStr = strings.Replace(formulaStr, k, v, -1)
  730. }
  731. replaceCount := 0
  732. for tag, edbInfoId := range tagEdbIdMap {
  733. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  734. dvStr := fmt.Sprintf("%v", val)
  735. formulaStr = strings.Replace(formulaStr, tag, dvStr, -1)
  736. replaceCount++
  737. }
  738. }
  739. for k, v := range funMap {
  740. formulaStr = strings.Replace(formulaStr, v, k, -1)
  741. }
  742. if replaceCount == len(tagEdbIdMap) {
  743. return formulaStr
  744. } else {
  745. return ""
  746. }
  747. }
  748. func GetFormulaMap() map[string]string {
  749. funMap := make(map[string]string)
  750. funMap["MAX"] = "[@@]"
  751. funMap["MIN"] = "[@!]"
  752. funMap["ABS"] = "[@#]"
  753. funMap["CEIL"] = "[@$]"
  754. funMap["COS"] = "[@%]"
  755. funMap["FLOOR"] = "[@^]"
  756. funMap["MOD"] = "[@&]"
  757. funMap["POW"] = "[@*]"
  758. funMap["ROUND"] = "[@(]"
  759. return funMap
  760. }
  761. // GetNeedDateData 获取合约内需要的日期数据
  762. func GetNeedDateData(needDateTime time.Time, dataList []*models.EdbDataList, edbDataMap map[string]float64, allEdbDataMap map[int]map[string]float64) (findDateTime time.Time, findDataValue float64, isFind bool, err error) {
  763. //dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  764. if len(dataList) <= 0 {
  765. // 没有数据的指标id
  766. return
  767. }
  768. //最早的日期
  769. minDateTime, err := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  770. if err != nil {
  771. return
  772. }
  773. // 该日期存在数据的期货指标的最小数量,目前是现货和期货各1个,总共2个
  774. maxCount := 1
  775. for tmpDateTime := needDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  776. tmpDate := tmpDateTime.Format(utils.FormatDate)
  777. tmpValue, ok := edbDataMap[tmpDate]
  778. if !ok {
  779. continue
  780. }
  781. // 该日期存在数据的指标数量
  782. count := 0
  783. for _, currEdbDataMap := range allEdbDataMap {
  784. _, tmpIsFind := currEdbDataMap[tmpDate]
  785. if tmpIsFind {
  786. count++
  787. if count >= maxCount {
  788. continue
  789. }
  790. }
  791. }
  792. // 该日期存在数据的期货指标数量小于2个,那么要继续往前找
  793. if count < maxCount {
  794. continue
  795. }
  796. //如果能找到数据,那么就返回
  797. // 数据为0,也直接返回,做无值处理
  798. if tmpValue == 0 {
  799. return
  800. }
  801. findDateTime, _ = time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  802. findDataValue = tmpValue
  803. isFind = true
  804. return
  805. }
  806. return
  807. }