互动
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.

96 lines
3.0 KiB

5 years ago
  1. package client
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. ws_send_service "hudongzhuanjia/services/ws_send"
  8. "hudongzhuanjia/utils/code"
  9. "hudongzhuanjia/utils/define"
  10. "time"
  11. )
  12. //签到
  13. type SignCtl struct {
  14. controllers.AuthorCtl
  15. }
  16. //签到动作
  17. func (t *SignCtl) Sign() {
  18. uid := t.MustGetUID()
  19. activityId := t.MustGetInt64("activity_id")
  20. customerId := t.MustGetInt64("customer_id")
  21. //根据activity_id查找主活动的信息
  22. activity := new(models.Activity)
  23. exist, err := models.GetById(activity, activityId)
  24. t.CheckErr(err)
  25. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  26. t.CheckRunning(activity.Status)
  27. customer := new(models.Customer)
  28. exist, err = models.GetById(customer, customerId)
  29. t.CheckErr(err)
  30. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  31. user := new(models.User)
  32. exist, err = models.GetById(user, uid)
  33. t.CheckErr(err)
  34. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  35. //根据activity_id查找副活动的规则信息
  36. signUp := new(models.SignUp)
  37. exist, err = signUp.GetByActivityId(activityId)
  38. t.CheckErr(err)
  39. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "签到规则不存在")
  40. if signUp.OnlyInvitation == 1 { //
  41. // 邀请函才能签到
  42. letter := new(models.InvitationLetter)
  43. exist, err := letter.GetByUserIdAndActivityId(uid, activityId, activity.RehearsalId)
  44. t.CheckErr(err)
  45. t.Assert(exist, code.MSG_INVITE_LETTER_NOT_EXIST, "您没收到邀请函")
  46. }
  47. //检查是否已经签到了
  48. signHistory := new(models.SignHistory)
  49. exist, err = signHistory.GetByUserId(activityId, uid, activity.RehearsalId, t.MustGetAreaId())
  50. t.CheckErr(err)
  51. t.Assert(!exist, code.MSG_SIGN_HISTORY_EXIST, "您已经签到过了") // 存在判断
  52. // 签到人数
  53. signTotal, err := core.GetXormAuto().Where("is_delete=0 and sign_rule_id=? and rehearsal_id=? and activity_id=?",
  54. signUp.Id, activity.RehearsalId, activity.Id).Count(signHistory)
  55. t.CheckErr(err)
  56. signUpTotal, err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activity.Id).Count(new(models.InvitationLetter))
  57. t.CheckErr(err)
  58. if activity.RehearsalId != 0 && signTotal >= 10 {
  59. t.ERROR("彩排人数不能超过10人", code.MSG_SIGN_UP_REHEARSAL_LIMIT)
  60. }
  61. signHistory = new(models.SignHistory)
  62. signHistory.UserId = uid
  63. signHistory.RehearsalId = activity.RehearsalId
  64. signHistory.ActivityId = activityId
  65. signHistory.SignRuleId = signUp.Id
  66. signHistory.AreaId = user.AreaId
  67. signHistory.IsDelete = false
  68. signHistory.UpdatedAt = time.Now()
  69. signHistory.CreatedAt = time.Now()
  70. _, err = core.GetXormAuto().InsertOne(signHistory)
  71. t.CheckErr(err)
  72. go ws_send_service.SendSign(fmt.Sprintf("%d", activity.Id),
  73. define.TYPE_CUSTOMER, customerId, map[string]interface{}{
  74. "type": "sign_up",
  75. "customer_id": customer.Id,
  76. "data": map[string]interface{}{
  77. "avatar": user.Avatar,
  78. "sign_total": signTotal + 1,
  79. "sign_up_total": signUpTotal,
  80. },
  81. })
  82. t.SUCCESS("签到成功")
  83. }