activity_remind.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package activity
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "hongze/hongze_yb/models/tables/yb_activity"
  6. "hongze/hongze_yb/models/tables/yb_activity_remind"
  7. "hongze/hongze_yb/services/user"
  8. "log"
  9. "time"
  10. )
  11. type RemindCheckInfo struct {
  12. Specific int `json:"specific" description:"是否指定返回"`
  13. Msg string `json:"msg" description:"提示信息"`
  14. Type string `json:"type" description:"校验失败,需要让前端处理的类型,枚举值:time"`
  15. }
  16. // CreateRemind 新增提醒
  17. func CreateRemind(activityId int, userInfo user.UserInfo) (err error, remindCheck RemindCheckInfo) {
  18. userId := int(userInfo.UserID)
  19. ok, msg, remindCheck := checkRemindOk(userId, activityId)
  20. if !ok {
  21. err = errors.New(msg)
  22. return
  23. }
  24. remindInfo := &yb_activity_remind.YbActivityRemind{
  25. ActivityID: uint32(activityId),
  26. UserID: uint64(userId),
  27. CompanyID: uint64(userInfo.CompanyID),
  28. RealName: userInfo.RealName,
  29. Mobile: userInfo.Mobile,
  30. CreateTime: time.Now(),
  31. }
  32. err = remindInfo.Create()
  33. if err != nil {
  34. log.Print(err)
  35. err = errors.New("设置提醒失败")
  36. }
  37. return
  38. }
  39. // checkRemindOk 验证是否可以新增提醒
  40. func checkRemindOk(userId, activityId int) (ok bool, msg string, remindCheck RemindCheckInfo) {
  41. activity, err := yb_activity.GetOneById(activityId)
  42. if err == gorm.ErrRecordNotFound {
  43. msg = "活动信息有误"
  44. return
  45. }
  46. _, err = yb_activity_remind.RemindExist(userId, activityId)
  47. if err != gorm.ErrRecordNotFound {
  48. msg = "请勿重复设置提醒"
  49. return
  50. }
  51. // 活动时间
  52. nowUtime := time.Now().Unix()
  53. endUtime := activity.EndTime.Unix()
  54. if nowUtime > endUtime {
  55. msg = "活动已结束"
  56. return
  57. }
  58. nowUtime += 60 * 15
  59. startUtime := activity.StartTime.Unix()
  60. if nowUtime > startUtime {
  61. msg = "活动开始前十五分钟内无法设置会议提醒"
  62. remindCheck = RemindCheckInfo{
  63. Specific: 1,
  64. Msg: msg,
  65. Type: "time",
  66. }
  67. return
  68. }
  69. ok = true
  70. return
  71. }
  72. // CancelRemind 取消提醒
  73. func CancelRemind(activityId int, userInfo user.UserInfo) (err error) {
  74. userId := int(userInfo.UserID)
  75. _, err = yb_activity.GetOneById(activityId)
  76. if err == gorm.ErrRecordNotFound {
  77. err = errors.New("活动信息有误")
  78. return
  79. }
  80. remindInfo, err := yb_activity_remind.RemindExist(userId, activityId)
  81. if err == gorm.ErrRecordNotFound {
  82. err = errors.New("提醒信息有误")
  83. return
  84. }
  85. err = yb_activity_remind.Delete(remindInfo)
  86. if err != nil {
  87. log.Print(err.Error())
  88. err = errors.New("取消提醒失败")
  89. }
  90. return
  91. }