123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- 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
- }
|