search_log.go 2.2 KB
package command

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"reflect"
	"strings"
	"time"

	"github.com/beego/beego/v2/core/validation"
)

type SearchLogCommand struct {
	// 日志类型 VerifiedStepLog:校验步骤 CommonLog:常规日志
	LogType string `json:"logType"`
	// 日志内容
	MatchContent string `cname:"匹配日志内容" json:"matchContent,omitempty"`
	// 源数据ID
	SourceId int `cname:"源数据ID" json:"sourceId"`
	// 多个源数据ID
	InSourceId []int `cname:"多个源数据ID" json:"inSourceId"`
	// 页码
	PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
	// 页数
	PageSize int `cname:"页数" json:"pageSize,omitempty"`

	Year  int `cname:"年" json:"year,omitempty"`
	Month int `cname:"月" json:"month,omitempty"`
	Day   int `cname:"日" json:"day,omitempty"`
	// 开始时间
	BeginTime time.Time `cname:"开始时间" json:"beginTime"`
	// 结束时间
	EndTime time.Time `cname:"结束时间" json:"endTime"`
	// 按log_id 排序
	SortByLogId string `json:"sortByLogId"`
	Context     *domain.Context
}

func (cmd *SearchLogCommand) Valid(validation *validation.Validation) {
	if cmd.Year > 0 && cmd.Month == 0 && cmd.Day == 0 {
		cmd.BeginTime = time.Date(cmd.Year, 1, 1, 0, 0, 0, 0, time.Local)
		cmd.EndTime = cmd.BeginTime.AddDate(1, 0, 0)
	}
	if cmd.Year > 0 && cmd.Month > 0 && cmd.Day == 0 {
		cmd.BeginTime = time.Date(cmd.Year, time.Month(cmd.Month), 1, 0, 0, 0, 0, time.Local)
		cmd.EndTime = cmd.BeginTime.AddDate(0, 1, 0)
	}
	if cmd.Year > 0 && cmd.Month > 0 && cmd.Day > 0 {
		cmd.BeginTime = time.Date(cmd.Year, time.Month(cmd.Month), cmd.Day, 0, 0, 0, 0, time.Local)
		cmd.EndTime = cmd.BeginTime.AddDate(0, 0, cmd.Day)
	}
}

func (cmd *SearchLogCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(cmd)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(cmd).Elem()
		for _, validErr := range valid.Errors {
			field, isExist := elem.FieldByName(validErr.Field)
			if isExist {
				return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
			} else {
				return fmt.Errorf(validErr.Message)
			}
		}
	}
	return nil
}