作者 linmadan

添加素币与关闭任务记录接口

package query
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type GetOffTaskRecordQuery struct {
// 关闭任务记录ID
OffTaskRecordId int64 `json:"offTaskRecordId" valid:"Required"`
}
func (getOffTaskRecordQuery *GetOffTaskRecordQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getOffTaskRecordQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -284,8 +284,8 @@ func (taskService *TaskService) SearchTask(searchTaskCommand *command.SearchTask
}
// 搜索关闭任务记录
func (taskService *TaskService) SearchOffTaskRecord(searchOffTaskRecord *command.SearchTaskCommand) (interface{}, error) {
if err := searchOffTaskRecord.ValidateCommand(); err != nil {
func (taskService *TaskService) SearchOffTaskRecord(searchOffTaskRecordCommand *command.SearchOffTaskRecordCommand) (interface{}, error) {
if err := searchOffTaskRecordCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
... ... @@ -306,19 +306,88 @@ func (taskService *TaskService) SearchOffTaskRecord(searchOffTaskRecord *command
} else {
taskRepository = value
}
if count, tasks, err := taskRepository.Find(tool_funs.SimpleStructToMap(searchOffTaskRecord)); err != nil {
var offTaskRecordRepository domain.OffTaskRecordRepository
if value, err := factory.CreateOffTaskRecordRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
offTaskRecordRepository = value
}
_, tasks, err := taskRepository.Find(map[string]interface{}{
"companyId": searchOffTaskRecordCommand.CompanyId,
"taskStatus": domain.TASK_STATUS_CLOSED,
"taskContentMatch": searchOffTaskRecordCommand.TaskContentMatch,
"taskType": searchOffTaskRecordCommand.TaskType,
"customerValue": searchOffTaskRecordCommand.CustomerValue,
"taskNature": searchOffTaskRecordCommand.TaskNature,
"offset": searchOffTaskRecordCommand.Offset,
"limit": searchOffTaskRecordCommand.Limit,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
fmt.Println(tasks)
var taskIds []int64
for _, task := range tasks {
taskIds = append(taskIds, task.TaskId)
}
if count, offTaskRecords, err := offTaskRecordRepository.Find(map[string]interface{}{
"taskIds": taskIds,
"customerValue": searchOffTaskRecordCommand.CustomerValue,
"offStartTime": searchOffTaskRecordCommand.OffStartTime,
"offEndTime": searchOffTaskRecordCommand.OffEndTime,
"limit": searchOffTaskRecordCommand.Limit,
}); 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 map[string]interface{}{
"count": count,
"tasks": tasks,
"count": count,
"offTaskRecords": offTaskRecords,
}, nil
}
}
// 返回关闭任务记录
func (taskService *TaskService) GetOffTaskRecord(getOffTaskRecordQuery *query.GetOffTaskRecordQuery) (interface{}, error) {
if err := getOffTaskRecordQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var offTaskRecordRepository domain.OffTaskRecordRepository
if value, err := factory.CreateOffTaskRecordRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
offTaskRecordRepository = value
}
offTaskRecord, err := offTaskRecordRepository.FindOne(map[string]interface{}{"offTaskRecordId": getOffTaskRecordQuery.OffTaskRecordId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if offTaskRecord == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOffTaskRecordQuery.OffTaskRecordId)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return offTaskRecord, nil
}
}
// 创建新任务
func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTaskCommand) (interface{}, error) {
if err := createTaskCommand.ValidateCommand(); err != nil {
... ...
... ... @@ -8,4 +8,6 @@ type TaskPercentageItem struct {
Percentage int `json:"percentage"`
// 分配到的奖励素币
SuMoney float64 `json:"suMoney"`
// 分配到的奖励素币
IssueScore float64 `json:"issueScore"`
}
... ...
... ... @@ -63,7 +63,7 @@ func (service *ExchangeSuMoneyService) Exchange(uid int64, operatorUid int64, su
RecordDescription: recordDescription,
CreateTime: time.Now(),
}
if err := employeeDao.TransferSuMoney(employee.EmployeeInfo.Uid, suMoney); err != nil {
if err := employeeDao.TransferSuMoney(employee.EmployeeInfo.Uid, 0 - suMoney); err != nil {
return nil, err
}
if suMoneyTransactionRecord, err := suMoneyTransactionRecordRepository.Save(suMoneyTransactionRecord); err != nil {
... ...
... ... @@ -2,11 +2,11 @@ package repository
import (
"fmt"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
"time"
)
type OffTaskRecordRepository struct {
... ... @@ -66,8 +66,14 @@ func (repository *OffTaskRecordRepository) Find(queryOptions map[string]interfac
var offTaskRecordModels []*models.OffTaskRecord
offTaskRecords := make([]*domain.OffTaskRecord, 0)
query := tx.Model(&offTaskRecordModels)
if offTaskRecordIds, ok := queryOptions["offTaskRecordIds"]; ok {
query = query.Where(`off_task_record.task_id IN (?)`, pg.In(offTaskRecordIds.([]int64)))
if taskIds, ok := queryOptions["taskIds"]; ok && len(taskIds.([]int64)) > 0 {
query = query.Where(`off_task_record.task_id IN (?)`, pg.In(taskIds.([]int64)))
}
if offStartTime, ok := queryOptions["offStartTime"]; ok && !offStartTime.(time.Time).IsZero() {
query = query.Where(`off_task_record.create_time > ?`, offStartTime)
}
if offEndTime, ok := queryOptions["offEndTime"]; ok && !offEndTime.(time.Time).IsZero() {
query = query.Where(`off_task_record.create_time < ?`, offEndTime)
}
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
... ...
package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/query"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/service"
)
type OffTaskRecordController struct {
beego.Controller
}
func (controller *OffTaskRecordController) GetOffTaskRecord() {
taskService := service.NewTaskService(nil)
getOffTaskRecordQuery := &query.GetOffTaskRecordQuery{}
offTaskRecordId, _ := controller.GetInt64(":offTaskRecordId")
getOffTaskRecordQuery.OffTaskRecordId = offTaskRecordId
data, err := taskService.GetOffTaskRecord(getOffTaskRecordQuery)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *OffTaskRecordController) SearchOffTaskRecord() {
taskService := service.NewTaskService(nil)
searchOffTaskRecordCommand := &command.SearchOffTaskRecordCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), searchOffTaskRecordCommand)
data, err := taskService.SearchOffTaskRecord(searchOffTaskRecordCommand)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
... ...
package routers
import (
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego/controllers"
)
func init() {
beego.Router("/off-task-records/:offTaskRecordId", &controllers.OffTaskRecordController{}, "Get:GetOffTaskRecord")
beego.Router("/off-task-records/search-off-task-record", &controllers.OffTaskRecordController{}, "Post:SearchOffTaskRecord")
}
... ...
package off_task_record
import (
"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() {
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO off_task_records (id, task_id, operator, off_reason, create_time) VALUES (?, ?, ?, ?, ?)",
1, 1, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, "testOffReason", time.Now())
Expect(err).NotTo(HaveOccurred())
})
Describe("返回关闭任务记录", func() {
Context("传入有效的offTaskRecordId", func() {
It("返回关闭任务记录", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/off-task-records/1").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM off_task_records WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package off_task_record
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/astaxie/beego"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/port/beego"
)
func TestOffTaskRecord(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port OffTaskRecord Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = beego.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...
package off_task_record
import (
"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 offTaskRecordId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&offTaskRecordId),
"INSERT INTO off_task_records (id, task_id, operator, off_reason, create_time) VALUES (?, ?, ?, ?, ?) RETURNING id",
1, 1, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, "testOffReason", time.Now())
Expect(err).NotTo(HaveOccurred())
dayAfter, _ := time.ParseDuration("72h")
_, err1 := pG.DB.QueryOne(
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", 1, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 6, "null", pg.Array([]string{"口感", "便利", "品牌", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, []*domain.EmployeeInfo{
{
Uid: 2499036607974745077,
},
{
Uid: 2499036607974745066,
},
}, "null", "", pg.Array([]string{}), 2499036607974745099, time.Now(), time.Now().Add(dayAfter))
Expect(err1).NotTo(HaveOccurred())
})
Describe("搜索关闭任务记录", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": 101,
"taskContentMatch": "",
"taskType": 1,
"customerValue": "",
"taskNature": "",
"offStartTime": time.Date(2020, time.Month(4), 5, 8, 0, 0, 0, time.Now().Location()),
"offEndTime": time.Time{},
"offset": 0,
"limit": 20,
}
httpExpect.POST("/off-task-records/search-off-task-record").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM off_task_records WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.Exec("DELETE FROM tasks WHERE true")
Expect(err1).NotTo(HaveOccurred())
})
})
... ...
package su_money
import (
"net/http"
"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"
"net/http"
)
var _ = Describe("素币兑换", func() {
var suMoneyTransactionRecordId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&suMoneyTransactionRecordId),
"INSERT INTO su_money_transaction_records (su_money_transaction_record_id, record_type, employee, su_money, operator, record_description, create_time) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id",
"testSuMoneyTransactionRecordId", "testRecordType", "testEmployee", "testSuMoney", "testOperator", "testRecordDescription", "testCreateTime")
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", 100.00)
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())
})
Describe("素币兑换", func() {
Context("", func() {
It("", func() {
Context("员工提交正确的数据兑换素币", func() {
It("兑换成功", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"uid": "int64",
"suMoney": "float64",
"operator": "int64",
"exchangeDescription": "string",
"uid": 2499036607974745088,
"suMoney": 50,
"operator": 2499036607974745099,
"exchangeDescription": "兑换说明",
}
httpExpect.POST("/su-money/exchange").
WithJSON(body).
... ... @@ -40,9 +43,30 @@ var _ = Describe("素币兑换", func() {
ContainsKey("data").Value("data").Object()
})
})
Context("员工提交超出现有素币的兑换素币请求", func() {
It("兑换失败", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"uid": 2499036607974745088,
"suMoney": 150,
"operator": 2499036607974745099,
"exchangeDescription": "兑换说明",
}
httpExpect.POST("/su-money/exchange").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 501).
ContainsKey("msg").ValueEqual("msg", "内部服务出错:当前素币不足")
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM su_money_transaction_records WHERE true")
_, err := pG.DB.Exec("DELETE FROM employees WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.Exec("DELETE FROM su_money_transaction_records WHERE true")
Expect(err1).NotTo(HaveOccurred())
})
})
... ...