edb_data.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_mobile/models/data_manage"
  5. "eta/eta_mobile/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, endDate 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 := make([]*data_manage.EdbDataList, 0)
  126. switch edbInfo.EdbInfoType {
  127. case 0:
  128. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDateReal, endDate)
  129. case 1:
  130. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, startDateReal, endDate, false)
  131. default:
  132. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  133. }
  134. if err != nil {
  135. return
  136. }
  137. latestDateStr := edbInfo.LatestDate //实际数据的截止日期
  138. latestDate, tmpErr := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  139. if tmpErr != nil {
  140. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + edbInfo.LatestDate))
  141. return
  142. }
  143. latestDateYear := latestDate.Year() //实际数据截止年份
  144. if calendar == "农历" {
  145. if len(dataList) <= 0 {
  146. resultData = new(data_manage.EdbDataResult)
  147. } else {
  148. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  149. if tmpErr != nil {
  150. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  151. return
  152. }
  153. // 处理季节图的截止日期
  154. for k, edbDataItems := range result.List {
  155. var cuttingDataTimestamp int64
  156. // 切割的日期时间字符串
  157. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  158. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  159. if edbDataItems.Year >= latestDateYear {
  160. for _, tmpData := range edbDataItems.Items {
  161. if tmpData.DataTime == cuttingDataTimeStr {
  162. cuttingDataTimestamp = tmpData.DataTimestamp
  163. break
  164. }
  165. }
  166. }
  167. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  168. result.List[k] = edbDataItems
  169. }
  170. if result.List[0].Year != calendarPreYear {
  171. itemList := make([]*data_manage.EdbDataList, 0)
  172. items := new(data_manage.EdbDataItems)
  173. //items.Year = calendarPreYear
  174. items.Items = itemList
  175. newResult := new(data_manage.EdbDataResult)
  176. newResult.List = append(newResult.List, items)
  177. newResult.List = append(newResult.List, result.List...)
  178. resultData = newResult
  179. } else {
  180. resultData = result
  181. }
  182. }
  183. } else {
  184. currentYear := time.Now().Year()
  185. quarterDataList := make([]*data_manage.QuarterData, 0)
  186. quarterMap := make(map[int][]*data_manage.EdbDataList)
  187. var quarterArr []int
  188. for _, v := range dataList {
  189. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  190. if tmpErr != nil {
  191. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  192. return
  193. }
  194. year := itemDate.Year()
  195. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  196. timestamp := newItemDate.UnixNano() / 1e6
  197. v.DataTimestamp = timestamp
  198. if findVal, ok := quarterMap[year]; !ok {
  199. quarterArr = append(quarterArr, year)
  200. findVal = append(findVal, v)
  201. quarterMap[year] = findVal
  202. } else {
  203. findVal = append(findVal, v)
  204. quarterMap[year] = findVal
  205. }
  206. }
  207. for _, v := range quarterArr {
  208. itemList := quarterMap[v]
  209. quarterItem := new(data_manage.QuarterData)
  210. quarterItem.Year = v
  211. quarterItem.DataList = itemList
  212. //如果等于最后的实际日期,那么将切割时间戳记录
  213. if v == latestDateYear {
  214. var cuttingDataTimestamp int64
  215. for _, tmpData := range itemList {
  216. if tmpData.DataTime == latestDateStr {
  217. cuttingDataTimestamp = tmpData.DataTimestamp
  218. break
  219. }
  220. }
  221. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  222. } else if v > latestDateYear {
  223. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  224. if len(itemList) > 0 {
  225. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  226. }
  227. }
  228. quarterDataList = append(quarterDataList, quarterItem)
  229. }
  230. resultData = quarterDataList
  231. }
  232. return
  233. }
  234. // TbzDiv 同比值计算
  235. func TbzDiv(a, b float64) float64 {
  236. var valFloat float64
  237. if b != 0 {
  238. af := decimal.NewFromFloat(a)
  239. bf := decimal.NewFromFloat(b)
  240. val, _ := af.Div(bf).Float64()
  241. val = val - 1
  242. valFloat, _ = decimal.NewFromFloat(val).RoundCeil(4).Float64()
  243. } else {
  244. valFloat = 0
  245. }
  246. return valFloat
  247. }
  248. // GetDateData 获取指标的单个日期的值
  249. func GetDateData(edbInfo *data_manage.EdbInfo, startDate, endDate string) (item *data_manage.EdbDataList, err error) {
  250. var dataList []*data_manage.EdbDataList
  251. switch edbInfo.EdbInfoType {
  252. case 0:
  253. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDate, endDate)
  254. case 1:
  255. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, startDate, endDate, false)
  256. default:
  257. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  258. }
  259. if err != nil {
  260. return
  261. }
  262. if len(dataList) <= 0 {
  263. return
  264. }
  265. item = dataList[0]
  266. return
  267. }