fix
add multi-criteria query to salesLeads
| | |
| | | package v1
|
| | |
|
| | | import (
|
| | | "aps_crm/model"
|
| | | "aps_crm/model/request"
|
| | | "aps_crm/model/response"
|
| | | "aps_crm/pkg/contextx"
|
| | | "aps_crm/pkg/ecode"
|
| | | "github.com/gin-gonic/gin"
|
| | | )
|
| | |
|
| | | type ContactApi struct{}
|
| | |
|
| | | // Add
|
| | | //
|
| | | // @Tags Contact
|
| | | // @Summary 添加联系人
|
| | | // @Produce application/json
|
| | | // @Param object body request.AddContact true "查询参数"
|
| | | // @Success 200 {object} contextx.Response{}
|
| | | // @Router /api/contact/add [post]
|
| | | func (con *ContactApi) Add(c *gin.Context) {
|
| | | var params request.AddContact
|
| | | ctx, ok := contextx.NewContext(c, ¶ms)
|
| | | if !ok {
|
| | | return
|
| | | }
|
| | |
|
| | | errCode, contact := checkContactParams(params.Contact)
|
| | | if errCode != ecode.OK {
|
| | | ctx.Fail(errCode)
|
| | | return
|
| | | }
|
| | |
|
| | | errCode = contactService.AddContact(&contact)
|
| | | if errCode != ecode.OK {
|
| | | ctx.Fail(errCode)
|
| | | return
|
| | | }
|
| | |
|
| | | ctx.Ok()
|
| | | }
|
| | |
|
| | | // Delete
|
| | | //
|
| | | // @Tags Contact
|
| | | // @Summary 删除联系人
|
| | | // @Produce application/json
|
| | | // @Param object body request.DeleteContact true "查询参数"
|
| | | // @Success 200 {object} contextx.Response{}
|
| | | // @Router /api/contact/delete [delete]
|
| | | func (con *ContactApi) Delete(c *gin.Context) {
|
| | | var params request.DeleteContact
|
| | | ctx, ok := contextx.NewContext(c, ¶ms)
|
| | | if !ok {
|
| | | return
|
| | | }
|
| | |
|
| | | errCode := contactService.DeleteContact(params.Ids)
|
| | | if errCode != ecode.OK {
|
| | | ctx.Fail(errCode)
|
| | | return
|
| | | }
|
| | |
|
| | | ctx.Ok()
|
| | | }
|
| | |
|
| | | // Update
|
| | | //
|
| | | // @Tags Contact
|
| | | // @Summary 更新联系人
|
| | | // @Produce application/json
|
| | | // @Param object body request.UpdateContact true "查询参数"
|
| | | // @Success 200 {object} contextx.Response{}
|
| | | // @Router /api/contact/update [put]
|
| | | func (con *ContactApi) Update(c *gin.Context) {
|
| | | var params request.UpdateContact
|
| | | ctx, ok := contextx.NewContext(c, ¶ms)
|
| | | if !ok {
|
| | | return
|
| | | }
|
| | |
|
| | | // check id
|
| | | if params.Id == 0 {
|
| | | ctx.Fail(ecode.InvalidParams)
|
| | | return
|
| | | }
|
| | |
|
| | | errCode, contact := checkContactParams(params.Contact)
|
| | | if errCode != ecode.OK {
|
| | | ctx.Fail(errCode)
|
| | | return
|
| | | }
|
| | |
|
| | | contact.Id = params.Id
|
| | |
|
| | | errCode = contactService.UpdateContact(&contact)
|
| | | if errCode != ecode.OK {
|
| | | ctx.Fail(errCode)
|
| | | return
|
| | | }
|
| | |
|
| | | ctx.Ok()
|
| | | }
|
| | |
|
| | | func checkContactParams(params request.Contact) (int, model.Contact) {
|
| | | var contact model.Contact
|
| | | // name not empty
|
| | | //if params.Name == "" {
|
| | | // return ecode.InvalidParams, contact
|
| | | //}
|
| | | contact.Name = params.Name
|
| | |
|
| | | contact.Phone = params.Phone
|
| | | contact.Email = params.Email
|
| | |
|
| | | // check member id not -1
|
| | | //if params.MemberId == 0 {
|
| | | // return ecode.InvalidParams, contact
|
| | | //}
|
| | | contact.MemberId = params.MemberId
|
| | |
|
| | | contact.ProvinceId = params.ProvinceId
|
| | | contact.CityId = params.CityId
|
| | | contact.CountryId = params.CountryId
|
| | | contact.RegionId = params.RegionId
|
| | | contact.Position = params.Position
|
| | | contact.ClientId = params.ClientId
|
| | | contact.Desc = params.Desc
|
| | |
|
| | | // check number not empty
|
| | | //if params.Number == "" {
|
| | | // return ecode.InvalidParams, contact
|
| | | //}
|
| | | contact.Number = params.Number
|
| | |
|
| | | t, err := checkTimeFormat(params.Birthday)
|
| | | if err != nil {
|
| | | return ecode.InvalidParams, contact
|
| | | }
|
| | | contact.Birthday = t
|
| | | contact.Wechat = params.Wechat
|
| | | contact.IsFirst = params.IsFirst
|
| | | return ecode.OK, contact
|
| | | }
|
| | |
|
| | | // List
|
| | | //
|
| | | // @Tags Contact
|
| | | // @Summary 联系人列表
|
| | | // @Produce application/json
|
| | | // @Param object body request.GetContactList true "参数"
|
| | | // @Success 200 {object} contextx.Response{data=response.ContactResponse}
|
| | | // @Router /api/contact/list [post]
|
| | | func (con *ContactApi) List(c *gin.Context) {
|
| | | var params request.GetContactList
|
| | | ctx, ok := contextx.NewContext(c, ¶ms)
|
| | | if !ok {
|
| | | return
|
| | | }
|
| | |
|
| | | contacts, total, errCode := contactService.GetContactList(params.Page, params.PageSize, params.Keyword)
|
| | | if errCode != ecode.OK {
|
| | | ctx.Fail(errCode)
|
| | | return
|
| | | }
|
| | |
|
| | | ctx.OkWithDetailed(response.ContactResponse{
|
| | | List: contacts,
|
| | | Count: int(total),
|
| | | })
|
| | | }
|
| | | package v1 |
| | | |
| | | import ( |
| | | "aps_crm/model" |
| | | "aps_crm/model/request" |
| | | "aps_crm/model/response" |
| | | "aps_crm/pkg/contextx" |
| | | "aps_crm/pkg/ecode" |
| | | "github.com/gin-gonic/gin" |
| | | ) |
| | | |
| | | type ContactApi struct{} |
| | | |
| | | // Add |
| | | // |
| | | // @Tags Contact |
| | | // @Summary 添加联系人 |
| | | // @Produce application/json |
| | | // @Param object body request.AddContact true "查询参数" |
| | | // @Success 200 {object} contextx.Response{} |
| | | // @Router /api/contact/add [post] |
| | | func (con *ContactApi) Add(c *gin.Context) { |
| | | var params request.AddContact |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | errCode, contact := checkContactParams(params.Contact) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | |
| | | errCode = contactService.AddContact(&contact) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | |
| | | ctx.Ok() |
| | | } |
| | | |
| | | // Delete |
| | | // |
| | | // @Tags Contact |
| | | // @Summary 删除联系人 |
| | | // @Produce application/json |
| | | // @Param object body request.DeleteContact true "查询参数" |
| | | // @Success 200 {object} contextx.Response{} |
| | | // @Router /api/contact/delete [delete] |
| | | func (con *ContactApi) Delete(c *gin.Context) { |
| | | var params request.DeleteContact |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | errCode := contactService.DeleteContact(params.Ids) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | |
| | | ctx.Ok() |
| | | } |
| | | |
| | | // Update |
| | | // |
| | | // @Tags Contact |
| | | // @Summary 更新联系人 |
| | | // @Produce application/json |
| | | // @Param object body request.UpdateContact true "查询参数" |
| | | // @Success 200 {object} contextx.Response{} |
| | | // @Router /api/contact/update [put] |
| | | func (con *ContactApi) Update(c *gin.Context) { |
| | | var params request.UpdateContact |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | // check id |
| | | if params.Id == 0 { |
| | | ctx.Fail(ecode.InvalidParams) |
| | | return |
| | | } |
| | | |
| | | errCode, contact := checkContactParams(params.Contact) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | |
| | | contact.Id = params.Id |
| | | |
| | | errCode = contactService.UpdateContact(&contact) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | |
| | | ctx.Ok() |
| | | } |
| | | |
| | | func checkContactParams(params request.Contact) (int, model.Contact) { |
| | | var contact model.Contact |
| | | // name not empty |
| | | //if params.Name == "" { |
| | | // return ecode.InvalidParams, contact |
| | | //} |
| | | contact.Name = params.Name |
| | | |
| | | contact.Phone = params.Phone |
| | | contact.Email = params.Email |
| | | |
| | | // check member id not -1 |
| | | //if params.MemberId == 0 { |
| | | // return ecode.InvalidParams, contact |
| | | //} |
| | | contact.MemberId = params.MemberId |
| | | |
| | | contact.ProvinceId = params.ProvinceId |
| | | contact.CityId = params.CityId |
| | | contact.CountryId = params.CountryId |
| | | contact.RegionId = params.RegionId |
| | | contact.Position = params.Position |
| | | contact.ClientId = params.ClientId |
| | | contact.Desc = params.Desc |
| | | |
| | | // check number not empty |
| | | //if params.Number == "" { |
| | | // return ecode.InvalidParams, contact |
| | | //} |
| | | contact.Number = params.Number |
| | | |
| | | t, err := checkTimeFormat(params.Birthday) |
| | | if err != nil { |
| | | return ecode.InvalidParams, contact |
| | | } |
| | | contact.Birthday = t |
| | | contact.Wechat = params.Wechat |
| | | contact.IsFirst = params.IsFirst |
| | | return ecode.OK, contact |
| | | } |
| | | |
| | | // List |
| | | // |
| | | // @Tags Contact |
| | | // @Summary 联系人列表 |
| | | // @Produce application/json |
| | | // @Param object body request.GetContactList true "参数" |
| | | // @Success 200 {object} contextx.Response{data=response.ContactResponse} |
| | | // @Router /api/contact/list [post] |
| | | func (con *ContactApi) List(c *gin.Context) { |
| | | var params request.GetContactList |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | contacts, total, errCode := contactService.GetContactList(params.Page, params.PageSize, params.SearchMap) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | |
| | | ctx.OkWithDetailed(response.ContactResponse{ |
| | | List: contacts, |
| | | Count: int(total), |
| | | }) |
| | | } |
| | |
| | | "request.GetContactList": { |
| | | "type": "object", |
| | | "properties": { |
| | | "keyword": { |
| | | "type": "string" |
| | | }, |
| | | "page": { |
| | | "description": "页码", |
| | | "type": "integer" |
| | |
| | | "pageSize": { |
| | | "description": "每页大小", |
| | | "type": "integer" |
| | | }, |
| | | "search_map": { |
| | | "description": "搜索条件: map[string]interface{}{\"name\": \"xxx\"}; {\"name\": \"客户名称\", \"phone\": \"手机号码\", \"detail_address\":\"详细地址\", \"next_visit_time\":\"下回回访日期\", \"member_name\": \"销售负责人\", \"client_status\": \"客户状态\", \"client_level\": \"重要级别\"}", |
| | | "type": "object", |
| | | "additionalProperties": true |
| | | } |
| | | } |
| | | }, |
| | |
| | | "request.GetContactList": { |
| | | "type": "object", |
| | | "properties": { |
| | | "keyword": { |
| | | "type": "string" |
| | | }, |
| | | "page": { |
| | | "description": "页码", |
| | | "type": "integer" |
| | |
| | | "pageSize": { |
| | | "description": "每页大小", |
| | | "type": "integer" |
| | | }, |
| | | "search_map": { |
| | | "description": "搜索条件: map[string]interface{}{\"name\": \"xxx\"}; {\"name\": \"客户名称\", \"phone\": \"手机号码\", \"detail_address\":\"详细地址\", \"next_visit_time\":\"下回回访日期\", \"member_name\": \"销售负责人\", \"client_status\": \"客户状态\", \"client_level\": \"重要级别\"}", |
| | | "type": "object", |
| | | "additionalProperties": true |
| | | } |
| | | } |
| | | }, |
| | |
| | | type: object |
| | | request.GetContactList: |
| | | properties: |
| | | keyword: |
| | | type: string |
| | | page: |
| | | description: 页码 |
| | | type: integer |
| | | pageSize: |
| | | description: 每页大小 |
| | | type: integer |
| | | search_map: |
| | | additionalProperties: true |
| | | description: '搜索条件: map[string]interface{}{"name": "xxx"}; {"name": "客户名称", |
| | | "phone": "手机号码", "detail_address":"详细地址", "next_visit_time":"下回回访日期", "member_name": |
| | | "销售负责人", "client_status": "客户状态", "client_level": "重要级别"}' |
| | | type: object |
| | | type: object |
| | | request.GetContractList: |
| | | properties: |
| | |
| | | package model
|
| | |
|
| | | import (
|
| | | "aps_crm/pkg/mysqlx"
|
| | | "gorm.io/gorm"
|
| | | "time"
|
| | | )
|
| | |
|
| | | type (
|
| | | Contact struct {
|
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
|
| | | Name string `json:"name" gorm:"column:name;type:varchar(255);comment:联系人姓名"`
|
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:联系人编号"`
|
| | | ClientId int `json:"client_id" gorm:"column:client_id;type:int(11);comment:客户ID"`
|
| | | Position string `json:"position" gorm:"column:position;type:varchar(255);comment:职位"`
|
| | | Phone string `json:"phone" gorm:"column:phone;type:varchar(255);comment:电话"`
|
| | | MemberId int `json:"member_id" gorm:"column:member_id;type:int(11);comment:负责人ID"`
|
| | | IsFirst bool `json:"is_first" gorm:"column:is_first;type:tinyint(1);comment:是否首要联系人"`
|
| | | Wechat string `json:"wechat" gorm:"column:wechat;type:varchar(255);comment:微信"`
|
| | | Birthday time.Time `json:"birthday" gorm:"column:birthday;type:datetime;comment:生日"`
|
| | | Email string `json:"email" gorm:"column:email;type:varchar(255);comment:邮箱"`
|
| | | Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:备注"`
|
| | | Address
|
| | | gorm.Model `json:"-"`
|
| | | }
|
| | |
|
| | | ContactSearch struct {
|
| | | Contact
|
| | |
|
| | | Orm *gorm.DB
|
| | | Keyword string
|
| | | OrderBy string
|
| | | PageNum int
|
| | | PageSize int
|
| | | }
|
| | |
|
| | | ContactDetail struct {
|
| | | Contact
|
| | | Client Client
|
| | | FollowRecord []FollowRecord `gorm:"foreignKey:ContactId"`
|
| | | }
|
| | | )
|
| | |
|
| | | func (Contact) TableName() string {
|
| | | return "contacts"
|
| | | }
|
| | |
|
| | | func NewContactSearch(db *gorm.DB) *ContactSearch {
|
| | | if db == nil {
|
| | | db = mysqlx.GetDB()
|
| | | }
|
| | | return &ContactSearch{
|
| | | Orm: db,
|
| | | }
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) build() *gorm.DB {
|
| | | var db = slf.Orm.Model(&Contact{})
|
| | | if slf.Keyword != "" {
|
| | | db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
|
| | | }
|
| | | if slf.Id != 0 {
|
| | | db = db.Where("id = ?", slf.Id)
|
| | | }
|
| | | if slf.Name != "" {
|
| | | db = db.Where("name = ?", slf.Name)
|
| | | }
|
| | |
|
| | | if slf.ClientId != 0 {
|
| | | db = db.Where("client_id = ?", slf.ClientId)
|
| | | }
|
| | |
|
| | | return db
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) Create(record *Contact) error {
|
| | | var db = slf.build()
|
| | | return db.Create(record).Error
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) Update(record *Contact) error {
|
| | | var db = slf.build()
|
| | | m := map[string]interface{}{
|
| | | "name": record.Name,
|
| | | "number": record.Number,
|
| | | "client_id": record.ClientId,
|
| | | "position": record.Position,
|
| | | "phone": record.Phone,
|
| | | "member_id": record.MemberId,
|
| | | "is_first": record.IsFirst,
|
| | | "wechat": record.Wechat,
|
| | | "birthday": record.Birthday,
|
| | | "email": record.Email,
|
| | | "desc": record.Desc,
|
| | | "country_id": record.CountryId,
|
| | | "province_id": record.ProvinceId,
|
| | | "city_id": record.CityId,
|
| | | "region_id": record.RegionId,
|
| | | }
|
| | | return db.Updates(m).Error
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) Delete() error {
|
| | | var db = slf.build()
|
| | | return db.Delete(&Contact{}).Error
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) Find() (*Contact, error) {
|
| | | var db = slf.build()
|
| | | var record = new(Contact)
|
| | | err := db.First(record).Error
|
| | | return record, err
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) FindAll() ([]*ContactDetail, int64, error) {
|
| | | var db = slf.build()
|
| | | var records = make([]*ContactDetail, 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("FollowRecord").Preload("Client").Preload("Country").Preload("Province").Preload("City").Preload("Region").Find(&records).Error
|
| | | return records, total, err
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) SetId(id int) *ContactSearch {
|
| | | slf.Id = id
|
| | | return slf
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) SetName(name string) *ContactSearch {
|
| | | slf.Name = name
|
| | | return slf
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) First() (*Contact, error) {
|
| | | var db = slf.build()
|
| | | var record = new(Contact)
|
| | | err := db.First(record).Error
|
| | | return record, err
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) SetClientId(clientId int) *ContactSearch {
|
| | | slf.ClientId = clientId
|
| | | return slf
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) UpdateByMap(data map[string]interface{}) error {
|
| | | var db = slf.build()
|
| | | return db.Updates(data).Error
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) SetKeyword(keyword string) *ContactSearch {
|
| | | slf.Keyword = keyword
|
| | | return slf
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) SetPage(page, size int) *ContactSearch {
|
| | | slf.PageNum, slf.PageSize = page, size
|
| | | return slf
|
| | | }
|
| | |
|
| | | func (slf *ContactSearch) SetOrder(order string) *ContactSearch {
|
| | | slf.OrderBy = order
|
| | | return slf
|
| | | }
|
| | | func (slf *ContactSearch) SetIds(ids []int) *ContactSearch {
|
| | | slf.Orm = slf.Orm.Where("id in (?)", ids)
|
| | | return slf
|
| | | }
|
| | | package model |
| | | |
| | | import ( |
| | | "aps_crm/pkg/mysqlx" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | ) |
| | | |
| | | type ( |
| | | Contact struct { |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | Name string `json:"name" gorm:"column:name;type:varchar(255);comment:联系人姓名"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:联系人编号"` |
| | | ClientId int `json:"client_id" gorm:"column:client_id;type:int(11);comment:客户ID"` |
| | | Position string `json:"position" gorm:"column:position;type:varchar(255);comment:职位"` |
| | | Phone string `json:"phone" gorm:"column:phone;type:varchar(255);comment:电话"` |
| | | MemberId int `json:"member_id" gorm:"column:member_id;type:int(11);comment:负责人ID"` |
| | | IsFirst bool `json:"is_first" gorm:"column:is_first;type:tinyint(1);comment:是否首要联系人"` |
| | | Wechat string `json:"wechat" gorm:"column:wechat;type:varchar(255);comment:微信"` |
| | | Birthday time.Time `json:"birthday" gorm:"column:birthday;type:datetime;comment:生日"` |
| | | Email string `json:"email" gorm:"column:email;type:varchar(255);comment:邮箱"` |
| | | Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:备注"` |
| | | Address |
| | | gorm.Model `json:"-"` |
| | | } |
| | | |
| | | ContactSearch struct { |
| | | Contact |
| | | |
| | | Orm *gorm.DB |
| | | SearchMap map[string]interface{} |
| | | OrderBy string |
| | | PageNum int |
| | | PageSize int |
| | | } |
| | | |
| | | ContactDetail struct { |
| | | Contact |
| | | Client Client |
| | | FollowRecord []FollowRecord `gorm:"foreignKey:ContactId"` |
| | | } |
| | | ) |
| | | |
| | | func (Contact) TableName() string { |
| | | return "contacts" |
| | | } |
| | | |
| | | func NewContactSearch(db *gorm.DB) *ContactSearch { |
| | | if db == nil { |
| | | db = mysqlx.GetDB() |
| | | } |
| | | return &ContactSearch{ |
| | | Orm: db, |
| | | } |
| | | } |
| | | |
| | | func (slf *ContactSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&Contact{}) |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | if slf.Name != "" { |
| | | db = db.Where("name = ?", slf.Name) |
| | | } |
| | | |
| | | if slf.ClientId != 0 { |
| | | db = db.Where("client_id = ?", slf.ClientId) |
| | | } |
| | | |
| | | if len(slf.SearchMap) > 0 { |
| | | for key, value := range slf.SearchMap { |
| | | switch v := value.(type) { |
| | | case string: |
| | | if key == "name" || key == "position" || key == "phone" || key == "Number" { |
| | | db = db.Where(key+" LIKE ?", "%"+v+"%") |
| | | } |
| | | |
| | | if key == "member_name" { |
| | | db = db.Joins("Member").Where("Member.username LIKE ?", "%"+v+"%") |
| | | } |
| | | |
| | | if key == "client_name" { |
| | | db = db.Joins("inner join clients on clients.id = contacts.client_id").Where("clients.name LIKE ?", "%"+v+"%") |
| | | } |
| | | case int: |
| | | } |
| | | } |
| | | } |
| | | |
| | | return db |
| | | } |
| | | |
| | | func (slf *ContactSearch) Create(record *Contact) error { |
| | | var db = slf.build() |
| | | return db.Create(record).Error |
| | | } |
| | | |
| | | func (slf *ContactSearch) Update(record *Contact) error { |
| | | var db = slf.build() |
| | | m := map[string]interface{}{ |
| | | "name": record.Name, |
| | | "number": record.Number, |
| | | "client_id": record.ClientId, |
| | | "position": record.Position, |
| | | "phone": record.Phone, |
| | | "member_id": record.MemberId, |
| | | "is_first": record.IsFirst, |
| | | "wechat": record.Wechat, |
| | | "birthday": record.Birthday, |
| | | "email": record.Email, |
| | | "desc": record.Desc, |
| | | "country_id": record.CountryId, |
| | | "province_id": record.ProvinceId, |
| | | "city_id": record.CityId, |
| | | "region_id": record.RegionId, |
| | | } |
| | | return db.Updates(m).Error |
| | | } |
| | | |
| | | func (slf *ContactSearch) Delete() error { |
| | | var db = slf.build() |
| | | return db.Delete(&Contact{}).Error |
| | | } |
| | | |
| | | func (slf *ContactSearch) Find() (*Contact, error) { |
| | | var db = slf.build() |
| | | var record = new(Contact) |
| | | err := db.First(record).Error |
| | | return record, err |
| | | } |
| | | |
| | | func (slf *ContactSearch) FindAll() ([]*ContactDetail, int64, error) { |
| | | var db = slf.build() |
| | | var records = make([]*ContactDetail, 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("FollowRecord").Preload("Client").Preload("Country").Preload("Province").Preload("City").Preload("Region").Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *ContactSearch) SetId(id int) *ContactSearch { |
| | | slf.Id = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContactSearch) SetName(name string) *ContactSearch { |
| | | slf.Name = name |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContactSearch) First() (*Contact, error) { |
| | | var db = slf.build() |
| | | var record = new(Contact) |
| | | err := db.First(record).Error |
| | | return record, err |
| | | } |
| | | |
| | | func (slf *ContactSearch) SetClientId(clientId int) *ContactSearch { |
| | | slf.ClientId = clientId |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContactSearch) UpdateByMap(data map[string]interface{}) error { |
| | | var db = slf.build() |
| | | return db.Updates(data).Error |
| | | } |
| | | |
| | | func (slf *ContactSearch) SetPage(page, size int) *ContactSearch { |
| | | slf.PageNum, slf.PageSize = page, size |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContactSearch) SetOrder(order string) *ContactSearch { |
| | | slf.OrderBy = order |
| | | return slf |
| | | } |
| | | func (slf *ContactSearch) SetIds(ids []int) *ContactSearch { |
| | | slf.Orm = slf.Orm.Where("id in (?)", ids) |
| | | return slf |
| | | } |
| | | |
| | | func (slf *ContactSearch) SetSearchMap(data map[string]interface{}) *ContactSearch { |
| | | slf.SearchMap = data |
| | | return slf |
| | | } |
| | |
| | | package request
|
| | |
|
| | | type AddContact struct {
|
| | | Contact
|
| | | }
|
| | |
|
| | | type Contact struct {
|
| | | Name string `json:"name"` // 联系人名称
|
| | | Number string `json:"number"` // 联系人编号
|
| | | MemberId int `json:"member_id"` // 所属成员ID
|
| | | ClientId int `json:"client_id"` // 所属公司ID
|
| | | Position string `json:"position"` // 职位
|
| | | Phone string `json:"phone"` // 手机号
|
| | | IsFirst bool `json:"is_first"` // 是否首要联系人
|
| | | Wechat string `json:"wechat"` // 微信号
|
| | | Birthday string `json:"birthday" example:"1970-01-01 08:00:00"` // 生日
|
| | | Email string `json:"email"` // 邮箱
|
| | | Desc string `json:"desc"` // 备注
|
| | | CountryId int `json:"country_id"` // 国家ID
|
| | | ProvinceId int `json:"province_id"` // 省份ID
|
| | | CityId int `json:"city_id"` // 城市ID
|
| | | RegionId int `json:"region_id"` // 区域ID
|
| | | }
|
| | |
|
| | | type UpdateContact struct {
|
| | | Id int `json:"id"`
|
| | | Contact
|
| | | }
|
| | |
|
| | | type GetContactList struct {
|
| | | PageInfo
|
| | | Keyword string `json:"keyword"`
|
| | | }
|
| | |
|
| | | type DeleteContact struct {
|
| | | Ids []int `json:"ids"`
|
| | | }
|
| | | package request |
| | | |
| | | type AddContact struct { |
| | | Contact |
| | | } |
| | | |
| | | type Contact struct { |
| | | Name string `json:"name"` // 联系人名称 |
| | | Number string `json:"number"` // 联系人编号 |
| | | MemberId int `json:"member_id"` // 所属成员ID |
| | | ClientId int `json:"client_id"` // 所属公司ID |
| | | Position string `json:"position"` // 职位 |
| | | Phone string `json:"phone"` // 手机号 |
| | | IsFirst bool `json:"is_first"` // 是否首要联系人 |
| | | Wechat string `json:"wechat"` // 微信号 |
| | | Birthday string `json:"birthday" example:"1970-01-01 08:00:00"` // 生日 |
| | | Email string `json:"email"` // 邮箱 |
| | | Desc string `json:"desc"` // 备注 |
| | | CountryId int `json:"country_id"` // 国家ID |
| | | ProvinceId int `json:"province_id"` // 省份ID |
| | | CityId int `json:"city_id"` // 城市ID |
| | | RegionId int `json:"region_id"` // 区域ID |
| | | } |
| | | |
| | | type UpdateContact struct { |
| | | Id int `json:"id"` |
| | | Contact |
| | | } |
| | | |
| | | type GetContactList struct { |
| | | PageInfo |
| | | SearchMap map[string]interface{} `json:"search_map"` // 搜索条件: map[string]interface{}{"name": "xxx"}; {"name": "客户名称", "phone": "手机号码", "detail_address":"详细地址", "next_visit_time":"下回回访日期", "member_name": "销售负责人", "client_status": "客户状态", "client_level": "重要级别"} |
| | | } |
| | | |
| | | type DeleteContact struct { |
| | | Ids []int `json:"ids"` |
| | | } |
| | |
| | | package service
|
| | |
|
| | | import (
|
| | | "aps_crm/model"
|
| | | "aps_crm/pkg/ecode"
|
| | | "aps_crm/pkg/mysqlx"
|
| | | "gorm.io/gorm"
|
| | | )
|
| | |
|
| | | type ContactService struct{}
|
| | |
|
| | | func (ContactService) AddContact(contact *model.Contact) int {
|
| | |
|
| | | code := checkMemberAndCompany(contact)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | tx := mysqlx.GetDB().Begin()
|
| | | // check isFirst
|
| | | errCode := setFirstContact(tx, contact)
|
| | | if errCode != ecode.OK {
|
| | | return errCode
|
| | | }
|
| | |
|
| | | err := model.NewContactSearch(tx).Create(contact)
|
| | | if err != nil {
|
| | | tx.Rollback()
|
| | | return ecode.ContactExist
|
| | | }
|
| | |
|
| | | tx.Commit()
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ContactService) UpdateContact(contact *model.Contact) int {
|
| | | // check contact exist
|
| | | _, err := model.NewContactSearch(nil).SetId(contact.Id).First()
|
| | | if err != nil {
|
| | | return ecode.ContactNotExist
|
| | | }
|
| | |
|
| | | code := checkMemberAndCompany(contact)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | tx := mysqlx.GetDB().Begin()
|
| | | code = setFirstContact(tx, contact)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | // update contact
|
| | | err = model.NewContactSearch(tx).SetId(contact.Id).Update(contact)
|
| | | if err != nil {
|
| | | tx.Rollback()
|
| | | return ecode.ContactUpdateErr
|
| | | }
|
| | |
|
| | | tx.Commit()
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func checkMemberAndCompany(contact *model.Contact) int {
|
| | | // check client exist
|
| | | code := CheckClientExist(contact.ClientId)
|
| | | if code != ecode.OK {
|
| | | return code
|
| | | }
|
| | |
|
| | | // check member exist
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func setFirstContact(tx *gorm.DB, contact *model.Contact) int {
|
| | | if contact.IsFirst && contact.ClientId != 0 {
|
| | | err := model.NewContactSearch(tx).SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{
|
| | | "is_first": false,
|
| | | })
|
| | | if err != nil {
|
| | | tx.Rollback()
|
| | | return ecode.ContactUpdateErr
|
| | | }
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | // CheckContactExist check contact exist
|
| | | func CheckContactExist(id int) int {
|
| | | tmp, err := model.NewContactSearch(nil).SetId(id).First()
|
| | | if err != nil {
|
| | | return ecode.ContactNotExist
|
| | | }
|
| | |
|
| | | if tmp.Id == 0 {
|
| | | return ecode.ContactNotExist
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ContactService) GetContactList(page, pageSize int, keyword string) ([]*model.ContactDetail, int64, int) {
|
| | | // get contact list
|
| | | contacts, total, err := model.NewContactSearch(nil).SetKeyword(keyword).SetPage(page, pageSize).FindAll()
|
| | | if err != nil {
|
| | | return nil, 0, ecode.ContactListErr
|
| | | }
|
| | | return contacts, total, ecode.OK
|
| | | }
|
| | |
|
| | | func (ContactService) DeleteContact(ids []int) int {
|
| | | // delete client
|
| | | err := model.NewContactSearch(nil).SetIds(ids).Delete()
|
| | | if err != nil {
|
| | | return ecode.ContactDeleteErr
|
| | | }
|
| | | return ecode.OK
|
| | | }
|
| | |
|
| | | func (ContactService) Assign(ids []int, memberId int) int {
|
| | | // check contact exist
|
| | | //errCode := CheckContactExist(id)
|
| | | //if errCode != ecode.OK {
|
| | | // return errCode
|
| | | //}
|
| | |
|
| | | // assign contact
|
| | | err := model.NewContactSearch(nil).SetIds(ids).UpdateByMap(map[string]interface{}{
|
| | | "member_id": memberId,
|
| | | })
|
| | | if err != nil {
|
| | | return ecode.ContactAssignErr
|
| | | }
|
| | |
|
| | | return ecode.OK
|
| | | }
|
| | | package service |
| | | |
| | | import ( |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/mysqlx" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type ContactService struct{} |
| | | |
| | | func (ContactService) AddContact(contact *model.Contact) int { |
| | | |
| | | code := checkMemberAndCompany(contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | tx := mysqlx.GetDB().Begin() |
| | | // check isFirst |
| | | errCode := setFirstContact(tx, contact) |
| | | if errCode != ecode.OK { |
| | | return errCode |
| | | } |
| | | |
| | | err := model.NewContactSearch(tx).Create(contact) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactExist |
| | | } |
| | | |
| | | tx.Commit() |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) UpdateContact(contact *model.Contact) int { |
| | | // check contact exist |
| | | _, err := model.NewContactSearch(nil).SetId(contact.Id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | | |
| | | code := checkMemberAndCompany(contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | tx := mysqlx.GetDB().Begin() |
| | | code = setFirstContact(tx, contact) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | // update contact |
| | | err = model.NewContactSearch(tx).SetId(contact.Id).Update(contact) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactUpdateErr |
| | | } |
| | | |
| | | tx.Commit() |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func checkMemberAndCompany(contact *model.Contact) int { |
| | | // check client exist |
| | | code := CheckClientExist(contact.ClientId) |
| | | if code != ecode.OK { |
| | | return code |
| | | } |
| | | |
| | | // check member exist |
| | | return ecode.OK |
| | | } |
| | | |
| | | func setFirstContact(tx *gorm.DB, contact *model.Contact) int { |
| | | if contact.IsFirst && contact.ClientId != 0 { |
| | | err := model.NewContactSearch(tx).SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{ |
| | | "is_first": false, |
| | | }) |
| | | if err != nil { |
| | | tx.Rollback() |
| | | return ecode.ContactUpdateErr |
| | | } |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | // CheckContactExist check contact exist |
| | | func CheckContactExist(id int) int { |
| | | tmp, err := model.NewContactSearch(nil).SetId(id).First() |
| | | if err != nil { |
| | | return ecode.ContactNotExist |
| | | } |
| | | |
| | | if tmp.Id == 0 { |
| | | return ecode.ContactNotExist |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) GetContactList(page, pageSize int, data map[string]interface{}) ([]*model.ContactDetail, int64, int) { |
| | | // get contact list |
| | | contacts, total, err := model.NewContactSearch(nil).SetPage(page, pageSize).SetSearchMap(data).FindAll() |
| | | if err != nil { |
| | | return nil, 0, ecode.ContactListErr |
| | | } |
| | | return contacts, total, ecode.OK |
| | | } |
| | | |
| | | func (ContactService) DeleteContact(ids []int) int { |
| | | // delete client |
| | | err := model.NewContactSearch(nil).SetIds(ids).Delete() |
| | | if err != nil { |
| | | return ecode.ContactDeleteErr |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (ContactService) Assign(ids []int, memberId int) int { |
| | | // check contact exist |
| | | //errCode := CheckContactExist(id) |
| | | //if errCode != ecode.OK { |
| | | // return errCode |
| | | //} |
| | | |
| | | // assign contact |
| | | err := model.NewContactSearch(nil).SetIds(ids).UpdateByMap(map[string]interface{}{ |
| | | "member_id": memberId, |
| | | }) |
| | | if err != nil { |
| | | return ecode.ContactAssignErr |
| | | } |
| | | |
| | | return ecode.OK |
| | | } |