Commit 928a987b authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

optimize SQL query

parent cc651198
...@@ -136,11 +136,7 @@ func (d *Dao) IsTaskDone(tid int, userId string) (ok bool, err error) { ...@@ -136,11 +136,7 @@ func (d *Dao) IsTaskDone(tid int, userId string) (ok bool, err error) {
return count > 0, err return count > 0, err
} }
func (d *Dao) GetTaskResult(tid int, userId string) (status string, submittedAt time.Time, err error) { func (d *Dao) GetTaskResult(isDailyTask bool, tid int, userId string) (status string, submittedAt time.Time, err error) {
isDailyTask, err := d.IsDailyTask(tid)
if err != nil {
return "", time.Time{}, err
}
temp := &dbModel.TaskHistory{} temp := &dbModel.TaskHistory{}
tx := d.db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}) tx := d.db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)})
if !isDailyTask { if !isDailyTask {
......
...@@ -79,7 +79,18 @@ func checkTask(c *gin.Context) { ...@@ -79,7 +79,18 @@ func checkTask(c *gin.Context) {
return return
} }
status, _, err := srv.GetTaskResult(taskId, userId) task, err := srv.GetTaskDetail(taskId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
if task == nil {
c.JSON(200, withError("task not found"))
return
}
status, _, err := srv.GetTaskResult(task.Daily, taskId, userId)
if err != nil { if err != nil {
c.JSON(200, withError(constant.InternalError)) c.JSON(200, withError(constant.InternalError))
return return
...@@ -116,8 +127,7 @@ func submitTask(c *gin.Context) { ...@@ -116,8 +127,7 @@ func submitTask(c *gin.Context) {
c.JSON(200, withError("task not found")) c.JSON(200, withError("task not found"))
return return
} }
status, _, err := srv.GetTaskResult(task.Daily, taskId, userId)
status, _, err := srv.GetTaskResult(taskId, userId)
if err != nil { if err != nil {
c.JSON(200, withError(constant.InternalError)) c.JSON(200, withError(constant.InternalError))
return return
......
...@@ -74,7 +74,7 @@ func (s *Service) GetGroup(gid int, userId string, admin bool) (resp *apiModel.G ...@@ -74,7 +74,7 @@ func (s *Service) GetGroup(gid int, userId string, admin bool) (resp *apiModel.G
Enable: task.Enable, Enable: task.Enable,
} }
status, submittedAt, err := s.GetTaskResult(_task.TaskId.(int), userId) status, submittedAt, err := s.GetTaskResult(_task.Daily, _task.TaskId.(int), userId)
if err != nil { if err != nil {
log.WithError(err).Error("group check task") log.WithError(err).Error("group check task")
return nil, err return nil, err
......
...@@ -149,8 +149,8 @@ func (s *Service) GetTaskDetail(taskId int) (task *dbModel.Task, err error) { ...@@ -149,8 +149,8 @@ func (s *Service) GetTaskDetail(taskId int) (task *dbModel.Task, err error) {
return return
} }
func (s *Service) GetTaskResult(taskId int, userId string) (status string, submittedAt time.Time, err error) { func (s *Service) GetTaskResult(isDailyTask bool, taskId int, userId string) (status string, submittedAt time.Time, err error) {
status, submittedAt, err = s.d.GetTaskResult(taskId, userId) status, submittedAt, err = s.d.GetTaskResult(isDailyTask, taskId, userId)
if err != nil { if err != nil {
log.WithError(err).Error("get task result error") log.WithError(err).Error("get task result error")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment