add
wangpengfei
2023-08-26 2083c6a4f3c69d7f0f1deca562b50df3e8bfd91e
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
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(p *test.Product) (err error) {
    err = global.GVA_DB.Create(p).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
    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
    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
    return err
}
 
// GetProduct 根据id获取Product记录
// Author [piexlmax](https://github.com/piexlmax)
func (pService *ProductService) GetProduct(id uint) (p test.Product, 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) {
    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)
    }
    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 {
        db = db.Where("supplier_id = ?", info.SupplierId)
    }
 
    err = db.Count(&total).Error
    if err != nil {
        return
    }
 
    err = db.Limit(limit).Offset(offset).Preload("Supplier").Find(&ps).Error
    return ps, total, err
}