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"`
|
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;"`
|
}
|
|
// InvoiceSearch 销售发票搜索条件
|
InvoiceSearch struct {
|
Invoice
|
Orm *gorm.DB
|
QueryClass constvar.InvoiceQueryClass
|
KeywordType constvar.InvoiceKeywordType
|
Keyword string
|
PageNum int
|
PageSize int
|
}
|
)
|
|
func (Invoice) TableName() string {
|
return "invoice"
|
}
|
|
func NewInvoiceSearch() *InvoiceSearch {
|
return &InvoiceSearch{
|
Orm: mysqlx.GetDB(),
|
}
|
}
|
|
func (slf *InvoiceSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&Invoice{})
|
if slf.Id != 0 {
|
db = db.Where("id = ?", slf.Id)
|
}
|
|
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) 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)
|
}
|