Skip to content

Commit

Permalink
订单列表
Browse files Browse the repository at this point in the history
  • Loading branch information
taxus authored and taxus committed Nov 17, 2017
1 parent c49abd2 commit dce321c
Show file tree
Hide file tree
Showing 25 changed files with 4,155 additions and 277 deletions.
65 changes: 50 additions & 15 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
// pcategory "fresh/proto/category"
// pgoods "fresh/proto/goods"
// pcart "fresh/proto/cart"
paddress "fresh/proto/address"
// paddress "fresh/proto/address"
porder "fresh/proto/order"

"github.com/golang/protobuf/proto"
"github.com/kataras/iris/httptest"
Expand Down Expand Up @@ -174,26 +175,60 @@ var app = newApp()
// log.Printf("[%s] %v", "TestCartRemoveSelected", data)
// }

func TestLoadUserDefatultAddress(t *testing.T) {
e := httptest.New(t, app)
// func TestLoadUserDefatultAddress(t *testing.T) {
// e := httptest.New(t, app)
//
// body := e.GET("/user/address/default").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
// bytes, _ := base64.StdEncoding.DecodeString(body.Raw())
// data := &paddress.Address{}
// if err := proto.Unmarshal([]byte(bytes), data); err != nil {
// panic(err)
// }
// log.Printf("[TestLoadUserDefatultAddress] %v", data)
// }
//
// func TestLoadUserAllAddress(t *testing.T) {
// e := httptest.New(t, app)
//
// body := e.GET("/user/address/all").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
// bytes, _ := base64.StdEncoding.DecodeString(body.Raw())
// data := &paddress.AllAddress{}
// if err := proto.Unmarshal([]byte(bytes), data); err != nil {
// panic(err)
// }
// log.Printf("[TestLoadUserAllAddress] %v", data)
// }

body := e.GET("/user/address/default").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
bytes, _ := base64.StdEncoding.DecodeString(body.Raw())
data := &paddress.Address{}
if err := proto.Unmarshal([]byte(bytes), data); err != nil {
panic(err)
}
log.Printf("[TestLoadUserDefatultAddress] %v", data)
}
// func TestCreateOrder(t *testing.T) {
// e := httptest.New(t, app)
//
// param := &porder.CreateParam{AddressID: 814}
// src, err := proto.Marshal(param)
// if err != nil {
// panic(err)
// }
//
// dst := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
// base64.StdEncoding.Encode(dst, src)
// body := e.POST("/order/create").WithBytes(dst).WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
// bytes, _ := base64.StdEncoding.DecodeString(body.Raw())
// data := &porder.Order{}
// if err := proto.Unmarshal([]byte(bytes), data); err != nil {
// panic(err)
// }
// log.Printf("[TestCreateOrder] %v", data)
// }

func TestLoadUserAllAddress(t *testing.T) {
func TestLoadOrderList(t *testing.T) {
e := httptest.New(t, app)

body := e.GET("/user/address/all").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
body := e.GET("/order/list").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
bytes, _ := base64.StdEncoding.DecodeString(body.Raw())
data := &paddress.AllAddress{}
data := &porder.List{}
if err := proto.Unmarshal([]byte(bytes), data); err != nil {
panic(err)
}
log.Printf("[TestLoadUserAllAddress] %v", data)
for _, v := range data.GetOrders() {
log.Printf("[TestLoadOrderList] %v", v)
}
}
32 changes: 32 additions & 0 deletions config/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,35 @@ func (m *Mysql) ConvBool(bo sql.NullBool) sql.NullBool {
}
return bo
}

// SafeString 转换sql.NullString
func (m *Mysql) SafeString(str sql.NullString) string {
if !str.Valid {
return ""
}
return str.String
}

// SafeInt64 转换sql.NullInt64
func (m *Mysql) SafeInt64(i64 sql.NullInt64) int64 {
if !i64.Valid {
return 0
}
return i64.Int64
}

// SafeFloat64 转换sql.NullFloat64
func (m *Mysql) SafeFloat64(f64 sql.NullFloat64) float64 {
if !f64.Valid {
return 0.0
}
return f64.Float64
}

// SafeBool 转换sql.NullBool
func (m *Mysql) SafeBool(bo sql.NullBool) bool {
if !bo.Valid {
return false
}
return bo.Bool
}
7 changes: 7 additions & 0 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ var (

// CartController 购物车
CartController *cartController

// AddressController 收货地址
AddressController *addressController

// OrderController 订单
OrderController *orderController
)

// InitControllers 初始化
Expand All @@ -71,4 +77,5 @@ func InitControllers() {
UserController = &userController{}
GoodsController = &goodsController{}
CartController = &cartController{}
OrderController = &orderController{}
}
1 change: 1 addition & 0 deletions controllers/goods_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (c *goodsController) Recommend(ctx iris.Context) {
ctx.Text(err.Error())
return
}

