package services import ( "Medical_ERP/common/global" db "Medical_ERP/common/initialize" model2 "Medical_ERP/common/model" "Medical_ERP/dto" "Medical_ERP/models" "errors" "fmt" "github.com/beego/beego/v2/core/logs" "gorm.io/gorm" "time" ) type Barcode struct { } // getNextIncrementalCode 从数据库中获取下一个递增码 func (e *Barcode) getNextIncrementalCode(deptId int) (string, error) { // 获取当前日期 var count int64 current := fmt.Sprintf("%05d%s", deptId, time.Now().Format("20060102")) err := db.DB.Model(&models.Barcode{}).Where("code LIKE ?", current+"%").Count(&count).Error if err != nil { logs.Error("db error: %s", err) return "", err } return fmt.Sprintf("%s%04d", current, count+1), nil } // Insert 创建Spec对象 func (e *Barcode) Insert(c models.TransportRecord) (code string, err error) { incrementalCode, err := e.getNextIncrementalCode(c.DeptID) if err != nil { logs.Error("生成条形码失败", err) return code, global.CreateFailedErr } data := models.Barcode{Code: incrementalCode, Data: c, ControlBy: model2.ControlBy{CreateBy: c.CreateBy, DeptId: c.DeptId}} err = db.DB.Create(&data).Error if err != nil { logs.Error("db error: %s", err) return code, global.CreateFailedErr } return data.Code, nil } // Insert 创建Spec对象 func (e *Barcode) InsertRefundOut(c dto.StockRefundOutExport) (code string, err error) { incrementalCode, err := e.getNextIncrementalCode(c.DeptId) if err != nil { logs.Error("生成条形码失败", err) return code, global.CreateFailedErr } var obj models.TransportRecord obj.StockOutIds = c.StockOutIds obj.DeptID = c.DeptId obj.DeptName = c.DeptName data := models.Barcode{Code: incrementalCode, Data: obj, ControlBy: model2.ControlBy{CreateBy: obj.CreateBy, DeptId: c.DeptId}} err = db.DB.Create(&data).Error if err != nil { logs.Error("db error: %s", err) return code, global.CreateFailedErr } return data.Code, nil } // Get 获取DosageForm对象 func (e *Barcode) Get(d *dto.GetBarCodeInfoReq, barcodeModel *models.Barcode) error { err := db.DB.Where("code = ?", d.Code).First(barcodeModel).Error if err != nil { logs.Error("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.GetNotFoundOrNoPermissionErr } return global.GetFailedErr } return nil }