作者 linmadan

完成任务关闭接口

@@ -13,3 +13,11 @@ func CreateReleaseTaskService(options map[string]interface{}) (service.ReleaseTa @@ -13,3 +13,11 @@ func CreateReleaseTaskService(options map[string]interface{}) (service.ReleaseTa
13 } 13 }
14 return domainService.NewReleaseTaskService(transactionContext) 14 return domainService.NewReleaseTaskService(transactionContext)
15 } 15 }
  16 +
  17 +func CreateOffTaskService(options map[string]interface{}) (service.OffTaskService, error) {
  18 + var transactionContext *pgTransaction.TransactionContext
  19 + if value, ok := options["transactionContext"]; ok {
  20 + transactionContext = value.(*pgTransaction.TransactionContext)
  21 + }
  22 + return domainService.NewOffTaskService(transactionContext)
  23 +}
@@ -21,3 +21,11 @@ func CreateTaskRepository(options map[string]interface{}) (domain.TaskRepository @@ -21,3 +21,11 @@ func CreateTaskRepository(options map[string]interface{}) (domain.TaskRepository
21 } 21 }
22 return repository.NewTaskRepository(transactionContext) 22 return repository.NewTaskRepository(transactionContext)
23 } 23 }
  24 +
  25 +func CreateOffTaskRecordRepository(options map[string]interface{}) (domain.OffTaskRecordRepository, error) {
  26 + var transactionContext *pg.TransactionContext
  27 + if value, ok := options["transactionContext"]; ok {
  28 + transactionContext = value.(*pg.TransactionContext)
  29 + }
  30 + return repository.NewOffTaskRecordRepository(transactionContext)
  31 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/validation"
  8 +)
  9 +
  10 +type SearchOffTaskRecordCommand struct {
  11 + // 公司ID
  12 + CompanyId int64 `json:"companyId" valid:"Required"`
  13 + // 任务内容匹配
  14 + TaskContentMatch string `json:"taskContentMatch,omitempty"`
  15 + // 任务类型
  16 + TaskType int `json:"taskType,omitempty"`
  17 + // 客户价值
  18 + CustomerValue string `json:"customerValue,omitempty"`
  19 + // 任务性质
  20 + TaskNature string `json:"taskNature,omitempty"`
  21 + // 关闭任务时间区间-开始时间
  22 + OffStartTime time.Time `json:"offStartTime,omitempty"`
  23 + // 关闭任务时间区间-截止时间
  24 + OffEndTime time.Time `json:"offEndTime,omitempty"`
  25 + // 查询偏离量
  26 + Offset int `json:"offset,omitempty"`
  27 + // 查询限制
  28 + Limit int `json:"limit,omitempty"`
  29 +}
  30 +
  31 +func (searchOffTaskRecordCommand *SearchOffTaskRecordCommand) ValidateCommand() error {
  32 + valid := validation.Validation{}
  33 + b, err := valid.Valid(searchOffTaskRecordCommand)
  34 + if err != nil {
  35 + return err
  36 + }
  37 + if !b {
  38 + for _, validErr := range valid.Errors {
  39 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  40 + }
  41 + }
  42 + return nil
  43 +}
@@ -147,10 +147,22 @@ func (taskService *TaskService) OffTask(offTaskCommand *command.OffTaskCommand) @@ -147,10 +147,22 @@ func (taskService *TaskService) OffTask(offTaskCommand *command.OffTaskCommand)
147 defer func() { 147 defer func() {
148 transactionContext.RollbackTransaction() 148 transactionContext.RollbackTransaction()
149 }() 149 }()
150 - if err := transactionContext.CommitTransaction(); err != nil {  
151 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 150 + var offTaskService service.OffTaskService
  151 + if value, err := factory.CreateOffTaskService(map[string]interface{}{
  152 + "transactionContext": transactionContext,
  153 + }); err != nil {
  154 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  155 + } else {
  156 + offTaskService = value
  157 + }
  158 + if task, err := offTaskService.Off(offTaskCommand.TaskId, offTaskCommand.Operator, offTaskCommand.OffReason); err != nil {
  159 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  160 + } else {
  161 + if err := transactionContext.CommitTransaction(); err != nil {
  162 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  163 + }
  164 + return task, nil
152 } 165 }
153 - return nil, nil  
154 } 166 }
155 167
156 // 验收任务 168 // 验收任务
@@ -210,6 +222,42 @@ func (taskService *TaskService) SearchTask(searchTaskCommand *command.SearchTask @@ -210,6 +222,42 @@ func (taskService *TaskService) SearchTask(searchTaskCommand *command.SearchTask
210 } 222 }
211 } 223 }
212 224
  225 +// 搜索关闭任务记录
  226 +func (taskService *TaskService) SearchOffTaskRecord(searchOffTaskRecord *command.SearchTaskCommand) (interface{}, error) {
  227 + if err := searchOffTaskRecord.ValidateCommand(); err != nil {
  228 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  229 + }
  230 + transactionContext, err := factory.CreateTransactionContext(nil)
  231 + if err != nil {
  232 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  233 + }
  234 + if err := transactionContext.StartTransaction(); err != nil {
  235 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  236 + }
  237 + defer func() {
  238 + transactionContext.RollbackTransaction()
  239 + }()
  240 + var taskRepository domain.TaskRepository
  241 + if value, err := factory.CreateTaskRepository(map[string]interface{}{
  242 + "transactionContext": transactionContext,
  243 + }); err != nil {
  244 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  245 + } else {
  246 + taskRepository = value
  247 + }
  248 + if count, tasks, err := taskRepository.Find(tool_funs.SimpleStructToMap(searchOffTaskRecord)); err != nil {
  249 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  250 + } else {
  251 + if err := transactionContext.CommitTransaction(); err != nil {
  252 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  253 + }
  254 + return map[string]interface{}{
  255 + "count": count,
  256 + "tasks": tasks,
  257 + }, nil
  258 + }
  259 +}
  260 +
