| | |
| | | purchaserequest "srm/model/purchase/request" |
| | | "srm/proto/qualityinspect" |
| | | "srm/service/test" |
| | | "srm/utils" |
| | | "time" |
| | | ) |
| | | |
| | | type PurchaseService struct{} |
| | |
| | | err := global.GVA_DB.Model(&purchase.Purchase{}).Count(&total).Error |
| | | return int(total), err |
| | | } |
| | | |
| | | func (slf *PurchaseService) SavePurchaseProductConfirm(list []*purchase.PurchaseProductConfirm) (err error) { |
| | | if len(list) == 0 { |
| | | return errors.New("产品列表不能为空") |
| | | } |
| | | purchaseNumber := list[0].PurchaseNumber |
| | | var pur purchase.Purchase |
| | | err = global.GVA_DB.Where("number = ?", purchaseNumber).First(&pur).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | m := make(map[string]interface{}) |
| | | now := int64(0) |
| | | yu := int64(0) |
| | | for _, confirm := range list { |
| | | if !confirm.NowReceiveAmount.IsZero() { |
| | | now = confirm.NowReceiveAmount.IntPart() |
| | | } |
| | | if !confirm.SurplusReceiveAmount.IsZero() { |
| | | yu = confirm.SurplusReceiveAmount.IntPart() |
| | | } |
| | | confirm.OverReceiveAmount = confirm.OverReceiveAmount.Add(confirm.NowReceiveAmount) |
| | | confirm.NotReceiveAmount = confirm.Amount.Sub(confirm.OverReceiveAmount) |
| | | } |
| | | if now == 0 && yu > 0 { |
| | | m["status"] = purchase.OrderStatusWaitReceive |
| | | } |
| | | if now > 0 && yu > 0 { |
| | | m["status"] = purchase.OrderStatusPartReceive |
| | | } |
| | | if now > 0 && yu == 0 { |
| | | m["status"] = purchase.OrderStatusWaitQuality |
| | | } |
| | | |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = tx.Where("purchase_number = ?", purchaseNumber).Delete(&purchase.PurchaseProductConfirm{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | err = tx.Create(list).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = tx.Where("number = ?", purchaseNumber).Model(&purchase.Purchase{}).Updates(m).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | return nil |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | func (slf *PurchaseService) GetPurchaseProductConfirmInfo(number string) ([]*purchase.PurchaseProductConfirm, error) { |
| | | var list []*purchase.PurchaseProductConfirm |
| | | err := global.GVA_DB.Model(&purchase.PurchaseProductConfirm{}).Where("purchase_number = ?", number).Find(&list).Error |
| | | return list, err |
| | | } |
| | | |
| | | func (slf *PurchaseService) SavePurchaseQualityInspection(list []*purchase.PurchaseProductConfirm) (err error) { |
| | | if len(list) == 0 { |
| | | return errors.New("产品列表不能为空") |
| | | } |
| | | purchaseNumber := list[0].PurchaseNumber |
| | | qualityList := make([]*purchase.PurchaseQualityInspection, 0) |
| | | for _, confirm := range list { |
| | | var pqi purchase.PurchaseQualityInspection |
| | | pqi.PurchaseNumber = purchaseNumber |
| | | pqi.Principal = confirm.Principal |
| | | pqi.ProductId = confirm.ProductId |
| | | pqi.ProductName = confirm.ProductName |
| | | pqi.Amount = confirm.NowReceiveAmount |
| | | pqi.Status = purchase.WaitInspection |
| | | pqi.CreateTime = utils.TimeToString(time.Now()) |
| | | pqi.Unit = confirm.Unit |
| | | pqi.Specs = confirm.Specs |
| | | pqi.Type = confirm.Type |
| | | qualityList = append(qualityList, &pqi) |
| | | } |
| | | err = global.GVA_DB.Model(purchase.PurchaseQualityInspection{}).Create(qualityList).Error |
| | | return err |
| | | } |
| | | |
| | | func (slf *PurchaseService) GetPurchaseQualityInspection(params purchaserequest.GetQualityInspectionInfo) ([]*purchase.PurchaseQualityInspection, error) { |
| | | var list []*purchase.PurchaseQualityInspection |
| | | db := global.GVA_DB.Model(&purchase.PurchaseQualityInspection{}).Where("purchase_number = ?", params.PurchaseNumber) |
| | | if len(params.Status) > 0 { |
| | | db = db.Where("status in (?)", params.Status) |
| | | } |
| | | if len(params.Times) > 0 { |
| | | db = db.Where("create_time in (?)", params.Times) |
| | | } |
| | | err := db.Find(&list).Error |
| | | return list, err |
| | | } |
| | | |
| | | func (slf *PurchaseService) UpdatePurchaseQualityInspection(ids []int, status int) error { |
| | | err := global.GVA_DB.Model(&purchase.PurchaseQualityInspection{}).Where("id in (?)", ids).Updates(map[string]interface{}{"status": status}).Error |
| | | return err |
| | | } |
| | | |
| | | func (slf *PurchaseService) GetPurchaseQualityInspectionList(ids []int) ([]*purchase.PurchaseQualityInspection, error) { |
| | | var list []*purchase.PurchaseQualityInspection |
| | | err := global.GVA_DB.Model(&purchase.PurchaseQualityInspection{}).Where("id in (?)", ids).Find(&list).Error |
| | | return list, err |
| | | } |