package services import ( "Medical_ERP/common/global" db "Medical_ERP/common/initialize" model2 "Medical_ERP/common/model" "Medical_ERP/dto" "Medical_ERP/models" "errors" "github.com/beego/beego/v2/core/logs" "gorm.io/gorm" ) type Stock struct { } // StockIn 入库 func (e *Stock) StockIn(c *dto.StockInInsertReq) error { var err error // 检查药品信息是否已存在 medicineInfo, err := e.GetMedicineInfo(c.ProductID, c.EnterpriseID, c.SpecID, c.ApprovalNumber, c.ExpiryDate) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { logs.Error("db error: %s", err) return global.CreateFailedErr } tx := db.DB.Begin() // 如果药品信息不存在,则创建新的药品信息 if errors.Is(err, gorm.ErrRecordNotFound) { medicineInfo = models.MedicineInfo{ ProductID: c.ProductID, EnterpriseID: c.EnterpriseID, SpecID: c.SpecID, UnitID: c.UnitID, DosageFormID: c.DosageFormID, ApprovalNumber: c.ApprovalNumber, QualificationNumber: c.QualificationNumber, ProductionDate: c.ProductionDate, ExpiryDate: c.ExpiryDate, BatchNumber: c.BatchNumber, } //medicineInfo.SetDeptId(c.DeptId) //medicineInfo.SetCreateBy(c.CreateBy) err = tx.Create(&medicineInfo).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } } // 进行入库操作 stockInRecord := models.StockIn{ MedicineID: medicineInfo.Id, Quantity: c.Quantity, UnitPrice: c.UnitPrice, Operator: c.Operator, ForwardingUnit: c.ForwardingUnit, Date: c.Date, } stockInRecord.SetDeptId(c.DeptId) stockInRecord.SetCreateBy(c.CreateBy) err = tx.Create(&stockInRecord).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } c.Id = stockInRecord.Id var inventory models.MedicineInventory err = tx.First(&inventory, "medicine_id = ? AND dept_id = ?", medicineInfo.Id, c.DeptId).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } // 添加库存记录 inventory = models.MedicineInventory{ MedicineID: medicineInfo.Id, TotalIn: c.Quantity, StockInID: stockInRecord.Id, TotalOut: 0, StockOutID: 0, Balance: c.Quantity, ForwardingUnit: c.ForwardingUnit, } inventory.SetDeptId(c.DeptId) inventory.SetCreateBy(c.CreateBy) inventory.Balance += c.Quantity err = tx.Create(&inventory).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } return nil } // 出库 func (e *Stock) StockInStockInList(c *dto.StockInInsertReq) error { var err error // 检查药品信息是否已存在 medicineInfo, err := e.GetMedicineInfo(c.ProductID, c.EnterpriseID, c.SpecID, c.ApprovalNumber, c.ExpiryDate) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { logs.Error("db error: %s", err) return global.CreateFailedErr } tx := db.DB.Begin() // 如果药品信息不存在,则创建新的药品信息 if errors.Is(err, gorm.ErrRecordNotFound) { medicineInfo = models.MedicineInfo{ ProductID: c.ProductID, EnterpriseID: c.EnterpriseID, SpecID: c.SpecID, UnitID: c.UnitID, DosageFormID: c.DosageFormID, ApprovalNumber: c.ApprovalNumber, QualificationNumber: c.QualificationNumber, ProductionDate: c.ProductionDate, ExpiryDate: c.ExpiryDate, BatchNumber: c.BatchNumber, } //medicineInfo.SetDeptId(c.DeptId) //medicineInfo.SetCreateBy(c.CreateBy) err = tx.Create(&medicineInfo).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } } // 进行入库操作 stockInRecord := models.StockIn{ MedicineID: medicineInfo.Id, Quantity: c.Quantity, UnitPrice: c.UnitPrice, Operator: c.Operator, ForwardingUnit: c.ForwardingUnit, Date: c.Date, } stockInRecord.SetDeptId(c.DeptId) stockInRecord.SetCreateBy(c.CreateBy) err = tx.Create(&stockInRecord).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } c.Id = stockInRecord.Id var inventory models.MedicineInventory err = tx.First(&inventory, "medicine_id = ? AND dept_id = ?", medicineInfo.Id, c.DeptId).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } // 添加库存记录 inventory = models.MedicineInventory{ MedicineID: medicineInfo.Id, TotalIn: c.Quantity, StockInID: stockInRecord.Id, TotalOut: 0, StockOutID: 0, Balance: c.Quantity, } inventory.Balance += c.Quantity inventory.SetDeptId(c.DeptId) inventory.SetCreateBy(c.CreateBy) err = tx.Create(&inventory).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } return nil } func (e *Stock) StockOut(c *dto.StockOutInsertReq) error { var err error // 检查药品信息是否已存在 medicineInfo, err := e.GetMedicineInfo(c.ProductID, c.EnterpriseID, c.SpecID, c.ApprovalNumber, c.ExpiryDate) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { logs.Error("db error: %s", err) return global.CreateFailedErr } // 如果药品信息不存在,提示错误,不能出库 if errors.Is(err, gorm.ErrRecordNotFound) { logs.Error("db error: %s", err) return errors.New("药品信息不存在") } tx := db.DB.Begin() // 进行入库操作 stockOutRecord := models.StockOut{ MedicineID: medicineInfo.Id, Quantity: c.Quantity, //UnitPrice: c.UnitPrice, Operator: c.Operator, ReceivingUnit: c.ReceivingUnit, Date: c.Date, } err = tx.Create(&stockOutRecord).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } c.Id = stockOutRecord.Id var inventory models.MedicineInventory err = tx.First(&inventory, "medicine_id = ? AND dept_id = ?", medicineInfo.Id, c.DeptId).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } // 添加库存记录 inventory = models.MedicineInventory{ MedicineID: medicineInfo.Id, TotalIn: 0, StockInID: 0, TotalOut: c.Quantity, StockOutID: stockOutRecord.Id, Balance: c.Quantity, ReceivingUnit: c.ReceivingUnit, Operator: c.Operator, Date: c.Date, ControlBy: model2.ControlBy{ DeptId: c.DeptId, CreateBy: c.CreateBy, }, } inventory.Balance -= c.Quantity err = tx.Create(&inventory).Error if err != nil { tx.Rollback() logs.Error("db error: %s", err) return global.CreateFailedErr } return nil } func (e *Stock) GetMedicineInfo(productId, enterpriseId, specId int, approvalNumber, expiryDate string) (medicineInfo models.MedicineInfo, err error) { err = db.DB.Where("product_id = ? AND enterprise_id = ? AND spec_id = ?"+ "AND approval_number = ? AND expiry_date", productId, enterpriseId, specId, approvalNumber, expiryDate). First(&medicineInfo).Error return } func (e *Stock) InsertMedicineInfo(c *dto.StockInInsertReq) (medicineInfo models.MedicineInfo, err error) { data := models.MedicineInfo{ ProductID: c.ProductID, EnterpriseID: c.EnterpriseID, SpecID: c.SpecID, UnitID: c.UnitID, DosageFormID: c.DosageFormID, ApprovalNumber: c.ApprovalNumber, QualificationNumber: c.QualificationNumber, ProductionDate: c.ProductionDate, ExpiryDate: c.ExpiryDate, BatchNumber: c.BatchNumber, } //data.SetDeptId(c.DeptId) //data.SetCreateBy(c.CreateBy) err = db.DB.Create(&data).Error if err != nil { logs.Error("db error: %s", err) return data, global.CreateFailedErr } return data, nil }