213 // 创建新任务 261 // 创建新任务
214 func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTaskCommand) (interface{}, error) { 262 func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTaskCommand) (interface{}, error) {
215 if err := createTaskCommand.ValidateCommand(); err != nil { 263 if err := createTaskCommand.ValidateCommand(); err != nil {
@@ -4,10 +4,14 @@ package domain @@ -4,10 +4,14 @@ package domain
4 type Employee struct { 4 type Employee struct {
5 // 员工ID 5 // 员工ID
6 EmployeeId int64 `json:"employeeId"` 6 EmployeeId int64 `json:"employeeId"`
  7 + // 公司ID
  8 + CompanyId int64 `json:"companyId"`
7 // 员工信息 9 // 员工信息
8 EmployeeInfo *EmployeeInfo `json:"employeeInfo"` 10 EmployeeInfo *EmployeeInfo `json:"employeeInfo"`
9 // 当前素币 11 // 当前素币
10 SuMoney float64 `json:"suMoney"` 12 SuMoney float64 `json:"suMoney"`
  13 + // 员工状态(启用或者禁用)
  14 + Status int `json:"status"`
11 } 15 }
12 16
13 type EmployeeRepository interface { 17 type EmployeeRepository interface {
@@ -31,6 +35,9 @@ func (employee *Employee) Update(data map[string]interface{}) error { @@ -31,6 +35,9 @@ func (employee *Employee) Update(data map[string]interface{}) error {
31 if employeeAccount, ok := data["employeeAccount"]; ok { 35 if employeeAccount, ok := data["employeeAccount"]; ok {
32 employee.EmployeeInfo.EmployeeAccount = employeeAccount.(string) 36 employee.EmployeeInfo.EmployeeAccount = employeeAccount.(string)
33 } 37 }
  38 + if status, ok := data["status"]; ok {
  39 + employee.Status = status.(int)
  40 + }
34 return nil 41 return nil
35 } 42 }
36 43
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// 关闭任务记录
  6 +type OffTaskRecord struct {
  7 + // 关闭任务记录ID
  8 + OffTaskRecordId int64 `json:"offTaskRecordId"`
  9 + // 任务
  10 + Task *Task `json:"task"`
  11 + // 操作人
  12 + Operator *EmployeeInfo `json:"operator"`
  13 + // 关闭理由
  14 + OffReason string `json:"offReason"`
  15 + // 创建时间
  16 + CreateTime time.Time `json:"createTime"`
  17 +}
  18 +
  19 +type OffTaskRecordRepository interface {
  20 + Save(offTaskRecord *OffTaskRecord) (*OffTaskRecord, error)
  21 + Remove(offTaskRecord *OffTaskRecord) (*OffTaskRecord, error)
  22 + FindOne(queryOptions map[string]interface{}) (*OffTaskRecord, error)
  23 + Find(queryOptions map[string]interface{}) (int64, []*OffTaskRecord, error)
  24 +}
  25 +
  26 +func (offTaskRecord *OffTaskRecord) Identify() interface{} {
  27 + if offTaskRecord.OffTaskRecordId == 0 {
  28 + return nil
  29 + }
  30 + return offTaskRecord.OffTaskRecordId
  31 +}
  32 +
  33 +func (offTaskRecord *OffTaskRecord) Update(data map[string]interface{}) error {
  34 + if offReason, ok := data["offReason"]; ok {
  35 + offTaskRecord.OffReason = offReason.(string)
  36 + }
  37 + return nil
  38 +}
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  4 +
  5 +type OffTaskService interface {
  6 + Off(taskId int64, operatorUid int64, offReason string) (*domain.Task, error)
  7 +}
  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/repository"
  9 + "time"
  10 +)
  11 +
  12 +type OffTaskService struct {
  13 + coreDomain.BaseEventPublisher
  14 + transactionContext *pgTransaction.TransactionContext
  15 +}
  16 +
  17 +func (service *OffTaskService) Off(taskId int64, operatorUid int64, offReason string) (*domain.Task, error) {
  18 + var employeeRepository domain.EmployeeRepository
  19 + var taskRepository domain.TaskRepository
  20 + var offTaskRecordRepository domain.OffTaskRecordRepository
  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 repository, err := repository.NewOffTaskRecordRepository(service.transactionContext); err != nil {
  32 + return nil, err
  33 + } else {
  34 + offTaskRecordRepository = repository
  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 + if err := task.Off(); err != nil {
  58 + return nil, err
  59 + }
  60 + offTaskRecord := &domain.OffTaskRecord{
  61 + Task: task,
  62 + Operator: operator.EmployeeInfo,
  63 + OffReason: offReason,
  64 + CreateTime: time.Now(),
  65 + }
  66 + if _, err := offTaskRecordRepository.Save(offTaskRecord); err != nil {
  67 + return nil, err
  68 + }
  69 + if task, err := taskRepository.Save(task); err != nil {
  70 + return nil, err
  71 + } else {
  72 + return task, nil
  73 + }
  74 +}
  75 +
  76 +func NewOffTaskService(transactionContext *pgTransaction.TransactionContext) (*OffTaskService, error) {
  77 + if transactionContext == nil {
  78 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  79 + } else {
  80 + return &OffTaskService{
  81 + transactionContext: transactionContext,
  82 + }, nil
  83 + }
  84 +}
@@ -28,6 +28,7 @@ func init() { @@ -28,6 +28,7 @@ func init() {
28 (*models.BidderInfo)(nil), 28 (*models.BidderInfo)(nil),
29 (*models.BidInfo)(nil), 29 (*models.BidInfo)(nil),
30 (*models.Task)(nil), 30 (*models.Task)(nil),
  31 + (*models.OffTaskRecord)(nil),
31 } { 32 } {
32 err := DB.CreateTable(model, &orm.CreateTableOptions{ 33 err := DB.CreateTable(model, &orm.CreateTableOptions{
33 Temp: false, 34 Temp: false,
@@ -4,6 +4,8 @@ type Employee struct { @@ -4,6 +4,8 @@ type Employee struct {
4 TableName string `pg:"employees,alias:employee"` 4 TableName string `pg:"employees,alias:employee"`
5 // 员工ID 5 // 员工ID
6 Id int64 `pg:",pk"` 6 Id int64 `pg:",pk"`
  7 + // 公司ID
  8 + CompanyId int64
7 // 统一用户UID 9 // 统一用户UID
8 Uid int64 10 Uid int64
9 // 员工姓名 11 // 员工姓名
@@ -12,4 +14,6 @@ type Employee struct { @@ -12,4 +14,6 @@ type Employee struct {
12 EmployeeAccount string 14 EmployeeAccount string
13 // 当前素币 15 // 当前素币
14 SuMoney float64 16 SuMoney float64
  17 + // 员工状态(启用或者禁用)
  18 + Status int
15 } 19 }
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type OffTaskRecord struct {
  9 + TableName string `pg:"off_task_records,alias:off_task_record"`
  10 + // 关闭任务记录ID
  11 + Id int64 `pg:",pk"`
  12 + // 任务
  13 + TaskId int64
  14 + // 操作人
  15 + Operator *domain.EmployeeInfo
  16 + // 关闭理由
  17 + OffReason string
  18 + // 创建时间
  19 + CreateTime time.Time
  20 +}
@@ -30,16 +30,16 @@ func (repository *EmployeeRepository) Save(employee *domain.Employee) (*domain.E @@ -30,16 +30,16 @@ func (repository *EmployeeRepository) Save(employee *domain.Employee) (*domain.E
30 return employee, err 30 return employee, err
31 } 31 }
32 if _, err := tx.QueryOne( 32 if _, err := tx.QueryOne(
33 - pg.Scan(&employee.EmployeeId, &employee.EmployeeInfo.Uid, &employee.EmployeeInfo.EmployeeName, &employee.EmployeeInfo.EmployeeAccount, &employee.SuMoney),  
34 - "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?) RETURNING id, uid, employee_name, employee_account, su_money",  
35 - employee.EmployeeId, employee.EmployeeInfo.Uid, employee.EmployeeInfo.EmployeeName, employee.EmployeeInfo.EmployeeAccount, employee.SuMoney); err != nil { 33 + pg.Scan(&employee.EmployeeId, &employee.CompanyId, &employee.EmployeeInfo.Uid, &employee.EmployeeInfo.EmployeeName, &employee.EmployeeInfo.EmployeeAccount, &employee.SuMoney, &employee.Status),
  34 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money, status) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id, company_id, uid, employee_name, employee_account, su_money, status",
  35 + employee.EmployeeId, employee.CompanyId, employee.EmployeeInfo.Uid, employee.EmployeeInfo.EmployeeName, employee.EmployeeInfo.EmployeeAccount, employee.SuMoney, employee.Status); err != nil {
36 return employee, err 36 return employee, err
37 } 37 }
38 } else { 38 } else {
39 if _, err := tx.QueryOne( 39 if _, err := tx.QueryOne(
40 - pg.Scan(&employee.EmployeeId, &employee.EmployeeInfo.Uid, &employee.EmployeeInfo.EmployeeName, &employee.EmployeeInfo.EmployeeAccount, &employee.SuMoney),  
41 - "UPDATE employees SET employee_name=?, employee_account=?, su_money=? WHERE uid=? RETURNING id, uid, employee_name, employee_account, su_money",  
42 - employee.EmployeeInfo.EmployeeName, employee.EmployeeInfo.EmployeeAccount, employee.SuMoney, employee.EmployeeInfo.Uid); err != nil { 40 + pg.Scan(&employee.EmployeeId, &employee.CompanyId, &employee.EmployeeInfo.Uid, &employee.EmployeeInfo.EmployeeName, &employee.EmployeeInfo.EmployeeAccount, &employee.SuMoney, &employee.Status),
  41 + "UPDATE employees SET employee_name=?, employee_account=?, su_money=?, status=? WHERE uid=? RETURNING id, company_id, uid, employee_name, employee_account, su_money, status",
  42 + employee.EmployeeInfo.EmployeeName, employee.EmployeeInfo.EmployeeAccount, employee.SuMoney, employee.Status, employee.EmployeeInfo.Uid); err != nil {
43 return employee, err 43 return employee, err
44 } 44 }
45 } 45 }
@@ -73,22 +73,14 @@ func (repository *EmployeeRepository) FindOne(queryOptions map[string]interface{ @@ -73,22 +73,14 @@ func (repository *EmployeeRepository) FindOne(queryOptions map[string]interface{
73 if employeeModel.Id == 0 { 73 if employeeModel.Id == 0 {
74 return nil, nil 74 return nil, nil
75 } else { 75 } else {
76 - return &domain.Employee{  
77 - EmployeeId: employeeModel.Id,  
78 - EmployeeInfo: &domain.EmployeeInfo{  
79 - Uid: employeeModel.Uid,  
80 - EmployeeName: employeeModel.EmployeeName,  
81 - EmployeeAccount: employeeModel.EmployeeAccount,  
82 - },  
83 - SuMoney: employeeModel.SuMoney,  
84 - }, nil 76 + return repository.transformPgModelToDomainModel(employeeModel)
85 } 77 }
86 } 78 }
87 79
88 func (repository *EmployeeRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Employee, error) { 80 func (repository *EmployeeRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Employee, error) {
89 tx := repository.transactionContext.PgTx 81 tx := repository.transactionContext.PgTx
90 var employeeModels []*models.Employee 82 var employeeModels []*models.Employee
91 - var employees []*domain.Employee 83 + employees := make([]*domain.Employee, 0)
92 query := tx.Model(&employeeModels) 84 query := tx.Model(&employeeModels)
93 if offset, ok := queryOptions["offset"]; ok { 85 if offset, ok := queryOptions["offset"]; ok {
94 offset := offset.(int) 86 offset := offset.(int)
@@ -107,23 +99,33 @@ func (repository *EmployeeRepository) Find(queryOptions map[string]interface{}) @@ -107,23 +99,33 @@ func (repository *EmployeeRepository) Find(queryOptions map[string]interface{})
107 query = query.Limit(20) 99 query = query.Limit(20)
108 } 100 }
109 if count, err := query.Order("uid DESC").SelectAndCount(); err != nil { 101 if count, err := query.Order("uid DESC").SelectAndCount(); err != nil {
110 - return 0, nil, err 102 + return 0, employees, err
111 } else { 103 } else {
112 for _, employeeModel := range employeeModels { 104 for _, employeeModel := range employeeModels {
113 - employees = append(employees, &domain.Employee{  
114 - EmployeeId: employeeModel.Id,  
115 - EmployeeInfo: &domain.EmployeeInfo{  
116 - Uid: employeeModel.Uid,  
117 - EmployeeName: employeeModel.EmployeeName,  
118 - EmployeeAccount: employeeModel.EmployeeAccount,  
119 - },  
120 - SuMoney: employeeModel.SuMoney,  
121 - }) 105 + if employee, err := repository.transformPgModelToDomainModel(employeeModel); err != nil {
  106 + return 0, employees, err
  107 + } else {
  108 + employees = append(employees, employee)
  109 + }
122 } 110 }
123 return int64(count), employees, nil 111 return int64(count), employees, nil
124 } 112 }
125 } 113 }
126 114
  115 +func (repository *EmployeeRepository) transformPgModelToDomainModel(employeeModel *models.Employee) (*domain.Employee, error) {
  116 + return &domain.Employee{
  117 + EmployeeId: employeeModel.Id,
  118 + CompanyId: employeeModel.CompanyId,
  119 + EmployeeInfo: &domain.EmployeeInfo{
  120 + Uid: employeeModel.Uid,
  121 + EmployeeName: employeeModel.EmployeeName,
  122 + EmployeeAccount: employeeModel.EmployeeAccount,
  123 + },
  124 + SuMoney: employeeModel.SuMoney,
  125 + Status: employeeModel.Status,
  126 + }, nil
  127 +}
  128 +
127 func NewEmployeeRepository(transactionContext *pgTransaction.TransactionContext) (*EmployeeRepository, error) { 129 func NewEmployeeRepository(transactionContext *pgTransaction.TransactionContext) (*EmployeeRepository, error) {
128 if transactionContext == nil { 130 if transactionContext == nil {
129 return nil, fmt.Errorf("transactionContext参数不能为nil") 131 return nil, fmt.Errorf("transactionContext参数不能为nil")
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/go-pg/pg"
  7 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
  10 +)
  11 +
  12 +type OffTaskRecordRepository struct {
  13 + transactionContext *pgTransaction.TransactionContext
  14 +}
  15 +
  16 +func (repository *OffTaskRecordRepository) nextIdentify() (int64, error) {
  17 + return 0, nil
  18 +}
  19 +func (repository *OffTaskRecordRepository) Save(offTaskRecord *domain.OffTaskRecord) (*domain.OffTaskRecord, error) {
  20 + tx := repository.transactionContext.PgTx
  21 + if offTaskRecord.Identify() == nil {
  22 + _, err := repository.nextIdentify()
  23 + if err != nil {
  24 + return offTaskRecord, err
  25 + }
  26 + if _, err := tx.QueryOne(
  27 + pg.Scan(&offTaskRecord.OffTaskRecordId, &offTaskRecord.Task.TaskId, &offTaskRecord.Operator, &offTaskRecord.OffReason, &offTaskRecord.CreateTime),
  28 + "INSERT INTO off_task_records (task_id, operator, off_reason, create_time) VALUES (?, ?, ?, ?) RETURNING id, task_id, operator, off_reason, create_time",
  29 + offTaskRecord.Task.TaskId, offTaskRecord.Operator, offTaskRecord.OffReason, offTaskRecord.CreateTime); err != nil {
  30 + return offTaskRecord, err
  31 + }
  32 + } else {
  33 + if _, err := tx.QueryOne(
  34 + pg.Scan(&offTaskRecord.OffTaskRecordId, &offTaskRecord.Task, &offTaskRecord.Operator, &offTaskRecord.OffReason, &offTaskRecord.CreateTime),
  35 + "UPDATE off_task_records SET task_id=?, operator=?, off_reason=?, create_time=? WHERE id=? RETURNING id, task_id, operator, off_reason, create_time",
  36 + offTaskRecord.Task.TaskId, offTaskRecord.Operator, offTaskRecord.OffReason, offTaskRecord.CreateTime, offTaskRecord.OffTaskRecordId); err != nil {
  37 + return offTaskRecord, err
  38 + }
  39 + }
  40 + return offTaskRecord, nil
  41 +}
  42 +func (repository *OffTaskRecordRepository) Remove(offTaskRecord *domain.OffTaskRecord) (*domain.OffTaskRecord, error) {
  43 + tx := repository.transactionContext.PgTx
  44 + offTaskRecordModel := new(models.OffTaskRecord)
  45 + offTaskRecordModel.Id = offTaskRecord.Identify().(int64)
  46 + if _, err := tx.Model(offTaskRecordModel).WherePK().Delete(); err != nil {
  47 + return offTaskRecord, err
  48 + }
  49 + return offTaskRecord, nil
  50 +}
  51 +func (repository *OffTaskRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.OffTaskRecord, error) {
  52 + tx := repository.transactionContext.PgTx
  53 + offTaskRecordModel := new(models.OffTaskRecord)
  54 + query := tx.Model(offTaskRecordModel)
  55 + if offTaskRecordId, ok := queryOptions["offTaskRecordId"]; ok {
  56 + query = query.Where("off_task_record.id = ?", offTaskRecordId)
  57 + }
  58 + if err := query.First(); err != nil {
  59 + if err.Error() == "pg: no rows in result set" {
  60 + return nil, fmt.Errorf("没有此资源")
  61 + } else {
  62 + return nil, err
  63 + }
  64 + }
  65 + if offTaskRecordModel.Id == 0 {
  66 + return nil, nil
  67 + } else {
  68 + return repository.transformPgModelToDomainModel(offTaskRecordModel)
  69 + }
  70 +}
  71 +func (repository *OffTaskRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OffTaskRecord, error) {
  72 + tx := repository.transactionContext.PgTx
  73 + var offTaskRecordModels []*models.OffTaskRecord
  74 + offTaskRecords := make([]*domain.OffTaskRecord, 0)
  75 + query := tx.Model(&offTaskRecordModels)
  76 + if offTaskRecordIds, ok := queryOptions["offTaskRecordIds"]; ok {
  77 + query = query.Where(`off_task_record.task_id IN (?)`, pg.In(offTaskRecordIds.([]int64)))
  78 + }
  79 + if offset, ok := queryOptions["offset"]; ok {
  80 + offset := offset.(int)
  81 + if offset > -1 {
  82 + query = query.Offset(offset)
  83 + }
  84 + } else {
  85 + query = query.Offset(0)
  86 + }
  87 + if limit, ok := queryOptions["limit"]; ok {
  88 + limit := limit.(int)
  89 + if limit > -1 {
  90 + query = query.Limit(limit)
  91 + }
  92 + } else {
  93 + query = query.Limit(20)
  94 + }
  95 + if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
  96 + return 0, offTaskRecords, err
  97 + } else {
  98 + for _, offTaskRecordModel := range offTaskRecordModels {
  99 + if offTaskRecord, err := repository.transformPgModelToDomainModel(offTaskRecordModel); err != nil {
  100 + return 0, offTaskRecords, err
  101 + } else {
  102 + offTaskRecords = append(offTaskRecords, offTaskRecord)
  103 + }
  104 + offTaskRecords = append(offTaskRecords, &domain.OffTaskRecord{})
  105 + }
  106 + return int64(count), offTaskRecords, nil
  107 + }
  108 +}
  109 +func (repository *OffTaskRecordRepository) transformPgModelToDomainModel(offTaskRecordModel *models.OffTaskRecord) (*domain.OffTaskRecord, error) {
  110 + return &domain.OffTaskRecord{
  111 + OffTaskRecordId: offTaskRecordModel.Id,
  112 + Task: &domain.Task{
  113 + TaskId: offTaskRecordModel.TaskId,
  114 + },
  115 + Operator: offTaskRecordModel.Operator,
  116 + OffReason: offTaskRecordModel.OffReason,
  117 + CreateTime: offTaskRecordModel.CreateTime,
  118 + }, nil
  119 +}
  120 +func NewOffTaskRecordRepository(transactionContext *pgTransaction.TransactionContext) (*OffTaskRecordRepository, error) {
  121 + if transactionContext == nil {
  122 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  123 + } else {
  124 + return &OffTaskRecordRepository{
  125 + transactionContext: transactionContext,
  126 + }, nil
  127 + }
  128 +}
@@ -143,6 +143,16 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int @@ -143,6 +143,16 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
143 if taskIds, ok := queryOptions["taskIds"]; ok { 143 if taskIds, ok := queryOptions["taskIds"]; ok {
144 query = query.Where(`task.task_id IN (?)`, pg.In(taskIds.([]int64))) 144 query = query.Where(`task.task_id IN (?)`, pg.In(taskIds.([]int64)))
145 } 145 }
  146 +
  147 + if offRangTime, ok := queryOptions["offRangTime"]; ok {
  148 + query = query.Join("JOIN off_task_records ON off_task_record.task_id = task.id")
  149 + if offStartTime, ok := offRangTime.(map[string]time.Time)["offStartTime"]; ok {
  150 + query = query.Where("off_task_record.create_time > ?", offStartTime)
  151 + }
  152 + if offEndTime, ok := offRangTime.(map[string]time.Time)["offEndTime"]; ok {
  153 + query = query.Where("off_task_record.create_time < ?", offEndTime)
  154 + }
  155 + }
146 if offset, ok := queryOptions["offset"]; ok { 156 if offset, ok := queryOptions["offset"]; ok {
147 offset := offset.(int) 157 offset := offset.(int)
148 if offset > -1 { 158 if offset > -1 {
@@ -15,8 +15,10 @@ var _ = Describe("创建新员工", func() { @@ -15,8 +15,10 @@ var _ = Describe("创建新员工", func() {
15 httpExpect := httpexpect.New(GinkgoT(), server.URL) 15 httpExpect := httpexpect.New(GinkgoT(), server.URL)
16 body := map[string]interface{}{ 16 body := map[string]interface{}{
17 "uid": 123456, 17 "uid": 123456,
  18 + "companyId": 101,
18 "employeeName": "蔡晓生", 19 "employeeName": "蔡晓生",
19 "employeeAccount": "13799999999", 20 "employeeAccount": "13799999999",
  21 + "status": 1,
20 } 22 }
21 httpExpect.POST("/employees/"). 23 httpExpect.POST("/employees/").
22 WithJSON(body). 24 WithJSON(body).
@@ -14,8 +14,8 @@ var _ = Describe("返回员工", func() { @@ -14,8 +14,8 @@ var _ = Describe("返回员工", func() {
14 BeforeEach(func() { 14 BeforeEach(func() {
15 _, err := pG.DB.QueryOne( 15 _, err := pG.DB.QueryOne(
16 pg.Scan(&employeeId), 16 pg.Scan(&employeeId),
17 - "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?) RETURNING id",  
18 - 1, 123456, "testEmployeeName", "testEmployeeAccount", 0) 17 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?) RETURNING id",
  18 + 1, 101, 123456, "testEmployeeName", "testEmployeeAccount", 0)
19 Expect(err).NotTo(HaveOccurred()) 19 Expect(err).NotTo(HaveOccurred())
20 }) 20 })
21 Describe("根据uid参数返回员工", func() { 21 Describe("根据uid参数返回员工", func() {
@@ -14,8 +14,8 @@ var _ = Describe("返回员工列表", func() { @@ -14,8 +14,8 @@ var _ = Describe("返回员工列表", func() {
14 BeforeEach(func() { 14 BeforeEach(func() {
15 _, err := pG.DB.QueryOne( 15 _, err := pG.DB.QueryOne(
16 pg.Scan(&employeeId), 16 pg.Scan(&employeeId),
17 - "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?) RETURNING id",  
18 - 1, 123456, "testEmployeeName", "testEmployeeAccount", 0) 17 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?) RETURNING id",
  18 + 1, 101, 123456, "testEmployeeName", "testEmployeeAccount", 0)
19 Expect(err).NotTo(HaveOccurred()) 19 Expect(err).NotTo(HaveOccurred())
20 }) 20 })
21 Describe("根据参数返回员工列表", func() { 21 Describe("根据参数返回员工列表", func() {
@@ -15,8 +15,8 @@ var _ = Describe("移除员工", func() { @@ -15,8 +15,8 @@ var _ = Describe("移除员工", func() {
15 BeforeEach(func() { 15 BeforeEach(func() {
16 _, err := pG.DB.QueryOne( 16 _, err := pG.DB.QueryOne(
17 pg.Scan(&employeeId), 17 pg.Scan(&employeeId),
18 - "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?) RETURNING id",  
19 - 1, 123456, "testEmployeeName", "testEmployeeAccount", 0) 18 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?) RETURNING id",
  19 + 1, 101, 123456, "testEmployeeName", "testEmployeeAccount", 0)
20 Expect(err).NotTo(HaveOccurred()) 20 Expect(err).NotTo(HaveOccurred())
21 }) 21 })
22 Describe("根据参数移除员工", func() { 22 Describe("根据参数移除员工", func() {
@@ -15,8 +15,8 @@ var _ = Describe("更新员工", func() { @@ -15,8 +15,8 @@ var _ = Describe("更新员工", func() {
15 BeforeEach(func() { 15 BeforeEach(func() {
16 _, err := pG.DB.QueryOne( 16 _, err := pG.DB.QueryOne(
17 pg.Scan(&employeeId), 17 pg.Scan(&employeeId),
18 - "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?) RETURNING id",  
19 - 1, 123456, "testEmployeeName", "testEmployeeAccount", 0) 18 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?) RETURNING id",
  19 + 1, 101, 123456, "testEmployeeName", "testEmployeeAccount", 0)
20 Expect(err).NotTo(HaveOccurred()) 20 Expect(err).NotTo(HaveOccurred())
21 }) 21 })
22 Describe("提交数据更新员工", func() { 22 Describe("提交数据更新员工", func() {
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  
15 - BeforeEach(func() {  
16 - _, 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")  
20 - Expect(err).NotTo(HaveOccurred())  
21 - })  
22 Describe("关闭任务", func() { 16 Describe("关闭任务", func() {
23 - Context("", func() {  
24 - It("", func() { 17 + Context("任务发起者关闭任务", func() {
  18 + BeforeEach(func() {
  19 + dayAfter, _ := time.ParseDuration("72h")
  20 + _, err := pG.DB.QueryOne(
  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", 1, &domain.EmployeeInfo{
  24 + Uid: 2499036607974745088,
  25 + }, 3, "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{}), 2499036607974745099, time.Now(), time.Now().Add(dayAfter))
  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 + })
  40 + It("返回被关闭的任务", func() {
25 httpExpect := httpexpect.New(GinkgoT(), server.URL) 41 httpExpect := httpexpect.New(GinkgoT(), server.URL)
26 body := map[string]interface{}{ 42 body := map[string]interface{}{
27 - "operator": "int64",  
28 - "offReason": "string", 43 + "operator": 2499036607974745088,
  44 + "offReason": "测试关闭任务",
29 } 45 }
30 - httpExpect.POST("/tasks/{taskId}/dff"). 46 + httpExpect.POST("/tasks/1/dff").
31 WithJSON(body). 47 WithJSON(body).
32 Expect(). 48 Expect().
33 Status(http.StatusOK). 49 Status(http.StatusOK).
@@ -35,12 +51,60 @@ var _ = Describe("关闭任务", func() { @@ -35,12 +51,60 @@ var _ = Describe("关闭任务", func() {
35 Object(). 51 Object().
36 ContainsKey("code").ValueEqual("code", 0). 52 ContainsKey("code").ValueEqual("code", 0).
37 ContainsKey("msg").ValueEqual("msg", "ok"). 53 ContainsKey("msg").ValueEqual("msg", "ok").
38 - ContainsKey("data").Value("data").Object() 54 + ContainsKey("data").Value("data").Object().
  55 + ContainsKey("taskStatus").ValueEqual("taskStatus", 6)
  56 + })
  57 + })
  58 + Context("任务发起者关闭已经关闭的任务", func() {
  59 + BeforeEach(func() {
  60 + dayAfter, _ := time.ParseDuration("72h")
  61 + _, err := pG.DB.QueryOne(
  62 + pg.Scan(),
  63 + "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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  64 + 1, 101, "抢单任务1", 1, &domain.EmployeeInfo{
  65 + Uid: 2499036607974745088,
  66 + }, 6, "null", pg.Array([]string{"口感", "便利", "品牌", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, []*domain.EmployeeInfo{
  67 + {
  68 + Uid: 2499036607974745077,
  69 + },
  70 + {
  71 + Uid: 2499036607974745066,
  72 + },
  73 + }, "null", "", pg.Array([]string{}), 2499036607974745099, time.Now(), time.Now().Add(dayAfter))
  74 + Expect(err).NotTo(HaveOccurred())
  75 + _, err1 := pG.DB.QueryOne(
  76 + pg.Scan(),
  77 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
  78 + 1, 101, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
  79 + Expect(err1).NotTo(HaveOccurred())
  80 + })
  81 + It("关闭失败", func() {
  82 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  83 + body := map[string]interface{}{
  84 + "operator": 2499036607974745088,
  85 + "offReason": "测试关闭任务",
  86 + }
  87 + httpExpect.POST("/tasks/1/dff").
  88 + WithJSON(body).
  89 + Expect().
  90 + Status(http.StatusOK).
  91 + JSON().
  92 + Object().
  93 + ContainsKey("code").ValueEqual("code", 501).
  94 + ContainsKey("msg").ValueEqual("msg", "内部服务出错:已关闭的任务不允许关闭")
39 }) 95 })
40 }) 96 })
41 }) 97 })
42 AfterEach(func() { 98 AfterEach(func() {
43 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true") 99 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true")
44 Expect(err).NotTo(HaveOccurred()) 100 Expect(err).NotTo(HaveOccurred())
  101 + _, err1 := pG.DB.Exec("DELETE FROM bid_infos WHERE true")
  102 + Expect(err1).NotTo(HaveOccurred())
  103 + _, err2 := pG.DB.Exec("DELETE FROM bidder_infos WHERE true")
  104 + Expect(err2).NotTo(HaveOccurred())
  105 + _, err3 := pG.DB.Exec("DELETE FROM employees WHERE true")
  106 + Expect(err3).NotTo(HaveOccurred())
  107 + _, err4 := pG.DB.Exec("DELETE FROM off_task_records WHERE true")
  108 + Expect(err4).NotTo(HaveOccurred())
45 }) 109 })
46 }) 110 })
@@ -32,8 +32,8 @@ var _ = Describe("发布任务", func() { @@ -32,8 +32,8 @@ var _ = Describe("发布任务", func() {
32 Expect(err).NotTo(HaveOccurred()) 32 Expect(err).NotTo(HaveOccurred())
33 _, err1 := pG.DB.QueryOne( 33 _, err1 := pG.DB.QueryOne(
34 pg.Scan(), 34 pg.Scan(),
35 - "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?)",  
36 - 1, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0) 35 + "INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
  36 + 1, 101, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
37 Expect(err1).NotTo(HaveOccurred()) 37 Expect(err1).NotTo(HaveOccurred())
38 }) 38 })
39 It("发布任务成功", func() { 39 It("发布任务成功", func() {
@@ -49,7 +49,8 @@ var _ = Describe("发布任务", func() { @@ -49,7 +49,8 @@ var _ = Describe("发布任务", func() {
49 Object(). 49 Object().
50 ContainsKey("code").ValueEqual("code", 0). 50 ContainsKey("code").ValueEqual("code", 0).
51 ContainsKey("msg").ValueEqual("msg", "ok"). 51 ContainsKey("msg").ValueEqual("msg", "ok").
52 - ContainsKey("data").Value("data").Object() 52 + ContainsKey("data").Value("data").Object().
  53 + ContainsKey("taskStatus").ValueEqual("taskStatus", 2)
53 }) 54 })
54 }) 55 })
55 Context("任务发起者发布不是待发布的任务", func() { 56 Context("任务发起者发布不是待发布的任务", func() {