From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 01 十一月 2023 19:20:21 +0800 Subject: [PATCH] 公海member_id使用0 --- model/client.go | 392 ++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 243 insertions(+), 149 deletions(-) diff --git a/model/client.go b/model/client.go index a1c5cf2..dea1044 100644 --- a/model/client.go +++ b/model/client.go @@ -1,149 +1,243 @@ -package model - -import ( - "aps_crm/pkg/mysqlx" - "gorm.io/gorm" - "time" -) - -type ( - Client struct { - Id int `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"` - Name string `json:"name" gorm:"column:name;unique;type:varchar(255);comment:瀹㈡埛鍚嶇О"` - Number string `json:"number" gorm:"column:number;type:varchar(255);comment:瀹㈡埛缂栧彿"` - ClientStatusId int `json:"client_status_id" gorm:"column:client_status_id;type:int(11);comment:瀹㈡埛鐘舵�両D"` - ClientStatus ClientStatus `json:"client_status" gorm:"foreignKey:ClientStatusId"` - ClientTypeId int `json:"client_type_id" gorm:"column:client_type_id;type:int(11);comment:瀹㈡埛绫诲瀷ID"` - ClientType ClientType `json:"client_type" gorm:"foreignKey:ClientTypeId"` - ClientOriginId int `json:"client_origin_id" gorm:"column:client_origin_id;type:int(11);comment:瀹㈡埛鏉ユ簮ID"` - ClientOrigin ClientOrigin `json:"client_origin" gorm:"foreignKey:ClientOriginId"` - ClientLevelId int `json:"client_level_id" gorm:"column:client_level_id;type:int(11);comment:瀹㈡埛绛夌骇ID"` - ClientLevel ClientLevel `json:"client_level" gorm:"foreignKey:ClientLevelId"` - Contacts []Contact `json:"contacts" gorm:"foreignKey:ClientId"` - MemberId int `json:"member_id" gorm:"column:member_id;type:int(11);comment:閿�鍞礋璐d汉ID"` - ServiceMemberId int `json:"service_member_id" gorm:"column:service_member_id;type:int(11);comment:鏈嶅姟璐熻矗浜篒D"` - DetailAddress string `json:"detail_address" gorm:"column:detail_address;type:varchar(255);comment:璇︾粏鍦板潃"` - Remark string `json:"remark" gorm:"column:remark;type:varchar(255);comment:澶囨敞"` - NextVisitTime time.Time `json:"next_visit_time" gorm:"column:next_visit_time;type:datetime;comment:涓嬫鍥炶鏃堕棿"` - LatestServiceTime time.Time `json:"latest_service_time" gorm:"column:latest_service_time;type:datetime;comment:鏈�鏅氭湇鍔℃椂闂�"` - FollowRecord []FollowRecord `json:"follow_record" gorm:"foreignKey:ClientId"` - Address - Business - gorm.Model `json:"-"` - } - - ClientSearch struct { - Client - - Orm *gorm.DB - Keyword string - OrderBy string - PageNum int - PageSize int - } -) - -func (Client) TableName() string { - return "clients" -} - -func NewClientSearch(db *gorm.DB) *ClientSearch { - if db == nil { - db = mysqlx.GetDB() - } - - return &ClientSearch{ - Orm: db, - } -} - -func (slf *ClientSearch) build() *gorm.DB { - var db = slf.Orm.Model(&Client{}) - if slf.Keyword != "" { - db = db.Where("name LIKE ?", "%"+slf.Keyword+"%") - } - if slf.Id != 0 { - db.Where("id = ?", slf.Id) - } - if slf.Name != "" { - db.Where("name = ?", slf.Name) - } - - return db -} - -func (slf *ClientSearch) Create(record *Client) error { - var db = slf.build() - return db.Create(record).Error -} - -func (slf *ClientSearch) Update(record *Client) error { - var db = slf.build() - return db.Updates(record).Error -} - -func (slf *ClientSearch) Delete() error { - var db = slf.build() - return db.Delete(&Client{}).Error -} - -func (slf *ClientSearch) SetId(id int) *ClientSearch { - slf.Id = id - return slf -} - -func (slf *ClientSearch) SetName(name string) *ClientSearch { - slf.Name = name - return slf -} - -func (slf *ClientSearch) SetNumber(number string) *ClientSearch { - slf.Number = number - return slf -} - -func (slf *ClientSearch) First() (*Client, error) { - var db = slf.build() - var record = &Client{} - err := db.First(record).Error - return record, err -} - -func (slf *ClientSearch) Find() ([]*Client, int64, error) { - var db = slf.build() - var records = make([]*Client, 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("ClientStatus").Preload("ClientType").Preload("ClientOrigin").Preload("ClientLevel").Preload("FollowRecord").Preload("EnterpriseNature").Preload("RegisteredCapital").Preload("Industry").Preload("EnterpriseScale").Preload("Contacts").Preload("Country").Preload("Province").Preload("City").Preload("Region").Find(&records).Error - return records, total, err -} - -func (slf *ClientSearch) UpdateByMap(data map[string]interface{}) error { - var db = slf.build() - return db.Updates(data).Error -} - -func (slf *ClientSearch) SetKeyword(keyword string) *ClientSearch { - slf.Keyword = keyword - return slf -} - -func (slf *ClientSearch) SetPage(page, size int) *ClientSearch { - slf.PageNum, slf.PageSize = page, size - return slf -} - -func (slf *ClientSearch) SetOrder(order string) *ClientSearch { - slf.OrderBy = order - return slf -} -func (slf *ClientSearch) SetIds(ids []int) *ClientSearch { - slf.Orm = slf.Orm.Where("id in (?)", ids) - return slf -} +package model + +import ( + "aps_crm/constvar" + "aps_crm/pkg/mysqlx" + "fmt" + "gorm.io/gorm" + "time" +) + +type ( + Client struct { + Id int `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"` + Name string `json:"name" gorm:"column:name;uniqueIndex:name_isDeleted_idx;type:varchar(255);comment:瀹㈡埛鍚嶇О"` + IsDeleted bool `json:"-" gorm:"column:is_deleted;uniqueIndex:name_isDeleted_idx;type:tinyint(1);comment:鏄惁鍒犻櫎"` + Number string `json:"number" gorm:"column:number;type:varchar(255);comment:瀹㈡埛缂栧彿"` + ClientStatusId int `json:"client_status_id" gorm:"column:client_status_id;type:int(11);default:1;comment:瀹㈡埛鐘舵�両D"` + ClientStatus ClientStatus `json:"client_status" gorm:"foreignKey:ClientStatusId"` + ClientTypeId int `json:"client_type_id" gorm:"column:client_type_id;type:int(11);default:1;comment:瀹㈡埛绫诲瀷ID"` + ClientType ClientType `json:"client_type" gorm:"foreignKey:ClientTypeId"` + ClientOriginId int `json:"client_origin_id" gorm:"column:client_origin_id;type:int(11);default:1;comment:瀹㈡埛鏉ユ簮ID"` + ClientOrigin ClientOrigin `json:"client_origin" gorm:"foreignKey:ClientOriginId"` + ClientLevelId int `json:"client_level_id" gorm:"column:client_level_id;type:int(11);default:1;comment:瀹㈡埛绛夌骇ID"` + ClientLevel ClientLevel `json:"client_level" gorm:"foreignKey:ClientLevelId"` + Contacts []Contact `json:"contacts" gorm:"foreignKey:ClientId"` + MemberId int `json:"member_id" gorm:"column:member_id;type:int(11);comment:閿�鍞礋璐d汉ID"` + Member User `json:"member" gorm:"foreignKey:MemberId"` + ServiceMemberId int `json:"service_member_id" gorm:"column:service_member_id;type:int(11);comment:鏈嶅姟璐熻矗浜篒D"` + DetailAddress string `json:"detail_address" gorm:"column:detail_address;type:varchar(255);comment:璇︾粏鍦板潃"` + Remark string `json:"remark" gorm:"column:remark;type:varchar(255);comment:澶囨敞"` + CreatorId int `json:"creator_id" gorm:"column:creator_id;type:int;comment:鍒涘缓浜篿d"` + NextVisitTime *CustomTime `json:"next_visit_time" gorm:"column:next_visit_time;type:datetime;comment:涓嬫鍥炶鏃堕棿"` + LatestServiceTime *CustomTime `json:"latest_service_time" gorm:"column:latest_service_time;type:datetime;comment:鏈�鏅氭湇鍔℃椂闂�"` + FollowRecord []FollowRecord `json:"follow_record" gorm:"foreignKey:ClientId"` + CodeStandID string `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:缂栫爜id"` + Address + Business + CrmModel + } + + ClientSearch struct { + Client + + Orm *gorm.DB + SearchMap map[string]interface{} + OrderBy string + PageNum int + PageSize int + } +) + +func (Client) TableName() string { + return "clients" +} + +func NewClientSearch(db *gorm.DB) *ClientSearch { + if db == nil { + db = mysqlx.GetDB() + } + + return &ClientSearch{ + Orm: db, + } +} + +func (slf *ClientSearch) build() *gorm.DB { + var db = slf.Orm.Model(&Client{}) + if slf.Id != 0 { + db.Where("id = ?", slf.Id) + } + if slf.Name != "" { + db.Where("name = ?", slf.Name) + } + if slf.Number != "" { + db.Where("number = ?", slf.Number) + } + + if len(slf.SearchMap) > 0 { + for key, value := range slf.SearchMap { + switch v := value.(type) { + case string: + if key == "name" || key == "number" || key == "detail_address" || key == "remark" || key == "business_scope" || key == "registration_time" || key == "representative" { + db = db.Where(key+" LIKE ?", "%"+v+"%") + } + + if key == "phone" { + db = db.Joins("Contacts").Where("Contacts.phone LIKE ?", "%"+v+"%") + } + + if key == "contact_name" { + db = db.Joins("Contacts").Where("Contacts.name LIKE ?", "%"+v+"%") + } + + if key == "next_visit_time" { + db = db.Where(key+" = ?", v) + } + + // 鏌ヨ瓒呭嚭15澶╂湭鑱旂郴鐨勫鎴� + if key == "not_contact" { + db = db.Joins("FollowRecord").Where("FollowRecord.follow_time < ?", time.Now().AddDate(0, 0, -15)).Group("clients.id") + } + + // 鏌ヨ鍏捣瀹㈡埛 + if key == "public_sea" { + db = db.Where("clients.member_id = ?", constvar.UserTypeSuper) + } + + if key == "client_status" { + db = db.Joins("ClientStatus").Where("ClientStatus.name LIKE ?", "%"+v+"%") + } + + if key == "client_level" { + db = db.Joins("ClientLevel").Where("ClientLevel.name LIKE ?", "%"+v+"%") + } + + if key == "member_name" { + db = db.Joins("Member").Where("Member.username LIKE ?", "%"+v+"%") + } + + case int, uint, int64, float64, bool: + if key == "id" || key == "client_type_id" || key == "client_status_id" || key == "member_id" { + db = db.Where(key+" = ?", v) + } + + if key == "is_public" { + if val, ok := v.(bool); ok { + if val { + db = db.Where("clients.member_id = ?", 0) + } + } + } + case []int: + if key == "member_ids" { + db = db.Where("clients.member_id in ?", v) + } + + } + } + } + + return db +} + +func (slf *ClientSearch) Create(record *Client) error { + var db = slf.build() + return db.Create(record).Error +} + +func (slf *ClientSearch) Update(record *Client) error { + var db = slf.build() + return db.Updates(record).Error +} + +func (slf *ClientSearch) Delete() error { + var db = slf.build() + db.Update("is_deleted", 1) + return db.Delete(&Client{}).Error +} + +func (slf *ClientSearch) SetId(id int) *ClientSearch { + slf.Id = id + return slf +} + +func (slf *ClientSearch) SetName(name string) *ClientSearch { + slf.Name = name + return slf +} + +func (slf *ClientSearch) SetSearchMap(data map[string]interface{}) *ClientSearch { + slf.SearchMap = data + return slf +} + +func (slf *ClientSearch) SetNumber(number string) *ClientSearch { + slf.Number = number + return slf +} + +func (slf *ClientSearch) First() (*Client, error) { + var db = slf.build() + var record = &Client{} + err := db.First(record).Error + return record, err +} + +func (slf *ClientSearch) Find() ([]*Client, int64, error) { + var db = slf.build() + var records = make([]*Client, 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("Member").Preload("ClientStatus").Preload("ClientType").Preload("ClientOrigin").Preload("ClientLevel").Preload("FollowRecord").Preload("EnterpriseNature").Preload("RegisteredCapital").Preload("Industry").Preload("EnterpriseScale").Preload("Contacts").Preload("Country").Preload("Province").Preload("City").Preload("Region").Order("id desc").Find(&records).Error + return records, total, err +} + +func (slf *ClientSearch) UpdateByMap(data map[string]interface{}) error { + var db = slf.build() + return db.Updates(data).Error +} + +func (slf *ClientSearch) Count() (int64, error) { + var db = slf.build() + var total int64 + err := db.Count(&total).Error + return total, err +} + +func (slf *ClientSearch) 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 *ClientSearch) SetPage(page, size int) *ClientSearch { + slf.PageNum, slf.PageSize = page, size + return slf +} + +func (slf *ClientSearch) SetOrder(order string) *ClientSearch { + slf.OrderBy = order + return slf +} +func (slf *ClientSearch) SetIds(ids []int) *ClientSearch { + slf.Orm = slf.Orm.Where("id in (?)", ids) + return slf +} -- Gitblit v1.8.0