rule.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dto
  2. import (
  3. "encoding/json"
  4. "github.com/Jinnrry/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. UserId int `json:"user_id"`
  17. Name string `json:"name"`
  18. Rules []*Value `json:"rules"`
  19. Action RuleType `json:"action"`
  20. Params string `json:"params"`
  21. Sort int `json:"sort"`
  22. }
  23. type Value struct {
  24. Field string `json:"field"`
  25. Type string `json:"type"`
  26. Rule string `json:"rule"`
  27. }
  28. func (p *Rule) Decode(data *models.Rule) *Rule {
  29. json.Unmarshal([]byte(data.Value), &p.Rules)
  30. p.Id = data.Id
  31. p.Name = data.Name
  32. p.Action = RuleType(data.Action)
  33. p.Sort = data.Sort
  34. p.Params = data.Params
  35. p.UserId = data.UserId
  36. return p
  37. }
  38. func (p *Rule) Encode() *models.Rule {
  39. v, _ := json.Marshal(p.Rules)
  40. ret := &models.Rule{
  41. Id: p.Id,
  42. Name: p.Name,
  43. Value: string(v),
  44. Action: int(p.Action),
  45. Sort: p.Sort,
  46. Params: p.Params,
  47. }
  48. return ret
  49. }