作者 linmadan

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

... ... @@ -29,6 +29,45 @@ func (subscriber *AbilityServiceSubscriber) HandleEvent(domainEvent domain.Domai
}
fmt.Println(data)
break
case event.TASK_CLOSED_EVENT:
taskClosedEvent := domainEvent.(*event.TaskClosed)
var referenceResourceIds []int64
for _, referenceResourceItem := range taskClosedEvent.ReferenceResource.ReferenceResourceItems {
referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
}
data, err := abilityServiceGateway.CloseTaskCallback(taskClosedEvent.TaskId, referenceResourceIds)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(data)
break
case event.TASK_ROBED_EVENT:
taskRobedEvent := domainEvent.(*event.TaskRobed)
var referenceResourceIds []int64
for _, referenceResourceItem := range taskRobedEvent.ReferenceResource.ReferenceResourceItems {
referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
}
data, err := abilityServiceGateway.ReceiverTaskCallback(taskRobedEvent.RobInfo.Receiver.Uid, taskRobedEvent.TaskId, referenceResourceIds, taskRobedEvent.RobInfo.Receiver.Uid, taskRobedEvent.RobInfo.ReceiveTime)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(data)
break
case event.TASK_BIDDED_EVENT:
taskBiddedEvent := domainEvent.(*event.TaskBidded)
var referenceResourceIds []int64
for _, referenceResourceItem := range taskBiddedEvent.ReferenceResource.ReferenceResourceItems {
referenceResourceIds = append(referenceResourceIds, referenceResourceItem.ReferenceResourceId)
}
data, err := abilityServiceGateway.ReceiverTaskCallback(taskBiddedEvent.BidInfo.SuccessfulBidder.Uid, taskBiddedEvent.TaskId, referenceResourceIds, taskBiddedEvent.Operator.Uid, taskBiddedEvent.BidInfo.WinBidTime)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(data)
break
case event.TASK_ACCEPTANCED_EVENT:
taskAcceptancedEvent := domainEvent.(*event.TaskAcceptanced)
if taskAcceptancedEvent.ReferenceResource != nil {
... ... @@ -63,5 +102,8 @@ func (subscriber *AbilityServiceSubscriber) SubscribedToEventTypes() []string {
return [] string{
event.TASK_RELEASED_EVENT,
event.TASK_ACCEPTANCED_EVENT,
event.TASK_CLOSED_EVENT,
event.TASK_ROBED_EVENT,
event.TASK_BIDDED_EVENT,
}
}
... ...
... ... @@ -40,6 +40,7 @@ func (taskService *TaskService) RobTask(robTaskCommand *command.RobTaskCommand)
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
robTaskService = value
robTaskService.Subscribe(&subscriber.AbilityServiceSubscriber{})
}
if task, err := robTaskService.Rob(robTaskCommand.TaskId, robTaskCommand.Receiver); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -106,6 +107,7 @@ func (taskService *TaskService) ChooseSuccessfulBidder(chooseSuccessfulBidderCom
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
chooseSuccessfulBidderService = value
chooseSuccessfulBidderService.Subscribe(&subscriber.AbilityServiceSubscriber{})
}
if task, err := chooseSuccessfulBidderService.Choose(chooseSuccessfulBidderCommand.TaskId, chooseSuccessfulBidderCommand.SuccessfulBidder, chooseSuccessfulBidderCommand.Operator); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -206,6 +208,7 @@ func (taskService *TaskService) OffTask(offTaskCommand *command.OffTaskCommand)
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
offTaskService = value
offTaskService.Subscribe(&subscriber.AbilityServiceSubscriber{})
}
if task, err := offTaskService.Off(offTaskCommand.TaskId, offTaskCommand.Operator, offTaskCommand.OffReason); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ...
package event
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)
const TASK_BIDDED_EVENT = "task-bidded-event"
type TaskBidded struct {
coreDomain.BaseEvent
// 任务ID
TaskId int64 `json:"taskId"`
// 公司ID
CompanyId int64 `json:"companyId"`
// 任务名称
TaskName string `json:"taskName"`
// 任务类型Type
TaskType int `json:"taskType"`
// 任务发起者
Sponsor *domain.EmployeeInfo `json:"sponsor"`
// 任务状态
TaskStatus int `json:"taskStatus"`
// 引用资源
ReferenceResource *domain.ReferenceResource `json:"referenceResource"`
// 竞标任务信息
BidInfo *domain.BidInfo `json:"bidInfo"`
// 操作人
Operator *domain.EmployeeInfo `json:"operator"`
}
func (event *TaskBidded) EventType() string {
return TASK_BIDDED_EVENT
}
... ...
package event
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)
const TASK_CLOSED_EVENT = "task-closed-event"
type TaskClosed struct {
coreDomain.BaseEvent
// 任务ID
TaskId int64 `json:"taskId"`
// 公司ID
CompanyId int64 `json:"companyId"`
// 任务名称
TaskName string `json:"taskName"`
// 任务类型Type
TaskType int `json:"taskType"`
// 任务发起者
Sponsor *domain.EmployeeInfo `json:"sponsor"`
// 任务状态
TaskStatus int `json:"taskStatus"`
// 引用资源
ReferenceResource *domain.ReferenceResource `json:"referenceResource"`
}
func (event *TaskClosed) EventType() string {
return TASK_CLOSED_EVENT
}
... ...
package event
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)
const TASK_ROBED_EVENT = "task-robed-event"
type TaskRobed struct {
coreDomain.BaseEvent
// 任务ID
TaskId int64 `json:"taskId"`
// 公司ID
CompanyId int64 `json:"companyId"`
// 任务名称
TaskName string `json:"taskName"`
// 任务类型Type
TaskType int `json:"taskType"`
// 任务发起者
Sponsor *domain.EmployeeInfo `json:"sponsor"`
// 任务状态
TaskStatus int `json:"taskStatus"`
// 引用资源
ReferenceResource *domain.ReferenceResource `json:"referenceResource"`
// 抢单任务信息
RobInfo *domain.RobInfo `json:"robInfo"`
}
func (event *TaskRobed) EventType() string {
return TASK_ROBED_EVENT
}
... ...
... ... @@ -5,8 +5,10 @@ import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
"time"
)
type ChooseSuccessfulBidderService struct {
... ... @@ -72,6 +74,20 @@ func (service *ChooseSuccessfulBidderService) Choose(taskId int64, successfulBid
if task, err := taskRepository.Save(task); err != nil {
return nil, err
} else {
taskBiddedEvent := new(event.TaskBidded)
taskBiddedEvent.OccurredOn = time.Now()
taskBiddedEvent.TaskId = task.TaskId
taskBiddedEvent.CompanyId = task.CompanyId
taskBiddedEvent.TaskName = task.TaskName
taskBiddedEvent.TaskType = task.TaskType
taskBiddedEvent.Sponsor = task.Sponsor
taskBiddedEvent.TaskStatus = task.TaskStatus
taskBiddedEvent.ReferenceResource = task.ReferenceResource
taskBiddedEvent.BidInfo = task.BidInfo
taskBiddedEvent.Operator = operator.EmployeeInfo
if err := service.Publish(taskBiddedEvent); err != nil {
println(err.Error())
}
return task, nil
}
}
... ...
... ... @@ -5,6 +5,7 @@ import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
"time"
)
... ... @@ -69,6 +70,18 @@ func (service *OffTaskService) Off(taskId int64, operatorUid int64, offReason st
if task, err := taskRepository.Save(task); err != nil {
return nil, err
} else {
taskClosedEvent := new(event.TaskClosed)
taskClosedEvent.OccurredOn = time.Now()
taskClosedEvent.TaskId = task.TaskId
taskClosedEvent.CompanyId = task.CompanyId
taskClosedEvent.TaskName = task.TaskName
taskClosedEvent.TaskType = task.TaskType
taskClosedEvent.Sponsor = task.Sponsor
taskClosedEvent.TaskStatus = task.TaskStatus
taskClosedEvent.ReferenceResource = task.ReferenceResource
if err := service.Publish(taskClosedEvent); err != nil {
println(err.Error())
}
return task, nil
}
}
... ...
... ... @@ -5,8 +5,10 @@ import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/event"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
"time"
)
type RobTaskService struct {
... ... @@ -54,7 +56,7 @@ func (service *RobTaskService) Rob(taskId int64, receiverUid int64) (*domain.Tas
if receiver.EmployeeInfo.Uid == task.Sponsor.Uid {
return nil, fmt.Errorf("无法领取自己发布的任务")
}
if task.RobInfo != nil && task.RobInfo.Receiver != nil{
if task.RobInfo != nil && task.RobInfo.Receiver != nil {
return nil, fmt.Errorf("任务已经被人领取")
}
if err := task.Rob(receiver.EmployeeInfo); err != nil {
... ... @@ -66,6 +68,19 @@ func (service *RobTaskService) Rob(taskId int64, receiverUid int64) (*domain.Tas
if task, err := taskRepository.Save(task); err != nil {
return nil, err
} else {
taskRobedEvent := new(event.TaskRobed)
taskRobedEvent.OccurredOn = time.Now()
taskRobedEvent.TaskId = task.TaskId
taskRobedEvent.CompanyId = task.CompanyId
taskRobedEvent.TaskName = task.TaskName
taskRobedEvent.TaskType = task.TaskType
taskRobedEvent.Sponsor = task.Sponsor
taskRobedEvent.TaskStatus = task.TaskStatus
taskRobedEvent.ReferenceResource = task.ReferenceResource
taskRobedEvent.RobInfo = task.RobInfo
if err := service.Publish(taskRobedEvent); err != nil {
println(err.Error())
}
return task, nil
}
}
... ...
... ... @@ -42,6 +42,35 @@ func (serviceGateway *HttplibAbilityServiceGateway) CommitQuestionSolution(qid i
return data, err
}
func (serviceGateway *HttplibAbilityServiceGateway) CloseTaskCallback(taskId int64, referenceResourceIds []int64) (map[string]interface{}, error) {
url := strings.Join([]string{serviceGateway.baseURL, "task/closeTaskCallback"}, "/")
request := serviceGateway.createRequest(url, "post")
options := make(map[string]interface{})
options["taskId"] = taskId
options["referenceResourceIds"] = referenceResourceIds
request.JSONBody(options)
response := make(map[string]interface{})
request.ToJSON(&response)
data, err := serviceGateway.responseHandle(response)
return data, err
}
func (serviceGateway *HttplibAbilityServiceGateway) ReceiverTaskCallback(uid int64, taskId int64, referenceResourceIds []int64, operatorId int64, operatorTime time.Time) (map[string]interface{}, error) {
url := strings.Join([]string{serviceGateway.baseURL, "task/receiverTaskCallback"}, "/")
request := serviceGateway.createRequest(url, "post")
options := make(map[string]interface{})
options["uid"] = uid
options["taskId"] = taskId
options["referenceResourceIds"] = referenceResourceIds
options["operator_id"] = operatorId
options["operator_time"] = operatorTime
request.JSONBody(options)
response := make(map[string]interface{})
request.ToJSON(&response)
data, err := serviceGateway.responseHandle(response)
return data, err
}
func NewHttplibAbilityServiceGateway() *HttplibAbilityServiceGateway {
return &HttplibAbilityServiceGateway{
httplibBaseServiceGateway: httplibBaseServiceGateway{
... ...
package service_gateway
import "time"
type AbilityServiceGateway interface {
CommitQuestionQuotes(uid int64, taskId int64, serials []int64) (map[string]interface{}, error)
CommitQuestionSolution(qid int64, uid int64, solveUid int64, content string, scoreSolve float64, imgs []string, partners []map[string]interface{}) (map[string]interface{}, error)
CloseTaskCallback(taskId int64, referenceResourceIds []int64) (map[string]interface{}, error)
ReceiverTaskCallback(uid int64, taskId int64, referenceResourceIds []int64, operatorId int64, operatorTime time.Time) (map[string]interface{}, error)
}
\ No newline at end of file
... ...
... ... @@ -2,8 +2,6 @@ package controllers
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/command"
... ... @@ -157,7 +155,6 @@ func (controller *TaskController) CreateTask() {
data, err := taskService.CreateTask(createTaskCommand)
var response utils.JsonResponse
if err != nil {
fmt.Println(err.Error())
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
... ...