chart.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. package cross_variety
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. cross_varietyModel "eta/eta_mobile/models/data_manage/cross_variety"
  7. "eta/eta_mobile/models/data_manage/cross_variety/request"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/services/data"
  10. "eta/eta_mobile/utils"
  11. "fmt"
  12. "github.com/shopspring/decimal"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // ChartInfoResp 截面散点图数据
  18. type ChartInfoResp struct {
  19. XName string `description:"x轴名称"`
  20. XNameEn string `description:"x轴名称(英文)"`
  21. XUnitName string `description:"x轴单位名称"`
  22. XUnitNameEn string `description:"x轴单位名称(英文)"`
  23. YName string `description:"y轴名称"`
  24. YNameEn string `description:"y轴名称(英文)"`
  25. YUnitName string `description:"y轴单位名称"`
  26. YUnitNameEn string `description:"y轴单位名称(英文)"`
  27. XMinValue string `description:"X轴的最小值"`
  28. XMaxValue string `description:"X轴的最大值"`
  29. YMinValue string `description:"Y轴的最小值"`
  30. YMaxValue string `description:"Y轴的最大值"`
  31. DataList []SectionScatterSeriesItemResp `description:"数据列"`
  32. }
  33. // SectionScatterSeriesItemResp 系列的返回
  34. type SectionScatterSeriesItemResp struct {
  35. Name string `description:"系列名"`
  36. NameEn string `description:"系列名(英文)"`
  37. Color string `description:"颜色"`
  38. CoordinatePointData []CoordinatePoint `description:"趋势线的前后坐标点"`
  39. }
  40. // SectionScatterEdbItemResp 截面散点的返回参数
  41. type SectionScatterEdbItemResp struct {
  42. XEdbInfoId int `description:"X轴指标id"`
  43. XDate string `description:"X轴指标实际日期"`
  44. XName string `description:"X轴指标名称"`
  45. XNameEn string `description:"X轴指标英文名称"`
  46. XValue float64 `description:"X轴实际值"`
  47. YEdbInfoId int `description:"Y轴指标id"`
  48. YDate string `description:"Y轴指标实际日期"`
  49. YName string `description:"Y轴指标名称"`
  50. YNameEn string `description:"Y轴指标英文名称"`
  51. YValue float64 `description:"Y轴实际值"`
  52. IsShow bool `description:"是否展示"`
  53. Name string `description:"标签名称"`
  54. NameEn string `description:"英文标签名称"`
  55. }
  56. // CoordinatePoint 坐标点
  57. type CoordinatePoint struct {
  58. X float64
  59. Y float64
  60. XEdbInfoId int
  61. YEdbInfoId int
  62. XDate string
  63. YDate string
  64. DateType int `description:"日期类型:1-最新日期;2-N天前;3-固定日期"`
  65. DaysAgo int `description:"N天前的N值"`
  66. ShowTips int `description:"是否显示标注:0-否;1-是"`
  67. }
  68. // GetChartData
  69. // @Description: 获取跨品种分析图表数据
  70. // @author: Roc
  71. // @datetime 2023-11-24 09:42:59
  72. // @param chartInfoId int
  73. // @param config request.ChartConfigReq
  74. // @return edbList []*data_manage.ChartEdbInfoMapping
  75. // @return dataResp ChartInfoResp
  76. // @return err error
  77. // @return errMsg string
  78. // @return isSendEmail bool
  79. func GetChartData(chartInfoId int, config request.ChartConfigReq) (edbList []*data_manage.ChartEdbInfoMapping, dataResp ChartInfoResp, err error, errMsg string, isSendEmail bool) {
  80. moveUnitDays, ok := utils.FrequencyDaysMap[config.CalculateUnit]
  81. if !ok {
  82. errMsg = "错误的分析周期"
  83. err = errors.New(errMsg)
  84. isSendEmail = false
  85. return
  86. }
  87. isSendEmail = true
  88. // 品种map
  89. varietyMap := make(map[int]*cross_varietyModel.ChartVariety)
  90. {
  91. varietyList, tmpErr := cross_varietyModel.GetVarietyListByIdList(config.VarietyList)
  92. if tmpErr != nil {
  93. err = tmpErr
  94. return
  95. }
  96. for _, v := range varietyList {
  97. varietyMap[v.ChartVarietyId] = v
  98. }
  99. }
  100. // 标签m
  101. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  102. {
  103. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{config.TagX, config.TagY})
  104. if tmpErr != nil {
  105. err = tmpErr
  106. return
  107. }
  108. for _, v := range tagList {
  109. if v.ChartTagId == config.TagX {
  110. xTagInfo = v
  111. } else if v.ChartTagId == config.TagY {
  112. yTagInfo = v
  113. }
  114. }
  115. }
  116. if xTagInfo == nil {
  117. errMsg = "找不到对应的X轴标签"
  118. err = errors.New(errMsg)
  119. return
  120. }
  121. if yTagInfo == nil {
  122. errMsg = "找不到对应的Y轴标签"
  123. err = errors.New(errMsg)
  124. return
  125. }
  126. xVarietyEdbMap, yVarietyEdbMap, edbInfoIdList, err := GetXYEdbIdList(config.TagX, config.TagY, config.VarietyList)
  127. if err != nil {
  128. return
  129. }
  130. if len(edbInfoIdList) <= 0 {
  131. errMsg = "品种未配置指标"
  132. err = errors.New(errMsg)
  133. isSendEmail = false
  134. return
  135. }
  136. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  137. if err != nil {
  138. errMsg = "获取指标信息失败"
  139. err = errors.New("获取指标信息失败,ERR:" + err.Error())
  140. return
  141. }
  142. // 指标对应的所有数据
  143. chartType := 1 //1:普通图,2:季节性图
  144. calendar := "公历"
  145. edbDataListMap, edbList, err := data.GetEdbDataMapList(chartInfoId, chartType, calendar, "", "", mappingList, "")
  146. if err != nil {
  147. return
  148. }
  149. currDay := time.Now()
  150. currDay = time.Date(currDay.Year(), currDay.Month(), currDay.Day(), 0, 0, 0, 0, time.Local)
  151. dataMap := make(map[string]float64)
  152. dateMap := make(map[string]string)
  153. dateTypeMap := make(map[int]int) // 日期配置key对应的日期类型
  154. daysAgoMap := make(map[int]int) // 日期配置key对应的N天前的N值
  155. showTipsMap := make(map[int]int) // 日期配置key对应点是否显示标注
  156. for dateIndex, dateConfig := range config.DateConfigList {
  157. dateTypeMap[dateIndex] = dateConfig.DateType
  158. daysAgoMap[dateIndex] = dateConfig.Num
  159. showTipsMap[dateIndex] = dateConfig.ShowTips
  160. for _, edbInfoMapping := range mappingList {
  161. // 数据会是正序的
  162. dataList, ok := edbDataListMap[edbInfoMapping.EdbInfoId]
  163. if !ok {
  164. continue
  165. }
  166. lenData := len(dataList)
  167. if lenData <= 0 {
  168. continue
  169. }
  170. // 数据的开始索引
  171. k := lenData - 1
  172. // 数据的最晚日期
  173. dataEndDateStr := dataList[k].DataTime
  174. dataEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, dataEndDateStr, time.Local)
  175. if tmpErr != nil {
  176. err = tmpErr
  177. return
  178. }
  179. // 数据开始日期
  180. endDateStr := ``
  181. var endDate time.Time
  182. var currVal float64
  183. switch dateConfig.DateType {
  184. case 1: // 1:最新日期;
  185. endDateStr = dataEndDateStr
  186. endDate = dataEndDate
  187. currVal = dataList[k].Value
  188. case 2: // 2:N天前(原为指标最新日期的N天前, 现为系统日期的N天)
  189. tmpEndDate := time.Now().AddDate(0, 0, -dateConfig.Num)
  190. tmpEndDateStr := tmpEndDate.Format(utils.FormatDate)
  191. for i := k; i >= 0; i-- {
  192. tmpDateStr := dataList[i].DataTime
  193. // 如果正好是这一天,那么就直接break了
  194. if tmpEndDateStr == tmpDateStr {
  195. k = i
  196. endDateStr = tmpDateStr
  197. endDate = tmpEndDate
  198. currVal = dataList[i].Value
  199. break
  200. }
  201. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  202. if tmpErr != nil {
  203. err = tmpErr
  204. return
  205. }
  206. // 如果这期的日期晚于选择的日期,那么继续遍历
  207. if tmpDate.After(tmpEndDate) {
  208. continue
  209. }
  210. k = i
  211. endDateStr = tmpDateStr
  212. endDate = tmpDate
  213. currVal = dataList[i].Value
  214. break
  215. }
  216. case 3: // 固定日期
  217. if dateConfig.FixDate == "" {
  218. errMsg = "固定日期不可为空"
  219. err = fmt.Errorf("固定日期为空")
  220. return
  221. }
  222. strFixDate := dateConfig.FixDate
  223. fixDate, e := time.ParseInLocation(utils.FormatDate, strFixDate, time.Local)
  224. if e != nil {
  225. errMsg = "固定日期格式有误"
  226. err = fmt.Errorf("固定日期有误, FixDate: %s", dateConfig.FixDate)
  227. return
  228. }
  229. for i := k; i >= 0; i-- {
  230. strThisDate := dataList[i].DataTime
  231. // 如果正好是这一天,那么就直接break了
  232. if strFixDate == strThisDate {
  233. k = i
  234. endDateStr = strThisDate
  235. endDate = fixDate
  236. currVal = dataList[i].Value
  237. break
  238. }
  239. // 若固定日期无值, 则取固定日期之前, 能找到的第一个值(同上N天前的逻辑)
  240. thisDate, e := time.ParseInLocation(utils.FormatDate, strThisDate, time.Local)
  241. if e != nil {
  242. err = fmt.Errorf("数据日期格式有误: %s", e.Error())
  243. return
  244. }
  245. if thisDate.After(fixDate) {
  246. continue
  247. }
  248. k = i
  249. endDateStr = strThisDate
  250. endDate = thisDate
  251. currVal = dataList[i].Value
  252. break
  253. }
  254. }
  255. // 没有找到日期,那么就不处理
  256. if endDateStr == `` || endDate.IsZero() {
  257. continue
  258. }
  259. // 最早的日期
  260. earliestDate := endDate.AddDate(0, 0, -config.CalculateValue*moveUnitDays)
  261. earliestDateStr := earliestDate.Format(utils.FormatDate)
  262. var percentVal float64 // 百分位计算值
  263. // 百分位数据区间算法
  264. if config.PercentType == utils.PercentCalculateTypeRange {
  265. var minVal, maxVal float64
  266. var isNotFirst bool // 是否是第一条数据
  267. for i := k; i >= 0; i-- {
  268. tmpData := dataList[i]
  269. if !isNotFirst {
  270. maxVal = tmpData.Value
  271. minVal = tmpData.Value
  272. isNotFirst = true
  273. continue
  274. }
  275. tmpDateStr := dataList[i].DataTime
  276. // 如果正好是这一天,那么就直接break了
  277. if earliestDateStr == tmpDateStr {
  278. break
  279. }
  280. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  281. if tmpErr != nil {
  282. err = tmpErr
  283. return
  284. }
  285. // 如果这期的日期早于选择的日期,那么继续停止遍历
  286. if tmpDate.Before(earliestDate) {
  287. continue
  288. }
  289. if tmpData.Value > maxVal {
  290. maxVal = tmpData.Value
  291. }
  292. if tmpData.Value < minVal {
  293. minVal = tmpData.Value
  294. }
  295. }
  296. // 最大值等于最小值,说明计算结果无效
  297. if maxVal == minVal {
  298. continue
  299. }
  300. // 数据区间百分位=(现值-Min)/(Max-Min)
  301. tmpV := (currVal - minVal) / (maxVal - minVal) * 100
  302. percentVal, _ = decimal.NewFromFloat(tmpV).Round(4).Float64()
  303. }
  304. // 百分位数据个数算法
  305. // 数据区间第一个和最后一个数据点的时间和数据分别为(T1,S1)(T2,S2); N=T1到T2指标数据个数, n=小于等于S2的数据个数
  306. // 个数百分位=(n-1)/(N-1)
  307. if config.PercentType == utils.PercentCalculateTypeNum {
  308. // T1为earliestDate, T2为endDate, S2为currVal
  309. var tinyN, bigN int
  310. lastVal := decimal.NewFromFloat(currVal)
  311. for i := k; i >= 0; i-- {
  312. date, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  313. if e != nil {
  314. err = fmt.Errorf("数据日期格式有误: %s", e.Error())
  315. return
  316. }
  317. if !date.Before(earliestDate) && !date.After(endDate) {
  318. bigN += 1
  319. dateVal := decimal.NewFromFloat(dataList[i].Value)
  320. if dateVal.LessThanOrEqual(lastVal) {
  321. tinyN += 1
  322. }
  323. }
  324. }
  325. // N=1时说明计算无效
  326. if bigN == 1 {
  327. continue
  328. }
  329. numerator := decimal.NewFromInt(int64(tinyN - 1))
  330. denominator := decimal.NewFromInt(int64(bigN - 1))
  331. percentVal, _ = numerator.Div(denominator).Mul(decimal.NewFromFloat(100)).Round(4).Float64()
  332. }
  333. // key的生成(日期配置下标+指标id)
  334. key := fmt.Sprint(dateIndex, "_", edbInfoMapping.EdbInfoId)
  335. dataMap[key] = percentVal
  336. dateMap[key] = endDateStr
  337. }
  338. }
  339. // 返回数据处理
  340. dataList := make([]SectionScatterSeriesItemResp, 0)
  341. var xMinVal, xMaxVal, yMinVal, yMaxVal float64
  342. var isNotFirst bool
  343. for _, varietyId := range config.VarietyList {
  344. xEdbInfoId, ok1 := xVarietyEdbMap[varietyId]
  345. if !ok1 {
  346. continue
  347. }
  348. yEdbInfoId, ok2 := yVarietyEdbMap[varietyId]
  349. if !ok2 {
  350. continue
  351. }
  352. variety, ok := varietyMap[varietyId]
  353. if !ok {
  354. continue
  355. }
  356. coordinatePointList := make([]CoordinatePoint, 0)
  357. for dateIndex, _ := range config.DateConfigList {
  358. key1 := fmt.Sprint(dateIndex, "_", xEdbInfoId)
  359. xVal, ok1 := dataMap[key1]
  360. if !ok1 {
  361. continue
  362. }
  363. key2 := fmt.Sprint(dateIndex, "_", yEdbInfoId)
  364. yVal, ok2 := dataMap[key2]
  365. if !ok2 {
  366. continue
  367. }
  368. if !isNotFirst {
  369. xMinVal = xVal
  370. xMaxVal = xVal
  371. yMinVal = yVal
  372. yMaxVal = yVal
  373. isNotFirst = true
  374. } else {
  375. if xVal < xMinVal {
  376. xMinVal = xVal
  377. }
  378. if xVal > xMaxVal {
  379. xMaxVal = xVal
  380. }
  381. if yVal < yMinVal {
  382. yMinVal = yVal
  383. }
  384. if yVal > yMaxVal {
  385. yMaxVal = yVal
  386. }
  387. }
  388. coordinatePointList = append(coordinatePointList, CoordinatePoint{
  389. X: xVal,
  390. Y: yVal,
  391. XEdbInfoId: xEdbInfoId,
  392. YEdbInfoId: yEdbInfoId,
  393. XDate: dateMap[key1],
  394. YDate: dateMap[key2],
  395. DateType: dateTypeMap[dateIndex], // 日期类型
  396. DaysAgo: daysAgoMap[dateIndex], // N天前的N值
  397. ShowTips: showTipsMap[dateIndex], // 是否显示标注
  398. })
  399. }
  400. dataList = append(dataList, SectionScatterSeriesItemResp{
  401. Name: variety.ChartVarietyName,
  402. NameEn: variety.ChartVarietyNameEn,
  403. Color: "",
  404. CoordinatePointData: coordinatePointList,
  405. })
  406. }
  407. // 处理颜色
  408. colorMap := utils.GetColorMap()
  409. for k, _ := range dataList {
  410. if c, ok1 := colorMap[k]; ok1 {
  411. dataList[k].Color = c
  412. }
  413. }
  414. dataResp = ChartInfoResp{
  415. XName: xTagInfo.ChartTagName + "百分位",
  416. XNameEn: xTagInfo.ChartTagNameEn,
  417. XUnitName: "%",
  418. XUnitNameEn: "%",
  419. YName: yTagInfo.ChartTagName + "百分位",
  420. YNameEn: yTagInfo.ChartTagNameEn,
  421. YUnitName: "%",
  422. YUnitNameEn: "%",
  423. XMinValue: fmt.Sprint(xMinVal),
  424. XMaxValue: fmt.Sprint(xMaxVal),
  425. YMinValue: fmt.Sprint(yMinVal),
  426. YMaxValue: fmt.Sprint(yMaxVal),
  427. DataList: dataList,
  428. }
  429. // 去除返回指标中的数据信息,避免没必要的数据传输
  430. for k, _ := range edbList {
  431. edbList[k].DataList = nil
  432. }
  433. return
  434. }
  435. // CheckIsEnChart
  436. // @Description: 检测是否展示英文名称
  437. // @author: Roc
  438. // @datetime 2023-11-28 20:54:41
  439. // @param chartNameEn string
  440. // @param dataResp ChartInfoResp
  441. // @return bool
  442. func CheckIsEnChart(chartNameEn string, dataResp ChartInfoResp) bool {
  443. if chartNameEn == "" {
  444. return false
  445. }
  446. if dataResp.XNameEn == `` {
  447. return false
  448. }
  449. if dataResp.YNameEn == `` {
  450. return false
  451. }
  452. for _, v := range dataResp.DataList {
  453. if v.NameEn == `` {
  454. return false
  455. }
  456. }
  457. return true
  458. }
  459. // GetXYEdbIdList
  460. // @Description: 根据标签id和品种获取指标列表信息
  461. // @author: Roc
  462. // @datetime 2023-11-27 14:31:23
  463. // @param tagX int
  464. // @param tagY int
  465. // @param varietyList []int
  466. // @return xVarietyEdbMap map[int]int
  467. // @return yVarietyEdbMap map[int]int
  468. // @return edbInfoIdList []int
  469. // @return errMsg string
  470. // @return err error
  471. func GetXYEdbIdList(tagX, tagY int, varietyList []int) (xVarietyEdbMap, yVarietyEdbMap map[int]int, edbInfoIdList []int, err error) {
  472. edbInfoIdList = make([]int, 0)
  473. xVarietyEdbMap = make(map[int]int)
  474. yVarietyEdbMap = make(map[int]int)
  475. xList, err := cross_varietyModel.GetChartTagVarietyListByTagAndVariety(tagX, varietyList)
  476. if err != nil {
  477. err = errors.New("获取X轴的品种指标配置信息失败,Err:" + err.Error())
  478. return
  479. }
  480. yList, err := cross_varietyModel.GetChartTagVarietyListByTagAndVariety(tagY, varietyList)
  481. if err != nil {
  482. err = errors.New("获取Y轴的品种指标配置信息失败,Err:" + err.Error())
  483. return
  484. }
  485. baseVarietyIdMap := make(map[int]int)
  486. for _, v := range xList {
  487. baseVarietyIdMap[v.ChartVarietyId] = v.ChartVarietyId
  488. }
  489. // 两个标签里面的品种并集
  490. needVarietyIdMap := make(map[int]int)
  491. for _, v := range yList {
  492. if val, ok := baseVarietyIdMap[v.ChartVarietyId]; ok {
  493. // 如果在 map2 中存在相同的键,则将键和值添加到结果中
  494. needVarietyIdMap[v.ChartVarietyId] = val
  495. }
  496. }
  497. for _, v := range xList {
  498. if _, ok := needVarietyIdMap[v.ChartVarietyId]; ok {
  499. xVarietyEdbMap[v.ChartVarietyId] = v.EdbInfoId
  500. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  501. }
  502. }
  503. for _, v := range yList {
  504. if _, ok := needVarietyIdMap[v.ChartVarietyId]; ok {
  505. yVarietyEdbMap[v.ChartVarietyId] = v.EdbInfoId
  506. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  507. }
  508. }
  509. return
  510. }
  511. // AddChartInfo
  512. // @Description: AddChartInfo
  513. // @author: Roc
  514. // @datetime 2023-11-24 15:58:14
  515. // @param req request.AddChartReq
  516. // @param sysUser *system.Admin
  517. // @return chartInfo *data_manage.ChartInfo
  518. // @return lang string
  519. // @return err error
  520. // @return errMsg string
  521. // @return isSendEmail bool
  522. func AddChartInfo(req request.AddChartReq, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  523. isSendEmail = true
  524. source := utils.CHART_SOURCE_CROSS_HEDGING
  525. req.ChartName = strings.Trim(req.ChartName, " ")
  526. if req.ChartName == "" {
  527. errMsg = "请填写图表名称!"
  528. err = errors.New(errMsg)
  529. isSendEmail = false
  530. return
  531. }
  532. if req.TagX <= 0 {
  533. errMsg = "请选择X轴坐标的标签!"
  534. err = errors.New(errMsg)
  535. isSendEmail = false
  536. return
  537. }
  538. if req.TagY <= 0 {
  539. errMsg = "请选择Y轴坐标的标签!"
  540. err = errors.New(errMsg)
  541. isSendEmail = false
  542. return
  543. }
  544. if req.CalculateValue <= 0 {
  545. errMsg = "请设置时间长度!"
  546. err = errors.New(errMsg)
  547. isSendEmail = false
  548. return
  549. }
  550. if req.CalculateUnit == `` {
  551. errMsg = "请设置时间频度!"
  552. err = errors.New(errMsg)
  553. isSendEmail = false
  554. return
  555. }
  556. if req.PercentType != utils.PercentCalculateTypeRange && req.PercentType != utils.PercentCalculateTypeNum {
  557. errMsg = "请选择百分位"
  558. err = errors.New(errMsg)
  559. isSendEmail = false
  560. return
  561. }
  562. // 品种配置
  563. if len(req.VarietyList) < 0 {
  564. errMsg = "请选择品种!"
  565. err = errors.New(errMsg)
  566. isSendEmail = false
  567. return
  568. }
  569. // 日期配置
  570. dateConfigList := len(req.DateConfigList)
  571. if dateConfigList < 0 {
  572. errMsg = "请选择日期!"
  573. err = errors.New(errMsg)
  574. isSendEmail = false
  575. return
  576. }
  577. if dateConfigList > 5 {
  578. errMsg = "日期数量已达上限!"
  579. err = errors.New(errMsg)
  580. isSendEmail = false
  581. return
  582. }
  583. // 基础配置转string
  584. extraConfigByte, err := json.Marshal(req)
  585. if err != nil {
  586. return
  587. }
  588. chartClassify, err := data_manage.GetCrossVarietyChartClassifyBySysUserId(sysUser.AdminId)
  589. if err != nil {
  590. if err.Error() != utils.ErrNoRow() {
  591. errMsg = "获取分类信息失败"
  592. err = errors.New("获取分类信息失败,Err:" + err.Error())
  593. return
  594. }
  595. // 分类没有,需要创建新的分类,那么err置空
  596. err = nil
  597. _, err, errMsg, isSendEmail = data.AddChartClassify(sysUser.RealName, 0, 1, source, lang, sysUser)
  598. if err != nil {
  599. return
  600. }
  601. }
  602. // 数据校验(品种、标签、指标)
  603. {
  604. // 标签m
  605. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  606. {
  607. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{req.TagX, req.TagY})
  608. if tmpErr != nil {
  609. err = tmpErr
  610. return
  611. }
  612. for _, v := range tagList {
  613. if v.ChartTagId == req.TagX {
  614. xTagInfo = v
  615. } else if v.ChartTagId == req.TagY {
  616. yTagInfo = v
  617. }
  618. }
  619. }
  620. if xTagInfo == nil {
  621. errMsg = "找不到对应的X轴标签"
  622. err = errors.New(errMsg)
  623. return
  624. }
  625. if yTagInfo == nil {
  626. errMsg = "找不到对应的Y轴标签"
  627. err = errors.New(errMsg)
  628. return
  629. }
  630. _, _, edbInfoIdList, tmpErr := GetXYEdbIdList(req.TagX, req.TagY, req.VarietyList)
  631. if tmpErr != nil {
  632. err = errors.New("获取标签配置的品种失败,ERR:" + tmpErr.Error())
  633. return
  634. }
  635. if len(edbInfoIdList) <= 0 {
  636. errMsg = "品种未配置指标"
  637. err = errors.New(errMsg)
  638. isSendEmail = false
  639. return
  640. }
  641. mappingList, tmpErr := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  642. if tmpErr != nil {
  643. errMsg = "获取指标信息失败"
  644. err = errors.New("获取指标信息失败,ERR:" + tmpErr.Error())
  645. return
  646. }
  647. if len(mappingList) <= 0 {
  648. errMsg = "关联指标为空"
  649. err = errors.New(errMsg)
  650. isSendEmail = false
  651. return
  652. }
  653. }
  654. var chartInfoId int
  655. // 判断图表是否存在
  656. //var condition string
  657. //var pars []interface{}
  658. //condition += " AND chart_name=? AND source = ? "
  659. //pars = append(pars, req.ChartName, source)
  660. //count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  661. //if err != nil {
  662. // errMsg = "判断图表名称是否存在失败"
  663. // err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  664. // return
  665. //}
  666. //if count > 0 {
  667. // errMsg = "图表已存在,请重新填写"
  668. // err = errors.New(errMsg)
  669. // isSendEmail = false
  670. // return
  671. //}
  672. chartInfo = new(data_manage.ChartInfo)
  673. chartInfo.ChartName = req.ChartName
  674. chartInfo.ChartNameEn = req.ChartNameEn
  675. //chartInfo.EdbInfoIds = edbInfoIdStr
  676. //chartInfo.ChartClassifyId = req.ChartClassifyId
  677. chartInfo.SysUserId = sysUser.AdminId
  678. chartInfo.SysUserRealName = sysUser.RealName
  679. chartInfo.ChartImage = req.ChartImage
  680. chartInfo.CreateTime = time.Now()
  681. chartInfo.ModifyTime = time.Now()
  682. chartInfo.IsSetName = 0
  683. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  684. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  685. chartInfo.ChartType = 9 // 相关性图
  686. chartInfo.Calendar = "公历"
  687. chartInfo.DateType = 6
  688. //chartInfo.StartDate = req.StartDate
  689. //chartInfo.EndDate = req.EndDate
  690. //chartInfo.SeasonStartDate = req.StartDate
  691. //chartInfo.SeasonEndDate = req.EndDate
  692. chartInfo.LeftMin = req.LeftMin
  693. chartInfo.LeftMax = req.LeftMax
  694. chartInfo.XMin = req.XMin
  695. chartInfo.XMax = req.XMax
  696. //chartInfo.RightMin = req.RightMin
  697. //chartInfo.RightMax = req.RightMax
  698. //chartInfo.Disabled = disableVal
  699. chartInfo.Source = source
  700. chartInfo.ExtraConfig = string(extraConfigByte)
  701. chartInfo.ChartImage = req.ChartImage
  702. chartInfo.ChartThemeId = req.ChartThemeId
  703. chartInfo.SourcesFrom = req.SourcesFrom
  704. chartInfo.Instructions = req.Instructions
  705. chartInfo.MarkersLines = req.MarkersLines
  706. chartInfo.MarkersAreas = req.MarkersAreas
  707. // 图表品种
  708. chartVarietyMappingList := make([]*cross_varietyModel.ChartVarietyMapping, 0)
  709. for _, varietyId := range req.VarietyList {
  710. chartVarietyMappingList = append(chartVarietyMappingList, &cross_varietyModel.ChartVarietyMapping{
  711. Id: 0,
  712. ChartInfoId: 0,
  713. ChartVarietyId: varietyId,
  714. ModifyTime: time.Now(),
  715. CreateTime: time.Now(),
  716. })
  717. }
  718. // 图表配置
  719. chartInfoCrossVariety := &cross_varietyModel.ChartInfoCrossVariety{
  720. Id: 0,
  721. ChartInfoId: 0,
  722. ChartXTagId: req.TagX,
  723. ChartYTagId: req.TagY,
  724. CalculateValue: req.CalculateValue,
  725. CalculateUnit: req.CalculateUnit,
  726. ModifyTime: time.Now(),
  727. CreateTime: time.Now(),
  728. }
  729. // 新增图表和指标mapping
  730. chartInfoId, e := cross_varietyModel.CreateChart(chartInfo, chartClassify, chartVarietyMappingList, chartInfoCrossVariety)
  731. if e != nil {
  732. errMsg = "操作失败"
  733. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  734. return
  735. }
  736. //添加es数据
  737. go data.EsAddOrEditChartInfo(chartInfoId)
  738. return
  739. }
  740. // EditChartInfo
  741. // @Description: 编辑图表
  742. // @author: Roc
  743. // @datetime 2023-11-24 15:58:31
  744. // @param req request.EditChartReq
  745. // @param sysUser *system.Admin
  746. // @return chartItem *data_manage.ChartInfo
  747. // @return err error
  748. // @return errMsg string
  749. // @return isSendEmail bool
  750. func EditChartInfo(req request.EditChartReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  751. isSendEmail = true
  752. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  753. if err != nil {
  754. if err.Error() == utils.ErrNoRow() {
  755. errMsg = "图表已被删除,请刷新页面"
  756. err = errors.New(errMsg)
  757. isSendEmail = false
  758. return
  759. }
  760. errMsg = "获取图表信息失败"
  761. err = errors.New("获取图表信息失败,Err:" + err.Error())
  762. return
  763. }
  764. if chartItem.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  765. errMsg = "该图不是跨品种分析图表!"
  766. err = errors.New(errMsg)
  767. isSendEmail = false
  768. return
  769. }
  770. req.ChartName = strings.Trim(req.ChartName, " ")
  771. if req.ChartName == "" {
  772. errMsg = "请填写图表名称!"
  773. err = errors.New(errMsg)
  774. isSendEmail = false
  775. return
  776. }
  777. if req.TagX <= 0 {
  778. errMsg = "请选择X轴坐标的标签!"
  779. err = errors.New(errMsg)
  780. isSendEmail = false
  781. return
  782. }
  783. if req.TagY <= 0 {
  784. errMsg = "请选择Y轴坐标的标签!"
  785. err = errors.New(errMsg)
  786. isSendEmail = false
  787. return
  788. }
  789. if req.CalculateValue <= 0 {
  790. errMsg = "请设置时间长度!"
  791. err = errors.New(errMsg)
  792. isSendEmail = false
  793. return
  794. }
  795. if req.CalculateUnit == `` {
  796. errMsg = "请设置时间频度!"
  797. err = errors.New(errMsg)
  798. isSendEmail = false
  799. return
  800. }
  801. if req.PercentType != utils.PercentCalculateTypeRange && req.PercentType != utils.PercentCalculateTypeNum {
  802. errMsg = "请选择百分位"
  803. err = errors.New(errMsg)
  804. isSendEmail = false
  805. return
  806. }
  807. // 品种配置
  808. if len(req.VarietyList) < 0 {
  809. errMsg = "请选择品种!"
  810. err = errors.New(errMsg)
  811. isSendEmail = false
  812. return
  813. }
  814. // 日期配置
  815. dateConfigList := len(req.DateConfigList)
  816. if dateConfigList < 0 {
  817. errMsg = "请选择日期!"
  818. err = errors.New(errMsg)
  819. isSendEmail = false
  820. return
  821. }
  822. if dateConfigList > 5 {
  823. errMsg = "日期数量已达上限!"
  824. err = errors.New(errMsg)
  825. isSendEmail = false
  826. return
  827. }
  828. // 基础配置转string
  829. extraConfigByte, err := json.Marshal(req)
  830. if err != nil {
  831. return
  832. }
  833. // 图表操作权限
  834. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  835. if !ok {
  836. errMsg = "没有该图表的操作权限"
  837. err = errors.New(errMsg)
  838. isSendEmail = false
  839. return
  840. }
  841. // 数据校验(品种、标签、指标)
  842. {
  843. // 标签m
  844. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  845. {
  846. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{req.TagX, req.TagY})
  847. if tmpErr != nil {
  848. err = tmpErr
  849. return
  850. }
  851. for _, v := range tagList {
  852. if v.ChartTagId == req.TagX {
  853. xTagInfo = v
  854. } else if v.ChartTagId == req.TagY {
  855. yTagInfo = v
  856. }
  857. }
  858. }
  859. if xTagInfo == nil {
  860. errMsg = "找不到对应的X轴标签"
  861. err = errors.New(errMsg)
  862. return
  863. }
  864. if yTagInfo == nil {
  865. errMsg = "找不到对应的Y轴标签"
  866. err = errors.New(errMsg)
  867. return
  868. }
  869. _, _, edbInfoIdList, tmpErr := GetXYEdbIdList(req.TagX, req.TagY, req.VarietyList)
  870. if tmpErr != nil {
  871. err = errors.New("获取标签配置的品种失败,ERR:" + tmpErr.Error())
  872. return
  873. }
  874. if len(edbInfoIdList) <= 0 {
  875. errMsg = "品种未配置指标"
  876. err = errors.New(errMsg)
  877. isSendEmail = false
  878. return
  879. }
  880. mappingList, tmpErr := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  881. if tmpErr != nil {
  882. errMsg = "获取指标信息失败"
  883. err = errors.New("获取指标信息失败,ERR:" + tmpErr.Error())
  884. return
  885. }
  886. if len(mappingList) <= 0 {
  887. errMsg = "关联指标为空"
  888. err = errors.New(errMsg)
  889. isSendEmail = false
  890. return
  891. }
  892. }
  893. //判断图表是否存在
  894. //var condition string
  895. //var pars []interface{}
  896. //condition += " AND chart_info_id <> ? "
  897. //pars = append(pars, req.ChartInfoId)
  898. //condition += " AND chart_name=? AND source = ? "
  899. //pars = append(pars, req.ChartName, chartItem.Source)
  900. //count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  901. //if err != nil {
  902. // errMsg = "判断图表名称是否存在失败"
  903. // err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  904. // return
  905. //}
  906. //if count > 0 {
  907. // errMsg = "图表已存在,请重新填写"
  908. // err = errors.New(errMsg)
  909. // isSendEmail = false
  910. // return
  911. //}
  912. switch lang {
  913. case utils.EnLangVersion:
  914. chartItem.ChartNameEn = req.ChartName
  915. default:
  916. chartItem.ChartName = req.ChartName
  917. }
  918. chartItem.ExtraConfig = string(extraConfigByte)
  919. chartItem.ModifyTime = time.Now()
  920. chartItem.LeftMin = req.LeftMin
  921. chartItem.LeftMax = req.LeftMax
  922. chartItem.XMin = req.XMin
  923. chartItem.XMax = req.XMax
  924. chartUpdateCols := []string{"ChartName", "ChartNameEn", "ExtraConfig", "ModifyTime", "LeftMin", "LeftMax", "XMin", "XMax"}
  925. // 跨品种分析配置
  926. chartInfoCrossVariety, err := cross_varietyModel.GetChartInfoCrossVarietyByChartInfoId(chartItem.ChartInfoId)
  927. if err != nil {
  928. return
  929. }
  930. chartInfoCrossVariety.ChartXTagId = req.TagX
  931. chartInfoCrossVariety.ChartYTagId = req.TagY
  932. chartInfoCrossVariety.CalculateValue = req.CalculateValue
  933. chartInfoCrossVariety.CalculateUnit = req.CalculateUnit
  934. chartInfoCrossVariety.ModifyTime = time.Now()
  935. chartInfoCrossVarietyUpdateCols := []string{"ChartXTagId", "ChartYTagId", "CalculateValue", "CalculateUnit", "ModifyTime"}
  936. // 图表品种
  937. chartVarietyMappingList := make([]*cross_varietyModel.ChartVarietyMapping, 0)
  938. for _, varietyId := range req.VarietyList {
  939. chartVarietyMappingList = append(chartVarietyMappingList, &cross_varietyModel.ChartVarietyMapping{
  940. Id: 0,
  941. ChartInfoId: 0,
  942. ChartVarietyId: varietyId,
  943. ModifyTime: time.Now(),
  944. CreateTime: time.Now(),
  945. })
  946. }
  947. err = cross_varietyModel.EditChart(chartItem, chartVarietyMappingList, chartInfoCrossVariety,
  948. chartUpdateCols, chartInfoCrossVarietyUpdateCols)
  949. if err != nil {
  950. errMsg = "保存失败"
  951. err = errors.New("保存失败,Err:" + err.Error())
  952. return
  953. }
  954. resp := new(data_manage.AddChartInfoResp)
  955. resp.ChartInfoId = chartItem.ChartInfoId
  956. resp.UniqueCode = chartItem.UniqueCode
  957. //resp.ChartType = req.ChartType
  958. //添加es数据
  959. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  960. //修改my eta es数据
  961. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  962. return
  963. }
  964. // ModifyChartEdbMapping
  965. // @Description: 修改图表的关系表
  966. // @author: Roc
  967. // @datetime 2023-12-11 17:32:31
  968. // @param tagId int
  969. func ModifyChartEdbMapping(tagId int) {
  970. errMsgList := make([]string, 0)
  971. var err error
  972. defer func() {
  973. if err != nil {
  974. errMsgList = append(errMsgList, err.Error())
  975. }
  976. if len(errMsgList) > 0 {
  977. utils.FileLog.Error("修改跨品种分析的图表关联指标失败:\n" + strings.Join(errMsgList, "\n"))
  978. }
  979. }()
  980. // 找出标签关联的图表
  981. list, err := cross_varietyModel.GeChartInfoCrossVarietyListByTagId(tagId)
  982. if err != nil {
  983. return
  984. }
  985. chartInfoIdList := make([]int, 0)
  986. tagIdList := make([]int, 0)
  987. tagIdMap := make(map[int]int, 0)
  988. for _, v := range list {
  989. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  990. if _, ok := tagIdMap[v.ChartXTagId]; !ok {
  991. tagIdList = append(tagIdList, v.ChartXTagId)
  992. tagIdMap[v.ChartXTagId] = 1
  993. }
  994. if _, ok := tagIdMap[v.ChartYTagId]; !ok {
  995. tagIdList = append(tagIdList, v.ChartYTagId)
  996. tagIdMap[v.ChartYTagId] = 1
  997. }
  998. }
  999. // 找出所有标签关联的品种和对应的指标
  1000. chartTagVarietyList, err := cross_varietyModel.GetChartTagVarietyListByTagIdList(tagIdList)
  1001. if err != nil {
  1002. return
  1003. }
  1004. // 所有指标id
  1005. allEdbInfoIdList := make([]int, 0)
  1006. // 标签关联品种的指标列表
  1007. tagVarietyEdbMap := make(map[string][]int)
  1008. for _, v := range chartTagVarietyList {
  1009. if v.EdbInfoId <= 0 {
  1010. continue
  1011. }
  1012. key := fmt.Sprint(v.ChartTagId, "_", v.ChartVarietyId)
  1013. tagVarietyEdbList, ok := tagVarietyEdbMap[key]
  1014. if !ok {
  1015. tagVarietyEdbList = make([]int, 0)
  1016. }
  1017. tagVarietyEdbMap[key] = append(tagVarietyEdbList, v.EdbInfoId)
  1018. allEdbInfoIdList = append(allEdbInfoIdList, v.EdbInfoId)
  1019. }
  1020. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1021. // 获取指标列表
  1022. if len(allEdbInfoIdList) > 0 {
  1023. allEdbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(allEdbInfoIdList)
  1024. if tmpErr != nil {
  1025. err = tmpErr
  1026. return
  1027. }
  1028. for _, v := range allEdbInfoList {
  1029. edbInfoMap[v.EdbInfoId] = v
  1030. }
  1031. }
  1032. // 图表关联的品种列表
  1033. chartVarietyMappingList, err := cross_varietyModel.GetChartVarietyMappingListByChartInfoIdList(chartInfoIdList)
  1034. if err != nil {
  1035. return
  1036. }
  1037. // 图表关联的品种map
  1038. chartVarietyMappingListMap := make(map[int][]int)
  1039. for _, v := range chartVarietyMappingList {
  1040. if v.ChartInfoId <= 0 {
  1041. continue
  1042. }
  1043. varietyMappingList, ok := chartVarietyMappingListMap[v.ChartInfoId]
  1044. if !ok {
  1045. varietyMappingList = make([]int, 0)
  1046. }
  1047. chartVarietyMappingListMap[v.ChartInfoId] = append(varietyMappingList, v.ChartVarietyId)
  1048. }
  1049. // 处理图表
  1050. for _, v := range list {
  1051. // 获取关联的指标id
  1052. edbInfoList := make([]*data_manage.EdbInfo, 0)
  1053. edbInfoIdMap := make(map[int]int, 0)
  1054. // 找出图表关联的品种
  1055. varietyIdList, ok := chartVarietyMappingListMap[v.ChartInfoId]
  1056. if ok {
  1057. // 获取品种与标签的id
  1058. for _, varietyId := range varietyIdList {
  1059. // 先处理x轴
  1060. key := fmt.Sprint(v.ChartXTagId, "_", varietyId)
  1061. if tmpEdbInfoIdList, ok2 := tagVarietyEdbMap[key]; ok2 {
  1062. for _, edbInfoId := range tmpEdbInfoIdList {
  1063. if _, ok3 := edbInfoIdMap[edbInfoId]; !ok3 {
  1064. if edbInfo, ok4 := edbInfoMap[edbInfoId]; ok4 {
  1065. edbInfoList = append(edbInfoList, edbInfo)
  1066. }
  1067. edbInfoIdMap[edbInfoId] = edbInfoId
  1068. }
  1069. }
  1070. }
  1071. // 处理y轴
  1072. key = fmt.Sprint(v.ChartYTagId, "_", varietyId)
  1073. if tmpEdbInfoIdList, ok2 := tagVarietyEdbMap[key]; ok2 {
  1074. for _, edbInfoId := range tmpEdbInfoIdList {
  1075. if _, ok3 := edbInfoIdMap[edbInfoId]; !ok3 {
  1076. if edbInfo, ok4 := edbInfoMap[edbInfoId]; ok4 {
  1077. edbInfoList = append(edbInfoList, edbInfo)
  1078. }
  1079. edbInfoIdMap[edbInfoId] = edbInfoId
  1080. }
  1081. }
  1082. }
  1083. }
  1084. }
  1085. tmpErr := data_manage.ModifyChartEdbMapping(v.ChartInfoId, edbInfoList)
  1086. if tmpErr != nil {
  1087. errMsgList = append(errMsgList, fmt.Sprint("修改", v.ChartInfoId, "失败,err:", tmpErr))
  1088. }
  1089. }
  1090. return
  1091. }