diff --git a/controllers/pc/order_draw.go b/controllers/pc/order_draw.go index 8b28287..d085611 100644 --- a/controllers/pc/order_draw.go +++ b/controllers/pc/order_draw.go @@ -213,9 +213,11 @@ func (t *OrderDrawCtl) Prize() { } type OrderUsersResult struct { - UserId int `json:"user_id"` - Username string `json:"username"` - Avatar string `json:"avatar"` + UserId int `json:"user_id"` + Username string `json:"username"` + Avatar string `json:"avatar"` + Phone string `json:"phone"` + EntryPersonName string `json:"entry_person_name"` } //统计人数和订单数量 @@ -228,25 +230,50 @@ func (t *OrderDrawCtl) Users() { t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") - result := make([]*OrderUsersResult, 0) - session := core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o"). - Select("o.buyer_id as user_id, u.nickname as username, u.avatar").Distinct("o.buyer_id"). - Join("LEFT", (&models.User{}).Alias("u"), "o.buyer_id=u.id and u.is_delete=0"). - Where("o.activity_id=? and o.is_delete=0 and o.rehearsal_id=? and o.arch_id=?", activityId, - activity.RehearsalId, activity.ArchId) + customer := models.Customer{} + exist, err = models.Get(&customer, activity.CustomerId) + t.CheckErr(err) + t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") moduleService, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在") - if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { - recordIds := make([]int, 0) - err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("user_id"). - Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0", - ruleId, activity.RehearsalId, activity.ArchId).Find(&recordIds) - t.CheckErr(err) - session = session.NotIn("o.buyer_id", recordIds) + + result := make([]*OrderUsersResult, 0) + s := core.GetXormAuto().NewSession() + defer s.Close() + if customer.IsSpecial == 2 { + s = s.Table(&models.CustomerOrder{}).Alias("o").Select("o.buyer_id as user_id, "+ + " o.phone, o.order_entry_person_name as entry_person_name, u.nickname as username, u.avatar"). + Distinct("o.buyer_id").Join("LEFT", (&models.User{}).Alias("u"), + "o.buyer_id=u.id and u.is_delete=0").Where("o.activity_id=? and o.is_delete=0 "+ + " and o.rehearsal_id=? and o.arch_id=?", activityId, activity.RehearsalId, activity.ArchId) + + if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { + recordIds := make([]int, 0) + err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("customer_order_id"). + Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0", + ruleId, activity.RehearsalId, activity.ArchId).Find(&recordIds) + t.CheckErr(err) + s = s.NotIn("o.id", recordIds) + } + } else { + s = s.Table(&models.CustomerOrder{}).Alias("o").Select("o.buyer_id as user_id, "+ + " o.phone, o.order_entry_person_name as entry_person_name, u.nickname as username, u.avatar"). + Distinct("o.buyer_id").Join("LEFT", (&models.User{}).Alias("u"), + "o.buyer_id=u.id and u.is_delete=0").Where("o.activity_id=? and o.is_delete=0 "+ + " and o.rehearsal_id=? and o.arch_id=?", activityId, activity.RehearsalId, activity.ArchId) + if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { + recordIds := make([]int, 0) + err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("user_id"). + Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0", + ruleId, activity.RehearsalId, activity.ArchId).Find(&recordIds) + t.CheckErr(err) + s = s.NotIn("o.buyer_id", recordIds) + } } - err = session.Find(&result) + + err = s.Find(&result) t.CheckErr(err) t.JSON(map[string]interface{}{ @@ -300,50 +327,7 @@ func (t *OrderDrawCtl) Draw() { module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在") - if customer.IsSpecial == 0 { - userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId) - if len(userIds) < number { - t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH) - } - - lottery_service.RandLotteryUserIds(userIds) - winnerIds := userIds[:number] - - winners, err := lottery_service.GetLotteryUsers(winnerIds) - t.CheckErr(err) - for i := range winners { - winners[i].LadderId = ladder.Id - winners[i].PrizeImg = ladder.PrizeImg - winners[i].PrizeName = ladder.PrizeName - prize := models.UserPrize{} - prize.ActivityId = activityId - prize.RehearsalId = activity.RehearsalId - prize.ActivityName = activity.Name - prize.UserId = winners[i].UserId - prize.PrizeImg = ladder.PrizeImg - prize.PrizeName = ladder.PrizeName - prize.ArchId = activity.ArchId - prize.PrizeType = 2 - _, err = models.Add(&prize) - t.CheckErr(err) - - record := models.OrderDrawRecord{} - record.UserPrizeId = prize.Id - record.ActivityId = activityId - record.RehearsalId = activity.RehearsalId - record.ArchId = activity.ArchId - record.OrderDrawActivityId = rule.OrderDrawActivityId - record.OrderDrawRuleId = rule.Id - record.UserId = winners[i].UserId - record.OrderDrawRuleLadderId = ladder.Id - record.PrizeName = ladder.PrizeName - record.AreaId = area.Id - record.AreaName = area.Name - _, err = models.Add(&record) - t.CheckErr(err) - } - t.JSON(winners) - } else if customer.IsSpecial == 2 { + if customer.IsSpecial == 2 { orders, err := lottery_service.GetSpecialOrderLottery(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId) if len(orders) < number { t.ERROR("抽奖订单数量不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH) @@ -406,8 +390,50 @@ func (t *OrderDrawCtl) Draw() { t.CheckErr(err) } t.JSON(winners) + } else { + userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id, activity.ArchId) + if len(userIds) < number { + t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH) + } + + lottery_service.RandLotteryUserIds(userIds) + winnerIds := userIds[:number] + + winners, err := lottery_service.GetLotteryUsers(winnerIds) + t.CheckErr(err) + for i := range winners { + winners[i].LadderId = ladder.Id + winners[i].PrizeImg = ladder.PrizeImg + winners[i].PrizeName = ladder.PrizeName + prize := models.UserPrize{} + prize.ActivityId = activityId + prize.RehearsalId = activity.RehearsalId + prize.ActivityName = activity.Name + prize.UserId = winners[i].UserId + prize.PrizeImg = ladder.PrizeImg + prize.PrizeName = ladder.PrizeName + prize.ArchId = activity.ArchId + prize.PrizeType = 2 + _, err = models.Add(&prize) + t.CheckErr(err) + + record := models.OrderDrawRecord{} + record.UserPrizeId = prize.Id + record.ActivityId = activityId + record.RehearsalId = activity.RehearsalId + record.ArchId = activity.ArchId + record.OrderDrawActivityId = rule.OrderDrawActivityId + record.OrderDrawRuleId = rule.Id + record.UserId = winners[i].UserId + record.OrderDrawRuleLadderId = ladder.Id + record.PrizeName = ladder.PrizeName + record.AreaId = area.Id + record.AreaName = area.Name + _, err = models.Add(&record) + t.CheckErr(err) + } + t.JSON(winners) } - t.ERROR("客户信息异常", code.MSG_CUSTOMER_NOT_EXIST) } type WinnerResult struct { @@ -471,33 +497,7 @@ func (t *OrderDrawCtl) Orders() { // 下订单人数 buyerCount, err := (&models.CustomerOrder{}).CountCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId) t.CheckErr(err) - - if customer.IsSpecial == 0 { - orders := make([]*OrdersResult, 0) - err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o"). - Select("o.area_name, o.goods_name, o.buyer_id as user_id, u.nickname, l.extra_data as extra_data, o.created_at"). - Distinct("o.id").Join("LEFT", (&models.InvitationLetter{}).Alias("l"), - "o.buyer_id=l.user_id and o.activity_id=l.activity_id and l.arch_id=o.arch_id"). - Join("LEFT", (&models.User{}).Alias("u"), "u.id=o.buyer_id"). - Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?", - activityId, activity.RehearsalId, activity.ArchId). - Asc("o.created_at").Find(&orders) - t.CheckErr(err) - - items, err := invitation_service.GetOptionItem(activityId) - t.CheckErr(err) - for i := range orders { - data, err := invitation_service.GetOptionValue(items, orders[i].ExtraData) - t.CheckErr(err) - orders[i].Extra = data - } - t.JSON(map[string]interface{}{ - "orders": orders, - "total": len(orders), - "buyer_count": buyerCount, - }) - return - } else if customer.IsSpecial == 2 { + if customer.IsSpecial == 2 { orders := make([]*SpecialOrdersResult, 0) err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o").Select("o.id as id, "+ " o.area_name as area_name, o.order_entry_person_name as entry_person_name, o.buyer_id as user_id, "+ @@ -525,8 +525,32 @@ func (t *OrderDrawCtl) Orders() { "buyer_count": buyerCount, }) return + } else { + orders := make([]*OrdersResult, 0) + err = core.GetXormAuto().Table(&models.CustomerOrder{}).Alias("o"). + Select("o.area_name, o.goods_name, o.buyer_id as user_id, u.nickname, l.extra_data as extra_data, o.created_at"). + Distinct("o.id").Join("LEFT", (&models.InvitationLetter{}).Alias("l"), + "o.buyer_id=l.user_id and o.activity_id=l.activity_id and l.arch_id=o.arch_id"). + Join("LEFT", (&models.User{}).Alias("u"), "u.id=o.buyer_id"). + Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?", + activityId, activity.RehearsalId, activity.ArchId). + Asc("o.created_at").Find(&orders) + t.CheckErr(err) + + items, err := invitation_service.GetOptionItem(activityId) + t.CheckErr(err) + for i := range orders { + data, err := invitation_service.GetOptionValue(items, orders[i].ExtraData) + t.CheckErr(err) + orders[i].Extra = data + } + t.JSON(map[string]interface{}{ + "orders": orders, + "total": len(orders), + "buyer_count": buyerCount, + }) + return } - t.ERROR("客户信息异常", code.MSG_CUSTOMER_NOT_EXIST) } // 录入人员排行榜 diff --git a/services/lottery/order.go b/services/lottery/order.go index ee5441f..2b96b53 100644 --- a/services/lottery/order.go +++ b/services/lottery/order.go @@ -17,7 +17,6 @@ func GetOrderLotteryUserIds(repeat string, activityId, ruleId, ladderId, rehears Where("order_draw_rule_id=? and rehearsal_id=? and arch_id=? and is_delete=0", ruleId, rehearsalId, archId).Find(&recordIds) } else { - // 不去除 err = core.GetXormAuto().Table(&models.OrderDrawRecord{}).Select("user_id"). Where("order_draw_rule_ladder_id=? and rehearsal_id=? and arch_id=? and is_delete=0", ladderId, rehearsalId, archId).Find(&recordIds)