backend/app/brahma/api/murder/internal/model/dto.go

125 lines
3.8 KiB
Go
Raw Permalink Normal View History

2022-01-12 14:34:31 +08:00
package model
import (
"fmt"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
"github.com/pkg/errors"
"strings"
)
const (
jsonArray = "jsonArr"
normal = "normal"
)
type (
//区分query中不同规则
ruleType struct {
rule string
value string
}
QueryMap map[string]ruleType
)
type GetSqlMapFromQuery interface {
// ProcessMap 生成queryMap
ProcessMap() (QueryMap, error)
}
type GetQuery struct {
Query string
}
type ScriptsDto struct {
ScriptName string `json:"script_name" gorm:"column:script_name"`
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
ScriptTag strs `json:"script_tag" gorm:"column:script_tag"`
ScriptScore float64 `json:"script_score" gorm:"column:script_score"`
GroupDuration int `json:"group_duration" gorm:"column:group_duration"`
ScriptCoverUrl string `json:"script_cover_url" gorm:"column:script_cover_url"`
ScriptTextContext string `json:"script_text_context" gorm:"column:script_text_context"`
ScriptPlotScore float64 `json:"script_plot_score" gorm:"column:script_plot_score"`
ScriptImageContent strs `json:"script_image_content" gorm:"column:script_image_content"`
ScriptMalePlayer int `json:"script_male_player" gorm:"column:script_male_player"`
ScriptFemalePlayer int `json:"script_female_player" gorm:"column:script_female_player"`
ScriptDifficultDegree string `json:"script_difficult_degree" gorm:"column:script_difficult_degree"`
ScriptPlayerLimit int `json:"script_player_limit" gorm:"column:script_player_limit"`
Uuid string `json:"uuid" gorm:"column:uuid" valid:"no_empty"`
ScriptComplexScore float64 `json:"script_complex_score" gorm:"column:script_complex_score"`
}
//检查语法
func checkGrammar(value string) bool {
//如果 = 左右两侧 不是完整的key、value返回错误
var pairs []string
if pairs = strings.Split(value, "="); len(pairs) != 2 {
return false
}
//如果 value 没有完整闭合括号则返回错误
bytes := []byte(pairs[1])
if (bytes[0] == '(' && bytes[len(bytes)-1] != ')') || (bytes[len(bytes)-1] == ')' && bytes[0] != '(') {
return false
}
return true
}
//处理json数组
func processJsonArr(pair []string) (string, bool) {
value := fmt.Sprintf("JSON_CONTAINS(%v,JSON_ARRAY(", pair[0])
//处理括号,遍历元素
arrs := strings.Split(string([]byte(pair[1])[1:len([]byte(pair[1]))-1]), "|")
for _, v := range arrs {
//如果大括号里面没有元素
if v == "" {
return "", false
}
value = fmt.Sprintf("%v%v,", value, v)
}
s := []byte(value)
value = string(s[0:len(s)-1]) + "))"
return value, true
}
// ProcessMap 生成查询map
func (g GetQuery) ProcessMap() (QueryMap, error) {
m := make(QueryMap)
//寻找没有软删掉的数据
m["is_del"] = ruleType{rule: normal, value: `0`}
//不满足最小查询条件直接返回
if len([]byte(g.Query)) < 5 {
return m, nil
}
//括号不匹配直接返回
if bytes := []byte(g.Query); bytes[0] != '(' || bytes[len(bytes)-1] != ')' {
return nil, errors.New(error_process.GrammerError)
}
//处理query字符串、掐头去尾
g.Query = string([]byte(g.Query)[1 : len([]byte(g.Query))-1])
//遍历处理过的字符串根据query规则生成queryMap
for _, v := range strings.Split(g.Query, ",") {
if !checkGrammar(v) {
return nil, errors.New(error_process.GrammerError)
}
pairs := strings.Split(v, "=")
bytes := []byte(pairs[1])
//是否是json数组
if bytes[0] == '(' && bytes[len(bytes)-1] == ')' {
if value, usable := processJsonArr(pairs); usable {
m[pairs[0]] = ruleType{
rule: jsonArray,
value: value,
}
continue
}
return nil, errors.New(error_process.GrammerError)
}
//普通字段
m[pairs[0]] = ruleType{
rule: normal,
value: pairs[1],
}
}
return m, nil
}