rule.go 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dto
  2. import (
  3. "encoding/json"
  4. "pmail/models"
  5. )
  6. type RuleType int
  7. // 1已读,2转发,3删除
  8. var (
  9. READ RuleType = 1
  10. FORWARD RuleType = 2
  11. DELETE RuleType = 3
  12. MOVE RuleType = 4
  13. )
  14. type Rule struct {
  15. Id int `json:"id"`
  16. Name string `json:"name"`
  17. Rules []*Value `json:"rules"`
  18. Action RuleType `json:"action"`
  19. Params string `json:"params"`
  20. Sort int `json:"sort"`
  21. }
  22. type Value struct {
  23. Field string `json:"field"`
  24. Type string `json:"type"`
  25. Rule string `json:"rule"`
  26. }
  27. func (p *Rule) Decode(data *models.Rule) *Rule {
  28. json.Unmarshal([]byte(data.Value), &p.Rules)
  29. p.Id = data.Id
  30. p.Name = data.Name
  31. p.Action = RuleType(data.Action)
  32. p.Sort = data.Sort
  33. p.Params = data.Params
  34. return p
  35. }
  36. func (p *Rule) Encode() *models.Rule {
  37. v, _ := json.Marshal(p.Rules)
  38. ret := &models.Rule{
  39. Id: p.Id,
  40. Name: p.Name,
  41. Value: string(v),
  42. Action: int(p.Action),
  43. Sort: p.Sort,
  44. Params: p.Params,
  45. }
  46. return ret
  47. }