| | |
| | | "srm/global" |
| | | "srm/model/common/request" |
| | | "srm/model/purchase" |
| | | purchaserequest "srm/model/purchase/request" |
| | | "srm/proto/qualityinspect" |
| | | "srm/service/test" |
| | | ) |
| | |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (slf *PurchaseService) CreatePurchase(params purchaserequest.AddPurchase) (err error) { |
| | | func (slf *PurchaseService) CreatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Create(¶ms.Purchase).Error |
| | | err = tx.Create(¶ms).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range params.ProductList { |
| | | product.PurchaseId = cast.ToInt(params.Purchase.ID) |
| | | for _, product := range productList { |
| | | product.PurchaseId = cast.ToInt(params.ID) |
| | | } |
| | | return global.GVA_DB.Create(params.ProductList).Error |
| | | return tx.Create(productList).Error |
| | | }) |
| | | |
| | | return err |
| | |
| | | |
| | | 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 |
| | | err = tx.Where("id = ?", id).Delete(&purchase.Purchase{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return global.GVA_DB.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | return tx.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | }) |
| | | return err |
| | | } |
| | |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (slf *PurchaseService) UpdatePurchase(params *purchaserequest.AddPurchase) (err error) { |
| | | func (slf *PurchaseService) UpdatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Updates(params.Purchase).Error |
| | | err = tx.Where("id = ?", params.ID).Updates(params).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = global.GVA_DB.Where("purchase_id = ?", params.Purchase.ID).Delete(&purchase.PurchaseProducts{}).Error |
| | | err = tx.Where("purchase_id = ?", params.ID).Delete(&purchase.PurchaseProducts{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range params.ProductList { |
| | | for _, product := range productList { |
| | | product.ID = 0 |
| | | product.PurchaseId = cast.ToInt(params.Purchase.ID) |
| | | product.PurchaseId = cast.ToInt(params.ID) |
| | | } |
| | | return global.GVA_DB.Create(params.ProductList).Error |
| | | return tx.Create(productList).Error |
| | | }) |
| | | return err |
| | | } |
| | |
| | | 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 |
| | | err = tx.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": targetStatus}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | |
| | | _, err = qualityinspect.NewQualityInspectServiceClient(qualityinspect.Conn).SendPurchaseInspect(context.Background(), &inspectRequest) |
| | | return err |
| | | } |
| | | |
| | | func (slf *PurchaseService) SavePurchaseType(list []*purchase.PurchaseType) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = tx.Where("1 = ?", 1).Delete(&purchase.PurchaseType{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, item := range list { |
| | | item.ID = 0 |
| | | } |
| | | return tx.Create(list).Error |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | func (slf *PurchaseService) GetPurchaseTypeList() (list []*purchase.PurchaseType, err error) { |
| | | db := global.GVA_DB.Model(&purchase.PurchaseType{}) |
| | | list = make([]*purchase.PurchaseType, 0) |
| | | err = db.Order("pin desc, sort desc, id asc").Find(&list).Error |
| | | return list, err |
| | | } |