This commit is contained in:
laodaming 2023-06-27 17:04:58 +08:00
parent 9050b4ab0e
commit 66ed487249
12 changed files with 390 additions and 9 deletions

7
constants/paging.go Normal file
View File

@ -0,0 +1,7 @@
package constants
// 分页默认当前页
const DEFAULT_PAGE = 1
// 默认每页数量
const DEFAULT_PAGE_SIZE = 20

View File

@ -23,7 +23,23 @@ func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []i
}
return
}
func (p *FsProductModel) GetProductListByIdsWithoutStatus(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) {
if len(productIds) == 0 {
return
}
db := p.db.Model(&FsProduct{}).WithContext(ctx).
Where("`id` in (?) ", productIds)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, sort string) (resp []FsProduct, err error) {
if len(productTypes) == 0 {
return

View File

@ -19,6 +19,17 @@ func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, fi
err = db.Find(&resp).Error
return resp, err
}
func (d *FsProductModel3dModel) GetAllByIdsWithoutStatus(ctx context.Context, ids []int64, fields ...string) (resp []FsProductModel3d, err error) {
if len(ids) == 0 {
return
}
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?)", ids)
if len(fields) > 0 {
db = db.Select(fields[0])
}
err = db.Find(&resp).Error
return resp, err
}
func (d *FsProductModel3dModel) GetAllByIdsTag(ctx context.Context, ids []int64, tag int64) (resp []FsProductModel3d, err error) {
if len(ids) == 0 {
return

View File

@ -32,6 +32,9 @@ func (s *FsProductSizeModel) CountByStatus(ctx context.Context, status int) (tot
return
}
func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, sort string) (resp []FsProductSize, err error) {
if len(productIds) == 0 {
return nil, nil
}
db := s.db.WithContext(ctx).Model(&FsProductSize{}).Where("`product_id` in(?) and `status` = ?", productIds, 1)
switch sort {
case "sort-asc":
@ -45,6 +48,23 @@ func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds
}
return
}
func (s *FsProductSizeModel) GetAllByProductIdsWithoutStatus(ctx context.Context, productIds []int64, sort string) (resp []FsProductSize, err error) {
if len(productIds) == 0 {
return nil, nil
}
db := s.db.WithContext(ctx).Model(&FsProductSize{}).Where("`product_id` in(?)", productIds)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
err = db.Find(&resp).Error
if err != nil {
return nil, err
}
return
}
type CapacityId struct {
Id int64 `json:"id"`

View File

@ -24,6 +24,16 @@ func (t *FsProductTemplateV2Model) FindAllByIds(ctx context.Context, ids []int64
}
return
}
func (t *FsProductTemplateV2Model) FindAllByIdsWithoutStatus(ctx context.Context, ids []int64) (resp []FsProductTemplateV2, err error) {
if len(ids) == 0 {
return
}
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Where("`id` in (?) ", ids).Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (t *FsProductTemplateV2Model) FindOne(ctx context.Context, id int64) (resp *FsProductTemplateV2, err error) {
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Where("`id` = ? ", id).Find(&resp).Error

View File

@ -3,19 +3,37 @@ package gmodel
import "context"
// TODO: 使用model的属性做你想做的
type GetStockListReq struct {
UserId int64
Ids []int64
Status *int64
Page int
Limit int
}
func (s *FsUserStockModel) GetUserAllStockByIds(ctx context.Context, ids []int64, userId int64) (resp []FsUserStock, err error) {
if len(ids) == 0 {
return nil, nil
func (s *FsUserStockModel) GetStockList(ctx context.Context, req GetStockListReq) (resp []FsUserStock, total int64, err error) {
db := s.db.WithContext(ctx).Model(&FsUserStock{})
if req.UserId > 0 {
db = db.Where("`user_id` = ?", req.UserId)
}
if userId <= 0 {
return nil, nil
if len(req.Ids) > 0 {
db = db.Where("`id` in (?)", req.Ids)
}
err = s.db.WithContext(ctx).Model(&FsUserStock{}).Where("`id` in (?) and `user_id` = ?", ids, userId).Find(&resp).Error
return resp, err
if req.Status != nil {
db = db.Where("`status` = ?", *req.Status)
}
if err = db.Limit(1).Count(&total).Error; err != nil {
return nil, 0, err
}
offset := (req.Page - 1) * req.Limit
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
if err != nil {
return nil, 0, err
}
return
}
func (s *FsUserStockModel) FindOne(ctx context.Context, id int64, userId int64, lock ...bool) (resp *FsUserStock, err error) {
db := s.db.WithContext(ctx).Model(&FsUserStock{}).Where("`id` = ? and `user_id` = ?", id, userId)
db := s.db.WithContext(ctx).Model(&FsUserStock{}).Where("`id` = ? and `user_id` = ? and `status` = ?", id, userId, 1)
if len(lock) != 0 && lock[0] {
db = db.Set("gorm:query_option", "FOR UPDATE")
}

View File

@ -0,0 +1,78 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/inventory/internal/logic"
"fusenapi/server/inventory/internal/svc"
"fusenapi/server/inventory/internal/types"
)
func GetCloudListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.UserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
if claims != nil {
// 从token中获取对应的用户信息
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
} else {
// 如果claims为nil,则认为用户身份为白板用户
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
}
var req types.GetCloudListReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetCloudListLogic(r.Context(), svcCtx)
resp := l.GetCloudList(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/inventory/take",
Handler: TakeHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/inventory/list",
Handler: GetCloudListHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,123 @@
package logic
import (
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/inventory/internal/svc"
"fusenapi/server/inventory/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCloudListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCloudListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCloudListLogic {
return &GetCloudListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCloudListLogic) GetCloudList(req *types.GetCloudListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
}
if req.Page <= 0 {
req.Page = constants.DEFAULT_PAGE
}
if req.PageSize <= 0 || req.PageSize > 200 {
req.Page = constants.DEFAULT_PAGE_SIZE
}
//获取个人云仓列表
stockList, total, err := l.svcCtx.AllModels.FsUserStock.GetStockList(l.ctx, gmodel.GetStockListReq{
UserId: userinfo.UserId,
Page: int(req.Page),
Limit: int(req.PageSize),
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user stock list")
}
if len(stockList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCloudListRsp{
ListData: []types.ListDataItem{},
})
}
designIds := make([]int64, 0, len(stockList))
for _, v := range stockList {
designIds = append(designIds, *v.DesignId)
}
//获取设计数据
productDesignList, err := l.svcCtx.AllModels.FsProductDesign.GetAllByIds(l.ctx, designIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product design list")
}
//尺寸ids
sizeIds := make([]int64, 0, len(productDesignList))
//产品ids
productIds := make([]int64, 0, len(productDesignList))
//模板ids
templateIds := make([]int64, 0, len(productDesignList))
//配件ids
optionalIds := make([]int64, 0, len(productDesignList))
mapProductDesign := make(map[int64]int)
for k, v := range productDesignList {
sizeIds = append(sizeIds, *v.SizeId)
productIds = append(productIds, *v.ProductId)
templateIds = append(templateIds, *v.TemplateId)
optionalIds = append(optionalIds, *v.OptionalId)
mapProductDesign[v.Id] = k
}
//获取尺寸信息
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIdsWithoutStatus(l.ctx, sizeIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product size list")
}
mapSize := make(map[int64]int)
for k, v := range sizeList {
mapSize[v.Id] = k
}
//获取产品信息
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIdsWithoutStatus(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list")
}
mapProduct := make(map[int64]int)
for k, v := range productList {
mapProduct[v.Id] = k
}
//获取模板信息
productTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIdsWithoutStatus(l.ctx, templateIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template list")
}
mapTemplate := make(map[int64]int)
for k, v := range productTemplateList {
mapTemplate[v.Id] = k
}
//获取配件列表
productModel3dList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsWithoutStatus(l.ctx, optionalIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list")
}
mapProductModel := make(map[int64]int)
for k, v := range productModel3dList {
mapProductModel[v.Id] = k
}
return resp.SetStatus(basic.CodeOK)
}

View File

@ -15,6 +15,45 @@ type TakeForm struct {
Num int64 `json:"num"`
}
type GetCloudListReq struct {
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
Size int64 `json:"size"`
}
type GetCloudListRsp struct {
WarehouseBoxes int64 `json:"warehouse_boxes"`
TransitBoxes int64 `json:"transit_boxes"`
MinTakeNum int64 `json:"minTakeNum"`
ListData []ListDataItem `json:"listData"`
Pagnation Pagnation `json:"pagnation"`
}
type ListDataItem struct {
Id int64 `json:"id"`
Sn string `json:"sn"`
Cover string `json:"cover"`
Name string `json:"name"`
DesignSn string `json:"design_sn"`
Fitting string `json:"fitting"`
Production int64 `json:"production"`
ProductionBox int64 `json:"production_box"`
EachBoxNum int64 `json:"each_box_num"`
Stick int64 `json:"stick"`
StickBox int64 `json:"stick_box"`
Type int64 `json:"type"`
TakeNum int64 `json:"takeNum"`
Size string `json:"size"`
IsStop int64 `json:"is_stop"`
PriceList []PriceItem `json:"price"`
}
type PriceItem struct {
Num int `json:"num"`
TotalNum int `json:"total_num"`
Price int `json:"price"`
}
type Request struct {
}
@ -30,6 +69,13 @@ type Auth struct {
RefreshAfter int64 `json:"refreshAfter"`
}
type Pagnation struct {
TotalCount int64 `json:"total_count"`
TotalPage int64 `json:"total_page"`
CurPage int64 `json:"cur_page"`
PageSize int64 `json:"page_size"`
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{

View File

@ -25,3 +25,11 @@ type Auth {
RefreshAfter int64 `json:"refreshAfter"`
}
// 统一分页
type Pagnation{
TotalCount int64 `json:"total_count"`
TotalPage int64 `json:"total_page"`
CurPage int64 `json:"cur_page"`
PageSize int64 `json:"page_size"`
}

View File

@ -12,6 +12,9 @@ service inventory {
//提取云仓货物
@handler TakeHandler
post /inventory/take(TakeReq) returns (response);
//获取云仓库存列表
@handler GetCloudListHandler
get /inventory/list(GetCloudListReq) returns (response);
}
//提取云仓货物
@ -22,4 +25,40 @@ type TakeReq {
type TakeForm {
Id int64 `json:"id"`
Num int64 `json:"num"`
}
//获取云仓库存列表
type GetCloudListReq {
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
Size int64 `json:"size"`
}
type GetCloudListRsp {
WarehouseBoxes int64 `json:"warehouse_boxes"`
TransitBoxes int64 `json:"transit_boxes"`
MinTakeNum int64 `json:"minTakeNum"`
ListData []ListDataItem `json:"listData"`
Pagnation Pagnation `json:"pagnation"`
}
type ListDataItem {
Id int64 `json:"id"`
Sn string `json:"sn"`
Cover string `json:"cover"`
Name string `json:"name"`
DesignSn string `json:"design_sn"`
Fitting string `json:"fitting"`
Production int64 `json:"production"`
ProductionBox int64 `json:"production_box"`
EachBoxNum int64 `json:"each_box_num"`
Stick int64 `json:"stick"`
StickBox int64 `json:"stick_box"`
Type int64 `json:"type"`
TakeNum int64 `json:"takeNum"`
Size string `json:"size"`
IsStop int64 `json:"is_stop"`
PriceList []PriceItem `json:"price"`
}
type PriceItem {
Num int `json:"num"`
TotalNum int `json:"total_num"`
Price int `json:"price"`
}