common_statistics_controller.go
4.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
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
package controllers
import (
c "context"
"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/crontab"
"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"
"time"
)
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 (controller *StatisticsController) TaskHandler() func(ctx *context.Context) {
return func(ctx *context.Context) {
task := ctx.Input.Query(":taskId")
switch task {
case "1":
crontab.AutoApproveProductAttendanceRecord(nil)
break
case "2":
crontab.AutoApproveProductRecord(nil)
break
case "3":
crontab.AutoFlushDeviceDailyRunningRecord(nil)
break
case "4":
crontab.AutoFlushDeviceDailyRunningRecordOEE(nil)
break
case "5":
crontab.AutoWorkshopPlanCompletionRecord(nil)
break
case "6":
bc := c.Background()
t := ctx.Input.Query("t")
if len(t) != 0 {
if v, err := time.ParseInLocation("2006-01-02 15:04:05", t, time.Local); err == nil {
bc = c.WithValue(bc, "fromTime", v)
}
}
crontab.SyncProductPlan(bc)
break
case "7":
crontab.AutoTodayWorkshopPlanCompletionRecord(nil)
break
}
Response(ctx, nil, nil)
}
}
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
}