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

233 lines
7.3 KiB

package client
import (
"fmt"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
invitation_service "hudongzhuanjia/services/invitation"
"hudongzhuanjia/utils"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
"time"
"github.com/ouxuanserver/osmanthuswine/src/core"
"github.com/ouxuanserver/osmanthuswine/src/helper"
)
type OrderEntryCtl struct {
controllers.AuthorCtl
}
// 商品 == > 用户查看所有商品
func (t *OrderEntryCtl) List() {
uid := t.MustGetUID()
activityId := t.MustGetActivityId()
goods := make([]*models.CustomerGoods, 0)
err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
Asc("created_at").Find(&goods)
t.CheckErr(err)
for index := range goods {
url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?"+
"user_id=%d&activity_id=%d&good_id=%d", define.HOST, uid, activityId, goods[index].Id)
qrcode, err := utils.Qrcode2Base64(url)
t.CheckErr(err)
goods[index].Qrcode = qrcode
}
t.JSON(map[string]interface{}{
"list": goods,
"total": len(goods),
})
}
// 下订单 == > 二维码跳转
func (t *OrderEntryCtl) Order() {
userId := t.MustGetInt64("user_id")
activityId := t.MustGetInt64("activity_id")
goodId := t.MustGetInt64("good_id")
entryId := t.MustGetUID()
activity := new(models.Activity)
exist, err := models.GetById(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
entryPerson := new(models.OrderEntryPerson)
exist, err = models.GetById(entryPerson, entryId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
area := new(models.AreaStore)
exist, err = area.GetAreaStoreById(entryPerson.AreaId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
good := new(models.CustomerGoods)
exist, err = models.GetById(good, goodId)
t.CheckErr(err)
t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在")
orderGift := new(models.OrderGift)
exist, err = orderGift.GetByActivityId(activityId)
t.CheckErr(err)
userPrize := new(models.UserPrize)
userPrize.UserId = userId
userPrize.ActivityId = activityId
userPrize.RehearsalId = activity.RehearsalId
userPrize.ActivityName = activity.Name
userPrize.PrizeName = orderGift.GiftName
userPrize.PrizeImg = orderGift.GiftPicUrl
userPrize.PrizeType = 3
userPrize.IsDelete = false
userPrize.CreatedAt = time.Now()
userPrize.UpdatedAt = time.Now()
if exist && orderGift.Num == 0 { // 存在无限制
_, err := core.GetXormAuto().Insert(userPrize)
t.CheckErr(err)
}
order := new(models.CustomerOrder)
order.UserPrizeId = userPrize.Id
order.AreaId = entryPerson.AreaId
order.AreaName = area.Name
order.RehearsalId = activity.RehearsalId
order.BuyerId = userId
order.GoodsId = goodId
order.ActivityId = activityId
order.OrderEntryPersonId = entryPerson.Id
order.GoodsName = good.Name
order.TotalAmount = good.Price
order.OutTradeNo = helper.CreateUUID()
order.IsDelete = false
order.UpdatedAt = time.Now()
order.CreatedAt = time.Now()
_, err = core.GetXormAuto().Insert(order)
t.CheckErr(err)
// 查出这个订单多少名
// 防止并发出现错误
if exist && orderGift.Num > 0 {
count, err := core.GetXormAuto().Where("created_at >= ? and activity_id=? and rehearsal_id=? and is_delete=0",
order.CreatedAt, activityId, activity.RehearsalId).Count(new(models.CustomerOrder))
t.CheckErr(err)
if orderGift.Num >= int(count) {
_, err = core.GetXormAuto().InsertOne(userPrize)
t.CheckErr(err)
order.UserPrizeId = userPrize.Id
_, err := core.GetXormAuto().Id(order.Id).Cols("user_prize_id").Update(order)
t.CheckErr(err)
}
}
t.SUCCESS("success")
}
func (t *OrderEntryCtl) DeleteOrder() {
orderId := t.MustGetInt64("order_id")
order := new(models.CustomerOrder)
exist, err := core.GetXormAuto().Where("is_delete=0 and id=?", orderId).Get(order)
t.CheckErr(err)
t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单不存在")
_, err = new(models.CustomerOrder).SoftDeleteById(orderId)
t.CheckErr(err)
if order.UserPrizeId != 0 {
_, err = new(models.UserPrize).SoftDeleteById(order.UserPrizeId)
t.CheckErr(err)
}
t.SUCCESS("删除成功")
}
type OrderListResult struct {
OrderId int64 `json:"order_id"`
UserId int64 `json:"user_id"`
EntryName string `json:"entry_name"`
GoodName string `json:"good_name"`
OrderTime string `json:"order_time"`
OrderMoney float64 `json:"order_money"`
Extra []map[string]interface{} `json:"extra"` // 额外信息
ExtraData string `json:"-"`
}
func (t *OrderEntryCtl) EntryOrders() {
uid := t.MustGetUID()
activityId := t.MustGetInt64("activity_id")
entry := new(models.OrderEntryPerson)
exist, err := entry.GetById(uid)
t.CheckErr(err)
t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
activity := new(models.Activity)
exist, err = models.GetById(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
list := make([]*OrderListResult, 0)
err = core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
Select("l.extra_data as extra_data, g.name as good_name, o.buyer_id as user_id, o.total_amount as order_money, "+
" o.id as order_id, DATE_FORMAT(o.created_at, '%Y-%m-%d %H:%i:%S') AS order_time").
Join("LEFT", new(models.CustomerGoods).Alias("g"),
"o.goods_id=g.id and o.activity_id=g.activity_id and g.is_delete=0").
Join("LEFT", new(models.InvitationLetter).Alias("l"),
"l.user_id = o.buyer_id and o.activity_id=l.activity_id and l.is_delete=0").
Where("o.activity_id=? and o.order_entry_person_id=? and o.rehearsal_id=? and o.is_delete=0",
activityId, uid, activity.RehearsalId).Desc("o.created_at").Find(&list)
t.CheckErr(err)
// 添加邀请函的内容
optionItems, err := invitation_service.GetOptionItem(activityId)
t.CheckErr(err)
for i := range list {
data, err := invitation_service.GetOptionValue(optionItems, list[i].ExtraData)
t.CheckErr(err)
list[i].Extra = data
list[i].EntryName = entry.Name
}
t.JSON(map[string]interface{}{
"list": list,
"total": len(list),
})
}
// 用户订单列表
type UserOrdersResult struct {
models.CustomerOrder `xorm:"extends"`
Good *models.CustomerGoods `json:"good" xorm:"extends"`
}
// 用户查看订单列表
func (t *OrderEntryCtl) UserOrders() {
uid := t.MustGetUID()
orders := make([]UserOrdersResult, 0)
s := core.GetXormAuto().Table(new(models.CustomerOrder)).Alias("o").
Join("LEFT", new(models.CustomerGoods).Alias("g"), "o.goods_id=g.id and g.is_delete=0").
Desc("o.created_at").Where("o.buyer_id=? and o.is_delete=0", uid)
if t.PageSize > 0 {
s = s.Limit(t.PageSize, t.Page*t.PageSize)
}
total, err := s.Desc("o.created_at").FindAndCount(&orders)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"list": orders,
"total": total,
})
}
// 二维码
func (t *OrderEntryCtl) Qrcode() {
userId := t.MustGetInt64("user_id")
activityId := t.MustGetInt64("activity_id")
goodId := t.MustGetInt64("good_id")
Url := fmt.Sprintf("%s/PcClient/Client/OrderEntryCtl/order?user_id=%d&activity_id=%d&good_id=%d",
define.HOST, userId, activityId, goodId)
qr, err := utils.Qrcode2Base64(Url)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"qrcode": qr,
})
}