common_statistics_controller.go 5.2 KB
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/infrastructure/domainService"
	"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) InternalRedirectHandler() func(ctx *context.Context) {
	return func(ctx *context.Context) {
		attendanceService := service.NewCommonStatisticsService(nil)
		cmd := &query.CommonStatisticsQuery{}
		options := make(map[string]interface{})
		Unmarshal(ctx, &options)

		actionType := ctx.Input.Query(":actionType")
		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
		case "8":
			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.SyncProduct(bc)
			break
		case "9":
			svr := domainService.NewByteBankService()
			response, err := svr.DeviceOperationEfficiency()
			if err != nil {
				Response(ctx, nil, err)
				return
			}
			Response(ctx, response, 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
}