作者 linmadan

添加任务关闭与领取事件订阅回调功能

@@ -29,6 +29,45 @@ func (subscriber *AbilityServiceSubscriber) HandleEvent(domainEvent domain.Domai @@ -29,6 +29,45 @@ func (subscriber *AbilityServiceSubscriber) HandleEvent(domainEvent domain.Domai
29 } 29 }
30 fmt.Println(data) 30 fmt.Println(data)
31 break 31 break
  32 + case event.TASK_CLOSED_EVENT:
  33 + taskClosedEvent := domainEvent.(*event.TaskClosed)
  34 + var referenceResourceIds []int64
  35 + for _, referenceResourceItem := range taskClosedEvent.ReferenceResource.ReferenceResourceItems {
  36 + referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
  37 + }
  38 + data, err := abilityServiceGateway.CloseTaskCallback(taskClosedEvent.TaskId, referenceResourceIds)
  39 + if err != nil {
  40 + fmt.Println(err.Error())
  41 + return err
  42 + }
  43 + fmt.Println(data)
  44 + break
  45 + case event.TASK_ROBED_EVENT:
  46 + taskRobedEvent := domainEvent.(*event.TaskRobed)
  47 + var referenceResourceIds []int64
  48 + for _, referenceResourceItem := range taskRobedEvent.ReferenceResource.ReferenceResourceItems {
  49 + referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
  50 + }
  51 + data, err := abilityServiceGateway.ReceiverTaskCallback(taskRobedEvent.RobInfo.Receiver.Uid, taskRobedEvent.TaskId, referenceResourceIds, taskRobedEvent.RobInfo.Receiver.Uid, taskRobedEvent.RobInfo.ReceiveTime)
  52 + if err != nil {
  53 + fmt.Println(err.Error())
  54 + return err
  55 + }
  56 + fmt.Println(data)
  57 + break
  58 + case event.TASK_BIDDED_EVENT:
  59 + taskBiddedEvent := domainEvent.(*event.TaskBidded)
  60 + var referenceResourceIds []int64
  61 + for _, referenceResourceItem := range taskBiddedEvent.ReferenceResource.ReferenceResourceItems {
  62 + referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
  63 + }
  64 + data, err := abilityServiceGateway.ReceiverTaskCallback(taskBiddedEvent.BidInfo.SuccessfulBidder.Uid, taskBiddedEvent.TaskId, referenceResourceIds, taskBiddedEvent.Operator.Uid, taskBiddedEvent.BidInfo.WinBidTime)
  65 + if err != nil {
  66 + fmt.Println(err.Error())
  67 + return err
  68 + }
  69 + fmt.Println(data)
  70 + break
