作者 linmadan

添加统计功能,重构任务查询参数

package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type PersonSuMoneyStatisticsCommand struct {
// 统一用户UID
Uid int64 `json:"uid" valid:"Required"`
}
func (personSuMoneyStatisticsCommand *PersonSuMoneyStatisticsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(personSuMoneyStatisticsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type PersonTaskStatisticsCommand struct {
// 统一用户UID
Uid int64 `json:"uid" valid:"Required"`
}
func (personTaskStatisticsCommand *PersonTaskStatisticsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(personTaskStatisticsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type SystemTaskStatisticsCommand struct {
// 公司ID
CompanyId int64 `json:"companyId" valid:"Required"`
}
func (systemTaskStatisticsCommand *SystemTaskStatisticsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(systemTaskStatisticsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/statistics/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
)
// 数据统计服务
type StatisticsService struct {
}
// 获取系统任务统计
func (statisticsService *StatisticsService) SystemTaskStatistics(systemTaskStatisticsCommand *command.SystemTaskStatisticsCommand) (interface{}, error) {
if err := systemTaskStatisticsCommand.ValidateCommand(); 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 taskDao *dao.TaskDao
if value, err := factory.CreateTaskDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
taskDao = value
}
if systemTaskStatistics, err := taskDao.CalculateSystemTask(systemTaskStatisticsCommand.CompanyId); 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 systemTaskStatistics, nil
}
}
// 获取个人任务统计
func (statisticsService *StatisticsService) PersonTaskStatistics(personTaskStatisticsCommand *command.PersonTaskStatisticsCommand) (interface{}, error) {
if err := personTaskStatisticsCommand.ValidateCommand(); 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 taskDao *dao.TaskDao
if value, err := factory.CreateTaskDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
taskDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personTaskStatisticsCommand.Uid,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
if personTaskStatistics, err := taskDao.CalculatePersonTask(personTaskStatisticsCommand.Uid); 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 personTaskStatistics, nil
}
}
// 获取个人素币统计
func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMoneyStatisticsCommand *command.PersonSuMoneyStatisticsCommand) (interface{}, error) {
if err := personSuMoneyStatisticsCommand.ValidateCommand(); 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 employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personSuMoneyStatisticsCommand.Uid,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
if personSuMoneyStatistics, err := employeeDao.CalculatePersonSuMoney(personSuMoneyStatisticsCommand.Uid); 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())
}
personSuMoneyStatistics.CurrentSuMoney = employee.SuMoney
return personSuMoneyStatistics, nil
}
}
func NewStatisticsService(options map[string]interface{}) *StatisticsService {
newStatisticsService := &StatisticsService{}
return newStatisticsService
}
... ...
... ... @@ -2,6 +2,7 @@ package command
import (
"fmt"
"time"
"github.com/astaxie/beego/validation"
)
... ... @@ -12,6 +13,10 @@ type SearchSuMoneyTransactionRecordCommand struct {
// 记录类型(1兑换,2任务奖励)
RecordType int `json:"recordType" valid:"Required"`
// 操作人UID
// 事务时间区间-开始时间
TransactionStartTime time.Time `json:"transactionStartTime,omitempty"`
// 事务时间区间-截止时间
TransactionEndTime time.Time `json:"transactionEndTime,omitempty"`
Operator int64 `json:"operator,omitempty"`
// 查询偏离量
Offset int `json:"offset,omitempty"`
... ...
... ... @@ -23,6 +23,8 @@ type SearchTaskCommand struct {
TaskNature string `json:"taskNature,omitempty"`
// 是否悬赏任务
IsRewardTake bool `json:"isRewardTake,omitempty"`
// 竞标参与者UID
Bidder int64 `json:"bidder,omitempty"`
// 竞标时间(1全部,2已截止,3未截止)
BidTimeMatch int `json:"bidTimeMatch,omitempty"`
// 任务领取人
... ...
... ... @@ -25,6 +25,8 @@ type ListTaskQuery struct {
TaskNature string `json:"taskNature,omitempty"`
// 是否悬赏任务
IsRewardTake bool `json:"isRewardTake,omitempty"`
// 竞标参与者UID
Bidder int64 `json:"bidder,omitempty"`
// 竞标时间(1全部,2已截止,3未截止)
BidTimeMatch int `json:"bidTimeMatch,omitempty"`
// 任务领取人
... ...
package domain
// 个人素币统计
type PersonSuMoneyStatistics struct {
// 当前素币
CurrentSuMoney float64 `json:"currentSuMoney"`
// 昨日收益
IncomeSuMoneyOfYesterday float64 `json:"incomeSuMoneyOfYesterday"`
}
... ...
package domain
// 个人任务统计
type PersonTaskStatistics struct {
// 个人领取的进行中任务
UnderwayAsReceiver int64 `json:"underwayAsReceiver"`
// 个人领取的待验收任务
UnAcceptanceAsReceiver int64 `json:"unAcceptanceAsReceiver"`
// 个人领取的已完成任务
CompletedAsReceiver int64 `json:"completedAsReceiver"`
// 个人发起的待发布任务
UnReleasedAsSponsor int64 `json:"unReleasedAsSponsor"`
// 个人发起的待领取任务
UnClaimedAsSponsor int64 `json:"unClaimedAsSponsor"`
// 个人发起的进行中任务
UnderwayAsSponsor int64 `json:"underwayAsSponsor"`
// 个人发起的待验收任务
UnAcceptanceAsSponsor int64 `json:"unAcceptanceAsSponsor"`
// 个人发起的已完成任务
CompletedAsSponsor int64 `json:"completedAsSponsor"`
// 个人参与的竞标中任务
BidAsParticipator int64 `json:"bidAsParticipator"`
// 个人参与的已完成任务
CompletedAsParticipator int64 `json:"completedAsParticipator"`
}
... ...
package domain
// 系统任务统计
type SystemTaskStatistics struct {
// 系统已发布任务
Released int64 `json:"released"`
// 系统进行中任务
Underway int64 `json:"underway"`
// 系统已完成任务
Completed int64 `json:"completed"`
}
... ...
... ... @@ -4,6 +4,9 @@ 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 EmployeeDao struct {
... ... @@ -19,6 +22,24 @@ func (dao *EmployeeDao) TransferSuMoney(uid int64, suMoney float64) error {
return err
}
func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (*domain.PersonSuMoneyStatistics, error) {
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
personSuMoneyStatistics := &domain.PersonSuMoneyStatistics{}
yesterday := time.Now().AddDate(0, 0, -1)
if err := tx.Model(suMoneyTransactionRecordModel).
ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
Where(`su_money_transaction_record.record_type = ?`, 2).
Where(`su_money_transaction_record.create_time > ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location())).
Where(`su_money_transaction_record.create_time < ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, yesterday.Location())).
Select(&personSuMoneyStatistics.IncomeSuMoneyOfYesterday); err != nil {
return nil, err
} else {
return personSuMoneyStatistics, nil
}
}
func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -2,6 +2,7 @@ package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
"time"
"github.com/go-pg/pg"
... ... @@ -49,6 +50,124 @@ func (dao *TaskDao) SetSuccessfulBidder(taskId int64, successfulBidder *domain.E
return err
}
func (dao *TaskDao) CalculateSystemTask(companyId int64) (*domain.SystemTaskStatistics, error) {
tx := dao.transactionContext.PgTx
taskModel := new(models.Task)
systemTaskStatistics := &domain.SystemTaskStatistics{}
if count, err := tx.Model(taskModel).
Where("task.company_id = ?", companyId).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
} else {
systemTaskStatistics.Released = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.company_id = ?", companyId).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
systemTaskStatistics.Underway = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.company_id = ?", companyId).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
systemTaskStatistics.Completed = int64(count)
}
return systemTaskStatistics, nil
}
func (dao *TaskDao) CalculatePersonTask(uid int64) (*domain.PersonTaskStatistics, error) {
tx := dao.transactionContext.PgTx
taskModel := new(models.Task)
personTaskStatistics := &domain.PersonTaskStatistics{}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.UnderwayAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.UnAcceptanceAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where("task.receiver_uid = ?", uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.CompletedAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNRELEASED).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.UnReleasedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.UnClaimedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.UnderwayAsReceiver = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.UnAcceptanceAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.sponsor @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.CompletedAsSponsor = int64(count)
}
if count, err := tx.Model(taskModel).Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id").
Where(`bidder_info.Bidder @> '{"uid":?}'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.BidAsParticipator = int64(count)
}
if count, err := tx.Model(taskModel).
Where(`task.participators @> '[{"uid":?}]'`, uid).
Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
Count(); err != nil {
return nil, err
} else {
personTaskStatistics.CompletedAsParticipator = int64(count)
}
return personTaskStatistics, nil
}
func NewTaskDao(transactionContext *pgTransaction.TransactionContext) (*TaskDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -2,6 +2,7 @@ package repository
import (
"fmt"
"time"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -75,6 +76,12 @@ func (repository *SuMoneyTransactionRecordRepository) Find(queryOptions map[stri
if operator, ok := queryOptions["operator"]; ok && (operator != int64(0)) {
query = query.Where(`su_money_transaction_record.operator @> '{"uid":?}'`, operator)
}
if transactionStartTime, ok := queryOptions["transactionStartTime"]; ok && !transactionStartTime.(time.Time).IsZero() {
query = query.Where(`su_money_transaction_record.create_time > ?`, transactionStartTime)
}
if transactionEndTime, ok := queryOptions["transactionEndTime"]; ok && !transactionEndTime.(time.Time).IsZero() {
query = query.Where(`su_money_transaction_record.create_time < ?`, transactionEndTime)
}
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
... ...
... ... @@ -137,6 +137,10 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
query = query.Where("bid_info.bid_end_time > ?", time.Now())
}
}
if bidder, ok := queryOptions["bidder"]; ok && (bidder != int64(0)) {
query = query.Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id")
query = query.Where(`bidder_info.Bidder @> '{"uid":?}'`, bidder)
}
if receiver, ok := queryOptions["receiver"]; ok && (receiver != int64(0)) {
query = query.Where(`task.receiver_uid = ?`, receiver)
}
... ... @@ -146,7 +150,6 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
if taskIds, ok := queryOptions["taskIds"]; ok {
query = query.Where(`task.task_id IN (?)`, pg.In(taskIds.([]int64)))
}
if offRangTime, ok := queryOptions["offRangTime"]; ok {
query = query.Join("JOIN off_task_records ON off_task_record.task_id = task.id")
if offStartTime, ok := offRangTime.(map[string]time.Time)["offStartTime"]; ok {
... ...
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/statistics/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/statistics/service"
)
type StatisticsController struct {
beego.Controller
}
func (controller *StatisticsController) SystemTaskStatistics() {
statisticsService := service.NewStatisticsService(nil)
systemTaskStatisticsCommand := &command.SystemTaskStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), systemTaskStatisticsCommand)
data, err := statisticsService.SystemTaskStatistics(systemTaskStatisticsCommand)
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 *StatisticsController) PersonTaskStatistics() {
statisticsService := service.NewStatisticsService(nil)
personTaskStatisticsCommand := &command.PersonTaskStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), personTaskStatisticsCommand)
data, err := statisticsService.PersonTaskStatistics(personTaskStatisticsCommand)
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 *StatisticsController) PersonSuMoneyStatistics() {
statisticsService := service.NewStatisticsService(nil)
personSuMoneyStatisticsCommand := &command.PersonSuMoneyStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), personSuMoneyStatisticsCommand)
data, err := statisticsService.PersonSuMoneyStatistics(personSuMoneyStatisticsCommand)
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("/statistics/system-task", &controllers.StatisticsController{}, "Post:SystemTaskStatistics")
beego.Router("/statistics/person-task", &controllers.StatisticsController{}, "Post:PersonTaskStatistics")
beego.Router("/statistics/person-su-money", &controllers.StatisticsController{}, "Post:PersonSuMoneyStatistics")
}
... ...
package statistics
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 suMoneyTransactionRecordId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&suMoneyTransactionRecordId),
"INSERT INTO su_money_transaction_records (id, record_type, employee, su_money, operator, record_description, create_time) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id",
1, 2, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 1000.00, &domain.EmployeeInfo{
Uid: 2499036607974745099,
}, "testRecordDescription", time.Now().AddDate(0, 0, -1))
Expect(err).NotTo(HaveOccurred())
_, err2 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
2, 101, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 1000)
Expect(err2).NotTo(HaveOccurred())
})
Describe("获取个人素币统计", func() {
Context("请求获取个人素币统计", func() {
It("返回个人素币统计", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"uid": 2499036607974745088,
}
httpExpect.POST("/statistics/person-su-money").
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 su_money_transaction_records WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.Exec("DELETE FROM employees WHERE true")
Expect(err1).NotTo(HaveOccurred())
})
})
... ...
package statistics
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() {
dayAfter, _ := time.ParseDuration("72h")
_, err := 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,
}, 2, "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(err).NotTo(HaveOccurred())
_, 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, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
2, 101, "抢单任务2", 1, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 2, "null", pg.Array([]string{"口感", "便利", "品牌"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), false, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now().Add(dayAfter))
Expect(err1).NotTo(HaveOccurred())
_, err2 := 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, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
3, 102, "竞标任务1", 2, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 1, "null", pg.Array([]string{"口感", "便利", "品牌"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now().Add(dayAfter))
Expect(err2).NotTo(HaveOccurred())
_, err3 := 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, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
4, 101, "竞标任务1", 2, &domain.EmployeeInfo{
Uid: 2499036607974745099,
}, 2, "null", pg.Array([]string{"口感", "便利", "品牌"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), false, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now().Add(dayAfter))
Expect(err3).NotTo(HaveOccurred())
_, err4 := 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
5, 303, "抢单任务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(err4).NotTo(HaveOccurred())
_, err5 := 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(err5).NotTo(HaveOccurred())
_, err11 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO bid_infos (id, task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?, ?)",
1, 4, time.Date(2020, time.Month(4), 5, 8, 0, 0, 0, time.Now().Location()), time.Date(2020, time.Month(4), 10, 8, 0, 0, 0, time.Now().Location()))
Expect(err11).NotTo(HaveOccurred())
_, err22 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO bidder_infos (id, bid_info_id, task_id, bidder) VALUES (?, ?, ?, ?)",
1, 1, 4, &domain.EmployeeInfo{
Uid: 2499036607974745099,
})
Expect(err22).NotTo(HaveOccurred())
})
Describe("获取个人任务统计", func() {
Context("请求个人任务统计", func() {
It("返回个人任务统计", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"uid": 2499036607974745099,
}
httpExpect.POST("/statistics/person-task").
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 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())
})
})
... ...
package statistics
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 TestStatistics(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port Statistics 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 statistics
import (
"github.com/go-pg/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"net/http"
"time"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
)
var _ = Describe("获取系统任务统计", func() {
BeforeEach(func() {
dayAfter, _ := time.ParseDuration("72h")
_, err := 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,
}, 2, "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(err).NotTo(HaveOccurred())
_, 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, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
2, 101, "抢单任务2", 1, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 2, "null", pg.Array([]string{"口感", "便利", "品牌"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), false, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now().Add(dayAfter))
Expect(err1).NotTo(HaveOccurred())
_, err2 := 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, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
3, 102, "竞标任务1", 2, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, 1, "null", pg.Array([]string{"口感", "便利", "品牌"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now().Add(dayAfter))
Expect(err2).NotTo(HaveOccurred())
_, err3 := 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, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
4, 101, "竞标任务1", 2, &domain.EmployeeInfo{
Uid: 2499036607974745099,
}, 5, "null", pg.Array([]string{"口感", "便利", "品牌"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), false, "null", "null", "", pg.Array([]string{}), time.Now(), time.Now().Add(dayAfter))
Expect(err3).NotTo(HaveOccurred())
_, err4 := 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
5, 303, "抢单任务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(err4).NotTo(HaveOccurred())
_, err11 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO bid_infos (id, task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?, ?)",
1, 4, time.Date(2020, time.Month(4), 5, 8, 0, 0, 0, time.Now().Location()), time.Date(2020, time.Month(4), 10, 8, 0, 0, 0, time.Now().Location()))
Expect(err11).NotTo(HaveOccurred())
_, err22 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO bidder_infos (id, bid_info_id, task_id) VALUES (?, ?, ?)",
1, 1, 4)
Expect(err22).NotTo(HaveOccurred())
})
Describe("获取系统任务统计", func() {
Context("请求系统任务统计", func() {
It("返回统计结果", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"companyId": 101,
}
httpExpect.POST("/statistics/system-task").
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 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())
})
})
... ...