chart_extra_config.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. extraConfigStr = 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(chartInfoId int, 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. for _, seriesItem := range extraConfig.SeriesList {
  343. var maxDate time.Time
  344. var minVal, maxVal float64
  345. noDataEdbIdList := make([]int, 0)
  346. dataList := make([]float64, len(seriesItem.EdbInfoList))
  347. for index, edbConf := range seriesItem.EdbInfoList {
  348. edbInfoId := edbConf.EdbInfoId //X轴的指标
  349. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  350. if !ok {
  351. continue
  352. }
  353. seriesItem.EdbInfoList[index].EdbName = edbMappingInfo.EdbName
  354. seriesItem.EdbInfoList[index].EdbNameEn = edbMappingInfo.EdbNameEn
  355. seriesItem.EdbInfoList[index].Unit = edbMappingInfo.Unit
  356. seriesItem.EdbInfoList[index].UnitEn = edbMappingInfo.UnitEn
  357. if index == 0 {
  358. firstUnit = &data_manage.XData{
  359. Name: edbMappingInfo.Unit,
  360. NameEn: edbMappingInfo.UnitEn,
  361. }
  362. }
  363. edbDataList, ok3 := edbDataListMap[edbInfoId]
  364. if !ok3 {
  365. err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
  366. return
  367. }
  368. /*dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  369. if len(dataList) <= 0 {
  370. // 没有数据的指标id
  371. //findDataList = append(findDataList, 0)
  372. continue
  373. }*/
  374. //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
  375. var findDate string
  376. if edbConf.DateConfName == "" {
  377. findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
  378. } else {
  379. // 获取日期配置
  380. dateConfItem, ok1 := DateConfListMap[edbConf.DateConfName]
  381. if !ok1 {
  382. err = fmt.Errorf("引用日期配置不存在")
  383. return
  384. }
  385. // todo 根据日期变换得到最终日期
  386. edbDataListTmp := make([]*data_manage.EdbDataList, 0)
  387. if dateConfItem.EdbInfoId > 0 {
  388. edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
  389. if !ok1 {
  390. err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
  391. return
  392. }
  393. }
  394. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  395. }
  396. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  397. if maxDate.IsZero() {
  398. maxDate = findDateTime
  399. } else {
  400. if findDateTime.After(maxDate) {
  401. maxDate = findDateTime
  402. }
  403. }
  404. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  405. dataList[index] = tmpValue
  406. if index == 0 {
  407. minVal = tmpValue
  408. maxVal = tmpValue
  409. } else {
  410. if tmpValue < minVal {
  411. minVal = tmpValue
  412. }
  413. if tmpValue > maxVal {
  414. maxVal = tmpValue
  415. }
  416. }
  417. } else {
  418. noDataEdbIdList = append(noDataEdbIdList, edbInfoId)
  419. continue
  420. }
  421. }
  422. seriesItem.DataList = dataList
  423. seriesItem.MinData = minVal
  424. seriesItem.MaxData = maxVal
  425. seriesItem.NoDataEdbIdList = noDataEdbIdList
  426. if extraConfig.BaseChartSeriesName == seriesItem.SeriesName {
  427. baseSeries = seriesItem
  428. }
  429. if seriesItem.IsAxis == 1 && leftUnit == nil { //左轴,右轴
  430. leftUnit = firstUnit
  431. } else if seriesItem.IsAxis == 0 && rightUnit == nil {
  432. rightUnit = firstUnit
  433. } else if seriesItem.IsAxis == 2 && right2Unit == nil {
  434. right2Unit = firstUnit
  435. }
  436. }
  437. // 处理横轴
  438. // 遍历基准系列,判断有几个横轴名称
  439. if baseSeries == nil {
  440. err = fmt.Errorf("基准系列不存在")
  441. return
  442. }
  443. xDataList := make([]data_manage.XData, 0)
  444. for index, item := range baseSeries.EdbInfoList {
  445. if index == 0 {
  446. firstUnit = &data_manage.XData{
  447. Name: item.Unit,
  448. NameEn: item.UnitEn,
  449. }
  450. }
  451. tmp := data_manage.XData{
  452. Name: item.EdbName,
  453. NameEn: item.EdbNameEn,
  454. }
  455. // 如果已经设置了横轴名称,则用设置的名称替换
  456. if len(extraConfig.XDataList) > index {
  457. newItem := extraConfig.XDataList[index]
  458. if newItem.Name != "" {
  459. tmp = newItem
  460. }
  461. }
  462. xDataList = append(xDataList, tmp)
  463. }
  464. dataListResp.XDataList = xDataList
  465. // todo 处理纵轴, 如果只存在一个右轴会有问题
  466. unitList := new(data_manage.ChartSectionCombineUnit)
  467. if baseSeries.IsAxis == 1 { //左轴,右轴
  468. leftUnit = firstUnit
  469. } else if baseSeries.IsAxis == 2 {
  470. rightUnit = firstUnit
  471. } else {
  472. right2Unit = firstUnit
  473. }
  474. if unitList.LeftName != "" {
  475. unitList.LeftName = leftUnit.Name
  476. unitList.LeftNameEn = leftUnit.NameEn
  477. }
  478. if unitList.RightName != "" {
  479. unitList.RightName = rightUnit.Name
  480. unitList.RightNameEn = rightUnit.NameEn
  481. }
  482. if unitList.RightTwoName != "" {
  483. unitList.RightTwoName = right2Unit.Name
  484. unitList.RightTwoNameEn = right2Unit.NameEn
  485. }
  486. dataListResp.XDataList = extraConfig.XDataList
  487. dataListResp.SeriesList = extraConfig.SeriesList
  488. dataListResp.DateConfList = extraConfig.DateConfList
  489. dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
  490. dataListResp.UnitList = extraConfig.UnitList
  491. dataListResp.IsHeap = extraConfig.IsHeap
  492. dataListResp.SortType = extraConfig.SortType
  493. return
  494. }
  495. // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
  496. func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*data_manage.EdbDataList, dateChange []*data_manage.ChartSectionDateChange, moveForward int) (newDate string, err error) {
  497. if edbInfoId > 0 { //指标日期
  498. newDate = GetEdbDateByMoveForward(moveForward, dataList)
  499. } else {
  500. //系统日期
  501. newDate = time.Now().Format(utils.FormatDate)
  502. }
  503. if newDate != "" && len(dateChange) > 0 {
  504. newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
  505. }
  506. return
  507. }
  508. func GetEdbDateByMoveForward(moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  509. dateList := make([]string, 0)
  510. for _, v := range edbDataList {
  511. dateList = append(dateList, v.DataTime)
  512. }
  513. date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
  514. return
  515. }
  516. func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
  517. // 根据日期进行排序
  518. index := len(dateList) - 1 - moveForward
  519. for k, v := range dateList {
  520. if k == index {
  521. date = v
  522. return
  523. }
  524. }
  525. return
  526. }
  527. // HandleChartSectionSeriesDateChange 处理日期变换
  528. func HandleChartSectionSeriesDateChange(date string, dateChange []*data_manage.ChartSectionDateChange) (newDate string, err error) {
  529. newDate = date
  530. if newDate != "" {
  531. if len(dateChange) > 0 {
  532. var dateTime time.Time
  533. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  534. if err != nil {
  535. err = fmt.Errorf("日期解析失败: %s", err.Error())
  536. return
  537. }
  538. for _, v := range dateChange {
  539. if v.ChangeType == 1 {
  540. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  541. newDate = dateTime.Format(utils.FormatDate)
  542. } else if v.ChangeType == 2 {
  543. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  544. if err != nil {
  545. return
  546. }
  547. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  548. if err != nil {
  549. err = fmt.Errorf("日期解析失败: %s", err.Error())
  550. return
  551. }
  552. }
  553. }
  554. }
  555. }
  556. return
  557. }
  558. // handleSystemAppointDateT
  559. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  560. // @author: Roc
  561. // @datetime2023-10-27 09:31:35
  562. // @param Frequency string
  563. // @param Day string
  564. // @return date string
  565. // @return err error
  566. // @return errMsg string
  567. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  568. //currDate := time.Now()
  569. switch frequency {
  570. case "本周":
  571. day := int(currDate.Weekday())
  572. if day == 0 { // 周日
  573. day = 7
  574. }
  575. num := 0
  576. switch appointDay {
  577. case "周一":
  578. num = 1
  579. case "周二":
  580. num = 2
  581. case "周三":
  582. num = 3
  583. case "周四":
  584. num = 4
  585. case "周五":
  586. num = 5
  587. case "周六":
  588. num = 6
  589. case "周日":
  590. num = 7
  591. }
  592. day = num - day
  593. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  594. case "本旬":
  595. day := currDate.Day()
  596. var tmpDate time.Time
  597. switch appointDay {
  598. case "第一天":
  599. if day <= 10 {
  600. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  601. } else if day <= 20 {
  602. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  603. } else {
  604. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  605. }
  606. case "最后一天":
  607. if day <= 10 {
  608. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  609. } else if day <= 20 {
  610. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  611. } else {
  612. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  613. }
  614. }
  615. date = tmpDate.Format(utils.FormatDate)
  616. case "本月":
  617. var tmpDate time.Time
  618. switch appointDay {
  619. case "第一天":
  620. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  621. case "最后一天":
  622. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  623. }
  624. date = tmpDate.Format(utils.FormatDate)
  625. case "本季":
  626. month := currDate.Month()
  627. var tmpDate time.Time
  628. switch appointDay {
  629. case "第一天":
  630. if month <= 3 {
  631. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  632. } else if month <= 6 {
  633. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  634. } else if month <= 9 {
  635. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  636. } else {
  637. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  638. }
  639. case "最后一天":
  640. if month <= 3 {
  641. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  642. } else if month <= 6 {
  643. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  644. } else if month <= 9 {
  645. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  646. } else {
  647. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  648. }
  649. }
  650. date = tmpDate.Format(utils.FormatDate)
  651. case "本半年":
  652. month := currDate.Month()
  653. var tmpDate time.Time
  654. switch appointDay {
  655. case "第一天":
  656. if month <= 6 {
  657. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  658. } else {
  659. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  660. }
  661. case "最后一天":
  662. if month <= 6 {
  663. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  664. } else {
  665. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  666. }
  667. }
  668. date = tmpDate.Format(utils.FormatDate)
  669. case "本年":
  670. var tmpDate time.Time
  671. switch appointDay {
  672. case "第一天":
  673. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  674. case "最后一天":
  675. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  676. }
  677. date = tmpDate.Format(utils.FormatDate)
  678. default:
  679. errMsg = "错误的日期频度:" + frequency
  680. err = errors.New(errMsg)
  681. return
  682. }
  683. return
  684. }