package purchase
|
|
import (
|
"context"
|
"github.com/gin-gonic/gin"
|
"github.com/shopspring/decimal"
|
"go.uber.org/zap"
|
"gorm.io/gorm"
|
"srm/global"
|
"srm/model/common/response"
|
"srm/model/purchase"
|
purchaserequest "srm/model/purchase/request"
|
"srm/proto/purchase_wms"
|
"strconv"
|
"strings"
|
//"srm/model/purchase"
|
|
//"srm/model/purchase"
|
purchaseRes "srm/model/purchase/response"
|
service "srm/service/purchase"
|
"srm/utils"
|
)
|
|
type PurchaseApi struct{}
|
|
// CreatePurchase
|
// @Tags Purchase
|
// @Summary 创建采购单
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.AddPurchase true "采购单,采购单产品"
|
// @Success 200 {object} response.Response{msg=string} "创建采购单"
|
// @Router /purchase/purchase [post]
|
func (e *PurchaseApi) CreatePurchase(c *gin.Context) {
|
var params purchaserequest.AddPurchase
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
global.GVA_LOG.Error("Add Purchase failed", zap.Error(err))
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
var purchaseRecord purchase.Purchase
|
if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil {
|
global.GVA_LOG.Error("Add Purchase failed", zap.Error(err))
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
purchaseRecord.ID = 0
|
purchaseRecord.Status = purchase.OrderStatusConfirmed
|
purchaseRecord.HandledBy = "admin"
|
purchaseRecord.Creator = "admin"
|
purchaseRecord.OrderType = "采购订单"
|
|
if !purchaseRecord.WholeDiscountType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.WholeDiscount) {
|
response.FailWithMessage("整单折扣数值不正确", c)
|
return
|
}
|
|
if !purchaseRecord.PriceAdjustmentType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.PriceAdjustment) {
|
response.FailWithMessage("价格调整数值不正确", c)
|
return
|
}
|
|
err = service.NewPurchaseService().CreatePurchase(&purchaseRecord, params.ProductList)
|
|
if err != nil {
|
if err == gorm.ErrDuplicatedKey || strings.Contains(err.Error(), "Duplicate entry") {
|
response.FailWithMessage("编号重复", c)
|
return
|
}
|
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
response.OkWithMessage("创建成功", c)
|
}
|
|
// DeletePurchase
|
// @Tags Purchase
|
// @Summary 删除采购单
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param id path int true "采购单ID"
|
// @Success 200 {object} response.Response{msg=string} "删除采购单"
|
// @Router /purchase/purchase/{id} [delete]
|
func (e *PurchaseApi) DeletePurchase(c *gin.Context) {
|
id, _ := strconv.Atoi(c.Param("id"))
|
if id == 0 {
|
response.FailWithMessage("参数缺失", c)
|
return
|
}
|
err := service.NewPurchaseService().DeletePurchase(uint(id))
|
if err != nil {
|
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
response.FailWithMessage("删除失败", c)
|
return
|
}
|
response.OkWithMessage("删除成功", c)
|
}
|
|
// UpdatePurchase
|
// @Tags Purchase
|
// @Summary 更新采购单信息
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.UpdatePurchase true "采购单ID, 采购单信息"
|
// @Success 200 {object} response.Response{msg=string} "更新采购单信息"
|
// @Router /purchase/purchase [put]
|
func (e *PurchaseApi) UpdatePurchase(c *gin.Context) {
|
var params purchaserequest.UpdatePurchase
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
var purchaseRecord purchase.Purchase
|
if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
purchaseRecord.HandledBy = "admin"
|
purchaseRecord.Creator = "admin"
|
err = service.NewPurchaseService().UpdatePurchase(&purchaseRecord, params.ProductList)
|
if err != nil {
|
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
response.FailWithMessage("更新失败", c)
|
return
|
}
|
response.OkWithMessage("更新成功", c)
|
}
|
|
// GetPurchase
|
// @Tags Purchase
|
// @Summary 获取单一采购单信息
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param id path int true "采购单ID" true "采购单ID"
|
// @Success 200 {object} response.Response{data=purchaseRes.PurchaseResponse,msg=string} "获取单一采购单信息,返回包括采购单详情"
|
// @Router /purchase/purchase/{id} [get]
|
func (e *PurchaseApi) GetPurchase(c *gin.Context) {
|
id, _ := strconv.Atoi(c.Param("id"))
|
if id == 0 {
|
response.FailWithMessage("参数缺失", c)
|
return
|
}
|
data, err := service.NewPurchaseService().GetPurchase(uint(id))
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
productList, err := service.NewPurchaseService().GetPurchaseProductList(uint(id))
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
respProductList := make([]*purchaseRes.PurchaseProducts, len(productList))
|
err = utils.AssignTo(productList, &respProductList)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
for k, item := range productList {
|
respProductList[k].Amount = item.Amount
|
respProductList[k].Price = item.Price
|
respProductList[k].Total = item.Total
|
err = utils.AssignTo(item.Product, &respProductList[k])
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
}
|
|
response.OkWithDetailed(purchaseRes.PurchaseResponse{Purchase: data, ProductList: respProductList}, "获取成功", c)
|
}
|
|
// GetPurchaseList
|
// @Tags Purchase
|
// @Summary 分页获取采购单列表
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data query purchaserequest.PurchaseSearch true "参数"
|
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页获取采购单列表,返回包括列表,总数,页码,每页数量"
|
// @Router /purchase/purchaseList [get]
|
func (e *PurchaseApi) GetPurchaseList(c *gin.Context) {
|
var pageInfo purchaserequest.PurchaseSearch
|
err := c.ShouldBindQuery(&pageInfo)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
err = utils.Verify(pageInfo, utils.PageInfoVerify)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
purchaseList, total, err := service.NewPurchaseService().GetPurchaseList(pageInfo)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败"+err.Error(), c)
|
return
|
}
|
response.OkWithDetailed(response.PageResult{
|
List: purchaseList,
|
Total: total,
|
Page: pageInfo.Page,
|
PageSize: pageInfo.PageSize,
|
}, "获取成功", c)
|
}
|
|
// Submit
|
// @Tags Purchase
|
// @Summary 提交采购单
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.SubmitPurchase true "参数"
|
// @Success 200 {object} response.Response{msg=string} "提交采购单"
|
// @Router /purchase/submit [post]
|
func (e *PurchaseApi) Submit(c *gin.Context) {
|
var params purchaserequest.SubmitPurchase
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
global.GVA_LOG.Error("Submit failed", zap.Error(err))
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
warehouse := ""
|
if params.Status == purchase.OrderStatusReceived {
|
data, err := service.NewPurchaseService().GetPurchase(uint(params.Id))
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
productList, err := service.NewPurchaseService().GetPurchaseProductList(uint(params.Id))
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
product := make([]*purchase_wms.PurchaseProduct, 0)
|
for _, products := range productList {
|
var p purchase_wms.PurchaseProduct
|
p.Id = products.Product.Number
|
p.Amount = products.Amount.IntPart()
|
product = append(product, &p)
|
}
|
client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
|
resp, err := client.PurchaseToWms(context.Background(), &purchase_wms.PurchaseToWmsRequest{
|
Number: data.Number,
|
SupplierName: data.Supplier.Name,
|
Product: product,
|
Source: "SRM_PURCHASE",
|
OperationSource: purchase_wms.OperationSource_OperationSourcePurchase,
|
})
|
if err != nil {
|
global.GVA_LOG.Error("grpc调用失败!", zap.Error(err))
|
response.FailWithMessage("grpc调用失败", c)
|
return
|
}
|
warehouse = resp.Warehouse
|
}
|
|
err = service.NewPurchaseService().Submit(params.Id, params.Status, warehouse)
|
if err != nil {
|
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
response.FailWithMessage("更新失败", c)
|
return
|
}
|
response.OkWithMessage("更新成功", c)
|
}
|
|
// SavePurchaseType
|
// @Tags Purchase
|
// @Summary 创建采购类型
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body []purchaserequest.PurchaseType true "采购类型list"
|
// @Success 200 {object} response.Response{msg=string} "创建采购类型"
|
// @Router /purchase/purchaseType [post]
|
func (e *PurchaseApi) SavePurchaseType(c *gin.Context) {
|
var params []*purchaserequest.PurchaseType
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
purchaseTypeList := make([]*purchase.PurchaseType, 0, len(params))
|
if err := utils.AssignTo(params, &purchaseTypeList); err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
err = service.NewPurchaseService().SavePurchaseType(purchaseTypeList)
|
|
if err != nil {
|
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
response.FailWithMessage("创建失败", c)
|
return
|
}
|
response.OkWithMessage("创建成功", c)
|
}
|
|
// GetPurchaseTypeList
|
// @Tags Purchase
|
// @Summary 获取采购类型列表
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Success 200 {object} response.Response{data=[]purchase.PurchaseType,msg=string} "获取采购类型列表"
|
// @Router /purchase/purchaseTypeList [get]
|
func (e *PurchaseApi) GetPurchaseTypeList(c *gin.Context) {
|
list, err := service.NewPurchaseService().GetPurchaseTypeList()
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败"+err.Error(), c)
|
return
|
}
|
response.OkWithDetailed(list, "获取成功", c)
|
}
|
|
// GetWarehouseInfo
|
// @Tags Purchase
|
// @Summary 获取仓库列表
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Success 200 {object} response.Response{data=[]purchase_wms.SrmGetWarehouseInfoResponse} "获取采购类型列表"
|
// @Router /purchase/getWarehouseInfo [get]
|
func (e *PurchaseApi) GetWarehouseInfo(c *gin.Context) {
|
client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
|
info, err := client.SrmGetWarehouseInfo(context.Background(), &purchase_wms.SrmGetWarehouseInfoRequest{})
|
if err != nil {
|
global.GVA_LOG.Error("grpc调用失败!", zap.Error(err))
|
response.FailWithMessage("grpc调用失败", c)
|
return
|
}
|
response.OkWithData(info, c)
|
}
|
|
// GetOperationInfo
|
// @Tags Purchase
|
// @Summary 获取操作信息
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Success 200 {object} response.Response{data=[]purchaseRes.OperationInfo} "获取操作信息"
|
// @Router /purchase/getOperationInfo/{id} [get]
|
func (e *PurchaseApi) GetOperationInfo(c *gin.Context) {
|
id, _ := strconv.Atoi(c.Param("id"))
|
if id == 0 {
|
response.FailWithMessage("参数缺失", c)
|
return
|
}
|
server := service.NewPurchaseService()
|
data, err := server.GetPurchase(uint(id))
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
productList, err := server.GetPurchaseProductList(uint(id))
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
confirmInfo, err := server.GetPurchaseProductConfirmInfo(data.Number)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
|
info, err := client.SrmGetOperationInfo(context.Background(), &purchase_wms.SrmGetOperationInfoRequest{Number: data.Number})
|
if err != nil {
|
global.GVA_LOG.Error("grpc调用失败!", zap.Error(err))
|
response.FailWithMessage("grpc调用失败", c)
|
return
|
}
|
|
var operationInfos purchaseRes.OperationInfo
|
productInfos := make([]purchaseRes.ProductInfo, 0)
|
inWarehouseInfos := make([]purchaseRes.InWarehouseInfo, 0)
|
productMap := make(map[string]*purchaseRes.ProductInfo)
|
if len(info.Operations) > 0 {
|
for _, operation := range info.Operations {
|
var pi purchaseRes.ProductInfo
|
var iwi purchaseRes.InWarehouseInfo
|
if p, ok := productMap[operation.ProductId]; ok {
|
pi = *p
|
}
|
pi.Number = operation.ProductId
|
pi.Name = operation.ProductName
|
iwi.Number = operation.ProductId
|
iwi.Name = operation.ProductName
|
iwi.OperationNumber = operation.Number
|
iwi.Status = operation.Status
|
iwi.Principal = data.Principal
|
iwi.WarehouseName = data.Warehouse
|
if operation.OverTime != "" {
|
iwi.OverTime = operation.OverTime
|
iwi.OverAmount = operation.Amount
|
pi.OverAmount = pi.OverAmount + operation.Amount
|
}
|
for _, products := range productList {
|
if products.Product.Number == operation.ProductId {
|
pi.Amount = products.Amount
|
pi.PurchasePrice = products.Price
|
pi.Total = products.Total
|
pi.Unit = products.Product.Unit
|
pi.Specifications = products.Product.Specifications
|
pi.ModelNumber = products.Product.ModelNumber
|
break
|
}
|
}
|
for _, confirm := range confirmInfo {
|
if operation.ProductId == confirm.ProductId {
|
pi.SendAmount = confirm.OverReceiveAmount.IntPart()
|
break
|
}
|
}
|
inWarehouseInfos = append(inWarehouseInfos, iwi)
|
productMap[operation.ProductId] = &pi
|
}
|
} else {
|
for _, products := range productList {
|
var pi purchaseRes.ProductInfo
|
pi.Number = products.Product.Number
|
pi.Name = products.Product.Name
|
pi.Amount = products.Amount
|
pi.PurchasePrice = products.Price
|
pi.Total = products.Total
|
pi.Unit = products.Product.Unit
|
pi.Specifications = products.Product.Specifications
|
pi.ModelNumber = products.Product.ModelNumber
|
for _, confirm := range confirmInfo {
|
if products.Product.Number == confirm.ProductId {
|
pi.SendAmount = confirm.OverReceiveAmount.IntPart()
|
break
|
}
|
}
|
productMap[pi.Number] = &pi
|
}
|
}
|
for _, productInfo := range productMap {
|
productInfos = append(productInfos, *productInfo)
|
}
|
operationInfos.InWarehouseInfos = inWarehouseInfos
|
operationInfos.ProductInfos = productInfos
|
|
response.OkWithData(operationInfos, c)
|
}
|
|
// NewSubmit
|
// @Tags Purchase
|
// @Summary 新版提交
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.SubmitPurchase true "参数"
|
// @Success 200 {object} response.Response{msg=string} "新版提交"
|
// @Router /purchase/newSubmit [post]
|
func (e *PurchaseApi) NewSubmit(c *gin.Context) {
|
var params purchaserequest.SubmitPurchase
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
global.GVA_LOG.Error("Submit failed", zap.Error(err))
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
id := uint(params.Id)
|
if id == 0 {
|
response.FailWithMessage("参数缺失", c)
|
return
|
}
|
if params.Status == purchase.OrderStatusCanceled {
|
err = service.NewPurchaseService().Submit(params.Id, params.Status, "")
|
if err != nil {
|
global.GVA_LOG.Error("提交失败!", zap.Error(err))
|
response.FailWithMessage("提交失败", c)
|
return
|
}
|
response.OkWithMessage("提交成功", c)
|
return
|
}
|
data, err := service.NewPurchaseService().GetPurchase(id)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
productList, err := service.NewPurchaseService().GetPurchaseProductList(id)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
list := make([]*purchase.PurchaseProductConfirm, 0)
|
for _, products := range productList {
|
var ppc purchase.PurchaseProductConfirm
|
ppc.PurchaseNumber = data.Number
|
ppc.Principal = data.Principal
|
ppc.ProductId = products.Product.Number
|
ppc.ProductName = products.Product.Name
|
ppc.Unit = products.Product.Unit
|
ppc.Specs = products.Product.Specifications
|
ppc.Type = products.Product.ModelNumber
|
ppc.Amount = products.Amount
|
ppc.OverReceiveAmount = decimal.NewFromInt(0)
|
ppc.NotReceiveAmount = products.Amount
|
ppc.NowReceiveAmount = decimal.NewFromInt(0)
|
ppc.SurplusReceiveAmount = products.Amount
|
list = append(list, &ppc)
|
}
|
err = service.NewPurchaseService().SavePurchaseProductConfirm(list, true)
|
|
if err != nil {
|
global.GVA_LOG.Error("提交失败!", zap.Error(err))
|
response.FailWithMessage("提交失败", c)
|
return
|
}
|
response.OkWithMessage("提交成功", c)
|
}
|
|
// GetPurchaseProductConfirmInfo
|
// @Tags Purchase
|
// @Summary 获取确认信息
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param number path string true "采购单编码"
|
// @Success 200 {object} response.Response{data=[]purchase.PurchaseProductConfirm} "获取确认信息"
|
// @Router /purchase/getPurchaseProductConfirmInfo/{number} [get]
|
func (e *PurchaseApi) GetPurchaseProductConfirmInfo(c *gin.Context) {
|
number := c.Param("number")
|
if number == "" {
|
response.FailWithMessage("参数缺失", c)
|
return
|
}
|
info, err := service.NewPurchaseService().GetPurchaseProductConfirmInfo(number)
|
if err != nil {
|
global.GVA_LOG.Error("获取确认信息失败!", zap.Error(err))
|
response.FailWithMessage("获取确认信息失败", c)
|
return
|
}
|
response.OkWithData(info, c)
|
}
|
|
// SavePurchaseProductConfirm
|
// @Tags Purchase
|
// @Summary 确认收货
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body []purchaserequest.PurchaseProductConfirmInfo true "list"
|
// @Success 200 {object} response.Response{msg=string} "确认收货"
|
// @Router /purchase/savePurchaseProductConfirm [post]
|
func (e *PurchaseApi) SavePurchaseProductConfirm(c *gin.Context) {
|
var params []*purchaserequest.PurchaseProductConfirmInfo
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
list := make([]*purchase.PurchaseProductConfirm, 0, len(params))
|
if err := utils.AssignTo(params, &list); err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
server := service.NewPurchaseService()
|
err = server.SavePurchaseProductConfirm(list, false)
|
|
if err != nil {
|
global.GVA_LOG.Error("保存失败!", zap.Error(err))
|
response.FailWithMessage("保存失败", c)
|
return
|
}
|
_, err = server.SavePurchaseQualityInspection(list)
|
if err != nil {
|
global.GVA_LOG.Error("保存失败!", zap.Error(err))
|
response.FailWithMessage("保存失败", c)
|
return
|
}
|
response.OkWithMessage("保存成功", c)
|
}
|
|
// GetPurchaseQualityInspectionInfo
|
// @Tags Purchase
|
// @Summary 获取采购质检信息信息
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.GetQualityInspectionInfo true "参数"
|
// @Success 200 {object} response.Response{data=[]purchase.PurchaseProductConfirm} "获取确认信息"
|
// @Router /purchase/getPurchaseQualityInspectionInfo [post]
|
func (e *PurchaseApi) GetPurchaseQualityInspectionInfo(c *gin.Context) {
|
var params purchaserequest.GetQualityInspectionInfo
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if params.PurchaseNumber == "" {
|
response.FailWithMessage("采购单编码不能为空", c)
|
return
|
}
|
infos, err := service.NewPurchaseService().GetPurchaseQualityInspection(params)
|
if err != nil {
|
global.GVA_LOG.Error("获取采购质检信息信息!", zap.Error(err))
|
response.FailWithMessage("获取采购质检信息信息", c)
|
return
|
}
|
response.OkWithData(infos, c)
|
}
|
|
// SavePurchaseQualityInspectionInfo
|
// @Tags Purchase
|
// @Summary 保存采购质检信息信息
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.SaveQualityInspectionInfo true "参数"
|
// @Success 200 {object} response.Response{data=[]purchase.PurchaseProductConfirm} "获取确认信息"
|
// @Router /purchase/savePurchaseQualityInspectionInfo [post]
|
func (e *PurchaseApi) SavePurchaseQualityInspectionInfo(c *gin.Context) {
|
var params purchaserequest.SaveQualityInspectionInfo
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
if params.PurchaseId == 0 || len(params.Ids) == 0 {
|
response.FailWithMessage("采购单编码和id不能为空", c)
|
return
|
}
|
server := service.PurchaseService{}
|
purchaseData, err := server.GetPurchase(params.PurchaseId)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
if params.Status == purchase.Unqualified {
|
err := server.UpdatePurchaseQualityInspection(params.Ids, params.Status)
|
if err != nil {
|
global.GVA_LOG.Error("质检失败!", zap.Error(err))
|
response.FailWithMessage("质检失败", c)
|
return
|
}
|
} else if params.Status == purchase.InWarehouse {
|
inspectionList, err := server.GetPurchaseQualityInspectionList(params.Ids)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
product := make([]*purchase_wms.PurchaseProduct, 0)
|
for _, inspection := range inspectionList {
|
var p purchase_wms.PurchaseProduct
|
p.Id = inspection.ProductId
|
p.Amount = inspection.Amount.IntPart()
|
product = append(product, &p)
|
}
|
client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
|
_, err = client.PurchaseToWms(context.Background(), &purchase_wms.PurchaseToWmsRequest{
|
Number: purchaseData.Number,
|
SupplierName: purchaseData.Supplier.Name,
|
SupplierId: int64(purchaseData.SupplierId),
|
Product: product,
|
Source: "SRM_PURCHASE",
|
WarehouseName: purchaseData.Warehouse,
|
OperationSource: purchase_wms.OperationSource_OperationSourcePurchase,
|
})
|
if err != nil {
|
global.GVA_LOG.Error("grpc调用失败!", zap.Error(err))
|
response.FailWithMessage("grpc调用失败", c)
|
return
|
}
|
err = server.UpdatePurchaseQualityInspection(params.Ids, params.Status)
|
if err != nil {
|
global.GVA_LOG.Error("质检失败!", zap.Error(err))
|
response.FailWithMessage("质检失败", c)
|
return
|
}
|
}
|
err = server.UpdatePurchaseStatus(purchaseData.Number, purchase.WaitInspection)
|
if err != nil {
|
global.GVA_LOG.Error("状态更新失败!", zap.Error(err))
|
response.FailWithMessage("状态更新失败", c)
|
return
|
}
|
response.OkWithMessage("质检成功", c)
|
}
|
|
// AllProductInWarehouse
|
// @Tags Purchase
|
// @Summary 全部合格入库
|
// @Security ApiKeyAuth
|
// @accept application/json
|
// @Produce application/json
|
// @Param data body purchaserequest.PurchaseProductConfirmInfo true "参数"
|
// @Success 200 {object} response.Response{} "获取确认信息"
|
// @Router /purchase/allProductInWarehouse [post]
|
func (e *PurchaseApi) AllProductInWarehouse(c *gin.Context) {
|
var params []*purchaserequest.PurchaseProductConfirmInfo
|
err := c.ShouldBindJSON(¶ms)
|
if err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
list := make([]*purchase.PurchaseProductConfirm, 0, len(params))
|
if err := utils.AssignTo(params, &list); err != nil {
|
response.FailWithMessage(err.Error(), c)
|
return
|
}
|
|
server := service.NewPurchaseService()
|
err = server.SavePurchaseProductConfirm(list, false)
|
|
if err != nil {
|
global.GVA_LOG.Error("保存失败!", zap.Error(err))
|
response.FailWithMessage("保存失败", c)
|
return
|
}
|
inspectionList, err := server.SavePurchaseQualityInspection(list)
|
if err != nil {
|
global.GVA_LOG.Error("保存失败!", zap.Error(err))
|
response.FailWithMessage("保存失败", c)
|
return
|
}
|
purchaseData, err := server.GetPurchaseByNumber(list[0].PurchaseNumber)
|
if err != nil {
|
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
response.FailWithMessage("获取失败", c)
|
return
|
}
|
product := make([]*purchase_wms.PurchaseProduct, 0)
|
ids := make([]int, 0)
|
productMap := make(map[string]int64)
|
for _, inspection := range inspectionList {
|
productMap[inspection.ProductId] = inspection.Amount.IntPart()
|
ids = append(ids, int(inspection.ID))
|
}
|
for k, v := range productMap {
|
var p purchase_wms.PurchaseProduct
|
p.Id = k
|
p.Amount = v
|
product = append(product, &p)
|
}
|
client := purchase_wms.NewPurchaseServiceClient(purchase_wms.PurchaseConn)
|
_, err = client.PurchaseToWms(context.Background(), &purchase_wms.PurchaseToWmsRequest{
|
Number: purchaseData.Number,
|
SupplierName: purchaseData.Supplier.Name,
|
SupplierId: int64(purchaseData.SupplierId),
|
Product: product,
|
Source: "SRM_PURCHASE",
|
WarehouseName: purchaseData.Warehouse,
|
OperationSource: purchase_wms.OperationSource_OperationSourcePurchase,
|
})
|
if err != nil {
|
global.GVA_LOG.Error("grpc调用失败!", zap.Error(err))
|
response.FailWithMessage("grpc调用失败", c)
|
return
|
}
|
err = server.UpdatePurchaseQualityInspection(ids, purchase.InWarehouse)
|
if err != nil {
|
global.GVA_LOG.Error("质检失败!", zap.Error(err))
|
response.FailWithMessage("质检失败", c)
|
return
|
}
|
err = server.UpdatePurchaseStatus(purchaseData.Number, purchase.WaitInspection)
|
if err != nil {
|
global.GVA_LOG.Error("质检失败!", zap.Error(err))
|
response.FailWithMessage("质检失败", c)
|
return
|
}
|
response.OkWithMessage("质检成功", c)
|
}
|