dialog.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/vip/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _vipConfDialog = "vip_conf_dialog"
  11. )
  12. // DialogAll .
  13. func (d *Dao) DialogAll(c context.Context, appID, platform int64, status string) (res []*model.ConfDialog, err error) {
  14. db := d.vip.Table(_vipConfDialog)
  15. if appID != 0 {
  16. db = db.Where("app_id=?", appID)
  17. }
  18. if platform != 0 {
  19. db = db.Where("platform=?", platform)
  20. }
  21. if len(status) > 0 {
  22. curr := time.Now().Format("2006-01-02 15:04:05")
  23. //padding:待生效,active:已经生效,inactive:已经失效
  24. switch status {
  25. case "padding":
  26. db = db.Where("stage = true AND start_time>?", curr)
  27. case "active":
  28. db = db.Where("stage = true AND start_time<=? AND (end_time = '1970-01-01 08:00:00' OR end_time >?)", curr, curr)
  29. case "inactive":
  30. db = db.Where("stage = false OR (end_time > '1970-01-01 08:00:00' AND end_time < ?)", curr)
  31. default:
  32. log.Info("query all dialog.")
  33. }
  34. }
  35. if err := db.Find(&res).Error; err != nil {
  36. return nil, err
  37. }
  38. return
  39. }
  40. // DialogByID vip price config by id.
  41. func (d *Dao) DialogByID(c context.Context, id int64) (dlg *model.ConfDialog, err error) {
  42. dlg = &model.ConfDialog{}
  43. if err := d.vip.Table(_vipConfDialog).Where("id=?", id).First(dlg).Error; err != nil {
  44. if err == ecode.NothingFound {
  45. err = nil
  46. }
  47. return nil, err
  48. }
  49. return
  50. }
  51. // DialogBy vip price config by .
  52. func (d *Dao) DialogBy(c context.Context, appID, platform int64, id int64) (res []*model.ConfDialog, err error) {
  53. if err := d.vip.Table(_vipConfDialog).Where("stage = true AND app_id=? AND platform=? AND id<>?", appID, platform, id).Find(&res).Error; err != nil {
  54. return nil, err
  55. }
  56. return
  57. }
  58. // DialogSave .
  59. func (d *Dao) DialogSave(c context.Context, arg *model.ConfDialog) (eff int64, err error) {
  60. db := d.vip.Table(_vipConfDialog).Save(arg)
  61. if err = db.Error; err != nil {
  62. return
  63. }
  64. eff = db.RowsAffected
  65. return
  66. }
  67. // DialogEnable .
  68. func (d *Dao) DialogEnable(c context.Context, arg *model.ConfDialog) (eff int64, err error) {
  69. stage := map[string]interface{}{
  70. "stage": arg.Stage,
  71. "end_time": time.Now(),
  72. "operator": arg.Operator,
  73. }
  74. db := d.vip.Table(_vipConfDialog).Where("id=?", arg.ID).Updates(stage)
  75. if err = db.Error; err != nil {
  76. return
  77. }
  78. eff = db.RowsAffected
  79. return
  80. }
  81. // DialogDel delete vip price config by id.
  82. func (d *Dao) DialogDel(c context.Context, id int64) (eff int64, err error) {
  83. db := d.vip.Table(_vipConfDialog).Where("id=?", id).Delete(&model.ConfDialog{})
  84. if err = db.Error; err != nil {
  85. return
  86. }
  87. eff = db.RowsAffected
  88. return
  89. }
  90. // CountDialogByPlatID count dialog by platform id .
  91. func (d *Dao) CountDialogByPlatID(c context.Context, plat int64) (count int64, err error) {
  92. if err := d.vip.Table(_vipConfDialog).Where("platform=?", plat).Count(&count).Error; err != nil {
  93. return 0, err
  94. }
  95. return
  96. }