| | |
| | | |
| | | import ( |
| | | "aps_crm/pkg/mysqlx" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | ) |
| | | |
| | | type ( |
| | | FollowRecord struct { |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | ClientId int `json:"client_id" gorm:"column:client_id;type:int(11);comment:客户id"` |
| | | ClientStatusId int `json:"client_status_id" gorm:"column:client_status_id;type:int(11);comment:客户状态id"` |
| | | MemberId int `json:"member_id" gorm:"column:member_id;type:int(11);comment:跟进人id"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:跟进编号"` |
| | | ContactId int `json:"contact_id" gorm:"column:contact_id;type:int(11);comment:联系人id"` |
| | | Topic string `json:"topic" gorm:"column:topic;type:varchar(255);comment:跟进主题"` |
| | | Record string `json:"record" gorm:"column:record;type:MEDIUMTEXT;comment:跟进记录"` |
| | | SaleChanceId int `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int(11);comment:销售机会id"` |
| | | SalesLeadsId int `json:"sales_leads_id" gorm:"column:sales_leads_id;type:int(11);comment:销售线索id"` |
| | | ContactInformationId int `json:"contact_information_id" gorm:"column:contact_information_id;type:int(11);comment:联系方式id"` |
| | | FollowTime time.Time `json:"follow_time" gorm:"column:follow_time;type:datetime;comment:跟进时间"` |
| | | NextFollowTime time.Time `json:"next_follow_time" gorm:"column:next_follow_time;type:datetime;comment:下次跟进时间"` |
| | | Purpose string `json:"purpose" gorm:"column:purpose;type:varchar(255);comment:跟进目的"` |
| | | Content string `json:"content" gorm:"column:content;type:MEDIUMTEXT;comment:跟进内容"` |
| | | Client Client `json:"client" gorm:"foreignKey:ClientId"` |
| | | Contact Contact `json:"contact" gorm:"foreignKey:ContactId"` |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | ClientId int `json:"client_id" gorm:"column:client_id;type:int(11);comment:客户id"` |
| | | ClientStatusId int `json:"client_status_id" gorm:"column:client_status_id;type:int(11);comment:客户状态id"` |
| | | ClientStatus ClientStatus `json:"client_status" gorm:"foreignKey:ClientStatusId"` |
| | | MemberId int `json:"member_id" gorm:"column:member_id;type:int(11);comment:跟进人id"` |
| | | Member User `json:"member" gorm:"foreignKey:MemberId"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:跟进编号"` |
| | | ContactId int `json:"contact_id" gorm:"column:contact_id;type:int(11);comment:联系人id"` |
| | | Topic string `json:"topic" gorm:"column:topic;type:varchar(255);comment:跟进主题"` |
| | | Record string `json:"record" gorm:"column:record;type:MEDIUMTEXT;comment:跟进记录"` |
| | | SaleChanceId int `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int(11);comment:销售机会id"` |
| | | SaleChance SaleChance `json:"sale_chance" gorm:"foreignKey:SaleChanceId"` |
| | | SalesLeadsId int `json:"sales_leads_id" gorm:"column:sales_leads_id;type:int(11);comment:销售线索id"` |
| | | SalesLeads SalesLeads `json:"sales_leads" gorm:"foreignKey:SalesLeadsId"` |
| | | ContactInformationId int `json:"contact_information_id" gorm:"column:contact_information_id;type:int(11);comment:联系方式id"` |
| | | FollowTime *CustomTime `json:"follow_time" gorm:"column:follow_time;type:datetime;comment:跟进时间"` |
| | | NextFollowTime *CustomTime `json:"next_follow_time" gorm:"column:next_follow_time;type:datetime;comment:下次跟进时间"` |
| | | Purpose string `json:"purpose" gorm:"column:purpose;type:varchar(255);comment:跟进目的"` |
| | | Content string `json:"content" gorm:"column:content;type:MEDIUMTEXT;comment:跟进内容"` |
| | | Client Client `json:"client" gorm:"foreignKey:ClientId"` |
| | | Contact Contact `json:"contact" gorm:"foreignKey:ContactId"` |
| | | ContactInformation ContactInformation `json:"contact_information" gorm:"foreignKey:ContactInformationId"` |
| | | CodeStandID string `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"` |
| | | gorm.Model `json:"-"` |
| | | } |
| | | |
| | | FollowRecordSearch struct { |
| | | FollowRecord |
| | | |
| | | 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 *FollowRecordSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&FollowRecord{}) |
| | | if slf.Keyword != "" { |
| | | db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") |
| | | } |
| | | if slf.Keyword != "" { |
| | | db = db.Where("topic LIKE ?", "%"+slf.Keyword+"%") |
| | | } |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | if slf.ClientId != 0 { |
| | | db = db.Where("client_id = ?", slf.ClientId) |
| | | } |
| | | if slf.ClientStatusId != 0 { |
| | | db = db.Where("client_status_id = ?", slf.ClientStatusId) |
| | | } |
| | | if slf.MemberId != 0 { |
| | | db = db.Where("member_id = ?", slf.MemberId) |
| | | } |
| | | if slf.Number != "" { |
| | | db = db.Where("number = ?", slf.Number) |
| | | } |
| | | if slf.ContactId != 0 { |
| | | db = db.Where("contact_id = ?", slf.ContactId) |
| | | } |
| | | if slf.Topic != "" { |
| | | db = db.Where("topic = ?", slf.Topic) |
| | | } |
| | | if slf.Record != "" { |
| | | db = db.Where("record = ?", slf.Record) |
| | | } |
| | | if slf.SaleChanceId != 0 { |
| | | db = db.Where("sale_chance_id = ?", slf.SaleChanceId) |
| | | } |
| | | if slf.SalesLeadsId != 0 { |
| | | db = db.Where("sales_leads_id = ?", slf.SalesLeadsId) |
| | | } |
| | | if slf.ContactInformationId != 0 { |
| | | db = db.Where("contact_information_id = ?", slf.ContactInformationId) |
| | | } |
| | | if !slf.FollowTime.IsZero() { |
| | | db = db.Where("follow_time = ?", slf.FollowTime) |
| | | } |
| | | if !slf.NextFollowTime.IsZero() { |
| | | db = db.Where("next_follow_time = ?", slf.NextFollowTime) |
| | | } |
| | | if slf.Purpose != "" { |
| | | db = db.Where("purpose = ?", slf.Purpose) |
| | | } |
| | | if slf.Content != "" { |
| | | db = db.Where("content = ?", slf.Content) |
| | | |
| | | if len(slf.SearchMap) > 0 { |
| | | for key, value := range slf.SearchMap { |
| | | switch v := value.(type) { |
| | | case string: |
| | | if key == "topic" || key == "record" { |
| | | 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+"%") |
| | | } |
| | | |
| | | if key == "phone" { |
| | | db = db.Joins("Contact").Where("Contacts.phone LIKE ?", "%"+v+"%") |
| | | } |
| | | |
| | | if key == "follow_time" { |
| | | db = db.Where("follow_time = ?", v) |
| | | } |
| | | |
| | | if key == "next_follow_time" { |
| | | db = db.Where("next_follow_time = ?", v) |
| | | } |
| | | |
| | | if key == "client_status" { |
| | | db = db.Joins("Client").Joins("Client.ClientStatus").Where("Client__ClientStatus.name LIKE ?", "%"+v+"%") |
| | | } |
| | | case int, int64, float64: |
| | | if key == "client_id" || key == "contact_id" || key == "sales_leads_id" || key == "sale_chance_id" || key == "member_id" { |
| | | db = db.Where(key+" = ?", v) |
| | | } |
| | | case []int: |
| | | if key == "member_ids" { |
| | | db = db.Where("follow_records.member_id in ?", v) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return db |
| | |
| | | return record, err |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) Find() ([]*FollowRecord, error) { |
| | | func (slf *FollowRecordSearch) FindAll() ([]*FollowRecord, int64, error) { |
| | | var db = slf.build() |
| | | var records = make([]*FollowRecord, 0) |
| | | var total int64 |
| | | if err := db.Count(&total).Error; err != nil { |
| | | return records, total, err |
| | | } |
| | | if slf.PageNum > 0 && slf.PageSize > 0 { |
| | | db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) |
| | | } |
| | | |
| | | err := db.Preload("Client").Preload("Contact").Find(&records).Error |
| | | return records, err |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) Count() (int64, error) { |
| | | var count int64 |
| | | err := slf.build().Count(&count).Error |
| | | return count, err |
| | | err := db.Preload("SalesLeads").Preload("SaleChance").Preload("ClientStatus").Preload("Member").Preload("Client").Preload("Contact").Preload("ContactInformation").Order("id desc").Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) Page(page, pageSize int) ([]*FollowRecord, int64, error) { |
| | |
| | | if err != nil { |
| | | return records, count, err |
| | | } |
| | | err = slf.build().Offset((page - 1) * pageSize).Limit(pageSize).Find(&records).Error |
| | | err = slf.build().Offset((page - 1) * pageSize).Limit(pageSize).Order("id desc").Find(&records).Error |
| | | return records, count, err |
| | | } |
| | | |
| | |
| | | return db.Delete(&slf.FollowRecord).Error |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) Count() (int64, error) { |
| | | var db = slf.build() |
| | | var total int64 |
| | | err := db.Count(&total).Error |
| | | return total, err |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) 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 *FollowRecordSearch) SetId(id int) *FollowRecordSearch { |
| | | slf.Id = id |
| | | return slf |
| | |
| | | return slf |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) SetKeyword(keyword string) *FollowRecordSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) SetPage(page, size int) *FollowRecordSearch { |
| | | slf.PageNum, slf.PageSize = page, size |
| | | return slf |
| | |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |
| | | func (slf *FollowRecordSearch) SetIds(ids []int) *FollowRecordSearch { |
| | | slf.Orm = slf.Orm.Where("id in (?)", ids) |
| | | return slf |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) UpdateMap(data map[string]interface{}) error { |
| | | var db = slf.build() |
| | | return db.Updates(data).Error |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) SetSearchMap(data map[string]interface{}) *FollowRecordSearch { |
| | | slf.SearchMap = data |
| | | return slf |
| | | } |
| | | func (slf *FollowRecordSearch) SetOrm(tx *gorm.DB) *FollowRecordSearch { |
| | | slf.Orm = tx |
| | | return slf |
| | | } |
| | | |
| | | func (slf *FollowRecordSearch) SetNumber(number string) *FollowRecordSearch { |
| | | slf.Number = number |
| | | return slf |
| | | } |