rule.go 969 B

1234567891011121314151617181920212223242526272829303132333435
  1. package models
  2. import (
  3. "pmail/db"
  4. "pmail/utils/context"
  5. "pmail/utils/errors"
  6. )
  7. type Rule struct {
  8. Id int `db:"id" json:"id"`
  9. UserId string `db:"user_id" json:"user_id"`
  10. Name string `db:"name" json:"name"`
  11. Value string `db:"value" json:"value"`
  12. Action int `db:"action" json:"action"`
  13. Params string `db:"params" json:"params"`
  14. Sort int `db:"sort" json:"sort"`
  15. }
  16. func (p *Rule) Save(ctx *context.Context) error {
  17. if p.Id > 0 {
  18. _, 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)
  19. if err != nil {
  20. return errors.Wrap(err)
  21. }
  22. return nil
  23. } else {
  24. _, 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)
  25. if err != nil {
  26. return errors.Wrap(err)
  27. }
  28. return nil
  29. }
  30. }