chart_extra_config.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_forum_admin/models"
  5. "eta/eta_forum_admin/utils"
  6. "fmt"
  7. "sort"
  8. "strconv"
  9. "time"
  10. )
  11. // GetChartSectionCombineData 截面组合图的数据处理
  12. func GetChartSectionCombineData(chartInfo *models.ChartInfo, mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig models.ChartSectionAllExtraConf) (edbIdList []int, dataListResp models.ChartSectionCombineDataResp, err error) {
  13. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  14. edbDataMap := make(map[int]map[string]float64)
  15. for edbInfoId, edbDataList := range edbDataListMap {
  16. edbDateData := make(map[string]float64)
  17. for _, edbData := range edbDataList {
  18. edbDateData[edbData.DataTime] = edbData.Value
  19. }
  20. edbDataMap[edbInfoId] = edbDateData
  21. }
  22. // edbIdList 指标展示顺序;x轴的指标顺序
  23. edbIdList = make([]int, 0)
  24. edbMappingMap := make(map[int]*models.ChartEdbInfoMapping)
  25. for _, v := range mappingList {
  26. edbIdList = append(edbIdList, v.EdbInfoId)
  27. edbMappingMap[v.EdbInfoId] = v
  28. }
  29. // 确定好截面散点图返回的数据格式
  30. // 获取所有的引用日期设置
  31. dateConfListMap := make(map[string]*models.ChartSectionDateConfItem)
  32. dateConfEdbIds := make([]int, 0)
  33. for _, v := range extraConfig.DateConfList {
  34. if v.EdbInfoId > 0 {
  35. dateConfEdbIds = append(dateConfEdbIds, v.EdbInfoId)
  36. }
  37. dateConfListMap[v.DateConfName] = v
  38. }
  39. // 遍历每个系列
  40. // 遍历每个指标,根据选中的日期,进行日期变换得到最终的日期,根据最终的日期获取对应的值
  41. // 组装数据
  42. baseSeries := new(models.ChartSectionSeriesItem) //y轴的系列
  43. var firstUnit, leftUnit, rightUnit, right2Unit *models.XData
  44. var (
  45. LeftMin float64
  46. LeftMax float64
  47. RightMin float64
  48. RightMax float64
  49. Right2Min float64
  50. Right2Max float64
  51. )
  52. seriesDataListMap := make(map[string][]float64)
  53. seriesNoDataIndexMap := make(map[string][]int)
  54. for _, seriesItem := range extraConfig.SeriesList {
  55. var maxDate time.Time
  56. var minVal, maxVal float64
  57. noDataEdbIndex := make([]int, 0)
  58. dataList := make([]float64, len(seriesItem.EdbInfoList))
  59. for index, edbConf := range seriesItem.EdbInfoList {
  60. edbInfoId := edbConf.EdbInfoId //X轴的指标
  61. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  62. if !ok {
  63. continue
  64. }
  65. seriesItem.EdbInfoList[index].EdbName = edbMappingInfo.EdbName
  66. seriesItem.EdbInfoList[index].EdbNameEn = edbMappingInfo.EdbNameEn
  67. seriesItem.EdbInfoList[index].EdbInfoType = edbMappingInfo.EdbInfoCategoryType
  68. seriesItem.EdbInfoList[index].Unit = edbMappingInfo.Unit
  69. seriesItem.EdbInfoList[index].UnitEn = edbMappingInfo.UnitEn
  70. if index == 0 {
  71. firstUnit = &models.XData{
  72. Name: edbMappingInfo.Unit,
  73. NameEn: edbMappingInfo.UnitEn,
  74. }
  75. }
  76. edbDataList, ok3 := edbDataListMap[edbInfoId]
  77. if !ok3 {
  78. err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
  79. return
  80. }
  81. //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
  82. var findDate string
  83. if edbConf.DateConfType == 0 {
  84. if edbInfoId == 0 {
  85. err = fmt.Errorf("请选择指标")
  86. return
  87. }
  88. findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
  89. if err != nil {
  90. err = fmt.Errorf("指标%d的日期变换处理失败", edbInfoId)
  91. return
  92. }
  93. } else {
  94. // 获取日期配置
  95. dateConfItem, ok1 := dateConfListMap[edbConf.DateConfName]
  96. if !ok1 {
  97. err = fmt.Errorf("引用日期配置不存在")
  98. return
  99. }
  100. // todo 根据日期变换得到最终日期
  101. edbDataListTmp := make([]*models.EdbDataList, 0)
  102. if dateConfItem.DateType == 0 {
  103. if dateConfItem.EdbInfoId > 0 {
  104. edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
  105. if !ok1 {
  106. err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
  107. return
  108. }
  109. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  110. if err != nil {
  111. err = fmt.Errorf("指标%d的日期变换处理失败", dateConfItem.EdbInfoId)
  112. return
  113. }
  114. } else {
  115. err = fmt.Errorf("请选择指标")
  116. return
  117. }
  118. } else if dateConfItem.DateType == 1 {
  119. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  120. if err != nil {
  121. err = fmt.Errorf("指标%d的日期变换处理失败", dateConfItem.EdbInfoId)
  122. return
  123. }
  124. } else if dateConfItem.DateType == 2 {
  125. if dateConfItem.StaticDate == "" {
  126. err = fmt.Errorf("请输入固定日期")
  127. return
  128. }
  129. findDate = dateConfItem.StaticDate
  130. }
  131. }
  132. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  133. if maxDate.IsZero() {
  134. maxDate = findDateTime
  135. } else {
  136. if findDateTime.After(maxDate) {
  137. maxDate = findDateTime
  138. }
  139. }
  140. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  141. dataList[index] = tmpValue
  142. if index == 0 {
  143. minVal = tmpValue
  144. maxVal = tmpValue
  145. } else {
  146. if tmpValue < minVal {
  147. minVal = tmpValue
  148. }
  149. if tmpValue > maxVal {
  150. maxVal = tmpValue
  151. }
  152. }
  153. } else {
  154. dataList[index] = 0
  155. noDataEdbIndex = append(noDataEdbIndex, index)
  156. continue
  157. }
  158. }
  159. seriesDataListMap[seriesItem.SeriesName] = dataList
  160. seriesNoDataIndexMap[seriesItem.SeriesName] = noDataEdbIndex
  161. seriesItem.DataList = dataList
  162. seriesItem.MinData = minVal
  163. seriesItem.MaxData = maxVal
  164. seriesItem.NoDataEdbIndex = noDataEdbIndex
  165. if extraConfig.BaseChartSeriesName == seriesItem.SeriesName {
  166. baseSeries = seriesItem
  167. }
  168. if seriesItem.IsAxis == 1 && leftUnit == nil { //左轴,右轴
  169. leftUnit = firstUnit
  170. } else if seriesItem.IsAxis == 0 && rightUnit == nil {
  171. rightUnit = firstUnit
  172. } else if seriesItem.IsAxis == 2 && right2Unit == nil {
  173. right2Unit = firstUnit
  174. }
  175. //处理上下限
  176. var minData, maxData float64
  177. for _, d := range seriesItem.DataList {
  178. if minData > d {
  179. minData = d
  180. }
  181. if maxData < d {
  182. maxData = d
  183. }
  184. }
  185. if seriesItem.IsAxis == 1 {
  186. if LeftMin > minData {
  187. LeftMin = minData
  188. }
  189. if LeftMax < maxData {
  190. LeftMax = maxData
  191. }
  192. } else if seriesItem.IsAxis == 0 {
  193. if RightMin > minData {
  194. RightMin = minData
  195. }
  196. if RightMax < maxData {
  197. RightMax = maxData
  198. }
  199. } else {
  200. if Right2Min > minData {
  201. Right2Min = minData
  202. }
  203. if Right2Max < maxData {
  204. Right2Max = maxData
  205. }
  206. }
  207. }
  208. // 处理横轴
  209. // 遍历基准系列,判断有几个横轴名称
  210. if baseSeries == nil {
  211. err = fmt.Errorf("基准系列不存在")
  212. return
  213. }
  214. defaultIndexXDataList := make([]int, 0) //默认排序时的横轴
  215. defaultXDataMap := make(map[int]models.XData) //默认排序时的横轴单位
  216. for index, item := range baseSeries.EdbInfoList {
  217. if index == 0 {
  218. firstUnit = &models.XData{
  219. Name: item.Unit,
  220. NameEn: item.UnitEn,
  221. }
  222. }
  223. tmp := models.XData{
  224. Name: item.EdbName,
  225. NameEn: item.EdbNameEn,
  226. }
  227. defaultXDataMap[index] = tmp
  228. defaultIndexXDataList = append(defaultIndexXDataList, index)
  229. }
  230. // 处理系列排序
  231. if extraConfig.SortType > 0 {
  232. newSeriesDataListMap, newSeriesNoDataIndexMap, newIndexXDataList := SortChartSeriesDataSet(baseSeries.SeriesName, baseSeries.DataList, baseSeries.NoDataEdbIndex, seriesDataListMap, seriesNoDataIndexMap, extraConfig.SortType)
  233. for k, item := range extraConfig.SeriesList {
  234. dataList, ok := newSeriesDataListMap[item.SeriesName]
  235. if ok {
  236. extraConfig.SeriesList[k].DataList = dataList
  237. }
  238. noIndex, ok := newSeriesNoDataIndexMap[item.SeriesName]
  239. if ok {
  240. extraConfig.SeriesList[k].NoDataEdbIndex = noIndex
  241. }
  242. }
  243. defaultIndexXDataList = newIndexXDataList
  244. }
  245. xDataList := make([]models.XData, 0)
  246. for index, itemIndex := range defaultIndexXDataList {
  247. nameItem, ok := defaultXDataMap[itemIndex]
  248. if !ok {
  249. err = fmt.Errorf("单位不存在")
  250. return
  251. }
  252. // 如果已经设置了横轴名称,则用设置的名称替换
  253. if len(extraConfig.XDataList) > index {
  254. newItem := extraConfig.XDataList[index]
  255. if newItem.Name != "" {
  256. nameItem = newItem
  257. }
  258. }
  259. xDataList = append(xDataList, nameItem)
  260. }
  261. dataListResp.XDataList = xDataList
  262. unitList := new(models.ChartSectionCombineUnit)
  263. if baseSeries.IsAxis == 1 { //左轴,右轴
  264. leftUnit = firstUnit
  265. } else if baseSeries.IsAxis == 2 {
  266. rightUnit = firstUnit
  267. } else {
  268. right2Unit = firstUnit
  269. }
  270. if leftUnit != nil {
  271. unitList.LeftName = leftUnit.Name
  272. unitList.LeftNameEn = leftUnit.NameEn
  273. }
  274. if rightUnit != nil {
  275. unitList.RightName = rightUnit.Name
  276. unitList.RightNameEn = rightUnit.NameEn
  277. }
  278. if right2Unit != nil {
  279. unitList.RightTwoName = right2Unit.Name
  280. unitList.RightTwoNameEn = right2Unit.NameEn
  281. }
  282. if extraConfig.UnitList.LeftName != "" {
  283. unitList.LeftName = extraConfig.UnitList.LeftName
  284. unitList.LeftNameEn = extraConfig.UnitList.LeftNameEn
  285. }
  286. if extraConfig.UnitList.RightName != "" {
  287. unitList.RightName = extraConfig.UnitList.RightName
  288. unitList.RightNameEn = extraConfig.UnitList.RightNameEn
  289. }
  290. if extraConfig.UnitList.RightTwoName != "" {
  291. unitList.RightTwoName = extraConfig.UnitList.RightTwoName
  292. unitList.RightTwoNameEn = extraConfig.UnitList.RightTwoNameEn
  293. }
  294. if chartInfo != nil && chartInfo.MinMaxSave == 1 {
  295. dataListResp.LeftMin = chartInfo.LeftMin
  296. dataListResp.LeftMax = chartInfo.LeftMax
  297. dataListResp.RightMin = chartInfo.RightMin
  298. dataListResp.RightMax = chartInfo.RightMax
  299. dataListResp.Right2Min = chartInfo.Right2Min
  300. dataListResp.Right2Max = chartInfo.Right2Max
  301. } else {
  302. dataListResp.LeftMin = strconv.FormatFloat(LeftMin, 'f', -1, 64)
  303. dataListResp.LeftMax = strconv.FormatFloat(LeftMax, 'f', -1, 64)
  304. dataListResp.RightMin = strconv.FormatFloat(RightMin, 'f', -1, 64)
  305. dataListResp.RightMax = strconv.FormatFloat(RightMax, 'f', -1, 64)
  306. dataListResp.Right2Min = strconv.FormatFloat(Right2Min, 'f', -1, 64)
  307. dataListResp.Right2Max = strconv.FormatFloat(Right2Max, 'f', -1, 64)
  308. }
  309. // 查询引用日期里的指标信息
  310. if len(dateConfEdbIds) > 0 {
  311. dateConfEdbList, e := models.GetEdbInfoByIdList(dateConfEdbIds)
  312. if e != nil {
  313. err = fmt.Errorf("查询引用日期里的指标信息失败,错误信息:%s", e.Error())
  314. return
  315. }
  316. dateConfEdbMap := make(map[int]*models.EdbInfo)
  317. for _, dateConfEdb := range dateConfEdbList {
  318. dateConfEdbMap[dateConfEdb.EdbInfoId] = dateConfEdb
  319. }
  320. for i, dateConf := range extraConfig.DateConfList {
  321. if dateConf.EdbInfoId > 0 {
  322. edbItem, ok := dateConfEdbMap[dateConf.EdbInfoId]
  323. if ok {
  324. extraConfig.DateConfList[i].EdbName = edbItem.EdbName
  325. extraConfig.DateConfList[i].EdbInfoId = edbItem.EdbInfoId
  326. extraConfig.DateConfList[i].EdbInfoType = edbItem.EdbInfoType
  327. extraConfig.DateConfList[i].Frequency = edbItem.Frequency
  328. extraConfig.DateConfList[i].EndDate = edbItem.EndDate
  329. }
  330. }
  331. }
  332. }
  333. dataListResp.SeriesList = extraConfig.SeriesList
  334. dataListResp.DateConfList = extraConfig.DateConfList
  335. dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
  336. dataListResp.UnitList = unitList
  337. dataListResp.IsHeap = extraConfig.IsHeap
  338. dataListResp.SortType = extraConfig.SortType
  339. return
  340. }
  341. // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
  342. func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*models.EdbDataList, dateChange []*models.ChartSectionDateChange, moveForward int) (newDate string, err error) {
  343. if edbInfoId > 0 { //指标日期
  344. newDate = GetEdbDateByMoveForward(moveForward, dataList)
  345. } else {
  346. //系统日期
  347. newDate = time.Now().Format(utils.FormatDate)
  348. }
  349. if newDate != "" && len(dateChange) > 0 {
  350. newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
  351. }
  352. return
  353. }
  354. func GetEdbDateByMoveForward(moveForward int, edbDataList []*models.EdbDataList) (date string) {
  355. dateList := make([]string, 0)
  356. for _, v := range edbDataList {
  357. dateList = append(dateList, v.DataTime)
  358. }
  359. date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
  360. return
  361. }
  362. func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
  363. // 根据日期进行排序
  364. index := len(dateList) - 1 - moveForward
  365. for k, v := range dateList {
  366. if k == index {
  367. date = v
  368. return
  369. }
  370. }
  371. return
  372. }
  373. // HandleChartSectionSeriesDateChange 处理日期变换
  374. func HandleChartSectionSeriesDateChange(date string, dateChange []*models.ChartSectionDateChange) (newDate string, err error) {
  375. newDate = date
  376. if newDate != "" {
  377. if len(dateChange) > 0 {
  378. var dateTime time.Time
  379. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  380. if err != nil {
  381. err = fmt.Errorf("日期解析失败: %s", err.Error())
  382. return
  383. }
  384. for _, v := range dateChange {
  385. if v.ChangeType == 1 {
  386. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  387. newDate = dateTime.Format(utils.FormatDate)
  388. } else if v.ChangeType == 2 {
  389. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  390. if err != nil {
  391. return
  392. }
  393. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  394. if err != nil {
  395. err = fmt.Errorf("日期解析失败: %s", err.Error())
  396. return
  397. }
  398. }
  399. }
  400. }
  401. }
  402. return
  403. }
  404. // handleSystemAppointDateT
  405. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  406. // @author: Roc
  407. // @datetime2023-10-27 09:31:35
  408. // @param Frequency string
  409. // @param Day string
  410. // @return date string
  411. // @return err error
  412. // @return errMsg string
  413. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  414. //currDate := time.Now()
  415. switch frequency {
  416. case "本周":
  417. day := int(currDate.Weekday())
  418. if day == 0 { // 周日
  419. day = 7
  420. }
  421. num := 0
  422. switch appointDay {
  423. case "周一":
  424. num = 1
  425. case "周二":
  426. num = 2
  427. case "周三":
  428. num = 3
  429. case "周四":
  430. num = 4
  431. case "周五":
  432. num = 5
  433. case "周六":
  434. num = 6
  435. case "周日":
  436. num = 7
  437. }
  438. day = num - day
  439. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  440. case "本旬":
  441. day := currDate.Day()
  442. var tmpDate time.Time
  443. switch appointDay {
  444. case "第一天":
  445. if day <= 10 {
  446. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  447. } else if day <= 20 {
  448. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  449. } else {
  450. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  451. }
  452. case "最后一天":
  453. if day <= 10 {
  454. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  455. } else if day <= 20 {
  456. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  457. } else {
  458. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  459. }
  460. }
  461. date = tmpDate.Format(utils.FormatDate)
  462. case "本月":
  463. var tmpDate time.Time
  464. switch appointDay {
  465. case "第一天":
  466. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  467. case "最后一天":
  468. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  469. }
  470. date = tmpDate.Format(utils.FormatDate)
  471. case "本季":
  472. month := currDate.Month()
  473. var tmpDate time.Time
  474. switch appointDay {
  475. case "第一天":
  476. if month <= 3 {
  477. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  478. } else if month <= 6 {
  479. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  480. } else if month <= 9 {
  481. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  482. } else {
  483. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  484. }
  485. case "最后一天":
  486. if month <= 3 {
  487. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  488. } else if month <= 6 {
  489. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  490. } else if month <= 9 {
  491. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  492. } else {
  493. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  494. }
  495. }
  496. date = tmpDate.Format(utils.FormatDate)
  497. case "本半年":
  498. month := currDate.Month()
  499. var tmpDate time.Time
  500. switch appointDay {
  501. case "第一天":
  502. if month <= 6 {
  503. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  504. } else {
  505. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  506. }
  507. case "最后一天":
  508. if month <= 6 {
  509. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  510. } else {
  511. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  512. }
  513. }
  514. date = tmpDate.Format(utils.FormatDate)
  515. case "本年":
  516. var tmpDate time.Time
  517. switch appointDay {
  518. case "第一天":
  519. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  520. case "最后一天":
  521. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  522. }
  523. date = tmpDate.Format(utils.FormatDate)
  524. default:
  525. errMsg = "错误的日期频度:" + frequency
  526. err = errors.New(errMsg)
  527. return
  528. }
  529. return
  530. }
  531. // sortTripleDataSet 以第一组数据为基准,排序之后,空数组的位置也要同步变更
  532. 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, newIndexXDataList []int) {
  533. newDataListMap = make(map[string][]float64)
  534. newNoDataListIndexMap = make(map[string][]int)
  535. indices := make([]int, len(baseDataList))
  536. newIndices := make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList))
  537. // 初始化indices
  538. for i := range indices {
  539. indices[i] = i
  540. }
  541. if len(baseSeriesNoDataIndexList) > 0 { //把空值移动到最右边
  542. j := 0
  543. for i := range indices {
  544. isEmpty := false
  545. for _, v := range baseSeriesNoDataIndexList {
  546. if i == v {
  547. isEmpty = true
  548. break
  549. }
  550. }
  551. if isEmpty {
  552. continue
  553. }
  554. newIndices[j] = i
  555. j += 1
  556. }
  557. newIndices = append(newIndices, baseSeriesNoDataIndexList...)
  558. // 根据排序后的indices重新排列所有组的数据
  559. for i, idx := range newIndices {
  560. for k, _ := range dataListMap {
  561. if _, ok := newDataListMap[k]; !ok {
  562. newDataListMap[k] = make([]float64, len(baseDataList))
  563. }
  564. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  565. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  566. }
  567. newDataListMap[k][i] = dataListMap[k][idx]
  568. }
  569. }
  570. dataListMap = newDataListMap
  571. noDataListIndexMap = newNoDataListIndexMap
  572. newDataListMap = make(map[string][]float64)
  573. newNoDataListIndexMap = make(map[string][]int)
  574. baseDataList, _ = dataListMap[baseName]
  575. //先把空的数据移动到最后面
  576. indices = make([]int, len(baseDataList)-len(baseSeriesNoDataIndexList)) //空值不参与排序
  577. newIndices = make([]int, len(baseDataList)) //空值不参与排序
  578. // 初始化indices
  579. for i := range indices {
  580. indices[i] = i
  581. }
  582. }
  583. length := len(indices)
  584. baseDataSortList := make([]models.ChartSectionSeriesValSort, length)
  585. for i, value := range baseDataList {
  586. if i < length {
  587. baseDataSortList[i] = models.ChartSectionSeriesValSort{Index: i, Value: value}
  588. }
  589. }
  590. if asc == 1 {
  591. // 使用sort.Sort进行排序
  592. sort.Sort(models.ChartSectionSeriesValSortAsc(baseDataSortList))
  593. } else {
  594. sort.Sort(models.ChartSectionSeriesValSortDesc(baseDataSortList))
  595. }
  596. for k, v := range baseDataSortList {
  597. indices[k] = v.Index
  598. }
  599. for i := range newIndices {
  600. if i < length {
  601. newIndices[i] = indices[i]
  602. } else {
  603. newIndices[i] = i
  604. }
  605. }
  606. // 根据排序后的indices重新排列所有组的数据
  607. for i, idx := range newIndices {
  608. for k, _ := range dataListMap {
  609. if _, ok := newDataListMap[k]; !ok {
  610. newDataListMap[k] = make([]float64, len(baseDataList))
  611. }
  612. if utils.InArrayByInt(noDataListIndexMap[k], idx) { //如果i位置上的数据为空,那么
  613. newNoDataListIndexMap[k] = append(newNoDataListIndexMap[k], i)
  614. }
  615. newDataListMap[k][i] = dataListMap[k][idx]
  616. }
  617. }
  618. newIndexXDataList = newIndices
  619. return
  620. }