chart.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. package cross_variety
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. cross_varietyModel "eta_gn/eta_api/models/data_manage/cross_variety"
  7. "eta_gn/eta_api/models/data_manage/cross_variety/request"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services/data"
  10. "eta_gn/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. // GetXYEdbIdList
  436. // @Description: 根据标签id和品种获取指标列表信息
  437. // @author: Roc
  438. // @datetime 2023-11-27 14:31:23
  439. // @param tagX int
  440. // @param tagY int
  441. // @param varietyList []int
  442. // @return xVarietyEdbMap map[int]int
  443. // @return yVarietyEdbMap map[int]int
  444. // @return edbInfoIdList []int
  445. // @return errMsg string
  446. // @return err error
  447. func GetXYEdbIdList(tagX, tagY int, varietyList []int) (xVarietyEdbMap, yVarietyEdbMap map[int]int, edbInfoIdList []int, err error) {
  448. edbInfoIdList = make([]int, 0)
  449. xVarietyEdbMap = make(map[int]int)
  450. yVarietyEdbMap = make(map[int]int)
  451. xList, err := cross_varietyModel.GetChartTagVarietyListByTagAndVariety(tagX, varietyList)
  452. if err != nil {
  453. err = errors.New("获取X轴的品种指标配置信息失败,Err:" + err.Error())
  454. return
  455. }
  456. yList, err := cross_varietyModel.GetChartTagVarietyListByTagAndVariety(tagY, varietyList)
  457. if err != nil {
  458. err = errors.New("获取Y轴的品种指标配置信息失败,Err:" + err.Error())
  459. return
  460. }
  461. baseVarietyIdMap := make(map[int]int)
  462. for _, v := range xList {
  463. baseVarietyIdMap[v.ChartVarietyId] = v.ChartVarietyId
  464. }
  465. // 两个标签里面的品种并集
  466. needVarietyIdMap := make(map[int]int)
  467. for _, v := range yList {
  468. if val, ok := baseVarietyIdMap[v.ChartVarietyId]; ok {
  469. // 如果在 map2 中存在相同的键,则将键和值添加到结果中
  470. needVarietyIdMap[v.ChartVarietyId] = val
  471. }
  472. }
  473. for _, v := range xList {
  474. if _, ok := needVarietyIdMap[v.ChartVarietyId]; ok {
  475. xVarietyEdbMap[v.ChartVarietyId] = v.EdbInfoId
  476. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  477. }
  478. }
  479. for _, v := range yList {
  480. if _, ok := needVarietyIdMap[v.ChartVarietyId]; ok {
  481. yVarietyEdbMap[v.ChartVarietyId] = v.EdbInfoId
  482. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  483. }
  484. }
  485. return
  486. }
  487. // AddChartInfo
  488. // @Description: AddChartInfo
  489. // @author: Roc
  490. // @datetime 2023-11-24 15:58:14
  491. // @param req request.AddChartReq
  492. // @param sysUser *system.Admin
  493. // @return chartInfo *data_manage.ChartInfo
  494. // @return lang string
  495. // @return err error
  496. // @return errMsg string
  497. // @return isSendEmail bool
  498. func AddChartInfo(req request.AddChartReq, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  499. isSendEmail = true
  500. source := utils.CHART_SOURCE_CROSS_HEDGING
  501. req.ChartName = strings.Trim(req.ChartName, " ")
  502. if req.ChartName == "" {
  503. errMsg = "请填写图表名称!"
  504. err = errors.New(errMsg)
  505. isSendEmail = false
  506. return
  507. }
  508. if req.TagX <= 0 {
  509. errMsg = "请选择X轴坐标的标签!"
  510. err = errors.New(errMsg)
  511. isSendEmail = false
  512. return
  513. }
  514. if req.TagY <= 0 {
  515. errMsg = "请选择Y轴坐标的标签!"
  516. err = errors.New(errMsg)
  517. isSendEmail = false
  518. return
  519. }
  520. if req.CalculateValue <= 0 {
  521. errMsg = "请设置时间长度!"
  522. err = errors.New(errMsg)
  523. isSendEmail = false
  524. return
  525. }
  526. if req.CalculateUnit == `` {
  527. errMsg = "请设置时间频度!"
  528. err = errors.New(errMsg)
  529. isSendEmail = false
  530. return
  531. }
  532. if req.PercentType != utils.PercentCalculateTypeRange && req.PercentType != utils.PercentCalculateTypeNum {
  533. errMsg = "请选择百分位"
  534. err = errors.New(errMsg)
  535. isSendEmail = false
  536. return
  537. }
  538. // 品种配置
  539. if len(req.VarietyList) < 0 {
  540. errMsg = "请选择品种!"
  541. err = errors.New(errMsg)
  542. isSendEmail = false
  543. return
  544. }
  545. // 日期配置
  546. dateConfigList := len(req.DateConfigList)
  547. if dateConfigList < 0 {
  548. errMsg = "请选择日期!"
  549. err = errors.New(errMsg)
  550. isSendEmail = false
  551. return
  552. }
  553. if dateConfigList > 5 {
  554. errMsg = "日期数量已达上限!"
  555. err = errors.New(errMsg)
  556. isSendEmail = false
  557. return
  558. }
  559. // 基础配置转string
  560. extraConfigByte, err := json.Marshal(req)
  561. if err != nil {
  562. return
  563. }
  564. chartClassify, err := data_manage.GetCrossVarietyChartClassifyBySysUserId(sysUser.AdminId)
  565. if err != nil {
  566. if !utils.IsErrNoRow(err) {
  567. errMsg = "获取分类信息失败"
  568. err = errors.New("获取分类信息失败,Err:" + err.Error())
  569. return
  570. }
  571. // 分类没有,需要创建新的分类,那么err置空
  572. err = nil
  573. chartClassify, err, errMsg, isSendEmail = data.AddChartClassify(sysUser.RealName, 0, 1, source, lang, sysUser)
  574. if err != nil {
  575. return
  576. }
  577. }
  578. edbInfoIdArr := make([]int, 0)
  579. // 数据校验(品种、标签、指标)
  580. {
  581. // 标签m
  582. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  583. {
  584. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{req.TagX, req.TagY})
  585. if tmpErr != nil {
  586. err = tmpErr
  587. return
  588. }
  589. for _, v := range tagList {
  590. if v.ChartTagId == req.TagX {
  591. xTagInfo = v
  592. } else if v.ChartTagId == req.TagY {
  593. yTagInfo = v
  594. }
  595. }
  596. }
  597. if xTagInfo == nil {
  598. errMsg = "找不到对应的X轴标签"
  599. err = errors.New(errMsg)
  600. return
  601. }
  602. if yTagInfo == nil {
  603. errMsg = "找不到对应的Y轴标签"
  604. err = errors.New(errMsg)
  605. return
  606. }
  607. _, _, edbInfoIdList, tmpErr := GetXYEdbIdList(req.TagX, req.TagY, req.VarietyList)
  608. if tmpErr != nil {
  609. err = errors.New("获取标签配置的品种失败,ERR:" + tmpErr.Error())
  610. return
  611. }
  612. if len(edbInfoIdList) <= 0 {
  613. errMsg = "品种未配置指标"
  614. err = errors.New(errMsg)
  615. isSendEmail = false
  616. return
  617. }
  618. edbInfoIdArr = edbInfoIdList
  619. mappingList, tmpErr := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  620. if tmpErr != nil {
  621. errMsg = "获取指标信息失败"
  622. err = errors.New("获取指标信息失败,ERR:" + tmpErr.Error())
  623. return
  624. }
  625. if len(mappingList) <= 0 {
  626. errMsg = "关联指标为空"
  627. err = errors.New(errMsg)
  628. isSendEmail = false
  629. return
  630. }
  631. }
  632. var chartInfoId int
  633. // 判断图表是否存在
  634. chartInfo = new(data_manage.ChartInfo)
  635. chartInfo.ChartName = req.ChartName
  636. chartInfo.ChartNameEn = req.ChartNameEn
  637. chartInfo.SysUserId = sysUser.AdminId
  638. chartInfo.SysUserRealName = sysUser.RealName
  639. chartInfo.ChartImage = req.ChartImage
  640. chartInfo.CreateTime = time.Now()
  641. chartInfo.ModifyTime = time.Now()
  642. chartInfo.IsSetName = 0
  643. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  644. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  645. chartInfo.ChartType = 9 // 相关性图
  646. chartInfo.Calendar = "公历"
  647. chartInfo.DateType = 6
  648. chartInfo.LeftMin = req.LeftMin
  649. chartInfo.LeftMax = req.LeftMax
  650. chartInfo.XMin = req.XMin
  651. chartInfo.XMax = req.XMax
  652. chartInfo.Source = source
  653. chartInfo.ExtraConfig = string(extraConfigByte)
  654. chartInfo.ChartImage = req.ChartImage
  655. chartInfo.ChartThemeId = req.ChartThemeId
  656. chartInfo.SourcesFrom = req.SourcesFrom
  657. chartInfo.Instructions = req.Instructions
  658. chartInfo.MarkersLines = req.MarkersLines
  659. chartInfo.MarkersAreas = req.MarkersAreas
  660. // 图表品种
  661. chartVarietyMappingList := make([]*cross_varietyModel.ChartVarietyMapping, 0)
  662. for _, varietyId := range req.VarietyList {
  663. chartVarietyMappingList = append(chartVarietyMappingList, &cross_varietyModel.ChartVarietyMapping{
  664. Id: 0,
  665. ChartInfoId: 0,
  666. ChartVarietyId: varietyId,
  667. ModifyTime: time.Now(),
  668. CreateTime: time.Now(),
  669. })
  670. }
  671. // 图表配置
  672. chartInfoCrossVariety := &cross_varietyModel.ChartInfoCrossVariety{
  673. Id: 0,
  674. ChartInfoId: 0,
  675. ChartXTagId: req.TagX,
  676. ChartYTagId: req.TagY,
  677. CalculateValue: req.CalculateValue,
  678. CalculateUnit: req.CalculateUnit,
  679. ModifyTime: time.Now(),
  680. CreateTime: time.Now(),
  681. }
  682. // 新增图表和指标mapping
  683. chartInfoId, e := cross_varietyModel.CreateChart(chartInfo, chartClassify, chartVarietyMappingList, chartInfoCrossVariety)
  684. if e != nil {
  685. errMsg = "操作失败"
  686. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  687. return
  688. }
  689. chartInfo.ChartInfoId = chartInfoId
  690. // 添加指标引用记录
  691. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
  692. //添加es数据
  693. go data.AddOrEditChartInfoToEs(chartInfoId)
  694. return
  695. }
  696. // EditChartInfo
  697. // @Description: 编辑图表
  698. // @author: Roc
  699. // @datetime 2023-11-24 15:58:31
  700. // @param req request.EditChartReq
  701. // @param sysUser *system.Admin
  702. // @return chartItem *data_manage.ChartInfo
  703. // @return err error
  704. // @return errMsg string
  705. // @return isSendEmail bool
  706. func EditChartInfo(req request.EditChartReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  707. isSendEmail = true
  708. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  709. if err != nil {
  710. if utils.IsErrNoRow(err) {
  711. errMsg = "图表已被删除,请刷新页面"
  712. err = errors.New(errMsg)
  713. isSendEmail = false
  714. return
  715. }
  716. errMsg = "获取图表信息失败"
  717. err = errors.New("获取图表信息失败,Err:" + err.Error())
  718. return
  719. }
  720. if chartItem.Source != utils.CHART_SOURCE_CROSS_HEDGING {
  721. errMsg = "该图不是跨品种分析图表!"
  722. err = errors.New(errMsg)
  723. isSendEmail = false
  724. return
  725. }
  726. req.ChartName = strings.Trim(req.ChartName, " ")
  727. if req.ChartName == "" {
  728. errMsg = "请填写图表名称!"
  729. err = errors.New(errMsg)
  730. isSendEmail = false
  731. return
  732. }
  733. if req.TagX <= 0 {
  734. errMsg = "请选择X轴坐标的标签!"
  735. err = errors.New(errMsg)
  736. isSendEmail = false
  737. return
  738. }
  739. if req.TagY <= 0 {
  740. errMsg = "请选择Y轴坐标的标签!"
  741. err = errors.New(errMsg)
  742. isSendEmail = false
  743. return
  744. }
  745. if req.CalculateValue <= 0 {
  746. errMsg = "请设置时间长度!"
  747. err = errors.New(errMsg)
  748. isSendEmail = false
  749. return
  750. }
  751. if req.CalculateUnit == `` {
  752. errMsg = "请设置时间频度!"
  753. err = errors.New(errMsg)
  754. isSendEmail = false
  755. return
  756. }
  757. if req.PercentType != utils.PercentCalculateTypeRange && req.PercentType != utils.PercentCalculateTypeNum {
  758. errMsg = "请选择百分位"
  759. err = errors.New(errMsg)
  760. isSendEmail = false
  761. return
  762. }
  763. // 品种配置
  764. if len(req.VarietyList) < 0 {
  765. errMsg = "请选择品种!"
  766. err = errors.New(errMsg)
  767. isSendEmail = false
  768. return
  769. }
  770. // 日期配置
  771. dateConfigList := len(req.DateConfigList)
  772. if dateConfigList < 0 {
  773. errMsg = "请选择日期!"
  774. err = errors.New(errMsg)
  775. isSendEmail = false
  776. return
  777. }
  778. if dateConfigList > 5 {
  779. errMsg = "日期数量已达上限!"
  780. err = errors.New(errMsg)
  781. isSendEmail = false
  782. return
  783. }
  784. // 基础配置转string
  785. extraConfigByte, err := json.Marshal(req)
  786. if err != nil {
  787. return
  788. }
  789. // 图表操作权限
  790. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  791. if !ok {
  792. errMsg = "没有该图表的操作权限"
  793. err = errors.New(errMsg)
  794. isSendEmail = false
  795. return
  796. }
  797. edbInfoIdArr := make([]int, 0)
  798. // 数据校验(品种、标签、指标)
  799. {
  800. // 标签m
  801. var xTagInfo, yTagInfo *cross_varietyModel.ChartTag
  802. {
  803. tagList, tmpErr := cross_varietyModel.GetTagListByIdList([]int{req.TagX, req.TagY})
  804. if tmpErr != nil {
  805. err = tmpErr
  806. return
  807. }
  808. for _, v := range tagList {
  809. if v.ChartTagId == req.TagX {
  810. xTagInfo = v
  811. } else if v.ChartTagId == req.TagY {
  812. yTagInfo = v
  813. }
  814. }
  815. }
  816. if xTagInfo == nil {
  817. errMsg = "找不到对应的X轴标签"
  818. err = errors.New(errMsg)
  819. return
  820. }
  821. if yTagInfo == nil {
  822. errMsg = "找不到对应的Y轴标签"
  823. err = errors.New(errMsg)
  824. return
  825. }
  826. _, _, edbInfoIdList, tmpErr := GetXYEdbIdList(req.TagX, req.TagY, req.VarietyList)
  827. if tmpErr != nil {
  828. err = errors.New("获取标签配置的品种失败,ERR:" + tmpErr.Error())
  829. return
  830. }
  831. if len(edbInfoIdList) <= 0 {
  832. errMsg = "品种未配置指标"
  833. err = errors.New(errMsg)
  834. isSendEmail = false
  835. return
  836. }
  837. edbInfoIdArr = edbInfoIdList
  838. mappingList, tmpErr := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  839. if tmpErr != nil {
  840. errMsg = "获取指标信息失败"
  841. err = errors.New("获取指标信息失败,ERR:" + tmpErr.Error())
  842. return
  843. }
  844. if len(mappingList) <= 0 {
  845. errMsg = "关联指标为空"
  846. err = errors.New(errMsg)
  847. isSendEmail = false
  848. return
  849. }
  850. }
  851. //判断图表是否存在
  852. //var condition string
  853. //var pars []interface{}
  854. //condition += " AND chart_info_id <> ? "
  855. //pars = append(pars, req.ChartInfoId)
  856. //condition += " AND chart_name=? AND source = ? "
  857. //pars = append(pars, req.ChartName, chartItem.Source)
  858. //count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  859. //if err != nil {
  860. // errMsg = "判断图表名称是否存在失败"
  861. // err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  862. // return
  863. //}
  864. //if count > 0 {
  865. // errMsg = "图表已存在,请重新填写"
  866. // err = errors.New(errMsg)
  867. // isSendEmail = false
  868. // return
  869. //}
  870. switch lang {
  871. case utils.EnLangVersion:
  872. chartItem.ChartNameEn = req.ChartName
  873. default:
  874. chartItem.ChartName = req.ChartName
  875. }
  876. chartItem.ExtraConfig = string(extraConfigByte)
  877. chartItem.ModifyTime = time.Now()
  878. chartItem.LeftMin = req.LeftMin
  879. chartItem.LeftMax = req.LeftMax
  880. chartItem.XMin = req.XMin
  881. chartItem.XMax = req.XMax
  882. chartUpdateCols := []string{"ChartName", "ChartNameEn", "ExtraConfig", "ModifyTime", "LeftMin", "LeftMax", "XMin", "XMax"}
  883. // 跨品种分析配置
  884. chartInfoCrossVariety, err := cross_varietyModel.GetChartInfoCrossVarietyByChartInfoId(chartItem.ChartInfoId)
  885. if err != nil {
  886. return
  887. }
  888. chartInfoCrossVariety.ChartXTagId = req.TagX
  889. chartInfoCrossVariety.ChartYTagId = req.TagY
  890. chartInfoCrossVariety.CalculateValue = req.CalculateValue
  891. chartInfoCrossVariety.CalculateUnit = req.CalculateUnit
  892. chartInfoCrossVariety.ModifyTime = time.Now()
  893. chartInfoCrossVarietyUpdateCols := []string{"ChartXTagId", "ChartYTagId", "CalculateValue", "CalculateUnit", "ModifyTime"}
  894. // 图表品种
  895. chartVarietyMappingList := make([]*cross_varietyModel.ChartVarietyMapping, 0)
  896. for _, varietyId := range req.VarietyList {
  897. chartVarietyMappingList = append(chartVarietyMappingList, &cross_varietyModel.ChartVarietyMapping{
  898. Id: 0,
  899. ChartInfoId: 0,
  900. ChartVarietyId: varietyId,
  901. ModifyTime: time.Now(),
  902. CreateTime: time.Now(),
  903. })
  904. }
  905. err = cross_varietyModel.EditChart(chartItem, chartVarietyMappingList, chartInfoCrossVariety,
  906. chartUpdateCols, chartInfoCrossVarietyUpdateCols)
  907. if err != nil {
  908. errMsg = "保存失败"
  909. err = errors.New("保存失败,Err:" + err.Error())
  910. return
  911. }
  912. resp := new(data_manage.AddChartInfoResp)
  913. resp.ChartInfoId = chartItem.ChartInfoId
  914. resp.UniqueCode = chartItem.UniqueCode
  915. //resp.ChartType = req.ChartType
  916. // 添加指标引用记录
  917. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
  918. //添加es数据
  919. go data.AddOrEditChartInfoToEs(chartItem.ChartInfoId)
  920. //修改my eta es数据
  921. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  922. return
  923. }
  924. // ModifyChartEdbMapping
  925. // @Description: 修改图表的关系表
  926. // @author: Roc
  927. // @datetime 2023-12-11 17:32:31
  928. // @param tagId int
  929. func ModifyChartEdbMapping(tagId int) {
  930. errMsgList := make([]string, 0)
  931. var err error
  932. defer func() {
  933. if err != nil {
  934. errMsgList = append(errMsgList, err.Error())
  935. }
  936. if len(errMsgList) > 0 {
  937. utils.FileLog.Error("修改跨品种分析的图表关联指标失败:\n" + strings.Join(errMsgList, "\n"))
  938. }
  939. }()
  940. // 找出标签关联的图表
  941. list, err := cross_varietyModel.GeChartInfoCrossVarietyListByTagId(tagId)
  942. if err != nil {
  943. return
  944. }
  945. chartInfoIdList := make([]int, 0)
  946. tagIdList := make([]int, 0)
  947. tagIdMap := make(map[int]int, 0)
  948. for _, v := range list {
  949. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  950. if _, ok := tagIdMap[v.ChartXTagId]; !ok {
  951. tagIdList = append(tagIdList, v.ChartXTagId)
  952. tagIdMap[v.ChartXTagId] = 1
  953. }
  954. if _, ok := tagIdMap[v.ChartYTagId]; !ok {
  955. tagIdList = append(tagIdList, v.ChartYTagId)
  956. tagIdMap[v.ChartYTagId] = 1
  957. }
  958. }
  959. // 找出所有标签关联的品种和对应的指标
  960. chartTagVarietyList, err := cross_varietyModel.GetChartTagVarietyListByTagIdList(tagIdList)
  961. if err != nil {
  962. return
  963. }
  964. // 所有指标id
  965. allEdbInfoIdList := make([]int, 0)
  966. // 标签关联品种的指标列表
  967. tagVarietyEdbMap := make(map[string][]int)
  968. for _, v := range chartTagVarietyList {
  969. if v.EdbInfoId <= 0 {
  970. continue
  971. }
  972. key := fmt.Sprint(v.ChartTagId, "_", v.ChartVarietyId)
  973. tagVarietyEdbList, ok := tagVarietyEdbMap[key]
  974. if !ok {
  975. tagVarietyEdbList = make([]int, 0)
  976. }
  977. tagVarietyEdbMap[key] = append(tagVarietyEdbList, v.EdbInfoId)
  978. allEdbInfoIdList = append(allEdbInfoIdList, v.EdbInfoId)
  979. }
  980. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  981. // 获取指标列表
  982. if len(allEdbInfoIdList) > 0 {
  983. allEdbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(allEdbInfoIdList)
  984. if tmpErr != nil {
  985. err = tmpErr
  986. return
  987. }
  988. for _, v := range allEdbInfoList {
  989. edbInfoMap[v.EdbInfoId] = v
  990. }
  991. }
  992. // 图表关联的品种列表
  993. chartVarietyMappingList, err := cross_varietyModel.GetChartVarietyMappingListByChartInfoIdList(chartInfoIdList)
  994. if err != nil {
  995. return
  996. }
  997. // 图表关联的品种map
  998. chartVarietyMappingListMap := make(map[int][]int)
  999. for _, v := range chartVarietyMappingList {
  1000. if v.ChartInfoId <= 0 {
  1001. continue
  1002. }
  1003. varietyMappingList, ok := chartVarietyMappingListMap[v.ChartInfoId]
  1004. if !ok {
  1005. varietyMappingList = make([]int, 0)
  1006. }
  1007. chartVarietyMappingListMap[v.ChartInfoId] = append(varietyMappingList, v.ChartVarietyId)
  1008. }
  1009. // 处理图表
  1010. for _, v := range list {
  1011. // 获取关联的指标id
  1012. edbInfoList := make([]*data_manage.EdbInfo, 0)
  1013. edbInfoIdMap := make(map[int]int, 0)
  1014. // 找出图表关联的品种
  1015. varietyIdList, ok := chartVarietyMappingListMap[v.ChartInfoId]
  1016. if ok {
  1017. // 获取品种与标签的id
  1018. for _, varietyId := range varietyIdList {
  1019. // 先处理x轴
  1020. key := fmt.Sprint(v.ChartXTagId, "_", varietyId)
  1021. if tmpEdbInfoIdList, ok2 := tagVarietyEdbMap[key]; ok2 {
  1022. for _, edbInfoId := range tmpEdbInfoIdList {
  1023. if _, ok3 := edbInfoIdMap[edbInfoId]; !ok3 {
  1024. if edbInfo, ok4 := edbInfoMap[edbInfoId]; ok4 {
  1025. edbInfoList = append(edbInfoList, edbInfo)
  1026. }
  1027. edbInfoIdMap[edbInfoId] = edbInfoId
  1028. }
  1029. }
  1030. }
  1031. // 处理y轴
  1032. key = fmt.Sprint(v.ChartYTagId, "_", varietyId)
  1033. if tmpEdbInfoIdList, ok2 := tagVarietyEdbMap[key]; ok2 {
  1034. for _, edbInfoId := range tmpEdbInfoIdList {
  1035. if _, ok3 := edbInfoIdMap[edbInfoId]; !ok3 {
  1036. if edbInfo, ok4 := edbInfoMap[edbInfoId]; ok4 {
  1037. edbInfoList = append(edbInfoList, edbInfo)
  1038. }
  1039. edbInfoIdMap[edbInfoId] = edbInfoId
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. tmpErr := data_manage.ModifyChartEdbMapping(v.ChartInfoId, edbInfoList)
  1046. if tmpErr != nil {
  1047. errMsgList = append(errMsgList, fmt.Sprint("修改", v.ChartInfoId, "失败,err:", tmpErr))
  1048. }
  1049. }
  1050. return
  1051. }