chart_extra_config.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. for _, v := range extraConfig.UnitList {
  241. if _, ok := translateNameMap[v.Name]; !ok && v.NameEn == "" {
  242. translateNameMap[v.Name] = true
  243. tmpName := strings.TrimSuffix(v.Name, " ")
  244. tmpName = strings.TrimPrefix(tmpName, " ")
  245. translateNameList = append(translateNameList, tmpName)
  246. }
  247. }
  248. for _, v := range extraConfig.XDataList {
  249. if _, ok := translateNameMap[v.Name]; !ok && v.NameEn == "" {
  250. translateNameMap[v.Name] = true
  251. tmpName := strings.TrimSuffix(v.Name, " ")
  252. tmpName = strings.TrimPrefix(tmpName, " ")
  253. translateNameList = append(translateNameList, tmpName)
  254. }
  255. }
  256. for _, v := range extraConfig.SeriesList {
  257. if v.SeriesNameEn == `` {
  258. if _, ok := translateNameMap[v.SeriesName]; !ok {
  259. translateNameMap[v.SeriesName] = true
  260. tmpName := strings.TrimSuffix(v.SeriesName, " ")
  261. tmpName = strings.TrimPrefix(tmpName, " ")
  262. translateNameList = append(translateNameList, tmpName)
  263. }
  264. }
  265. }
  266. // 获取英文名称map
  267. enNameMap, _, _ := GetEnNameMapByCnNameList(translateNameList)
  268. for k, seriesItem := range extraConfig.SeriesList {
  269. if len(seriesItem.EdbInfoList) <= 0 {
  270. errMsg = "指标不能为空"
  271. err = errors.New(errMsg)
  272. return
  273. }
  274. if seriesItem.SeriesNameEn == `` { //系列英文名称
  275. if tmpNameEn, ok := enNameMap[seriesItem.SeriesName]; ok {
  276. seriesItem.SeriesNameEn = tmpNameEn
  277. }
  278. }
  279. extraConfig.SeriesList[k] = seriesItem
  280. }
  281. for k, v := range extraConfig.XDataList {
  282. if tmpNameEn, ok := enNameMap[v.Name]; ok && v.NameEn == "" {
  283. extraConfig.XDataList[k].NameEn = tmpNameEn
  284. }
  285. }
  286. for k, v := range extraConfig.UnitList {
  287. if tmpNameEn, ok := enNameMap[v.Name]; ok && v.NameEn == "" {
  288. extraConfig.UnitList[k].NameEn = tmpNameEn
  289. }
  290. }
  291. for k, v := range extraConfig.DateConfList {
  292. if tmpNameEn, ok := enNameMap[v.DateConfName]; ok && v.DateConfNameEn == "" {
  293. extraConfig.DateConfList[k].DateConfNameEn = tmpNameEn
  294. }
  295. }
  296. newExtraConfig = extraConfig
  297. return
  298. }
  299. // GetChartSectionCombineData 截面组合图的数据处理
  300. 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) {
  301. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  302. edbDataMap := make(map[int]map[string]float64)
  303. for edbInfoId, edbDataList := range edbDataListMap {
  304. edbDateData := make(map[string]float64)
  305. for _, edbData := range edbDataList {
  306. edbDateData[edbData.DataTime] = edbData.Value
  307. }
  308. edbDataMap[edbInfoId] = edbDateData
  309. }
  310. // edbIdList 指标展示顺序;x轴的指标顺序
  311. edbIdList = make([]int, 0)
  312. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  313. for _, v := range mappingList {
  314. edbIdList = append(edbIdList, v.EdbInfoId)
  315. edbMappingMap[v.EdbInfoId] = v
  316. }
  317. // 确定好截面散点图返回的数据格式
  318. // 获取所有的引用日期设置
  319. DateConfListMap := make(map[string]*data_manage.ChartSectionDateConfItem)
  320. for _, v := range extraConfig.DateConfList {
  321. DateConfListMap[v.DateConfName] = v
  322. }
  323. // 遍历每个系列
  324. // 遍历每个指标,根据选中的日期,进行日期变换得到最终的日期,根据最终的日期获取对应的值
  325. // 组装数据
  326. baseSeries := new(data_manage.ChartSectionSeriesItem) //y轴的系列
  327. var firstUnit, leftUnit, rightUnit, right2Unit *data_manage.XData
  328. for _, seriesItem := range extraConfig.SeriesList {
  329. var maxDate time.Time
  330. var minVal, maxVal float64
  331. noDataEdbIdList := make([]int, 0)
  332. dataList := make([]float64, len(seriesItem.EdbInfoList))
  333. for index, edbConf := range seriesItem.EdbInfoList {
  334. edbInfoId := edbConf.EdbInfoId //X轴的指标
  335. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  336. if !ok {
  337. continue
  338. }
  339. seriesItem.EdbInfoList[index].EdbName = edbMappingInfo.EdbName
  340. seriesItem.EdbInfoList[index].EdbNameEn = edbMappingInfo.EdbNameEn
  341. seriesItem.EdbInfoList[index].Unit = edbMappingInfo.Unit
  342. seriesItem.EdbInfoList[index].UnitEn = edbMappingInfo.UnitEn
  343. if index == 0 {
  344. firstUnit = &data_manage.XData{
  345. Name: edbMappingInfo.Unit,
  346. NameEn: edbMappingInfo.UnitEn,
  347. }
  348. }
  349. edbDataList, ok3 := edbDataListMap[edbInfoId]
  350. if !ok3 {
  351. err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
  352. return
  353. }
  354. /*dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  355. if len(dataList) <= 0 {
  356. // 没有数据的指标id
  357. //findDataList = append(findDataList, 0)
  358. continue
  359. }*/
  360. //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
  361. var findDate string
  362. if edbConf.DateConfName == "" {
  363. findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
  364. } else {
  365. // 获取日期配置
  366. dateConfItem, ok1 := DateConfListMap[edbConf.DateConfName]
  367. if !ok1 {
  368. err = fmt.Errorf("引用日期配置不存在")
  369. return
  370. }
  371. // todo 根据日期变换得到最终日期
  372. edbDataListTmp := make([]*data_manage.EdbDataList, 0)
  373. if dateConfItem.EdbInfoId > 0 {
  374. edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
  375. if !ok1 {
  376. err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
  377. return
  378. }
  379. }
  380. findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
  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. noDataEdbIdList = append(noDataEdbIdList, edbInfoId)
  405. continue
  406. }
  407. }
  408. seriesItem.DataList = dataList
  409. seriesItem.MinData = minVal
  410. seriesItem.MaxData = maxVal
  411. seriesItem.NoDataEdbIdList = noDataEdbIdList
  412. if extraConfig.BaseChartSeriesName == seriesItem.SeriesName {
  413. baseSeries = seriesItem
  414. }
  415. if seriesItem.IsAxis == 1 && leftUnit == nil { //左轴,右轴
  416. leftUnit = firstUnit
  417. } else if seriesItem.IsAxis == 0 && rightUnit == nil {
  418. rightUnit = firstUnit
  419. } else if seriesItem.IsAxis == 2 && right2Unit == nil {
  420. right2Unit = firstUnit
  421. }
  422. }
  423. // 处理横轴
  424. // 遍历基准系列,判断有几个横轴名称
  425. if baseSeries == nil {
  426. err = fmt.Errorf("基准系列不存在")
  427. return
  428. }
  429. xDataList := make([]data_manage.XData, 0)
  430. for index, item := range baseSeries.EdbInfoList {
  431. if index == 0 {
  432. firstUnit = &data_manage.XData{
  433. Name: item.Unit,
  434. NameEn: item.UnitEn,
  435. }
  436. }
  437. tmp := data_manage.XData{
  438. Name: item.EdbName,
  439. NameEn: item.EdbNameEn,
  440. }
  441. // 如果已经设置了横轴名称,则用设置的名称替换
  442. if len(extraConfig.XDataList) > index {
  443. newItem := extraConfig.XDataList[index]
  444. if newItem.Name != "" {
  445. tmp = newItem
  446. }
  447. }
  448. xDataList = append(xDataList, tmp)
  449. }
  450. dataListResp.XDataList = xDataList
  451. // todo 处理纵轴, 如果只存在一个右轴会有问题
  452. unitList := make([]*data_manage.XData, 3)
  453. if baseSeries.IsAxis == 1 { //左轴,右轴
  454. leftUnit = firstUnit
  455. } else if baseSeries.IsAxis == 2 {
  456. rightUnit = firstUnit
  457. } else {
  458. right2Unit = firstUnit
  459. }
  460. if len(extraConfig.UnitList) == 0 {
  461. unitList[0] = leftUnit
  462. unitList[1] = rightUnit
  463. unitList[2] = right2Unit
  464. } else if len(extraConfig.UnitList) == 1 {
  465. unitList[0] = extraConfig.UnitList[0]
  466. unitList[1] = rightUnit
  467. unitList[2] = right2Unit
  468. } else if len(extraConfig.UnitList) == 2 {
  469. unitList[0] = extraConfig.UnitList[0]
  470. unitList[1] = extraConfig.UnitList[1]
  471. unitList[2] = right2Unit
  472. }
  473. dataListResp.XDataList = extraConfig.XDataList
  474. dataListResp.SeriesList = extraConfig.SeriesList
  475. dataListResp.DateConfList = extraConfig.DateConfList
  476. dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
  477. dataListResp.UnitList = extraConfig.UnitList
  478. dataListResp.IsHeap = extraConfig.IsHeap
  479. dataListResp.SortType = extraConfig.SortType
  480. return
  481. }
  482. // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
  483. func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*data_manage.EdbDataList, dateChange []*data_manage.ChartSectionDateChange, moveForward int) (newDate string, err error) {
  484. if edbInfoId > 0 { //指标日期
  485. newDate = GetEdbDateByMoveForward(moveForward, dataList)
  486. } else {
  487. //系统日期
  488. newDate = time.Now().Format(utils.FormatDate)
  489. }
  490. if newDate != "" && len(dateChange) > 0 {
  491. newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
  492. }
  493. return
  494. }
  495. func GetEdbDateByMoveForward(moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  496. dateList := make([]string, 0)
  497. for _, v := range edbDataList {
  498. dateList = append(dateList, v.DataTime)
  499. }
  500. date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
  501. return
  502. }
  503. func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
  504. // 根据日期进行排序
  505. index := len(dateList) - 1 - moveForward
  506. for k, v := range dateList {
  507. if k == index {
  508. date = v
  509. return
  510. }
  511. }
  512. return
  513. }
  514. // HandleChartSectionSeriesDateChange 处理日期变换
  515. func HandleChartSectionSeriesDateChange(date string, dateChange []*data_manage.ChartSectionDateChange) (newDate string, err error) {
  516. newDate = date
  517. if newDate != "" {
  518. if len(dateChange) > 0 {
  519. var dateTime time.Time
  520. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  521. if err != nil {
  522. err = fmt.Errorf("日期解析失败: %s", err.Error())
  523. return
  524. }
  525. for _, v := range dateChange {
  526. if v.ChangeType == 1 {
  527. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  528. newDate = dateTime.Format(utils.FormatDate)
  529. } else if v.ChangeType == 2 {
  530. newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  531. if err != nil {
  532. return
  533. }
  534. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  535. if err != nil {
  536. err = fmt.Errorf("日期解析失败: %s", err.Error())
  537. return
  538. }
  539. }
  540. }
  541. }
  542. }
  543. return
  544. }
  545. // handleSystemAppointDateT
  546. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  547. // @author: Roc
  548. // @datetime2023-10-27 09:31:35
  549. // @param Frequency string
  550. // @param Day string
  551. // @return date string
  552. // @return err error
  553. // @return errMsg string
  554. func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  555. //currDate := time.Now()
  556. switch frequency {
  557. case "本周":
  558. day := int(currDate.Weekday())
  559. if day == 0 { // 周日
  560. day = 7
  561. }
  562. num := 0
  563. switch appointDay {
  564. case "周一":
  565. num = 1
  566. case "周二":
  567. num = 2
  568. case "周三":
  569. num = 3
  570. case "周四":
  571. num = 4
  572. case "周五":
  573. num = 5
  574. case "周六":
  575. num = 6
  576. case "周日":
  577. num = 7
  578. }
  579. day = num - day
  580. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  581. case "本旬":
  582. day := currDate.Day()
  583. var tmpDate time.Time
  584. switch appointDay {
  585. case "第一天":
  586. if day <= 10 {
  587. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  588. } else if day <= 20 {
  589. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  590. } else {
  591. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  592. }
  593. case "最后一天":
  594. if day <= 10 {
  595. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  596. } else if day <= 20 {
  597. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  598. } else {
  599. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  600. }
  601. }
  602. date = tmpDate.Format(utils.FormatDate)
  603. case "本月":
  604. var tmpDate time.Time
  605. switch appointDay {
  606. case "第一天":
  607. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  608. case "最后一天":
  609. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  610. }
  611. date = tmpDate.Format(utils.FormatDate)
  612. case "本季":
  613. month := currDate.Month()
  614. var tmpDate time.Time
  615. switch appointDay {
  616. case "第一天":
  617. if month <= 3 {
  618. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  619. } else if month <= 6 {
  620. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  621. } else if month <= 9 {
  622. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  623. } else {
  624. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  625. }
  626. case "最后一天":
  627. if month <= 3 {
  628. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  629. } else if month <= 6 {
  630. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  631. } else if month <= 9 {
  632. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  633. } else {
  634. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  635. }
  636. }
  637. date = tmpDate.Format(utils.FormatDate)
  638. case "本半年":
  639. month := currDate.Month()
  640. var tmpDate time.Time
  641. switch appointDay {
  642. case "第一天":
  643. if month <= 6 {
  644. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  645. } else {
  646. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  647. }
  648. case "最后一天":
  649. if month <= 6 {
  650. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  651. } else {
  652. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  653. }
  654. }
  655. date = tmpDate.Format(utils.FormatDate)
  656. case "本年":
  657. var tmpDate time.Time
  658. switch appointDay {
  659. case "第一天":
  660. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  661. case "最后一天":
  662. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  663. }
  664. date = tmpDate.Format(utils.FormatDate)
  665. default:
  666. errMsg = "错误的日期频度:" + frequency
  667. err = errors.New(errMsg)
  668. return
  669. }
  670. return
  671. }