strategy.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package model
  2. import (
  3. "github.com/710leo/urlooker/dataobj"
  4. . "github.com/710leo/urlooker/modules/web/store"
  5. )
  6. type Strategy dataobj.Strategy
  7. func GetAllStrategyCount(mine int, query, username string) (int64, error) {
  8. if mine == 1 {
  9. if query != "" {
  10. return Orm.Where("url LIKE ? AND creator = ?", "%"+query+"%", username).Count(new(Strategy))
  11. } else {
  12. num, err := Orm.Where("creator = ?", username).Count(new(Strategy))
  13. return num, err
  14. }
  15. } else {
  16. if query != "" {
  17. return Orm.Where("url LIKE ?", "%"+query+"%").Count(new(Strategy))
  18. } else {
  19. num, err := Orm.Count(new(Strategy))
  20. return num, err
  21. }
  22. }
  23. }
  24. func GetAllStrategy(mine, limit, offset int, query, username string) ([]*Strategy, error) {
  25. items := make([]*Strategy, 0)
  26. var err error
  27. if mine == 1 {
  28. if query != "" {
  29. err = Orm.Where("url LIKE ? AND creator = ?", "%"+query+"%", username).Limit(limit, offset).OrderBy("id").Find(&items)
  30. } else {
  31. err = Orm.Where("creator = ?", username).Limit(limit, offset).OrderBy("id").Find(&items)
  32. }
  33. } else {
  34. if query != "" {
  35. err = Orm.Where("url LIKE ?", "%"+query+"%").Limit(limit, offset).OrderBy("id").Find(&items)
  36. } else {
  37. err = Orm.Limit(limit, offset).Find(&items)
  38. }
  39. }
  40. return items, err
  41. }
  42. func GetAllStrategyByCron() ([]*Strategy, error) {
  43. strategies := make([]*Strategy, 0)
  44. err := Orm.Where("enable = 1").Find(&strategies)
  45. return strategies, err
  46. }
  47. func GetStrategyById(sid int64) (*Strategy, error) {
  48. strategy := new(Strategy)
  49. _, err := Orm.Where("id=?", sid).Get(strategy)
  50. return strategy, err
  51. }
  52. func (this *Strategy) Add() (int64, error) {
  53. _, err := Orm.Insert(this)
  54. return this.Id, err
  55. }
  56. func (this *Strategy) Update() error {
  57. _, err := Orm.Where("id=?", this.Id).Cols("times", "max_step", "expect_code", "timeout", "url", "idc", "enable", "ip", "keywords", "note", "data", "tag", "endpoint", "header", "post_data", "method", "teams").Update(this)
  58. return err
  59. }
  60. func (this *Strategy) Delete() error {
  61. _, err := Orm.Where("id=?", this.Id).Delete(new(Strategy))
  62. return err
  63. }