作者 yangfu

按月统计修改

@@ -706,21 +706,33 @@ func histogramStatisticsByMonth(start time.Time) ([]queryItem, []string) { @@ -706,21 +706,33 @@ func histogramStatisticsByMonth(start time.Time) ([]queryItem, []string) {
706 var monthEnd = time.Date(year, month+1, 1, 0, 0, 0, 0, time.Local).Add(-time.Second) 706 var monthEnd = time.Date(year, month+1, 1, 0, 0, 0, 0, time.Local).Add(-time.Second)
707 var xAxisData []string 707 var xAxisData []string
708 for { 708 for {
709 - if beginTime.AddDate(0, 0, increaseDay).After(monthEnd) { 709 + currentIncreaseDay := increaseDay
  710 + leftDay := monthEnd.Day() - endTime.Day()
  711 + if leftDay == 0 {
  712 + break
  713 + }
  714 + // 不足5天的情况,比如二月 28、29天
  715 + if leftDay > 0 && leftDay < increaseDay {
  716 + currentIncreaseDay = leftDay
  717 + }
  718 + if beginTime.AddDate(0, 0, currentIncreaseDay).After(monthEnd) {
710 endTime = monthEnd 719 endTime = monthEnd
711 break 720 break
712 } else { 721 } else {
713 - endTime = beginTime.AddDate(0, 0, increaseDay).Add(-time.Second) 722 + endTime = beginTime.AddDate(0, 0, currentIncreaseDay).Add(-time.Second)
714 } 723 }
715 item := queryItem{ 724 item := queryItem{
716 BeginTime: beginTime, 725 BeginTime: beginTime,
717 - EndTime: beginTime.AddDate(0, 0, increaseDay).Add(-time.Second), 726 + EndTime: beginTime.AddDate(0, 0, currentIncreaseDay).Add(-time.Second),
  727 + }
  728 + // 30号接近月末时,如果有31号 就取月末值
  729 + left := monthEnd.Day() - item.EndTime.Day()
  730 + if left <= 1 {
  731 + item.EndTime = monthEnd
  732 + endTime = monthEnd
718 } 733 }
719 xAxisData = append(xAxisData, item.EndTime.Format("01-02")) 734 xAxisData = append(xAxisData, item.EndTime.Format("01-02"))
720 beginTime = endTime 735 beginTime = endTime
721 - if endTime == monthEnd {  
722 - break  
723 - }  
724 ret = append(ret, item) 736 ret = append(ret, item)
725 } 737 }
726 return ret, xAxisData 738 return ret, xAxisData
  1 +package domain_service
  2 +
  3 +import (
  4 + "fmt"
  5 + "log"
  6 + "testing"
  7 + "time"
  8 +)
  9 +
  10 +func Test_histogramStatisticsByMonth(t *testing.T) {
  11 + input := time.Date(2021, 1, 1, 0, 0, 0, 0, time.Local)
  12 +
  13 + for i := 0; i < 12; i++ {
  14 + y, x := histogramStatisticsByMonth(input)
  15 + log.Println(fmt.Sprintf("月份:%d", input.Month()), x)
  16 + log.Println(fmt.Sprintf("月份:%d", input.Month()), y)
  17 + input = input.AddDate(0, 1, 0)
  18 + }
  19 +}