statistic.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "hongze/fms_api/controller/resp"
  8. "hongze/fms_api/global"
  9. "hongze/fms_api/models"
  10. "hongze/fms_api/services/alarm_msg"
  11. "hongze/fms_api/utils"
  12. "time"
  13. )
  14. // // StatisticController 统计报告
  15. type StatisticController struct{}
  16. // Home
  17. // @Title 获取首页工作台数据
  18. // @Description 获取首页工作台数据接口
  19. // @Success 200 {object} response.WorktopResp
  20. // @router /statistic [get]
  21. func (this *StatisticController) Statistic(c *gin.Context) {
  22. //近6收入统计
  23. var incomeList models.IncomeChartResp
  24. ch := make(chan models.IncomeChartResp, 1)
  25. go getIncomeList(ch)
  26. for v := range ch {
  27. incomeList = v
  28. close(ch)
  29. }
  30. resp.OkData("获取成功", incomeList, c)
  31. }
  32. //获取收入统计
  33. func getIncomeList(ch chan models.IncomeChartResp) (incomeChart models.IncomeChartResp, err error) {
  34. defer func() {
  35. if err != nil {
  36. go alarm_msg.SendAlarmMsg("获取近两年的收入统计数据异常,Err:"+err.Error(), 3)
  37. //go utils.SendEmail(utils.APPNAME+"获取近12个月的收入统计数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  38. }
  39. ch <- incomeChart
  40. }()
  41. todayStr := utils.GetToday("20060102")
  42. key := "admin:home:fmsIncomeList:" + todayStr
  43. redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  44. if redisErr != nil {
  45. var dateSlice []string
  46. var contractMoneySlice []float64
  47. var ArrivalMoneySlice []float64
  48. yearNum := time.Now().Year() - time.Now().AddDate(-2, 0, 0).Year()
  49. monthNum := time.Now().Month() - 1
  50. numMonth := yearNum*12 + int(monthNum) //距离2021-01存在多少个月
  51. for i := numMonth; i >= 0; i-- {
  52. timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  53. dateSlice = append(dateSlice, timeNow.AddDate(0, -i, 0).Format("06/01"))
  54. //开始日期
  55. startDate := timeNow.AddDate(0, -i, 0).Format("2006-01")
  56. startDate = fmt.Sprint(startDate, "-01")
  57. //结束日期
  58. endDateTime := timeNow.AddDate(0, -i+1, 0)
  59. endDate := endDateTime.Format("2006-01")
  60. //因为就算是当月的后续事件还没到,也要计入数据统计,所以不做限制到当天处理
  61. //if endDateTime.After(time.Now()) {
  62. // endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  63. //} else {
  64. // endDate = fmt.Sprint(endDate, "-01")
  65. //}
  66. endDate = fmt.Sprint(endDate, "-01")
  67. cond := ` AND create_time >= '`+startDate +`' AND create_time < '`+endDate +`' `
  68. item, countErr := models.GetIncomeListCount(cond)
  69. if countErr != nil && countErr != utils.ErrNoRow {
  70. err = countErr
  71. return
  72. }
  73. contractMoneySlice = append(contractMoneySlice, item.ContractMoney)
  74. ArrivalMoneySlice = append(ArrivalMoneySlice, item.ArrivalMoney)
  75. }
  76. incomeChart.Title = "开票到款统计图"
  77. incomeChart.Date = dateSlice
  78. incomeChart.ContractMoney = contractMoneySlice
  79. incomeChart.ArrivalMoney = ArrivalMoneySlice
  80. redisJsonData, err := json.Marshal(incomeChart)
  81. if err == nil {
  82. global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  83. }
  84. } else {
  85. err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  86. if err != nil {
  87. fmt.Println("近两年的收入统计数据,json转换失败")
  88. }
  89. }
  90. return
  91. }