/
bill.go
118 lines (95 loc) · 2.91 KB
/
bill.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
/*
* Revision History:
* Initial: 2018/05/25 Chen Yanchen
*/
package handler
import (
"github.com/labstack/echo"
"net/http"
"github.com/JonSnow007/Gymnasium-management-system/GMS/common"
"github.com/JonSnow007/Gymnasium-management-system/GMS/model"
"github.com/JonSnow007/Gymnasium-management-system/GMS/util"
"gopkg.in/mgo.v2"
)
type billHandler struct{}
var Bill *billHandler
func (*billHandler) Info(c echo.Context) error {
var req struct {
Id string `validate:"alphanum,len=24"`
}
//req.Id = c.FormValue("id")
if err := c.Bind(&req); err != nil {
c.Logger().Error("[Bind]", err)
return c.JSON(http.StatusOK, Resp(common.ErrParam))
}
if err := c.Validate(&req); err != nil {
c.Logger().Error("[Validate]", err)
return c.JSON(http.StatusOK, Resp(common.ErrValidate))
}
a, err := model.BillService.Info(req.Id)
if err != nil {
if err == mgo.ErrNotFound {
return c.JSON(http.StatusOK, Resp(common.ErrNotFound))
}
c.Logger().Error("[Info]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, a))
}
func (*billHandler) ListByPhone(c echo.Context) error {
var req struct {
Phone string `json:"phone" validate:"required,len=11"`
}
//req.Phone = c.FormValue("phone")
if err := c.Bind(&req); err != nil {
c.Logger().Error("[Bind]", err)
return c.JSON(http.StatusOK, Resp(common.ErrParam))
}
if !util.PhoneNum(req.Phone) {
c.Logger().Error("[Validate]", common.ErrParam)
return c.JSON(http.StatusOK, Resp(common.ErrValidate))
}
bills, err := model.BillService.ListByPhone(req.Phone)
if err != nil {
c.Logger().Error("[ListByPhone]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, bills))
}
func (*billHandler) ListByGid(c echo.Context) error {
var req struct {
Id int `json:"id" validate:"require,numeric"`
}
if err := c.Bind(&req); err != nil {
c.Logger().Error("[Bind]", err)
return c.JSON(http.StatusOK, Resp(common.ErrValidate))
}
a, err := model.BillService.ListByGid(req.Id)
if err != nil {
c.Logger().Error("[ListByPid]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, a))
}
func (*billHandler) List(c echo.Context) error {
a, err := model.BillService.List()
if err != nil {
c.Logger().Error("[List]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, a))
}
func (*billHandler) Total(c echo.Context) error {
total, err := model.BillService.Total()
if err != nil {
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB, nil))
}
recorded, err := model.AccountService.Recorded()
if err != nil {
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB, nil))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, map[string]int{
"total": total,
"recorded": recorded,
}))
}