package services import ( "Medical_ERP/common/actions" cDto "Medical_ERP/common/dto" "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 ProductCode struct { } func ProductCodeNameScopes(name string) func(db *gorm.DB) *gorm.DB { return func(db *gorm.DB) *gorm.DB { if len(name) == 0 { return db } return db.Where("product like ? OR enterprise like ? OR batch_number like ?", "%"+name+"%", "%"+name+"%", "%"+name+"%") } } // Insert 创建Enterprise对象 func (e *ProductCode) CreateProductCode(c *dto.GenProductCodePageReq, deptId int) error { var err error var data models.ProductCode err = db.DB.Where("product = ? AND enterprise = ? AND spec = ? AND batch_number = ? AND dept_id = ?", c.Product, c.Enterprise, c.Spec, c.BatchNumber, deptId).First(&data).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { err = db.DB.Create(&models.ProductCode{ Product: c.Product, Enterprise: c.Enterprise, Spec: c.Spec, BatchNumber: c.BatchNumber, ControlBy: model2.ControlBy{ DeptId: deptId, }, }).Error if err != nil { logs.Error("db error: %s", err) return errors.New("创建产品条码信息失败") } } else { logs.Error("db error: %s", err) return errors.New("查询产品条码信息失败") } } return nil } func (e *ProductCode) GetPage(c *dto.ProductCodePageReq, list *[]models.ProductCode, count *int64, p *actions.DataPermission) error { var err error var data models.ProductCode err = db.DB.Model(&data). Scopes( ProductCodeNameScopes(c.Name), cDto.Paginate(c.GetPageSize(), c.GetPageIndex()), actions.Permission(data.TableName(), p), ). Find(list). Count(count).Error if err != nil { logs.Error("db error: %s ", err) return global.GetFailedErr } return nil }