dialog.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package service
  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. // DialogAll .
  10. func (s *Service) DialogAll(c context.Context, appID, platform int64, status string) (res []*model.ConfDialogList, err error) {
  11. var list []*model.ConfDialog
  12. if list, err = s.dao.DialogAll(c, appID, platform, status); err != nil {
  13. return
  14. }
  15. res = make([]*model.ConfDialogList, len(list))
  16. for i, v := range list {
  17. res[i] = &model.ConfDialogList{ConfDialog: v, Status: dialogStatus(v)}
  18. }
  19. return
  20. }
  21. func dialogStatus(v *model.ConfDialog) (status string) {
  22. curr := time.Now()
  23. switch {
  24. case v.Stage && v.StartTime.Time().After(curr):
  25. return "padding"
  26. case v.Stage && v.StartTime.Time().Before(curr) && (int64(v.EndTime) == 0 || v.EndTime.Time().After(curr)):
  27. return "active"
  28. case !v.Stage || (int64(v.EndTime) > 0 && v.EndTime.Time().Before(curr)):
  29. return "inactive"
  30. default:
  31. return ""
  32. }
  33. }
  34. // DialogByID .
  35. func (s *Service) DialogByID(c context.Context, arg *model.ArgID) (dlg *model.ConfDialog, err error) {
  36. return s.dao.DialogByID(c, arg.ID)
  37. }
  38. // DialogSave .
  39. func (s *Service) DialogSave(c context.Context, arg *model.ConfDialog) (eff int64, err error) {
  40. var db *model.ConfDialog
  41. if arg.ID != 0 {
  42. if db, err = s.dao.DialogByID(c, arg.ID); err != nil {
  43. return
  44. }
  45. if db != nil {
  46. arg.Ctime = db.Ctime
  47. arg.Stage = db.Stage
  48. }
  49. }
  50. // PRD: 如果平台、app_id完全相同时,生效时间不可冲突,否则不允许保存
  51. var exist []*model.ConfDialog
  52. if exist, err = s.dao.DialogBy(c, arg.AppID, arg.Platform, arg.ID); err != nil {
  53. return
  54. }
  55. for _, v := range exist {
  56. if v.StartTime.Time().Unix() <= arg.EndTime.Time().Unix() && v.EndTime.Time().Unix() >= arg.StartTime.Time().Unix() {
  57. err = ecode.VipDialogConflictErr
  58. return
  59. }
  60. }
  61. return s.dao.DialogSave(c, arg)
  62. }
  63. // DialogEnable .
  64. func (s *Service) DialogEnable(c context.Context, arg *model.ConfDialog) (eff int64, err error) {
  65. var db *model.ConfDialog
  66. if arg.ID != 0 {
  67. if db, err = s.dao.DialogByID(c, arg.ID); err != nil {
  68. return
  69. }
  70. }
  71. if db == nil || db.StartTime.Time().After(time.Now()) {
  72. err = ecode.VipDialogTimeErr
  73. return
  74. }
  75. return s.dao.DialogEnable(c, arg)
  76. }
  77. // DialogDel .
  78. func (s *Service) DialogDel(c context.Context, arg *model.ArgID, operator string) (eff int64, err error) {
  79. eff, err = s.dao.DialogDel(c, arg.ID)
  80. log.Warn("user(%s) delete dialog(%d) effect row(%d)", operator, arg.ID, eff)
  81. return
  82. }