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

232 lines
6.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. "hudongzhuanjia/utils"
  7. "hudongzhuanjia/utils/code"
  8. "hudongzhuanjia/utils/define"
  9. "time"
  10. "github.com/ouxuanserver/osmanthuswine/src/core"
  11. )
  12. type TugOfWarCtl struct {
  13. controllers.AuthorCtl
  14. }
  15. func (t *TugOfWarCtl) Ready() { // 准备中
  16. baheId := t.MustGetInt64("bahe_activity_id")
  17. bahe := new(models.TugOfWar)
  18. exist, err := models.Get(bahe, baheId)
  19. t.CheckErr(err)
  20. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  21. if bahe.Status != define.StatusNotBegin {
  22. t.ERROR(fmt.Sprintf("该活动%s", bahe.Status), code.MSG_ERR)
  23. }
  24. // done: 把其他的准备中的状态改为未开始
  25. _, err = bahe.UpdateToStatusByAid(bahe.ActivityId, define.StatusReady, define.StatusNotBegin)
  26. t.CheckErr(err)
  27. // 创建队伍 只容许创建一次
  28. redTeam := &models.BaheTeam{
  29. BaheTeamName: "red",
  30. ActivityId: bahe.ActivityId,
  31. BaheActivityId: bahe.Id,
  32. IsDelete: false,
  33. CreatedAt: time.Now(),
  34. UpdatedAt: time.Now(),
  35. }
  36. err = models.Save(map[string]interface{}{
  37. "bahe_activity_id=": bahe.Id,
  38. "is_delete=": 0,
  39. "bahe_team_name=": define.TUGWAR_TEAM_RED,
  40. }, redTeam)
  41. t.CheckErr(err)
  42. blueTeam := &models.BaheTeam{
  43. BaheTeamName: "blue",
  44. ActivityId: bahe.ActivityId,
  45. BaheActivityId: bahe.Id,
  46. IsDelete: false,
  47. CreatedAt: time.Now(),
  48. UpdatedAt: time.Now(),
  49. }
  50. err = models.Save(map[string]interface{}{
  51. "bahe_activity_id=": bahe.Id,
  52. "is_delete=": 0,
  53. "bahe_team_name=": define.TUGWAR_TEAM_BULE,
  54. }, blueTeam)
  55. t.CheckErr(err)
  56. _, err = bahe.UpdateStatusById(bahe.Id, define.StatusReady)
  57. t.CheckErr(err)
  58. t.SUCCESS("操作成功")
  59. }
  60. func (t *TugOfWarCtl) Start() {
  61. baheId := t.MustGetInt64("bahe_activity_id")
  62. bahe := new(models.TugOfWar)
  63. exist, err := models.Get(bahe, baheId)
  64. t.CheckErr(err)
  65. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  66. if bahe.Status != define.StatusReady {
  67. t.ERROR(fmt.Sprintf("该活动%s", bahe.Status), code.MSG_ERR)
  68. }
  69. _, err = bahe.UpdateStatusById(baheId, define.StatusRunning)
  70. t.CheckErr(err)
  71. t.SUCCESS("操作成功")
  72. }
  73. func (t *TugOfWarCtl) Stop() {
  74. baheId := t.MustGetInt64("bahe_activity_id")
  75. bahe := new(models.TugOfWar)
  76. exist, err := models.Get(bahe, baheId)
  77. t.CheckErr(err)
  78. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  79. if bahe.Status != define.StatusRunning {
  80. t.ERROR(fmt.Sprintf("该活动%s", bahe.Status), code.MSG_ERR)
  81. }
  82. bahe.Status = define.StatusEnding
  83. bahe.UpdatedAt = time.Now()
  84. _, err = bahe.UpdateStatusById(bahe.Id, bahe.Status)
  85. t.CheckErr(err)
  86. t.SUCCESS("操作成功")
  87. }
  88. func (t *TugOfWarCtl) Team() {
  89. baheId := t.MustGetInt64("bahe_activity_id")
  90. customerId := t.MustGetUID()
  91. bahe := new(models.TugOfWar)
  92. exist, err := models.Get(bahe, baheId)
  93. t.CheckErr(err)
  94. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  95. activity := new(models.Activity)
  96. exist, err = models.Get(activity, bahe.ActivityId)
  97. t.CheckErr(err)
  98. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  99. area := new(models.AreaStore)
  100. exist, err = area.GetByCustomerId(customerId, bahe.ActivityId)
  101. t.CheckErr(err)
  102. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区信息异常")
  103. //获取队伍信息: 名字、二维码、颜色
  104. teams := make([]*models.BaheTeam, 0)
  105. total, err := core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", bahe.Id).FindAndCount(&teams)
  106. t.CheckErr(err)
  107. // 队伍颜色
  108. qrcode := ""
  109. // 二维码
  110. if bahe.Model != "随机分配模式" {
  111. // 非随机模式,自选队伍
  112. for index := range teams {
  113. qrcode, err = utils.GenH5Qrcode(define.H5TugOfWar, map[string]interface{}{
  114. "area_id": area.Id,
  115. "activity_id": bahe.ActivityId,
  116. "customer_id": customerId,
  117. "bahe_activity_id": bahe.Id,
  118. "bahe_team_id": teams[index].Id,
  119. "rehearsal_id": activity.RehearsalId,
  120. })
  121. t.CheckErr(err)
  122. teams[index].Qrcode = qrcode
  123. }
  124. } else {
  125. qrcode, err = utils.GenH5Qrcode(define.H5TugOfWar, map[string]interface{}{
  126. "area_id": area.Id,
  127. "activity_id": bahe.ActivityId,
  128. "customer_id": customerId,
  129. "bahe_activity_id": bahe.Id,
  130. "bahe_team_id": 0,
  131. "rehearsal_id": activity.RehearsalId,
  132. })
  133. t.CheckErr(err)
  134. }
  135. t.JSON(map[string]interface{}{
  136. "total": total,
  137. "list": teams,
  138. "qrcode": qrcode,
  139. "model": bahe.Model,
  140. })
  141. }
  142. func (t *TugOfWarCtl) Member() {
  143. baheId := t.MustGetInt64("bahe_activity_id")
  144. rehearsalId := t.MustGetInt64("rehearsal_id")
  145. teams := make([]*models.BaheTeam, 0)
  146. err := core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", baheId).Find(&teams)
  147. t.CheckErr(err)
  148. var total int64 = 0
  149. for index := range teams {
  150. members := make([]*models.BaheTeamMember, 0)
  151. num, err := core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=?",
  152. teams[index].Id, rehearsalId).Desc("score").Asc("sort_time").FindAndCount(&members)
  153. t.CheckErr(err)
  154. total += num
  155. teams[index].Members = members
  156. }
  157. t.JSON(map[string]interface{}{
  158. "total": total,
  159. "list": teams,
  160. })
  161. }
  162. type BaheScoreResult struct {
  163. TotalScore int64 `json:"total_score" description:"总分数"`
  164. Id int64 `json:"id" description:"id"`
  165. ActivityId int64 `json:"activity_id" description:"活动id"`
  166. BaheActivityId int64 `json:"bahe_activity_id" description:"拔河活动id"`
  167. BaheTeamName string `json:"bahe_team_name" description:"拔河队伍得名称"`
  168. Members []*models.BaheTeamMember `json:"members"`
  169. }
  170. func (t *TugOfWarCtl) Score() {
  171. baheActivityId := t.MustGetInt64("bahe_activity_id")
  172. rehearsalId := t.MustGetInt64("rehearsal_id")
  173. teams := make([]*BaheScoreResult, 0)
  174. err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  175. Select("sum(m.score) as total_score, t.id, t.activity_id, t.bahe_activity_id, t.bahe_team_name").
  176. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  177. "t.id=m.team_id and m.is_delete=0 and m.rehearsal_id=?", rehearsalId).
  178. Where("t.is_delete=0 and t.bahe_activity_id=?", baheActivityId).
  179. GroupBy("t.id").Find(&teams)
  180. t.CheckErr(err)
  181. for index := range teams {
  182. members := make([]*models.BaheTeamMember, 0)
  183. err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=?", teams[index].Id, rehearsalId).
  184. Desc("score").Asc("sort_time").Find(&members)
  185. t.CheckErr(err)
  186. teams[index].Members = members
  187. }
  188. t.JSON(map[string]interface{}{
  189. "total": len(teams),
  190. "list": teams,
  191. })
  192. }
  193. func (t *TugOfWarCtl) List() {
  194. activityId := t.MustGetInt64("activity_id")
  195. bahes := make([]*models.TugOfWar, 0)
  196. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
  197. Asc("created_at").Find(&bahes)
  198. t.CheckErr(err)
  199. t.JSON(map[string]interface{}{
  200. "bahe_activities": bahes,
  201. })
  202. }