pgs := make([]*pgoods.Recommend, len(gs))
for i, g := range gs {
pgs[i] = &pgoods.Recommend{
Expand Down
118 changes: 118 additions & 0 deletions controllers/order_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package controllers

import (
"fresh/model"
porder "fresh/proto/order"
"log"

"github.com/kataras/iris"
)

type orderController struct {
*controller
}

// Create 创建订单
func (c *orderController) Create(ctx iris.Context) {
params := &porder.CreateParam{}
if err := c.ReadProto(ctx, params); err != nil {
ctx.Text(err.Error())
return
}

u, err := model.LoadUserBy(ctx.GetHeader("token"))
if err != nil {
ctx.Text(err.Error())
return
}

cs, err := model.LoadSelectedCarts(u.ID)
if err != nil {
ctx.Text(err.Error())
return
}

ua, err := model.LoadShippingAddress(params.AddressID)
if err != nil {
ctx.Text(err.Error())
return
}

s, err := model.LoadShippingBy("sto_express")
if err != nil {
ctx.Text(err.Error())
return
}

o := model.NewOrder()
log.Println(o)
if err := o.Create(cs, ua, params.Remark, s, u.ID); err != nil {
ctx.Text(err.Error())
log.Println(err)
return
}

result := &porder.Order{
ID: o.ID,
OrderSN: o.OrderSN,
TotalAmount: o.TotalAmount,
}
c.WriteProto(ctx, result)
}

// List 订单列表
func (c *orderController) List(ctx iris.Context) {
u, err := model.LoadUserBy(ctx.GetHeader("token"))
if err != nil {
ctx.Text(err.Error())
return
}

owgs, err := model.LoadOrderList(u.ID)
if err != nil {
ctx.Text(err.Error())
return
}

presult := &porder.List{}
powgs := make([]*porder.List_OrderWithGoods, len(owgs))
for i, v := range owgs {
powg := &porder.List_OrderWithGoods{
Order: &porder.List_OrderWithGoods_Order{
ID: v.Order.ID,
OrderState: uint32(v.Order.OrderState),
OrderSN: v.Order.OrderSN,
ShippingState: uint32(v.Order.ShippingState),
PayState: uint32(v.Order.PayState),
GoodsPrice: v.Order.GoodsPrice,
ShippingPrice: v.Order.ShippingPrice,
OrderAmount: v.Order.OrderAmount,
TotalAmount: v.Order.TotalAmount,
AddTime: v.Order.AddTime,
UserNote: v.Order.UserNote,
AdminNote: v.Order.AdminNote,
},
}

pogs := make([]*porder.List_OrderWithGoods_OrderGoods, len(v.OrderGoodses))
for j, v1 := range v.OrderGoodses {
pog := &porder.List_OrderWithGoods_OrderGoods{
ID: v1.ID,
OrderID: v1.OrderID,
GoodsID: v1.GoodsID,
GoodsName: v1.GoodsName,
GoodsNum: uint32(v1.GoodsNum),
GoodsPrice: v1.GoodsPrice,
CostPrice: v1.CostPrice,
SpecKeyName: v1.SpecKeyName,
}
pogs[j] = pog
}

powg.OrderGoodses = pogs
powgs[i] = powg
}
presult.Orders = powgs

c.WriteProto(ctx, presult)
}
66 changes: 0 additions & 66 deletions controllers/user_controller.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,8 @@
package controllers

import (
"fresh/model"
puser "fresh/proto/user"

"github.com/kataras/iris"
)

type userController struct {
*controller
}

func (c *userController) Get() {
}

// AllAddress 用户所有地址
func (c *userController) AllAddress(ctx iris.Context) {
u, err := model.LoadUserBy(ctx.GetHeader("token"))
if err != nil {
ctx.Text(err.Error())
return
}

as, err := model.LoadUserAddress(u.ID)
if err != nil {
ctx.Text(err.Error())
return
}
c.WriteProto(ctx, c.convAddresses(as))
}

// DefatultAddress 默认收货地址
func (c *userController) DefatultAddress(ctx iris.Context) {
u, err := model.LoadUserBy(ctx.GetHeader("token"))
if err != nil {
ctx.Text(err.Error())
return
}

a, err := model.LoadDefaultAddress(u.ID)
if err != nil {
ctx.Text(err.Error())
return
}

c.WriteProto(ctx, c.convAddress(a))
}

func (c *userController) convAddresses(as []*model.UserAddress) *puser.AllAddress {
plist := &puser.AllAddress{}
list := make([]*puser.Address, len(as))
for i, v := range as {
list[i] = c.convAddress(v)
}
plist.Addresses = list
return plist
}

func (c *userController) convAddress(a *model.UserAddress) *puser.Address {
return &puser.Address{
ID: a.ID,
Consignee: a.Consignee,
Country: a.Country,
Province: a.Province,
City: a.City,
District: a.District,
Twon: a.Twon,
Address: a.Address,
Zipcode: a.Zipcode,
Mobile: a.Mobile,
IsDefault: a.IsDefault,
}
}
21 changes: 8 additions & 13 deletions model/ad_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package model

import (
"log"
"testing"
)

func TestLoadAd(t *testing.T) {
ads, err := LoadAds(9)
checkErr(err)

for _, v := range *ads {
log.Printf("获取广告: %v \n", v)
}
}
// func TestLoadAd(t *testing.T) {
// ads, err := LoadAds(9)
// checkErr(err)
//
// for _, v := range *ads {
// log.Printf("获取广告: %v \n", v)
// }
// }
6 changes: 3 additions & 3 deletions model/cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func CountUserCarts(userID uint32) (int32, error) {
}

// LoadSelectedCarts 获取购物车中选中的商品
func LoadSelectedCarts(userID uint32) (*Carts, error) {
func LoadSelectedCarts(userID uint32) (Carts, error) {
var cs Carts
query := ` SELECT c.*, g.cost_price FROM tp_cart c
LEFT JOIN tp_goods g ON c.goods_id = g.goods_id
Expand All @@ -114,10 +114,10 @@ func LoadSelectedCarts(userID uint32) (*Carts, error) {
return nil
}
err := DataSource.QueryMore(query, f, userID)
if err != nil && err != sql.ErrNoRows {
if err != nil {
err = fmt.Errorf("[LoadSelectedCarts] %v", err)
}
return &cs, err
return cs, err
}

// Remove 删除购物车
Expand Down
Loading

0 comments on commit dce321c

Please sign in to comment.