edb_data_quarter.go 6.1 KB

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