chart.go 30 KB

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