common_statistics_controller.go 3.1 KB
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
}