作者 linmadan

完成选择竞标任务的中标人接口

@@ -37,3 +37,11 @@ func CreateBidTaskService(options map[string]interface{}) (service.BidTaskServic @@ -37,3 +37,11 @@ func CreateBidTaskService(options map[string]interface{}) (service.BidTaskServic
37 } 37 }
38 return domainService.NewBidTaskService(transactionContext) 38 return domainService.NewBidTaskService(transactionContext)
39 } 39 }
  40 +
  41 +func CreateChooseSuccessfulBidderService(options map[string]interface{}) (service.ChooseSuccessfulBidderService, error) {
  42 + var transactionContext *pgTransaction.TransactionContext
  43 + if value, ok := options["transactionContext"]; ok {
  44 + transactionContext = value.(*pgTransaction.TransactionContext)
  45 + }
  46 + return domainService.NewChooseSuccessfulBidderService(transactionContext)
  47 +}
@@ -97,10 +97,22 @@ func (taskService *TaskService) ChooseSuccessfulBidder(chooseSuccessfulBidderCom @@ -97,10 +97,22 @@ func (taskService *TaskService) ChooseSuccessfulBidder(chooseSuccessfulBidderCom
97 defer func() { 97 defer func() {
98 transactionContext.RollbackTransaction() 98 transactionContext.RollbackTransaction()
99 }() 99 }()
  100 + var chooseSuccessfulBidderService service.ChooseSuccessfulBidderService
  101 + if value, err := factory.CreateChooseSuccessfulBidderService(map[string]interface{}{
  102 + "transactionContext": transactionContext,
  103 + }); err != nil {
  104 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  105 + } else {
  106 + chooseSuccessfulBidderService = value
  107 + }
  108 + if task, err := chooseSuccessfulBidderService.Choose(chooseSuccessfulBidderCommand.TaskId, chooseSuccessfulBidderCommand.SuccessfulBidder, chooseSuccessfulBidderCommand.Operator); err != nil {
  109 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  110 + } else {
100 if err := transactionContext.CommitTransaction(); err != nil { 111 if err := transactionContext.CommitTransaction(); err != nil {
101 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 112 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
102 } 113 }
103 - return nil, nil 114 + return task, nil
  115 + }
104 } 116 }
105 117
106 // 申请完成任务 118 // 申请完成任务
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  4 +
  5 +type ChooseSuccessfulBidderService interface {
  6 + Choose(taskId int64, successfulBidderUid int64, operatorUid int64) (*domain.Task, error)
  7 +}
@@ -201,8 +201,6 @@ func (status *UnClaimedStatus) Bib(task *Task, bidder *EmployeeInfo) error { @@ -201,8 +201,6 @@ func (status *UnClaimedStatus) Bib(task *Task, bidder *EmployeeInfo) error {
201 Bidder: bidder, 201 Bidder: bidder,
202 BidTime: bidTime, 202 BidTime: bidTime,
203 }) 203 })
204 - task.TaskStatus = TASK_STATUS_UNDERWAY  
205 - task.CurrentStatus = &UnderwayStatus{}  
206 return nil 204 return nil
207 } 205 }
208 } 206 }
@@ -26,7 +26,7 @@ func (dao *TaskDao) AddBidInfo(taskId int64, bidStartTime time.Time, bidEndTime @@ -26,7 +26,7 @@ func (dao *TaskDao) AddBidInfo(taskId int64, bidStartTime time.Time, bidEndTime
26 tx := dao.transactionContext.PgTx 26 tx := dao.transactionContext.PgTx
27 _, err := tx.QueryOne( 27 _, err := tx.QueryOne(
28 pg.Scan(), 28 pg.Scan(),
29 - "INSERT INTO rob_infos (task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?)", 29 + "INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?)",
30 taskId, bidStartTime, bidEndTime) 30 taskId, bidStartTime, bidEndTime)
31 return err 31 return err
32 } 32 }
@@ -40,6 +40,15 @@ func (dao *TaskDao) AddBidderInfo(taskId int64, bidder *domain.EmployeeInfo) err @@ -40,6 +40,15 @@ func (dao *TaskDao) AddBidderInfo(taskId int64, bidder *domain.EmployeeInfo) err
40 return err 40 return err
41 } 41 }
42 42
  43 +func (dao *TaskDao) SetSuccessfulBidder(taskId int64, successfulBidder *domain.EmployeeInfo, winBidTime time.Time) error {
  44 + tx := dao.transactionContext.PgTx
  45 + _, err := tx.QueryOne(
  46 + pg.Scan(),
  47 + "UPDATE bid_infos SET successful_bidder=?, win_bid_time=? WHERE task_id=?",
  48 + successfulBidder, winBidTime, taskId)
  49 + return err
  50 +}
  51 +
