作者 linmadan

重构任务创建与更新时候对引用资源的检查

@@ -463,6 +463,21 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask @@ -463,6 +463,21 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
463 } else { 463 } else {
464 taskRepository = value 464 taskRepository = value
465 } 465 }
  466 + if createTaskCommand.ReferenceResourceItems != nil && len(createTaskCommand.ReferenceResourceItems) > 0 {
  467 + var referenceResourceIds []int64
  468 + for _, referenceResourceItem := range createTaskCommand.ReferenceResourceItems {
  469 + referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
  470 + }
  471 + queryOptions := make(map[string]interface{})
  472 + queryOptions["referenceResourceIds"] = referenceResourceIds
  473 + if count, _, err := taskRepository.Find(queryOptions); err != nil {
  474 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  475 + } else {
  476 + if count > 0 {
  477 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "问题资源已经被引用,创建任务失败")
  478 + }
  479 + }
  480 + }
466 if task, err := taskRepository.Save(newTask); err != nil { 481 if task, err := taskRepository.Save(newTask); err != nil {
467 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 482 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
468 } else { 483 } else {
@@ -553,6 +568,26 @@ func (taskService *TaskService) UpdateTask(updateTaskCommand *command.UpdateTask @@ -553,6 +568,26 @@ func (taskService *TaskService) UpdateTask(updateTaskCommand *command.UpdateTask
553 if task == nil { 568 if task == nil {
554 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateTaskCommand.TaskId))) 569 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateTaskCommand.TaskId)))
555 } 570 }
  571 + if updateTaskCommand.ReferenceResourceItems != nil && len(updateTaskCommand.ReferenceResourceItems) > 0 {
  572 + var referenceResourceIds []int64
  573 + for _, referenceResourceItem := range updateTaskCommand.ReferenceResourceItems {
  574 + referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
  575 + }
  576 + queryOptions := make(map[string]interface{})
  577 + queryOptions["referenceResourceIds"] = referenceResourceIds
  578 + if count, tasks, err := taskRepository.Find(queryOptions); err != nil {
  579 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  580 + } else {
  581 + if count > 1 {
  582 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "问题资源已经被引用,创建任务失败")
  583 + }
  584 + if count == 1 {
  585 + if tasks[0].TaskId != task.TaskId {
  586 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "问题资源已经被引用,创建任务失败")
  587 + }
  588 + }
  589 + }
  590 + }
556 if err := task.Update(tool_funs.SimpleStructToMap(updateTaskCommand)); err != nil { 591 if err := task.Update(tool_funs.SimpleStructToMap(updateTaskCommand)); err != nil {
557 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 592 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
558 } 593 }
@@ -165,7 +165,7 @@ func (dao *TaskDao) CalculatePersonTask(uid int64) (*domain.PersonTaskStatistics @@ -165,7 +165,7 @@ func (dao *TaskDao) CalculatePersonTask(uid int64) (*domain.PersonTaskStatistics
165 personTaskStatistics.CompletedAsSponsor = int64(count) 165 personTaskStatistics.CompletedAsSponsor = int64(count)
166 } 166 }
167 if count, err := tx.Model(taskModel).Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id"). 167 if count, err := tx.Model(taskModel).Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id").
168 - Where(`bidder_info.Bidder @> '{"uid":?}'`, uid). 168 + Where(`bidder_info.bidder @> '{"uid":?}'`, uid).
169 Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED). 169 Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
170 Count(); err != nil { 170 Count(); err != nil {
171 return nil, err 171 return nil, err
@@ -146,7 +146,7 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int @@ -146,7 +146,7 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
146 } 146 }
147 if bidder, ok := queryOptions["bidder"]; ok && (bidder != int64(0)) { 147 if bidder, ok := queryOptions["bidder"]; ok && (bidder != int64(0)) {
148 query = query.Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id") 148 query = query.Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id")
149 - query = query.Where(`bidder_info.Bidder @> '{"uid":?}'`, bidder) 149 + query = query.Where(`bidder_info.bidder @> '{"uid":?}'`, bidder)
150 } 150 }
151 if receiver, ok := queryOptions["receiver"]; ok && (receiver != int64(0)) { 151 if receiver, ok := queryOptions["receiver"]; ok && (receiver != int64(0)) {
152 query = query.Where(`task.receiver_uid = ?`, receiver) 152 query = query.Where(`task.receiver_uid = ?`, receiver)
@@ -166,6 +166,14 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int @@ -166,6 +166,14 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
166 query = query.Where("off_task_record.create_time < ?", offEndTime) 166 query = query.Where("off_task_record.create_time < ?", offEndTime)
167 } 167 }
168 } 168 }
  169 + if referenceResourceIds, ok := queryOptions["referenceResourceIds"]; ok && len(referenceResourceIds.([]int64)) != 0 {
  170 + query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
  171 + for _, value := range referenceResourceIds.([]int64) {
  172 + q = q.WhereOr(`task.reference_resource @> '{"referenceResourceItems":[{"referenceResourceId":?}]}'`, value)
  173 + }
  174 + return q, nil
  175 + })
  176 + }
