From c7e82fd1d76b9fb25497d5aba4372bbfe13a8468 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 28 三月 2024 20:00:21 +0800
Subject: [PATCH] 采购入库增加来源

---
 service/test/product.go |   89 +++++++++++++++++++++++++++++++++++---------
 1 files changed, 71 insertions(+), 18 deletions(-)

diff --git a/service/test/product.go b/service/test/product.go
index 0cf3317..57ad6d9 100644
--- a/service/test/product.go
+++ b/service/test/product.go
@@ -12,59 +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) {
-	err = global.GVA_DB.Save(&p).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
-	// 濡傛灉鏈夋潯浠舵悳绱� 涓嬫柟浼氳嚜鍔ㄥ垱寤烘悳绱㈣鍙�
-	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
-		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
+	db := global.GVA_DB.Model(&test.SupplierMaterial{})
+	var ps []test.SupplierMaterial
+	//鎼滅储妗嗗悎涓�娣诲姞鏌ヨ鏉′欢
+	if info.Keyword != "" {
+		db = db.Where("`srm_supplier_material`.name LIKE ?", "%"+info.Keyword+"%").Joins("Supplier").Or("Supplier.name LIKE ?", "%"+info.Keyword+"%")
 	}
 	if info.Name != "" {
 		db = db.Where("name LIKE ?", "%"+info.Name+"%")
 	}
 	if info.Number != "" {
 		db = db.Where("number LIKE ?", "%"+info.Number+"%")
-	}
-	if info.Unit != "" {
-		db = db.Where("unit LIKE ?", "%"+info.Unit+"%")
 	}
 
 	if info.SupplierId != 0 {
@@ -76,6 +91,44 @@
 		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.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.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
 }

--
Gitblit v1.8.0