| | |
| | | package model |
| | | |
| | | import ( |
| | | "aps_crm/constvar" |
| | | "aps_crm/pkg/mysqlx" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | "gorm.io/gorm/clause" |
| | | ) |
| | | |
| | | type ( |
| | | SalesRefund struct { |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | ClientId int `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:退款单号"` |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | RefundDate time.Time `json:"refundDate" gorm:"column:refund_date;type:datetime;comment:退款日期"` |
| | | RefundMethod string `json:"refundMethod" gorm:"column:refund_method;type:varchar(255);comment:退款方式"` |
| | | AccountId int `json:"accountId" gorm:"column:account_id;type:int;comment:账户"` |
| | | IsInvoice int `json:"isInvoice" gorm:"column:is_invoice;type:int;comment:是否开票"` |
| | | Reason string `json:"reason" gorm:"column:reason;type:varchar(255);comment:退款原因"` |
| | | Products []Product `json:"products" gorm:"many2many:salesRefund_product;"` |
| | | gorm.Model `json:"-"` |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | ClientId int `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"` |
| | | Client Client `json:"client" gorm:"foreignKey:ClientId"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:退款单号"` |
| | | SourceType constvar.RefundSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售退货)" json:"sourceType"` // 来源类型(1销售退货) |
| | | SalesReturnId int `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"` // 源单id |
| | | SalesReturn SalesReturn `gorm:"foreignKey:SalesReturnId" json:"salesReturn"` |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | Member User `json:"member" gorm:"foreignKey:MemberId"` |
| | | RefundDate string `json:"refundDate" gorm:"column:refund_date;type:varchar(255);comment:退款日期"` |
| | | PaymentTypeId int `gorm:"column:payment_type_id;type:int;not null;default 0;comment:收款方式ID" json:"paymentTypeId"` // 收款方式ID |
| | | PaymentType PaymentType `gorm:"foreignKey:PaymentTypeId" json:"paymentType"` |
| | | RefundTypeId int `gorm:"column:refund_type_id;type:int;not null;default 0;comment:收款方式ID" json:"refundTypeId"` // 退款方式ID |
| | | RefundType RefundType `gorm:"foreignKey:RefundTypeId" json:"RefundType"` //退款方式 |
| | | BankAccountId int `gorm:"column:bank_account_id;type:int;not null;default 0;comment:账户id" json:"bankAccountId"` // 账户id |
| | | BankAccount BankAccount `gorm:"foreignKey:BankAccountId" json:"bankAccount"` |
| | | IsInvoice string `json:"isInvoice" gorm:"column:is_invoice;type:varchar(255);comment:是否开票"` |
| | | Reason string `json:"reason" gorm:"column:reason;type:varchar(255);comment:退款原因"` |
| | | Products []*Product `json:"products" gorm:"many2many:salesRefund_product;"` |
| | | AmountTotal decimal.Decimal `gorm:"column:amount_total;type:decimal(12,2);comment:价税合计" json:"amountTotal"` // 价税合计 |
| | | CodeStandID string `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"` |
| | | CrmModel |
| | | } |
| | | |
| | | SalesRefundSearch struct { |
| | | SalesRefund |
| | | Orm *gorm.DB |
| | | Orm *gorm.DB |
| | | KeywordType constvar.SalesRefundKeywordType |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | Preload bool |
| | | Ids []int |
| | | MemberIds []int |
| | | } |
| | | ) |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetOrm(tx *gorm.DB) *SalesRefundSearch { |
| | | slf.Orm = tx |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&SalesRefund{}) |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | |
| | | if len(slf.Ids) != 0 { |
| | | db = db.Where("id in ?", slf.Ids) |
| | | } |
| | | |
| | | if slf.SourceType != 0 { |
| | | db = db.Where("source_type = ?", slf.SourceType) |
| | | } |
| | | |
| | | if slf.SalesReturnId != 0 { |
| | | db = db.Where("source_id = ?", slf.SalesReturnId) |
| | | } |
| | | |
| | | if slf.Preload { |
| | | db = db.Preload("Client"). |
| | | Preload("PaymentType"). |
| | | Preload("BankAccount"). |
| | | Preload("Products"). |
| | | Preload("SalesReturn") |
| | | } |
| | | if slf.KeywordType != "" { |
| | | switch slf.KeywordType { |
| | | case constvar.SalesRefundKeywordRefundNumber: |
| | | db = db.Where("number like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordClientName: |
| | | db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordRefundDate: |
| | | db = db.Where("refund_date like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordAccount: |
| | | db = db.Joins("BankAccount").Where("BankAccount name like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordIsInvoice: |
| | | db = db.Where("is_invoice like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordPaymentType: |
| | | db = db.Joins("PaymentType").Where("PaymentType.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordPrincipal: |
| | | db = db.Joins("left join user on user.id = sales_refund.member_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordUpdateAt: |
| | | db = db.Where("updated_at like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesRefundKeywordPriceTotal: |
| | | db = db.Where("amount_total like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | |
| | | } |
| | | } |
| | | |
| | | if len(slf.MemberIds) > 0 { |
| | | db = db.Where("sales_refund.member_id in ?", slf.MemberIds) |
| | | } |
| | | if slf.Number != "" { |
| | | db = db.Where("number = ?", slf.Number) |
| | | } |
| | | |
| | | return db |
| | |
| | | return db.Updates(record).Error |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) Find() (*SalesRefund, error) { |
| | | func (slf *SalesRefundSearch) First() (*SalesRefund, error) { |
| | | var db = slf.build() |
| | | var record = new(SalesRefund) |
| | | err := db.First(record).Error |
| | | return record, err |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) FindAll() ([]*SalesRefund, error) { |
| | | func (slf *SalesRefundSearch) Find() ([]*SalesRefund, error) { |
| | | var db = slf.build() |
| | | var records = make([]*SalesRefund, 0) |
| | | err := db.Preload("Products").Find(&records).Error |
| | | |
| | | err := db.Find(&records).Error |
| | | return records, err |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) FindAll() ([]*SalesRefund, int64, error) { |
| | | var db = slf.build() |
| | | var records = make([]*SalesRefund, 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) |
| | | } |
| | | |
| | | if slf.PageNum > 0 && slf.PageSize > 0 { |
| | | db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) |
| | | } |
| | | |
| | | err := db.Preload("BankAccount").Preload("PaymentType").Preload("Member").Preload("Products").Order("id desc").Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) Count() (int64, error) { |
| | | var db = slf.build() |
| | | var total int64 |
| | | err := db.Count(&total).Error |
| | | return total, err |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) 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 *SalesRefundSearch) SetId(id int) *SalesRefundSearch { |
| | | slf.Id = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetIds(id []int) *SalesRefundSearch { |
| | | slf.Ids = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetSourceType(sourceType constvar.RefundSourceType) *SalesRefundSearch { |
| | | slf.SourceType = sourceType |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetSourceId(id int) *SalesRefundSearch { |
| | | slf.SalesReturnId = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetPreload(preload bool) *SalesRefundSearch { |
| | | slf.Preload = preload |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetKeywordType(keywordType constvar.SalesRefundKeywordType) *SalesRefundSearch { |
| | | slf.KeywordType = keywordType |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetKeyword(keyword string) *SalesRefundSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetPage(page, size int) *SalesRefundSearch { |
| | | slf.PageNum, slf.PageSize = page, size |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetOrder(order string) *SalesRefundSearch { |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetNumber(number string) *SalesRefundSearch { |
| | | slf.Number = number |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) UpdateByMap(data map[string]interface{}) error { |
| | | var db = slf.build() |
| | | return db.Updates(data).Error |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetMemberIds(memberIds []int) *SalesRefundSearch { |
| | | slf.MemberIds = memberIds |
| | | return slf |
| | | } |