liujiandao
2024-04-25 c0f8f8d3a74dbdab4f6ab4926fc664d818fb50f2
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
package test
 
import (
    "srm/global"
    "srm/model/common/request"
    "srm/model/test"
    testReq "srm/model/test/request"
)
 
type SupplierService struct {
}
 
// CreateSupplier 创建Supplier记录
func (sService *SupplierService) CreateSupplier(s *test.Supplier) (err error) {
    err = global.GVA_DB.Create(s).Error
    return err
}
 
// DeleteSupplier 删除Supplier记录
func (sService *SupplierService) DeleteSupplier(s test.Supplier) (err error) {
    err = global.GVA_DB.Delete(&s).Error
    return err
}
 
// DeleteSupplierByIds 批量删除Supplier记录
func (sService *SupplierService) DeleteSupplierByIds(ids request.IdsReq) (err error) {
    err = global.GVA_DB.Delete(&[]test.Supplier{}, "id in ?", ids.Ids).Error
    return err
}
 
// UpdateSupplier 更新Supplier记录
func (sService *SupplierService) UpdateSupplier(s test.Supplier) (err error) {
    err = global.GVA_DB.Updates(&s).Error
    return err
}
 
// GetSupplier 根据id获取Supplier记录
func (sService *SupplierService) GetSupplier(id uint) (s test.Supplier, err error) {
    err = global.GVA_DB.Model(&test.Supplier{}).Where("id = ?", id).First(&s).Error
    return
}
 
// GetSupplierByNumber 根据编码获取Supplier记录
func (sService *SupplierService) GetSupplierByNumber(number string) (s test.Supplier, err error) {
    err = global.GVA_DB.Model(&test.Supplier{}).Where("number = ?", number).Preload("Contract").First(&s).Error
    return
}
 
// GetSupplierInfoList 分页获取Supplier记录
func (sService *SupplierService) GetSupplierInfoList(info testReq.SupplierSearch) (list []test.Supplier, total int64, err error) {
    limit := info.PageSize
    offset := info.PageSize * (info.Page - 1)
    // 创建db
    db := global.GVA_DB.Model(&test.Supplier{})
    var ss []test.Supplier
    // 如果有条件搜索 下方会自动创建搜索语句
    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.SupplierType != "" {
        db = db.Where("srm_supplier_type LIKE ?", "%"+info.SupplierType+"%")
    }
    if info.Industry != "" {
        db = db.Where("industry LIKE ?", "%"+info.Industry+"%")
    }
    if info.Contact != "" {
        db = db.Where("contact LIKE ?", "%"+info.Contact+"%")
    }
    if info.Phone != "" {
        db = db.Where("phone LIKE ?", "%"+info.Phone+"%")
    }
    if info.CreatedAt != nil {
        db = db.Where("created_at = ?", info.CreatedAt)
    }
    if info.Status != 0 {
        db = db.Where("status = ?", info.Status)
    }
    err = db.Count(&total).Error
    if err != nil {
        return
    }
 
    err = db.Debug().Limit(limit).Offset(offset).Order("created_at desc").Preload("Contract").Find(&ss).Error
    return ss, total, err
}
 
// ChangeStatus Change supplier status
func (sService *SupplierService) ChangeStatus(id uint, status int) (err error) {
    err = global.GVA_DB.Model(&test.Supplier{}).Where("id = ?", id).Update("status", status).Error
    return err
}
 
func (sService *SupplierService) MaxAutoIncr() (int, error) {
    var total int64
    err := global.GVA_DB.Model(&test.Supplier{}).Count(&total).Error
    return int(total), err
}
 
func (sService *SupplierService) GetSupplierProduct(info testReq.SupplierProduct) ([]test.SupplierMaterial, int64, error) {
    limit := info.PageSize
    offset := info.PageSize * (info.Page - 1)
    // 创建db
    db := global.GVA_DB.Model(&test.SupplierMaterial{})
    var ps []test.SupplierMaterial
    var total int64
    if info.SupplierId > 0 {
        db = db.Where("supplier_id = ?", info.SupplierId)
    }
    err := db.Count(&total).Error
    if err != nil {
        return ps, total, err
    }
    err = db.Limit(limit).Offset(offset).Order("id desc").Preload("Supplier").Find(&ps).Error
    return ps, total, err
}