作者 linmadan

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

... ... @@ -463,6 +463,21 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
} else {
taskRepository = value
}
if createTaskCommand.ReferenceResourceItems != nil && len(createTaskCommand.ReferenceResourceItems) > 0 {
var referenceResourceIds []int64
for _, referenceResourceItem := range createTaskCommand.ReferenceResourceItems {
referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
}
queryOptions := make(map[string]interface{})
queryOptions["referenceResourceIds"] = referenceResourceIds
if count, _, err := taskRepository.Find(queryOptions); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "问题资源已经被引用,创建任务失败")
}
}
}
if task, err := taskRepository.Save(newTask); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -553,6 +568,26 @@ func (taskService *TaskService) UpdateTask(updateTaskCommand *command.UpdateTask
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateTaskCommand.TaskId)))
}
if updateTaskCommand.ReferenceResourceItems != nil && len(updateTaskCommand.ReferenceResourceItems) > 0 {
var referenceResourceIds []int64
for _, referenceResourceItem := range updateTaskCommand.ReferenceResourceItems {
referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
}
queryOptions := make(map[string]interface{})
queryOptions["referenceResourceIds"] = referenceResourceIds
if count, tasks, err := taskRepository.Find(queryOptions); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 1 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "问题资源已经被引用,创建任务失败")
}
if count == 1 {
if tasks[0].TaskId != task.TaskId {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "问题资源已经被引用,创建任务失败")
}
}
}
}
if err := task.Update(tool_funs.SimpleStructToMap(updateTaskCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
... ...
... ... @@ -165,7 +165,7 @@ func (dao *TaskDao) CalculatePersonTask(uid int64) (*domain.PersonTaskStatistics
personTaskStatistics.CompletedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id").
Where(`bidder_info.Bidder @> '{"uid":?}'`, uid).
Where(`bidder_info.bidder @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
... ...
... ... @@ -146,7 +146,7 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
}
if bidder, ok := queryOptions["bidder"]; ok && (bidder != int64(0)) {
query = query.Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id")
query = query.Where(`bidder_info.Bidder @> '{"uid":?}'`, bidder)
query = query.Where(`bidder_info.bidder @> '{"uid":?}'`, bidder)
}
if receiver, ok := queryOptions["receiver"]; ok && (receiver != int64(0)) {
query = query.Where(`task.receiver_uid = ?`, receiver)
... ... @@ -166,6 +166,14 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
query = query.Where("off_task_record.create_time < ?", offEndTime)
}
}
if referenceResourceIds, ok := queryOptions["referenceResourceIds"]; ok && len(referenceResourceIds.([]int64)) != 0 {
query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
for _, value := range referenceResourceIds.([]int64) {
q = q.WhereOr(`task.reference_resource @> '{"referenceResourceItems":[{"referenceResourceId":?}]}'`, value)
}
return q, nil
})
}
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
... ...
... ... @@ -2,6 +2,7 @@ package task
import (
"github.com/go-pg/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"net/http"
"time"
... ... @@ -18,6 +19,20 @@ var _ = Describe("创建新任务", func() {
"INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?)",
1, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.QueryOne(
pg.Scan(),
"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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
2, 101, "抢单任务2", 1, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 1, &domain.ReferenceResource{
ReferenceResourceType: 1,
ReferenceResourceItems: []*domain.ReferenceResourceItem{
{
ReferenceResourceId: 4,
},
},
}, pg.Array([]string{"口感", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), false, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now())
Expect(err1).NotTo(HaveOccurred())
})
Describe("提交数据创建新任务", func() {
Context("提交正确的抢单类型任务数据", func() {
... ... @@ -30,9 +45,9 @@ var _ = Describe("创建新任务", func() {
"sponsor": 2499036607974745088,
"referenceResourceType": 1,
"referenceResourceItems": []map[string]interface{}{
{"serialNumber": 1, "title": "问题标题1"},
{"serialNumber": 2, "title": "问题标题2"},
{"serialNumber": 3, "title": "问题标题3"},
{"referenceResourceId": 1, "serialNumber": 1, "title": "问题标题1"},
{"referenceResourceId": 2, "serialNumber": 2, "title": "问题标题2"},
{"referenceResourceId": 3, "serialNumber": 3, "title": "问题标题3"},
},
"customerValue": []string{
"口味",
... ... @@ -107,6 +122,44 @@ var _ = Describe("创建新任务", func() {
ContainsKey("taskId").ValueNotEqual("taskId", BeZero())
})
})
Context("提交已经被引用的问题ID任务数据", func() {
It("创建失败", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": 101,
"taskName": "抢单任务",
"taskType": 1,
"sponsor": 2499036607974745088,
"referenceResourceType": 1,
"referenceResourceItems": []map[string]interface{}{
{"referenceResourceId": 4, "serialNumber": 4, "title": "问题标题4"},
},
"customerValue": []string{
"口味",
"色泽",
"商务服务",
},
"taskNature": "线",
"suMoney": 1000.00,
"acceptanceStandard": "验收标准",
"taskDescription": "任务描述",
"taskPictureUrls": []string{
"url-1",
"url-2",
"url-3",
},
"isRewardTake": false,
}
httpExpect.POST("/tasks/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 501).
ContainsKey("msg").ValueEqual("msg", "内部服务出错:问题资源已经被引用,创建任务失败")
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM tasks WHERE true")
... ...