fix
wangpengfei
2023-08-28 c5a0eb549cba2cd358a2d0496c44f3a289f15d9c
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,30 @@
      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:varchar(255);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"`       // 已退款金额
      AmountTotal         decimal.Decimal                `gorm:"column:amount_total;type:decimal(12,2);comment:退货产品总金额" json:"-"`
      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
      Ids         []int
   }
)
@@ -47,13 +56,47 @@
   }
}
func (slf *SalesReturnSearch) SetOrm(tx *gorm.DB) *SalesReturnSearch {
   slf.Orm = tx
   return slf
}
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 len(slf.Ids) != 0 {
      db = db.Where("id in ?", slf.Ids)
   }
   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
@@ -80,7 +123,7 @@
   return db.Updates(record).Error
}
func (slf *SalesReturnSearch) Find() (*SalesReturn, error) {
func (slf *SalesReturnSearch) First() (*SalesReturn, error) {
   var db = slf.build()
   var record = new(SalesReturn)
   err := db.First(record).Error
@@ -97,9 +140,16 @@
   if slf.PageNum > 0 && slf.PageSize > 0 {
      db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
   }
   err := db.Preload("Member").Preload("Client").Preload("Products").Order("id desc").Find(&records).Error
   return records, total, err
}
func (slf *SalesReturnSearch) Find() ([]*SalesReturn, error) {
   var db = slf.build()
   var records = make([]*SalesReturn, 0)
   err := db.Find(&records).Error
   return records, err
}
func (slf *SalesReturnSearch) SetId(id int) *SalesReturnSearch {
@@ -107,8 +157,18 @@
   return slf
}
func (slf *SalesReturnSearch) SetIds(id []int) *SalesReturnSearch {
   slf.Ids = id
   return slf
}
func (slf *SalesReturnSearch) SetKeyword(keyword string) *SalesReturnSearch {
   slf.Keyword = keyword
   return slf
}
func (slf *SalesReturnSearch) SetKeywordType(keywordType constvar.SalesReturnKeywordType) *SalesReturnSearch {
   slf.KeywordType = keywordType
   return slf
}
@@ -121,3 +181,8 @@
   slf.OrderBy = order
   return slf
}
func (slf *SalesReturnSearch) SetPreload(preload bool) *SalesReturnSearch {
   slf.Preload = preload
   return slf
}