chart.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. package cross_variety
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. cross_varietyModel "eta/eta_api/models/data_manage/cross_variety"
  7. "eta/eta_api/models/data_manage/cross_variety/request"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/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. edbInfoIdArr := make([]int, 0)
  603. // 数据校验(品种、标签、指标)
  604. {
  605. // 标签m
  606. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  607. {
  608. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{req.TagX, req.TagY})
  609. if tmpErr != nil {
  610. err = tmpErr
  611. return
  612. }
  613. for _, v := range tagList {
  614. if v.ChartTagId == req.TagX {
  615. xTagInfo = v
  616. } else if v.ChartTagId == req.TagY {
  617. yTagInfo = v
  618. }
  619. }
  620. }
  621. if xTagInfo == nil {
  622. errMsg = "找不到对应的X轴标签"
  623. err = errors.New(errMsg)
  624. return
  625. }
  626. if yTagInfo == nil {
  627. errMsg = "找不到对应的Y轴标签"
  628. err = errors.New(errMsg)
  629. return
  630. }
  631. _, _, edbInfoIdList, tmpErr := GetXYEdbIdList(req.TagX, req.TagY, req.VarietyList)
  632. if tmpErr != nil {
  633. err = errors.New("获取标签配置的品种失败,ERR:" + tmpErr.Error())
  634. return
  635. }
  636. if len(edbInfoIdList) <= 0 {
  637. errMsg = "品种未配置指标"
  638. err = errors.New(errMsg)
  639. isSendEmail = false
  640. return
  641. }
  642. edbInfoIdArr = edbInfoIdList
  643. mappingList, tmpErr := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  644. if tmpErr != nil {
  645. errMsg = "获取指标信息失败"
  646. err = errors.New("获取指标信息失败,ERR:" + tmpErr.Error())
  647. return
  648. }
  649. if len(mappingList) <= 0 {
  650. errMsg = "关联指标为空"
  651. err = errors.New(errMsg)
  652. isSendEmail = false
  653. return
  654. }
  655. }
  656. var chartInfoId int
  657. // 判断图表是否存在
  658. //var condition string
  659. //var pars []interface{}
  660. //condition += " AND chart_name=? AND source = ? "
  661. //pars = append(pars, req.ChartName, source)
  662. //count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  663. //if err != nil {
  664. // errMsg = "判断图表名称是否存在失败"
  665. // err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  666. // return
  667. //}
  668. //if count > 0 {
  669. // errMsg = "图表已存在,请重新填写"
  670. // err = errors.New(errMsg)
  671. // isSendEmail = false
  672. // return
  673. //}
  674. chartInfo = new(data_manage.ChartInfo)
  675. chartInfo.ChartName = req.ChartName
  676. chartInfo.ChartNameEn = req.ChartNameEn
  677. //chartInfo.EdbInfoIds = edbInfoIdStr
  678. //chartInfo.ChartClassifyId = req.ChartClassifyId
  679. chartInfo.SysUserId = sysUser.AdminId
  680. chartInfo.SysUserRealName = sysUser.RealName
  681. chartInfo.ChartImage = req.ChartImage
  682. chartInfo.CreateTime = time.Now()
  683. chartInfo.ModifyTime = time.Now()
  684. chartInfo.IsSetName = 0
  685. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  686. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  687. chartInfo.ChartType = 9 // 相关性图
  688. chartInfo.Calendar = "公历"
  689. chartInfo.DateType = 6
  690. //chartInfo.StartDate = req.StartDate
  691. //chartInfo.EndDate = req.EndDate
  692. //chartInfo.SeasonStartDate = req.StartDate
  693. //chartInfo.SeasonEndDate = req.EndDate
  694. chartInfo.LeftMin = req.LeftMin
  695. chartInfo.LeftMax = req.LeftMax
  696. chartInfo.XMin = req.XMin
  697. chartInfo.XMax = req.XMax
  698. //chartInfo.RightMin = req.RightMin
  699. //chartInfo.RightMax = req.RightMax
  700. //chartInfo.Disabled = disableVal
  701. chartInfo.Source = source
  702. chartInfo.ExtraConfig = string(extraConfigByte)
  703. chartInfo.ChartImage = req.ChartImage
  704. chartInfo.ChartThemeId = req.ChartThemeId
  705. chartInfo.SourcesFrom = req.SourcesFrom
  706. chartInfo.Instructions = req.Instructions
  707. chartInfo.MarkersLines = req.MarkersLines
  708. chartInfo.MarkersAreas = req.MarkersAreas
  709. // 图表品种
  710. chartVarietyMappingList := make([]*cross_varietyModel.ChartVarietyMapping, 0)
  711. for _, varietyId := range req.VarietyList {
  712. chartVarietyMappingList = append(chartVarietyMappingList, &cross_varietyModel.ChartVarietyMapping{
  713. Id: 0,
  714. ChartInfoId: 0,
  715. ChartVarietyId: varietyId,
  716. ModifyTime: time.Now(),
  717. CreateTime: time.Now(),
  718. })
  719. }
  720. // 图表配置
  721. chartInfoCrossVariety := &cross_varietyModel.ChartInfoCrossVariety{
  722. Id: 0,
  723. ChartInfoId: 0,
  724. ChartXTagId: req.TagX,
  725. ChartYTagId: req.TagY,
  726. CalculateValue: req.CalculateValue,
  727. CalculateUnit: req.CalculateUnit,
  728. ModifyTime: time.Now(),
  729. CreateTime: time.Now(),
  730. }
  731. // 新增图表和指标mapping
  732. chartInfoId, e := cross_varietyModel.CreateChart(chartInfo, chartClassify, chartVarietyMappingList, chartInfoCrossVariety)
  733. if e != nil {
  734. errMsg = "操作失败"
  735. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  736. return
  737. }
  738. chartInfo.ChartInfoId = chartInfoId
  739. // 添加指标引用记录
  740. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
  741. //添加es数据
  742. go data.EsAddOrEditChartInfo(chartInfoId)
  743. return
  744. }
  745. // EditChartInfo
  746. // @Description: 编辑图表
  747. // @author: Roc
  748. // @datetime 2023-11-24 15:58:31
  749. // @param req request.EditChartReq
  750. // @param sysUser *system.Admin
  751. // @return chartItem *data_manage.ChartInfo
  752. // @return err error
  753. // @return errMsg string
  754. // @return isSendEmail bool
  755. func EditChartInfo(req request.EditChartReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  756. isSendEmail = true
  757. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  758. if err != nil {
  759. if err.Error() == utils.ErrNoRow() {
  760. errMsg = "图表已被删除,请刷新页面"
  761. err = errors.New(errMsg)
  762. isSendEmail = false
  763. return
  764. }
  765. errMsg = "获取图表信息失败"
  766. err = errors.New("获取图表信息失败,Err:" + err.Error())
  767. return
  768. }
  769. if chartItem.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  770. errMsg = "该图不是跨品种分析图表!"
  771. err = errors.New(errMsg)
  772. isSendEmail = false
  773. return
  774. }
  775. req.ChartName = strings.Trim(req.ChartName, " ")
  776. if req.ChartName == "" {
  777. errMsg = "请填写图表名称!"
  778. err = errors.New(errMsg)
  779. isSendEmail = false
  780. return
  781. }
  782. if req.TagX <= 0 {
  783. errMsg = "请选择X轴坐标的标签!"
  784. err = errors.New(errMsg)
  785. isSendEmail = false
  786. return
  787. }
  788. if req.TagY <= 0 {
  789. errMsg = "请选择Y轴坐标的标签!"
  790. err = errors.New(errMsg)
  791. isSendEmail = false
  792. return
  793. }
  794. if req.CalculateValue <= 0 {
  795. errMsg = "请设置时间长度!"
  796. err = errors.New(errMsg)
  797. isSendEmail = false
  798. return
  799. }
  800. if req.CalculateUnit == `` {
  801. errMsg = "请设置时间频度!"
  802. err = errors.New(errMsg)
  803. isSendEmail = false
  804. return
  805. }
  806. if req.PercentType != utils.PercentCalculateTypeRange && req.PercentType != utils.PercentCalculateTypeNum {
  807. errMsg = "请选择百分位"
  808. err = errors.New(errMsg)
  809. isSendEmail = false
  810. return
  811. }
  812. // 品种配置
  813. if len(req.VarietyList) < 0 {
  814. errMsg = "请选择品种!"
  815. err = errors.New(errMsg)
  816. isSendEmail = false
  817. return
  818. }
  819. // 日期配置
  820. dateConfigList := len(req.DateConfigList)
  821. if dateConfigList < 0 {
  822. errMsg = "请选择日期!"
  823. err = errors.New(errMsg)
  824. isSendEmail = false
  825. return
  826. }
  827. if dateConfigList > 5 {
  828. errMsg = "日期数量已达上限!"
  829. err = errors.New(errMsg)
  830. isSendEmail = false
  831. return
  832. }
  833. // 基础配置转string
  834. extraConfigByte, err := json.Marshal(req)
  835. if err != nil {
  836. return
  837. }
  838. // 图表操作权限
  839. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  840. if !ok {
  841. errMsg = "没有该图表的操作权限"
  842. err = errors.New(errMsg)
  843. isSendEmail = false
  844. return
  845. }
  846. edbInfoIdArr := make([]int, 0)
  847. // 数据校验(品种、标签、指标)
  848. {
  849. // 标签m
  850. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  851. {
  852. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{req.TagX, req.TagY})
  853. if tmpErr != nil {
  854. err = tmpErr
  855. return
  856. }
  857. for _, v := range tagList {
  858. if v.ChartTagId == req.TagX {
  859. xTagInfo = v
  860. } else if v.ChartTagId == req.TagY {
  861. yTagInfo = v
  862. }
  863. }
  864. }
  865. if xTagInfo == nil {
  866. errMsg = "找不到对应的X轴标签"
  867. err = errors.New(errMsg)
  868. return
  869. }
  870. if yTagInfo == nil {
  871. errMsg = "找不到对应的Y轴标签"
  872. err = errors.New(errMsg)
  873. return
  874. }
  875. _, _, edbInfoIdList, tmpErr := GetXYEdbIdList(req.TagX, req.TagY, req.VarietyList)
  876. if tmpErr != nil {
  877. err = errors.New("获取标签配置的品种失败,ERR:" + tmpErr.Error())
  878. return
  879. }
  880. if len(edbInfoIdList) <= 0 {
  881. errMsg = "品种未配置指标"
  882. err = errors.New(errMsg)
  883. isSendEmail = false
  884. return
  885. }
  886. edbInfoIdArr = edbInfoIdList
  887. mappingList, tmpErr := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  888. if tmpErr != nil {
  889. errMsg = "获取指标信息失败"
  890. err = errors.New("获取指标信息失败,ERR:" + tmpErr.Error())
  891. return
  892. }
  893. if len(mappingList) <= 0 {
  894. errMsg = "关联指标为空"
  895. err = errors.New(errMsg)
  896. isSendEmail = false
  897. return
  898. }
  899. }
  900. //判断图表是否存在
  901. //var condition string
  902. //var pars []interface{}
  903. //condition += " AND chart_info_id <> ? "
  904. //pars = append(pars, req.ChartInfoId)
  905. //condition += " AND chart_name=? AND source = ? "
  906. //pars = append(pars, req.ChartName, chartItem.Source)
  907. //count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  908. //if err != nil {
  909. // errMsg = "判断图表名称是否存在失败"
  910. // err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  911. // return
  912. //}
  913. //if count > 0 {
  914. // errMsg = "图表已存在,请重新填写"
  915. // err = errors.New(errMsg)
  916. // isSendEmail = false
  917. // return
  918. //}
  919. switch lang {
  920. case utils.EnLangVersion:
  921. chartItem.ChartNameEn = req.ChartName
  922. default:
  923. chartItem.ChartName = req.ChartName
  924. }
  925. chartItem.ExtraConfig = string(extraConfigByte)
  926. chartItem.ModifyTime = time.Now()
  927. chartItem.LeftMin = req.LeftMin
  928. chartItem.LeftMax = req.LeftMax
  929. chartItem.XMin = req.XMin
  930. chartItem.XMax = req.XMax
  931. chartUpdateCols := []string{"ChartName", "ChartNameEn", "ExtraConfig", "ModifyTime", "LeftMin", "LeftMax", "XMin", "XMax"}
  932. // 跨品种分析配置
  933. chartInfoCrossVariety, err := cross_varietyModel.GetChartInfoCrossVarietyByChartInfoId(chartItem.ChartInfoId)
  934. if err != nil {
  935. return
  936. }
  937. chartInfoCrossVariety.ChartXTagId = req.TagX
  938. chartInfoCrossVariety.ChartYTagId = req.TagY
  939. chartInfoCrossVariety.CalculateValue = req.CalculateValue
  940. chartInfoCrossVariety.CalculateUnit = req.CalculateUnit
  941. chartInfoCrossVariety.ModifyTime = time.Now()
  942. chartInfoCrossVarietyUpdateCols := []string{"ChartXTagId", "ChartYTagId", "CalculateValue", "CalculateUnit", "ModifyTime"}
  943. // 图表品种
  944. chartVarietyMappingList := make([]*cross_varietyModel.ChartVarietyMapping, 0)
  945. for _, varietyId := range req.VarietyList {
  946. chartVarietyMappingList = append(chartVarietyMappingList, &cross_varietyModel.ChartVarietyMapping{
  947. Id: 0,
  948. ChartInfoId: 0,
  949. ChartVarietyId: varietyId,
  950. ModifyTime: time.Now(),
  951. CreateTime: time.Now(),
  952. })
  953. }
  954. err = cross_varietyModel.EditChart(chartItem, chartVarietyMappingList, chartInfoCrossVariety,
  955. chartUpdateCols, chartInfoCrossVarietyUpdateCols)
  956. if err != nil {
  957. errMsg = "保存失败"
  958. err = errors.New("保存失败,Err:" + err.Error())
  959. return
  960. }
  961. resp := new(data_manage.AddChartInfoResp)
  962. resp.ChartInfoId = chartItem.ChartInfoId
  963. resp.UniqueCode = chartItem.UniqueCode
  964. //resp.ChartType = req.ChartType
  965. // 添加指标引用记录
  966. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
  967. //添加es数据
  968. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  969. //修改my eta es数据
  970. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  971. return
  972. }
  973. // ModifyChartEdbMapping
  974. // @Description: 修改图表的关系表
  975. // @author: Roc
  976. // @datetime 2023-12-11 17:32:31
  977. // @param tagId int
  978. func ModifyChartEdbMapping(tagId int) {
  979. errMsgList := make([]string, 0)
  980. var err error
  981. defer func() {
  982. if err != nil {
  983. errMsgList = append(errMsgList, err.Error())
  984. }
  985. if len(errMsgList) > 0 {
  986. utils.FileLog.Error("修改跨品种分析的图表关联指标失败:\n" + strings.Join(errMsgList, "\n"))
  987. }
  988. }()
  989. // 找出标签关联的图表
  990. list, err := cross_varietyModel.GeChartInfoCrossVarietyListByTagId(tagId)
  991. if err != nil {
  992. return
  993. }
  994. chartInfoIdList := make([]int, 0)
  995. tagIdList := make([]int, 0)
  996. tagIdMap := make(map[int]int, 0)
  997. for _, v := range list {
  998. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  999. if _, ok := tagIdMap[v.ChartXTagId]; !ok {
  1000. tagIdList = append(tagIdList, v.ChartXTagId)
  1001. tagIdMap[v.ChartXTagId] = 1
  1002. }
  1003. if _, ok := tagIdMap[v.ChartYTagId]; !ok {
  1004. tagIdList = append(tagIdList, v.ChartYTagId)
  1005. tagIdMap[v.ChartYTagId] = 1
  1006. }
  1007. }
  1008. // 找出所有标签关联的品种和对应的指标
  1009. chartTagVarietyList, err := cross_varietyModel.GetChartTagVarietyListByTagIdList(tagIdList)
  1010. if err != nil {
  1011. return
  1012. }
  1013. // 所有指标id
  1014. allEdbInfoIdList := make([]int, 0)
  1015. // 标签关联品种的指标列表
  1016. tagVarietyEdbMap := make(map[string][]int)
  1017. for _, v := range chartTagVarietyList {
  1018. if v.EdbInfoId <= 0 {
  1019. continue
  1020. }
  1021. key := fmt.Sprint(v.ChartTagId, "_", v.ChartVarietyId)
  1022. tagVarietyEdbList, ok := tagVarietyEdbMap[key]
  1023. if !ok {
  1024. tagVarietyEdbList = make([]int, 0)
  1025. }
  1026. tagVarietyEdbMap[key] = append(tagVarietyEdbList, v.EdbInfoId)
  1027. allEdbInfoIdList = append(allEdbInfoIdList, v.EdbInfoId)
  1028. }
  1029. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1030. // 获取指标列表
  1031. if len(allEdbInfoIdList) > 0 {
  1032. allEdbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(allEdbInfoIdList)
  1033. if tmpErr != nil {
  1034. err = tmpErr
  1035. return
  1036. }
  1037. for _, v := range allEdbInfoList {
  1038. edbInfoMap[v.EdbInfoId] = v
  1039. }
  1040. }
  1041. // 图表关联的品种列表
  1042. chartVarietyMappingList, err := cross_varietyModel.GetChartVarietyMappingListByChartInfoIdList(chartInfoIdList)
  1043. if err != nil {
  1044. return
  1045. }
  1046. // 图表关联的品种map
  1047. chartVarietyMappingListMap := make(map[int][]int)
  1048. for _, v := range chartVarietyMappingList {
  1049. if v.ChartInfoId <= 0 {
  1050. continue
  1051. }
  1052. varietyMappingList, ok := chartVarietyMappingListMap[v.ChartInfoId]
  1053. if !ok {
  1054. varietyMappingList = make([]int, 0)
  1055. }
  1056. chartVarietyMappingListMap[v.ChartInfoId] = append(varietyMappingList, v.ChartVarietyId)
  1057. }
  1058. // 处理图表
  1059. for _, v := range list {
  1060. // 获取关联的指标id
  1061. edbInfoList := make([]*data_manage.EdbInfo, 0)
  1062. edbInfoIdMap := make(map[int]int, 0)
  1063. // 找出图表关联的品种
  1064. varietyIdList, ok := chartVarietyMappingListMap[v.ChartInfoId]
  1065. if ok {
  1066. // 获取品种与标签的id
  1067. for _, varietyId := range varietyIdList {
  1068. // 先处理x轴
  1069. key := fmt.Sprint(v.ChartXTagId, "_", varietyId)
  1070. if tmpEdbInfoIdList, ok2 := tagVarietyEdbMap[key]; ok2 {
  1071. for _, edbInfoId := range tmpEdbInfoIdList {
  1072. if _, ok3 := edbInfoIdMap[edbInfoId]; !ok3 {
  1073. if edbInfo, ok4 := edbInfoMap[edbInfoId]; ok4 {
  1074. edbInfoList = append(edbInfoList, edbInfo)
  1075. }
  1076. edbInfoIdMap[edbInfoId] = edbInfoId
  1077. }
  1078. }
  1079. }
  1080. // 处理y轴
  1081. key = fmt.Sprint(v.ChartYTagId, "_", varietyId)
  1082. if tmpEdbInfoIdList, ok2 := tagVarietyEdbMap[key]; ok2 {
  1083. for _, edbInfoId := range tmpEdbInfoIdList {
  1084. if _, ok3 := edbInfoIdMap[edbInfoId]; !ok3 {
  1085. if edbInfo, ok4 := edbInfoMap[edbInfoId]; ok4 {
  1086. edbInfoList = append(edbInfoList, edbInfo)
  1087. }
  1088. edbInfoIdMap[edbInfoId] = edbInfoId
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. tmpErr := data_manage.ModifyChartEdbMapping(v.ChartInfoId, edbInfoList)
  1095. if tmpErr != nil {
  1096. errMsgList = append(errMsgList, fmt.Sprint("修改", v.ChartInfoId, "失败,err:", tmpErr))
  1097. }
  1098. }
  1099. return
  1100. }