chart_extra_config.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/services/google"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func HandleExtraConfig(chartType int, extraConfigStr string) (newExtraConfigStr string, newAllExtraConfigStr string, err error, errMsg string) {
  15. newExtraConfigStr = extraConfigStr
  16. switch chartType {
  17. case 10: // 截面散点图
  18. var tmpExtraConfig data_manage.SectionScatterReq
  19. if extraConfigStr == `` {
  20. errMsg = "截面散点图未配置"
  21. err = errors.New(errMsg)
  22. return
  23. }
  24. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  25. if err != nil {
  26. errMsg = "截面散点配置异常"
  27. err = errors.New(errMsg)
  28. return
  29. }
  30. newExtraConfigStr, err, errMsg = handleSectionScatterChartData(tmpExtraConfig)
  31. case utils.CHART_TYPE_SECTION_COMBINE:
  32. //整理组合图的配置信息,将部分信息存入其他表
  33. // 预览和详情都走这个接口
  34. var sectionExtraConfig data_manage.ChartSectionAllExtraConf
  35. if extraConfigStr == `` {
  36. errMsg = "截面组合图未配置"
  37. err = errors.New(errMsg)
  38. return
  39. }
  40. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  41. if err != nil {
  42. errMsg = "截面组合图配置异常"
  43. err = errors.New(errMsg)
  44. return
  45. }
  46. newAllExtraConfig, e, msg := handleChartSectionCombineData(sectionExtraConfig)
  47. if e != nil {
  48. err = e
  49. errMsg = msg
  50. return
  51. }
  52. newAllExtraConfigByte, e := json.Marshal(newAllExtraConfig)
  53. if e != nil {
  54. err = e
  55. return
  56. }
  57. newAllExtraConfigStr = string(newAllExtraConfigByte)
  58. newExtraConfig := &data_manage.ChartSectionExtraConf{
  59. DateConfList: newAllExtraConfig.DateConfList,
  60. IsHeap: newAllExtraConfig.IsHeap,
  61. XDataList: newAllExtraConfig.XDataList,
  62. UnitList: newAllExtraConfig.UnitList,
  63. BaseChartSeriesName: newAllExtraConfig.BaseChartSeriesName,
  64. SortType: newAllExtraConfig.SortType,
  65. }
  66. extraConfigByte, e := json.Marshal(newExtraConfig)
  67. if e != nil {
  68. err = e
  69. return
  70. }
  71. newExtraConfigStr = string(extraConfigByte)
  72. }
  73. return
  74. }
  75. // handleSectionScatterChartData 截面散点图的数据处理
  76. func handleSectionScatterChartData(extraConfig data_manage.SectionScatterReq) (extraConfigStr string, err error, errMsg string) {
  77. translateNameList := make([]string, 0)
  78. translateNameMap := make(map[string]bool, 0)
  79. if extraConfig.XNameEn == `` {
  80. if _, ok := translateNameMap[extraConfig.XName]; !ok {
  81. translateNameMap[extraConfig.XName] = true
  82. tmpName := strings.TrimSuffix(extraConfig.XName, " ")
  83. tmpName = strings.TrimPrefix(tmpName, " ")
  84. translateNameList = append(translateNameList, tmpName)
  85. }
  86. }
  87. if extraConfig.XUnitNameEn == `` {
  88. if _, ok := translateNameMap[extraConfig.XUnitName]; !ok {
  89. translateNameMap[extraConfig.XUnitName] = true
  90. tmpName := strings.TrimSuffix(extraConfig.XUnitName, " ")
  91. tmpName = strings.TrimPrefix(tmpName, " ")
  92. translateNameList = append(translateNameList, tmpName)
  93. }
  94. }
  95. if extraConfig.YNameEn == `` {
  96. if _, ok := translateNameMap[extraConfig.YName]; !ok {
  97. translateNameMap[extraConfig.YName] = true
  98. tmpName := strings.TrimSuffix(extraConfig.YName, " ")
  99. tmpName = strings.TrimPrefix(tmpName, " ")
  100. translateNameList = append(translateNameList, tmpName)
  101. }
  102. }
  103. if extraConfig.YUnitNameEn == `` {
  104. if _, ok := translateNameMap[extraConfig.YUnitName]; !ok {
  105. translateNameMap[extraConfig.YUnitName] = true
  106. tmpName := strings.TrimSuffix(extraConfig.YUnitName, " ")
  107. tmpName = strings.TrimPrefix(tmpName, " ")
  108. translateNameList = append(translateNameList, tmpName)
  109. }
  110. }
  111. for _, v := range extraConfig.SeriesList {
  112. if v.NameEn == `` {
  113. if _, ok := translateNameMap[v.Name]; !ok {
  114. translateNameMap[v.Name] = true
  115. tmpName := strings.TrimSuffix(v.Name, " ")
  116. tmpName = strings.TrimPrefix(tmpName, " ")
  117. translateNameList = append(translateNameList, tmpName)
  118. }
  119. }
  120. for _, edbInfo := range v.EdbInfoList {
  121. if edbInfo.NameEn == `` {
  122. if _, ok := translateNameMap[edbInfo.Name]; !ok {
  123. translateNameMap[edbInfo.Name] = true
  124. tmpName := strings.TrimSuffix(edbInfo.Name, " ")
  125. tmpName = strings.TrimPrefix(tmpName, " ")
  126. translateNameList = append(translateNameList, tmpName)
  127. }
  128. }
  129. }
  130. }
  131. // 获取英文名称map
  132. enNameMap, _, _ := GetEnNameMapByCnNameList(translateNameList)
  133. for k, seriesItem := range extraConfig.SeriesList {
  134. if len(seriesItem.EdbInfoList) <= 0 {
  135. errMsg = "指标不能为空"
  136. err = errors.New(errMsg)
  137. return
  138. }
  139. for edbIndex, edbConf := range seriesItem.EdbInfoList {
  140. if edbConf.NameEn == `` { //标签英文名称
  141. if tmpNameEn, ok := enNameMap[edbConf.Name]; ok {
  142. edbConf.NameEn = tmpNameEn
  143. }
  144. }
  145. seriesItem.EdbInfoList[edbIndex] = edbConf
  146. }
  147. if seriesItem.NameEn == `` { //系列英文名称
  148. if tmpNameEn, ok := enNameMap[seriesItem.Name]; ok {
  149. seriesItem.NameEn = tmpNameEn
  150. }
  151. }
  152. extraConfig.SeriesList[k] = seriesItem
  153. }
  154. if extraConfig.XNameEn == `` {
  155. if tmpNameEn, ok := enNameMap[extraConfig.XName]; ok {
  156. extraConfig.XNameEn = tmpNameEn
  157. }
  158. }
  159. if extraConfig.XUnitNameEn == `` {
  160. if tmpNameEn, ok := enNameMap[extraConfig.XUnitName]; ok {
  161. extraConfig.XUnitNameEn = tmpNameEn
  162. }
  163. }
  164. if extraConfig.YNameEn == `` {
  165. if tmpNameEn, ok := enNameMap[extraConfig.YName]; ok {
  166. extraConfig.YNameEn = tmpNameEn
  167. }
  168. }
  169. if extraConfig.YUnitNameEn == `` {
  170. if tmpNameEn, ok := enNameMap[extraConfig.YUnitName]; ok {
  171. extraConfig.YUnitNameEn = tmpNameEn
  172. }
  173. }
  174. extraConfigByte, err := json.Marshal(extraConfig)
  175. if err != nil {
  176. return
  177. }
  178. extraConfigStr = string(extraConfigByte)
  179. return
  180. }
  181. // GetEnNameMapByCnNameList 根据中文名称列表获取英文名称map
  182. func GetEnNameMapByCnNameList(cnNameList []string) (contentEnMap map[string]string, err error, errMsg string) {
  183. // 返回参初始化
  184. contentEnMap = make(map[string]string)
  185. // 英文临时赋值变量
  186. tmpContentEnMapAll := make(map[string]string)
  187. count := 0
  188. contentMap := make(map[string]string, 0)
  189. for k, v := range cnNameList {
  190. //如果单条翻译的字符数超过1000,则直接翻译,否则批量翻译
  191. if count >= 50 { //待翻译的条数不能超过50; 单条翻译字符数不能超过1000字符
  192. tmpContentEnMap, tmpErr, tmpErrMsg := google.BatchTranslateHandlerByGoogle(contentMap)
  193. if tmpErr != nil {
  194. err = tmpErr
  195. errMsg = tmpErrMsg
  196. return
  197. }
  198. for tmpK, contentEn := range tmpContentEnMap {
  199. tmpContentEnMapAll[tmpK] = contentEn
  200. }
  201. contentMap = make(map[string]string, 0)
  202. count = 0
  203. }
  204. contentMap[strconv.Itoa(k)] = v
  205. count += 1
  206. }
  207. //剩余不满50条的content
  208. if count > 0 {
  209. tmpContentEnMap, tmpErr, tmpErrMsg := google.BatchTranslateHandlerByGoogle(contentMap)
  210. if tmpErr != nil {
  211. err = tmpErr
  212. errMsg = tmpErrMsg
  213. return
  214. }
  215. for tmpK, contentEn := range tmpContentEnMap {
  216. tmpContentEnMapAll[tmpK] = contentEn
  217. }
  218. }
  219. // 重新组装拼接返回
  220. lenCnNameList := len(cnNameList)
  221. for k, v := range tmpContentEnMapAll {
  222. tmpIndex, _ := strconv.Atoi(k)
  223. if tmpIndex < lenCnNameList {
  224. contentEnMap[cnNameList[tmpIndex]] = v
  225. }
  226. }
  227. return
  228. }
  229. // handleSectionScatterChartData 截面组合图的英文文案处理
  230. func handleChartSectionCombineData(extraConfig data_manage.ChartSectionAllExtraConf) (newExtraConfig data_manage.ChartSectionAllExtraConf, err error, errMsg string) {
  231. translateNameList := make([]string, 0)
  232. translateNameMap := make(map[string]bool, 0)
  233. for _, v := range extraConfig.DateConfList {
  234. if _, ok := translateNameMap[v.DateConfName]; !ok && v.DateConfNameEn == "" {
  235. translateNameMap[v.DateConfName] = true
  236. tmpName := strings.TrimSuffix(v.DateConfName, " ")
  237. tmpName = strings.TrimPrefix(tmpName, " ")
  238. translateNameList = append(translateNameList, tmpName)
  239. }
  240. }
  241. if _, ok := translateNameMap[extraConfig.UnitList.LeftName]; !ok && extraConfig.UnitList.LeftNameEn == "" {
  242. translateNameMap[extraConfig.UnitList.LeftName] = true
  243. tmpName := strings.TrimSuffix(extraConfig.UnitList.LeftName, " ")
  244. tmpName = strings.TrimPrefix(tmpName, " ")
  245. translateNameList = append(translateNameList, tmpName)
  246. }
  247. if _, ok := translateNameMap[extraConfig.UnitList.RightName]; !ok && extraConfig.UnitList.RightNameEn == "" {
  248. translateNameMap[extraConfig.UnitList.RightName] = true
  249. tmpName := strings.TrimSuffix(extraConfig.UnitList.RightName, " ")
  250. tmpName = strings.TrimPrefix(tmpName, " ")
  251. translateNameList = append(translateNameList, tmpName)
  252. }
  253. if _, ok := translateNameMap[extraConfig.UnitList.RightTwoName]; !ok && extraConfig.UnitList.RightTwoNameEn == "" {
  254. translateNameMap[extraConfig.UnitList.RightTwoName] = true
  255. tmpName := strings.TrimSuffix(extraConfig.UnitList.RightTwoName, " ")
  256. tmpName = strings.TrimPrefix(tmpName, " ")
  257. translateNameList = append(translateNameList, tmpName)
  258. }
  259. for _, v := range extraConfig.XDataList {
  260. if _, ok := translateNameMap[v.Name]; !ok && v.NameEn == "" {
  261. translateNameMap[v.Name] = true
  262. tmpName := strings.TrimSuffix(v.Name, " ")
  263. tmpName = strings.TrimPrefix(tmpName, " ")
  264. translateNameList = append(translateNameList, tmpName)
  265. }
  266. }
  267. for _, v := range extraConfig.SeriesList {
  268. if v.SeriesNameEn == `` {
  269. if _, ok := translateNameMap[v.SeriesName]; !ok {
  270. translateNameMap[v.SeriesName] = true
  271. tmpName := strings.TrimSuffix(v.SeriesName, " ")
  272. tmpName = strings.TrimPrefix(tmpName, " ")
  273. translateNameList = append(translateNameList, tmpName)
  274. }
  275. }
  276. }
  277. // 获取英文名称map
  278. enNameMap, _, _ := GetEnNameMapByCnNameList(translateNameList)
  279. for k, seriesItem := range extraConfig.SeriesList {
  280. if len(seriesItem.EdbInfoList) <= 0 {
  281. errMsg = "指标不能为空"
  282. err = errors.New(errMsg)
  283. return
  284. }
  285. if seriesItem.SeriesNameEn == `` { //系列英文名称
  286. if tmpNameEn, ok := enNameMap[seriesItem.SeriesName]; ok {
  287. seriesItem.SeriesNameEn = tmpNameEn
  288. }
  289. }
  290. extraConfig.SeriesList[k] = seriesItem
  291. }
  292. for k, v := range extraConfig.XDataList {
  293. if tmpNameEn, ok := enNameMap[v.Name]; ok && v.NameEn == "" {
  294. extraConfig.XDataList[k].NameEn = tmpNameEn
  295. }
  296. }
  297. if tmpNameEn, ok := enNameMap[extraConfig.UnitList.LeftName]; ok && extraConfig.UnitList.LeftNameEn == "" {
  298. extraConfig.UnitList.LeftName = tmpNameEn
  299. }
  300. if tmpNameEn, ok := enNameMap[extraConfig.UnitList.RightName]; ok && extraConfig.UnitList.RightNameEn == "" {
  301. extraConfig.UnitList.RightName = tmpNameEn
  302. }
  303. if tmpNameEn, ok := enNameMap[extraConfig.UnitList.RightTwoName]; ok && extraConfig.UnitList.RightTwoNameEn == "" {
  304. extraConfig.UnitList.RightTwoName = tmpNameEn
  305. }
  306. for k, v := range extraConfig.DateConfList {
  307. if tmpNameEn, ok := enNameMap[v.DateConfName]; ok && v.DateConfNameEn == "" {
  308. extraConfig.DateConfList[k].DateConfNameEn = tmpNameEn
  309. }
  310. }
  311. newExtraConfig = extraConfig
  312. return
  313. }
  314. // GetChartSectionCombineData 截面组合图的数据处理
  315. func GetChartSectionCombineData(chartInfo *data_manage.ChartInfo, mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.ChartSectionAllExtraConf) (edbIdList []int, dataListResp data_manage.ChartSectionCombineDataResp, err error) {
  316. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  317. edbDataMap := make(map[int]map[string]float64)
  318. for edbInfoId, edbDataList := range edbDataListMap {
  319. edbDateData := make(map[string]float64)
  320. for _, edbData := range edbDataList {
  321. edbDateData[edbData.DataTime] = edbData.Value
  322. }
  323. edbDataMap[edbInfoId] = edbDateData
  324. }
  325. // edbIdList 指标展示顺序;x轴的指标顺序
  326. edbIdList = make([]int, 0)
  327. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  328. for _, v := range mappingList {
  329. edbIdList = append(edbIdList, v.EdbInfoId)
  330. edbMappingMap[v.EdbInfoId] = v
  331. }
  332. // 确定好截面散点图返回的数据格式
  333. // 获取所有的引用日期设置
  334. dateConfListMap := make(map[string]*data_manage.ChartSectionDateConfItem)
  335. dateConfEdbIds := make([]int, 0)
  336. for _, v := range extraConfig.DateConfList {
  337. if v.EdbInfoId > 0 {
  338. dateConfEdbIds = append(dateConfEdbIds, v.EdbInfoId)
  339. }
  340. dateConfListMap[v.DateConfName] = v
  341. }
  342. // 遍历每个系列
  343. // 遍历每个指标,根据选中的日期,进行日期变换得到最终的日期,根据最终的日期获取对应的值
  344. // 组装数据
  345. baseSeries := new(data_manage.ChartSectionSeriesItem) //y轴的系列
  346. var firstUnit, leftUnit, rightUnit, right2Unit *data_manage.XData
  347. var (
  348. LeftMin float64
  349. LeftMax float64
  350. RightMin float64
  351. RightMax float64
  352. Right2Min float64
  353. Right2Max float64
  354. )
  355. seriesDataListMap := make(map[string][]float64)
  356. seriesNoDataIndexMap := make(map[string][]int)
  357. for _, seriesItem := range extraConfig.SeriesList {
  358. var maxDate time.Time
  359. var minVal, maxVal float64
  360. noDataEdbIndex := make([]int, 0)
  361. dataList := make([]float64, len(seriesItem.EdbInfoList))
  362. for index, edbConf := range seriesItem.EdbInfoList {
  363. edbInfoId := edbConf.EdbInfoId //X轴的指标
  364. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  365. if !ok {
  366. continue
  367. }
  368. seriesItem.EdbInfoList[index].EdbName = edbMappingInfo.EdbName
  369. seriesItem.EdbInfoList[index].EdbNameEn = edbMappingInfo.EdbNameEn
  370. seriesItem.EdbInfoList[index].EdbInfoType = edbMappingInfo.EdbInfoCategoryType
  371. seriesItem.EdbInfoList[index].Unit = edbMappingInfo.Unit
  372. seriesItem.EdbInfoList[index].UnitEn = edbMappingInfo.UnitEn
  373. if index == 0 {
  374. firstUnit = &data_manage.XData{
  375. Name: edbMappingInfo.Unit,
  376. NameEn: edbMappingInfo.UnitEn,
  377. }
  378. }
  379. edbDataList, ok3 := edbDataListMap[edbInfoId]
  380. if !ok3 {
  381. err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
  382. return
  383. }
  384. //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
  385. var findDate string
  386. if edbConf.DateConfType == 0 {
  387. if edbInfoId == 0 {
  388. err = fmt.Errorf("请选择指标")
  389. return
  390. }
  391. findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
  392. if err != nil {
  393. err = fmt.Errorf("指标%d的日期变换处理失败", edbInfoId)
  394. return
  395. }
  396. } else {
  397. // 获取日期配置
  398. dateConfItem, ok1 := dateConfListMap[edbConf.DateConfName]
  399. if !ok1 {
  400. err = fmt.Errorf("引用日期配置不存在")
  401. return
  402. }
  403. // todo 根据日期变换得到最终日期
  404. edbDataListTmp := make([]*data_manage.EdbDataList, 0)
  405. if dateConfItem.EdbInfoId > 0 {
  406. edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
  407. if !ok1 {
  408. err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
  409. return
  410. }
  411. }
  412. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  413. if err != nil {
  414. err = fmt.Errorf("指标%d的日期变换处理失败", dateConfItem.EdbInfoId)
  415. return
  416. }
  417. }
  418. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  419. if maxDate.IsZero() {
  420. maxDate = findDateTime
  421. } else {
  422. if findDateTime.After(maxDate) {
  423. maxDate = findDateTime
  424. }
  425. }
  426. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  427. dataList[index] = tmpValue
  428. if index == 0 {
  429. minVal = tmpValue
  430. maxVal = tmpValue
  431. } else {
  432. if tmpValue < minVal {
  433. minVal = tmpValue
  434. }
  435. if tmpValue > maxVal {
  436. maxVal = tmpValue
  437. }
  438. }
  439. } else {
  440. dataList[index] = 0
  441. noDataEdbIndex = append(noDataEdbIndex, index)
  442. continue
  443. }
  444. }
  445. seriesDataListMap[seriesItem.SeriesName] = dataList
  446. seriesNoDataIndexMap[seriesItem.SeriesName] = noDataEdbIndex
  447. seriesItem.DataList = dataList
  448. seriesItem.MinData = minVal
  449. seriesItem.MaxData = maxVal
  450. seriesItem.NoDataEdbIndex = noDataEdbIndex
  451. if extraConfig.BaseChartSeriesName == seriesItem.SeriesName {
  452. baseSeries = seriesItem
  453. }
  454. if seriesItem.IsAxis == 1 && leftUnit == nil { //左轴,右轴
  455. leftUnit = firstUnit
  456. } else if seriesItem.IsAxis == 0 && rightUnit == nil {
  457. rightUnit = firstUnit
  458. } else if seriesItem.IsAxis == 2 && right2Unit == nil {
  459. right2Unit = firstUnit
  460. }
  461. //处理上下限
  462. var minData, maxData float64
  463. for _, d := range seriesItem.DataList {
  464. if minData > d {
  465. minData = d
  466. }
  467. if maxData < d {
  468. maxData = d
  469. }
  470. }
  471. if seriesItem.IsAxis == 1 {
  472. if LeftMin > minData {
  473. LeftMin = minData
  474. }
  475. if LeftMax < maxData {
  476. LeftMax = maxData
  477. }
  478. } else if seriesItem.IsAxis == 0 {
  479. if RightMin > minData {
  480. RightMin = minData
  481. }
  482. if RightMax < maxData {
  483. RightMax = maxData
  484. }
  485. } else {
  486. if Right2Min > minData {
  487. Right2Min = minData
  488. }
  489. if Right2Max < maxData {
  490. Right2Max = maxData
  491. }
  492. }
  493. }
  494. // 处理横轴
  495. // 遍历基准系列,判断有几个横轴名称
  496. if baseSeries == nil {
  497. err = fmt.Errorf("基准系列不存在")
  498. return
  499. }
  500. // 处理系列排序
  501. if extraConfig.SortType > 0 {
  502. newSeriesDataListMap, newSeriesNoDataIndexMap := SortChartSeriesDataSet(baseSeries.SeriesName, baseSeries.DataList, baseSeries.NoDataEdbIndex, seriesDataListMap, seriesNoDataIndexMap, extraConfig.SortType)
  503. for k, item := range extraConfig.SeriesList {
  504. dataList, ok := newSeriesDataListMap[item.SeriesName]
  505. if ok {
  506. extraConfig.SeriesList[k].DataList = dataList
  507. }
  508. noIndex, ok := newSeriesNoDataIndexMap[item.SeriesName]
  509. if ok {
  510. extraConfig.SeriesList[k].NoDataEdbIndex = noIndex
  511. }
  512. }
  513. }
  514. xDataList := make([]data_manage.XData, 0)
  515. for index, item := range baseSeries.EdbInfoList {
  516. if index == 0 {
  517. firstUnit = &data_manage.XData{
  518. Name: item.Unit,
  519. NameEn: item.UnitEn,
  520. }
  521. }
  522. tmp := data_manage.XData{
  523. Name: item.EdbName,
  524. NameEn: item.EdbNameEn,
  525. }
  526. // 如果已经设置了横轴名称,则用设置的名称替换
  527. if len(extraConfig.XDataList) > index {
  528. newItem := extraConfig.XDataList[index]
  529. if newItem.Name != "" {
  530. tmp = newItem
  531. }
  532. }
  533. xDataList = append(xDataList, tmp)
  534. }
  535. dataListResp.XDataList = xDataList
  536. unitList := new(data_manage.ChartSectionCombineUnit)
  537. if baseSeries.IsAxis == 1 { //左轴,右轴
  538. leftUnit = firstUnit
  539. } else if baseSeries.IsAxis == 2 {
  540. rightUnit = firstUnit
  541. } else {
  542. right2Unit = firstUnit
  543. }
  544. if leftUnit != nil {
  545. unitList.LeftName = leftUnit.Name
  546. unitList.LeftNameEn = leftUnit.NameEn
  547. }
  548. if rightUnit != nil {
  549. unitList.RightName = rightUnit.Name
  550. unitList.RightNameEn = rightUnit.NameEn
  551. }
  552. if right2Unit != nil {
  553. unitList.RightTwoName = right2Unit.Name
  554. unitList.RightTwoNameEn = right2Unit.NameEn
  555. }
  556. if extraConfig.UnitList.LeftName != "" {
  557. unitList.LeftName = extraConfig.UnitList.LeftName
  558. unitList.LeftNameEn = extraConfig.UnitList.LeftNameEn
  559. }
  560. if extraConfig.UnitList.RightName != "" {
  561. unitList.RightName = extraConfig.UnitList.RightName
  562. unitList.RightNameEn = extraConfig.UnitList.RightNameEn
  563. }
  564. if extraConfig.UnitList.RightTwoName != "" {
  565. unitList.RightTwoName = extraConfig.UnitList.RightTwoName
  566. unitList.RightTwoNameEn = extraConfig.UnitList.RightTwoNameEn
  567. }
  568. if chartInfo != nil && chartInfo.MinMaxSave == 1 {
  569. dataListResp.LeftMin = chartInfo.LeftMin
  570. dataListResp.LeftMax = chartInfo.LeftMax
  571. dataListResp.RightMin = chartInfo.RightMin
  572. dataListResp.RightMax = chartInfo.RightMax
  573. dataListResp.Right2Min = chartInfo.Right2Min
  574. dataListResp.Right2Max = chartInfo.Right2Max
  575. } else {
  576. dataListResp.LeftMin = strconv.FormatFloat(LeftMin, 'f', -1, 64)
  577. dataListResp.LeftMax = strconv.FormatFloat(LeftMax, 'f', -1, 64)
  578. dataListResp.RightMin = strconv.FormatFloat(RightMin, 'f', -1, 64)
  579. dataListResp.RightMax = strconv.FormatFloat(RightMax, 'f', -1, 64)
  580. dataListResp.Right2Min = strconv.FormatFloat(Right2Min, 'f', -1, 64)
  581. dataListResp.Right2Max = strconv.FormatFloat(Right2Max, 'f', -1, 64)
  582. }
  583. // 查询引用日期里的指标信息
  584. if len(dateConfEdbIds) > 0 {
  585. dateConfEdbList, e := data_manage.GetEdbInfoByIdList(dateConfEdbIds)
  586. if e != nil {
  587. err = fmt.Errorf("查询引用日期里的指标信息失败,错误信息:%s", e.Error())
  588. return
  589. }
  590. dateConfEdbMap := make(map[int]*data_manage.EdbInfo)
  591. for _, dateConfEdb := range dateConfEdbList {
  592. dateConfEdbMap[dateConfEdb.EdbInfoId] = dateConfEdb
  593. }
  594. for i, dateConf := range extraConfig.DateConfList {
  595. if dateConf.EdbInfoId > 0 {
  596. edbItem, ok := dateConfEdbMap[dateConf.EdbInfoId]
  597. if ok {
  598. extraConfig.DateConfList[i].EdbName = edbItem.EdbName
  599. extraConfig.DateConfList[i].EdbInfoId = edbItem.EdbInfoId
  600. extraConfig.DateConfList[i].EdbInfoType = edbItem.EdbInfoType
  601. extraConfig.DateConfList[i].Frequency = edbItem.Frequency
  602. extraConfig.DateConfList[i].EndDate = edbItem.EndDate
  603. }
  604. }
  605. }
  606. }
  607. dataListResp.SeriesList = extraConfig.SeriesList
  608. dataListResp.DateConfList = extraConfig.DateConfList
  609. dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
  610. dataListResp.UnitList = unitList
  611. dataListResp.IsHeap = extraConfig.IsHeap
  612. dataListResp.SortType = extraConfig.SortType
  613. return
  614. }
  615. // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
  616. func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*data_manage.EdbDataList, dateChange []*data_manage.ChartSectionDateChange, moveForward int) (newDate string, err error) {
  617. if edbInfoId > 0 { //指标日期
  618. newDate = GetEdbDateByMoveForward(moveForward, dataList)
  619. } else {
  620. //系统日期
  621. newDate = time.Now().Format(utils.FormatDate)
  622. }
  623. if newDate != "" && len(dateChange) > 0 {
  624. newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
  625. }
  626. return
  627. }
  628. func GetEdbDateByMoveForward(moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  629. dateList := make([]string, 0)
  630. for _, v := range edbDataList {
  631. dateList = append(dateList, v.DataTime)
  632. }
  633. date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
  634. return
  635. }
  636. func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
  637. // 根据日期进行排序
  638. index := len(dateList) - 1 - moveForward
  639. for k, v := range dateList {
  640. if k == index {
  641. date = v
  642. return
  643. }
  644. }
  645. return
  646. }
  647. // HandleChartSectionSeriesDateChange 处理日期变换
  648. func HandleChartSectionSeriesDateChange(date string, dateChange []*data_manage.ChartSectionDateChange) (newDate string, err error) {
  649. newDate = date
  650. if newDate != "" {
  651. if len(dateChange) > 0 {
  652. var dateTime time.Time
  653. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  654. if err != nil {
  655. err = fmt.Errorf("日期解析失败: %s", err.Error())
  656. return
  657. }
  658. for _, v := range dateChange {
  659. if v.ChangeType == 1 {
  660. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  661. newDate = dateTime.Format(utils.FormatDate)
  662. } else if v.ChangeType == 2 {
  663. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  664. if err != nil {
  665. return
  666. }
  667. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  668. if err != nil {
  669. err = fmt.Errorf("日期解析失败: %s", err.Error())
  670. return
  671. }
  672. }
  673. }
  674. }
  675. }
  676. return
  677. }
  678. // handleSystemAppointDateT
  679. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  680. // @author: Roc
  681. // @datetime2023-10-27 09:31:35
  682. // @param Frequency string
  683. // @param Day string
  684. // @return date string
  685. // @return err error
  686. // @return errMsg string
  687. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  688. //currDate := time.Now()
  689. switch frequency {
  690. case "本周":
  691. day := int(currDate.Weekday())
  692. if day == 0 { // 周日
  693. day = 7
  694. }
  695. num := 0
  696. switch appointDay {
  697. case "周一":
  698. num = 1
  699. case "周二":
  700. num = 2
  701. case "周三":
  702. num = 3
  703. case "周四":
  704. num = 4
  705. case "周五":
  706. num = 5
  707. case "周六":
  708. num = 6
  709. case "周日":
  710. num = 7
  711. }
  712. day = num - day
  713. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  714. case "本旬":
  715. day := currDate.Day()
  716. var tmpDate time.Time
  717. switch appointDay {
  718. case "第一天":
  719. if day <= 10 {
  720. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  721. } else if day <= 20 {
  722. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  723. } else {
  724. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  725. }
  726. case "最后一天":
  727. if day <= 10 {
  728. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  729. } else if day <= 20 {
  730. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  731. } else {
  732. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  733. }
  734. }
  735. date = tmpDate.Format(utils.FormatDate)
  736. case "本月":
  737. var tmpDate time.Time
  738. switch appointDay {
  739. case "第一天":
  740. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  741. case "最后一天":
  742. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  743. }
  744. date = tmpDate.Format(utils.FormatDate)
  745. case "本季":
  746. month := currDate.Month()
  747. var tmpDate time.Time
  748. switch appointDay {
  749. case "第一天":
  750. if month <= 3 {
  751. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  752. } else if month <= 6 {
  753. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  754. } else if month <= 9 {
  755. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  756. } else {
  757. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  758. }
  759. case "最后一天":
  760. if month <= 3 {
  761. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  762. } else if month <= 6 {
  763. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  764. } else if month <= 9 {
  765. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  766. } else {
  767. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  768. }
  769. }
  770. date = tmpDate.Format(utils.FormatDate)
  771. case "本半年":
  772. month := currDate.Month()
  773. var tmpDate time.Time
  774. switch appointDay {
  775. case "第一天":
  776. if month <= 6 {
  777. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  778. } else {
  779. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  780. }
  781. case "最后一天":
  782. if month <= 6 {
  783. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  784. } else {
  785. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  786. }
  787. }
  788. date = tmpDate.Format(utils.FormatDate)
  789. case "本年":
  790. var tmpDate time.Time
  791. switch appointDay {
  792. case "第一天":
  793. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  794. case "最后一天":
  795. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  796. }
  797. date = tmpDate.Format(utils.FormatDate)
  798. default:
  799. errMsg = "错误的日期频度:" + frequency
  800. err = errors.New(errMsg)
  801. return
  802. }
  803. return
  804. }
  805. // sortTripleDataSet 以第一组数据为基准,排序之后,空数组的位置也要同步变更
  806. func SortChartSeriesDataSet(baseName string, baseDataList []float64, baseSeriesNoDataIndexList []int, dataListMap map[string][]float64, noDataListIndexMap map[string][]int, asc int) (newDataListMap map[string][]float64, newNoDataListIndexMap map[string][]int) {
  807. newDataListMap = make(map[string][]float64)
  808. newNoDataListIndexMap = make(map[string][]int)
  809. indices := make([]int, len(baseDataList))
  810. newIndices := make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList))
  811. // 初始化indices
  812. for i := range indices {
  813. indices[i] = i
  814. }
  815. if len(baseSeriesNoDataIndexList) > 0 { //把空值移动到最右边
  816. j := 0
  817. for i := range indices {
  818. isEmpty := false
  819. for _, v := range baseSeriesNoDataIndexList {
  820. if i == v {
  821. isEmpty = true
  822. break
  823. }
  824. }
  825. if isEmpty {
  826. continue
  827. }
  828. newIndices[j] = i
  829. j += 1
  830. }
  831. newIndices = append(newIndices, baseSeriesNoDataIndexList...)
  832. // 根据排序后的indices重新排列所有组的数据
  833. for i, idx := range newIndices {
  834. for k, _ := range dataListMap {
  835. if _, ok := newDataListMap[k]; !ok {
  836. newDataListMap[k] = make([]float64, len(baseDataList))
  837. }
  838. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  839. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  840. }
  841. newDataListMap[k][i] = dataListMap[k][idx]
  842. }
  843. }
  844. dataListMap = newDataListMap
  845. noDataListIndexMap = newNoDataListIndexMap
  846. newDataListMap = make(map[string][]float64)
  847. newNoDataListIndexMap = make(map[string][]int)
  848. baseDataList, _ = dataListMap[baseName]
  849. //先把空的数据移动到最后面
  850. indices = make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList)) //空值不参与排序
  851. newIndices = make([]int, len(baseDataList)) //空值不参与排序
  852. // 初始化indices
  853. for i := range indices {
  854. indices[i] = i
  855. }
  856. }
  857. length := len(indices)
  858. // 根据Group1的数据进行排序,asc决定是升序还是降序
  859. if asc == 1 {
  860. sort.Slice(indices, func(i, j int) bool {
  861. return baseDataList[i] < baseDataList[j]
  862. })
  863. } else {
  864. sort.Slice(indices, func(i, j int) bool {
  865. return baseDataList[i] > baseDataList[j]
  866. })
  867. }
  868. for i := range newIndices {
  869. if i < length {
  870. newIndices[i] = indices[i]
  871. } else {
  872. newIndices[i] = i
  873. }
  874. }
  875. // 根据排序后的indices重新排列所有组的数据
  876. for i, idx := range newIndices {
  877. for k, _ := range dataListMap {
  878. if _, ok := newDataListMap[k]; !ok {
  879. newDataListMap[k] = make([]float64, len(baseDataList))
  880. }
  881. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  882. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  883. }
  884. newDataListMap[k][i] = dataListMap[k][idx]
  885. }
  886. }
  887. return
  888. }