zhangqian
2023-11-01 530fed8ec225453572d57b15c200ab062c335457
model/receipt.go
@@ -5,39 +5,56 @@
   "aps_crm/pkg/mysqlx"
   "errors"
   "fmt"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
)
type (
   // Receipt 收款单
   Receipt struct {
      Id            int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ClientId      int    `gorm:"client_id" json:"clientId"`            // 客户id
      SourceType    int    `gorm:"source_type" json:"sourceType"`        // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)
      SourceId      int    `gorm:"source_id" json:"sourceId"`            // 源单id
      PrincipalId   int    `gorm:"principal_id" json:"principalId"`      // 负责人id
      ReceiptDate   string `gorm:"receipt_date" json:"receiptDate"`      // 收款日期
      MoneyType     string `gorm:"money_type" json:"moneyType"`          // 币种
      PaymentTypeId int    `gorm:"payment_type_id" json:"paymentTypeId"` // 收款方式ID
      BankAccountId int    `gorm:"bank_account_id" json:"bankAccountId"` // 账户id
      Remark        string `gorm:"remark" json:"remark"`                 // 备注
      FileId        int    `gorm:"file_id" json:"fileId"`                // 附件id
      Id            int                        `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ClientId      int                        `gorm:"column:client_id;type:int;not null;default 0;comment:客户id" json:"clientId"`                                // 客户id
      Client        Client                     `gorm:"foreignKey:ClientId" json:"client"`                                                                        // 客户id
      SourceType    constvar.ReceiptSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)" json:"sourceType"` // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)
      SourceId      int                        `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"`                               // 源单id
      PrincipalId   int                        `gorm:"column:principal_id;type:int;not null;default 0;comment:负责人id" json:"principalId"`                         // 负责人id
      Principal     User                       `gorm:"foreignKey:PrincipalId" json:"principal"`                                                                  // 负责人id
      ReceiptDate   string                     `gorm:"column:receipt_date;type:varchar(255);not null;default '';comment:收款日期" json:"receiptDate"`                // 收款日期
      MoneyType     string                     `gorm:"column:money_type;type:varchar(255);not null;default '';comment:币种" json:"moneyType"`                      // 币种
      Amount        decimal.Decimal            `gorm:"column:amount;type:decimal(12,2);not null;default '0.00';comment:收款金额" json:"amount"`                      // 收款金额
      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"`
      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"`
      Remark        string                     `gorm:"column:remark;type:varchar(255);not null;default '';comment:备注" json:"remark"` // 备注
      FileId        int                        `gorm:"column:file_id;type:int;not null;default 0;comment:附件id" json:"fileId"`        // 附件id
      CrmModel
   }
   // ReceiptSearch 收款单搜索条件
   ReceiptSearch struct {
      Receipt
      Orm         *gorm.DB
      QueryClass  constvar.ReceiptQueryClass
      KeywordType constvar.ReceiptKeywordType
      Keyword     string
      PageNum     int
      PageSize    int
      Orm          *gorm.DB
      QueryClass   constvar.ReceiptQueryClass
      KeywordType  constvar.ReceiptKeywordType
      Keyword      string
      PageNum      int
      PageSize     int
      PrincipalIds []int
   }
)
func (Receipt) TableName() string {
func (slf *Receipt) TableName() string {
   return "receipt"
}
func (slf *Receipt) AfterFind(db *gorm.DB) (err error) {
   err = slf.CrmModel.AfterFind(db)
   if slf.CrmModel.ID == 0 {
      slf.CrmModel.ID = uint(slf.Id)
   }
   slf.CrmModel.SetNumber(constvar.NumberPrefixOfReceipt)
   return nil
}
func NewReceiptSearch() *ReceiptSearch {
@@ -46,10 +63,42 @@
   }
}
func (slf *ReceiptSearch) SetSourceType(sourceType constvar.ReceiptSourceType) *ReceiptSearch {
   slf.SourceType = sourceType
   return slf
}
func (slf *ReceiptSearch) SetSourceId(sourceId int) *ReceiptSearch {
   slf.SourceId = sourceId
   return slf
}
func (slf *ReceiptSearch) SetPrincipalIds(principalIds []int) *ReceiptSearch {
   slf.PrincipalIds = principalIds
   return slf
}
func (slf *ReceiptSearch) SetPage(page, size int) *ReceiptSearch {
   slf.PageNum, slf.PageSize = page, size
   return slf
}
func (slf *ReceiptSearch) build() *gorm.DB {
   var db = slf.Orm.Model(&Receipt{})
   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.ClientId != 0 {
      db = db.Where("client_id = ?", slf.ClientId)
   }
   if len(slf.PrincipalIds) != 0 {
      db = db.Where("principal_id in ?", slf.PrincipalIds)
   }
   return db
@@ -84,6 +133,11 @@
func (slf *ReceiptSearch) SetId(id int) *ReceiptSearch {
   slf.Id = id
   return slf
}
func (slf *ReceiptSearch) SetClientId(clientId int) *ReceiptSearch {
   slf.ClientId = clientId
   return slf
}
@@ -128,7 +182,7 @@
      db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
   }
   err := db.Find(&records).Error
   err := db.Preload("Principal").Preload("Client").Preload("PaymentType").Preload("BankAccount").Order("created_at desc").Find(&records).Error
   return records, total, err
}