/
transaction.go
137 lines (130 loc) · 3.56 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package transaction
import (
"context"
"github.com/XC-Zero/yinwan/internal/controller/services_controller/common"
"github.com/XC-Zero/yinwan/pkg/model/mongo_model"
"github.com/XC-Zero/yinwan/pkg/utils/logger"
my_mongo "github.com/XC-Zero/yinwan/pkg/utils/mongo"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson/bsontype"
"strconv"
"time"
)
func CreateTransaction(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
var transaction mongo_model.Transaction
err := ctx.ShouldBindBodyWith(&transaction, binding.JSON)
if err != nil {
logger.Error(errors.WithStack(err), "")
common.RequestParamErrorTemplate(ctx, common.REQUEST_PARM_ERROR)
return
}
var auto common.Auto
ctx.ShouldBindBodyWith(&auto, binding.JSON)
recID := int(time.Now().Unix())
transaction.RecID = &recID
transaction.BookName = bk.BookName
transaction.BookNameID = bk.StorageName
transaction.CreatedAt = strconv.Itoa(int(time.Now().Unix()))
common.CreateOneMongoDBRecordTemplate(ctx, common.CreateMongoDBTemplateOptions{
DB: bk.MongoDBClient,
Context: auto.WithContext(ctx),
TableModel: transaction,
})
return
}
func SelectTransaction(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
conditions := []common.MongoCondition{
{
my_mongo.EQUAL,
"rec_id",
ctx.PostForm("transaction_id"),
},
{
my_mongo.LIKE,
"transaction_name",
ctx.PostForm("transaction_name"),
},
{
my_mongo.EQUAL,
"transaction_owner_id",
ctx.PostForm("transaction_owner_id"),
},
{
my_mongo.GREATER_THEN_EQUAL,
"transaction_time",
ctx.PostForm("transaction_time"),
},
{
my_mongo.LESS_THAN_EQUAL,
"transaction_time",
ctx.PostForm("transaction_time2"),
},
{
my_mongo.EQUAL,
"deleted_at",
bsontype.Null,
},
}
common.SelectMongoDBTableContentWithCountTemplate(ctx, common.SelectMongoDBTemplateOptions{
DB: bk.MongoDBClient,
TableModel: mongo_model.Transaction{},
}, conditions...)
return
}
// UpdateTransaction TODO 考虑是否自动级联更新?
func UpdateTransaction(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
var transaction mongo_model.Transaction
err := ctx.ShouldBindBodyWith(&transaction, binding.JSON)
if err != nil || transaction.RecID == nil {
logger.Error(errors.WithStack(err), "")
common.RequestParamErrorTemplate(ctx, common.REQUEST_PARM_ERROR)
return
}
now := strconv.Itoa(int(time.Now().Unix()))
transaction.UpdatedAt = &now
var auto common.Auto
//TODO 考虑是否自动级联更新?
ctx.ShouldBindBodyWith(&auto, binding.JSON)
common.UpdateOneMongoDBRecordByIDTemplate(ctx, common.MongoDBTemplateOptions{
DB: bk.MongoDBClient,
Context: auto.WithContext(ctx),
TableModel: transaction,
RecID: *transaction.RecID,
})
return
}
func DeleteTransaction(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
var transaction mongo_model.Transaction
recID, err := strconv.Atoi(ctx.PostForm("transaction_id"))
transaction.RecID = &recID
if err != nil || transaction.RecID == nil || recID == 0 {
logger.Error(errors.WithStack(err), "")
common.RequestParamErrorTemplate(ctx, common.REQUEST_PARM_ERROR)
return
}
common.DeleteOneMongoDBRecordByIDTemplate(ctx, common.MongoDBTemplateOptions{
DB: bk.MongoDBClient,
Context: context.WithValue(ctx, "auto", ctx.PostForm("auto")),
TableModel: transaction,
RecID: *transaction.RecID,
})
return
}