fix
wangpengfei
2023-08-28 c5a0eb549cba2cd358a2d0496c44f3a289f15d9c
model/quotation.go
@@ -8,20 +8,22 @@
type (
   // Quotation 报价单
   Quotation struct {
      Id                int         `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ClientId          int         `json:"client_id" gorm:"column:client_id;type:int;comment:客户id"`
      Number            string      `json:"number" gorm:"column:number;type:varchar(255);comment:报价单号"`
      QuotationStatusId int         `json:"quotation_status_id" gorm:"column:quotation_status_id;type:int;comment:报价单状态id"`
      ValidityDate      *CustomTime `json:"validity_date" gorm:"column:validity_date;type:datetime;comment:有效期"`
      ContactId         int         `json:"contact_id" gorm:"column:contact_id;type:int;comment:联系人id"`
      MemberId          int         `json:"member_id" gorm:"column:member_id;type:int;comment:负责人id"`
      SaleChanceId      int         `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int;comment:销售机会id"`
      Conditions        string      `json:"conditions" gorm:"column:conditions;type:text;comment:报价条件"`
      File              string      `json:"file" gorm:"column:file;type:varchar(255);comment:附件"`
      Client            Client      `json:"client" gorm:"foreignKey:ClientId"`
      Contact           Contact     `json:"contact" gorm:"foreignKey:ContactId"`
      SaleChance        SaleChance  `json:"sale_chance" gorm:"foreignKey:SaleChanceId"`
      Products          []Product   `json:"products" gorm:"many2many:quotation_product"`
      Id                int             `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ClientId          int             `json:"client_id" gorm:"column:client_id;type:int;comment:客户id"`
      Number            string          `json:"number" gorm:"column:number;type:varchar(255);comment:报价单号"`
      QuotationStatusId int             `json:"quotation_status_id" gorm:"column:quotation_status_id;type:int;comment:报价单状态id"`
      QuotationStatus   QuotationStatus `json:"quotation_status" gorm:"foreignKey:QuotationStatusId"`
      ValidityDate      *CustomTime     `json:"validity_date" gorm:"column:validity_date;type:datetime;comment:有效期"`
      ContactId         int             `json:"contact_id" gorm:"column:contact_id;type:int;comment:联系人id"`
      MemberId          int             `json:"member_id" gorm:"column:member_id;type:int;comment:负责人id"`
      Member            User            `json:"member" gorm:"foreignKey:MemberId"`
      SaleChanceId      int             `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int;comment:销售机会id"`
      Conditions        string          `json:"conditions" gorm:"column:conditions;type:text;comment:报价条件"`
      File              string          `json:"file" gorm:"column:file;type:varchar(255);comment:附件"`
      Client            Client          `json:"client" gorm:"foreignKey:ClientId"`
      Contact           Contact         `json:"contact" gorm:"foreignKey:ContactId"`
      SaleChance        SaleChance      `json:"sale_chance" gorm:"foreignKey:SaleChanceId"`
      Products          []Product       `json:"products" gorm:"many2many:quotation_product"`
      gorm.Model        `json:"-"`
   }
@@ -41,9 +43,12 @@
   return "quotation"
}
func NewQuotationSearch() *QuotationSearch {
func NewQuotationSearch(db *gorm.DB) *QuotationSearch {
   if db == nil {
      db = mysqlx.GetDB()
   }
   return &QuotationSearch{
      Orm: mysqlx.GetDB(),
      Orm: db,
   }
}
@@ -57,9 +62,22 @@
      for key, value := range slf.SearchMap {
         switch v := value.(type) {
         case string:
            if key == "validity_date" {
               db = db.Where(key+" = ?", v)
            if key == "number" || key == "validity_date" {
               db = db.Where(key+" LIKE ?", "%"+v+"%")
            }
            if key == "client_name" {
               db = db.Joins("Client").Where("Client.name LIKE ?", "%"+v+"%")
            }
            if key == "contact_name" {
               db = db.Joins("Contact").Where("Contact.name LIKE ?", "%"+v+"%")
            }
            if key == "member_name" {
               db = db.Joins("Member").Where("Member.username LIKE ?", "%"+v+"%")
            }
         case int:
            if key == "client_id" || key == "sale_chance_id" {
               db = db.Where(key+" = ?", v)
@@ -89,7 +107,7 @@
func (slf *QuotationSearch) Find() (*Quotation, error) {
   var db = slf.build()
   var record Quotation
   err := db.Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
   err := db.Preload("Products").Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
   return &record, err
}
@@ -104,7 +122,7 @@
      db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
   }
   err := db.Find(&records).Error
   err := db.Preload("Products").Preload("Member").Preload("QuotationStatus").Preload("Client").Preload("Contact").Order("id desc").Find(&records).Error
   return records, total, err
}
@@ -132,3 +150,7 @@
   slf.SearchMap = searchMap
   return slf
}
func (slf *QuotationSearch) SetIds(ids []int) *QuotationSearch {
   slf.Orm = slf.Orm.Where("id in (?)", ids)
   return slf
}