作者 陈志颖

修改pg/model和现金池领域

@@ -7,14 +7,10 @@ import ( @@ -7,14 +7,10 @@ import (
7 ) 7 )
8 8
9 type ExchangeSuMoneyCommand struct { 9 type ExchangeSuMoneyCommand struct {
10 - // 兑换人UID  
11 - Uid int64 `json:"uid" valid:"Required"`  
12 - // 素币值  
13 - SuMoney float64 `json:"suMoney" valid:"Required"`  
14 - // 操作人UID  
15 - Operator int64 `json:"operator,omitempty"`  
16 - // 兑换描述  
17 - ExchangeDescription string `json:"exchangeDescription" valid:"Required"` 10 + Uid int64 `json:"uid" valid:"Required"` // 兑换人UID
  11 + SuMoney float64 `json:"suMoney" valid:"Required"` // 素币值
  12 + Operator int64 `json:"operator,omitempty"` // 操作人UID
  13 + ExchangeDescription string `json:"exchangeDescription" valid:"Required"` // 兑换描述
18 } 14 }
19 15
20 func (exchangeSuMoneyCommand *ExchangeSuMoneyCommand) ValidateCommand() error { 16 func (exchangeSuMoneyCommand *ExchangeSuMoneyCommand) ValidateCommand() error {
@@ -336,10 +336,10 @@ func (taskService *TaskService) AcceptanceTask(acceptanceTaskCommand *command.Ac @@ -336,10 +336,10 @@ func (taskService *TaskService) AcceptanceTask(acceptanceTaskCommand *command.Ac
336 336
337 // 搜索任务 337 // 搜索任务
338 func (taskService *TaskService) SearchTask(searchTaskCommand *command.SearchTaskCommand) (interface{}, error) { 338 func (taskService *TaskService) SearchTask(searchTaskCommand *command.SearchTaskCommand) (interface{}, error) {
339 - if err := searchTaskCommand.ValidateCommand(); err != nil { 339 + if err := searchTaskCommand.ValidateCommand(); err != nil { // 校验搜索命令
340 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 340 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
341 } 341 }
342 - transactionContext, err := factory.CreateTransactionContext(nil) 342 + transactionContext, err := factory.CreateTransactionContext(nil) // 工厂类创建事务上下文
343 if err != nil { 343 if err != nil {
344 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 344 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
345 } 345 }
@@ -4,9 +4,12 @@ import "os" @@ -4,9 +4,12 @@ import "os"
4 4
5 var POSTGRESQL_DB_NAME = "mmm-worth" 5 var POSTGRESQL_DB_NAME = "mmm-worth"
6 var POSTGRESQL_USER = "postgres" 6 var POSTGRESQL_USER = "postgres"
7 -var POSTGRESQL_PASSWORD = "abc123456" 7 +//var POSTGRESQL_PASSWORD = "abc123456"
  8 +//var POSTGRESQL_HOST = "127.0.0.1"
  9 +//var POSTGRESQL_PORT = "32432"
  10 +var POSTGRESQL_PASSWORD = "1993618jack"
8 var POSTGRESQL_HOST = "127.0.0.1" 11 var POSTGRESQL_HOST = "127.0.0.1"
9 -var POSTGRESQL_PORT = "32432" 12 +var POSTGRESQL_PORT = "5432"
10 var DISABLE_CREATE_TABLE = false 13 var DISABLE_CREATE_TABLE = false
11 var DISABLE_SQL_GENERATE_PRINT = false 14 var DISABLE_SQL_GENERATE_PRINT = false
12 15
1 package domain 1 package domain
2 2
3 // 现金池信息 3 // 现金池信息
4 -type CashPoolInfo struct { 4 +type CashPool struct {
5 Cash float64 `json:"cash"` // 投入现金池的现金 5 Cash float64 `json:"cash"` // 投入现金池的现金
6 ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金 6 ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金
7 UnExchangeCash float64 `json:"unExchangeCash"` // 未兑换的现金 7 UnExchangeCash float64 `json:"unExchangeCash"` // 未兑换的现金
1 package domain 1 package domain
  2 +
  3 +import "time"
  4 +
  5 +// 现金池事务记录
  6 +type CashPoolTransactionRecord struct {
  7 + CashPoolTransactionRecordId int64 `json:"cashPoolTransactionRecordId"` // 现金池事务记录ID
  8 + RecordType int `json:"recordType"` // 记录类型
  9 + CashPoolBeforeTransaction *CashPool `json:"cashPoolBeforeTransaction"` // 事务处理前现金池
  10 + CurrentCashPool *CashPool `json:"CurrentCashPool"` // 当前现金池
  11 + Cash float64 `json:"CashPool"` // 事务现金值
  12 + Operator *EmployeeInfo `json:"operator"` // 操作人
  13 + RecordDescription string `json:"recordDescription"` // 现金池事务记录描述
  14 + CreateTime time.Time `json:"createTime"` // 创建时间
  15 +}
  16 +
  17 +type CashPoolTransactionRecordRepository interface {
  18 + Save(cashPoolTransactionRecord *CashPoolTransactionRecord) (*CashPoolTransactionRecord, error)
  19 +}
  20 +
  21 +func (cashPoolTransactionRecord *CashPoolTransactionRecord) Identity() interface{} {
  22 + if cashPoolTransactionRecord.CashPoolTransactionRecordId == 0 {
  23 + return nil
  24 + }
  25 + return cashPoolTransactionRecord.CashPoolTransactionRecordId
  26 +}
  27 +
  28 +// 计算当前现金池中的未兑换现金
  29 +func (cashPoolTransactionRecord *CashPoolTransactionRecord) TransferUnExchangeCash(cash float64) error {
  30 + cashPoolTransactionRecord.CurrentCashPool.UnExchangeCash += cash
  31 + return nil
  32 +}
1 package domain 1 package domain
2 2
  3 +import "time"
  4 +
3 // 兑换活动 5 // 兑换活动
4 type ExchangeActivity struct { 6 type ExchangeActivity struct {
5 - ActivityId int64 `json:"activityId"` // 活动编号  
6 - ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金  
7 - ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币  
8 - Rate float64 `json:"rate"` // 兑换汇率 7 + ExchangeActivityId int64 `json:"exchangeActivityId"` // 兑换素币活动编号
  8 + ExchangeActivityName string `json:"exchangeActivityName"` // 兑换素币活动名称
  9 + CompanyId int64 `json:"companyId"` // 公司ID
  10 + ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金
  11 + ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币
  12 + CreateTime time.Time `json:"createTime"` // 创建兑换素币活动时间
  13 + Deadline time.Time `json:"deadline"` // 兑换素币活动截止时间
  14 + CountDown string `json:"countDown"` // 兑换素币活动结束倒计时
  15 + Rate float64 `json:"rate"` // 兑换汇率
  16 + LastRate float64 `json:"lastRate"` // 上期兑换汇率
  17 + ExchangeSuMoneyList []*ExchangePerson `json:"exchangeSuMoneyList"` // 素币兑换清单
9 } 18 }
10 19
11 type ExchangeActivityRepository interface { 20 type ExchangeActivityRepository interface {
@@ -16,13 +25,23 @@ type ExchangeActivityRepository interface { @@ -16,13 +25,23 @@ type ExchangeActivityRepository interface {
16 } 25 }
17 26
18 func (activity *ExchangeActivity) Identity() interface{} { 27 func (activity *ExchangeActivity) Identity() interface{} {
19 - if activity.ActivityId == 0 { 28 + if activity.ExchangeActivityId == 0 {
20 return nil 29 return nil
21 } 30 }
22 - return activity.ActivityId 31 + return activity.ExchangeActivityId
23 } 32 }
24 33
25 func (activity *ExchangeActivity) TransferSuMoney(rate float64) error { 34 func (activity *ExchangeActivity) TransferSuMoney(rate float64) error {
26 activity.ExchangedSuMoney = activity.ExchangedCash * rate 35 activity.ExchangedSuMoney = activity.ExchangedCash * rate
27 return nil 36 return nil
28 } 37 }
  38 +
  39 +func (activity *ExchangeActivity) TransferLastRate(lastRate float64) error {
  40 + activity.LastRate = lastRate
  41 + return nil
  42 +}
  43 +
  44 +func (activity *ExchangeActivity) TransferCountDown() error {
  45 + activity.CountDown = string(time.Until(activity.Deadline))
  46 + return nil
  47 +}
@@ -36,6 +36,9 @@ func init() { @@ -36,6 +36,9 @@ func init() {
36 (*models.RejectTaskRecord)(nil), 36 (*models.RejectTaskRecord)(nil),
37 (*models.Notification)(nil), 37 (*models.Notification)(nil),
38 (*models.SentNotification)(nil), 38 (*models.SentNotification)(nil),
  39 + (*models.ExchangeActivities)(nil),
  40 + (*models.CashPoolTransactionRecord)(nil),
  41 + (*models.ExchangePerson)(nil),
39 } { 42 } {
40 err := DB.CreateTable(model, &orm.CreateTableOptions{ 43 err := DB.CreateTable(model, &orm.CreateTableOptions{
41 Temp: false, 44 Temp: false,
@@ -6,6 +6,13 @@ import ( @@ -6,6 +6,13 @@ import (
6 ) 6 )
7 7
8 type CashPoolTransactionRecord struct { 8 type CashPoolTransactionRecord struct {
9 - TableName string `pg:"cash_pool"`  
10 - CreateTime time.Time // 创建时间 9 + TableName string `pg:"cash_pool_transaction_records,alias:cash_pool_transaction_record"`
  10 + Id int64 `pg:",pk"` // 现金池事务记录ID
  11 + RecordType int // 记录类型
  12 + CashPoolBeforeTransaction *domain.CashPoolInfo // 事务处理前现金池
  13 + CurrentCashPool *domain.CashPoolInfo // 当前现金池
  14 + Cash float64 // 事务现金值
  15 + Operator *domain.EmployeeInfo // 操作人
  16 + RecordDescription string // 现金投入事务记录描述
  17 + CreateTime time.Time // 创建时间
11 } 18 }
1 package models 1 package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type ExchangeActivities struct {
  9 + TableName string `pg:"exchange_activities,alias:exchange_activity"`
  10 + Id int64 `pg:",pk"` // 兑换活动ID
  11 + CompanyId int64 // 公司ID
  12 + ActivityName string // 兑换活动名称
  13 + ActivityStart time.Time // 兑换活动开始时间
  14 + ActivityEnd time.Time // 兑换活动结束时间
  15 + ActivityPeriod string // 兑换活动持续时间
  16 + ExchangedCash float64 // 已兑换现金
  17 + ExchangedSuMoney float64 // 已兑换素币
  18 + ExchangeRate float64 // 兑换汇率
  19 + ExchangeSuMoneyList []*domain.ExchangePerson // 素币兑换人员清单
  20 + Sponsor *domain.EmployeeInfo // 活动发布人
  21 + CreateTime time.Time // 创建时间
  22 +}
1 package repository 1 package repository
  2 +
  3 +import (
  4 + "github.com/go-pg/pg"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  7 +)
  8 +
  9 +type CashPoolTransactionRecordRepository struct {
  10 + transactionContext *pgTransaction.TransactionContext
  11 +}
  12 +
  13 +func (repository *CashPoolTransactionRecordRepository) Save(cashPoolTransactionRecord *domain.CashPoolTransactionRecord) (*domain.CashPoolTransactionRecord, error) {
  14 + tx := repository.transactionContext.PgTx
  15 + if cashPoolTransactionRecord.Identity() == nil {
  16 + if _, err := tx.QueryOne(
  17 + pg.Scan(&cashPoolTransactionRecord.CashPoolTransactionRecordId, &cashPoolTransactionRecord.RecordType, &cashPoolTransactionRecord.Cash, &cashPoolTransactionRecord.CashPoolBeforeTransaction, &cashPoolTransactionRecord.CurrentCashPool, &cashPoolTransactionRecord.Operator, &cashPoolTransactionRecord.CreateTime, &cashPoolTransactionRecord.RecordDescription),
  18 + "INSERT INTO cash_pool_transaction_records ()"); err != nil {
  19 + return cashPoolTransactionRecord, err
  20 + }
  21 + } else {
  22 + if _, err := tx.QueryOne(
  23 + pg.Scan(&cashPoolTransactionRecord.CashPoolTransactionRecordId, &cashPoolTransactionRecord.RecordType, &cashPoolTransactionRecord.Cash, &cashPoolTransactionRecord.CashPoolBeforeTransaction, &cashPoolTransactionRecord.CurrentCashPool, &cashPoolTransactionRecord.Operator, &cashPoolTransactionRecord.CreateTime, &cashPoolTransactionRecord.RecordDescription),
  24 + "UPDATE cash_pool_transaction_records SET "); err != nil {
  25 + return cashPoolTransactionRecord, err
  26 + }
  27 + }
  28 + return cashPoolTransactionRecord, nil
  29 +}
1 package controllers 1 package controllers
  2 +
  3 +import (
  4 +
  5 + "github.com/astaxie/beego"
  6 +)
  7 +
  8 +type CashPoolController struct {
  9 + beego.Controller
  10 +}
  11 +
  12 +/**
  13 + * 现金池投入
  14 + */
  15 +func (controller *CashPoolController) CashInput() {
  16 +
  17 +}
  18 +
  19 +/**
  20 + * 返回兑换活动列表
  21 + */
  22 +func (controller *CashPoolController) ListExchangeActivities () {
  23 +
  24 +}
  25 +
  26 +/**
  27 + * 更新兑换活动信息
  28 + */
  29 +func (controller *CashPoolController) UpdateExchangeActivities () {
  30 +
  31 +}
  32 +
  33 +/**
  34 + * 新增兑换活动
  35 + */
  36 +func (controller *CashPoolController) CreateExchangeActivities () {
  37 +
  38 +}
  39 +
  40 +/**
  41 + * 删除兑换活动
  42 + */
  43 +func (controller *CashPoolController) DeleteExchangeActivities () {
  44 +
  45 +}
  46 +
  47 +/**
  48 + * 搜索兑换素币活动
  49 + */
  50 +func (controller *CashPoolController) searchExchangeActivities () {
  51 +
  52 +}
  53 +
  54 +/**
  55 + * 兑换活动数据统计
  56 + */
  57 +func (controller *CashPoolController) exchangeActivitiesStatistics () {
  58 +
  59 +}
  60 +
  61 +/**
  62 + * 获取素币兑换清单
  63 + */
  64 +func (controller *CashPoolController) ListExchangeList () {
  65 +
  66 +}
  67 +
  68 +/**
  69 + * 新增素币兑换清单
  70 + */
  71 +func (controller *CashPoolController) CreateExchangeList () {
  72 +
  73 +}
  74 +
  75 +/**
  76 + * 更新素币兑换清单
  77 + */
  78 +func (controller *CashPoolController) UpdateExchangeList () {
  79 +
  80 +}
  81 +
  82 +/**
  83 + * 删除素币兑换清单
  84 + */
  85 +func (controller *CashPoolController) DeleteExchangeList () {
  86 +
  87 +}
  88 +
  89 +/**
  90 + * 搜索素币兑换清单
  91 + */
  92 +func (controller *CashPoolController) searchExchangeList () {
  93 +
  94 +}
  95 +
  96 +/**
  97 + * 导出素币兑换清单
  98 + */
  99 +func (controller *CashPoolController) ExportExchangeList () {
  100 +
  101 +}
  102 +
  103 +/**
  104 + * 导入素币兑换清单
  105 + */
  106 +func (controller *CashPoolController) ImportExchangeList () {
  107 +
  108 +}
  109 +
  110 +
  111 +
  112 +
1 package routers 1 package routers
2 2
  3 +// TODO
3 //func init() { 4 //func init() {
4 // /*****************************************现金池**********************************/ 5 // /*****************************************现金池**********************************/
5 -// beego.Router("/", &controllers.ConfigController{}, "POST:CashInput") // 现金池投入 6 +// beego.Router("/cash_pool/input", &controllers.ConfigController{}, "POST:CashInput") // 现金池投入
6 // 7 //
7 // /*****************************************兑换活动*********************************/ 8 // /*****************************************兑换活动*********************************/
8 -// beego.Router("/", &controllers.ConfigController{}, "GET:ListExchangeActivities") // 返回兑换活动列表  
9 -// beego.Router("/", &controllers.ConfigController{}, "PUT:UpdateExchangeActivities") // 更新兑换活动信息  
10 -// beego.Router("/", &controllers.ConfigController{}, "POST:CreateExchangeActivities") // 新增兑换活动  
11 -// beego.Router("/", &controllers.ConfigController{}, "DELETE:DeleteExchangeActivities") // 删除兑换活动  
12 -// beego.Router("/", &controllers.ConfigController{}, "POST:SearchExchangeActivities") // 搜索兑换素币活动  
13 -// beego.Router("/", &controllers.ConfigController{}, "GET:ExchangeActivitiesStatistics") // 兑换活动数据统计 9 +// beego.Router("/cash_pool/", &controllers.ConfigController{}, "GET:ListExchangeActivities") // 返回兑换活动列表
  10 +// beego.Router("/cash_pool/:aid", &controllers.ConfigController{}, "PUT:UpdateExchangeActivities") // 编辑兑换活动信息
  11 +// beego.Router("/cash_pool/", &controllers.ConfigController{}, "POST:CreateExchangeActivities") // 新增兑换活动
  12 +// beego.Router("/cash_pool/:aid", &controllers.ConfigController{}, "DELETE:DeleteExchangeActivities") // 删除兑换活动
14 // 13 //
15 // /*****************************************兑换素币清单****************************/ 14 // /*****************************************兑换素币清单****************************/
16 -// beego.Router("/", &controllers.ConfigController{}, "GET:ListExchangeList") // 返回素币兑换清单列表  
17 -// beego.Router("/", &controllers.ConfigController{}, "GET:GetExchangeList") // 返回素币兑换清单  
18 -// beego.Router("/", &controllers.ConfigController{}, "POST:CreateExchangeList") // 新增素币兑换清单  
19 -// beego.Router("/", &controllers.ConfigController{}, "PUT:UpdateExchangeList") // 更新素币兑换清单  
20 -// beego.Router("/", &controllers.ConfigController{}, "DELETE:RemoveExchangeList") // 删除素币兑换清单  
21 -// beego.Router("/", &controllers.ConfigController{}, "POST:SearchExchangeList") // 搜索素币兑换记录  
22 -// beego.Router("/", &controllers.ConfigController{}, "POST:ExportExchangeList") // 导出素币兑换清单  
23 -// beego.Router("/", &controllers.ConfigController{}, "POST:ImportExchangeList") // 导入素币兑换清单 15 +// beego.Router("/cash_pool/exchange", &controllers.ConfigController{}, "GET:ListExchangeList") // 返回素币兑换清单
  16 +// beego.Router("/cash_pool/exchange", &controllers.ConfigController{}, "POST:CreateExchangeList") // 新增素币兑换清单列表
  17 +// beego.Router("/cash_pool/exchange/:eid", &controllers.ConfigController{}, "PUT:UpdateExchangeList") // 编辑素币兑换清单
  18 +// beego.Router("/cash_pool/exchange/:eid", &controllers.ConfigController{}, "DELETE:RemoveExchangeList") // 删除素币兑换清单
  19 +// beego.Router("/cash_pool/export", &controllers.ConfigController{}, "POST:ExportExchangeList") // 导出素币兑换清单
  20 +// beego.Router("/cash_pool/import", &controllers.ConfigController{}, "POST:ImportExchangeList") // 导入素币兑换清单
24 //} 21 //}
1 package cash_pool 1 package cash_pool
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("投入现金", func() {
  13 + Describe("投入现金并更新现金池数据", func() {
  14 + Context("提交正确的现金值(>=已兑换的现金)", func() {
  15 + It("返回现金池数据", func() {
  16 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  17 + body := map[string]interface{}{
  18 + "cashAmount": 100,
  19 + }
  20 + httpExpect.POST("/cash_pool/input").
  21 + WithJSON(body).
  22 + Expect().
  23 + Status(http.StatusOK).
  24 + JSON().
  25 + Object().
  26 + ContainsKey("code").ValueEqual("code", 0).
  27 + ContainsKey("msg").ValueEqual("msg", "ok").
  28 + ContainsKey("data").Value("data").Object()
  29 + })
  30 + })
  31 + })
  32 + AfterEach(func() {
  33 + _, err := pG.DB.Exec("DELETE FROM ")
  34 + Expect(err).NotTo(HaveOccurred())
  35 + })
  36 +})
1 -package cashPool 1 +package cash_pool
2 2
3 import ( 3 import (
4 "github.com/onsi/ginkgo" 4 "github.com/onsi/ginkgo"
@@ -13,14 +13,14 @@ import ( @@ -13,14 +13,14 @@ import (
13 ) 13 )
14 14
15 func TestConfig(t *testing.T) { 15 func TestConfig(t *testing.T) {
16 - gomega.RegisterFailHandler(Fail)  
17 - ginkgo.RunSpecs() 16 + gomega.RegisterFailHandler(ginkgo.Fail)
  17 + ginkgo.RunSpecs(t, "Beego Port Cash Pool Correlations Test Case Suite")
18 } 18 }
19 19
20 var handler http.Handler 20 var handler http.Handler
21 var server *httptest.Server 21 var server *httptest.Server
22 22
23 -var _ = BeforeSuite(func() { 23 +var _ = ginkgo.BeforeSuite(func() {
24 handler = beego.BeeApp.Handlers 24 handler = beego.BeeApp.Handlers
25 server = httptest.NewServer(handler) 25 server = httptest.NewServer(handler)
26 }) 26 })