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

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