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/models/fms" "hongze/fms_api/services/alarm_msg" "hongze/fms_api/utils" "strconv" "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 getIncomeListV2(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 invoice_time >= '` + startDate + `' AND invoice_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 } func getIncomeListV2(ch chan models.IncomeChartResp) (incomeChart models.IncomeChartResp, err error) { defer func() { if err != nil { global.LOG.Error(err) 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, -1) endDate := endDateTime.Format(utils.FormatDate) //因为就算是当月的后续事件还没到,也要计入数据统计,所以不做限制到当天处理 //if endDateTime.After(time.Now()) { // endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate) //} else { // endDate = fmt.Sprint(endDate, "-01") //} cond := `1 = 1` pars := make([]interface{}, 0) st := fmt.Sprint(startDate, " 00:00:00") ed := fmt.Sprint(endDate, " 23:59:59") cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))` pars = append(pars, st, ed, st, ed) summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars) if e != nil { return } // 开票到款金额合计(换算后) var invoiceTotal, paymentTotal float64 if len(summaryIds) > 0 { //amountTotalCond := `a.id IN ?` amountTotalCond := `a.id IN ? AND (b.invoice_time BETWEEN ? AND ?)` amountTotalPars := make([]interface{}, 0) amountTotalPars = append(amountTotalPars, summaryIds, st, ed) invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1) if e != nil { return } invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64) paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2) if e != nil { return } paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64) } contractMoneySlice = append(contractMoneySlice, invoiceTotal) ArrivalMoneySlice = append(ArrivalMoneySlice, paymentTotal) } 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 }