package pc import ( "fmt" "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" "github.com/ouxuanserver/osmanthuswine/src/core" ) // 订单 type OrderDrawCtl struct { controllers.AuthorCtl } // 屏蔽 func (t *OrderDrawCtl) Block() { activityId := t.MustGetInt("activity_id") status := t.MustGet("status") module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块不存在") module.BesideRepeat = status _, err = models.Update(module.Id, module, "beside_repeat") t.CheckErr(err) t.SUCCESS("操作成功") } // 开启订单活动 func (t *OrderDrawCtl) Switch() { activityId := t.MustGetInt("activity_id") status := t.MustGetInt("status") activity := models.Activity{} exist, err := models.Get(&activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") option := &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 } option.Status = status _, err = models.Update(option.Id, option, "status") t.CheckErr(err) t.SUCCESS("操作成功") } // ===================== 订单抽奖 // 开始抽奖 func (t *OrderDrawCtl) Start() { ladderId := t.MustGetInt("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.MustGetInt("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 OrderLadderResult struct { OrderDrawRuleId int `json:"order_draw_rule_id"` OrderDrawLadderId int `json:"order_draw_ladder_id"` PrizeName string `json:"prize_name"` Status string `json:"status"` PrizeImg string `json:"prize_img"` PrizeNumber int `json:"prize_number"` } type OrderListResult struct { OrderDrawActivityId int `json:"order_draw_activity_id"` OrderDrawRuleId int `json:"order_draw_rule_id"` OrderDrawActivityName string `json:"order_draw_activity_name"` OrderDrawLadders []*OrderLadderResult `json:"order_draw_ladders"` PrizeNumber int `json:"prize_number"` } //获取所有订单奖品 func (t *OrderDrawCtl) List() { activityId := t.MustGetInt("activity_id") customerId := t.GetAccountId() // area := &models.AreaStore{} exist, err := area.GetByCustomerId(customerId, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区信息异常") activity := models.Activity{} exist, err = models.Get(&activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动信息异常") // 订单的开启或关闭 option := &models.CustomerOrderOption{} exist, err = option.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单信息异常") result := make([]*OrderListResult, 0) core.GetXormAuto().Table(&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", (&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) ruleIds := make([]int, 0) for _, v := range result { ruleIds = append(ruleIds, v.OrderDrawRuleId) } ladders := make([]*OrderLadderResult, 0) err = core.GetXormAuto().Table(&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([]int, 0) for _, ladder := range ladders { ladderIds = append(ladderIds, ladder.OrderDrawLadderId) } records := make([]map[string]int, 0) err = core.GetXormAuto().Table(&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=? and arch_id=? and area_id=?", activity.RehearsalId, activity.ArchId, area.Id). 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": option.Status, }) } //抽奖奖品 func (t *OrderDrawCtl) Prize() { ruleId := t.MustGetInt("order_draw_rule_id") list, err := models.GetOrderDrawRuleLaddersByRuleId(ruleId) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(list), "lise": list, }) } type OrderUsersResult struct { UserId int `json:"user_id"` Username string `json:"username"` Avatar string `json:"avatar"` Phone string `json:"phone"` EntryPersonName string `json:"entry_person_name"` } //统计人数和订单数量 func (t *OrderDrawCtl) Users() { activityId := t.MustGetInt("activity_id") ruleId := t.MustGetInt("order_draw_rule_id") activity := models.Activity{} exist, err := models.Get(&activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") 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, "模块服务不存在") 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, o.receiver as username"). Where("o.is_delete=0 and o.activity_id=? and o.rehearsal_id=? and o.arch_id=?", activity.Id, 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("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0", activity.Id, 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 = s.Find(&result) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } // 订单抽奖动作 func (t *OrderDrawCtl) Draw() { activityId := t.MustGetInt("activity_id") ladderId := t.MustGetInt("order_draw_rule_ladder_id") number := t.MustGetInt("number") customerId := t.GetAccountId() customer := models.Customer{} exist, err := models.Get(&customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") activity := models.Activity{} exist, err = models.Get(&activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) area := &models.AreaStore{} exist, err = area.GetByCustomerId(customerId, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在") // 查询奖品 ladder := models.OrderDrawRuleLadder{} exist, err = models.Get(&ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_LADDER_NOT_EXIST, "订单抽奖等级不存在") t.CheckRunning(ladder.Status) rule := models.OrderDrawRule{} exist, err = models.Get(&rule, ladder.OrderDrawRuleId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") count, err := (&models.OrderDrawRecord{}).Count(ladder.Id, activity.RehearsalId, activity.ArchId) 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, "模块服务不存在") 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) } lottery_service.RandSpecialOrderLottery(orders) winnerOrders := orders[:number] userIds := make([]int, 0) for _, v := range winnerOrders { userIds = append(userIds, v.BuyerId) } users, err := models.GetUsersByIds(userIds) t.CheckErr(err) winners := make([]*lottery_service.SpecialOrderLotteryUser, 0) t.CheckErr(err) for _, o := range winnerOrders { for _, u := range users { if u.Id == o.BuyerId { winners = append(winners, &lottery_service.SpecialOrderLotteryUser{ UserId: u.Id, Username: o.Receiver, UserPhone: o.Phone, Avatar: u.Avatar, PrizeName: ladder.PrizeName, LadderId: ladder.Id, PrizeImg: ladder.PrizeImg, EntryPersonName: o.OrderEntryPersonName, }) } } prize := models.UserPrize{} prize.ActivityId = activityId prize.RehearsalId = activity.RehearsalId prize.ActivityName = activity.Name prize.UserId = o.BuyerId prize.CustomerOrderId = o.Id 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 = o.BuyerId record.CustomerOrderId = o.Id record.OrderDrawRuleLadderId = ladder.Id record.PrizeName = ladder.PrizeName record.OrderEntryPersonName = o.OrderEntryPersonName record.AreaId = area.Id record.AreaName = area.Name _, err = models.Add(&record) 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) } } type WinnerResult struct { UserId int `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() { ruleId := t.MustGetInt("order_draw_rule_id") rule := &models.OrderDrawRule{} exist, err := models.Get(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") lottery := &models.OrderDrawActivity{} exist, err = models.Get(lottery, rule.OrderDrawActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") activity := &models.Activity{} exist, err = models.Get(activity, lottery.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") customer := models.Customer{} exist, err = models.Get(&customer, activity.CustomerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") var result []*lottery_service.OrderWinnersResult if customer.IsSpecial == 2 { result, err = lottery_service.GetSpecialOrderWinnersResult(rule.Id, activity.RehearsalId, activity.ArchId) } else { result, err = lottery_service.GetOrderWinnersResult(rule.Id, activity.RehearsalId, activity.ArchId) } t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } // 获取所有订单 type OrdersResult struct { UserId int `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"` } // 获取所有订单 type SpecialOrdersResult struct { Id int `json:"id"` UserId int `json:"user_id"` AreaName string `json:"area_name"` EntryPersonName string `json:"entry_person_name"` Username string `json:"username"` Phone string `json:"phone"` Address string `json:"address"` Goods []map[string]string `json:"goods"` } func (t *OrderDrawCtl) Orders() { activityId := t.MustGetInt("activity_id") activity := models.Activity{} exist, err := models.Get(&activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") customer := models.Customer{} exist, err = models.Get(&customer, activity.CustomerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") t.CheckErr(err) if customer.IsSpecial == 2 { count, err := (&models.CustomerOrder{}).CountCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, 0) 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, "+ " o.receiver as username, o.phone as phone, o.address as address, o.created_at"). 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) orderIds := make([]int, 0) for _, o := range orders { orderIds = append(orderIds, o.Id) } subs, err := models.GetCustomerOrderSubsByOrderIds(orderIds) t.CheckErr(err) for i := range orders { for j := range subs { if fmt.Sprint(orders[i].Id) == subs[j]["order_id"] { orders[i].Goods = append(orders[i].Goods, subs[j]) } } } t.JSON(map[string]interface{}{ "orders": orders, "total": len(orders), "buyer_count": count, }) return } else { count, err := (&models.CustomerOrder{}).CountCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, 1) 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": count, }) return } } // 录入人员排行榜 func (t *OrderDrawCtl) OrderRank() { activityId := t.MustGetInt("activity_id") limit := t.MustGetInt("limit") activity := models.Activity{} exist, err := models.Get(&activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") order := &models.CustomerOrder{} res, err := order.SumCustomerOrder(activity.Id, activity.RehearsalId, activity.ArchId, limit) t.CheckErr(err) var total int64 = 0 if len(res) == 1 && res[0]["num"] == "0" { res = nil } else { total, err = order.TotalCustomerOrderGoodsNum(activity.Id, activity.RehearsalId, activity.ArchId) } t.CheckErr(err) t.JSON(map[string]interface{}{ "list": res, "total": total, }) }