From c229f8922a4142a8ae2ac161bb6c121b9c9de672 Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期三, 09 八月 2023 19:35:49 +0800 Subject: [PATCH] fix --- service/contact.go | 278 +++++++------- api/v1/contact.go | 344 +++++++++--------- docs/swagger.yaml | 8 model/contact.go | 366 ++++++++++--------- docs/docs.go | 8 docs/swagger.json | 8 model/request/contact.go | 74 +- 7 files changed, 555 insertions(+), 531 deletions(-) diff --git a/api/v1/contact.go b/api/v1/contact.go index 5530eed..7daace8 100644 --- a/api/v1/contact.go +++ b/api/v1/contact.go @@ -1,172 +1,172 @@ -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), + }) +} diff --git a/docs/docs.go b/docs/docs.go index 8e464c9..fac055d 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -13673,9 +13673,6 @@ "request.GetContactList": { "type": "object", "properties": { - "keyword": { - "type": "string" - }, "page": { "description": "椤电爜", "type": "integer" @@ -13683,6 +13680,11 @@ "pageSize": { "description": "姣忛〉澶у皬", "type": "integer" + }, + "search_map": { + "description": "鎼滅储鏉′欢: map[string]interface{}{\"name\": \"xxx\"}; {\"name\": \"瀹㈡埛鍚嶇О\", \"phone\": \"鎵嬫満鍙风爜\", \"detail_address\":\"璇︾粏鍦板潃\", \"next_visit_time\":\"涓嬪洖鍥炶鏃ユ湡\", \"member_name\": \"閿�鍞礋璐d汉\", \"client_status\": \"瀹㈡埛鐘舵�乗", \"client_level\": \"閲嶈绾у埆\"}", + "type": "object", + "additionalProperties": true } } }, diff --git a/docs/swagger.json b/docs/swagger.json index 4be0e23..009ad54 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -13661,9 +13661,6 @@ "request.GetContactList": { "type": "object", "properties": { - "keyword": { - "type": "string" - }, "page": { "description": "椤电爜", "type": "integer" @@ -13671,6 +13668,11 @@ "pageSize": { "description": "姣忛〉澶у皬", "type": "integer" + }, + "search_map": { + "description": "鎼滅储鏉′欢: map[string]interface{}{\"name\": \"xxx\"}; {\"name\": \"瀹㈡埛鍚嶇О\", \"phone\": \"鎵嬫満鍙风爜\", \"detail_address\":\"璇︾粏鍦板潃\", \"next_visit_time\":\"涓嬪洖鍥炶鏃ユ湡\", \"member_name\": \"閿�鍞礋璐d汉\", \"client_status\": \"瀹㈡埛鐘舵�乗", \"client_level\": \"閲嶈绾у埆\"}", + "type": "object", + "additionalProperties": true } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 4b9f789..53bcd8b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2842,14 +2842,18 @@ 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": + "閿�鍞礋璐d汉", "client_status": "瀹㈡埛鐘舵��", "client_level": "閲嶈绾у埆"}' + type: object type: object request.GetContractList: properties: diff --git a/model/contact.go b/model/contact.go index a65213b..43d899a 100644 --- a/model/contact.go +++ b/model/contact.go @@ -1,175 +1,191 @@ -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:璐熻矗浜篒D"` - 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:璐熻矗浜篒D"` + 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 +} diff --git a/model/request/contact.go b/model/request/contact.go index b8a2255..7503d8c 100644 --- a/model/request/contact.go +++ b/model/request/contact.go @@ -1,37 +1,37 @@ -package request - -type AddContact struct { - Contact -} - -type Contact struct { - Name string `json:"name"` // 鑱旂郴浜哄悕绉� - Number string `json:"number"` // 鑱旂郴浜虹紪鍙� - MemberId int `json:"member_id"` // 鎵�灞炴垚鍛業D - ClientId int `json:"client_id"` // 鎵�灞炲叕鍙窱D - 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"` // 鎵�灞炴垚鍛業D + ClientId int `json:"client_id"` // 鎵�灞炲叕鍙窱D + 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": "閿�鍞礋璐d汉", "client_status": "瀹㈡埛鐘舵��", "client_level": "閲嶈绾у埆"} +} + +type DeleteContact struct { + Ids []int `json:"ids"` +} diff --git a/service/contact.go b/service/contact.go index 1455dc3..daab40e 100644 --- a/service/contact.go +++ b/service/contact.go @@ -1,139 +1,139 @@ -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 +} -- Gitblit v1.8.0