-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepaid_card.go
116 lines (106 loc) · 3.34 KB
/
prepaid_card.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 prepaidcard
import (
"time"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/formfilter"
"github.com/admpub/nging/v5/application/handler"
modelCustomer "github.com/admpub/webx/application/model/official/customer"
)
func formFilter() echo.FormDataFilter {
return formfilter.Build(
formfilter.StartDateToTimestamp(`Start`),
formfilter.EndDateToTimestamp(`End`),
)
}
func Index(ctx echo.Context) error {
m := modelCustomer.NewPrepaidCard(ctx)
cond := db.Compounds{}
q := ctx.Formx(`q`).String()
if len(q) == 0 {
q = ctx.Form(`number`)
}
if len(q) > 0 {
cond.AddKV(`number`, q)
}
status := ctx.Form(`status`)
if len(status) > 0 {
switch status {
case `used`:
cond.AddKV(`used`, db.Gt(0))
case `unused`:
cond.AddKV(`used`, 0)
}
}
_, err := handler.PagingWithLister(ctx, handler.NewLister(m, nil, func(r db.Result) db.Result {
return r.OrderBy(`-id`)
}, cond.And()))
ctx.Set(`listData`, m.Objects())
return ctx.Render(`official/customer/prepaid_card/index`, handler.Err(ctx, err))
}
func Add(ctx echo.Context) error {
user := handler.User(ctx)
var err error
if ctx.IsPost() {
m := modelCustomer.NewPrepaidCard(ctx)
err = ctx.MustBind(m.OfficialCustomerPrepaidCard, formFilter())
if err == nil {
count := ctx.Formx(`count`).Int()
err = m.BatchGenerate(user.Id, count, m.Amount, m.SalePrice, m.Start, m.End, m.BgImage)
}
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/official/customer/prepaid_card/index`))
}
}
ctx.Set(`activeURL`, `/official/customer/prepaid_card/index`)
ctx.Set(`title`, ctx.T(`添加充值卡`))
ctx.Set(`isAdd`, true)
return ctx.Render(`official/customer/prepaid_card/edit`, handler.Err(ctx, err))
}
func Edit(ctx echo.Context) error {
id := ctx.Formx(`id`).Uint64()
m := modelCustomer.NewPrepaidCard(ctx)
err := m.Get(nil, `id`, id)
if err != nil {
handler.SendFail(ctx, err.Error())
return ctx.Redirect(handler.URLFor(`/official/customer/prepaid_card/index`))
}
if ctx.IsPost() {
err = ctx.MustBind(m.OfficialCustomerPrepaidCard, formFilter())
if err == nil {
m.Id = id
err = m.Edit(nil, `id`, id)
}
if err == nil {
handler.SendOk(ctx, ctx.T(`修改成功`))
return ctx.Redirect(handler.URLFor(`/official/customer/prepaid_card/index`))
}
} else {
echo.StructToForm(ctx, m.OfficialCustomerPrepaidCard, ``, echo.LowerCaseFirstLetter)
var startDate, endDate string
if m.OfficialCustomerPrepaidCard.Start > 0 {
startDate = time.Unix(int64(m.OfficialCustomerPrepaidCard.Start), 0).Format(`2006-01-02`)
}
ctx.Request().Form().Set(`start`, startDate)
if m.OfficialCustomerPrepaidCard.End > 0 {
endDate = time.Unix(int64(m.OfficialCustomerPrepaidCard.End), 0).Format(`2006-01-02`)
}
ctx.Request().Form().Set(`end`, endDate)
}
ctx.Set(`activeURL`, `/official/customer/prepaid_card/index`)
ctx.Set(`title`, ctx.T(`修改充值卡`))
ctx.Set(`isAdd`, false)
return ctx.Render(`official/customer/prepaid_card/edit`, handler.Err(ctx, err))
}
func Delete(ctx echo.Context) error {
id := ctx.Formx(`id`).Uint64()
m := modelCustomer.NewPrepaidCard(ctx)
err := m.Delete(nil, db.Cond{`id`: id})
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
} else {
handler.SendFail(ctx, err.Error())
}
return ctx.Redirect(handler.URLFor(`/official/customer/prepaid_card/index`))
}