order apis

This commit is contained in:
2026-08-26 17:32:54 +08:00
parent eabdc8772e
commit 299191749b
32 changed files with 1321 additions and 251 deletions
+9
View File
@@ -16,3 +16,12 @@ type Cart struct {
type CartInfo struct {
Content string `json:"content" gorm:"content"` // 内容
}
type ProductInfo struct {
Id int64 `json:"id" gorm:"id"`
Price float64 `json:"price" gorm:"price"`
Number int `json:"number" gorm:"number"`
Name string `json:"name" gorm:"name"`
Images string `json:"images" gorm:"images"`
Stock int `json:"stock" gorm:"stock"`
}
+26 -24
View File
@@ -22,30 +22,32 @@ const (
)
type OrderCreate struct {
Id int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
OrderSn string `gorm:"column:ordersn;type:varchar(100);comment:订单号;NOT NULL" json:"ordersn"`
StoreId int `gorm:"column:store_id;type:int(11);comment:美容院关联ID号" json:"store_id"`
StoreName string `gorm:"column:store_name;type:varchar(50);comment:门店名" json:"store_name"`
SaleName string `gorm:"column:sale_name;type:varchar(50);comment:销售姓名" json:"sale_name"`
SaleMobile string `gorm:"column:sale_mobile;type:char(60);comment:销售电话" json:"sale_mobile"`
SaleProvince int `gorm:"column:sale_province;type:int(11);default:0;comment:销售到的地域" json:"sale_province"`
UserId int `gorm:"column:user_id;type:int(11);comment:用户ID" json:"user_id"`
UserName string `gorm:"column:user_name;type:varchar(255);comment:用户名" json:"user_name"`
SaleId int `gorm:"column:sale_id;type:int(11);comment:销售员ID号" json:"sale_id"`
Mobile string `gorm:"column:mobile;type:char(60);comment:收货人手机号;NOT NULL" json:"mobile"`
Address string `gorm:"column:address;type:varchar(255);comment:收货人地址;NOT NULL" json:"address"`
Addressee string `gorm:"column:addressee;type:varchar(50);comment:收货人姓名;NOT NULL" json:"addressee"`
DistrictIds string `gorm:"column:district_ids;type:varchar(255);comment:地区IDS;NOT NULL" json:"district_ids"`
TotalPrice float64 `gorm:"column:total_price;type:decimal(10,2);comment:总价;NOT NULL" json:"total_price"`
GroupId int `gorm:"column:group_id;type:int(11);default:0" json:"group_id"`
PayPrice float64 `gorm:"column:pay_price;type:decimal(10,2);comment:实际支付金额" json:"pay_price"`
PayType uint8 `gorm:"column:pay_type;type:tinyint(1);default:1;comment:支付类型,1为微信,2为支付宝,3为余额支付" json:"pay_type"`
Status uint8 `gorm:"column:status;type:tinyint(1);default:2;comment:支付状态,1为已支付,2为未支付,3为支付中,4支付失败,5已失效;NOT NULL" json:"status"`
IsSupply uint8 `gorm:"column:is_supply;type:tinyint(1);default:1;comment:是否补过货,1为没有,2为补过" json:"is_supply"`
Note string `gorm:"column:note;type:varchar(255);comment:备注" json:"note"`
VerifyStatus uint8 `gorm:"column:verify_status;type:tinyint(1);default:0;comment:审核状态,1为通过,2为失败,3为待审核,0为正常订单不需要审核" json:"verify_status"`
ProductIds string `gorm:"column:product_ids;type:varchar(150);comment:产品IDs" json:"product_ids"`
Type uint8 `gorm:"column:type;type:tinyint(1);default:0;comment:类型 1 销售,2为门店,3为个人,4为个人家庭试用,5为个人门店试用" json:"type"`
Id int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
OrderSn string `gorm:"column:ordersn;type:varchar(100);comment:订单号;NOT NULL" json:"ordersn"`
StoreId int `gorm:"column:store_id;type:int(11);comment:美容院关联ID号" json:"store_id"`
StoreName string `gorm:"column:store_name;type:varchar(50);comment:门店名" json:"store_name"`
SaleName string `gorm:"column:sale_name;type:varchar(50);comment:销售姓名" json:"sale_name"`
SaleMobile string `gorm:"column:sale_mobile;type:char(60);comment:销售电话" json:"sale_mobile"`
SaleProvince int `gorm:"column:sale_province;type:int(11);default:0;comment:销售到的地域" json:"sale_province"`
UserId int `gorm:"column:user_id;type:int(11);comment:用户ID" json:"user_id"`
UserName string `gorm:"column:user_name;type:varchar(255);comment:用户名" json:"user_name"`
SaleId int `gorm:"column:sale_id;type:int(11);comment:销售员ID号" json:"sale_id"`
Mobile string `gorm:"column:mobile;type:char(60);comment:收货人手机号;NOT NULL" json:"mobile"`
Address string `gorm:"column:address;type:varchar(255);comment:收货人地址;NOT NULL" json:"address"`
Addressee string `gorm:"column:addressee;type:varchar(50);comment:收货人姓名;NOT NULL" json:"addressee"`
DistrictIds string `gorm:"column:district_ids;type:varchar(255);comment:地区IDS;NOT NULL" json:"district_ids"`
TotalPrice float64 `gorm:"column:total_price;type:decimal(10,2);comment:总价;NOT NULL" json:"total_price"`
GroupId int `gorm:"column:group_id;type:int(11);default:0" json:"group_id"`
PayPrice float64 `gorm:"column:pay_price;type:decimal(10,2);comment:实际支付金额" json:"pay_price"`
PayType uint8 `gorm:"column:pay_type;type:tinyint(1);default:1;comment:支付类型,1为微信,2为支付宝,3为余额支付" json:"pay_type"`
Status uint8 `gorm:"column:status;type:tinyint(1);default:2;comment:支付状态,1为已支付,2为未支付,3为支付中,4支付失败,5已失效;NOT NULL" json:"status"`
IsSupply uint8 `gorm:"column:is_supply;type:tinyint(1);default:1;comment:是否补过货,1为没有,2为补过" json:"is_supply"`
Note string `gorm:"column:note;type:varchar(255);comment:备注" json:"note"`
VerifyStatus uint8 `gorm:"column:verify_status;type:tinyint(1);default:0;comment:审核状态,1为通过,2为失败,3为待审核,0为正常订单不需要审核" json:"verify_status"`
ExpiredTime utils.CustomTime `json:"expired_time" gorm:"expired_time"` // 过期时间
ProductIds string `gorm:"column:product_ids;type:varchar(150);comment:产品IDs" json:"product_ids"`
CreateType uint8 `gorm:"column:create_type;type:tinyint(1);default:1;comment:下单人类型,1为销售,2为门店,3为个人;NOT NULL" json:"create_type"`
Type uint8 `gorm:"column:type;type:tinyint(1);default:0;comment:类型 1 销售,2为门店,3为个人,4为个人家庭试用,5为个人门店试用" json:"type"`
}
type OrderInfo struct {
@@ -82,9 +82,7 @@ func (l *AddressItemLogic) AddressItem(in *order.AddressItemsRequest) (*order.Re
mobile, aErr := utils.DecryptPhone(v.Mobile)
if aErr == nil {
info[key].AesMobile = v.Mobile
l.Logger.Error(mobile)
info[key].Mobile = utils.DecryptPhoneReplace(mobile)
l.Logger.Error(utils.DecryptPhoneReplace(mobile))
}
}
return l.ok(info)
+68 -11
View File
@@ -2,6 +2,12 @@ package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/services/order/internal/dao"
"lone-services/services/order/internal/model"
"lone-services/services/order/validator"
"strconv"
order "lone-services/rpc/order/pb"
"lone-services/services/order/internal/svc"
@@ -25,16 +31,67 @@ func NewItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemsLogic
}
func (l *ItemsLogic) Items(in *order.OrderItemsRequest) (*order.Response, error) {
//调用其它服务实例
//cli, err := svc.GetRpcClient(l.svcCtx.ExpressSvcName)
//if err != nil {
// l.Logger.Errorf("get express rpc err: %v", err)
// //降级逻辑
// return l.out(utils.ErrorInternalServer, "express"+utils.ErrorInternalServer.Msg)
//}
//expClient := express.NewExpressClient(cli.Conn())
//res, _ := expClient.Ping(context.Background(), &express.Request{})
//l.Logger.Error("res: %v", res)
var v validator.OrderItemsValidator
if fail := l.checkParams(in, &v); fail != nil {
return fail, nil
}
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return l.fail(utils.ErrorNoLoginInfo)
}
var info []dao.OrderInfo
w := modelbase.Params{
Page: int(in.Page),
Size: int(in.Size),
Order: "id desc",
}
w.Like = make(map[string]string)
w.Eq = make(map[string]string)
w.Other = make(map[string]string)
if in.ProductId > utils.NumberZero {
w.Other["FIND_IN_SET(?, product_ids)"] = strconv.FormatInt(in.ProductId, utils.NumberTen)
}
//Time string
if len(in.Mobile) > utils.NumberZero {
phone := utils.GetSearchPhone(in.Mobile)
if len(phone) > utils.NumberZero {
w.Like["mobile LIKE ? "] = "%%" + phone + "%%"
}
}
if len(in.OrderSn) > utils.NumberZero {
w.Like["order_sn LIKE ? "] = "%%" + in.OrderSn + "%%"
}
if in.StoreId > utils.NumberZero {
w.Eq["store_id"] = strconv.FormatInt(in.StoreId, utils.NumberTen)
}
if in.SaleId > utils.NumberZero {
w.Eq["sale_id"] = strconv.FormatInt(in.SaleId, utils.NumberTen)
}
if in.Status > utils.NumberZero {
w.Eq["status"] = string(in.Status)
}
if len(in.Time) > utils.NumberZero {
w.Other["create_time > ?"] = strconv.Itoa(int(in.Time[utils.NumberZero]))
w.Other["create_time < ?"] = strconv.Itoa(int(in.Time[utils.NumberOne]))
}
err := model.OrderModel{}.Init().Items(w, &info)
if err != nil {
l.Logger.Error(err)
return l.fail(utils.ErrorNotFund)
}
for key, v := range info {
mobile, aErr := utils.DecryptPhone(v.Mobile)
if aErr == nil {
info[key].AesMobile = v.Mobile
info[key].Mobile = utils.DecryptPhoneReplace(mobile)
}
}
return l.ok(info)
return &order.Response{}, nil
}
+144 -2
View File
@@ -2,6 +2,13 @@ package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/services/order/internal/dao"
"lone-services/services/order/internal/model"
"lone-services/services/order/validator"
"strconv"
"strings"
"lone-services/rpc/order/pb"
"lone-services/services/order/internal/svc"
@@ -25,7 +32,142 @@ func NewWebCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebCrea
}
func (l *WebCreateLogic) WebCreate(in *order.CreateOrderRequest) (*order.Response, error) {
// todo: add your logic here and delete this line
var v validator.OrderCreateValidator
if fail := l.checkParams(in, &v); fail != nil {
return fail, nil
}
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return l.fail(utils.ErrorNoLoginInfo)
}
return &order.Response{}, nil
w := modelbase.Params{
Eq: map[string]string{
"id": strconv.FormatInt(in.AddressId, utils.NumberTen),
"type": strconv.Itoa(int(in.Type)),
"target_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
},
}
var address dao.Address
err := model.AddressModel{}.Init().GetOne(w, &address)
if err != nil {
return l.fail(utils.Fail)
}
if address.Id < utils.NumberOne {
return l.out(utils.Fail, "地址不存在")
}
data := dao.OrderCreate{
Note: in.Note,
ExpiredTime: utils.GetAfterMinutes(utils.NumberThirty),
Mobile: address.Mobile,
Address: address.Address,
Addressee: address.Name,
DistrictIds: address.DistrictIds,
}
w = modelbase.Params{
Eq: map[string]string{
"type": strconv.Itoa(int(in.Type)),
},
}
var orderSn string
switch in.Type {
case dao.CartTypeSale:
data.SaleId = int(adminInfo.ID)
data.SaleName = adminInfo.Name
data.CreateType = dao.OrderTypeSale
data.Type = dao.OrderTypeSale
w.Eq["sale_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeSale), adminInfo.ID)
case dao.CartTypeUser:
data.UserId = int(adminInfo.ID)
data.UserName = adminInfo.Name
data.CreateType = dao.OrderTypePersonal
data.Type = dao.OrderTypePersonal
w.Eq["user_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypePersonal), adminInfo.ID)
case dao.CartTypeStore:
data.StoreId = int(adminInfo.ID)
data.StoreName = adminInfo.Name
data.SaleId = int(adminInfo.SaleId)
data.GroupId = int(adminInfo.GroupId)
data.SaleMobile = adminInfo.SaleMobile
data.SaleProvince = int(adminInfo.SaleProvince)
data.CreateType = dao.OrderTypeStore
data.Type = dao.OrderTypeStore
w.Eq["store_id"] = strconv.FormatInt(adminInfo.ID, utils.NumberTen)
orderSn = svc.GetOrderSn(strconv.Itoa(dao.OrderTypeStore), adminInfo.ID)
}
data.OrderSn = orderSn
var cart dao.Cart
err = model.CartModel{}.Init().GetOne(w, &address)
if err != nil {
return l.fail(utils.Fail)
}
if cart.Id < utils.NumberOne {
return l.out(utils.Fail, "购买信息不存在")
}
ids, pErr := svc.GetCartInfo(cart.Content)
if pErr != utils.Ok {
return l.fail(utils.Fail)
}
products, pErr := svc.GetCartData(ids)
if pErr != utils.Ok {
return l.fail(utils.Fail)
}
keys := make([]string, utils.NumberZero, len(products))
var totalPrice float64
for _, item := range products {
keys = append(keys, strconv.Itoa(int(item.Id)))
totalPrice += item.Price * float64(item.Number)
}
data.ProductIds = strings.Join(keys, utils.DecollatorComma)
data.TotalPrice = totalPrice
mobileObj := model.OrderModel{}.Init()
mobileObj.Begin()
err = mobileObj.Create(&data)
if err != nil {
mobileObj.Rollback()
return l.fail(utils.Fail)
}
for _, item := range products {
orderProduct := dao.OrderProductCreate{
Ordersn: data.OrderSn,
ProductId: int(item.Id),
ProductName: item.Name,
Price: item.Price,
Number: item.Number,
}
err = model.OrderProductModel{}.Init().Create(&orderProduct)
if err != nil {
mobileObj.Rollback()
return l.fail(utils.Fail)
}
_, err = model.CartModel{}.Init().Del(w, &cart)
if err != nil {
mobileObj.Rollback()
return l.fail(utils.Fail)
}
//TODO 扣除库存
//$decrease = Product::decrease(
//['id' => $item['id'], ['number', '>=', $item['number']]],
//'number',
//$item['number']
//);
//if ($decrease < 1) {
//Order::rollBack();
//$retData['code'] = Code::code('low_stocks');
//$retData['data'] = [$item['id'] => Product::value(['id' => $item['id']], 'number')];
//return $retData;
}
mobileObj.Commit()
return l.ok(utils.NumberOne)
}
+61
View File
@@ -0,0 +1,61 @@
package svc
import (
"fmt"
"lone-services/pkg/utils"
"lone-services/services/order/internal/dao"
"math/rand"
"strconv"
"time"
jsoniter "github.com/json-iterator/go"
)
func GetOrderSn(orderType string, adminId int64) string {
now := time.Now().Format("20060102150405")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := r.Intn(90000000) + 10000000
sn := fmt.Sprintf("O%s%s%d%d", now, orderType, adminId, rnd)
return sn
}
func GetCartData(info map[string]int) ([]dao.ProductInfo, utils.Status) {
keys := make([]string, utils.NumberZero, len(info))
for k := range info {
keys = append(keys, k)
}
//TODO keys 查询产品
var products []dao.ProductInfo
products = append(products, dao.ProductInfo{
Id: 1, Name: "Product 1",
Price: 100,
Number: 5,
Images: "image1.jpg",
Stock: 10})
products = append(products, dao.ProductInfo{
Id: 2, Name: "Product 2",
Price: 100.33,
Number: 2,
Images: "image1.jpg",
Stock: 1033})
return products, utils.Ok
}
func GetCartInfo(info string) (map[string]int, utils.Status) {
var list [][]int
err := jsoniter.Unmarshal([]byte(info), &list)
if err != nil {
return nil, utils.ErrorJsonDataError
}
m := make(map[string]int, len(list))
for _, item := range list {
if len(item) >= 2 {
m[strconv.Itoa(item[0])] = item[1]
}
}
return m, utils.Ok
}
+21
View File
@@ -45,11 +45,32 @@ func (p AdminOrderCreateValidator) GetMessage() validate.ValidatorMessages {
type OrderCreateValidator struct {
AddressId int64 `validate:"required"`
Note string
Type int32 `validate:"required,oneof=1 2 3"` //类型 1门店 , 2销售,3为用户
}
func (p OrderCreateValidator) GetMessage() validate.ValidatorMessages {
return validate.ValidatorMessages{
"AddressId.required": "地址ID不能为空",
"Type.required": "类型不能为空",
"Type.oneof": "类型不对",
}
}
type OrderOneCreateValidator struct {
AddressId int64 `validate:"required"`
Note string
ProductId int64 `validate:"required"`
Num int32 `validate:"required"`
Type int32 `validate:"required,oneof=1 2 3"` //类型 1门店 , 2销售,3为用户
}
func (p OrderOneCreateValidator) GetMessage() validate.ValidatorMessages {
return validate.ValidatorMessages{
"AddressId.required": "地址ID不能为空",
"Type.required": "类型不能为空",
"Type.oneof": "类型不对",
"ProductId.required": "产品不能为空",
"Num.required": "产品数量不能为空",
}
}