作者 linmadan

完成任务竞标接口

... ... @@ -29,3 +29,11 @@ func CreateRobTaskService(options map[string]interface{}) (service.RobTaskServic
}
return domainService.NewRobTaskService(transactionContext)
}
func CreateBidTaskService(options map[string]interface{}) (service.BidTaskService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewBidTaskService(transactionContext)
}
... ...
... ... @@ -9,6 +9,7 @@ import (
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/query"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/service"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
)
// 任务服务
... ... @@ -63,10 +64,22 @@ func (taskService *TaskService) BidTask(bidTaskCommand *command.BidTaskCommand)
defer func() {
transactionContext.RollbackTransaction()
}()
var bidTaskService service.BidTaskService
if value, err := factory.CreateBidTaskService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
bidTaskService = value
}
if task, err := bidTaskService.Bid(bidTaskCommand.TaskId, bidTaskCommand.Bidder); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return task, nil
}
}
// 选择竞标任务的中标人
... ... @@ -285,6 +298,14 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
defer func() {
transactionContext.RollbackTransaction()
}()
var taskDao *dao.TaskDao
if value, err := factory.CreateTaskDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, err
} else {
taskDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -302,15 +323,6 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
if sponsor == nil {
return nil, fmt.Errorf("无效的发布者")
}
var bidInfo *domain.BidInfo
if createTaskCommand.TaskType == domain.TASK_TYPE_BID {
bidInfo = &domain.BidInfo{
BidStartTime: createTaskCommand.BidStartTime,
BidEndTime: createTaskCommand.BidEndTime,
}
} else {
bidInfo = nil
}
newTask := &domain.Task{
TaskStatus: domain.TASK_STATUS_UNRELEASED,
CompanyId: createTaskCommand.CompanyId,
... ... @@ -328,7 +340,6 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
TaskDescription: createTaskCommand.TaskDescription,
TaskPictureUrls: createTaskCommand.TaskPictureUrls,
IsRewardTake: createTaskCommand.IsRewardTake,
BidInfo: bidInfo,
}
var taskRepository domain.TaskRepository
if value, err := factory.CreateTaskRepository(map[string]interface{}{
... ... @@ -341,6 +352,11 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
if task, err := taskRepository.Save(newTask); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if createTaskCommand.TaskType == domain.TASK_TYPE_BID {
if err := taskDao.AddBidInfo(task.TaskId, createTaskCommand.BidStartTime, createTaskCommand.BidEndTime); err != nil {
return nil, err
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ...
package service
import "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
type BidTaskService interface {
Bid(taskId int64, bidderUid int64) (*domain.Task, error)
}
... ...
... ... @@ -3,5 +3,5 @@ package service
import "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
type RobTaskService interface {
Rob(taskId int64, receiverUid int64) (*domain.Task, error)
Rob(taskId int64, bidderUid int64) (*domain.Task, error)
}
... ...
... ... @@ -201,6 +201,8 @@ func (status *UnClaimedStatus) Bib(task *Task, bidder *EmployeeInfo) error {
Bidder: bidder,
BidTime: bidTime,
})
task.TaskStatus = TASK_STATUS_UNDERWAY
task.CurrentStatus = &UnderwayStatus{}
return nil
}
}
... ...
... ... @@ -31,6 +31,15 @@ func (dao *TaskDao) AddBidInfo(taskId int64, bidStartTime time.Time, bidEndTime
return err
}
func (dao *TaskDao) AddBidderInfo(taskId int64, bidder *domain.EmployeeInfo) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"INSERT INTO bidder_infos (task_id, bidder, bid_time) VALUES (?, ?, ?)",
taskId, bidder, time.Now())
return err
}
func NewTaskDao(transactionContext *pgTransaction.TransactionContext) (*TaskDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
package domain_service
import (
"fmt"
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/infrastructure/dao"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
)
type BidTaskService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (service *BidTaskService) Bid(taskId int64, bidderUid int64) (*domain.Task, error) {
var employeeRepository domain.EmployeeRepository
var taskRepository domain.TaskRepository
var taskDao *dao.TaskDao
if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
return nil, err
} else {
employeeRepository = repository
}
if repository, err := repository.NewTaskRepository(service.transactionContext); err != nil {
return nil, err
} else {
taskRepository = repository
}
if dao, err := dao.NewTaskDao(service.transactionContext); err != nil {
return nil, err
} else {
taskDao = dao
}
bidder, err := employeeRepository.FindOne(map[string]interface{}{
"uid": bidderUid,
})
if err != nil {
return nil, err
}
if bidder == nil {
return nil, fmt.Errorf("无效的竞标人")
}
task, err := taskRepository.FindOne(map[string]interface{}{
"taskId": taskId,
})
if err != nil {
return nil, err
}
if task == nil {
return nil, fmt.Errorf("无效的任务")
}
if bidder.EmployeeInfo.Uid == task.Sponsor.Uid {
return nil, fmt.Errorf("无法竞标自己发布的任务")
}
if err := task.Bib(bidder.EmployeeInfo); err != nil {
return nil, err
}
if err := taskDao.AddBidderInfo(taskId, bidder.EmployeeInfo); err != nil {
return nil, err
}
if task, err := taskRepository.Save(task); err != nil {
return nil, err
} else {
return task, nil
}
}
func NewBidTaskService(transactionContext *pgTransaction.TransactionContext) (*BidTaskService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &BidTaskService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -38,7 +38,7 @@ func (repository *TaskRepository) Save(task *domain.Task) (*domain.Task, error)
}
} else {
var taskReceiverUid int64
if task.RobInfo != nil {
if task.RobInfo != nil && task.RobInfo.Receiver != nil {
taskReceiverUid = task.RobInfo.Receiver.Uid
}
if task.BidInfo != nil && task.BidInfo.SuccessfulBidder != nil {
... ... @@ -83,7 +83,7 @@ func (repository *TaskRepository) FindOne(queryOptions map[string]interface{}) (
if taskModel.BidInfo != nil {
var bidderInfoModels []*models.BidderInfo
bidderInfoQuery := tx.Model(&bidderInfoModels)
if err := bidderInfoQuery.Where("bid_info_id = ?", taskModel.BidInfo.Id).Select(); err != nil {
if err := bidderInfoQuery.Where("task_id = ?", taskModel.Id).Select(); err != nil {
return nil, err
}
taskModel.BidInfo.BidderInfos = bidderInfoModels
... ... @@ -176,7 +176,7 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
if taskModel.BidInfo != nil {
var bidderInfoModels []*models.BidderInfo
bidderInfoQuery := tx.Model(&bidderInfoModels)
if err := bidderInfoQuery.Where("bid_info_id = ?", taskModel.BidInfo.Id).Select(); err != nil {
if err := bidderInfoQuery.Where("task_id = ?", taskModel.Id).Select(); err != nil {
return int64(count), tasks, nil
}
taskModel.BidInfo.BidderInfos = bidderInfoModels
... ... @@ -205,7 +205,7 @@ func (repository *TaskRepository) transformPgModelToDomainModel(taskModel *model
if taskModel.BidInfo == nil {
bidInfo = nil
} else {
bidderInfos := make([]*domain.BidderInfo, len(taskModel.BidInfo.BidderInfos))
bidderInfos := make([]*domain.BidderInfo, 0)
for _, bidderInfo := range taskModel.BidInfo.BidderInfos {
bidderInfos = append(bidderInfos, &domain.BidderInfo{
Bidder: bidderInfo.Bidder,
... ...
package task
import (
"github.com/go-pg/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"net/http"
"time"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
)
var _ = Describe("对任务进行竞标", func() {
var taskId int64
Describe("对任务进行竞标", func() {
Context("竞标人对已发布的竞标类型任务进行竞标", func() {
BeforeEach(func() {
dayAfter, _ := time.ParseDuration("72h")
_, err := pG.DB.QueryOne(
pg.Scan(&taskId),
"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",
"testTaskId", "testCompanyId", "testTaskName", "testTaskType", "testSponsor", "testTaskStatus", "testReferenceResource", "testCustomerValue", "testTaskNature", "testSuMoney", "testAcceptanceStandard", "testTaskDescription", "testTaskPictureUrls", "testIsRewardTake", "testRobInfo", "testBidInfo", "testParticipators", "testTaskPercentage", "testSolveReport", "testSolvePictureUrls", "testCreateTime", "testReleaseTime")
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, receiver_uid, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
1, 101, "竞标任务1", 2, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 2, "null", pg.Array([]string{"口感", "便利", "品牌", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, []*domain.EmployeeInfo{
{
Uid: 2499036607974745077,
},
{
Uid: 2499036607974745066,
},
}, "null", "", pg.Array([]string{}), 0, time.Now(), time.Now().Add(dayAfter))
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
1, 101, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
Expect(err1).NotTo(HaveOccurred())
_, err2 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
2, 101, 2499036607974745099, "testEmployeeName", "testEmployeeAccount", 0)
Expect(err2).NotTo(HaveOccurred())
_, err3 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?)",
1, time.Date(2020, time.Month(4), 5, 8, 0, 0, 0, time.Now().Location()), time.Now().Add(dayAfter))
Expect(err3).NotTo(HaveOccurred())
_, err4 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO bidder_infos (task_id) VALUES (?)",
1)
Expect(err4).NotTo(HaveOccurred())
})
Describe("对任务进行竞标", func() {
Context("", func() {
It("", func() {
It("竞标成功", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"bidder": "int64",
"bidder": 2499036607974745099,
}
httpExpect.POST("/tasks/{taskId}/bid").
httpExpect.POST("/tasks/1/bid").
WithJSON(body).
Expect().
Status(http.StatusOK).
... ... @@ -34,12 +65,21 @@ var _ = Describe("对任务进行竞标", func() {
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
ContainsKey("data").Value("data").Object().
ContainsKey("taskStatus").ValueEqual("taskStatus", 3).
ContainsKey("bidInfo").Value("bidInfo").Object().
ContainsKey("bidderInfos").Value("bidderInfos").Array().Length().Equal(2)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM tasks WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.Exec("DELETE FROM bid_infos WHERE true")
Expect(err1).NotTo(HaveOccurred())
_, err2 := pG.DB.Exec("DELETE FROM bidder_infos WHERE true")
Expect(err2).NotTo(HaveOccurred())
_, err3 := pG.DB.Exec("DELETE FROM employees WHERE true")
Expect(err3).NotTo(HaveOccurred())
})
})
... ...