推送
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.3 KiB

4 years ago
  1. package hasaki_push
  2. import (
  3. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
  4. "git.ouxuan.net/hasaki-service/hasaki-sdk/hsktime"
  5. "git.ouxuan.net/tommy/hasaki-service-push/hasaki_push_options"
  6. "xorm.io/xorm"
  7. )
  8. type Api struct {
  9. db *xorm.Engine
  10. config interface{}
  11. }
  12. func NewApi(db ...*xorm.Engine) *Api {
  13. if len(db) > 0 {
  14. return &Api{
  15. db: db[0],
  16. }
  17. } else {
  18. return &Api{
  19. db: hskdb.GetXormAuto(),
  20. }
  21. }
  22. }
  23. func GetApi(ctx interface{}) *Api {
  24. config := hasaki_push_options.GetMysqlConfig(ctx)
  25. if config == nil {
  26. api := NewApi(hskdb.GetXormAuto())
  27. return api
  28. } else {
  29. api := NewApi(hskdb.GetXormAuto(config))
  30. api.config = config
  31. return api
  32. }
  33. }
  34. type Model struct {
  35. Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
  36. CreatedAt hsktime.Time `json:"created_at" xorm:"created comment('创建时间')"`
  37. UpdatedAt hsktime.Time `json:"updated_at" xorm:"updated comment('更新时间')"`
  38. DeletedAt hsktime.Time `json:"-" xorm:"deleted comment('是否删除') TIMESTAMP"`
  39. Db *xorm.Engine `json:"-" xorm:"-"`
  40. }
  41. func NewModel(engs ...*xorm.Engine) *Model {
  42. var eng *xorm.Engine
  43. if len(engs) > 0 {
  44. eng = engs[0]
  45. } else {
  46. eng = hskdb.GetXormAuto()
  47. }
  48. return &Model{
  49. Db: eng,
  50. }
  51. }
  52. func (api *Api) NewModel() *Model {
  53. return &Model{Db: api.db}
  54. }