rule.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import (
  3. "pmail/db"
  4. "pmail/utils/context"
  5. "pmail/utils/errors"
  6. )
  7. type Rule struct {
  8. Id int `xorm:"id int unsigned not null pk autoincr" json:"id"`
  9. UserId int `xorm:"user_id notnull default(0) comment('用户id')" json:"user_id"`
  10. Name string `xorm:"name notnull default('') comment('规则名称')" json:"name"`
  11. Value string `xorm:"value text comment('规则内容')" json:"value"`
  12. Action int `xorm:"action notnull default(0) comment('执行动作,1已读,2转发,3删除')" json:"action"`
  13. Params string `xorm:"params notnull default('') comment('执行参数')" json:"params"`
  14. Sort int `xorm:"sort notnull default(0) comment('排序,越大约优先')" json:"sort"`
  15. }
  16. func (p *Rule) TableName() string {
  17. return "rule"
  18. }
  19. func (p *Rule) Save(ctx *context.Context) error {
  20. if p.Id > 0 {
  21. _, err := db.Instance.Exec(db.WithContext(ctx, "update rule set name=? ,value = ? ,action = ?,params = ?,sort = ? where id = ?"), p.Name, p.Value, p.Action, p.Params, p.Sort, p.Id)
  22. if err != nil {
  23. return errors.Wrap(err)
  24. }
  25. return nil
  26. } else {
  27. _, err := db.Instance.Exec(db.WithContext(ctx, "insert into rule (name,value,user_id,action,params,sort) values (?,?,?,?,?,?)"), p.Name, p.Value, ctx.UserID, p.Action, p.Params, p.Sort)
  28. if err != nil {
  29. return errors.Wrap(err)
  30. }
  31. return nil
  32. }
  33. }