| | |
| | | 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:客户状态ID"`
|
| | | 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:销售负责人ID"`
|
| | | ServiceMemberId int `json:"service_member_id" gorm:"column:service_member_id;type:int(11);comment:服务负责人ID"`
|
| | | 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/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:客户状态ID"` |
| | | 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:销售负责人ID"` |
| | | ServiceMemberId int `json:"service_member_id" gorm:"column:service_member_id;type:int(11);comment:服务负责人ID"` |
| | | 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 |
| | | 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 len(slf.SearchMap) > 0 { |
| | | for key, value := range slf.SearchMap { |
| | | switch v := value.(type) { |
| | | case string: |
| | | if key == "name" || key == "number" || key == "phone" || key == "detail_address" { |
| | | db = db.Where(key+" LIKE ?", "%"+v+"%") |
| | | } |
| | | case int: |
| | | if key == "id" || key == "client_type_id" || key == "client_status_id" { |
| | | db = db.Where(key+" = ?", 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() |
| | | 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("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) 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 |
| | | } |