edb_data_quarter.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package data_manage
  2. import (
  3. "eta/eta_hub/utils"
  4. "fmt"
  5. "github.com/nosixtools/solarlunar"
  6. "strconv"
  7. "time"
  8. )
  9. type EdbDataItems struct {
  10. Items []*EdbDataList
  11. Year int
  12. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  13. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  14. }
  15. type EdbDataResult struct {
  16. List []*EdbDataItems
  17. }
  18. // 指标季度数据计算(公历转农历)
  19. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  20. var errMsg string
  21. defer func() {
  22. if errMsg != "" {
  23. fmt.Println("errMsg:", errMsg)
  24. }
  25. }()
  26. endDate := dataList[len(dataList)-1].DataTime
  27. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  28. if err != nil {
  29. return result, err
  30. }
  31. thisMonth := int(endDateForm.Month())
  32. result = new(EdbDataResult)
  33. var yearArr []int
  34. yearMap := make(map[int]int)
  35. var cureentDate time.Time
  36. if thisMonth < 11 {
  37. for k, v := range dataList {
  38. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  39. if err != nil {
  40. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  41. return result, err
  42. }
  43. if k == len(dataList)-1 {
  44. cureentDate = dateTime
  45. }
  46. year := dateTime.Year()
  47. if _, ok := yearMap[year]; !ok {
  48. yearArr = append(yearArr, year)
  49. }
  50. yearMap[year] = year
  51. }
  52. } else {
  53. for k, v := range dataList {
  54. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  55. if err != nil {
  56. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  57. return result, err
  58. }
  59. if k == len(dataList)-1 {
  60. cureentDate = dateTime
  61. }
  62. year := dateTime.Year() + 1
  63. if _, ok := yearMap[year]; !ok {
  64. yearArr = append(yearArr, year)
  65. }
  66. yearMap[year] = year
  67. }
  68. }
  69. //排序
  70. fmt.Println("yearArr:", yearArr)
  71. thisYear := cureentDate.Year()
  72. //thisMonth := int(cureentDate.Month())
  73. fmt.Println("thisMonth:", thisMonth)
  74. for ky, vy := range yearArr {
  75. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  76. if thisMonth < 11 {
  77. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  78. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  79. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  80. if err != nil {
  81. errMsg = "生成当前春节失败,Err:" + err.Error()
  82. return result, err
  83. }
  84. preYear := vy
  85. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  86. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  87. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  88. if err != nil {
  89. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  90. return result, err
  91. }
  92. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  93. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  94. items := new(EdbDataItems)
  95. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  96. items.Year = preYear
  97. for _, v := range dataList {
  98. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  99. if err != nil {
  100. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  101. return result, err
  102. }
  103. newDate := dateTime.AddDate(0, 0, int(day))
  104. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  105. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  106. if newDate.Before(selectDate) || newDate == selectDate {
  107. timestamp := newDate.UnixNano() / 1e6
  108. item := new(EdbDataList)
  109. item.DataTime = newDate.Format(utils.FormatDate)
  110. //item.EdbInfoId = v.EdbInfoId
  111. item.Value = v.Value
  112. //item.EdbDataId = v.EdbDataId
  113. item.DataTimestamp = timestamp
  114. items.Items = append(items.Items, item)
  115. }
  116. }
  117. result.List = append(result.List, items)
  118. } else {
  119. nextYear := thisYear + 1
  120. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  121. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  122. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  123. if err != nil {
  124. errMsg = "生成当前春节失败,Err:" + err.Error()
  125. return result, err
  126. }
  127. preYear := vy
  128. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  129. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  130. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  131. if err != nil {
  132. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  133. return result, err
  134. }
  135. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  136. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  137. items := new(EdbDataItems)
  138. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  139. items.Year = preYear - 1
  140. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  141. if ky+1 < len(yearArr) {
  142. for _, v := range dataList {
  143. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  144. if err != nil {
  145. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  146. return result, err
  147. }
  148. newDate := dateTime.AddDate(0, 0, int(day))
  149. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  150. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  151. if newDate.Before(selectDate) || newDate == selectDate {
  152. timestamp := newDate.UnixNano() / 1e6
  153. item := new(EdbDataList)
  154. item.DataTime = newDate.Format(utils.FormatDate)
  155. //item.EdbInfoId = v.EdbInfoId
  156. item.Value = v.Value
  157. //item.EdbDataId = v.EdbDataId
  158. item.DataTimestamp = timestamp
  159. items.Items = append(items.Items, item)
  160. }
  161. }
  162. result.List = append(result.List, items)
  163. } else {
  164. for _, v := range dataList {
  165. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  166. if err != nil {
  167. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  168. return result, err
  169. }
  170. timestamp := dateTime.UnixNano() / 1e6
  171. item := new(EdbDataList)
  172. item.DataTime = dateTime.Format(utils.FormatDate)
  173. //item.EdbInfoId = v.EdbInfoId
  174. item.Value = v.Value
  175. //item.EdbDataId = v.EdbDataId
  176. item.DataTimestamp = timestamp
  177. items.Items = append(items.Items, item)
  178. }
  179. result.List = append(result.List, items)
  180. }
  181. }
  182. }
  183. return
  184. }