chart_extra_config.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. for k, v := range extraConfig.DateConfList {
  232. if v.DateConfNameEn == "" {
  233. extraConfig.DateConfList[k].DateConfNameEn = v.DateConfName
  234. }
  235. }
  236. if extraConfig.UnitList.LeftNameEn == "" {
  237. extraConfig.UnitList.LeftNameEn = extraConfig.UnitList.LeftName
  238. }
  239. if extraConfig.UnitList.RightNameEn == "" {
  240. extraConfig.UnitList.RightNameEn = extraConfig.UnitList.RightName
  241. }
  242. if extraConfig.UnitList.RightTwoNameEn == "" {
  243. extraConfig.UnitList.RightTwoNameEn = extraConfig.UnitList.RightTwoName
  244. }
  245. for k, v := range extraConfig.XDataList {
  246. if v.NameEn == "" {
  247. extraConfig.XDataList[k].NameEn = v.Name
  248. }
  249. }
  250. for k, v := range extraConfig.SeriesList {
  251. if v.SeriesNameEn == `` {
  252. extraConfig.SeriesList[k].SeriesNameEn = v.SeriesName
  253. }
  254. }
  255. newExtraConfig = extraConfig
  256. return
  257. }
  258. // GetChartSectionCombineData 截面组合图的数据处理
  259. 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) {
  260. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  261. edbDataMap := make(map[int]map[string]float64)
  262. for edbInfoId, edbDataList := range edbDataListMap {
  263. edbDateData := make(map[string]float64)
  264. for _, edbData := range edbDataList {
  265. edbDateData[edbData.DataTime] = edbData.Value
  266. }
  267. edbDataMap[edbInfoId] = edbDateData
  268. }
  269. // edbIdList 指标展示顺序;x轴的指标顺序
  270. edbIdList = make([]int, 0)
  271. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  272. for _, v := range mappingList {
  273. edbIdList = append(edbIdList, v.EdbInfoId)
  274. edbMappingMap[v.EdbInfoId] = v
  275. }
  276. // 确定好截面散点图返回的数据格式
  277. // 获取所有的引用日期设置
  278. dateConfListMap := make(map[string]*data_manage.ChartSectionDateConfItem)
  279. dateConfEdbIds := make([]int, 0)
  280. for _, v := range extraConfig.DateConfList {
  281. if v.EdbInfoId > 0 {
  282. dateConfEdbIds = append(dateConfEdbIds, v.EdbInfoId)
  283. }
  284. dateConfListMap[v.DateConfName] = v
  285. }
  286. // 遍历每个系列
  287. // 遍历每个指标,根据选中的日期,进行日期变换得到最终的日期,根据最终的日期获取对应的值
  288. // 组装数据
  289. baseSeries := new(data_manage.ChartSectionSeriesItem) //y轴的系列
  290. var firstUnit, leftUnit, rightUnit, right2Unit *data_manage.XData
  291. var (
  292. LeftMin float64
  293. LeftMax float64
  294. RightMin float64
  295. RightMax float64
  296. Right2Min float64
  297. Right2Max float64
  298. )
  299. seriesDataListMap := make(map[string][]float64)
  300. seriesNoDataIndexMap := make(map[string][]int)
  301. for _, seriesItem := range extraConfig.SeriesList {
  302. var maxDate time.Time
  303. var minVal, maxVal float64
  304. noDataEdbIndex := make([]int, 0)
  305. dataList := make([]float64, len(seriesItem.EdbInfoList))
  306. for index, edbConf := range seriesItem.EdbInfoList {
  307. edbInfoId := edbConf.EdbInfoId //X轴的指标
  308. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  309. if !ok {
  310. continue
  311. }
  312. seriesItem.EdbInfoList[index].EdbName = edbMappingInfo.EdbName
  313. seriesItem.EdbInfoList[index].EdbNameEn = edbMappingInfo.EdbNameEn
  314. seriesItem.EdbInfoList[index].EdbInfoType = edbMappingInfo.EdbInfoCategoryType
  315. seriesItem.EdbInfoList[index].Unit = edbMappingInfo.Unit
  316. seriesItem.EdbInfoList[index].UnitEn = edbMappingInfo.UnitEn
  317. if index == 0 {
  318. firstUnit = &data_manage.XData{
  319. Name: edbMappingInfo.Unit,
  320. NameEn: edbMappingInfo.UnitEn,
  321. }
  322. }
  323. edbDataList, ok3 := edbDataListMap[edbInfoId]
  324. if !ok3 {
  325. err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
  326. return
  327. }
  328. //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
  329. var findDate string
  330. if edbConf.DateConfType == 0 {
  331. if edbInfoId == 0 {
  332. err = fmt.Errorf("请选择指标")
  333. return
  334. }
  335. findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
  336. if err != nil {
  337. err = fmt.Errorf("指标%d的日期变换处理失败", edbInfoId)
  338. return
  339. }
  340. } else {
  341. // 获取日期配置
  342. dateConfItem, ok1 := dateConfListMap[edbConf.DateConfName]
  343. if !ok1 {
  344. err = fmt.Errorf("引用日期配置不存在")
  345. return
  346. }
  347. // todo 根据日期变换得到最终日期
  348. edbDataListTmp := make([]*data_manage.EdbDataList, 0)
  349. if dateConfItem.EdbInfoId > 0 {
  350. edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
  351. if !ok1 {
  352. err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
  353. return
  354. }
  355. }
  356. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  357. if err != nil {
  358. err = fmt.Errorf("指标%d的日期变换处理失败", dateConfItem.EdbInfoId)
  359. return
  360. }
  361. }
  362. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  363. if maxDate.IsZero() {
  364. maxDate = findDateTime
  365. } else {
  366. if findDateTime.After(maxDate) {
  367. maxDate = findDateTime
  368. }
  369. }
  370. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  371. dataList[index] = tmpValue
  372. if index == 0 {
  373. minVal = tmpValue
  374. maxVal = tmpValue
  375. } else {
  376. if tmpValue < minVal {
  377. minVal = tmpValue
  378. }
  379. if tmpValue > maxVal {
  380. maxVal = tmpValue
  381. }
  382. }
  383. } else {
  384. dataList[index] = 0
  385. noDataEdbIndex = append(noDataEdbIndex, index)
  386. continue
  387. }
  388. }
  389. seriesDataListMap[seriesItem.SeriesName] = dataList
  390. seriesNoDataIndexMap[seriesItem.SeriesName] = noDataEdbIndex
  391. seriesItem.DataList = dataList
  392. seriesItem.MinData = minVal
  393. seriesItem.MaxData = maxVal
  394. seriesItem.NoDataEdbIndex = noDataEdbIndex
  395. if extraConfig.BaseChartSeriesName == seriesItem.SeriesName {
  396. baseSeries = seriesItem
  397. }
  398. if seriesItem.IsAxis == 1 && leftUnit == nil { //左轴,右轴
  399. leftUnit = firstUnit
  400. } else if seriesItem.IsAxis == 0 && rightUnit == nil {
  401. rightUnit = firstUnit
  402. } else if seriesItem.IsAxis == 2 && right2Unit == nil {
  403. right2Unit = firstUnit
  404. }
  405. //处理上下限
  406. var minData, maxData float64
  407. for _, d := range seriesItem.DataList {
  408. if minData > d {
  409. minData = d
  410. }
  411. if maxData < d {
  412. maxData = d
  413. }
  414. }
  415. if seriesItem.IsAxis == 1 {
  416. if LeftMin > minData {
  417. LeftMin = minData
  418. }
  419. if LeftMax < maxData {
  420. LeftMax = maxData
  421. }
  422. } else if seriesItem.IsAxis == 0 {
  423. if RightMin > minData {
  424. RightMin = minData
  425. }
  426. if RightMax < maxData {
  427. RightMax = maxData
  428. }
  429. } else {
  430. if Right2Min > minData {
  431. Right2Min = minData
  432. }
  433. if Right2Max < maxData {
  434. Right2Max = maxData
  435. }
  436. }
  437. }
  438. // 处理横轴
  439. // 遍历基准系列,判断有几个横轴名称
  440. if baseSeries == nil {
  441. err = fmt.Errorf("基准系列不存在")
  442. return
  443. }
  444. // 处理系列排序
  445. if extraConfig.SortType > 0 {
  446. newSeriesDataListMap, newSeriesNoDataIndexMap := SortChartSeriesDataSet(baseSeries.SeriesName, baseSeries.DataList, baseSeries.NoDataEdbIndex, seriesDataListMap, seriesNoDataIndexMap, extraConfig.SortType)
  447. for k, item := range extraConfig.SeriesList {
  448. dataList, ok := newSeriesDataListMap[item.SeriesName]
  449. if ok {
  450. extraConfig.SeriesList[k].DataList = dataList
  451. }
  452. noIndex, ok := newSeriesNoDataIndexMap[item.SeriesName]
  453. if ok {
  454. extraConfig.SeriesList[k].NoDataEdbIndex = noIndex
  455. }
  456. }
  457. }
  458. xDataList := make([]data_manage.XData, 0)
  459. for index, item := range baseSeries.EdbInfoList {
  460. if index == 0 {
  461. firstUnit = &data_manage.XData{
  462. Name: item.Unit,
  463. NameEn: item.UnitEn,
  464. }
  465. }
  466. tmp := data_manage.XData{
  467. Name: item.EdbName,
  468. NameEn: item.EdbNameEn,
  469. }
  470. // 如果已经设置了横轴名称,则用设置的名称替换
  471. if len(extraConfig.XDataList) > index {
  472. newItem := extraConfig.XDataList[index]
  473. if newItem.Name != "" {
  474. tmp = newItem
  475. }
  476. }
  477. xDataList = append(xDataList, tmp)
  478. }
  479. dataListResp.XDataList = xDataList
  480. unitList := new(data_manage.ChartSectionCombineUnit)
  481. if baseSeries.IsAxis == 1 { //左轴,右轴
  482. leftUnit = firstUnit
  483. } else if baseSeries.IsAxis == 2 {
  484. rightUnit = firstUnit
  485. } else {
  486. right2Unit = firstUnit
  487. }
  488. if leftUnit != nil {
  489. unitList.LeftName = leftUnit.Name
  490. unitList.LeftNameEn = leftUnit.NameEn
  491. }
  492. if rightUnit != nil {
  493. unitList.RightName = rightUnit.Name
  494. unitList.RightNameEn = rightUnit.NameEn
  495. }
  496. if right2Unit != nil {
  497. unitList.RightTwoName = right2Unit.Name
  498. unitList.RightTwoNameEn = right2Unit.NameEn
  499. }
  500. if extraConfig.UnitList.LeftName != "" {
  501. unitList.LeftName = extraConfig.UnitList.LeftName
  502. unitList.LeftNameEn = extraConfig.UnitList.LeftNameEn
  503. }
  504. if extraConfig.UnitList.RightName != "" {
  505. unitList.RightName = extraConfig.UnitList.RightName
  506. unitList.RightNameEn = extraConfig.UnitList.RightNameEn
  507. }
  508. if extraConfig.UnitList.RightTwoName != "" {
  509. unitList.RightTwoName = extraConfig.UnitList.RightTwoName
  510. unitList.RightTwoNameEn = extraConfig.UnitList.RightTwoNameEn
  511. }
  512. if chartInfo != nil && chartInfo.MinMaxSave == 1 {
  513. dataListResp.LeftMin = chartInfo.LeftMin
  514. dataListResp.LeftMax = chartInfo.LeftMax
  515. dataListResp.RightMin = chartInfo.RightMin
  516. dataListResp.RightMax = chartInfo.RightMax
  517. dataListResp.Right2Min = chartInfo.Right2Min
  518. dataListResp.Right2Max = chartInfo.Right2Max
  519. } else {
  520. dataListResp.LeftMin = strconv.FormatFloat(LeftMin, 'f', -1, 64)
  521. dataListResp.LeftMax = strconv.FormatFloat(LeftMax, 'f', -1, 64)
  522. dataListResp.RightMin = strconv.FormatFloat(RightMin, 'f', -1, 64)
  523. dataListResp.RightMax = strconv.FormatFloat(RightMax, 'f', -1, 64)
  524. dataListResp.Right2Min = strconv.FormatFloat(Right2Min, 'f', -1, 64)
  525. dataListResp.Right2Max = strconv.FormatFloat(Right2Max, 'f', -1, 64)
  526. }
  527. // 查询引用日期里的指标信息
  528. if len(dateConfEdbIds) > 0 {
  529. dateConfEdbList, e := data_manage.GetEdbInfoByIdList(dateConfEdbIds)
  530. if e != nil {
  531. err = fmt.Errorf("查询引用日期里的指标信息失败,错误信息:%s", e.Error())
  532. return
  533. }
  534. dateConfEdbMap := make(map[int]*data_manage.EdbInfo)
  535. for _, dateConfEdb := range dateConfEdbList {
  536. dateConfEdbMap[dateConfEdb.EdbInfoId] = dateConfEdb
  537. }
  538. for i, dateConf := range extraConfig.DateConfList {
  539. if dateConf.EdbInfoId > 0 {
  540. edbItem, ok := dateConfEdbMap[dateConf.EdbInfoId]
  541. if ok {
  542. extraConfig.DateConfList[i].EdbName = edbItem.EdbName
  543. extraConfig.DateConfList[i].EdbInfoId = edbItem.EdbInfoId
  544. extraConfig.DateConfList[i].EdbInfoType = edbItem.EdbInfoType
  545. extraConfig.DateConfList[i].Frequency = edbItem.Frequency
  546. extraConfig.DateConfList[i].EndDate = edbItem.EndDate
  547. }
  548. }
  549. }
  550. }
  551. dataListResp.SeriesList = extraConfig.SeriesList
  552. dataListResp.DateConfList = extraConfig.DateConfList
  553. dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
  554. dataListResp.UnitList = unitList
  555. dataListResp.IsHeap = extraConfig.IsHeap
  556. dataListResp.SortType = extraConfig.SortType
  557. return
  558. }
  559. // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
  560. func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*data_manage.EdbDataList, dateChange []*data_manage.ChartSectionDateChange, moveForward int) (newDate string, err error) {
  561. if edbInfoId > 0 { //指标日期
  562. newDate = GetEdbDateByMoveForward(moveForward, dataList)
  563. } else {
  564. //系统日期
  565. newDate = time.Now().Format(utils.FormatDate)
  566. }
  567. if newDate != "" && len(dateChange) > 0 {
  568. newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
  569. }
  570. return
  571. }
  572. func GetEdbDateByMoveForward(moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  573. dateList := make([]string, 0)
  574. for _, v := range edbDataList {
  575. dateList = append(dateList, v.DataTime)
  576. }
  577. date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
  578. return
  579. }
  580. func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
  581. // 根据日期进行排序
  582. index := len(dateList) - 1 - moveForward
  583. for k, v := range dateList {
  584. if k == index {
  585. date = v
  586. return
  587. }
  588. }
  589. return
  590. }
  591. // HandleChartSectionSeriesDateChange 处理日期变换
  592. func HandleChartSectionSeriesDateChange(date string, dateChange []*data_manage.ChartSectionDateChange) (newDate string, err error) {
  593. newDate = date
  594. if newDate != "" {
  595. if len(dateChange) > 0 {
  596. var dateTime time.Time
  597. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  598. if err != nil {
  599. err = fmt.Errorf("日期解析失败: %s", err.Error())
  600. return
  601. }
  602. for _, v := range dateChange {
  603. if v.ChangeType == 1 {
  604. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  605. newDate = dateTime.Format(utils.FormatDate)
  606. } else if v.ChangeType == 2 {
  607. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  608. if err != nil {
  609. return
  610. }
  611. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  612. if err != nil {
  613. err = fmt.Errorf("日期解析失败: %s", err.Error())
  614. return
  615. }
  616. }
  617. }
  618. }
  619. }
  620. return
  621. }
  622. // handleSystemAppointDateT
  623. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  624. // @author: Roc
  625. // @datetime2023-10-27 09:31:35
  626. // @param Frequency string
  627. // @param Day string
  628. // @return date string
  629. // @return err error
  630. // @return errMsg string
  631. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  632. //currDate := time.Now()
  633. switch frequency {
  634. case "本周":
  635. day := int(currDate.Weekday())
  636. if day == 0 { // 周日
  637. day = 7
  638. }
  639. num := 0
  640. switch appointDay {
  641. case "周一":
  642. num = 1
  643. case "周二":
  644. num = 2
  645. case "周三":
  646. num = 3
  647. case "周四":
  648. num = 4
  649. case "周五":
  650. num = 5
  651. case "周六":
  652. num = 6
  653. case "周日":
  654. num = 7
  655. }
  656. day = num - day
  657. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  658. case "本旬":
  659. day := currDate.Day()
  660. var tmpDate time.Time
  661. switch appointDay {
  662. case "第一天":
  663. if day <= 10 {
  664. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  665. } else if day <= 20 {
  666. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  667. } else {
  668. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  669. }
  670. case "最后一天":
  671. if day <= 10 {
  672. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  673. } else if day <= 20 {
  674. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  675. } else {
  676. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  677. }
  678. }
  679. date = tmpDate.Format(utils.FormatDate)
  680. case "本月":
  681. var tmpDate time.Time
  682. switch appointDay {
  683. case "第一天":
  684. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  685. case "最后一天":
  686. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  687. }
  688. date = tmpDate.Format(utils.FormatDate)
  689. case "本季":
  690. month := currDate.Month()
  691. var tmpDate time.Time
  692. switch appointDay {
  693. case "第一天":
  694. if month <= 3 {
  695. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  696. } else if month <= 6 {
  697. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  698. } else if month <= 9 {
  699. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  700. } else {
  701. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  702. }
  703. case "最后一天":
  704. if month <= 3 {
  705. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  706. } else if month <= 6 {
  707. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  708. } else if month <= 9 {
  709. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  710. } else {
  711. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  712. }
  713. }
  714. date = tmpDate.Format(utils.FormatDate)
  715. case "本半年":
  716. month := currDate.Month()
  717. var tmpDate time.Time
  718. switch appointDay {
  719. case "第一天":
  720. if month <= 6 {
  721. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  722. } else {
  723. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  724. }
  725. case "最后一天":
  726. if month <= 6 {
  727. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  728. } else {
  729. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  730. }
  731. }
  732. date = tmpDate.Format(utils.FormatDate)
  733. case "本年":
  734. var tmpDate time.Time
  735. switch appointDay {
  736. case "第一天":
  737. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  738. case "最后一天":
  739. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  740. }
  741. date = tmpDate.Format(utils.FormatDate)
  742. default:
  743. errMsg = "错误的日期频度:" + frequency
  744. err = errors.New(errMsg)
  745. return
  746. }
  747. return
  748. }
  749. // sortTripleDataSet 以第一组数据为基准,排序之后,空数组的位置也要同步变更
  750. 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) {
  751. newDataListMap = make(map[string][]float64)
  752. newNoDataListIndexMap = make(map[string][]int)
  753. indices := make([]int, len(baseDataList))
  754. newIndices := make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList))
  755. // 初始化indices
  756. for i := range indices {
  757. indices[i] = i
  758. }
  759. if len(baseSeriesNoDataIndexList) > 0 { //把空值移动到最右边
  760. j := 0
  761. for i := range indices {
  762. isEmpty := false
  763. for _, v := range baseSeriesNoDataIndexList {
  764. if i == v {
  765. isEmpty = true
  766. break
  767. }
  768. }
  769. if isEmpty {
  770. continue
  771. }
  772. newIndices[j] = i
  773. j += 1
  774. }
  775. newIndices = append(newIndices, baseSeriesNoDataIndexList...)
  776. // 根据排序后的indices重新排列所有组的数据
  777. for i, idx := range newIndices {
  778. for k, _ := range dataListMap {
  779. if _, ok := newDataListMap[k]; !ok {
  780. newDataListMap[k] = make([]float64, len(baseDataList))
  781. }
  782. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  783. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  784. }
  785. newDataListMap[k][i] = dataListMap[k][idx]
  786. }
  787. }
  788. dataListMap = newDataListMap
  789. noDataListIndexMap = newNoDataListIndexMap
  790. newDataListMap = make(map[string][]float64)
  791. newNoDataListIndexMap = make(map[string][]int)
  792. baseDataList, _ = dataListMap[baseName]
  793. //先把空的数据移动到最后面
  794. indices = make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList)) //空值不参与排序
  795. newIndices = make([]int, len(baseDataList)) //空值不参与排序
  796. // 初始化indices
  797. for i := range indices {
  798. indices[i] = i
  799. }
  800. }
  801. length := len(indices)
  802. baseDataSortList := make([]data_manage.ChartSectionSeriesValSort, length)
  803. for i, value := range baseDataList {
  804. if i < length {
  805. baseDataSortList[i] = data_manage.ChartSectionSeriesValSort{Index: i, Value: value}
  806. }
  807. }
  808. if asc == 1 {
  809. // 使用sort.Sort进行排序
  810. sort.Sort(data_manage.ChartSectionSeriesValSortAsc(baseDataSortList))
  811. } else {
  812. sort.Sort(data_manage.ChartSectionSeriesValSortDesc(baseDataSortList))
  813. }
  814. for k, v := range baseDataSortList {
  815. indices[k] = v.Index
  816. }
  817. for i := range newIndices {
  818. if i < length {
  819. newIndices[i] = indices[i]
  820. } else {
  821. newIndices[i] = i
  822. }
  823. }
  824. // 根据排序后的indices重新排列所有组的数据
  825. for i, idx := range newIndices {
  826. for k, _ := range dataListMap {
  827. if _, ok := newDataListMap[k]; !ok {
  828. newDataListMap[k] = make([]float64, len(baseDataList))
  829. }
  830. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  831. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  832. }
  833. newDataListMap[k][i] = dataListMap[k][idx]
  834. }
  835. }
  836. return
  837. }