liujiandao
2024-04-07 4630cbf64d1d4f33376efaaec7b4fcb90b01bf05
service/test/product.go
@@ -12,54 +12,74 @@
// CreateProduct 创建Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) CreateProduct(p []*test.Product) (err error) {
   err = global.GVA_DB.Create(p).Error
func (pService *ProductService) CreateProduct(pList []*testReq.Product) (err error) {
   products := make([]*test.SupplierMaterial, 0, len(pList))
   for _, p := range pList {
      products = append(products, &test.SupplierMaterial{
         Name:             p.Name,
         Number:           p.Number,
         SupplierId:       p.SupplierId,
         Unit:             p.Unit,
         PurchasePrice:    p.PurchasePrice,
         DeliveryTime:     p.DeliveryTime,
         ShippingDuration: p.ShippingDuration,
         Specifications:   p.Specifications,
         CategoryName:     p.CategoryName,
         ModelNumber:      p.ModelNumber,
      })
   }
   err = global.GVA_DB.Create(products).Error
   return err
}
// DeleteProduct 删除Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) DeleteProduct(p test.Product) (err error) {
   err = global.GVA_DB.Delete(&p).Error
func (pService *ProductService) DeleteProduct(id int) (err error) {
   err = global.GVA_DB.Delete(&test.SupplierMaterial{}, "id = ?", id).Error
   return err
}
// DeleteProductByIds 批量删除Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) DeleteProductByIds(ids request.IdsReq) (err error) {
   err = global.GVA_DB.Delete(&[]test.Product{}, "id in ?", ids.Ids).Error
   err = global.GVA_DB.Delete(&[]test.SupplierMaterial{}, "id in ?", ids.Ids).Error
   return err
}
// UpdateProduct 更新Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) UpdateProduct(p test.Product) (err error) {
func (pService *ProductService) UpdateProduct(p test.SupplierMaterial) (err error) {
   err = global.GVA_DB.Updates(&p).Error
   return err
}
// GetProduct 根据id获取Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) GetProduct(id uint) (p test.Product, err error) {
func (pService *ProductService) GetProduct(id int) (p test.SupplierMaterial, err error) {
   err = global.GVA_DB.Where("id = ?", id).First(&p).Error
   return
}
// GetProductInfoList 分页获取Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) GetProductInfoList(info testReq.ProductSearch) (list []test.Product, total int64, err error) {
func (pService *ProductService) GetProductInfoList(info testReq.ProductSearch) (list []test.SupplierMaterial, total int64, err error) {
   limit := info.PageSize
   offset := info.PageSize * (info.Page - 1)
   // 创建db
   db := global.GVA_DB.Model(&test.Product{})
   var ps []test.Product
   // 如果有条件搜索 下方会自动创建搜索语句
   db := global.GVA_DB.Model(&test.SupplierMaterial{})
   var ps []test.SupplierMaterial
   //搜索框合一添加查询条件
   if info.Keyword != "" {
      db = db.Where("Product.name LIKE ?", "%"+info.Keyword+"%").Joins("Supplier").Or("Supplier.name LIKE ?", "%"+info.Keyword+"%")
   }
   if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
      db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
      kw := "%" + info.Keyword + "%"
      if info.SupplierId == 0 {
         db = db.Where("`srm_supplier_material`.name LIKE ?", kw).Joins("Supplier").Or("Supplier.name LIKE ?", kw)
      } else {
         db = db.Where("name LIKE ? OR number LIKE ? OR specifications LIKE ?", kw, kw, kw)
      }
   }
   if info.Name != "" {
      db = db.Where("name LIKE ?", "%"+info.Name+"%")
@@ -67,68 +87,56 @@
   if info.Number != "" {
      db = db.Where("number LIKE ?", "%"+info.Number+"%")
   }
   if info.Unit != "" {
      db = db.Where("unit LIKE ?", "%"+info.Unit+"%")
   }
   supplierIds := []uint{0}
   if info.SupplierId != 0 {
      db = db.Where("supplier_id = ?", info.SupplierId)
      supplierIds = append(supplierIds, info.SupplierId)
   }
   if info.MaximumStock != 0 {
      db = db.Where("maximum_stock = ?", info.MaximumStock)
   }
   if info.MinimumStock != 0 {
      db = db.Where("minimum_stock = ?", info.MinimumStock)
   }
   if info.PurchasePrice != 0 {
      db = db.Where("purchase_price = ?", info.PurchasePrice)
   }
   if info.Specifications != "" {
      db = db.Where("specifications LIKE ?", "%"+info.Specifications+"%")
   }
   if info.ModelNumber != "" {
      db = db.Where("model_number LIKE ?", "%"+info.ModelNumber+"%")
   }
   if info.ProductType != "" {
      db = db.Where("product_type LIKE ?", "%"+info.ProductType+"%")
   }
   if info.SupplierNumber != "" {
      db = db.Joins("Supplier").Where("Supplier.number LIKE ?", "%"+info.SupplierNumber+"%")
   }
   if info.DeliveryTime != 0 {
      db = db.Where("delivery_time = ?", info.DeliveryTime)
   }
   if info.ShippingDuration != 0 {
      db = db.Where("shipping_duration = ?", info.ShippingDuration)
   }
   db = db.Where("supplier_id in (?)", supplierIds)
   err = db.Count(&total).Error
   if err != nil {
      return
   }
   err = db.Limit(limit).Offset(offset).Preload("Supplier").Find(&ps).Error
   err = db.Limit(limit).Offset(offset).Order("id desc").Preload("Supplier").Find(&ps).Error
   return ps, total, err
}
// GetProducts 根据ids获取Product记录
func (pService *ProductService) GetProducts(ids []uint) (p []*test.Product, m map[uint]*test.Product, err error) {
func (pService *ProductService) GetProducts(ids []uint) (p []*test.SupplierMaterial, m map[uint]*test.SupplierMaterial, err error) {
   err = global.GVA_DB.Where("id in ?", ids).Find(&p).Error
   if err != nil {
      return
   }
   m = make(map[uint]*test.Product, len(p))
   m = make(map[uint]*test.SupplierMaterial, len(p))
   for _, product := range p {
      m[product.ID] = product
   }
   return
}
// GetMaterials 获取物料
func (pService *ProductService) GetMaterials(info testReq.ProductSearch) (list []test.Material, total int64, err error) {
   limit := info.PageSize
   offset := info.PageSize * (info.Page - 1)
   // 创建db
   db := global.GVA_DB.Model(&test.Material{})
   var ps []test.Material
   if info.Keyword != "" {
      db = db.Where("name LIKE ? or id LIKE ?", "%"+info.Keyword+"%", "%"+info.Keyword+"%")
   }
   //类型为采购
   db = db.Where("purchase_types LIKE ?", "%1%")
   db = db.Where("is_storage = ?", 1)
   err = db.Count(&total).Error
   if err != nil {
      return
   }
   err = db.Limit(limit).Offset(offset).Find(&ps).Error
   return ps, total, err
}