edb_data_quarter.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "github.com/nosixtools/solarlunar"
  8. )
  9. // 指标季度数据计算(公历转农历)
  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. items := new(EdbDataItems)
  85. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  86. items.Year = preYear
  87. for _, v := range dataList {
  88. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  89. if err != nil {
  90. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  91. return result, err
  92. }
  93. newDate := dateTime.AddDate(0, 0, int(day))
  94. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  95. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  96. if newDate.Before(selectDate) || newDate == selectDate {
  97. timestamp := newDate.UnixNano() / 1e6
  98. item := new(EdbDataList)
  99. item.DataTime = newDate.Format(utils.FormatDate)
  100. item.EdbInfoId = v.EdbInfoId
  101. item.Value = v.Value
  102. item.EdbDataId = v.EdbDataId
  103. item.DataTimestamp = timestamp
  104. items.Items = append(items.Items, item)
  105. }
  106. }
  107. result.List = append(result.List, items)
  108. } else {
  109. nextYear := thisYear + 1
  110. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  111. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  112. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  113. if err != nil {
  114. errMsg = "生成当前春节失败,Err:" + err.Error()
  115. return result, err
  116. }
  117. preYear := vy
  118. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  119. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  120. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  121. if err != nil {
  122. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  123. return result, err
  124. }
  125. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  126. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  127. items := new(EdbDataItems)
  128. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  129. items.Year = preYear - 1
  130. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  131. if ky+1 < len(yearArr) {
  132. for _, v := range dataList {
  133. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  134. if err != nil {
  135. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  136. return result, err
  137. }
  138. newDate := dateTime.AddDate(0, 0, int(day))
  139. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  140. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  141. if newDate.Before(selectDate) || newDate == selectDate {
  142. timestamp := newDate.UnixNano() / 1e6
  143. item := new(EdbDataList)
  144. item.DataTime = newDate.Format(utils.FormatDate)
  145. item.EdbInfoId = v.EdbInfoId
  146. item.Value = v.Value
  147. item.EdbDataId = v.EdbDataId
  148. item.DataTimestamp = timestamp
  149. items.Items = append(items.Items, item)
  150. }
  151. }
  152. result.List = append(result.List, items)
  153. } else {
  154. for _, v := range dataList {
  155. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  156. if err != nil {
  157. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  158. return result, err
  159. }
  160. timestamp := dateTime.UnixNano() / 1e6
  161. item := new(EdbDataList)
  162. item.DataTime = dateTime.Format(utils.FormatDate)
  163. item.EdbInfoId = v.EdbInfoId
  164. item.Value = v.Value
  165. item.EdbDataId = v.EdbDataId
  166. item.DataTimestamp = timestamp
  167. items.Items = append(items.Items, item)
  168. }
  169. result.List = append(result.List, items)
  170. }
  171. }
  172. }
  173. return
  174. }
  175. type EdbDataItems struct {
  176. Items []*EdbDataList
  177. Year int
  178. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  179. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  180. }
  181. type EdbDataResult struct {
  182. List []*EdbDataItems
  183. }