123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package controller
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "lims-extend/common"
- "lims-extend/model"
- "lims-extend/response"
- )
- func TakeTask(c *gin.Context) {
- db := common.GetDB()
-
- taskId := c.Params.ByName("taskId")
-
- var task model.Task
- if err := db.Model(&model.Task{}).Where("id = ?", taskId).First(&task).Error; err != nil {
- response.Fail(c, nil, "Task不存在,请检查")
- return
- }
-
- if task.State != 3 {
- response.Fail(c, nil, "任务已被领取,请勿重新领取")
- return
- }
-
- task.UpdatedAt = model.Time{}
- task.State = 0
-
- if err := db.Model(&model.Task{}).Select("UpdateAt", "State").
- Where("id = ?", taskId).Updates(task).Error; err != nil {
- response.Fail(c, nil, "任务领取失败")
- return
- }
- response.Success(c, gin.H{"task": task}, "任务领取成功")
- }
- func ShowTasks(c *gin.Context) {
- softwareId := c.Query("software_id")
- executorId := c.Query("worker_id")
- isJoined := c.Query("is_joined")
- db := common.GetDB()
- type result struct {
- ID string `json:"id"`
- CreatTime model.Time `json:"create_time" gorm:"column:create_time"`
- Creator string `json:"creator"`
- CreatorId string `json:"creator_id" gorm:"column:creator_id"`
- WorkerId string `json:"executor_id" gorm:"column:executor_id"`
- Worker string `json:"worker"`
- Description string `json:"description"`
- SoftwareName string `json:"software_name"`
- SoftwareId string `json:"software_id" gorm:"column:software_id"`
- State int `json:"state"`
- Title string `json:"title"`
- UpdateTime model.Time `json:"update_time" gorm:"column:update_time"`
- Version string `json:"version"`
- Type string `json:"type"`
- IsJoined bool `json:"is_joined"`
- }
- var res []result
- sql := "SELECT task.id, task.create_time,plan.creator_id,task.executor_id," +
- "plan.description,software_id,task.state,plan.title,task.update_time," +
- "plan.version,plan.type from task,plan WHERE task.plan_id = plan.id and" +
- " executor_id = ? %s %s"
- if isJoined == "true" {
- sql = fmt.Sprintf(sql, "and task.state <> 3", "%s")
- } else if isJoined == "false" {
- sql = fmt.Sprintf(sql, "and task.state = 3", "%s")
- } else if isJoined == "" {
- sql = fmt.Sprintf(sql, "%s", "")
- }
- if softwareId != "" {
- sql = fmt.Sprintf(sql, "and software_id = ?")
- db.Raw(sql, executorId, softwareId).Find(&res)
- } else {
- sql = fmt.Sprintf(sql, "")
- db.Raw(sql, executorId).Find(&res)
- }
- type UserResult struct {
- UserName string `gorm:"column:username"`
- }
- for i, v := range res {
- var creator UserResult
- var worker UserResult
- db.Model(&model.User{}).Where("id = ?", v.CreatorId).First(&creator)
- res[i].Creator = creator.UserName
- db.Model(&model.User{}).Where("id = ?", v.WorkerId).First(&worker)
- res[i].Worker = worker.UserName
- if v.State != 3 {
- res[i].IsJoined = true
- }
- var software model.Software
- db.Model(&model.Software{}).Where("id = ?", v.SoftwareId).First(&software)
- res[i].SoftwareName = software.Name
- }
- response.Success(c, gin.H{"taskList": res}, "success")
- }
|