| | |
| | | |
| | | import ( |
| | | "aps_crm/pkg/mysqlx" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type ( |
| | | Contract struct { |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | ClientId int `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"` |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:合同编号"` |
| | | QuotationId int `json:"quotationId" gorm:"column:quotation_id;type:int;comment:报价单id"` |
| | | Quotation Quotation `json:"quotation" gorm:"foreignKey:QuotationId;references:Id"` |
| | | StatusId int `json:"statusId" gorm:"column:status_id;type:int;comment:合同状态"` |
| | | File string `json:"file" gorm:"column:file;type:varchar(255);comment:合同文件"` |
| | | gorm.Model `json:"-"` |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | ContractName string `json:"contractName" gorm:"column:contract_name;type:varchar(255);comment:合同名称"` |
| | | ClientId int `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"` |
| | | Client Client `json:"client" gorm:"foreignKey:ClientId"` |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | Member User `json:"member" gorm:"foreignKey:MemberId"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:合同编号"` |
| | | QuotationId int `json:"quotationId" gorm:"column:quotation_id;type:int;comment:报价单id"` |
| | | Quotation Quotation `json:"quotation" gorm:"foreignKey:QuotationId;references:Id"` |
| | | StatusId int `json:"statusId" gorm:"column:status_id;type:int;comment:合同状态"` |
| | | ServiceContractStatus ServiceContractStatus `json:"serviceContractStatus" gorm:"foreignKey:StatusId;references:Id"` |
| | | File string `json:"file" gorm:"column:file;type:varchar(255);comment:合同文件"` |
| | | CreatedAt *CustomTime `json:"created_at" gorm:"column:created_at;type:datetime;comment:创建时间"` |
| | | CodeStandID string `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"` |
| | | SendTime string `json:"sendTime" gorm:"column:send_time;type:varchar(255);comment:发货时间"` |
| | | gormModel |
| | | } |
| | | |
| | | ContractSearch struct { |
| | | Contract |
| | | |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | |
| | | Orm *gorm.DB |
| | | SearchMap map[string]interface{} |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | } |
| | | ) |
| | | |
| | |
| | | |
| | | func (slf *ContractSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&Contract{}) |
| | | if slf.Keyword != "" { |
| | | db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") |
| | | } |
| | | |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | if slf.Number != "" { |
| | | db = db.Where("number = ?", slf.Number) |
| | | } |
| | | |
| | | if len(slf.SearchMap) > 0 { |
| | | for key, value := range slf.SearchMap { |
| | | switch v := value.(type) { |
| | | case string: |
| | | if key == "client_name" { |
| | | db = db.Joins("Client").Where("Client.name LIKE ?", "%"+v+"%") |
| | | } |
| | | |
| | | if key == "member_name" { |
| | | db = db.Joins("User").Where("User.username LIKE ?", "%"+v+"%") |
| | | } |
| | | |
| | | if key == "number" { |
| | | db = db.Where("number LIKE ?", "%"+v+"%") |
| | | } |
| | | |
| | | if key == "created_at" { |
| | | db = db.Where(key+"= ?", v) |
| | | } |
| | | case int: |
| | | if key == "member_id" { |
| | | db = db.Where(key+"= ?", v) |
| | | } |
| | | case []int: |
| | | if key == "member_ids" { |
| | | db = db.Where("contract.member_id in ?", v) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return db |
| | |
| | | db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) |
| | | } |
| | | |
| | | err := db.Preload("Quotation").Find(&records).Error |
| | | err := db.Preload("ServiceContractStatus").Preload("Client").Preload("Member").Preload("Quotation").Order("id desc").Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *ContractSearch) Count() (int64, error) { |
| | | var db = slf.build() |
| | | var total int64 |
| | | err := db.Count(&total).Error |
| | | return total, err |
| | | } |
| | | |
| | | func (slf *ContractSearch) 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 *ContractSearch) SetId(id int) *ContractSearch { |
| | | slf.Id = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetKeyword(keyword string) *ContractSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | | } |
| | | |
| | |
| | | func (slf *ContractSearch) SetOrder(order string) *ContractSearch { |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetSearchMap(data map[string]interface{}) *ContractSearch { |
| | | slf.SearchMap = data |
| | | return slf |
| | | } |
| | | func (slf *ContractSearch) SetIds(ids []int) *ContractSearch { |
| | | slf.Orm = slf.Orm.Where("id in (?)", ids) |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContractSearch) SetNumber(number string) *ContractSearch { |
| | | slf.Number = number |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContractSearch) UpdateByMap(data map[string]interface{}) error { |
| | | var db = slf.build() |
| | | return db.Updates(data).Error |
| | | } |