資料會回傳各群組的日期跟時間加總
[GET] api/report/group_heatmap
回傳值:
"data": {
"report": [
{
"name": "A",
"date": "2020-09-16",
"total_spend_time": 0
}
},
"result": true
}
handler/api.go
// HeatMapReport HeatMapReport
type HeatMapReport struct {
ID int `json:"-"`
Name string `json:"name"`
Date string `json:"date"`
TotalSpendTime int `json:"total_spend_time"`
}
// GetTaskHeatMap GetTaskHeatMap
func GetTaskHeatMap(c *gin.Context) {
var heatmap []*HeatMapReport
userID := c.GetString("user_id")
groupIDstr, _ := c.GetQuery("group_id")
groupID, err := strconv.Atoi(groupIDstr)
if err != nil || groupID == 0 {
res.BadRequest(c, res.ErrParamsCode, gin.H{"msg": "group_id not found"})
return
}
nowTime := time.Now()
endDate := nowTime.Format(config.Val.TimeFormat)
startDate := nowTime.AddDate(0, 0, -7).Format(config.Val.TimeFormat)
dateStatistics := rangeDate(nowTime, 7)
tasks, err := model.TaskModel.GetTasksByParentID([]int{groupID}, userID, []int{model.TaskAble})
if err != nil {
log.WithFields(log.Fields{
"user_id": userID,
"origin_err": err.Error(),
}).Error("GetTasksByParentID error")
res.SystemError(c, res.ErrSystemCode, gin.H{})
return
}
data, err := model.RecordsModel.SumByDateByParentID(userID, groupID, startDate, endDate)
if err != nil {
log.WithFields(log.Fields{
"user_id": userID,
"origin_err": err.Error(),
}).Error("SumByDateByParentID error")
res.SystemError(c, res.ErrSystemCode, gin.H{})
return
}
for _, t := range tasks {
for _, ds := range dateStatistics {
heatmap = append(heatmap, &HeatMapReport{
ID: t.ID,
Name: t.Name,
Date: ds,
})
}
}
for _, d := range data {
t1, err := time.Parse(time.RFC3339, d.Date)
if err != nil {
log.WithFields(log.Fields{
"date": d.Date,
"origin_err": err.Error(),
}).Error("GetAllGroupHeatMap Parse error")
res.SystemError(c, res.ErrSystemCode, gin.H{})
return
}
parsedDate := t1.Format("2006-01-02")
for _, h := range heatmap {
if h.ID == d.TaskID && h.Date == parsedDate {
h.TotalSpendTime = d.SpendTime
break
}
}
}
res.Success(c, gin.H{
"report": heatmap,
})
}
試打一下
今天的commit
謝謝大家~