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

258 lines
7.6 KiB

package client
import (
"fmt"
"github.com/ouxuanserver/osmanthuswine/src/helper"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
invitation_service "hudongzhuanjia/services/invitation"
"hudongzhuanjia/utils"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
"time"
"github.com/ouxuanserver/osmanthuswine/src/core"
)
type OrderEntryCtl struct {
controllers.AuthorCtl
}
// 商品 == > 用户查看所有商品
func (t *OrderEntryCtl) List() {
uid := t.MustGetUID()
activityId := t.MustGetInt64("activity_id")
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.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
entryPerson := new(models.OrderEntryPerson)
exist, err = models.Get(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.Get(good, goodId)
t.CheckErr(err)
t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在")
session := core.GetXormAuto().NewSession()
defer session.Close()
session.Begin()
gift := new(models.OrderGift)
exist, err = session.Where("is_delete=0 and activity_id=?", activityId).Get(gift)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
prize := new(models.UserPrize)
prize.UserId = userId
prize.ActivityId = activityId
prize.RehearsalId = activity.RehearsalId
prize.ActivityName = activity.Name
prize.PrizeName = gift.GiftName
prize.PrizeImg = gift.GiftPicUrl
prize.PrizeType = 3
prize.IsDelete = false
prize.CreatedAt = time.Now()
prize.UpdatedAt = time.Now()
if exist {
if gift.Num == 0 {
_, err = session.InsertOne(prize)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
} else if gift.Num > 0 {
count, err := core.GetXormAuto().Where("activity_id=? and rehearsal_id=? and is_delete=0",
activityId, activity.RehearsalId).Count(new(models.CustomerOrder))
if err != nil {
session.Rollback()
t.CheckErr(err)
}
if gift.Num > int(count) {
_, err = session.InsertOne(prize)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
}
}
}
order := new(models.CustomerOrder)
order.UserPrizeId = prize.Id
order.AreaId = entryPerson.AreaId
order.AreaName = area.Name
order.BuyerId = userId
order.GoodsId = goodId
order.Type = 0
order.ActivityId = activityId
order.RehearsalId = activity.RehearsalId
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 = session.InsertOne(order)
if err != nil {
session.Rollback()
t.CheckErr(err)
}
err = session.Commit()
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 := models.Get(entry, uid)
t.CheckErr(err)
t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
activity := new(models.Activity)
exist, err = models.Get(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()
activityId := t.MustGetInt64("activity_id")
activity := new(models.Activity)
exist, err := models.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
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").
Where("o.buyer_id=? and o.is_delete=0 and o.activity_id=? and o.rehearsal_id=?",
uid, activity.Id, activity.RehearsalId)
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,
})
}