| | |
| | | package model |
| | | |
| | | import ( |
| | | "aps_crm/constvar" |
| | | "aps_crm/pkg/mysqlx" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | "gorm.io/gorm/clause" |
| | | ) |
| | | |
| | | 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"` |
| | | SaleType int `json:"saleType" gorm:"column:sale_type;type:int;comment:销售类型"` |
| | | SignTime time.Time `json:"signTime" gorm:"column:sign_time;type:datetime;comment:签单时间"` |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | DeliveryDate time.Time `json:"deliveryDate" gorm:"column:delivery_date;type:datetime;comment:交货日期"` |
| | | WechatOrderStatus int `json:"wechatOrderStatus" gorm:"column:wechat_order_status;type:int;comment:微信订单状态"` |
| | | 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: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:物流费用"` |
| | | 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"` |
| | | 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:"amount_receivable" json:"amountReceivable"` // 应收金额 |
| | | AmountReceived decimal.Decimal `gorm:"amount_received" json:"amountReceived"` // 已收金额 |
| | | AmountInvoiced decimal.Decimal `gorm:"amount_invoiced" json:"amountInvoiced"` // 已开票金额 |
| | | AmountUnInvoiced decimal.Decimal `gorm:"-" json:"amountUnInvoiced"` // 未开票金额 |
| | | gorm.Model `json:"-"` |
| | | } |
| | | |
| | | SalesDetailsSearch struct { |
| | | SalesDetails |
| | | |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | |
| | | KeywordType constvar.SalesDetailsKeywordType |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | Preload bool |
| | | } |
| | | ) |
| | | |
| | |
| | | |
| | | func (slf *SalesDetailsSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&SalesDetails{}) |
| | | if slf.Keyword != "" { |
| | | db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") |
| | | } |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | |
| | | switch slf.KeywordType { |
| | | case constvar.SalesDetailsKeywordTypeCustomerName: |
| | | db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesDetailsKeywordTypeOrderNumber: |
| | | db = db.Where("number like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesDetailsKeywordTypeSignTime: |
| | | db = db.Where("sign_time like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesDetailsKeywordTypePrincipal: |
| | | db = db.Joins("left join user on user.id = sales_details.member_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | case constvar.SalesDetailsKeywordTypeProductName: |
| | | //db = db.Joins("left join sales_details_product sdp on sdp.sales_details_id = sales_details.id left join product").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword)) |
| | | |
| | | } |
| | | if slf.Preload { |
| | | db = db.Preload("Products"). |
| | | Preload("Client"). |
| | | Preload("SaleChance"). |
| | | Preload("WechatOrderStatus"). |
| | | Preload("Client") |
| | | } |
| | | |
| | | return db |
| | |
| | | db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) |
| | | } |
| | | |
| | | err := db.Preload("Products").Preload("Client").Find(&records).Error |
| | | err := db.Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *SalesDetailsSearch) SetKeyword(keyword string) *SalesDetailsSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesDetailsSearch) SetKeywordType(keywordType constvar.SalesDetailsKeywordType) *SalesDetailsSearch { |
| | | slf.KeywordType = keywordType |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesDetailsSearch) SetPreload(preload bool) *SalesDetailsSearch { |
| | | slf.Preload = preload |
| | | return slf |
| | | } |
| | | |
| | |
| | | func (slf *SalesDetailsSearch) SetOrder(order string) *SalesDetailsSearch { |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |
| | | } |