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

253 lines
7.4 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
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. bahe := models.TugOfWar{}
  145. exist, err := models.Get(bahe, baheId)
  146. t.CheckErr(err)
  147. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  148. activity := new(models.Activity)
  149. exist, err = models.Get(activity, bahe.ActivityId)
  150. t.CheckErr(err)
  151. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  152. teams := make([]*models.BaheTeam, 0)
  153. err = core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", baheId).Find(&teams)
  154. t.CheckErr(err)
  155. var total int64 = 0
  156. for index := range teams {
  157. members := make([]*models.BaheTeamMember, 0)
  158. num, err := core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=? and arch_id=?",
  159. teams[index].Id, activity.RehearsalId, activity.ArchId).Desc("score").
  160. Asc("sort_time").FindAndCount(&members)
  161. t.CheckErr(err)
  162. total += num
  163. teams[index].Members = members
  164. }
  165. t.JSON(map[string]interface{}{
  166. "total": total,
  167. "list": teams,
  168. })
  169. }
  170. type BaheScoreResult struct {
  171. TotalScore int64 `json:"total_score" description:"总分数"`
  172. Id int64 `json:"id" description:"id"`
  173. ActivityId int64 `json:"activity_id" description:"活动id"`
  174. BaheActivityId int64 `json:"bahe_activity_id" description:"拔河活动id"`
  175. BaheTeamName string `json:"bahe_team_name" description:"拔河队伍得名称"`
  176. Members []*models.BaheTeamMember `json:"members"`
  177. }
  178. func (t *TugOfWarCtl) Score() {
  179. baheId := t.MustGetInt64("bahe_activity_id")
  180. bahe := models.TugOfWar{}
  181. exist, err := models.Get(bahe, baheId)
  182. t.CheckErr(err)
  183. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  184. activity := new(models.Activity)
  185. exist, err = models.Get(activity, bahe.ActivityId)
  186. t.CheckErr(err)
  187. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  188. teams := make([]*BaheScoreResult, 0)
  189. err = core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  190. Select("sum(m.score) as total_score, t.id, t.activity_id, t.bahe_activity_id, t.bahe_team_name").
  191. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  192. "t.id=m.team_id and m.is_delete=0 and m.rehearsal_id=? and arch_id=?",
  193. activity.RehearsalId, activity.ArchId).Where("t.is_delete=0 and t.bahe_activity_id=?", baheId).
  194. GroupBy("t.id").Find(&teams)
  195. t.CheckErr(err)
  196. for index := range teams {
  197. members := make([]*models.BaheTeamMember, 0)
  198. err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=? and arch_id=?",
  199. teams[index].Id, activity.RehearsalId, activity.ArchId).Desc("score").
  200. Asc("sort_time").Find(&members)
  201. t.CheckErr(err)
  202. teams[index].Members = members
  203. }
  204. t.JSON(map[string]interface{}{
  205. "total": len(teams),
  206. "list": teams,
  207. })
  208. }
  209. func (t *TugOfWarCtl) List() {
  210. activityId := t.MustGetInt64("activity_id")
  211. bahes := make([]*models.TugOfWar, 0)
  212. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
  213. Asc("created_at").Find(&bahes)
  214. t.CheckErr(err)
  215. t.JSON(map[string]interface{}{
  216. "bahe_activities": bahes,
  217. })
  218. }