chart_extra_config.go 25 KB

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