statistic.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/models/fms"
  11. "hongze/fms_api/services/alarm_msg"
  12. "hongze/fms_api/utils"
  13. "strconv"
  14. "time"
  15. )
  16. // // StatisticController 统计报告
  17. type StatisticController struct{}
  18. // Home
  19. // @Title 获取首页工作台数据
  20. // @Description 获取首页工作台数据接口
  21. // @Success 200 {object} response.WorktopResp
  22. // @router /statistic [get]
  23. func (this *StatisticController) Statistic(c *gin.Context) {
  24. //近6收入统计
  25. var incomeList models.IncomeChartResp
  26. ch := make(chan models.IncomeChartResp, 1)
  27. go getIncomeListV2(ch)
  28. for v := range ch {
  29. incomeList = v
  30. close(ch)
  31. }
  32. resp.OkData("获取成功", incomeList, c)
  33. }
  34. // 获取收入统计
  35. func getIncomeList(ch chan models.IncomeChartResp) (incomeChart models.IncomeChartResp, err error) {
  36. defer func() {
  37. if err != nil {
  38. go alarm_msg.SendAlarmMsg("获取近两年的收入统计数据异常,Err:"+err.Error(), 3)
  39. //go utils.SendEmail(utils.APPNAME+"获取近12个月的收入统计数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  40. }
  41. ch <- incomeChart
  42. }()
  43. todayStr := utils.GetToday("20060102")
  44. key := "admin:home:fmsIncomeList:" + todayStr
  45. redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  46. if redisErr != nil {
  47. var dateSlice []string
  48. var contractMoneySlice []float64
  49. var ArrivalMoneySlice []float64
  50. yearNum := time.Now().Year() - time.Now().AddDate(-2, 0, 0).Year()
  51. monthNum := time.Now().Month() - 1
  52. numMonth := yearNum*12 + int(monthNum) //距离2021-01存在多少个月
  53. for i := numMonth; i >= 0; i-- {
  54. timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  55. dateSlice = append(dateSlice, timeNow.AddDate(0, -i, 0).Format("06/01"))
  56. //开始日期
  57. startDate := timeNow.AddDate(0, -i, 0).Format("2006-01")
  58. startDate = fmt.Sprint(startDate, "-01")
  59. //结束日期
  60. endDateTime := timeNow.AddDate(0, -i+1, 0)
  61. endDate := endDateTime.Format("2006-01")
  62. //因为就算是当月的后续事件还没到,也要计入数据统计,所以不做限制到当天处理
  63. //if endDateTime.After(time.Now()) {
  64. // endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  65. //} else {
  66. // endDate = fmt.Sprint(endDate, "-01")
  67. //}
  68. endDate = fmt.Sprint(endDate, "-01")
  69. cond := ` AND invoice_time >= '` + startDate + `' AND invoice_time < '` + endDate + `' `
  70. item, countErr := models.GetIncomeListCount(cond)
  71. if countErr != nil && countErr != utils.ErrNoRow {
  72. err = countErr
  73. return
  74. }
  75. contractMoneySlice = append(contractMoneySlice, item.ContractMoney)
  76. ArrivalMoneySlice = append(ArrivalMoneySlice, item.ArrivalMoney)
  77. }
  78. incomeChart.Title = "开票到款统计图"
  79. incomeChart.Date = dateSlice
  80. incomeChart.ContractMoney = contractMoneySlice
  81. incomeChart.ArrivalMoney = ArrivalMoneySlice
  82. redisJsonData, err := json.Marshal(incomeChart)
  83. if err == nil {
  84. global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  85. }
  86. } else {
  87. err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  88. if err != nil {
  89. fmt.Println("近两年的收入统计数据,json转换失败")
  90. }
  91. }
  92. return
  93. }
  94. func getIncomeListV2(ch chan models.IncomeChartResp) (incomeChart models.IncomeChartResp, err error) {
  95. defer func() {
  96. if err != nil {
  97. global.LOG.Error(err)
  98. go alarm_msg.SendAlarmMsg("获取近两年的收入统计数据异常,Err:"+err.Error(), 3)
  99. //go utils.SendEmail(utils.APPNAME+"获取近12个月的收入统计数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  100. }
  101. ch <- incomeChart
  102. }()
  103. todayStr := utils.GetToday("20060102")
  104. key := "admin:home:fmsIncomeList:" + todayStr
  105. redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  106. if redisErr != nil {
  107. var dateSlice []string
  108. var contractMoneySlice []float64
  109. var ArrivalMoneySlice []float64
  110. yearNum := time.Now().Year() - time.Now().AddDate(-2, 0, 0).Year()
  111. monthNum := time.Now().Month() - 1
  112. numMonth := yearNum*12 + int(monthNum) //距离2021-01存在多少个月
  113. for i := numMonth; i >= 0; i-- {
  114. timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  115. dateSlice = append(dateSlice, timeNow.AddDate(0, -i, 0).Format("06/01"))
  116. //开始日期
  117. startDate := timeNow.AddDate(0, -i, 0).Format("2006-01")
  118. startDate = fmt.Sprint(startDate, "-01")
  119. //结束日期
  120. endDateTime := timeNow.AddDate(0, -i+1, -1)
  121. endDate := endDateTime.Format(utils.FormatDate)
  122. //因为就算是当月的后续事件还没到,也要计入数据统计,所以不做限制到当天处理
  123. //if endDateTime.After(time.Now()) {
  124. // endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  125. //} else {
  126. // endDate = fmt.Sprint(endDate, "-01")
  127. //}
  128. cond := `1 = 1`
  129. pars := make([]interface{}, 0)
  130. st := fmt.Sprint(startDate, " 00:00:00")
  131. ed := fmt.Sprint(endDate, " 23:59:59")
  132. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  133. pars = append(pars, st, ed, st, ed)
  134. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  135. if e != nil {
  136. return
  137. }
  138. // 开票到款金额合计(换算后)
  139. var invoiceTotal, paymentTotal float64
  140. if len(summaryIds) > 0 {
  141. //amountTotalCond := `a.id IN ?`
  142. amountTotalCond := `a.id IN ? AND (b.invoice_time BETWEEN ? AND ?)`
  143. amountTotalPars := make([]interface{}, 0)
  144. amountTotalPars = append(amountTotalPars, summaryIds, st, ed)
  145. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  146. if e != nil {
  147. return
  148. }
  149. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  150. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  151. if e != nil {
  152. return
  153. }
  154. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  155. }
  156. contractMoneySlice = append(contractMoneySlice, invoiceTotal)
  157. ArrivalMoneySlice = append(ArrivalMoneySlice, paymentTotal)
  158. }
  159. incomeChart.Title = "开票到款统计图"
  160. incomeChart.Date = dateSlice
  161. incomeChart.ContractMoney = contractMoneySlice
  162. incomeChart.ArrivalMoney = ArrivalMoneySlice
  163. redisJsonData, err := json.Marshal(incomeChart)
  164. if err == nil {
  165. global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  166. }
  167. }else {
  168. err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  169. if err != nil {
  170. fmt.Println("近两年的收入统计数据,json转换失败")
  171. }
  172. }
  173. return
  174. }