feat: 完善工单统计。

This commit is contained in:
Mr. Lan 2021-03-14 21:46:07 +08:00
parent 9b256b2caf
commit 833e93c2db
2 changed files with 38 additions and 24 deletions

View File

@ -4,6 +4,7 @@ import (
"ferry/pkg/service" "ferry/pkg/service"
"ferry/tools/app" "ferry/tools/app"
"fmt" "fmt"
"strings"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -32,6 +33,11 @@ func InitData(c *gin.Context) {
// 默认为最近7天的数据 // 默认为最近7天的数据
startTime = fmt.Sprintf("%s 00:00:00", time.Now().AddDate(0, 0, -6).Format("2006-01-02")) startTime = fmt.Sprintf("%s 00:00:00", time.Now().AddDate(0, 0, -6).Format("2006-01-02"))
endTime = fmt.Sprintf("%s 23:59:59", time.Now().Format("2006-01-02")) endTime = fmt.Sprintf("%s 23:59:59", time.Now().Format("2006-01-02"))
} else {
if strings.Contains(startTime, "T") && strings.Contains(endTime, "T") {
startTime = fmt.Sprintf("%s 00:00:00", strings.Split(startTime, "T")[0])
endTime = fmt.Sprintf("%s 23:59:59", strings.Split(endTime, "T")[0])
}
} }
statistics := service.Statistics{ statistics := service.Statistics{

View File

@ -5,6 +5,9 @@ import (
"ferry/global/orm" "ferry/global/orm"
"ferry/models/process" "ferry/models/process"
"ferry/pkg/pagination" "ferry/pkg/pagination"
"fmt"
"strings"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -33,35 +36,40 @@ func NewStatistics(startTime string, endTime string) *Statistics {
// 查询范围统计数据 // 查询范围统计数据
func (s *Statistics) DateRangeStatistics() (statisticsData map[string][]interface{}, err error) { func (s *Statistics) DateRangeStatistics() (statisticsData map[string][]interface{}, err error) {
var ( var (
datetime string datetime string
total int total int
overs int overs int
processing int processing int
sqlValue string sqlValue string
rows *sql.Rows rows *sql.Rows
startTime time.Time
endTime time.Time
TimeDifference int
sqlDataValue string
) )
sqlValue = `SELECT
// 计算两个时间的差
startTime, _ = time.Parse("2006-01-02 15:04:05", s.StartTime)
endTime, _ = time.Parse("2006-01-02 15:04:05", fmt.Sprintf("%s 00:00:00", strings.Split(s.EndTime, " ")[0]))
TimeDifference = int(endTime.Sub(startTime).Hours() / 24)
for i := 0; i < TimeDifference; i++ {
if i == 0 {
sqlDataValue += "SELECT curdate() AS click_date UNION ALL"
} else if i == TimeDifference-1 {
sqlDataValue += fmt.Sprintf(` SELECT date_sub( curdate(), INTERVAL %d DAY ) AS click_date`, i)
} else {
sqlDataValue += fmt.Sprintf(` SELECT date_sub( curdate(), INTERVAL %d DAY ) AS click_date UNION ALL`, i)
}
}
sqlValue = fmt.Sprintf(`SELECT
a.click_date, a.click_date,
ifnull( b.total, 0 ) AS total, ifnull( b.total, 0 ) AS total,
ifnull( b.overs, 0 ) AS overs, ifnull( b.overs, 0 ) AS overs,
ifnull( b.processing, 0 ) AS processing ifnull( b.processing, 0 ) AS processing
FROM FROM
( (%s) a
SELECT
curdate() AS click_date UNION ALL
SELECT
date_sub( curdate(), INTERVAL 1 DAY ) AS click_date UNION ALL
SELECT
date_sub( curdate(), INTERVAL 2 DAY ) AS click_date UNION ALL
SELECT
date_sub( curdate(), INTERVAL 3 DAY ) AS click_date UNION ALL
SELECT
date_sub( curdate(), INTERVAL 4 DAY ) AS click_date UNION ALL
SELECT
date_sub( curdate(), INTERVAL 5 DAY ) AS click_date UNION ALL
SELECT
date_sub( curdate(), INTERVAL 6 DAY ) AS click_date
) a
LEFT JOIN ( LEFT JOIN (
SELECT SELECT
a1.datetime AS datetime, a1.datetime AS datetime,
@ -97,7 +105,7 @@ func (s *Statistics) DateRangeStatistics() (statisticsData map[string][]interfac
is_end = 0 is_end = 0
GROUP BY GROUP BY
date( create_time )) c ON a1.datetime = c.datetime date( create_time )) c ON a1.datetime = c.datetime
) b ON a.click_date = b.datetime order by a.click_date;` ) b ON a.click_date = b.datetime order by a.click_date;`, sqlDataValue)
rows, err = orm.Eloquent.Raw(sqlValue).Rows() rows, err = orm.Eloquent.Raw(sqlValue).Rows()
if err != nil { if err != nil {
return return