wangpengfei
2023-08-15 2387b4051c095af26f158500596d21167955f971
model/salesReturn.go
@@ -3,7 +3,10 @@
import (
   "aps_crm/constvar"
   "aps_crm/pkg/mysqlx"
   "fmt"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "gorm.io/gorm/clause"
)
type (
@@ -17,24 +20,29 @@
      Repository          string                         `json:"repository" gorm:"column:repository;type:varchar(255);comment:仓库"`
      MemberId            int                            `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"`
      Member              User                           `json:"Member"  gorm:"foreignKey:MemberId"`
      ReturnDate          string                         `json:"returnDate" gorm:"column:return_date;type:datetime(3);comment:退货日期"`          //退货日期
      SalesReturnStatusId int                            `json:"salesReturnStatusId" gorm:"column:sales_return_status;type:int;comment:退货状态"` //退货状态id
      SalesReturnStatus   SalesReturnStatus              `json:"SalesReturnStatus" gorm:"foreignKey:SalesReturnStatusId"`                     //退货状态
      CreatorId           int                            `json:"-" gorm:"column:creator_id;type:int;comment:创建人id"`                           //创建人ID
      Creator             User                           `json:"-"  gorm:"foreignKey:CreatorId"`                                              //创建人信息
      Reason              string                         `json:"reason" gorm:"column:reason;type:varchar(255);comment:退货原因"`                  //退货原因
      Products            []*Product                     `json:"products" gorm:"many2many:salesReturn_product;"`                              //退货产品
      ReturnDate          string                         `json:"returnDate" gorm:"column:return_date;type:datetime(3);comment:退货日期"`                     //退货日期
      SalesReturnStatusId int                            `json:"salesReturnStatusId" gorm:"column:sales_return_status;type:int;comment:退货状态"`            //退货状态id
      SalesReturnStatus   SalesReturnStatus              `json:"SalesReturnStatus" gorm:"foreignKey:SalesReturnStatusId"`                                //退货状态
      CreatorId           int                            `json:"-" gorm:"column:creator_id;type:int;comment:创建人id"`                                      //创建人ID
      Creator             User                           `json:"-"  gorm:"foreignKey:CreatorId"`                                                         //创建人信息
      Reason              string                         `json:"reason" gorm:"column:reason;type:varchar(255);comment:退货原因"`                             //退货原因
      Products            []*Product                     `json:"products" gorm:"many2many:salesReturn_product;"`                                         //退货产品
      AmountShouldRefund  decimal.Decimal                `gorm:"column:amount_should_refund;type:decimal(12,2);comment:应退款金额" json:"amountShouldRefund"` // 应退款金额
      AmountHasRefund     decimal.Decimal                `gorm:"column:amount_has_refund;type:decimal(12,2);comment:已退款金额" json:"amountHasRefund"`       // 已退款金额
      CrmModel
   }
   SalesReturnSearch struct {
      SalesReturn
      Orm      *gorm.DB
      Keyword  string
      OrderBy  string
      PageNum  int
      PageSize int
      Orm         *gorm.DB
      KeywordType constvar.SalesReturnKeywordType
      Keyword     string
      OrderBy     string
      PageNum     int
      PageSize    int
      Preload     bool
   }
)
@@ -49,11 +57,37 @@
func (slf *SalesReturnSearch) build() *gorm.DB {
   var db = slf.Orm.Model(&SalesReturn{})
   if slf.Keyword != "" {
      db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
   }
   if slf.Id != 0 {
      db = db.Where("id = ?", slf.Id)
   }
   if slf.Preload {
      db = db.Preload("Client").
         Preload("Member").
         Preload("SalesReturnStatus").
         Preload("Products")
   }
   if slf.KeywordType != "" {
      switch slf.KeywordType {
      case constvar.SalesReturnKeywordReturnNumber:
         db = db.Where("number like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      case constvar.SalesReturnKeywordClientName:
         db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      case constvar.SalesReturnKeywordReturnDate:
         db = db.Where("return_date like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      case constvar.SalesReturnKeywordStatus:
         db = db.Joins("SalesReturnStatus").Where("SalesReturnStatus name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      case constvar.SalesReturnKeywordRepository:
         db = db.Where("repository like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      case constvar.SalesReturnKeywordAmountShouldRefund:
         db = db.Where("amount_should_refund = ?", slf.Keyword)
      case constvar.SalesReturnKeywordPrincipal:
         db = db.Joins("left join user on user.id = sales_refund.member_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      case constvar.SalesReturnKeywordAmountHasRefund:
         db = db.Where("amount_has_refund = ?", slf.Keyword)
      }
   }
   return db
@@ -106,6 +140,11 @@
   return slf
}
func (slf *SalesReturnSearch) SetKeywordType(keywordType constvar.SalesReturnKeywordType) *SalesReturnSearch {
   slf.KeywordType = keywordType
   return slf
}
func (slf *SalesReturnSearch) SetPage(page, size int) *SalesReturnSearch {
   slf.PageNum, slf.PageSize = page, size
   return slf
@@ -115,3 +154,8 @@
   slf.OrderBy = order
   return slf
}
func (slf *SalesReturnSearch) SetPreload(preload bool) *SalesReturnSearch {
   slf.Preload = preload
   return slf
}