123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package controller
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "hongze/fms_api/controller/resp"
- "hongze/fms_api/global"
- "hongze/fms_api/models"
- "hongze/fms_api/services/alarm_msg"
- "hongze/fms_api/utils"
- "time"
- )
- // // StatisticController 统计报告
- type StatisticController struct{}
- // Home
- // @Title 获取首页工作台数据
- // @Description 获取首页工作台数据接口
- // @Success 200 {object} response.WorktopResp
- // @router /statistic [get]
- func (this *StatisticController) Statistic(c *gin.Context) {
- //近6收入统计
- var incomeList models.IncomeChartResp
- ch := make(chan models.IncomeChartResp, 1)
- go getIncomeList(ch)
- for v := range ch {
- incomeList = v
- close(ch)
- }
- resp.OkData("获取成功", incomeList, c)
- }
- //获取收入统计
- func getIncomeList(ch chan models.IncomeChartResp) (incomeChart models.IncomeChartResp, err error) {
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("获取近两年的收入统计数据异常,Err:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"获取近12个月的收入统计数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
- }
- ch <- incomeChart
- }()
- todayStr := utils.GetToday("20060102")
- key := "admin:home:fmsIncomeList:" + todayStr
- redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
- if redisErr != nil {
- var dateSlice []string
- var contractMoneySlice []float64
- var ArrivalMoneySlice []float64
- yearNum := time.Now().Year() - time.Now().AddDate(-2, 0, 0).Year()
- monthNum := time.Now().Month() - 1
- numMonth := yearNum*12 + int(monthNum) //距离2021-01存在多少个月
- for i := numMonth; i >= 0; i-- {
- timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
- dateSlice = append(dateSlice, timeNow.AddDate(0, -i, 0).Format("06/01"))
- //开始日期
- startDate := timeNow.AddDate(0, -i, 0).Format("2006-01")
- startDate = fmt.Sprint(startDate, "-01")
- //结束日期
- endDateTime := timeNow.AddDate(0, -i+1, 0)
- endDate := endDateTime.Format("2006-01")
- //因为就算是当月的后续事件还没到,也要计入数据统计,所以不做限制到当天处理
- //if endDateTime.After(time.Now()) {
- // endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
- //} else {
- // endDate = fmt.Sprint(endDate, "-01")
- //}
- endDate = fmt.Sprint(endDate, "-01")
- cond := ` AND create_time >= '`+startDate +`' AND create_time < '`+endDate +`' `
- item, countErr := models.GetIncomeListCount(cond)
- if countErr != nil && countErr != utils.ErrNoRow {
- err = countErr
- return
- }
- contractMoneySlice = append(contractMoneySlice, item.ContractMoney)
- ArrivalMoneySlice = append(ArrivalMoneySlice, item.ArrivalMoney)
- }
- incomeChart.Title = "开票到款统计图"
- incomeChart.Date = dateSlice
- incomeChart.ContractMoney = contractMoneySlice
- incomeChart.ArrivalMoney = ArrivalMoneySlice
- redisJsonData, err := json.Marshal(incomeChart)
- if err == nil {
- global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
- }
- } else {
- err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
- if err != nil {
- fmt.Println("近两年的收入统计数据,json转换失败")
- }
- }
- return
- }
|