2020-08-01 16:15:40 +08:00
package service
import (
"database/sql"
"ferry/global/orm"
2021-03-12 00:15:01 +08:00
"ferry/models/process"
2021-03-10 23:14:28 +08:00
"ferry/pkg/pagination"
2021-03-14 21:46:07 +08:00
"fmt"
"strings"
"time"
2021-03-10 23:14:28 +08:00
"github.com/gin-gonic/gin"
2020-08-01 16:15:40 +08:00
)
/ *
@ Author : lanyulei
* /
2021-03-12 00:15:01 +08:00
type Ranks struct {
Name string ` json:"name" `
Total int ` json:"total" `
}
type Statistics struct {
StartTime string ` json:"start_time" `
EndTime string ` json:"end_time" `
}
func NewStatistics ( startTime string , endTime string ) * Statistics {
return & Statistics {
StartTime : startTime ,
EndTime : endTime ,
}
}
2021-03-13 23:57:54 +08:00
// 查询范围统计数据
func ( s * Statistics ) DateRangeStatistics ( ) ( statisticsData map [ string ] [ ] interface { } , err error ) {
2020-08-01 16:15:40 +08:00
var (
2021-03-14 21:46:07 +08:00
datetime string
total int
overs int
processing int
sqlValue string
rows * sql . Rows
startTime time . Time
endTime time . Time
TimeDifference int
sqlDataValue string
2020-08-01 16:15:40 +08:00
)
2021-03-14 21:46:07 +08:00
// 计算两个时间的差
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 {
2022-09-27 15:06:13 +08:00
sqlDataValue += fmt . Sprintf ( "SELECT date_sub(date_format( '%s', '%%Y%%m%%d'), INTERVAL 0 DAY ) AS click_date UNION ALL" , endTime )
2021-03-14 21:46:07 +08:00
} else if i == TimeDifference - 1 {
2022-09-27 15:06:13 +08:00
sqlDataValue += fmt . Sprintf ( ` SELECT date_sub(date_format( '%s', '%%Y%%m%%d'), INTERVAL %d DAY ) AS click_date UNION ALL ` , endTime , i )
sqlDataValue += fmt . Sprintf ( ` SELECT date_sub(date_format( '%s', '%%Y%%m%%d'), INTERVAL %d DAY ) AS click_date ` , endTime , i + 1 )
2021-03-14 21:46:07 +08:00
} else {
2022-09-27 15:06:13 +08:00
sqlDataValue += fmt . Sprintf ( ` SELECT date_sub(date_format( '%s', '%%Y%%m%%d'), INTERVAL %d DAY ) AS click_date UNION ALL ` , endTime , i )
2021-03-14 21:46:07 +08:00
}
}
2022-09-27 15:06:13 +08:00
if TimeDifference == 1 {
sqlDataValue += fmt . Sprintf ( " SELECT date_sub(date_format( '%s', '%%Y%%m%%d'), INTERVAL 1 DAY ) AS click_date" , endTime )
}
2021-03-14 21:46:07 +08:00
sqlValue = fmt . Sprintf ( ` SELECT
2020-08-01 16:15:40 +08:00
a . click_date ,
ifnull ( b . total , 0 ) AS total ,
2020-08-13 17:57:51 +08:00
ifnull ( b . overs , 0 ) AS overs ,
2020-08-01 16:15:40 +08:00
ifnull ( b . processing , 0 ) AS processing
FROM
2021-03-14 21:46:07 +08:00
( % s ) a
2020-08-01 16:15:40 +08:00
LEFT JOIN (
SELECT
a1 . datetime AS datetime ,
a1 . count AS total ,
2020-08-13 17:57:51 +08:00
b1 . count AS overs ,
2020-08-01 16:15:40 +08:00
c . count AS processing
FROM
(
SELECT
date ( create_time ) AS datetime ,
count ( * ) AS count
FROM
p_work_order_info
GROUP BY
date ( create_time ) ) a1
LEFT JOIN (
SELECT
date ( create_time ) AS datetime ,
count ( * ) AS count
FROM
p_work_order_info
WHERE
is_end = 1
GROUP BY
date ( create_time ) ) b1 ON a1 . datetime = b1 . datetime
LEFT JOIN (
SELECT
date ( create_time ) AS datetime ,
count ( * ) AS count
FROM
p_work_order_info
WHERE
is_end = 0
GROUP BY
date ( create_time ) ) c ON a1 . datetime = c . datetime
2021-03-14 21:46:07 +08:00
) b ON a . click_date = b . datetime order by a . click_date ; ` , sqlDataValue )
2020-08-01 16:15:40 +08:00
rows , err = orm . Eloquent . Raw ( sqlValue ) . Rows ( )
if err != nil {
return
}
defer func ( ) {
_ = rows . Close ( )
} ( )
statisticsData = map [ string ] [ ] interface { } { }
for rows . Next ( ) {
2020-08-13 17:57:51 +08:00
err = rows . Scan ( & datetime , & total , & overs , & processing )
2020-08-01 16:15:40 +08:00
if err != nil {
return
}
statisticsData [ "datetime" ] = append ( statisticsData [ "datetime" ] , datetime [ : 10 ] )
statisticsData [ "total" ] = append ( statisticsData [ "total" ] , total )
2020-08-13 17:57:51 +08:00
statisticsData [ "overs" ] = append ( statisticsData [ "overs" ] , overs )
2020-08-01 16:15:40 +08:00
statisticsData [ "processing" ] = append ( statisticsData [ "processing" ] , processing )
}
return
}
// 查询工单提交排名
2021-03-13 23:57:54 +08:00
func ( s * Statistics ) SubmitRanking ( ) ( submitRankingData map [ string ] [ ] interface { } , err error ) {
2020-08-01 16:15:40 +08:00
var (
userId int
username string
nickname string
rankingCount int
rows * sql . Rows
)
sqlValue := ` SELECT
creator AS user_id ,
sys_user . username AS username ,
sys_user . nick_name ,
COUNT ( * ) AS rankingCount
FROM
p_work_order_info
LEFT JOIN sys_user ON sys_user . user_id = p_work_order_info . creator
GROUP BY
p_work_order_info . creator ORDER BY rankingCount limit 6 ; `
rows , err = orm . Eloquent . Raw ( sqlValue ) . Rows ( )
if err != nil {
return
}
defer func ( ) {
_ = rows . Close ( )
} ( )
submitRankingData = map [ string ] [ ] interface { } { }
for rows . Next ( ) {
err = rows . Scan ( & userId , & username , & nickname , & rankingCount )
if err != nil {
return
}
submitRankingData [ "userId" ] = append ( submitRankingData [ "userId" ] , userId )
submitRankingData [ "username" ] = append ( submitRankingData [ "username" ] , username )
submitRankingData [ "nickname" ] = append ( submitRankingData [ "nickname" ] , nickname )
submitRankingData [ "rankingCount" ] = append ( submitRankingData [ "rankingCount" ] , rankingCount )
}
return
}
2021-03-10 23:14:28 +08:00
// 查询工单数量统计
2021-03-12 00:15:01 +08:00
func ( s * Statistics ) WorkOrderCount ( c * gin . Context ) ( countList map [ string ] int , err error ) {
2021-03-10 23:14:28 +08:00
var (
w * WorkOrder
result interface { }
)
countList = make ( map [ string ] int )
for _ , i := range [ ] int { 1 , 2 , 3 , 4 } {
w = NewWorkOrder ( i , c )
if i != 1 {
result , err = w . PureWorkOrderList ( )
if err != nil {
return
}
} else {
w = NewWorkOrder ( i , c )
result , err = w . WorkOrderList ( )
if err != nil {
return
}
}
if i == 1 {
countList [ "upcoming" ] = result . ( * pagination . Paginator ) . TotalCount
} else if i == 2 {
countList [ "my_create" ] = result . ( * pagination . Paginator ) . TotalCount
} else if i == 3 {
countList [ "related" ] = result . ( * pagination . Paginator ) . TotalCount
} else if i == 4 {
countList [ "all" ] = result . ( * pagination . Paginator ) . TotalCount
}
}
return
}
2021-03-12 00:15:01 +08:00
// 查询指定范围内的提交工单排名数据
func ( s * Statistics ) WorkOrderRanks ( ) ( ranks [ ] Ranks , err error ) {
err = orm . Eloquent . Model ( & process . WorkOrderInfo { } ) .
Joins ( "left join p_process_info on p_process_info.id = p_work_order_info.process" ) .
Select ( "p_process_info.name as name, count(p_work_order_info.id) as total" ) .
2021-03-13 23:57:54 +08:00
Where ( "p_work_order_info.create_time between ? and ?" , s . StartTime , s . EndTime ) .
2021-03-12 00:15:01 +08:00
Group ( "p_work_order_info.process" ) .
Order ( "total desc" ) .
Limit ( 10 ) .
Scan ( & ranks ) . Error
return
}
2021-03-14 01:10:24 +08:00
// 处理工单人员排行榜
func ( s * Statistics ) HandlePersonRank ( ) ( interface { } , error ) {
var (
err error
ranks [ ] struct {
UserID int ` json:"user_id" `
Username string ` json:"username" `
Nickname string ` json:"nickname" `
Count int ` json:"count" `
}
)
err = orm . Eloquent . Model ( & process . CirculationHistory { } ) .
Joins ( "left join sys_user on sys_user.user_id = p_work_order_circulation_history.processor_id" ) .
Where ( "p_work_order_circulation_history.source like 'receiveTask%' and p_work_order_circulation_history.status = 1 and p_work_order_circulation_history.create_time between ? and ?" , s . StartTime , s . EndTime ) .
Select ( "p_work_order_circulation_history.processor_id as user_id, p_work_order_circulation_history.processor as nickname, sys_user.username as username, count(p_work_order_circulation_history.id) as count" ) .
Group ( "p_work_order_circulation_history.processor, p_work_order_circulation_history.processor_id" ) .
2021-03-14 22:54:46 +08:00
Order ( "count desc" ) .
2021-03-14 01:10:24 +08:00
Scan ( & ranks ) . Error
return ranks , err
}
// 工单处理耗时排行榜
func ( s * Statistics ) HandlePeriodRank ( ) ( interface { } , error ) {
var (
err error
ranks [ ] struct {
2021-03-26 14:24:47 +08:00
UserID int ` json:"user_id" `
Username string ` json:"username" `
Nickname string ` json:"nickname" `
CostDuration float64 ` json:"cost_duration" `
2021-03-14 01:10:24 +08:00
}
)
err = orm . Eloquent . Model ( & process . CirculationHistory { } ) .
Joins ( "left join sys_user on sys_user.user_id = p_work_order_circulation_history.processor_id" ) .
Where ( "p_work_order_circulation_history.source like 'receiveTask%' and p_work_order_circulation_history.status = 1 and p_work_order_circulation_history.create_time between ? and ?" , s . StartTime , s . EndTime ) .
2021-03-26 13:30:52 +08:00
Select ( "p_work_order_circulation_history.processor_id as user_id, p_work_order_circulation_history.processor as nickname, sys_user.username as username, round(sum(p_work_order_circulation_history.cost_duration), 2) as cost_duration" ) .
2021-03-14 01:10:24 +08:00
Group ( "p_work_order_circulation_history.processor, p_work_order_circulation_history.processor_id" ) .
Order ( "cost_duration desc" ) .
Scan ( & ranks ) . Error
return ranks , err
}