chart_extra_config.go 27 KB

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