zhangqian
2023-11-01 530fed8ec225453572d57b15c200ab062c335457
model/quotation.go
@@ -2,6 +2,7 @@
import (
   "aps_crm/pkg/mysqlx"
   "fmt"
   "gorm.io/gorm"
)
@@ -9,6 +10,7 @@
   // Quotation 报价单
   Quotation struct {
      Id                int             `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      QuotationName     string          `json:"quotationName" gorm:"column:quotation_name;type:varchar(255);comment:报价单名称"`
      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"`
@@ -24,6 +26,7 @@
      Contact           Contact         `json:"contact" gorm:"foreignKey:ContactId"`
      SaleChance        SaleChance      `json:"sale_chance" gorm:"foreignKey:SaleChanceId"`
      Products          []Product       `json:"products" gorm:"many2many:quotation_product"`
      CodeStandID       string          `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"`
      gorm.Model        `json:"-"`
   }
@@ -43,9 +46,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,
   }
}
@@ -53,6 +59,9 @@
   var db = slf.Orm.Model(&Quotation{})
   if slf.Id != 0 {
      db = db.Where("id = ?", slf.Id)
   }
   if slf.Number != "" {
      db = db.Where("number = ?", slf.Number)
   }
   if len(slf.SearchMap) > 0 {
@@ -75,9 +84,13 @@
               db = db.Joins("Member").Where("Member.username LIKE ?", "%"+v+"%")
            }
         case int:
            if key == "client_id" || key == "sale_chance_id" {
         case int, float64:
            if key == "client_id" || key == "sale_chance_id" || key == "member_id" {
               db = db.Where(key+" = ?", v)
            }
         case []int:
            if key == "member_ids" {
               db = db.Where("quotation.member_id in ?", v)
            }
         }
      }
@@ -104,7 +117,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
}
@@ -121,6 +134,30 @@
   err := db.Preload("Products").Preload("Member").Preload("QuotationStatus").Preload("Client").Preload("Contact").Order("id desc").Find(&records).Error
   return records, total, err
}
func (slf *QuotationSearch) Count() (int64, error) {
   var db = slf.build()
   var total int64
   err := db.Count(&total).Error
   return total, err
}
func (slf *QuotationSearch) 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 *QuotationSearch) SetId(id int) *QuotationSearch {
@@ -147,6 +184,11 @@
   slf.SearchMap = searchMap
   return slf
}
func (slf *QuotationSearch) SetNumber(number string) *QuotationSearch {
   slf.Number = number
   return slf
}
func (slf *QuotationSearch) SetIds(ids []int) *QuotationSearch {
   slf.Orm = slf.Orm.Where("id in (?)", ids)
   return slf