订单服务新增订单

master
shenzhuan 2 years ago
parent 0f5f0e4aa6
commit 12b95a1fa8

@ -10,6 +10,9 @@ import (
"github.com/opentracing/opentracing-go"
"go-micro.dev/v4/client"
"go-micro.dev/v4/web"
"strconv"
"strings"
//"goproduct/common"
common "git.mashibing.com/msb_47094/shopping-comm"
"log"
@ -70,7 +73,7 @@ func main() {
//UpdateCartClient := proto.NewUpdateCartService("shop-cart", rpcServer.Client())
//ShowProductDetailClient := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
//ShowDetailSkuClient := proto.NewShowDetailSkuService("shop-product", rpcServer.Client())
//GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client())
GetUserTokenClient := proto.NewGetUserTokenService("shop-user", rpcServer.Client())
//UpdateSkuClient := proto.NewUpdateSkuService("shop-product", rpcServer.Client())
AddTraderClient := proto.NewAddTradeOrderService("trade-order", rpcServer.Client())
//UpdateTraderClient := proto.NewUpdateTradeOrderService("trade-order", rpcServer.Client())
@ -104,10 +107,47 @@ func main() {
// }
// c.JSON(http.StatusOK, gin.H{"updateSku-compensate": "SUCCESS", "Message": "回滚库存成功!"})
//})
router.POST("/cartAdvanceOrder", func(c *gin.Context) {
req := &proto.AddTradeOrderReq{}
if err := c.BindJSON(req); err != nil {
log.Fatalln(err)
router.GET("/cartAdvanceOrder", func(c *gin.Context) {
//开始检验登录
uuid := c.Request.Header["Uuid"][0]
cc := common.GetInput(uuid)
out := common.SQ(cc)
sum := 0
for o := range out {
sum += o
}
//Token校验
//拼接请求信息
tokenReq := &proto.TokenReq{
Uuid: uuid,
}
//响应
tokenResp, err := GetUserTokenClient.GetUserToken(context.TODO(), tokenReq)
if err != nil || tokenResp.IsLogin == false {
log.Println("GetUserToken err : ", err)
common.RespFail(c.Writer, tokenResp, "未登录!")
return
}
//拼接请求信息
log.Println("GetUserToken success : ", tokenResp)
//结束检验登录
//开始订单插入
tempStr := c.Request.FormValue("cartIds") // 12,355,666
cartIds := SplitToInt32List(tempStr, ",")
isVirtual, _ := strconv.ParseBool(c.Request.FormValue("isVirtual"))
recipientAddressId, _ := strconv.Atoi(c.Request.FormValue("recipientAddressId"))
//构建tradeOrder
tradeOrder := &proto.TradeOrder{}
tradeOrder.UserId = int32(sum)
tradeOrder.CreateUser = int32(sum)
tradeOrder.OrderStatus = 1
req := &proto.AddTradeOrderReq{
CartIds: cartIds,
IsVirtual: isVirtual,
RecipientAddressId: int32(recipientAddressId),
TradeOrder: tradeOrder,
}
resp, err = AddTraderClient.AddTradeOrder(context.TODO(), req)
fmt.Println(resp)
@ -250,6 +290,24 @@ func (c clientWrapper) Call(ctx context.Context, req client.Request, resp interf
}
// 格式化页面传入的cartIds
func SplitToInt32List(str string, sep string) (int32List []int32) {
tempStr := strings.Split(str, sep)
if len(tempStr) > 0 {
for _, item := range tempStr {
if item == "" {
continue
}
val, err := strconv.ParseInt(item, 10, 32)
if err != nil {
continue
}
int32List = append(int32List, int32(val))
}
}
return int32List
}
func NewClientHystrixWrapper() client.Wrapper {
return func(i client.Client) client.Client {
return &clientWrapper{i}

@ -27,6 +27,8 @@ type TraderOrder struct {
UpdateUser int32 `json:"updateUser"`
UpdateTime time.Time `json:"updateTime"`
IsDeleted bool `json:"isDeleted"`
PayType int32 `gorm:"default:1" json:"payType"`
IsPackageFree int32 `gorm:"default:1" json:"isPackageFree"`
}
func (table *TraderOrder) TableName() string {

@ -1,7 +1,12 @@
package repository
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"time"
"trade-order/domain/model"
"trade-order/proto"
@ -31,13 +36,33 @@ type TradeRepository struct {
mysqlDB *gorm.DB
}
func SwapToStruct(req, target interface{}) (err error) {
dataByte, err := json.Marshal(req)
if err != nil {
return
}
err = json.Unmarshal(dataByte, target)
return err
}
func (u *TradeRepository) AddTradeOrder(req *proto.AddTradeOrderReq) (obj *model.TraderOrder, err error) {
trade := model.TraderOrder{}
trade.CreateTime = time.Now() //
trade := &model.TraderOrder{}
err = SwapToStruct(req.TradeOrder, trade)
if err != nil {
log.Println("SwapToStruct err :", err)
}
log.Println("SwapToStruct trade :", trade)
now := time.Now()
trade.CreateTime = now //
trade.SubmitTime = now
tp, _ := time.ParseDuration("30m")
trade.ExpireTime = now.Add(tp) //订单失效时间 30m后
trade.OrderNo = getOrderNo(now, trade.UserId)
tb := u.mysqlDB.Create(&trade)
//err = errors.New("400", "测试异常", 400)
fmt.Println("repository AddTradeOrder >>>> ", trade)
return &trade, tb.Error //err
return trade, tb.Error //err
}
func (u *TradeRepository) UpdateTradeOrder(req *proto.AddTradeOrderReq) (obj *model.TraderOrder, err error) {
@ -48,3 +73,14 @@ func (u *TradeRepository) UpdateTradeOrder(req *proto.AddTradeOrderReq) (obj *mo
fmt.Println("repository UpdateTradeOrder >>>> ", trade)
return &trade, tb.Error //err
}
// 生产 订单号 Y2022 06 27 11 00 53 948 97 103564
//
// 年 月 日 时 分 秒 毫秒 ID 随机数
func getOrderNo(time2 time.Time, userID int32) string {
var tradeNo string
tempNum := strconv.Itoa(rand.Intn(999999-100000+1) + 100000)
tradeNo = "Y" + time2.Format("20060102150405.000") + strconv.Itoa(int(userID)) + tempNum
tradeNo = strings.Replace(tradeNo, ".", "", -1)
return tradeNo
}

File diff suppressed because it is too large Load Diff

@ -9,27 +9,33 @@ package proto ; //默认在哪个包
//
/*
ID int32 `json:"id"`
UserId int32 `json:"userId"`
ProductId int32 `gorm:"product_id" json:"productId"`
ProductSkuId int32 `gorm:"product_sku_id" json:"productSkuId"`
ProductName string `json:"productName"`
ProductMainPicture string `gorm:"product_main_picture" json:"productMainPicture"`
Number int32 `gorm:"default:1" json:"number"`
CreateUser int32 `gorm:"default:1" json:"createUser"`
CreateTime time.Time `json:"createTime"`
UpdateUser int32 `json:"updateUser"`
UpdateTime time.Time `json:"updateTime"`
IsDeleted bool `json:"isDeleted"`
"serverTime": "2022-11-01 11:08:13",
"expireTime": "2022-11-01 11:38:13",
"totalAmount": 190.00,
"productAmount": 190.00,
"shippingAmount": 0,
"discountAmount": 0.00,
"payAmount": 190.00,
*/
message TradeOrder {
int32 id = 1;
int32 userId = 2;
int32 productId =3;
int32 productSkuId = 4;
string productName = 5;
string productMainPicture = 6;
int32 number = 7;
string serverTime = 1;
string expireTime = 2;
float totalAmount =3;
float productAmount = 4;
float shippingAmount = 5;
float discountAmount = 6;
float payAmount = 7; //resp
//
int32 iD = 8;
bool isDeleted = 9;
int32 orderStatus = 10;
string orderNo = 11;
int32 userId =12 ;
int32 createUser =13;
int32 updateUser =14;
string cancelReason =15;
string createTime =16;
string submitTime =17;
}
/**
@ -41,23 +47,42 @@ message TradeOrder {
**/
// request struct
message AddTradeOrderReq {
int32 number = 1;
int32 productId = 2;
int32 productSkuId =3;
string productName = 4;
string productMainPicture = 5;
int32 userId =6;
int32 id = 7;
repeated int32 cartIds = 1;
bool isVirtual = 2;
int32 recipientAddressId =3;
TradeOrder tradeOrder = 4;
}
message AddTradeOrderResp{
ProductDetail productSimple = 1;
ProductSku productSkuSimple =2;
int64 shoppingCartNumber = 3;
int64 canSetShoppingCartNumber = 4;
bool isBeyondMaxLimit = 5;
int32 ID = 6;
TradeOrder tradeOrder = 1;
repeated ProductOrder products =2;
}
//
/**
"productId": 74,
"productSkuId": 265,
"productName": "机器学习观止—核心原理与实践| 林学森 机器学习人工智能计算机",
"productImageUrl": "https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/mall-product/productO1CN010A7WjS1CP1BmI67Qg-101450072.jpg_430x430q90.jpg",
"skuDescribe": "",
"quantity": 1,
"productPrice": 118.00,
"realPrice": 118.00,
"realAmount": 118.00
*/
message ProductOrder {
int32 productId = 1;
int32 productSkuId = 2;
string productName = 3;
string productImageUrl = 4;
string skuDescribe =5 ;
int32 quantity =6;
float productPrice =7;
float realPrice =8;
float realAmount =9;
}
//RPC
service AddTradeOrder {
//rpc

Loading…
Cancel
Save