GAAS 广汽安道拓GFrame金属件MOM项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

  1. package grmi
  2. import (
  3. "log"
  4. "strings"
  5. )
  6. type Condition struct {
  7. *Paging
  8. Items map[string]ConditionItem
  9. OrderByFields []Field
  10. }
  11. func NewCondition(items map[string]ConditionItem, paging *Paging) Condition {
  12. orderByFields := make([]Field, 0, 10)
  13. conditionItems := make(map[string]ConditionItem, 0)
  14. for key, item := range items {
  15. if item.OrderBy {
  16. orderByFields = append(orderByFields, item.Field)
  17. }
  18. valueParser, ok := valueParsers[item.Field.GoType]
  19. if ok {
  20. item.valueParser = valueParser
  21. conditionItems[key] = item
  22. } else {
  23. log.Fatalf("不支持字段%s作为查询条件!\r\n", item.Field.Name)
  24. }
  25. }
  26. return Condition{Items: conditionItems, Paging: paging, OrderByFields: orderByFields}
  27. }
  28. func (self *Condition) BuildPredicates(urlParameters map[string]string) ([]Predicate, error) {
  29. predicates := make([]Predicate, 0, len(urlParameters))
  30. for name, value := range urlParameters {
  31. item, ok := self.Items[strings.ToLower(name)]
  32. if !ok {
  33. continue
  34. }
  35. predicateValue, err := item.valueParser(value)
  36. if err != nil {
  37. return nil, NewRequestError(err)
  38. }
  39. predicates = append(predicates, NewPredicate(item.ColumnName, item.PredicateType, predicateValue))
  40. }
  41. return predicates, nil
  42. }