169 if offset, ok := queryOptions["offset"]; ok { 177 if offset, ok := queryOptions["offset"]; ok {
170 offset := offset.(int) 178 offset := offset.(int)
171 if offset > -1 { 179 if offset > -1 {
@@ -2,6 +2,7 @@ package task @@ -2,6 +2,7 @@ package task
2 2
3 import ( 3 import (
4 "github.com/go-pg/pg" 4 "github.com/go-pg/pg"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
5 "net/http" 6 "net/http"
6 "time" 7 "time"
7 8
@@ -18,6 +19,20 @@ var _ = Describe("创建新任务", func() { @@ -18,6 +19,20 @@ var _ = Describe("创建新任务", func() {
18 "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?)", 19 "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?)",
19 1, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0) 20 1, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
20 Expect(err).NotTo(HaveOccurred()) 21 Expect(err).NotTo(HaveOccurred())
  22 + _, err1 := pG.DB.QueryOne(
  23 + pg.Scan(),
  24 + "INSERT INTO tasks (id, company_id, task_name, task_type, sponsor, task_status, reference_resource, customer_value, task_nature, su_money, acceptance_standard, task_description, task_picture_urls, is_reward_take, participators, task_percentage, solve_report, solve_picture_urls, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  25 + 2, 101, "抢单任务2", 1, &domain.EmployeeInfo{
  26 + Uid: 2499036607974745088,
  27 + }, 1, &domain.ReferenceResource{
  28 + ReferenceResourceType: 1,
  29 + ReferenceResourceItems: []*domain.ReferenceResourceItem{
  30 + {
  31 + ReferenceResourceId: 4,
  32 + },
  33 + },
  34 + }, pg.Array([]string{"口感", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), false, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now())
  35 + Expect(err1).NotTo(HaveOccurred())
21 }) 36 })
22 Describe("提交数据创建新任务", func() { 37 Describe("提交数据创建新任务", func() {
23 Context("提交正确的抢单类型任务数据", func() { 38 Context("提交正确的抢单类型任务数据", func() {
@@ -30,9 +45,9 @@ var _ = Describe("创建新任务", func() { @@ -30,9 +45,9 @@ var _ = Describe("创建新任务", func() {
30 "sponsor": 2499036607974745088, 45 "sponsor": 2499036607974745088,
31 "referenceResourceType": 1, 46 "referenceResourceType": 1,
32 "referenceResourceItems": []map[string]interface{}{ 47 "referenceResourceItems": []map[string]interface{}{
33 - {"serialNumber": 1, "title": "问题标题1"},  
34 - {"serialNumber": 2, "title": "问题标题2"},  
35 - {"serialNumber": 3, "title": "问题标题3"}, 48 + {"referenceResourceId": 1, "serialNumber": 1, "title": "问题标题1"},
  49 + {"referenceResourceId": 2, "serialNumber": 2, "title": "问题标题2"},
  50 + {"referenceResourceId": 3, "serialNumber": 3, "title": "问题标题3"},
36 }, 51 },
37 "customerValue": []string{ 52 "customerValue": []string{
38 "口味", 53 "口味",
@@ -107,6 +122,44 @@ var _ = Describe("创建新任务", func() { @@ -107,6 +122,44 @@ var _ = Describe("创建新任务", func() {
107 ContainsKey("taskId").ValueNotEqual("taskId", BeZero()) 122 ContainsKey("taskId").ValueNotEqual("taskId", BeZero())
108 }) 123 })
109 }) 124 })
  125 + Context("提交已经被引用的问题ID任务数据", func() {
  126 + It("创建失败", func() {
  127 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  128 + body := map[string]interface{}{
  129 + "companyId": 101,
  130 + "taskName": "抢单任务",
  131 + "taskType": 1,
  132 + "sponsor": 2499036607974745088,
  133 + "referenceResourceType": 1,
  134 + "referenceResourceItems": []map[string]interface{}{
  135 + {"referenceResourceId": 4, "serialNumber": 4, "title": "问题标题4"},
  136 + },
  137 + "customerValue": []string{
  138 + "口味",
  139 + "色泽",
  140 + "商务服务",
  141 + },
  142 + "taskNature": "线",
  143 + "suMoney": 1000.00,
  144 + "acceptanceStandard": "验收标准",
  145 + "taskDescription": "任务描述",
  146 + "taskPictureUrls": []string{
  147 + "url-1",
  148 + "url-2",
  149 + "url-3",
  150 + },
  151 + "isRewardTake": false,
  152 + }
  153 + httpExpect.POST("/tasks/").
  154 + WithJSON(body).
  155 + Expect().
  156 + Status(http.StatusOK).
  157 + JSON().
  158 + Object().
  159 + ContainsKey("code").ValueEqual("code", 501).
  160 + ContainsKey("msg").ValueEqual("msg", "内部服务出错:问题资源已经被引用,创建任务失败")
  161 + })
  162 + })
110 }) 163 })
111 AfterEach(func() { 164 AfterEach(func() {
112 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true") 165 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true")