123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package data
- import (
- "eta/eta_chart_lib/models"
- "eta/eta_chart_lib/utils"
- "time"
- )
- // GetEdbDataTbzForSeason 获取指标的同比值数据
- func GetEdbDataTbzForSeason(frequency string, tmpDataList []*models.EdbDataList, startDateTime time.Time) (dataList []*models.EdbDataList, minValue, maxValue float64, err error) {
- dataList = make([]*models.EdbDataList, 0)
- // 数据处理
- var dateArr []string
- dataMap := make(map[string]*models.EdbDataList)
- for _, v := range tmpDataList {
- dateArr = append(dateArr, v.DataTime)
- dataMap[v.DataTime] = v
- }
- for _, av := range dateArr {
- currentItem, ok := dataMap[av]
- // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
- if !ok {
- continue
- }
- tmpItem := *currentItem
- var isOk bool //是否计算出来结果
- //当前日期
- currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
- if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
- continue
- }
- //上一年的日期
- preDate := currentDate.AddDate(-1, 0, 0)
- preDateStr := preDate.Format(utils.FormatDate)
- if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
- tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
- isOk = true
- } else {
- if frequency == "月度" { //向上和向下,各找一个月
- for i := 0; i <= 35; i++ {
- nextDateDay := preDate.AddDate(0, 0, i)
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
- isOk = true
- break
- } else {
- preDateDay := preDate.AddDate(0, 0, -i)
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
- isOk = true
- break
- }
- }
- }
- } else if frequency == "季度" || frequency == "年度" {
- if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
- tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
- isOk = true
- break
- }
- } else {
- nextDateDay := preDate.AddDate(0, 0, 1)
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- preDateDay := preDate.AddDate(0, 0, -1)
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- for i := 0; i < 35; i++ {
- if i >= 1 {
- nextDateDay = nextDateDay.AddDate(0, 0, i)
- nextDateDayStr = nextDateDay.Format(utils.FormatDate)
- }
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
- isOk = true
- break
- } else {
- if i >= 1 {
- preDateDay = preDate.AddDate(0, 0, -i)
- preDateDayStr = nextDateDay.Format(utils.FormatDate)
- }
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
- isOk = true
- break
- }
- }
- }
- }
- }
- if isOk {
- if tmpItem.Value > maxValue {
- maxValue = tmpItem.Value
- }
- if tmpItem.Value < minValue {
- minValue = tmpItem.Value
- }
- dataList = append(dataList, &tmpItem)
- }
- }
- return
- }
|