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.

324 lines
9.3 KiB

package order
import (
"encoding/json"
"fmt"
"github.com/towgo/towgo/dao/basedboperat"
"github.com/towgo/towgo/dao/ormDriver/xormDriver"
"github.com/towgo/towgo/towgo"
"log"
"math/rand"
"src/module/cart"
"src/module/goods"
skuPrice "src/module/skuPrice"
"src/module/usercenter"
"strconv"
"time"
)
func init() {
xormDriver.Sync2(new(Order))
towgo.NewCRUDJsonrpcAPI("/order", Order{}, []Order{}).RegAPI()
towgo.SetFunc("/order/createOrder", CreateOrder)
towgo.SetFunc("/order/pre", Pre)
towgo.SetFunc("/order/index", OrderList)
//towgo.SetFunc("/order/detail", Detail)
}
func OrderList(rpc towgo.JsonRpcConnection) {
type params struct {
Type string `json:"type"`
Page int64 `json:"page"`
}
var p params
rpc.ReadParams(&p)
var listOrder basedboperat.List
listOrder.Limit = -1
if p.Type != "all" {
listOrder.Where = append(listOrder.Where, basedboperat.Condition{
Field: "status_code",
Operator: "=",
Value: p.Type,
})
}
var o Order
var os []Order
basedboperat.ListScan(&listOrder, &o, &os)
type data struct {
ResultOrder []Order `json:"result_order"`
CurrentPage int64 `json:"current_Page"`
LastPage int64 `json:"last_page"`
PerPage int64 `json:"per_page"`
Total int64 `json:"total"`
}
sql := "select count(id) as `count` from `" + o.TableName() + "`"
var m []map[string]interface{}
err := basedboperat.SqlQueryScan(&m, sql)
if err != nil {
log.Println(err)
}
count := int64(m[0]["count"].(float64))
var lastPage int64
if count%10 == 0 {
lastPage = count / 10
} else {
lastPage = count/10 + 1
}
d := data{
ResultOrder: os,
CurrentPage: p.Page,
LastPage: lastPage,
PerPage: 10,
Total: count,
}
rpc.WriteResult(d)
}
func Detail(rpc towgo.JsonRpcConnection) {
type idst struct {
Id int64 `json:"id"`
}
var i idst
rpc.ReadParams(&i)
log.Println(i)
var o Order
err := basedboperat.Get(&o, nil, "id = ?", i.Id)
log.Println("123")
if err != nil {
rpc.WriteError(500, err.Error())
return
}
log.Println("312")
rpc.WriteResult(o)
}
type PrePamars struct {
GoodsList []struct {
GoodsId int64 `json:"goods_id"`
SkuPriceId int64 `json:"sku_price_id"`
GoodsPrice string `json:"goods_price"`
GoodsNum int64 `json:"goods_num"`
} `json:"goods_list"`
From string `json:"from"`
AddressId int64 `json:"address_id"`
CouponsId int64 `json:"coupons_id"`
}
func Pre(rpc towgo.JsonRpcConnection) {
var params PrePamars
rpc.ReadParams(&params)
var s skuPrice.SKUPrice
var gs []tempGoods
var g goods.Goods
var total_pre float64
for _, v := range params.GoodsList {
total := 0.0
if v.SkuPriceId != 0 {
basedboperat.Get(&s, nil, "id=?", v.SkuPriceId)
p, err := strconv.ParseFloat(s.Price, 64)
if err != nil {
return
}
total += p * float64(v.GoodsNum)
} else {
gp, _ := strconv.ParseFloat(v.GoodsPrice, 64)
total += gp * float64(v.GoodsNum)
}
total_pre += total
basedboperat.Get(&g, nil, "id=?", v.GoodsId)
gres := tempGoods{
GoodsId: g.ID,
SkuPriceId: "",
GoodsPrice: g.Price,
GoodsNum: v.GoodsNum,
DispatchType: "express",
Detail: g,
GoodsOriginalAmount: g.Price,
GoodsAmount: "",
DispatchAmount: "0.00",
ScoreAmount: 0,
DispatchId: 0,
StoreId: 0,
ActivityType: "",
DiscountFee: "",
PayPrice: strconv.FormatFloat(total, 'f', -1, 64),
}
gs = append(gs, gres)
}
free := strconv.FormatFloat(total_pre, 'f', -1, 64)
rpc.WriteResult(PreRes{
GoodsOriginalAmount: free,
GoodsAmount: free,
OriginDispatchAmount: "",
DispatchAmount: "",
TotalAmount: free,
TotalFee: free,
DiscountFee: "",
CouponFee: "",
ActivityDiscountMoney: "",
DispatchDiscountMoney: "",
InvoiceAmount: "",
ActivityType: "",
ScoreAmount: 0,
NewGoodsList: gs,
NeedAddress: 1,
ActivityDiscountInfos: nil,
Msg: nil,
})
}
type PreRes struct {
GoodsOriginalAmount string `json:"goods_original_amount"`
GoodsAmount string `json:"goods_amount"`
OriginDispatchAmount string `json:"origin_dispatch_amount"`
DispatchAmount string `json:"dispatch_amount"`
TotalAmount string `json:"total_amount"`
TotalFee string `json:"total_fee"`
DiscountFee string `json:"discount_fee"`
CouponFee string `json:"coupon_fee"`
ActivityDiscountMoney string `json:"activity_discount_money"`
DispatchDiscountMoney string `json:"dispatch_discount_money"`
InvoiceAmount string `json:"invoice_amount"`
ActivityType string `json:"activity_type"`
ScoreAmount int `json:"score_amount"`
NewGoodsList []tempGoods `json:"new_goods_list"`
NeedAddress int `json:"need_address"`
ActivityDiscountInfos []interface{} `json:"activity_discount_infos"`
Msg interface{} `json:"msg"`
}
type tempGoods struct {
GoodsId int64 `json:"goods_id"`
SkuPriceId string `json:"sku_price_id"`
GoodsPrice string `json:"goods_price"`
GoodsNum int64 `json:"goods_num"`
DispatchType string `json:"dispatch_type"`
Detail goods.Goods `json:"detail"`
GoodsOriginalAmount string `json:"goods_original_amount"`
GoodsAmount string `json:"goods_amount"`
DispatchAmount string `json:"dispatch_amount"`
ScoreAmount int64 `json:"score_amount"`
DispatchId int64 `json:"dispatch_id"`
StoreId int64 `json:"store_id"`
ActivityType string `json:"activity_type"`
DiscountFee string `json:"discount_fee"`
PayPrice string `json:"pay_price"`
}
type CreateOrderParams struct {
GoodsList []struct {
GoodsId int64 `json:"goods_id"`
CartId int64 `json:"cart_id"`
SkuPriceId int64 `json:"sku_price_id"`
GoodsPrice string `json:"goods_price"`
GoodsNum int64 `json:"goods_num"`
DispatchType string `json:"dispatch_type"`
} `json:"goods_list"`
From string `json:"from"`
AddressId int64 `json:"address_id"`
CouponsId int64 `json:"coupons_id"`
Remark string `json:"remark"`
Invoice struct {
} `json:"invoice"`
}
func CreateOrder(rpc towgo.JsonRpcConnection) {
user, err := usercenter.LoginByToken(rpc.GetRpcRequest().Session)
//user, err := usercenter.LoginByToken("15c59252b34734f5f106be41a8108cf3")
user.Token = rpc.GetRpcRequest().Session
if err != nil {
rpc.GetRpcResponse().Error.Set(401, err.Error())
rpc.Write()
return
}
var params CreateOrderParams
rpc.ReadParams(&params)
var is []cart.Cart
var totalFree float64
for _, v := range params.GoodsList {
var i cart.Cart
basedboperat.Get(&i, nil, "id=?", v.CartId)
is = append(is, i)
basedboperat.Delete(&i, i.ID, nil, nil)
floatTemp, err := strconv.ParseFloat(v.GoodsPrice, 64)
if err != nil {
return
}
totalFree += float64(v.GoodsNum) * floatTemp
}
marshal, err := json.Marshal(ExtArr{
BuyType: "alone",
GrouponId: params.CouponsId,
ExpiredTime: time.Now().Unix(),
})
if err != nil {
return
}
o := Order{
Type: "goods",
OrderSN: generateOrderNumber(),
UserID: user.ID,
ActivityType: "",
GoodsAmount: strconv.FormatFloat(totalFree, 'f', -1, 64),
DispatchAmount: "0.00",
Phone: user.Mobile,
Consignee: "我去",
ProvinceName: "上海市",
CityName: "上海市",
AreaName: "黄埔区",
Address: strconv.FormatInt(params.AddressId, 10),
ProvinceID: 310000,
CityID: 310100,
AreaID: 310101,
Status: 0,
InvoiceStatus: "0",
Memo: "",
Remark: "",
TotalAmount: "0",
ScoreAmount: 0,
TotalFee: strconv.FormatFloat(totalFree, 'f', -1, 64),
LastTotalFee: "0.00",
DiscountFee: "0.00",
CouponFee: "0.00",
ActivityDiscountMoney: "0.00",
DispatchDiscountMoney: "",
PayFee: "0.00",
ScoreFee: 0,
GoodsOriginalAmount: "66.00",
CouponsID: 0,
TransactionID: "",
PaymentJSON: "",
PayType: "",
Paytime: 0,
Ext: string(marshal),
Platform: "H5",
Createtime: time.Now().Unix(),
Item: is,
StatusCode: "nopay",
StatusName: "待付款",
StatusDesc: "等待买家付款",
Btns: []string{"cancel", "pay"},
}
_, err = basedboperat.Create(&o)
if err != nil {
log.Println(err.Error())
rpc.WriteError(500, err.Error())
return
}
rpc.WriteResult(o)
}
func generateOrderNumber() string {
// 生成一个时间戳字符串
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
// 生成一个随机数字符串
rand.Seed(time.Now().UnixNano())
randomNumber := rand.Intn(10000) // 这里可以根据需要调整随机数的范围
// 构造订单号
orderNumber := fmt.Sprintf("%d%d", timestamp, randomNumber)
return orderNumber
}