liujiandao
2023-11-18 115bd9b51f5d8eade4658f844de37516486c60e7
model/salesDetails.go
@@ -11,32 +11,42 @@
type (
   SalesDetails struct {
      Id                  int               `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ClientId            int               `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"`
      Client              Client            `json:"client" gorm:"foreignKey:ClientId"`
      Number              string            `json:"number" gorm:"column:number;type:varchar(255);comment:销售子单号"`
      SaleChanceId        int               `json:"saleChanceId" gorm:"column:sale_chance_id;type:int;comment:销售机会id"`
      SaleChance          SaleChance        `json:"saleChance" gorm:"foreignKey:SaleChanceId"`
      SaleType            int               `json:"saleType" gorm:"column:sale_type;type:int;comment:销售类型"`
      SignTime            string            `json:"signTime" gorm:"column:sign_time;type:varchar(255);comment:签单时间"`
      MemberId            int               `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"`
      DeliveryDate        string            `json:"deliveryDate" gorm:"column:delivery_date;type:varchar(255);comment:交货日期"`
      WechatOrderStatusId int               `json:"wechatOrderStatusId" gorm:"column:wechat_order_status;type:int;comment:微信订单状态"`
      WechatOrderStatus   WechatOrderStatus `json:"wechatOrderStatus" gorm:"foreignKey:WechatOrderStatusId"`
      Address             string            `json:"address" gorm:"column:address;type:varchar(255);comment:地址"`
      Phone               string            `json:"phone" gorm:"column:phone;type:varchar(255);comment:电话"`
      Addressee           string            `json:"addressee" gorm:"column:addressee;type:varchar(255);comment:收件人"`
      Conditions          string            `json:"conditions" gorm:"column:conditions;type:text;comment:条件"`
      Remark              string            `json:"remark" gorm:"column:remark;type:text;comment:备注"`
      Products            []*Product        `json:"products" gorm:"many2many:sales_details_product;"`
      LogisticCompany     string            `json:"logisticCompany" gorm:"column:logistic_company;type:varchar(255);comment:物流公司"`
      LogisticNumber      string            `json:"logisticNumber" gorm:"column:logistic_number;type:varchar(255);comment:物流单号"`
      LogisticCost        float64           `json:"logisticCost" gorm:"column:logistic_cost;type:decimal(10,2);comment:物流费用"`
      AmountReceivable    decimal.Decimal   `gorm:"column:amount_receivable;type:decimal(12,2);comment:应收金额" json:"amountReceivable"`    // 应收金额
      AmountReceived      decimal.Decimal   `gorm:"column:amount_received;type:decimal(12,2);comment:已收金额" json:"amountReceived"`        // 已收金额
      AmountInvoiced      decimal.Decimal   `gorm:"column:amount_invoiced;type:decimal(12,2);comment:已开票金额" json:"amountInvoiced"`       // 已开票金额
      AmountUnInvoiced    decimal.Decimal   `gorm:"column:amount_not_invoiced;type:decimal(12,2);comment:未开票金额" json:"amountUnInvoiced"` // 未开票金额
      gorm.Model          `json:"-"`
      Id                  int                         `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ClientId            int                         `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"`
      Client              Client                      `json:"client" gorm:"foreignKey:ClientId"`
      Number              string                      `json:"number" gorm:"column:number;type:varchar(255);comment:销售明细单号"`
      SaleChanceId        int                         `json:"saleChanceId" gorm:"column:sale_chance_id;type:int;comment:销售机会id"`
      SaleChance          SaleChance                  `json:"saleChance" gorm:"foreignKey:SaleChanceId"`
      SaleType            int                         `json:"saleType" gorm:"column:sale_type;type:int;comment:销售类型"`
      SignTime            string                      `json:"signTime" gorm:"column:sign_time;type:varchar(255);comment:签单时间"`
      MemberId            int                         `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"`
      Member              User                        `json:"Member"  gorm:"foreignKey:MemberId"`
      DeliveryDate        string                      `json:"deliveryDate" gorm:"column:delivery_date;type:varchar(255);comment:交货日期"`
      WechatOrderStatusId int                         `json:"wechatOrderStatusId" gorm:"column:wechat_order_status;type:int;comment:微信订单状态"`
      WechatOrderStatus   WechatOrderStatus           `json:"wechatOrderStatus" gorm:"foreignKey:WechatOrderStatusId"`
      Address             string                      `json:"address" gorm:"column:address;type:varchar(255);comment:地址"`
      Phone               string                      `json:"phone" gorm:"column:phone;type:varchar(255);comment:电话"`
      Addressee           string                      `json:"addressee" gorm:"column:addressee;type:varchar(255);comment:收件人"`
      Conditions          string                      `json:"conditions" gorm:"column:conditions;type:text;comment:条件"`
      CreatorId           int                         `json:"creatorId" gorm:"column:creator_id;type:int;comment:创建人id"`
      Remark              string                      `json:"remark" gorm:"column:remark;type:text;comment:备注"`
      Products            []*Product                  `json:"products" gorm:"many2many:SalesDetails_Product;"`
      LogisticCompany     string                      `json:"logisticCompany" gorm:"column:logistic_company;type:varchar(255);comment:物流公司"`
      LogisticNumber      string                      `json:"logisticNumber" gorm:"column:logistic_number;type:varchar(255);comment:物流单号"`
      LogisticCost        float64                     `json:"logisticCost" gorm:"column:logistic_cost;type:decimal(10,2);comment:物流费用"`
      AmountReceivable    decimal.Decimal             `gorm:"column:amount_receivable;type:decimal(12,2);comment:应收金额" json:"amountReceivable"`    // 应收金额
      AmountReceived      decimal.Decimal             `gorm:"column:amount_received;type:decimal(12,2);comment:已收金额" json:"amountReceived"`        // 已收金额
      AmountInvoiced      decimal.Decimal             `gorm:"column:amount_invoiced;type:decimal(12,2);comment:已开票金额" json:"amountInvoiced"`       // 已开票金额
      AmountUnInvoiced    decimal.Decimal             `gorm:"column:amount_not_invoiced;type:decimal(12,2);comment:未开票金额" json:"amountUnInvoiced"` // 未开票金额
      AmountTotal         decimal.Decimal             `gorm:"column:amount_total;type:decimal(12,2);comment:价税合计" json:"amountTotal"`              // 价税合计
      CodeStandID         string                      `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"`
      DeliverType         int                         `json:"deliverType" gorm:"column:deliver_type;type:int;comment:交付类型(1.一次发货,2.多次发货)"`
      QuotationId         int                         `json:"quotationId" gorm:"column:quotation_id;type:int;comment:报价单id"`
      Quotation           Quotation                   `json:"quotation" gorm:"foreignKey:QuotationId"`
      Status              constvar.SalesDetailsStatus `json:"status" gorm:"column:status;type:int;comment:状态"`
      Source              string                      `json:"source" gorm:"column:source;type:varchar(255);comment:订单来源"`
      ProjectId           string                      `json:"projectId" gorm:"column:project_id;type:varchar(255);comment:aps项目id"`
      CrmModel
   }
   SalesDetailsSearch struct {
@@ -48,6 +58,7 @@
      PageNum     int
      PageSize    int
      Preload     bool
      MemberIds   []int
   }
)
@@ -67,6 +78,17 @@
      db = db.Where("id = ?", slf.Id)
   }
   if slf.SaleChanceId != 0 {
      db = db.Where("sale_chance_id = ?", slf.SaleChanceId)
   }
   if slf.ClientId != 0 {
      db = db.Where("client_id = ?", slf.ClientId)
   }
   if slf.Number != "" {
      db = db.Where("number = ?", slf.Number)
   }
   switch slf.KeywordType {
   case constvar.SalesDetailsKeywordTypeCustomerName:
      db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
@@ -80,12 +102,18 @@
      db = db.Joins("left join sales_details_product sdp on sdp.sales_details_id = sales_details.id left join products on sdp.product_id = products.id").Where("products.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   }
   if len(slf.MemberIds) > 0 {
      db = db.Where("sales_details.member_id in ?", slf.MemberIds)
   }
   if slf.Preload {
      db = db.Preload("Products").
         Preload("Client").
         Preload("Member").
         Preload("SaleChance").
         Preload("WechatOrderStatus").
         Preload("Client")
         Preload("Client").
         Preload("Quotation")
   }
   return db
@@ -101,13 +129,53 @@
   return db.Delete(&SalesDetails{}).Error
}
func (slf *SalesDetailsSearch) DeleteByIds(ids []int) error {
   var db = slf.build()
   db = db.Where("id in ?", ids)
   return db.Delete(&SalesDetails{}).Error
}
func (slf *SalesDetailsSearch) Update(record *SalesDetails) error {
   var db = slf.build()
   return db.Updates(record).Error
}
func (slf *SalesDetailsSearch) Count() (int64, error) {
   var db = slf.build()
   var total int64
   err := db.Count(&total).Error
   return total, err
}
func (slf *SalesDetailsSearch) 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 *SalesDetailsSearch) SetId(id int) *SalesDetailsSearch {
   slf.Id = id
   return slf
}
func (slf *SalesDetailsSearch) SetClientId(clientId int) *SalesDetailsSearch {
   slf.ClientId = clientId
   return slf
}
func (slf *SalesDetailsSearch) SetSaleChanceId(saleChanceId int) *SalesDetailsSearch {
   slf.SaleChanceId = saleChanceId
   return slf
}
@@ -133,7 +201,7 @@
      db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
   }
   err := db.Find(&records).Error
   err := db.Order("id desc").Find(&records).Error
   return records, total, err
}
@@ -144,6 +212,11 @@
func (slf *SalesDetailsSearch) SetKeywordType(keywordType constvar.SalesDetailsKeywordType) *SalesDetailsSearch {
   slf.KeywordType = keywordType
   return slf
}
func (slf *SalesDetailsSearch) SetMemberIds(memberIds []int) *SalesDetailsSearch {
   slf.MemberIds = memberIds
   return slf
}
@@ -159,6 +232,11 @@
func (slf *SalesDetailsSearch) SetOrder(order string) *SalesDetailsSearch {
   slf.OrderBy = order
   return slf
}
func (slf *SalesDetailsSearch) SetNumber(number string) *SalesDetailsSearch {
   slf.Number = number
   return slf
}
@@ -215,3 +293,8 @@
   amount = record.AmountUnInvoiced.Add(amount)
   return slf.UpdateByMap(map[string]interface{}{"amount_not_invoiced": amount})
}
func (slf *SalesDetailsSearch) UpdateProducts(record *SalesDetails, newProducts, removedProducts []*Product) error {
   var db = slf.build()
   return db.Updates(record).Error
}