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

112 lines
3.6 KiB

5 years ago
5 years ago
  1. package bahe_service
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/models"
  5. "hudongzhuanjia/utils/define"
  6. "github.com/ouxuanserver/osmanthuswine/src/core"
  7. )
  8. type CountMembersResult struct {
  9. Number int `json:"number,omitempty"`
  10. TeamId int64 `json:"team_id,omitempty"`
  11. Color string `json:"color,omitempty"`
  12. }
  13. func CountMembers(baheId int64) ([]*CountMembersResult, error) {
  14. result := make([]*CountMembersResult, 0)
  15. err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  16. Select("count(m.id) as number, t.id as team_id, t.bahe_team_name as color").
  17. Join("left", "ox_bahe_team_member as m", "m.team_id=t.id and m.is_delete=0").
  18. Where("t.is_delete=0 and t.bahe_activity_id = ?", baheId).Desc("color").
  19. GroupBy("t.id").Find(&result)
  20. return result, err
  21. }
  22. func GetMember(bid, rid, uid int64) (*models.BaheTeamMember, bool, error) {
  23. member := new(models.BaheTeamMember)
  24. exist, err := core.GetXormAuto().Where("is_delete=0 and rehearsal_id=? and "+
  25. "bahe_activity_id=? and member_id=?", rid, bid, uid).Get(member)
  26. return member, exist, err
  27. }
  28. func GetCurrentTugWar(aid, uid, rid int64) (map[string]interface{}, error) {
  29. bahe := new(models.TugOfWar)
  30. exist, err := bahe.GetCurrent(aid)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if !exist {
  35. return nil, fmt.Errorf("轮次尚未开启")
  36. }
  37. member := new(models.BaheTeamMember)
  38. exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, rid)
  39. if err != nil {
  40. return nil, err
  41. }
  42. if exist {
  43. return map[string]interface{}{
  44. "member": member,
  45. }, nil
  46. }
  47. if bahe.Model == define.TUGWAR_MODEL_RANDOM {
  48. return map[string]interface{}{
  49. "model": bahe.Model,
  50. "bahe_activity_id": bahe.Id,
  51. }, nil
  52. }
  53. res, err := CountMembers(bahe.Id)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return map[string]interface{}{
  58. "list": res,
  59. "model": bahe.Model,
  60. "bahe_activity_id": bahe.Id,
  61. "limit": bahe.Number,
  62. }, nil
  63. }
  64. type JoinTeamResult struct {
  65. Number int
  66. TeamId int64
  67. TeamName string
  68. }
  69. func GetJoinTeamByTeamId(teamId, rehearsalId interface{}) (*JoinTeamResult, bool, error) {
  70. result := new(JoinTeamResult)
  71. exist, err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  72. Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
  73. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  74. "m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", rehearsalId).
  75. Where("t.is_delete=0 and t.id=?", teamId).GroupBy("t.id").Get(result)
  76. return result, exist, err
  77. }
  78. func GetJoinTeamByBaheId(baheId, rehearsalId interface{}) (*JoinTeamResult, bool, error) {
  79. result := new(JoinTeamResult)
  80. exist, err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  81. Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
  82. Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  83. "m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", rehearsalId).
  84. Where("t.is_delete=0 and t.bahe_activity_id=? ", baheId).
  85. GroupBy("t.id").Asc("number").Get(result)
  86. return result, exist, err
  87. }
  88. //
  89. //team := new(JoinTeamResult)
  90. //session := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
  91. // Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
  92. // Join("LEFT", new(models.BaheTeamMember).Alias("m"),
  93. // "m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", activity.RehearsalId)
  94. //if teamId == 0 { // 人数最少的一队
  95. // session = session.Where("t.is_delete=0 and t.bahe_activity_id=? ", bahe.Id).
  96. // GroupBy("t.id").Asc("number")
  97. //} else {
  98. // session = session.Where("t.is_delete=0 and t.id=?", teamId).GroupBy("t.id")
  99. //}
  100. //exist, err = session.Get(team)