edb_data_quarter.go 6.1 KB

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