123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package definition
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
- "git.sxidc.com/go-framework/baize/framework/core/tag/rule"
- "git.sxidc.com/go-tools/utils/reflectutils"
- "github.com/pkg/errors"
- "reflect"
- )
- type Operator struct {
- Name string `json:"name"`
- CNName string `json:"cnName"`
- }
- const (
- opEqual = "eq"
- opNotEqual = "ne"
- opLessThan = "lt"
- opLessThanOrEqual = "lte"
- opGreaterThan = "gt"
- opGreaterThanOrEqual = "gte"
- opIn = "in"
- opNotIn = "not-in"
- opPrefix = "prefix"
- opSuffix = "suffix"
- opContains = "contains"
- )
- func IsSupportedOperator(op string) bool {
- return op == opEqual || op == opNotEqual ||
- op == opLessThan || op == opLessThanOrEqual ||
- op == opGreaterThan || op == opGreaterThanOrEqual ||
- op == opIn || op == opNotIn ||
- op == opPrefix || op == opSuffix || op == opContains
- }
- var opTranslationMap = map[string]string{
- opEqual: "等于",
- opNotEqual: "不等于",
- opLessThan: "小于",
- opLessThanOrEqual: "小于等于",
- opGreaterThan: "大于",
- opGreaterThanOrEqual: "大于等于",
- opIn: "包含在列表",
- opNotIn: "不包含在列表",
- opPrefix: "包含前缀",
- opSuffix: "包含后缀",
- opContains: "包含",
- }
- var (
- operatorEqual = Operator{
- Name: opEqual,
- CNName: opTranslationMap[opEqual],
- }
- operatorNotEqual = Operator{
- Name: opNotEqual,
- CNName: opTranslationMap[opNotEqual],
- }
- operatorLessThan = Operator{
- Name: opLessThan,
- CNName: opTranslationMap[opLessThan],
- }
- operatorLessThanOrEqual = Operator{
- Name: opLessThanOrEqual,
- CNName: opTranslationMap[opLessThanOrEqual],
- }
- operatorGreaterThan = Operator{
- Name: opGreaterThan,
- CNName: opTranslationMap[opGreaterThan],
- }
- operatorGreaterThanOrEqual = Operator{
- Name: opGreaterThanOrEqual,
- CNName: opTranslationMap[opGreaterThanOrEqual],
- }
- operatorIn = Operator{
- Name: opIn,
- CNName: opTranslationMap[opIn],
- }
- operatorNotIn = Operator{
- Name: opNotIn,
- CNName: opTranslationMap[opNotIn],
- }
- operatorPrefix = Operator{
- Name: opPrefix,
- CNName: opTranslationMap[opPrefix],
- }
- operatorSuffix = Operator{
- Name: opSuffix,
- CNName: opTranslationMap[opSuffix],
- }
- operatorContains = Operator{
- Name: opContains,
- CNName: opTranslationMap[opContains],
- }
- )
- var operatorOfTypeMap = map[string][]Operator{
- rule.TypeString: {
- operatorEqual, operatorNotEqual, operatorIn, operatorNotIn, operatorPrefix, operatorSuffix, operatorContains,
- },
- rule.TypeTime: {
- operatorEqual, operatorNotEqual, operatorLessThan, operatorLessThanOrEqual, operatorGreaterThan, operatorGreaterThanOrEqual, operatorIn, operatorNotIn,
- },
- rule.TypeNumber: {
- operatorEqual, operatorNotEqual, operatorLessThan, operatorLessThanOrEqual, operatorGreaterThan, operatorGreaterThanOrEqual, operatorIn, operatorNotIn,
- },
- rule.TypeBool: {
- operatorEqual, operatorNotEqual,
- },
- }
- func AddCondition(conditions *clause.Conditions, op string, columnName string, value any) error {
- switch op {
- case opEqual:
- conditions.Equal(columnName, value)
- case opNotEqual:
- conditions.Not(columnName, value)
- case opLessThan:
- conditions.LessThan(columnName, value)
- case opLessThanOrEqual:
- conditions.LessThanAndEqual(columnName, value)
- case opGreaterThan:
- conditions.GreaterThan(columnName, value)
- case opGreaterThanOrEqual:
- conditions.GreaterThanAndEqual(columnName, value)
- case opIn:
- if reflect.TypeOf(value).Kind() != reflect.Slice {
- return errors.New("使用\"包含在列表\"操作符必须使用列表")
- }
- conditions.In(columnName, value)
- case opNotIn:
- if reflect.TypeOf(value).Kind() != reflect.Slice {
- return errors.New("使用\"不包含在列表\"操作符必须使用列表")
- }
- conditions.NotIn(columnName, value)
- case opPrefix:
- strValue, err := reflectutils.ToString(value)
- if err != nil {
- return err
- }
- conditions.Like(columnName, strValue+"%")
- case opSuffix:
- strValue, err := reflectutils.ToString(value)
- if err != nil {
- return err
- }
- conditions.Like(columnName, "%"+strValue)
- case opContains:
- strValue, err := reflectutils.ToString(value)
- if err != nil {
- return err
- }
- conditions.Like(columnName, "%"+strValue+"%")
- default:
- return errors.Errorf("不支持的操作符%v", op)
- }
- return nil
- }
|