| | |
| | | package purchase |
| | | |
| | | import ( |
| | | "context" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "srm/global" |
| | | "srm/model/common/request" |
| | | "srm/model/purchase" |
| | | purchaserequest "srm/model/purchase/request" |
| | | "srm/proto/qualityinspect" |
| | | "srm/service/test" |
| | | ) |
| | | |
| | | type PurchaseService struct{} |
| | |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) CreatePurchase(params purchaserequest.AddPurchase) (err error) { |
| | | func (slf *PurchaseService) CreatePurchase(params purchaserequest.AddPurchase) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Create(¶ms.Purchase).Error |
| | | if err != nil { |
| | |
| | | //@param: id uint |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) DeletePurchase(id uint) (err error) { |
| | | func (slf *PurchaseService) DeletePurchase(id uint) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Where("id = ?", id).Delete(&purchase.Purchase{}).Error |
| | | if err != nil { |
| | |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) UpdatePurchase(params *purchaserequest.AddPurchase) (err error) { |
| | | func (slf *PurchaseService) UpdatePurchase(params *purchaserequest.AddPurchase) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Updates(params.Purchase).Error |
| | | if err != nil { |
| | |
| | | //@param: id uint |
| | | //@return: purchase model.Purchase, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchase(id uint) (purchase purchase.Purchase, err error) { |
| | | err = global.GVA_DB.Where("id = ?", id).First(&purchase).Error |
| | | func (slf *PurchaseService) GetPurchase(id uint) (purchase purchase.Purchase, err error) { |
| | | err = global.GVA_DB.Where("id = ?", id).Preload("Supplier").First(&purchase).Error |
| | | return |
| | | } |
| | | |
| | |
| | | //@param: info request.PageInfo |
| | | //@return: list interface{}, total int64, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchaseList(info request.PageInfo) (list interface{}, total int64, err error) { |
| | | func (slf *PurchaseService) GetPurchaseList(info request.PageInfo) (list interface{}, total int64, err error) { |
| | | limit := info.PageSize |
| | | offset := info.PageSize * (info.Page - 1) |
| | | db := global.GVA_DB.Model(&purchase.Purchase{}) |
| | |
| | | //@param: purchaseId int |
| | | //@return: list interface{}, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchaseProductList(purchaseId uint) (list []*purchase.PurchaseProducts, err error) { |
| | | func (slf *PurchaseService) GetPurchaseProductList(purchaseId uint) (list []*purchase.PurchaseProducts, err error) { |
| | | db := global.GVA_DB.Model(&purchase.PurchaseProducts{}) |
| | | list = make([]*purchase.PurchaseProducts, 0) |
| | | err = db.Where("purchase_id = ?", purchaseId).Find(&list).Error |
| | |
| | | //@param: id uint |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) Submit(id uint) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": 1}).Error |
| | | func (slf *PurchaseService) Submit(id uint) (err error) { |
| | | |
| | | purchaseData, err := slf.GetPurchase(id) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return global.GVA_DB.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | var targetStatus purchase.OrderStatus |
| | | switch purchaseData.Status { |
| | | case purchase.OrderStatusConfirmed: |
| | | targetStatus = purchase.OrderStatusReceived |
| | | case purchase.OrderStatusReceived: |
| | | targetStatus = purchase.OrderStatusStored |
| | | case purchase.OrderStatusStored: |
| | | targetStatus = purchase.OrderStatusCompleted |
| | | } |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": targetStatus}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | switch targetStatus { |
| | | case purchase.OrderStatusConfirmed: |
| | | return SendInspect(purchaseData) |
| | | case purchase.OrderStatusReceived: |
| | | case purchase.OrderStatusStored: |
| | | } |
| | | return nil |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | func SendInspect(record purchase.Purchase) error { |
| | | productList, err := NewPurchaseService().GetPurchaseProductList(record.ID) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | productIds := make([]uint, 0, len(productList)) |
| | | for _, product := range productList { |
| | | productIds = append(productIds, uint(product.ProductId)) |
| | | } |
| | | productService := &test.ProductService{} |
| | | _, productMap, err := productService.GetProducts(productIds) |
| | | |
| | | inspectOrders := make([]*qualityinspect.QualityInspect, 0, len(productList)) |
| | | for _, productItem := range productList { |
| | | product := productMap[uint(productItem.ProductId)] |
| | | if product == nil { |
| | | continue |
| | | } |
| | | inspectOrder := &qualityinspect.QualityInspect{ |
| | | InspectType: qualityinspect.InspectType_InspectTypePurchase, |
| | | MaterialType: qualityinspect.MaterialType_MaterialTypeRaw, |
| | | MaterialName: product.Name, |
| | | MaterialId: product.Number, |
| | | MaterialTp: product.ModelNumber, |
| | | MaterialUnit: product.Unit, |
| | | Supplier: record.Supplier.Name, |
| | | WarehouseName: "采购总仓", |
| | | ReportAmount: productItem.Amount.InexactFloat64(), |
| | | InspectMethod: qualityinspect.InspectMethod_InspectMethodAll, |
| | | InspectAmount: productItem.Amount.InexactFloat64(), |
| | | } |
| | | inspectOrders = append(inspectOrders, inspectOrder) |
| | | } |
| | | inspectRequest := qualityinspect.SendPurchaseInspectRequest{List: inspectOrders} |
| | | _, err = qualityinspect.NewQualityInspectServiceClient(qualityinspect.Conn).SendPurchaseInspect(context.Background(), &inspectRequest) |
| | | return err |
| | | } |