43 func NewTaskDao(transactionContext *pgTransaction.TransactionContext) (*TaskDao, error) { 52 func NewTaskDao(transactionContext *pgTransaction.TransactionContext) (*TaskDao, error) {
44 if transactionContext == nil { 53 if transactionContext == nil {
45 return nil, fmt.Errorf("transactionContext参数不能为nil") 54 return nil, fmt.Errorf("transactionContext参数不能为nil")
  1 +package domain_service
  2 +
  3 +import (
  4 + "fmt"
  5 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  6 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  7 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
  9 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
  10 +)
  11 +
  12 +type ChooseSuccessfulBidderService struct {
  13 + coreDomain.BaseEventPublisher
  14 + transactionContext *pgTransaction.TransactionContext
  15 +}
  16 +
  17 +func (service *ChooseSuccessfulBidderService) Choose(taskId int64, successfulBidderUid int64, operatorUid int64) (*domain.Task, error) {
  18 + var employeeRepository domain.EmployeeRepository
  19 + var taskRepository domain.TaskRepository
  20 + var taskDao *dao.TaskDao
  21 + if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
  22 + return nil, err
  23 + } else {
  24 + employeeRepository = repository
  25 + }
  26 + if repository, err := repository.NewTaskRepository(service.transactionContext); err != nil {
  27 + return nil, err
  28 + } else {
  29 + taskRepository = repository
  30 + }
  31 + if dao, err := dao.NewTaskDao(service.transactionContext); err != nil {
  32 + return nil, err
  33 + } else {
  34 + taskDao = dao
  35 + }
  36 + operator, err := employeeRepository.FindOne(map[string]interface{}{
  37 + "uid": operatorUid,
  38 + })
  39 + if err != nil {
  40 + return nil, err
  41 + }
  42 + if operator == nil {
  43 + return nil, fmt.Errorf("无效的操作者")
  44 + }
  45 + task, err := taskRepository.FindOne(map[string]interface{}{
  46 + "taskId": taskId,
  47 + })
  48 + if err != nil {
  49 + return nil, err
  50 + }
  51 + if task == nil {
  52 + return nil, fmt.Errorf("无效的任务")
  53 + }
  54 + if operator.EmployeeInfo.Uid != task.Sponsor.Uid {
  55 + return nil, fmt.Errorf("无效的操作者")
  56 + }
  57 + successfulBidder, err := employeeRepository.FindOne(map[string]interface{}{
  58 + "uid": successfulBidderUid,
  59 + })
  60 + if err != nil {
  61 + return nil, err
  62 + }
  63 + if successfulBidder == nil {
  64 + return nil, fmt.Errorf("无效的中标人")
  65 + }
  66 + if err := task.ChooseSuccessfulBidder(successfulBidder.EmployeeInfo); err != nil {
  67 + return nil, err
  68 + }
  69 + if err := taskDao.SetSuccessfulBidder(taskId, task.BidInfo.SuccessfulBidder, task.BidInfo.WinBidTime); err != nil {
  70 + return nil, err
  71 + }
  72 + if task, err := taskRepository.Save(task); err != nil {
  73 + return nil, err
  74 + } else {
  75 + return task, nil
  76 + }
  77 +}
  78 +
  79 +func NewChooseSuccessfulBidderService(transactionContext *pgTransaction.TransactionContext) (*ChooseSuccessfulBidderService, error) {
  80 + if transactionContext == nil {
  81 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  82 + } else {
  83 + return &ChooseSuccessfulBidderService{
  84 + transactionContext: transactionContext,
  85 + }, nil
  86 + }
  87 +}
@@ -66,7 +66,7 @@ var _ = Describe("对任务进行竞标", func() { @@ -66,7 +66,7 @@ var _ = Describe("对任务进行竞标", func() {
66 ContainsKey("code").ValueEqual("code", 0). 66 ContainsKey("code").ValueEqual("code", 0).
67 ContainsKey("msg").ValueEqual("msg", "ok"). 67 ContainsKey("msg").ValueEqual("msg", "ok").
68 ContainsKey("data").Value("data").Object(). 68 ContainsKey("data").Value("data").Object().
69 - ContainsKey("taskStatus").ValueEqual("taskStatus", 3). 69 + ContainsKey("taskStatus").ValueEqual("taskStatus", 2).
70 ContainsKey("bidInfo").Value("bidInfo").Object(). 70 ContainsKey("bidInfo").Value("bidInfo").Object().
71 ContainsKey("bidderInfos").Value("bidderInfos").Array().Length().Equal(2) 71 ContainsKey("bidderInfos").Value("bidderInfos").Array().Length().Equal(2)
72 }) 72 })
1 package task 1 package task
2 2
3 import ( 3 import (
  4 + "github.com/go-pg/pg"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
4 "net/http" 6 "net/http"
  7 + "time"
5 8
6 "github.com/gavv/httpexpect" 9 "github.com/gavv/httpexpect"
7 - "github.com/go-pg/pg"  
8 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega"
10 pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg" 12 pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
11 ) 13 )
12 14
13 var _ = Describe("选择竞标任务的中标人", func() { 15 var _ = Describe("选择竞标任务的中标人", func() {
14 - var taskId int64 16 + Describe("选择竞标任务的中标人", func() {
  17 + Context("任务发布者对已发布的竞标类型任务选择中标人", func() {
15 BeforeEach(func() { 18 BeforeEach(func() {
  19 + dayAfter, _ := time.ParseDuration("72h")
16 _, err := pG.DB.QueryOne( 20 _, err := pG.DB.QueryOne(
17 - pg.Scan(&taskId),  
18 - "INSERT INTO tasks (task_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, rob_info, bid_info, participators, task_percentage, solve_report, solve_picture_urls, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",  
19 - "testTaskId", "testCompanyId", "testTaskName", "testTaskType", "testSponsor", "testTaskStatus", "testReferenceResource", "testCustomerValue", "testTaskNature", "testSuMoney", "testAcceptanceStandard", "testTaskDescription", "testTaskPictureUrls", "testIsRewardTake", "testRobInfo", "testBidInfo", "testParticipators", "testTaskPercentage", "testSolveReport", "testSolvePictureUrls", "testCreateTime", "testReleaseTime") 21 + pg.Scan(),
  22 + "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, receiver_uid, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  23 + 1, 101, "竞标任务1", 2, &domain.EmployeeInfo{
  24 + Uid: 2499036607974745088,
  25 + }, 2, "null", pg.Array([]string{"口感", "便利", "品牌", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, []*domain.EmployeeInfo{
  26 + {
  27 + Uid: 2499036607974745077,
  28 + },
  29 + {
  30 + Uid: 2499036607974745066,
  31 + },
  32 + }, "null", "", pg.Array([]string{}), 0, time.Now(), time.Now().Add(dayAfter))
20 Expect(err).NotTo(HaveOccurred()) 33 Expect(err).NotTo(HaveOccurred())
  34 + _, err1 := pG.DB.QueryOne(
  35 + pg.Scan(),
  36 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
  37 + 1, 101, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
  38 + Expect(err1).NotTo(HaveOccurred())
  39 + _, err2 := pG.DB.QueryOne(
  40 + pg.Scan(),
  41 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
  42 + 2, 101, 2499036607974745099, "testEmployeeName", "testEmployeeAccount", 0)
  43 + Expect(err2).NotTo(HaveOccurred())
  44 + _, err3 := pG.DB.QueryOne(
  45 + pg.Scan(),
  46 + "INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?)",
  47 + 1, time.Date(2020, time.Month(4), 5, 8, 0, 0, 0, time.Now().Location()), time.Date(2020, time.Month(4), 10, 8, 0, 0, 0, time.Now().Location()))
  48 + Expect(err3).NotTo(HaveOccurred())
  49 + _, err4 := pG.DB.QueryOne(
  50 + pg.Scan(),
  51 + "INSERT INTO bidder_infos (task_id, bidder) VALUES (?, ?)",
  52 + 1, &domain.EmployeeInfo{
  53 + Uid: 2499036607974745099,
21 }) 54 })
22 - Describe("选择竞标任务的中标人", func() {  
23 - Context("", func() {  
24 - It("", func() { 55 + Expect(err4).NotTo(HaveOccurred())
  56 + })
  57 + It("选择中标人成功", func() {
25 httpExpect := httpexpect.New(GinkgoT(), server.URL) 58 httpExpect := httpexpect.New(GinkgoT(), server.URL)
26 body := map[string]interface{}{ 59 body := map[string]interface{}{
27 - "operator": "int64",  
28 - "successfulBidder": "int64", 60 + "operator": 2499036607974745088,
  61 + "successfulBidder": 2499036607974745099,
29 } 62 }
30 - httpExpect.POST("/tasks/{taskId}/choose-successful-bidder"). 63 + httpExpect.POST("/tasks/1/choose-successful-bidder").
31 WithJSON(body). 64 WithJSON(body).
32 Expect(). 65 Expect().
33 Status(http.StatusOK). 66 Status(http.StatusOK).
@@ -35,12 +68,22 @@ var _ = Describe("选择竞标任务的中标人", func() { @@ -35,12 +68,22 @@ var _ = Describe("选择竞标任务的中标人", func() {
35 Object(). 68 Object().
36 ContainsKey("code").ValueEqual("code", 0). 69 ContainsKey("code").ValueEqual("code", 0).
37 ContainsKey("msg").ValueEqual("msg", "ok"). 70 ContainsKey("msg").ValueEqual("msg", "ok").
38 - ContainsKey("data").Value("data").Object() 71 + ContainsKey("data").Value("data").Object().
  72 + ContainsKey("taskStatus").ValueEqual("taskStatus", 3).
  73 + ContainsKey("bidInfo").Value("bidInfo").Object().
  74 + ContainsKey("successfulBidder").Value("successfulBidder").Object().
  75 + ContainsKey("uid").ValueEqual("uid", 2499036607974745099)
39 }) 76 })
40 }) 77 })
41 }) 78 })
42 AfterEach(func() { 79 AfterEach(func() {
43 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true") 80 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true")
44 Expect(err).NotTo(HaveOccurred()) 81 Expect(err).NotTo(HaveOccurred())
  82 + _, err1 := pG.DB.Exec("DELETE FROM bid_infos WHERE true")
  83 + Expect(err1).NotTo(HaveOccurred())
  84 + _, err2 := pG.DB.Exec("DELETE FROM bidder_infos WHERE true")
  85 + Expect(err2).NotTo(HaveOccurred())
  86 + _, err3 := pG.DB.Exec("DELETE FROM employees WHERE true")
  87 + Expect(err3).NotTo(HaveOccurred())
45 }) 88 })
46 }) 89 })