common_statistics_controller.go
3.1 KB
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
package controllers
import (
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/utils/json"
"github.com/linmadan/egglib-go/web/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/statistics/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/statistics/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"net/http"
"strconv"
"strings"
)
type StatisticsController struct {
beego.BaseController
}
func (controller *StatisticsController) CommonStatisticsService() {
attendanceService := service.NewCommonStatisticsService(nil)
cmd := &query.CommonStatisticsQuery{}
controller.Unmarshal(cmd)
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.QueryOptions["companyId"] = operateInfo.CompanyId
cmd.QueryOptions["orgId"] = operateInfo.OrgId
data, err := attendanceService.CommonStatisticsService(cmd)
controller.Response(data, err)
}
func (controller *StatisticsController) CommonStatisticsHandler(actionType string) func(ctx *context.Context) {
return func(ctx *context.Context) {
attendanceService := service.NewCommonStatisticsService(nil)
cmd := &query.CommonStatisticsQuery{}
options := make(map[string]interface{})
Unmarshal(ctx, &options)
operateInfo := ContextParseOperateInfo(ctx)
options["companyId"] = operateInfo.CompanyId
options["orgId"] = operateInfo.OrgId
cmd.Action = actionType
cmd.QueryOptions = options
data, err := attendanceService.CommonStatisticsService(cmd)
Response(ctx, data, err)
}
}
func Response(ctx *context.Context, data interface{}, err error) {
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(ctx, err)
} else {
response = utils.ResponseData(ctx, data)
}
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(response, false, false)
}
func Unmarshal(ctx *context.Context, v interface{}) error {
body := ctx.Input.RequestBody
if len(body) == 0 {
body = []byte("{}")
}
return json.Unmarshal(body, v)
}
// ParseOperateInfo 从头部解析操作对象信息
func ContextParseOperateInfo(c *context.Context) *domain.OperateInfo {
opt := &domain.OperateInfo{}
opt.UserId = ContextHeader(c, constant.HeaderUserId)
opt.CompanyId = ContextHeader(c, constant.HeaderCompanyId)
opt.OrgId = ContextHeader(c, constant.HeaderOrgId)
orgIdList := c.Input.Header(constant.HeaderOrgIds)
splitOrgIdList := strings.Split(orgIdList, constant.CUSTOMER_ACCOUNT_DELIMITER)
for i := range splitOrgIdList {
orgId, _ := strconv.Atoi(splitOrgIdList[i])
if orgId == 0 {
continue
}
opt.OrgIds = append(opt.OrgIds, orgId)
}
return opt
}
func ContextHeader(c *context.Context, key string) int {
if len(c.Input.Header(key)) == 0 {
return 0
}
res, err := strconv.Atoi(c.Input.Header(key))
if err != nil {
log.Logger.Error(err.Error())
return 0
}
return res
}