zhangqian
2024-03-19 7d80867c1d2d8340da8e106b4af2ceaba3fc9fc1
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package model
 
import (
    "aps_crm/constvar"
    "aps_crm/pkg/mysqlx"
    "errors"
    "fmt"
    "gorm.io/gorm"
)
 
type (
    // Invoice 销售发票
    Invoice struct {
        Id               int                        `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        Number           string                     `json:"number" gorm:"column:number;type:varchar(255);comment:发票编号"`
        ClientId         int                        `gorm:"client_id" json:"clientId"` // 客户id
        Client           Client                     `gorm:"foreignKey:ClientId"`
        InvoiceTypeId    int                        `gorm:"invoice_type_id" json:"invoiceTypeId"` // 发票类型id
        InvoiceType      InvoiceType                `gorm:"foreignKey:InvoiceTypeId"`
        PrincipalId      int                        `gorm:"principal_id" json:"principalId"`          // 销售负责人id
        Subject          string                     `gorm:"subject" json:"subject"`                   // 主题
        InvoiceStatusId  int                        `gorm:"invoice_status_id" json:"invoiceStatusId"` // 发票状态id
        InvoiceStatus    InvoiceStatus              `gorm:"foreignKey:InvoiceStatusId"`
        SourceType       constvar.InvoiceSourceType `gorm:"source_type" json:"sourceType"`              // 源单类型(1销售明细单2服务合同)
        SourceId         int                        `gorm:"source_id" json:"sourceId"`                  // 源单id
        TaxpayerIdNumber string                     `gorm:"taxpayer_id_number" json:"taxpayerIdNumber"` // 纳税识别号
        InvoiceNumber    string                     `gorm:"invoice_number" json:"invoiceNumber"`        // 发票号码
        InvoiceDate      string                     `gorm:"invoice_date" json:"invoiceDate"`            // 开票日期
        CourierNumber    string                     `gorm:"courier_number" json:"courierNumber"`        // 物流单号
        CourierCompanyId int                        `gorm:"courier_company_id" json:"courierCompanyId"` // 物流公司
        CourierCompany   CourierCompany             `gorm:"foreignKey:CourierCompanyId"`
        Products         []*Product                 `json:"products" gorm:"many2many:invoice_product;"`
        CodeStandID      string                     `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"`
    }
 
    // InvoiceSearch 销售发票搜索条件
    InvoiceSearch struct {
        Invoice
        Orm          *gorm.DB
        QueryClass   constvar.InvoiceQueryClass
        KeywordType  constvar.InvoiceKeywordType
        Keyword      string
        PageNum      int
        PageSize     int
        PrincipalIds []int
    }
)
 
func (Invoice) TableName() string {
    return "invoice"
}
 
func NewInvoiceSearch() *InvoiceSearch {
    return &InvoiceSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *InvoiceSearch) SetSourceType(sourceType constvar.InvoiceSourceType) *InvoiceSearch {
    slf.SourceType = sourceType
    return slf
}
 
func (slf *InvoiceSearch) SetSourceId(sourceId int) *InvoiceSearch {
    slf.SourceId = sourceId
    return slf
}
 
func (slf *InvoiceSearch) SetPrincipalIds(principalIds []int) *InvoiceSearch {
    slf.PrincipalIds = principalIds
    return slf
}
 
func (slf *InvoiceSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&Invoice{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    if slf.SourceType > 0 {
        db = db.Where("source_type = ?", slf.SourceType)
    }
 
    if slf.SourceId > 0 {
        db = db.Where("source_id = ?", slf.SourceId)
    }
    if slf.Number != "" {
        db.Where("number = ?", slf.Number)
    }
    if len(slf.PrincipalIds) > 0 {
        db = db.Where("principal_id in ?", slf.PrincipalIds)
    }
 
    return db
}
 
func (slf *InvoiceSearch) Create(record *Invoice) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *InvoiceSearch) CreateBatch(records []*Invoice) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
func (slf *InvoiceSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&Invoice{}).Error
}
 
func (slf *InvoiceSearch) Update(record *Invoice) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *InvoiceSearch) FindAll() ([]*Invoice, error) {
    var db = slf.build()
    var record = make([]*Invoice, 0)
    err := db.Find(&record).Error
    return record, err
}
 
func (slf *InvoiceSearch) Count() (int64, error) {
    var db = slf.build()
    var total int64
    err := db.Count(&total).Error
    return total, err
}
 
func (slf *InvoiceSearch) MaxAutoIncr() (int, error) {
    type Result struct {
        Max int
    }
 
    var (
        result Result
        db     = slf.build()
    )
 
    err := db.Select("MAX(id) as max").Scan(&result).Error
    if err != nil {
        return result.Max, fmt.Errorf("max err: %v", err)
    }
    return result.Max, nil
}
 
func (slf *InvoiceSearch) SetNumber(number string) *InvoiceSearch {
    slf.Number = number
    return slf
}
 
func (slf *InvoiceSearch) SetId(id int) *InvoiceSearch {
    slf.Id = id
    return slf
}
 
func (slf *InvoiceSearch) SetOrm(tx *gorm.DB) *InvoiceSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *InvoiceSearch) First() (*Invoice, error) {
    var db = slf.build()
    var record = new(Invoice)
    err := db.First(record).Error
    return record, err
}
 
func (slf *InvoiceSearch) Updates(values interface{}) error {
    var db = slf.build()
    return db.Updates(values).Error
}
 
func (slf *InvoiceSearch) Save(record *Invoice) error {
    if record.Id == 0 {
        return errors.New("id为空")
    }
    var db = slf.build()
 
    if err := db.Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *InvoiceSearch) Find() ([]*Invoice, int64, error) {
    var db = slf.build()
    var records = make([]*Invoice, 0)
    var total int64
    if err := db.Count(&total).Error; err != nil {
        return records, total, err
    }
    if slf.PageNum > 0 && slf.PageSize > 0 {
        db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
    }
 
    err := db.
        Preload("Client").
        Preload("InvoiceType").
        Preload("InvoiceStatus").
        Preload("CourierCompany").
        Find(&records).Error
    return records, total, err
}
 
// InitDefaultData 初始化数据
func (slf *InvoiceSearch) InitDefaultData() error {
    var (
        db          = slf.Orm.Table(slf.TableName())
        total int64 = 0
    )
    if err := db.Count(&total).Error; err != nil {
        return err
    }
    if total != 0 {
        return nil
    }
    records := []*Invoice{}
    return slf.CreateBatch(records)
}