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

221 lines
6.4 KiB

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.GetById(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.GetById(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.GetById(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. rehearsalId := t.MustGetInt64("rehearsal_id")
  91. customerId := t.MustGetUID()
  92. bahe := new(models.TugOfWar)
  93. exist, err := models.GetById(bahe, baheId)
  94. t.CheckErr(err)
  95. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  96. //获取队伍信息: 名字、二维码、颜色
  97. teams := make([]*models.BaheTeam, 0)
  98. total, err := core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", bahe.Id).FindAndCount(&teams)
  99. t.CheckErr(err)
  100. // 队伍颜色
  101. qrcode := ""
  102. // 二维码
  103. if bahe.Model != "随机分配模式" {
  104. // 非随机模式,自选队伍
  105. for index := range teams {
  106. qrcode, err = utils.GenH5Qrcode(define.H5TugOfWar, map[string]interface{}{
  107. "activity_id": bahe.ActivityId,
  108. "customer_id": customerId,
  109. "bahe_activity_id": bahe.Id,
  110. "bahe_team_id": teams[index].Id,
  111. "rehearsal_id": rehearsalId,
  112. })
  113. t.CheckErr(err)
  114. teams[index].Qrcode = qrcode
  115. }
  116. } else {
  117. qrcode, err = utils.GenH5Qrcode(define.H5TugOfWar, map[string]interface{}{
  118. "activity_id": bahe.ActivityId,
  119. "customer_id": customerId,
  120. "bahe_activity_id": bahe.Id,
  121. "bahe_team_id": 0,
  122. "rehearsal_id": rehearsalId,
  123. })
  124. t.CheckErr(err)
  125. }
  126. t.JSON(map[string]interface{}{
  127. "total": total,
  128. "list": teams,
  129. "qrcode": qrcode,
  130. "model": bahe.Model,
  131. })
  132. }
  133. func (t *TugOfWarCtl) Member() {
  134. baheId := t.MustGetInt64("bahe_activity_id")
  135. rehearsalId := t.MustGetInt64("rehearsal_id")
  136. teams := make([]*models.BaheTeam, 0)
  137. err := core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", baheId).Find(&teams)
  138. t.CheckErr(err)
  139. var total int64 = 0
  140. for index := range teams {
  141. members := make([]*models.BaheTeamMember, 0)
  142. num, err := core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=?",
  143. teams[index].Id, rehearsalId).Desc("score").Asc("sort_time").FindAndCount(&members)
  144. t.CheckErr(err)
  145. total += num
  146. teams[index].Members = members
  147. }
  148. t.JSON(map[string]interface{}{
  149. "total": total,
  150. "list": teams,
  151. })
  152. }
  153. type BaheScoreResult struct {
  154. TotalScore int64 `json:"total_score" description:"总分数"`
  155. Id int64 `json:"id" description:"id"`
  156. ActivityId int64 `json:"activity_id" description:"活动id"`
  157. BaheActivityId int64 `json:"bahe_activity_id" description:"拔河活动id"`
  158. BaheTeamName string `json:"bahe_team_name" description:"拔河队伍得名称"`
  159. Members []*models.BaheTeamMember `json:"members"`
  160. }
  161. func (t *TugOfWarCtl) Score() {
  162. baheActivityId := t.MustGetInt64("bahe_activity_id")
  163. rehearsalId := t.MustGetInt64("rehearsal_id")
  164. teams := make([]*BaheScoreResult, 0)
  165. err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  166. Select("sum(m.score) as total_score, t.id, t.activity_id, t.bahe_activity_id, t.bahe_team_name").
  167. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  168. "t.id=m.team_id and m.is_delete=0 and m.rehearsal_id=?", rehearsalId).
  169. Where("t.is_delete=0 and t.bahe_activity_id=?", baheActivityId).
  170. GroupBy("t.id").Find(&teams)
  171. t.CheckErr(err)
  172. for index := range teams {
  173. members := make([]*models.BaheTeamMember, 0)
  174. err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=?", teams[index].Id, rehearsalId).
  175. Desc("score").Asc("sort_time").Find(&members)
  176. t.CheckErr(err)
  177. teams[index].Members = members
  178. }
  179. t.JSON(map[string]interface{}{
  180. "total": len(teams),
  181. "list": teams,
  182. })
  183. }
  184. func (t *TugOfWarCtl) List() {
  185. activityId := t.MustGetInt64("activity_id")
  186. bahes := make([]*models.TugOfWar, 0)
  187. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
  188. Asc("created_at").Find(&bahes)
  189. t.CheckErr(err)
  190. t.JSON(map[string]interface{}{
  191. "bahe_activities": bahes,
  192. })
  193. }