Browse Source

添加了任务列表显示功能(基于三种过滤条件)/其他细节调整

bigcat 2 years ago
parent
commit
aace1f2799
6 changed files with 126 additions and 51 deletions
  1. 47 42
      controller/plan.go
  2. 67 0
      controller/task.go
  3. 4 2
      middleware/AuthMiddleware.go
  4. 4 0
      model/software.go
  5. 1 0
      router.go
  6. 3 7
      vo/planVo.go

+ 47 - 42
controller/plan.go

@@ -19,18 +19,17 @@ func ShowPlans(c *gin.Context) {
 		Joins("left join group_users on plan_groups.group_id = group_users.group_id").
 		Where("user_id = ? and plan.delete <> 1", userId).Distinct().Find(&plans)
 	planVos := make([]vo.PlanVo, len(plans))
-	for i, v := range plans {
-		planVos[i] = vo.PlanVo{
-			Plan:     v,
-			IsJoined: IsJoinedInCurrentPlan(userId, v.Id),
-		}
-	}
+	//for i, v := range plans {
+	//	planVos[i] = vo.PlanVo{
+	//		Plan:     v,
+	//		IsJoined: IsJoinedInCurrentPlan(userId, v.Id),
+	//	}
+	//}
 	response.Success(c, gin.H{"planlists": planVos}, "success")
 }
 
 func ShowPlan(c *gin.Context) {
 	planId := c.Params.ByName("planId")
-	userId := c.Params.ByName("userId")
 	db := common.GetDB()
 	var plan model.Plan
 	if err := db.Model(&model.Plan{}).Where("id = ?", planId).First(&plan).Error; err != nil {
@@ -41,22 +40,14 @@ func ShowPlan(c *gin.Context) {
 		response.Fail(c, nil, "当前计划已经被删除")
 		return
 	}
-	var file model.File
-	db.Model(&model.File{}).Where("id = ?", plan.StatementFile).First(&file)
-	var user model.User
-	db.Model(&model.User{}).Where("id = ?", plan.CreatorId).First(&user)
 
 	planVo := vo.PlanVo{
-		Plan:          plan,
-		IsJoined:      IsJoinedInCurrentPlan(userId, planId),
-		SendAmount:    GetPlanTotalUserAmount(planId),
-		ReceiveAmount: GetPlanTaskAmount(planId),
+		SendAmount:    GetSendPacketAmount(planId),
+		ReceiveAmount: GetRecvPacketAmount(planId),
 		ReportAmount:  GetPlanReportAmount(planId),
 	}
 	response.Success(c, gin.H{
-		"plan":    planVo,
-		"creator": user,
-		"file":    file,
+		"plan": planVo,
 	}, "success")
 
 }
@@ -256,6 +247,27 @@ func Test(c *gin.Context) {
 // GetPlanTotalUserAmount 获取当前计划总发包数
 // 需要参数planId
 
+func GetSendPacketAmount(planId string) int {
+	db := common.GetDB()
+	var amount int64
+	db.Model(&model.Task{}).Where("plan_id = ?", planId).Count(&amount)
+	return int(amount)
+}
+
+func GetRecvPacketAmount(planId string) int {
+	db := common.GetDB()
+	var amount int64
+	db.Model(&model.Task{}).Where("plan_id = ? and state <> 3", planId).Count(&amount)
+	return int(amount)
+}
+
+func GetPlanReportAmount(planId string) int {
+	var count int64
+	db := common.GetDB()
+	db.Model(&model.Task{}).Where("plan_id = ? and state = 2", planId).Count(&count)
+	return int(count)
+}
+
 func GetPlanTotalUserAmount(planId string) int {
 	totalAmount := 0
 	var plan2groups []model.Plan2Group
@@ -280,27 +292,20 @@ func GetGroupUserAmount(groupId string) int {
 // GetGroupUserAmount 获取指定计划对接包数量
 // 需要参数groupId
 
-func GetPlanTaskAmount(planId string) int {
-	var count int64
-	db := common.GetDB()
-	db.Model(&model.Task{}).Where("plan_id = ?", planId).Count(&count)
-	return int(count)
-}
-
-func IsJoinedInCurrentPlan(userId string, planId string) bool {
-	var count int64
-	db := common.GetDB()
-	db.Model(&model.Task{}).Where("plan_id = ? and executor_id = ?", planId, userId).Count(&count)
-	if count > 0 {
-		return true
-	} else {
-		return false
-	}
-}
-
-func GetPlanReportAmount(planId string) int {
-	var count int64
-	db := common.GetDB()
-	db.Model(&model.Task{}).Where("plan_id = ? and state = 2", planId).Count(&count)
-	return int(count)
-}
+//func GetPlanTaskAmount(planId string) int {
+//	var count int64
+//	db := common.GetDB()
+//	db.Model(&model.Task{}).Where("plan_id = ?", planId).Count(&count)
+//	return int(count)
+//}
+//
+//func IsJoinedInCurrentPlan(userId string, planId string) bool {
+//	var count int64
+//	db := common.GetDB()
+//	db.Model(&model.Task{}).Where("plan_id = ? and executor_id = ?", planId, userId).Count(&count)
+//	if count > 0 {
+//		return true
+//	} else {
+//		return false
+//	}
+//}

+ 67 - 0
controller/task.go

@@ -1,6 +1,7 @@
 package controller
 
 import (
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"lims-extend/common"
 	"lims-extend/model"
@@ -40,3 +41,69 @@ func TakeTask(c *gin.Context) {
 	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")
+}

+ 4 - 2
middleware/AuthMiddleware.go

@@ -26,10 +26,12 @@ func AuthMiddleware() gin.HandlerFunc {
 		accessToken := c.GetHeader("accessToken")
 		client := &http.Client{Timeout: 1 * time.Second}
 		req, _ := http.NewRequest("GET",
-			fmt.Sprintf("http://%s:%s/api/v1/users/TestLaboratory_V1_User_1", pyBackendIp, pyPort), nil)
+			fmt.Sprintf("http://%s:%s/api/v1/users/TestLaboratory_V1_User_1?format=json", pyBackendIp, pyPort), nil)
 		req.Header.Add("accessToken", accessToken)
 		resp, err := client.Do(req)
-		if err != nil || resp.StatusCode != 200 {
+		if err != nil {
+			fmt.Println(err)
+			fmt.Println(resp.StatusCode)
 			response.Fail(c, nil, "无法连接Python_Backend权限认证服务器")
 			c.Abort()
 			return

+ 4 - 0
model/software.go

@@ -10,3 +10,7 @@ type Software struct {
 	CreatedAt  Time   `json:"created_at" gorm:"column:create_time;type:datetime default CURRENT_TIMESTAMP"`
 	UpdatedAt  Time   `json:"updated_at" gorm:"column:update_time;type:datetime default CURRENT_TIMESTAMP"`
 }
+
+func (Software) TableName() string {
+	return "software"
+}

+ 1 - 0
router.go

@@ -21,6 +21,7 @@ func CollectRoute(r *gin.Engine) *gin.Engine {
 	task := r.Group("/api/v-go/task")
 	{
 		task.POST("take/:taskId", controller.TakeTask)
+		task.GET("lists", controller.ShowTasks)
 	}
 
 	software := r.Group("/api/v-go/software")

+ 3 - 7
vo/planVo.go

@@ -1,11 +1,7 @@
 package vo
 
-import "lims-extend/model"
-
 type PlanVo struct {
-	model.Plan
-	IsJoined      bool `json:"is_joined"`
-	SendAmount    int  `json:"send_amount"`
-	ReceiveAmount int  `json:"receive_amount"`
-	ReportAmount  int  `json:"report_amount"`
+	SendAmount    int `json:"send_amount"`
+	ReceiveAmount int `json:"receive_amount"`
+	ReportAmount  int `json:"report_amount"`
 }