1
0

item_status.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package model
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/710leo/urlooker/modules/web/g"
  8. . "github.com/710leo/urlooker/modules/web/store"
  9. )
  10. type ItemStatus struct {
  11. Id int64 `json:"id"`
  12. Sid int64 `json:"sid"`
  13. Ip string `json:"ip"`
  14. RespTime int `json:"resp_time"`
  15. RespCode string `json:"resp_code"`
  16. PushTime int64 `json:"push_time"`
  17. Result int64 `json:"result"`
  18. }
  19. var ItemStatusRepo *ItemStatus
  20. func (this *ItemStatus) Save() error {
  21. sql := fmt.Sprintf("insert into item_status00 (ip, sid, resp_time, resp_code, push_time, result) value(?,?,?,?,?,?)")
  22. _, err := Orm.Exec(sql, this.Ip, this.Sid, this.RespTime, this.RespCode, this.PushTime, this.Result)
  23. return err
  24. }
  25. func (this *ItemStatus) GetByIpAndSid(ip string, sid int64) ([]*ItemStatus, error) {
  26. itemStatusArr := make([]*ItemStatus, 0)
  27. ts := time.Now().Unix() - int64(g.Config.ShowDurationMin*60)
  28. sql := fmt.Sprintf("select * from item_status00 where ip=? and sid=? and push_time > ?")
  29. err := Orm.Sql(sql, ip, sid, ts).Find(&itemStatusArr)
  30. return itemStatusArr, err
  31. }
  32. func (this *ItemStatus) PK() string {
  33. h := md5.New()
  34. io.WriteString(h, fmt.Sprintf("%s/%s", this.Sid, this.Ip))
  35. return fmt.Sprintf("%x", h.Sum(nil))
  36. }
  37. func (this *ItemStatus) DeleteOld(d int64) error {
  38. ts := time.Now().Unix() - 12*60*60
  39. sql := fmt.Sprintf("delete from item_status00 where push_time < ?")
  40. _, err := Orm.Exec(sql, ts)
  41. return err
  42. }