package v1
|
|
import (
|
"aps_crm/model"
|
"aps_crm/model/grpc_init"
|
"aps_crm/model/request"
|
"aps_crm/model/response"
|
"aps_crm/pkg/contextx"
|
"aps_crm/pkg/ecode"
|
"aps_crm/pkg/logx"
|
"aps_crm/proto/product"
|
"github.com/gin-gonic/gin"
|
"github.com/shopspring/decimal"
|
"github.com/spf13/cast"
|
)
|
|
type ProductApi struct{}
|
|
//var (
|
// productServiceConn *grpc.ClientConn
|
//)
|
//
|
//func InitProductServiceConn() {
|
// var err error
|
// productServiceConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Aps, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
// if err != nil {
|
// logx.Errorf("grpc dial product service error: %v", err.Error())
|
// return
|
// }
|
//}
|
//
|
//func CloseProductServiceConn() {
|
// if productServiceConn != nil {
|
// productServiceConn.Close()
|
// }
|
//}
|
|
// List
|
//
|
// @Tags 产品
|
// @Summary 获取产品列表
|
// @Produce application/json
|
// @Param object query request.GetProductList true "参数"
|
// @Success 200 {object} response.ListResponse{data=[]product.Product}
|
//
|
// @Router /api/product/list [get]
|
func (ci *ProductApi) List(c *gin.Context) {
|
var params request.GetProductList
|
ctx, ok := contextx.NewContext(c, ¶ms)
|
if !ok {
|
return
|
}
|
if !params.Check() {
|
ctx.FailWithMsg(ecode.ParamsErr, "page, pageSize超出范围")
|
return
|
}
|
|
if params.QuotationNumber != "" {
|
find, err := model.NewQuotationSearch(nil).SetNumber(params.QuotationNumber).Find()
|
if err != nil {
|
ctx.FailWithMsg(ecode.UnknownErr, "报价单信息查询失败")
|
return
|
}
|
products := find.Products
|
ctx.OkWithDetailed(response.ListResponse{
|
Data: products,
|
Count: int64(len(products)),
|
})
|
return
|
}
|
cli := product.NewProductServiceClient(grpc_init.CrmApsGrpcServiceConn)
|
getProductListResponse, err := cli.GetProductList(ctx.GetCtx(), &product.GetProductListRequest{
|
Page: cast.ToInt32(params.Page),
|
PageSize: cast.ToInt32(params.PageSize),
|
ProductNumber: params.ProductNumber,
|
ProductName: params.ProductName,
|
})
|
if err != nil {
|
logx.Errorf("grpc GetProductList err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
if getProductListResponse.Code != 0 {
|
logx.Errorf("grpc GetProductList err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
rawProductList := getProductListResponse.List
|
productList := make([]model.Product, len(rawProductList))
|
|
for k, v := range rawProductList {
|
productList[k].Number = v.Number
|
productList[k].Name = v.Name
|
productList[k].Price = decimal.NewFromFloat(v.SalePrice).Round(2)
|
productList[k].Amount = decimal.NewFromFloat(1)
|
productList[k].Total = productList[k].Price.Mul(productList[k].Amount).Round(2)
|
productList[k].Unit = v.Unit
|
productList[k].Type = v.Type
|
productList[k].Specs = v.Specs
|
productList[k].Price = decimal.NewFromFloat(v.SalePrice)
|
}
|
|
ctx.OkWithDetailed(response.ListResponse{
|
Data: productList,
|
Count: getProductListResponse.Total,
|
})
|
}
|
|
// Info
|
// @Tags 产品
|
// @Summary 获取产品详情
|
// @Produce application/json
|
// @Param productNumber query string true "参数"
|
// @Success 200 {object} contextx.Response{data=product.Product} "成功"
|
// @Router /api/product/info [get]
|
func (ci *ProductApi) Info(c *gin.Context) {
|
ctx, ok := contextx.NewContext(c, nil)
|
if !ok {
|
return
|
}
|
|
// 获取产品ID
|
productId := c.Query("productNumber")
|
cli := product.NewProductServiceClient(grpc_init.CrmApsGrpcServiceConn)
|
|
getProductInfoResponse, err := cli.GetProductInfo(ctx.GetCtx(), &product.GetProductInfoRequest{ProductId: productId})
|
if err != nil {
|
logx.Errorf(" grpc GetProductInfo err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
if getProductInfoResponse.Code != 0 {
|
logx.Errorf("grpc GetProductInfo err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
ctx.OkWithDetailed(getProductInfoResponse.Data)
|
}
|
|
// GetProductOrderInfo
|
//
|
// @Tags 产品
|
// @Summary 获取产品订单信息
|
// @Produce application/json
|
// @Param number path string true "明细编码"
|
// @Success 200 {object} contextx.Response{data=response.Info} "成功"
|
//
|
// @Router /api/product/getProductOrderInfo/{number} [get]
|
func (ci *ProductApi) GetProductOrderInfo(c *gin.Context) {
|
ctx, ok := contextx.NewContext(c, nil)
|
if !ok {
|
return
|
}
|
number := c.Param("number")
|
if number == "" || number == "undefined" {
|
ctx.Ok()
|
return
|
}
|
client := product.NewProductServiceClient(grpc_init.CrmApsGrpcServiceConn)
|
info, err := client.GetProductOrder(ctx.GetCtx(), &product.GetProductOrderRequest{SalesDetailsNumber: number})
|
if err != nil {
|
logx.Errorf("grpc GetProductOrder err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
var result response.Info
|
var list []response.WorkOrderInfo
|
for _, orderInfo := range info.List {
|
var wo response.WorkOrderInfo
|
wo.OrderId = orderInfo.OrderId
|
wo.ProductName = orderInfo.ProductName
|
wo.OrderStatus = orderInfo.OrderStatus
|
wo.WorkOrderId = orderInfo.WorkOrderId
|
wo.WorkOrderStatus = orderInfo.WorkOrderStatus
|
wo.StartTime = orderInfo.StartTime
|
wo.EndTime = orderInfo.EndTime
|
list = append(list, wo)
|
}
|
result.MakeInfo = list
|
var purchaseInfo []response.Purchase
|
for _, pl := range info.PurchaseList {
|
var p response.Purchase
|
p.Amount = pl.Amount
|
p.Status = pl.Status
|
p.PurchaseName = pl.PurchaseName
|
p.PurchaseNumber = pl.PurchaseNumber
|
p.SupplierName = pl.SupplierName
|
purchaseInfo = append(purchaseInfo, p)
|
}
|
result.PurchaseInfo = purchaseInfo
|
ctx.OkWithDetailed(result)
|
}
|