32 case event.TASK_ACCEPTANCED_EVENT: 71 case event.TASK_ACCEPTANCED_EVENT:
33 taskAcceptancedEvent := domainEvent.(*event.TaskAcceptanced) 72 taskAcceptancedEvent := domainEvent.(*event.TaskAcceptanced)
34 if taskAcceptancedEvent.ReferenceResource != nil { 73 if taskAcceptancedEvent.ReferenceResource != nil {
@@ -63,5 +102,8 @@ func (subscriber *AbilityServiceSubscriber) SubscribedToEventTypes() []string { @@ -63,5 +102,8 @@ func (subscriber *AbilityServiceSubscriber) SubscribedToEventTypes() []string {
63 return [] string{ 102 return [] string{
64 event.TASK_RELEASED_EVENT, 103 event.TASK_RELEASED_EVENT,
65 event.TASK_ACCEPTANCED_EVENT, 104 event.TASK_ACCEPTANCED_EVENT,
  105 + event.TASK_CLOSED_EVENT,
  106 + event.TASK_ROBED_EVENT,
  107 + event.TASK_BIDDED_EVENT,
66 } 108 }
67 } 109 }
@@ -40,6 +40,7 @@ func (taskService *TaskService) RobTask(robTaskCommand *command.RobTaskCommand) @@ -40,6 +40,7 @@ func (taskService *TaskService) RobTask(robTaskCommand *command.RobTaskCommand)
40 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 40 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
41 } else { 41 } else {
42 robTaskService = value 42 robTaskService = value
  43 + robTaskService.Subscribe(&subscriber.AbilityServiceSubscriber{})
43 } 44 }
44 if task, err := robTaskService.Rob(robTaskCommand.TaskId, robTaskCommand.Receiver); err != nil { 45 if task, err := robTaskService.Rob(robTaskCommand.TaskId, robTaskCommand.Receiver); err != nil {
45 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 46 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -106,6 +107,7 @@ func (taskService *TaskService) ChooseSuccessfulBidder(chooseSuccessfulBidderCom @@ -106,6 +107,7 @@ func (taskService *TaskService) ChooseSuccessfulBidder(chooseSuccessfulBidderCom
106 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 107 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
107 } else { 108 } else {
108 chooseSuccessfulBidderService = value 109 chooseSuccessfulBidderService = value
  110 + chooseSuccessfulBidderService.Subscribe(&subscriber.AbilityServiceSubscriber{})
109 } 111 }
110 if task, err := chooseSuccessfulBidderService.Choose(chooseSuccessfulBidderCommand.TaskId, chooseSuccessfulBidderCommand.SuccessfulBidder, chooseSuccessfulBidderCommand.Operator); err != nil { 112 if task, err := chooseSuccessfulBidderService.Choose(chooseSuccessfulBidderCommand.TaskId, chooseSuccessfulBidderCommand.SuccessfulBidder, chooseSuccessfulBidderCommand.Operator); err != nil {
111 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 113 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -206,6 +208,7 @@ func (taskService *TaskService) OffTask(offTaskCommand *command.OffTaskCommand) @@ -206,6 +208,7 @@ func (taskService *TaskService) OffTask(offTaskCommand *command.OffTaskCommand)
206 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 208 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
207 } else { 209 } else {
208 offTaskService = value 210 offTaskService = value
  211 + offTaskService.Subscribe(&subscriber.AbilityServiceSubscriber{})
209 } 212 }
210 if task, err := offTaskService.Off(offTaskCommand.TaskId, offTaskCommand.Operator, offTaskCommand.OffReason); err != nil { 213 if task, err := offTaskService.Off(offTaskCommand.TaskId, offTaskCommand.Operator, offTaskCommand.OffReason); err != nil {
211 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 214 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1 +package event
  2 +
  3 +import (
  4 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  6 +)
  7 +
  8 +const TASK_BIDDED_EVENT = "task-bidded-event"
  9 +
  10 +type TaskBidded struct {
  11 + coreDomain.BaseEvent
  12 + // 任务ID
  13 + TaskId int64 `json:"taskId"`
  14 + // 公司ID
  15 + CompanyId int64 `json:"companyId"`
  16 + // 任务名称
  17 + TaskName string `json:"taskName"`
  18 + // 任务类型Type
  19 + TaskType int `json:"taskType"`
  20 + // 任务发起者
  21 + Sponsor *domain.EmployeeInfo `json:"sponsor"`
  22 + // 任务状态
  23 + TaskStatus int `json:"taskStatus"`
  24 + // 引用资源
  25 + ReferenceResource *domain.ReferenceResource `json:"referenceResource"`
  26 + // 竞标任务信息
  27 + BidInfo *domain.BidInfo `json:"bidInfo"`
  28 + // 操作人
  29 + Operator *domain.EmployeeInfo `json:"operator"`
  30 +}
  31 +
  32 +func (event *TaskBidded) EventType() string {
  33 + return TASK_BIDDED_EVENT
  34 +}
  1 +package event
  2 +
  3 +import (
  4 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  6 +)
  7 +
  8 +const TASK_CLOSED_EVENT = "task-closed-event"
  9 +
  10 +type TaskClosed struct {
  11 + coreDomain.BaseEvent
  12 + // 任务ID
  13 + TaskId int64 `json:"taskId"`
  14 + // 公司ID
  15 + CompanyId int64 `json:"companyId"`
  16 + // 任务名称
  17 + TaskName string `json:"taskName"`
  18 + // 任务类型Type
  19 + TaskType int `json:"taskType"`
  20 + // 任务发起者
  21 + Sponsor *domain.EmployeeInfo `json:"sponsor"`
  22 + // 任务状态
  23 + TaskStatus int `json:"taskStatus"`
  24 + // 引用资源
  25 + ReferenceResource *domain.ReferenceResource `json:"referenceResource"`
  26 +}
  27 +
  28 +func (event *TaskClosed) EventType() string {
  29 + return TASK_CLOSED_EVENT
  30 +}
  1 +package event
  2 +
  3 +import (
  4 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  6 +)
  7 +
  8 +const TASK_ROBED_EVENT = "task-robed-event"
  9 +
  10 +type TaskRobed struct {
  11 + coreDomain.BaseEvent
  12 + // 任务ID
  13 + TaskId int64 `json:"taskId"`
  14 + // 公司ID
  15 + CompanyId int64 `json:"companyId"`
  16 + // 任务名称
  17 + TaskName string `json:"taskName"`
  18 + // 任务类型Type
  19 + TaskType int `json:"taskType"`
  20 + // 任务发起者
  21 + Sponsor *domain.EmployeeInfo `json:"sponsor"`
  22 + // 任务状态
  23 + TaskStatus int `json:"taskStatus"`
  24 + // 引用资源
  25 + ReferenceResource *domain.ReferenceResource `json:"referenceResource"`
  26 + // 抢单任务信息
  27 + RobInfo *domain.RobInfo `json:"robInfo"`
  28 +}
  29 +
  30 +func (event *TaskRobed) EventType() string {
  31 + return TASK_ROBED_EVENT
  32 +}
@@ -5,8 +5,10 @@ import ( @@ -5,8 +5,10 @@ import (
5 coreDomain "github.com/linmadan/egglib-go/core/domain" 5 coreDomain "github.com/linmadan/egglib-go/core/domain"
6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain" 7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
8 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao" 9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository" 10 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
  11 + "time"
10 ) 12 )
11 13
12 type ChooseSuccessfulBidderService struct { 14 type ChooseSuccessfulBidderService struct {
@@ -72,6 +74,20 @@ func (service *ChooseSuccessfulBidderService) Choose(taskId int64, successfulBid @@ -72,6 +74,20 @@ func (service *ChooseSuccessfulBidderService) Choose(taskId int64, successfulBid
72 if task, err := taskRepository.Save(task); err != nil { 74 if task, err := taskRepository.Save(task); err != nil {
73 return nil, err 75 return nil, err
74 } else { 76 } else {
  77 + taskBiddedEvent := new(event.TaskBidded)
  78 + taskBiddedEvent.OccurredOn = time.Now()
  79 + taskBiddedEvent.TaskId = task.TaskId
  80 + taskBiddedEvent.CompanyId = task.CompanyId
  81 + taskBiddedEvent.TaskName = task.TaskName
  82 + taskBiddedEvent.TaskType = task.TaskType
  83 + taskBiddedEvent.Sponsor = task.Sponsor
  84 + taskBiddedEvent.TaskStatus = task.TaskStatus
  85 + taskBiddedEvent.ReferenceResource = task.ReferenceResource
  86 + taskBiddedEvent.BidInfo = task.BidInfo
  87 + taskBiddedEvent.Operator = operator.EmployeeInfo
  88 + if err := service.Publish(taskBiddedEvent); err != nil {
  89 + println(err.Error())
  90 + }
75 return task, nil 91 return task, nil
76 } 92 }
77 } 93 }
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 coreDomain "github.com/linmadan/egglib-go/core/domain" 5 coreDomain "github.com/linmadan/egglib-go/core/domain"
6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain" 7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
8 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository" 9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
9 "time" 10 "time"
10 ) 11 )
@@ -69,6 +70,18 @@ func (service *OffTaskService) Off(taskId int64, operatorUid int64, offReason st @@ -69,6 +70,18 @@ func (service *OffTaskService) Off(taskId int64, operatorUid int64, offReason st
69 if task, err := taskRepository.Save(task); err != nil { 70 if task, err := taskRepository.Save(task); err != nil {
70 return nil, err 71 return nil, err
71 } else { 72 } else {
  73 + taskClosedEvent := new(event.TaskClosed)
  74 + taskClosedEvent.OccurredOn = time.Now()
  75 + taskClosedEvent.TaskId = task.TaskId
  76 + taskClosedEvent.CompanyId = task.CompanyId
  77 + taskClosedEvent.TaskName = task.TaskName
  78 + taskClosedEvent.TaskType = task.TaskType
  79 + taskClosedEvent.Sponsor = task.Sponsor
  80 + taskClosedEvent.TaskStatus = task.TaskStatus
  81 + taskClosedEvent.ReferenceResource = task.ReferenceResource
  82 + if err := service.Publish(taskClosedEvent); err != nil {
  83 + println(err.Error())
  84 + }
72 return task, nil 85 return task, nil
73 } 86 }
74 } 87 }
@@ -5,8 +5,10 @@ import ( @@ -5,8 +5,10 @@ import (
5 coreDomain "github.com/linmadan/egglib-go/core/domain" 5 coreDomain "github.com/linmadan/egglib-go/core/domain"
6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain" 7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
8 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao" 9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository" 10 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
  11 + "time"
10 ) 12 )
11 13
12 type RobTaskService struct { 14 type RobTaskService struct {
@@ -54,7 +56,7 @@ func (service *RobTaskService) Rob(taskId int64, receiverUid int64) (*domain.Tas @@ -54,7 +56,7 @@ func (service *RobTaskService) Rob(taskId int64, receiverUid int64) (*domain.Tas
54 if receiver.EmployeeInfo.Uid == task.Sponsor.Uid { 56 if receiver.EmployeeInfo.Uid == task.Sponsor.Uid {
55 return nil, fmt.Errorf("无法领取自己发布的任务") 57 return nil, fmt.Errorf("无法领取自己发布的任务")
56 } 58 }
57 - if task.RobInfo != nil && task.RobInfo.Receiver != nil{ 59 + if task.RobInfo != nil && task.RobInfo.Receiver != nil {
58 return nil, fmt.Errorf("任务已经被人领取") 60 return nil, fmt.Errorf("任务已经被人领取")
59 } 61 }
60 if err := task.Rob(receiver.EmployeeInfo); err != nil { 62 if err := task.Rob(receiver.EmployeeInfo); err != nil {
@@ -66,6 +68,19 @@ func (service *RobTaskService) Rob(taskId int64, receiverUid int64) (*domain.Tas @@ -66,6 +68,19 @@ func (service *RobTaskService) Rob(taskId int64, receiverUid int64) (*domain.Tas
66 if task, err := taskRepository.Save(task); err != nil { 68 if task, err := taskRepository.Save(task); err != nil {
67 return nil, err 69 return nil, err
68 } else { 70 } else {
  71 + taskRobedEvent := new(event.TaskRobed)
  72 + taskRobedEvent.OccurredOn = time.Now()
  73 + taskRobedEvent.TaskId = task.TaskId
  74 + taskRobedEvent.CompanyId = task.CompanyId
  75 + taskRobedEvent.TaskName = task.TaskName
  76 + taskRobedEvent.TaskType = task.TaskType
  77 + taskRobedEvent.Sponsor = task.Sponsor
  78 + taskRobedEvent.TaskStatus = task.TaskStatus
  79 + taskRobedEvent.ReferenceResource = task.ReferenceResource
  80 + taskRobedEvent.RobInfo = task.RobInfo
  81 + if err := service.Publish(taskRobedEvent); err != nil {
  82 + println(err.Error())
  83 + }
69 return task, nil 84 return task, nil
70 } 85 }
71 } 86 }
@@ -42,6 +42,35 @@ func (serviceGateway *HttplibAbilityServiceGateway) CommitQuestionSolution(qid i @@ -42,6 +42,35 @@ func (serviceGateway *HttplibAbilityServiceGateway) CommitQuestionSolution(qid i
42 return data, err 42 return data, err
43 } 43 }
44 44
  45 +func (serviceGateway *HttplibAbilityServiceGateway) CloseTaskCallback(taskId int64, referenceResourceIds []int64) (map[string]interface{}, error) {
  46 + url := strings.Join([]string{serviceGateway.baseURL, "task/closeTaskCallback"}, "/")
  47 + request := serviceGateway.createRequest(url, "post")
  48 + options := make(map[string]interface{})
  49 + options["taskId"] = taskId
  50 + options["referenceResourceIds"] = referenceResourceIds
  51 + request.JSONBody(options)
  52 + response := make(map[string]interface{})
  53 + request.ToJSON(&response)
  54 + data, err := serviceGateway.responseHandle(response)
  55 + return data, err
  56 +}
  57 +
  58 +func (serviceGateway *HttplibAbilityServiceGateway) ReceiverTaskCallback(uid int64, taskId int64, referenceResourceIds []int64, operatorId int64, operatorTime time.Time) (map[string]interface{}, error) {
  59 + url := strings.Join([]string{serviceGateway.baseURL, "task/receiverTaskCallback"}, "/")
  60 + request := serviceGateway.createRequest(url, "post")
  61 + options := make(map[string]interface{})
  62 + options["uid"] = uid
  63 + options["taskId"] = taskId
  64 + options["referenceResourceIds"] = referenceResourceIds
  65 + options["operator_id"] = operatorId
  66 + options["operator_time"] = operatorTime
  67 + request.JSONBody(options)
  68 + response := make(map[string]interface{})
  69 + request.ToJSON(&response)
  70 + data, err := serviceGateway.responseHandle(response)
  71 + return data, err
  72 +}
  73 +
45 func NewHttplibAbilityServiceGateway() *HttplibAbilityServiceGateway { 74 func NewHttplibAbilityServiceGateway() *HttplibAbilityServiceGateway {
46 return &HttplibAbilityServiceGateway{ 75 return &HttplibAbilityServiceGateway{
47 httplibBaseServiceGateway: httplibBaseServiceGateway{ 76 httplibBaseServiceGateway: httplibBaseServiceGateway{
1 package service_gateway 1 package service_gateway
2 2
  3 +import "time"
  4 +
3 type AbilityServiceGateway interface { 5 type AbilityServiceGateway interface {
4 CommitQuestionQuotes(uid int64, taskId int64, serials []int64) (map[string]interface{}, error) 6 CommitQuestionQuotes(uid int64, taskId int64, serials []int64) (map[string]interface{}, error)
5 CommitQuestionSolution(qid int64, uid int64, solveUid int64, content string, scoreSolve float64, imgs []string, partners []map[string]interface{}) (map[string]interface{}, error) 7 CommitQuestionSolution(qid int64, uid int64, solveUid int64, content string, scoreSolve float64, imgs []string, partners []map[string]interface{}) (map[string]interface{}, error)
6 -} 8 + CloseTaskCallback(taskId int64, referenceResourceIds []int64) (map[string]interface{}, error)
  9 + ReceiverTaskCallback(uid int64, taskId int64, referenceResourceIds []int64, operatorId int64, operatorTime time.Time) (map[string]interface{}, error)
  10 +}
@@ -2,8 +2,6 @@ package controllers @@ -2,8 +2,6 @@ package controllers
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 - "fmt"  
6 -  
7 "github.com/astaxie/beego" 5 "github.com/astaxie/beego"
8 "github.com/linmadan/egglib-go/web/beego/utils" 6 "github.com/linmadan/egglib-go/web/beego/utils"
9 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/command" 7 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/command"
@@ -157,7 +155,6 @@ func (controller *TaskController) CreateTask() { @@ -157,7 +155,6 @@ func (controller *TaskController) CreateTask() {
157 data, err := taskService.CreateTask(createTaskCommand) 155 data, err := taskService.CreateTask(createTaskCommand)
158 var response utils.JsonResponse 156 var response utils.JsonResponse
159 if err != nil { 157 if err != nil {
160 - fmt.Println(err.Error())  
161 response = utils.ResponseError(controller.Ctx, err) 158 response = utils.ResponseError(controller.Ctx, err)
162 } else { 159 } else {
163 response = utils.ResponseData(controller.Ctx, data) 160 response = utils.ResponseData(controller.Ctx, data)