package pc import ( "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" activity_service "hudongzhuanjia/services/activity" invitation_service "hudongzhuanjia/services/invitation" lottery_service "hudongzhuanjia/services/lottery" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" ) // 订单 type OrderDrawCtl struct { controllers.AuthorCtl } // 开启订单活动 func (t *OrderDrawCtl) Switch() { activityId := t.MustGetInt64("activity_id") status := t.MustGetInt("status") activity := new(models.Activity) exist, err := models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") option := new(models.CustomerOrderOption) exist, err = option.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "订单活动不存在") if option.Status == status { t.SUCCESS("操作成功") return } _, err = option.Switch(activityId, status) t.CheckErr(err) t.CheckErr(err) t.SUCCESS("操作成功") } // ===================== 订单抽奖 // 开始抽奖 func (t *OrderDrawCtl) Start() { ladderId := t.MustGetInt64("order_draw_ladder_id") ladder := new(models.OrderDrawRuleLadder) exist, err := models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") if ladder.Status != define.StatusNotBegin { t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR) } ladder.Status = define.StatusRunning ladder.UpdatedAt = time.Now() err = models.Save(map[string]interface{}{ "id=": ladder.Id, "is_delete=": 0, }, ladder, "status", "updated_at") t.CheckErr(err) t.SUCCESS("操作成功") } // 开始抽奖 func (t *OrderDrawCtl) Stop() { ladderId := t.MustGetInt64("order_draw_ladder_id") ladder := new(models.OrderDrawRuleLadder) exist, err := models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") if ladder.Status != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR) } ladder.Status = define.StatusEnding ladder.UpdatedAt = time.Now() err = models.Save(map[string]interface{}{ "id=": ladder.Id, "is_delete=": 0, }, ladder, "status", "updated_at") t.CheckErr(err) t.SUCCESS("操作成功") } type OrderListResult struct { OrderDrawActivityId int64 `json:"order_draw_activity_id"` OrderDrawRuleId int64 `json:"order_draw_rule_id"` OrderDrawActivityName string `json:"order_draw_activity_name"` OrderDrawLadders []*OrderLadderResult `json:"order_draw_ladders"` PrizeNumber int64 `json:"prize_number"` } type OrderLadderResult struct { OrderDrawRuleId int64 `json:"order_draw_rule_id"` OrderDrawLadderId int64 `json:"order_draw_ladder_id"` PrizeName string `json:"prize_name"` Status string `json:"status"` PrizeImg string `json:"prize_img"` PrizeNumber int64 `json:"prize_number"` } //获取所有订单奖品 func (t *OrderDrawCtl) List() { activityId := t.MustGetInt64("activity_id") customerId := t.MustGetUID() customer := new(models.Customer) exist, err := models.Get(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") activity := new(models.Activity) exist, err = models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") // 订单的开启或关闭 order := new(models.CustomerOrderOption) exist, err = order.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单活动不存在") result := make([]*OrderListResult, 0) core.GetXormAuto().Table(new(models.OrderDrawActivity)).Alias("a"). Select("a.id as order_draw_activity_id, r.id as order_draw_rule_id, a.order_draw_activity_name"). Join("LEFT", new(models.OrderDrawRule).Alias("r"), "a.id=r.order_draw_activity_id and r.is_delete=0"). Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result) // 多地区的一个坑:分配给主会场的area_id不是主账号的area_id area := new(models.AreaStore) if customer.Pid == 0 { exist, err = area.GetMainAreaById(activityId) } else { exist, err = area.GetAreaStoreById(t.MustGetAreaId()) } t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") ruleIds := make([]int64, 0) for _, v := range result { ruleIds = append(ruleIds, v.OrderDrawRuleId) } ladders := make([]*OrderLadderResult, 0) err = core.GetXormAuto().Table(new(models.OrderDrawRuleLadder)).Alias("l"). Select("id as order_draw_ladder_id, status, prize_name, prize_img, prize_number, order_draw_rule_id"). Where("is_delete=0").In("order_draw_rule_id", ruleIds).Find(&ladders) t.CheckErr(err) ladderIds := make([]int64, 0) for _, ladder := range ladders { ladderIds = append(ladderIds, ladder.OrderDrawLadderId) } records := make([]map[string]int64, 0) err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Alias("r"). Select("r.order_draw_rule_ladder_id as ladder_id, count(id) as num"). Where("is_delete=0 and rehearsal_id=?", activity.RehearsalId). In("r.order_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records) t.CheckErr(err) for i := range ladders { for j := range records { if ladders[i].OrderDrawLadderId == records[j]["ladder_id"] { ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"] } } } for i := range result { for j := range ladders { if result[i].OrderDrawRuleId == ladders[j].OrderDrawRuleId { result[i].PrizeNumber += ladders[j].PrizeNumber result[i].OrderDrawLadders = append(result[i].OrderDrawLadders, ladders[j]) } } } t.JSON(map[string]interface{}{ "total": len(result), "list": result, "status": order.Status, }) } //抽奖奖品 func (t *OrderDrawCtl) Prize() { ruleId := t.MustGetInt64("order_draw_rule_id") list := make([]*models.OrderDrawRuleLadder, 0) err := core.GetXormAuto().Where("is_delete=0 and order_draw_rule_id=?", ruleId).Find(&list) t.CheckErr(err) for index := range list { list[index].Des = "在该活动的所有订单用户中随机抽奖品数量的用户" } t.JSON(map[string]interface{}{ "total": len(list), "lise": list, }) } type OrderUsersResult struct { UserId int64 `json:"user_id"` Username string `json:"username"` Avatar string `json:"avatar"` } //统计人数和订单数量 func (t *OrderDrawCtl) Users() { activityId := t.MustGetInt64("activity_id") ruleId := t.MustGetInt64("order_draw_rule_id") activity := new(models.Activity) exist, err := models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") result := make([]*OrderUsersResult, 0) session := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o"). Select("o.buyer_id as user_id, u.nickname as username, u.avatar").Distinct("o.buyer_id"). Join("LEFT", new(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=?", activityId, activity.RehearsalId) 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([]int64, 0) err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id"). Where("order_draw_rule_id=? and rehearsal_id=? and is_delete=0", ruleId, activity.RehearsalId).Find(&recordIds) t.CheckErr(err) session = session.NotIn("o.buyer_id", recordIds) } err = session.Find(&result) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } // 订单抽奖动作 func (t *OrderDrawCtl) Draw() { activityId := t.MustGetInt64("activity_id") ruleId := t.MustGetInt64("order_draw_rule_id") ladderId := t.MustGetInt64("order_draw_rule_ladder_id") number := t.MustGetInt("number") customerId := t.MustGetUID() customer := new(models.Customer) exist, err := models.Get(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") activity := new(models.Activity) exist, err = models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) area := new(models.AreaStore) exist, err = area.GetByCustomerId(customerId, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在") rule := new(models.OrderDrawRule) exist, err = models.Get(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") // 查询奖品 ladder := new(models.OrderDrawRuleLadder) exist, err = models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_LADDER_NOT_EXIST, "订单抽奖等级不存在") t.CheckRunning(ladder.Status) count, err := core.GetXormAuto().Where("order_draw_rule_id=? and order_draw_rule_ladder_id=? "+ "and rehearsal_id=? and is_delete=0", ruleId, ladderId, activity.RehearsalId).Count(new(models.OrderDrawRecord)) t.CheckErr(err) prizeNum := ladder.PrizeNumber - int(count) if prizeNum <= 0 || prizeNum < number { t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH) } module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在") userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id) 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 // 订单抽奖 ==> 存入我的奖品 userPrize := new(models.UserPrize) userPrize.ActivityId = activityId userPrize.RehearsalId = activity.RehearsalId userPrize.ActivityName = activity.Name userPrize.UserId = winners[i].UserId userPrize.PrizeImg = ladder.PrizeImg userPrize.PrizeName = ladder.PrizeName userPrize.PrizeType = 2 userPrize.IsDelete = false userPrize.CreatedAt = time.Now() userPrize.UpdatedAt = time.Now() _, err = core.GetXormAuto().Insert(userPrize) t.CheckErr(err) record := new(models.OrderDrawRecord) record.UserPrizeId = userPrize.Id record.ActivityId = activityId record.RehearsalId = activity.RehearsalId 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 record.IsDelete = false record.CreatedAt = time.Now() record.UpdatedAt = time.Now() _, err = core.GetXormAuto().InsertOne(record) t.CheckErr(err) } t.JSON(winners) } type WinnerResult struct { UserId int64 `json:"user_id"` Username string `json:"user_name"` UserPhone string `json:"user_phone"` Avatar string `json:"avatar"` PrizeName string `json:"prize_name"` } func (t *OrderDrawCtl) ListOfWinners() { orderDrawRuleId := t.MustGetInt64("order_draw_rule_id") rehearsalId := t.MustGetInt64("rehearsal_id") result, err := lottery_service.GetOrderWinnersResult(orderDrawRuleId, rehearsalId) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } // 获取所有订单 type OrdersResult struct { UserId int64 `json:"user_id"` AreaName string `json:"area_name"` GoodsName string `json:"goods_name"` Nickname string `json:"nickname"` ExtraData string `json:"-"` Extra []map[string]interface{} `json:"extra"` } func (t *OrderDrawCtl) Orders() { activityId := t.MustGetInt64("activity_id") rehearsalId := t.MustGetInt64("rehearsal_id") orders := make([]*OrdersResult, 0) err := core.GetXormAuto().Table(new(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", new(models.InvitationLetter).Alias("l"), "o.buyer_id=l.user_id and o.activity_id=l.activity_id and l.is_delete=0"). Join("LEFT", new(models.User).Alias("u"), "u.id=o.buyer_id and u.is_delete=0"). Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=?", activityId, rehearsalId). 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 } // 下订单人数 buyerCount, err := core.GetXormAuto().Where("is_delete=0 and activity_id=? and rehearsal_id=?", activityId, rehearsalId).Distinct("buyer_id").Count(new(models.CustomerOrder)) t.CheckErr(err) t.JSON(map[string]interface{}{ "orders": orders, "total": len(orders), "buyer_count": buyerCount, }) }