edb_data.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_hub/models/data_manage"
  5. "eta/eta_hub/utils"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. "time"
  9. )
  10. // GetEdbDataTbz 获取指标的同比值数据
  11. func GetEdbDataTbz(edbInfo *data_manage.EdbInfo, tmpDataList []*data_manage.EdbDataList, startDateTime time.Time) (dataList []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  12. dataList = make([]*data_manage.EdbDataList, 0)
  13. // 数据处理
  14. var dateArr []string
  15. dataMap := make(map[string]*data_manage.EdbDataList)
  16. for _, v := range tmpDataList {
  17. dateArr = append(dateArr, v.DataTime)
  18. dataMap[v.DataTime] = v
  19. }
  20. for _, av := range dateArr {
  21. currentItem, ok := dataMap[av]
  22. // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
  23. if !ok {
  24. continue
  25. }
  26. tmpItem := *currentItem
  27. var isOk bool //是否计算出来结果
  28. //当前日期
  29. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  30. if tmpErr != nil {
  31. err = tmpErr
  32. return
  33. }
  34. // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
  35. if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
  36. continue
  37. }
  38. //上一年的日期
  39. preDate := currentDate.AddDate(-1, 0, 0)
  40. preDateStr := preDate.Format(utils.FormatDate)
  41. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  42. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  43. isOk = true
  44. } else {
  45. if edbInfo.Frequency == "月度" { //向上和向下,各找一个月
  46. for i := 0; i <= 35; i++ {
  47. nextDateDay := preDate.AddDate(0, 0, i)
  48. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  49. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  50. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  51. isOk = true
  52. break
  53. } else {
  54. preDateDay := preDate.AddDate(0, 0, -i)
  55. preDateDayStr := preDateDay.Format(utils.FormatDate)
  56. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  57. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  58. isOk = true
  59. break
  60. }
  61. }
  62. }
  63. } else if edbInfo.Frequency == "季度" || edbInfo.Frequency == "年度" {
  64. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  65. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  66. isOk = true
  67. break
  68. }
  69. } else {
  70. nextDateDay := preDate.AddDate(0, 0, 1)
  71. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  72. preDateDay := preDate.AddDate(0, 0, -1)
  73. preDateDayStr := preDateDay.Format(utils.FormatDate)
  74. for i := 0; i < 35; i++ {
  75. if i >= 1 {
  76. nextDateDay = nextDateDay.AddDate(0, 0, i)
  77. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  78. }
  79. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  80. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  81. isOk = true
  82. break
  83. } else {
  84. if i >= 1 {
  85. preDateDay = preDate.AddDate(0, 0, -i)
  86. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  87. }
  88. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  89. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  90. isOk = true
  91. break
  92. }
  93. }
  94. }
  95. }
  96. }
  97. if isOk {
  98. if tmpItem.Value > maxValue {
  99. maxValue = tmpItem.Value
  100. }
  101. if tmpItem.Value < minValue {
  102. minValue = tmpItem.Value
  103. }
  104. dataList = append(dataList, &tmpItem)
  105. }
  106. }
  107. return
  108. }
  109. // GetChartEdbSeasonalData 获取指标的季节性数据
  110. func GetChartEdbSeasonalData(calendar, startDate string, edbInfo *data_manage.EdbInfo) (resultData interface{}, err error) {
  111. var startDateReal string
  112. startDateReal = startDate
  113. calendarPreYear := 0
  114. if calendar == "农历" {
  115. newStartDateReal, tmpErr := time.Parse(utils.FormatDate, startDateReal)
  116. if tmpErr != nil {
  117. err = tmpErr
  118. fmt.Println("time.Parse:" + err.Error())
  119. return
  120. }
  121. calendarPreYear = newStartDateReal.Year() - 1
  122. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  123. startDateReal = newStartDateReal.Format(utils.FormatDate)
  124. }
  125. dataList, err := GetEdbDataList(edbInfo, startDateReal)
  126. if err != nil {
  127. err = fmt.Errorf("获取指标数据出错 Err:%s", err.Error())
  128. return
  129. }
  130. latestDateStr := edbInfo.LatestDate //实际数据的截止日期
  131. latestDate, tmpErr := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  132. if tmpErr != nil {
  133. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + edbInfo.LatestDate))
  134. return
  135. }
  136. latestDateYear := latestDate.Year() //实际数据截止年份
  137. if calendar == "农历" {
  138. if len(dataList) <= 0 {
  139. resultData = new(data_manage.EdbDataResult)
  140. } else {
  141. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  142. if tmpErr != nil {
  143. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  144. return
  145. }
  146. // 处理季节图的截止日期
  147. for k, edbDataItems := range result.List {
  148. var cuttingDataTimestamp int64
  149. // 切割的日期时间字符串
  150. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  151. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  152. if edbDataItems.Year >= latestDateYear {
  153. for _, tmpData := range edbDataItems.Items {
  154. if tmpData.DataTime == cuttingDataTimeStr {
  155. cuttingDataTimestamp = tmpData.DataTimestamp
  156. break
  157. }
  158. }
  159. }
  160. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  161. result.List[k] = edbDataItems
  162. }
  163. //fmt.Println("result", result.List[0].Items[0].DataTime)
  164. //fmt.Println("calendarPreYear:", calendarPreYear)
  165. //fmt.Println("result.List[0].Year", result.List[0].Year)
  166. if result.List[0].Year != calendarPreYear {
  167. itemList := make([]*data_manage.EdbDataList, 0)
  168. items := new(data_manage.EdbDataItems)
  169. //items.Year = calendarPreYear
  170. items.Items = itemList
  171. newResult := new(data_manage.EdbDataResult)
  172. newResult.List = append(newResult.List, items)
  173. newResult.List = append(newResult.List, result.List...)
  174. resultData = newResult
  175. } else {
  176. resultData = result
  177. }
  178. }
  179. } else {
  180. currentYear := time.Now().Year()
  181. quarterDataList := make([]*data_manage.QuarterData, 0)
  182. quarterMap := make(map[int][]*data_manage.EdbDataList)
  183. var quarterArr []int
  184. for _, v := range dataList {
  185. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  186. if tmpErr != nil {
  187. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  188. return
  189. }
  190. year := itemDate.Year()
  191. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  192. timestamp := newItemDate.UnixNano() / 1e6
  193. v.DataTimestamp = timestamp
  194. if findVal, ok := quarterMap[year]; !ok {
  195. quarterArr = append(quarterArr, year)
  196. findVal = append(findVal, v)
  197. quarterMap[year] = findVal
  198. } else {
  199. findVal = append(findVal, v)
  200. quarterMap[year] = findVal
  201. }
  202. }
  203. for _, v := range quarterArr {
  204. itemList := quarterMap[v]
  205. quarterItem := new(data_manage.QuarterData)
  206. quarterItem.Year = v
  207. quarterItem.DataList = itemList
  208. //如果等于最后的实际日期,那么将切割时间戳记录
  209. if v == latestDateYear {
  210. var cuttingDataTimestamp int64
  211. for _, tmpData := range itemList {
  212. if tmpData.DataTime == latestDateStr {
  213. cuttingDataTimestamp = tmpData.DataTimestamp
  214. break
  215. }
  216. }
  217. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  218. } else if v > latestDateYear {
  219. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  220. if len(itemList) > 0 {
  221. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  222. }
  223. }
  224. quarterDataList = append(quarterDataList, quarterItem)
  225. }
  226. resultData = quarterDataList
  227. }
  228. return
  229. }
  230. // TbzDiv 同比值计算
  231. func TbzDiv(a, b float64) float64 {
  232. var valFloat float64
  233. if b != 0 {
  234. af := decimal.NewFromFloat(a)
  235. bf := decimal.NewFromFloat(b)
  236. val, _ := af.Div(bf).Float64()
  237. val = val - 1
  238. valFloat, _ = decimal.NewFromFloat(val).Round(4).Float64()
  239. } else {
  240. valFloat = 0
  241. }
  242. return valFloat
  243. }
  244. // GetEdbDataList 获取指标数据
  245. func GetEdbDataList(edbInfo *data_manage.EdbInfo, startDate string) (dataList []*data_manage.EdbDataList, err error) {
  246. var list []*data_manage.EdbData
  247. switch edbInfo.EdbInfoType {
  248. case 0:
  249. dataOb := new(data_manage.EdbData)
  250. list, err = dataOb.GetItemsBySourceAndCode(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, startDate, []string{}, "data_time ASC")
  251. //case 1:
  252. //_, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, startDate, endDate, false)
  253. default:
  254. err = errors.New(fmt.Sprint("获取失败,不支持预测指标", edbInfo.EdbInfoType))
  255. }
  256. if err != nil {
  257. return
  258. }
  259. if len(list) <= 0 {
  260. return
  261. }
  262. for _, v := range list {
  263. dataList = append(dataList, data_manage.FormatEdbData2List(v))
  264. }
  265. return
  266. }