/
customer.go
116 lines (111 loc) · 3.1 KB
/
customer.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
package transaction
import (
"github.com/XC-Zero/yinwan/internal/controller/services_controller/common"
_const "github.com/XC-Zero/yinwan/pkg/const"
"github.com/XC-Zero/yinwan/pkg/model/mysql_model"
"github.com/XC-Zero/yinwan/pkg/utils/errs"
"github.com/XC-Zero/yinwan/pkg/utils/logger"
"github.com/XC-Zero/yinwan/pkg/utils/mysql"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/pkg/errors"
"strconv"
)
func CreateCustomer(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
var customer mysql_model.Customer
err := ctx.ShouldBindBodyWith(&customer, binding.JSON)
if err != nil {
logger.Error(errors.WithStack(err), "")
common.RequestParamErrorTemplate(ctx, common.REQUEST_PARM_ERROR)
return
}
err = bk.MysqlClient.WithContext(ctx).
Create(&customer).Error
if err != nil {
common.InternalDataBaseErrorTemplate(ctx, common.DATABASE_INSERT_ERROR, customer)
return
}
ctx.JSON(_const.OK, errs.CreateSuccessMsg("创建客户成功!"))
return
}
func SelectCustomer(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
conditions := []common.MysqlCondition{
{
Symbol: mysql.LIKE,
ColumnName: "customer_name",
ColumnValue: ctx.PostForm("customer_name"),
},
{
Symbol: mysql.EQUAL,
ColumnName: "rec_id",
ColumnValue: ctx.PostForm("customer_id"),
},
{
Symbol: mysql.EQUAL,
ColumnName: "customer_social_credit_code",
ColumnValue: ctx.PostForm("customer_social_credit_code"),
},
{
Symbol: mysql.NULL,
ColumnName: "deleted_at",
ColumnValue: " ",
},
}
op := common.SelectMysqlTemplateOptions{
DB: bk.MysqlClient.WithContext(ctx),
TableModel: mysql_model.Customer{},
}
common.SelectMysqlTableContentWithCountTemplate(ctx, op, conditions...)
return
}
func UpdateCustomer(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
var customer mysql_model.Customer
err := ctx.ShouldBindBodyWith(&customer, binding.JSON)
if err != nil || customer.RecID == nil {
common.RequestParamErrorTemplate(ctx, common.REQUEST_PARM_ERROR)
return
}
err = bk.MysqlClient.WithContext(ctx).
Updates(&customer).Where("rec_id", *customer.RecID).Error
if err != nil {
common.InternalDataBaseErrorTemplate(ctx, common.DATABASE_UPDATE_ERROR, customer)
return
}
ctx.JSON(_const.OK, errs.CreateSuccessMsg("修改客户成功!"))
return
}
func DeleteCustomer(ctx *gin.Context) {
bk := common.HarvestClientFromGinContext(ctx)
if bk == nil {
return
}
recID, err := strconv.Atoi(ctx.PostForm("customer_id"))
if err != nil {
common.RequestParamErrorTemplate(ctx, common.REQUEST_PARM_ERROR)
return
}
var customer = mysql_model.Customer{BasicModel: mysql_model.BasicModel{
RecID: &recID,
}}
err = bk.MysqlClient.WithContext(
ctx).Delete(&customer).Error
if err != nil {
logger.Error(errors.WithStack(err), "删除客户失败!")
common.InternalDataBaseErrorTemplate(ctx, common.DATABASE_DELETE_ERROR, customer)
return
}
ctx.JSON(_const.OK, errs.CreateSuccessMsg("删除客户成功!"))
return
}