@ -1,6 +1,7 @@
package client
package client
import (
import (
"encoding/json"
"github.com/ouxuanserver/osmanthuswine/src/core"
"github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/controllers"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
"hudongzhuanjia/models"
@ -20,7 +21,11 @@ func (t *GoodCtl) GoodOption() {
option := new ( models . CustomerOrderOption )
option := new ( models . CustomerOrderOption )
exist , err := option . GetByActivityId ( activityId )
exist , err := option . GetByActivityId ( activityId )
t . CheckErr ( err )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_DATA_NOT_EXIST , "订单活动不存在" )
if ! exist {
t . JSON ( [ ] interface { } { } )
return
}
//t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
t . JSON ( option )
t . JSON ( option )
}
}
@ -33,11 +38,19 @@ func (t *GoodCtl) ListGood() {
option := new ( models . CustomerOrderOption )
option := new ( models . CustomerOrderOption )
exist , err := option . GetByActivityId ( activityId )
exist , err := option . GetByActivityId ( activityId )
t . CheckErr ( err )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_DATA_NOT_EXIST , "订单活动不存在" )
if ! exist {
t . JSON ( [ ] interface { } { } )
return
}
//t.Assert(exist, code.MSG_ORDER_NOT_EXIST, "订单活动不存在")
goods , err := models . GetGoodsByActivityId ( activityId , areaId )
goods , err := models . GetGoodsByActivityId ( activityId , areaId )
t . CheckErr ( err )
t . CheckErr ( err )
//
//for index := range goods {
// goods[index].GoodType = option.PostFeeType
// goods[index].Postage = option.PostFee
//}
t . JSON ( goods )
t . JSON ( goods )
return
return
}
}
@ -46,36 +59,64 @@ func (t *GoodCtl) ListOrder() {
activityId := t . MustGetInt64 ( "activity_id" )
activityId := t . MustGetInt64 ( "activity_id" )
status := t . MustGetInt ( "status" )
status := t . MustGetInt ( "status" )
areaId := t . MustGetInt64 ( "area_id" )
areaId := t . MustGetInt64 ( "area_id" )
uid := t . MustGetUID ( )
activity := new ( models . Activity )
activity := new ( models . Activity )
exist , err := models . GetById ( activity , activityId )
exist , err := models . GetById ( activity , activityId )
t . CheckErr ( err )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_ACTIVITY_NOT_EXIST , "互动不存在" )
t . Assert ( exist , code . MSG_ACTIVITY_NOT_EXIST , "互动不存在" )
orders , err := models . GetCustomerOrdersByActivityId ( activity . Id , activity . RehearsalId , areaId , status )
orders , err := models . GetCustomerOrdersByActivityId ( uid , activity . Id , activity . RehearsalId , areaId , status )
t . CheckErr ( err )
t . CheckErr ( err )
outTradeNos := make ( [ ] string , 0 )
for _ , order := range orders {
outTradeNos = append ( outTradeNos , order . OutTradeNo )
}
subs , err := models . GetCustomerOrderSubByOutTradeNos ( outTradeNos )
t . CheckErr ( err )
for index , order := range orders {
for _ , sub := range subs {
if order . OutTradeNo == sub . OutTradeNo {
orders [ index ] . SubOrders = append ( orders [ index ] . SubOrders , sub )
}
}
}
t . JSON ( orders )
t . JSON ( orders )
}
}
type OrderParam struct {
ActivityId int64 ` json:"activity_id" `
AreaId int64 ` json:"area_id" `
Name string ` json:"name" `
Phone string ` json:"phone" `
Address string ` json:"address" `
Goods interface { } ` json:"goods" `
}
// 下订单
// 下订单
func ( t * GoodCtl ) Order ( ) {
func ( t * GoodCtl ) Order ( ) {
userId := t . MustGetUID ( ) //
userId := t . MustGetUID ( ) //
activityId := t . MustGetInt64 ( "activity_id" )
areaId := t . MustGetInt64 ( "area_id" ) // 地区id
name := t . MustGet ( "name" ) // 收货人名字
phone := t . MustGet ( "phone" ) // 收货电话
address := t . MustGet ( "address" ) // 收货地址
goodId := t . MustGetInt64 ( "good_id" ) // 商品id
num := t . MustGetInt ( "num" ) // 商品数量
param := OrderParam { }
if t . Request . OriginRequest . Method == "POST" {
t . CheckErr ( t . Bind ( & param ) )
} else if t . Request . OriginRequest . Method == "GET" {
param . ActivityId = t . MustGetInt64 ( "activity_id" )
param . AreaId = t . MustGetInt64 ( "area_id" )
param . Name = t . MustGet ( "name" )
param . Phone = t . MustGet ( "phone" )
param . Address = t . MustGet ( "address" )
goods := t . MustGet ( "goods" )
err := json . Unmarshal ( [ ] byte ( goods ) , & param . Goods )
t . CheckErr ( err )
}
activity := new ( models . Activity )
activity := new ( models . Activity )
exist , err := models . GetById ( activity , activityId )
exist , err := models . GetById ( activity , p aram . A ctivityId)
t . CheckErr ( err )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_ACTIVITY_NOT_EXIST , "互动不存在" )
t . Assert ( exist , code . MSG_ACTIVITY_NOT_EXIST , "互动不存在" )
area := new ( models . AreaStore )
area := new ( models . AreaStore )
exist , err = models . GetById ( area , areaId )
exist , err = models . GetById ( area , p aram . A reaId)
t . CheckErr ( err )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_AREASTORE_NOT_EXIST , "地区不存在" )
t . Assert ( exist , code . MSG_AREASTORE_NOT_EXIST , "地区不存在" )
@ -85,7 +126,7 @@ func (t *GoodCtl) Order() {
t . Assert ( exist , code . MSG_USER_NOT_EXIST , "用户不存在" )
t . Assert ( exist , code . MSG_USER_NOT_EXIST , "用户不存在" )
option := new ( models . CustomerOrderOption )
option := new ( models . CustomerOrderOption )
exist , err = option . GetByActivityId ( activityId )
exist , err = option . GetByActivityId ( p aram . A ctivityId)
t . CheckErr ( err )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_DATA_NOT_EXIST , "订单活动不存在" )
t . Assert ( exist , code . MSG_DATA_NOT_EXIST , "订单活动不存在" )
//if option.Status == 0 {
//if option.Status == 0 {
@ -98,48 +139,60 @@ func (t *GoodCtl) Order() {
defer session . Close ( )
defer session . Close ( )
session . Begin ( )
session . Begin ( )
good := new ( models . CustomerGoods )
exist , err = session . Where ( "is_delete=0 and id=?" , goodId ) . Get ( good )
if err != nil || ! exist {
session . Rollback ( )
t . ERROR ( "商品信息异常" , code . MSG_DATA_NOT_EXIST )
}
subOrders := make ( [ ] * models . CustomerOrderSub , 0 )
var price = 0
for _ , g := range goods {
good := new ( models . CustomerGoods )
exist , err = session . Where ( "is_delete=0 and id=?" , g [ "id" ] ) . Get ( good )
if err != nil || ! exist {
session . Rollback ( )
t . ERROR ( "商品信息异常" , code . MSG_DATA_NOT_EXIST )
}
if good . Stock == 0 {
session . Rollback ( )
t . ERROR ( "商品库存不足" , code . MSG_DATA_NOT_EXIST )
}
if good . Stock > 0 {
good . Stock --
_ , err = session . ID ( good . Id ) . Cols ( "stock" ) . Update ( good )
if err != nil {
if good . Stock - g [ "num" ] < 0 {
session . Rollback ( )
session . Rollback ( )
t . CheckErr ( err )
t . ERROR ( "商品库存不足" , code . MSG_DATA_NOT_EXIST )
}
}
if good . Stock > 0 {
_ , err = session . ID ( good . Id ) . Decr ( "stock" , g [ "num" ] ) . Cols ( "stock" ) . Update ( good )
if err != nil {
session . Rollback ( )
t . CheckErr ( err )
}
}
price += int ( good . Price * 100 ) * g [ "num" ]
subOrders = append ( subOrders , & models . CustomerOrderSub {
IsDelete : false ,
CreatedAt : time . Now ( ) ,
UpdatedAt : time . Now ( ) ,
OutTradeNo : "" ,
GoodsId : good . Id ,
GoodsNum : g [ "num" ] ,
GoodName : good . Name ,
GoodPrice : good . Price ,
} )
}
}
res , err := pay_service . UnifiedOrder ( "欧轩互动-直播商品" , user . Openid ,
res , err := pay_service . UnifiedOrder ( "欧轩互动-直播商品" , user . Openid ,
int64 ( int ( good . Price * 100 ) * num + int ( option . PostFee * 100 ) ) , 4 , userId , activity . Id )
int64 ( p rice+ int ( option . PostFee * 100 ) ) , 4 , userId , activity . Id )
if err != nil {
if err != nil {
session . Rollback ( )
session . Rollback ( )
t . CheckErr ( err )
t . CheckErr ( err )
}
}
order := new ( models . CustomerOrder )
order := new ( models . CustomerOrder )
order . AreaId = areaId
order . AreaId = p aram . A reaId
order . AreaName = area . Name
order . AreaName = area . Name
order . RehearsalId = activity . RehearsalId
order . RehearsalId = activity . RehearsalId
order . BuyerId = userId
order . BuyerId = userId
order . GoodsId = goodId
order . GoodsName = good . Name
order . ActivityId = activity . Id
order . ActivityId = activity . Id
order . TotalAmount = float64 ( num ) * good . Price + option . PostFee
order . TotalAmount = float64 ( price ) / 100 + option . PostFee
order . PayAmount = order . TotalAmount
order . PayAmount = order . TotalAmount
order . OutTradeNo = res [ "out_trade_no" ] . ( string )
order . OutTradeNo = res [ "out_trade_no" ] . ( string )
order . GoodsNum = num
order . Address = address
order . Receiver = name
order . Phone = phone
order . Address = param . Address
order . Receiver = param . Name
order . Phone = param . Phone
order . Postage = option . PostFee
order . Postage = option . PostFee
order . Status = 0
order . Status = 0
order . IsDelete = false
order . IsDelete = false
@ -150,9 +203,18 @@ func (t *GoodCtl) Order() {
session . Rollback ( )
session . Rollback ( )
t . CheckErr ( err )
t . CheckErr ( err )
}
}
session . Commit ( )
for _ , subOrder := range subOrders {
subOrder . OutTradeNo = order . OutTradeNo
_ , err = session . InsertOne ( subOrder )
if err != nil {
session . Rollback ( )
t . CheckErr ( err )
}
}
session . Commit ( )
res [ "order_id" ] = order . Id
res [ "order_id" ] = order . Id
t . JSON ( res )
t . JSON ( res )
}
}
@ -164,3 +226,39 @@ func (t *GoodCtl) Reorder() {
t . CheckErr ( err )
t . CheckErr ( err )
t . JSON ( res )
t . JSON ( res )
}
}
// 订单状态[0未支付1已支付即待发货3已发货4确认收货5申请退款6已退款7申请退货8已退货9已取消]
// 申请退款
func ( t * GoodCtl ) RefundOrder ( ) {
outTradeNo := t . MustGet ( "out_trade_no" )
order := new ( models . CustomerOrder )
exist , err := order . GetByOutTradeNO ( outTradeNo )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_CUSTOMER_ORDER_NOT_EXIST , "用户订单不存在" )
if order . Status == 1 { // 退款
_ , err = order . UpdateStatusBy ( outTradeNo , 1 , 5 )
} else if order . Status == 3 {
_ , err = order . UpdateStatusBy ( outTradeNo , 3 , 7 )
} else {
t . ERROR ( "订单完成状态不能申请退款" , code . MSG_CUSTOMER_ORDER_ERROR )
return
}
t . CheckErr ( err )
t . SUCCESS ( "成功申请退款" )
}
func ( t * GoodCtl ) CancelOrder ( ) {
outTradeNo := t . MustGet ( "out_trade_no" )
order := new ( models . CustomerOrder )
exist , err := order . GetByOutTradeNO ( outTradeNo )
t . CheckErr ( err )
t . Assert ( exist , code . MSG_CUSTOMER_ORDER_NOT_EXIST , "用户订单不存在" )
if order . Status != 0 {
t . ERROR ( "非待支付状态不能取消" , code . MSG_CUSTOMER_ORDER_ERROR )
}
_ , err = order . UpdateStatusBy ( outTradeNo , 0 , 9 )
t . CheckErr ( err )
t . SUCCESS ( "成功取消订单" )
}