liujiandao
2024-03-30 69f0410081da54e6cea8a04a07fd0119a8edc623
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package test
 
import (
    "srm/global"
    "srm/model/common/request"
    "srm/model/test"
    testReq "srm/model/test/request"
)
 
type ProductService struct {
}
 
// CreateProduct 创建Product记录
// Author [piexlmax](https://github.com/piexlmax)
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(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.SupplierMaterial{}, "id in ?", ids.Ids).Error
    return err
}
 
// UpdateProduct 更新Product记录
// Author [piexlmax](https://github.com/piexlmax)
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 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.SupplierMaterial, total int64, err error) {
    limit := info.PageSize
    offset := info.PageSize * (info.Page - 1)
    // 创建db
    db := global.GVA_DB.Model(&test.SupplierMaterial{})
    var ps []test.SupplierMaterial
    //搜索框合一添加查询条件
    if info.Keyword != "" {
        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+"%")
    }
    if info.Number != "" {
        db = db.Where("number LIKE ?", "%"+info.Number+"%")
    }
 
    if info.SupplierId != 0 {
        db = db.Where("supplier_id = ?", info.SupplierId)
    }
 
    err = db.Count(&total).Error
    if err != nil {
        return
    }
 
    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
}