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

266 lines
7.7 KiB

5 years ago
4 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
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. "git.ouxuan.net/tommy/osmanthuswine/src/core"
  11. )
  12. type TugOfWarCtl struct {
  13. controllers.AuthorCtl
  14. }
  15. func (t *TugOfWarCtl) Ready() { // 准备中
  16. baheId := t.MustGetInt("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.MustGetInt("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.MustGetInt("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.MustGetInt("bahe_activity_id")
  90. customerId := t.GetAccountId()
  91. customer := new(models.Customer)
  92. exist, err := models.Get(customer, customerId)
  93. t.CheckErr(err)
  94. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  95. bahe := new(models.TugOfWar)
  96. exist, err = models.Get(bahe, baheId)
  97. t.CheckErr(err)
  98. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  99. if bahe.Status == define.StatusNotBegin {
  100. t.ERROR("拔河尚未开始", code.MSG_TUGWAR_NOT_EXIST)
  101. return
  102. }
  103. activity := new(models.Activity)
  104. exist, err = models.Get(activity, bahe.ActivityId)
  105. t.CheckErr(err)
  106. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  107. area := new(models.AreaStore)
  108. exist, err = area.GetByCustomerId(customerId, bahe.ActivityId)
  109. t.CheckErr(err)
  110. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区信息异常")
  111. //获取队伍信息: 名字、二维码、颜色
  112. teams := make([]*models.BaheTeam, 0)
  113. total, err := core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", bahe.Id).FindAndCount(&teams)
  114. t.CheckErr(err)
  115. // 队伍颜色
  116. qrcode := ""
  117. // 二维码
  118. link := define.H5TugOfWar
  119. if customer.IsSpecial == 1 {
  120. link = define.H5TugOfWar2020
  121. }
  122. if bahe.Model != "随机分配模式" {
  123. // 非随机模式,自选队伍
  124. for index := range teams {
  125. qrcode, err = utils.GenH5Qrcode(link, 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": teams[index].Id,
  131. "rehearsal_id": activity.RehearsalId,
  132. })
  133. t.CheckErr(err)
  134. teams[index].Qrcode = qrcode
  135. }
  136. } else {
  137. qrcode, err = utils.GenH5Qrcode(link, map[string]interface{}{
  138. "area_id": area.Id,
  139. "activity_id": bahe.ActivityId,
  140. "customer_id": customerId,
  141. "bahe_activity_id": bahe.Id,
  142. "bahe_team_id": 0,
  143. "rehearsal_id": activity.RehearsalId,
  144. })
  145. t.CheckErr(err)
  146. }
  147. t.JSON(map[string]interface{}{
  148. "total": total,
  149. "list": teams,
  150. "qrcode": qrcode,
  151. "model": bahe.Model,
  152. })
  153. }
  154. func (t *TugOfWarCtl) Member() {
  155. baheId := t.MustGetInt("bahe_activity_id")
  156. bahe := &models.TugOfWar{}
  157. exist, err := models.Get(bahe, baheId)
  158. t.CheckErr(err)
  159. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  160. activity := new(models.Activity)
  161. exist, err = models.Get(activity, bahe.ActivityId)
  162. t.CheckErr(err)
  163. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  164. teams := make([]*models.BaheTeam, 0)
  165. err = core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", baheId).Find(&teams)
  166. t.CheckErr(err)
  167. var total int64 = 0
  168. for index := range teams {
  169. members := make([]*models.BaheTeamMember, 0)
  170. num, err := core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=? and arch_id=?",
  171. teams[index].Id, activity.RehearsalId, activity.ArchId).Desc("score").
  172. Asc("sort_time").FindAndCount(&members)
  173. t.CheckErr(err)
  174. total += num
  175. teams[index].Members = members
  176. }
  177. t.JSON(map[string]interface{}{
  178. "total": total,
  179. "list": teams,
  180. })
  181. }
  182. type BaheScoreResult struct {
  183. TotalScore int `json:"total_score" description:"总分数"`
  184. Id int `json:"id" description:"id"`
  185. ActivityId int `json:"activity_id" description:"活动id"`
  186. BaheActivityId int `json:"bahe_activity_id" description:"拔河活动id"`
  187. BaheTeamName string `json:"bahe_team_name" description:"拔河队伍得名称"`
  188. Members []*models.BaheTeamMember `json:"members"`
  189. }
  190. func (t *TugOfWarCtl) Score() {
  191. baheId := t.MustGetInt("bahe_activity_id")
  192. bahe := &models.TugOfWar{}
  193. exist, err := models.Get(bahe, baheId)
  194. t.CheckErr(err)
  195. t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在")
  196. activity := new(models.Activity)
  197. exist, err = models.Get(activity, bahe.ActivityId)
  198. t.CheckErr(err)
  199. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  200. teams := make([]*BaheScoreResult, 0)
  201. err = core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  202. Select("sum(m.score) as total_score, t.id, t.activity_id, t.bahe_activity_id, t.bahe_team_name").
  203. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  204. "t.id=m.team_id and m.is_delete=0 and m.rehearsal_id=? and m.arch_id=?",
  205. activity.RehearsalId, activity.ArchId).Where("t.is_delete=0 and t.bahe_activity_id=?", baheId).
  206. GroupBy("t.id").Find(&teams)
  207. t.CheckErr(err)
  208. for index := range teams {
  209. members := make([]*models.BaheTeamMember, 0)
  210. err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=? and arch_id=?",
  211. teams[index].Id, activity.RehearsalId, activity.ArchId).Desc("score").
  212. Asc("sort_time").Find(&members)
  213. t.CheckErr(err)
  214. teams[index].Members = members
  215. }
  216. t.JSON(map[string]interface{}{
  217. "total": len(teams),
  218. "list": teams,
  219. })
  220. }
  221. func (t *TugOfWarCtl) List() {
  222. activityId := t.MustGetInt("activity_id")
  223. bahes := make([]*models.TugOfWar, 0)
  224. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
  225. Asc("created_at").Find(&bahes)
  226. t.CheckErr(err)
  227. t.JSON(map[string]interface{}{
  228. "bahe_activities": bahes,
  229. })
  230. }