fix
wangpengfei
2023-08-02 7e0823fc3b352cfcbb4a2f21088c00db2c00c395
fix

add batch delete function to salesLeads, followRecord module
13个文件已修改
1190 ■■■■ 已修改文件
api/v1/followRecord.go 447 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/salesLeads.go 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 48 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 48 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 34 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/followRecord.go 374 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/request/followRecord.go 68 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/request/salesLeads.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/salesLeads.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/followRecord.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/salesLeads.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/followRecord.go 124 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/salesLeads.go 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/followRecord.go
@@ -1,224 +1,223 @@
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"
    "strconv"
    "time"
)
type FollowRecordApi struct{}
// Add
//
//    @Tags        FollowRecord
//    @Summary    添加跟进记录
//    @Produce    application/json
//    @Param        object    body        request.AddFollowRecord    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}
//    @Router        /api/followRecord/add [post]
func (fr *FollowRecordApi) Add(c *gin.Context) {
    var params request.AddFollowRecord
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    errCode, followRecord := checkFollowRecordParams(params.FollowRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    errCode = followRecordService.AddFollowRecord(followRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// Delete
//
//    @Tags        FollowRecord
//    @Summary    删除跟进记录
//    @Produce    application/json
//    @Param        id    path        string    true    "跟进记录id"
//    @Success    200    {object}    contextx.Response{}
//    @Router        /api/followRecord/delete/{id} [delete]
func (fr *FollowRecordApi) Delete(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    if !ok {
        return
    }
    id, _ := strconv.Atoi(c.Param("id"))
    errCode := followRecordService.DeleteFollowRecord(id)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// Update
//
//    @Tags        FollowRecord
//    @Summary    更新跟进记录
//    @Produce    application/json
//    @Param        object    body        request.UpdateFollowRecord    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}
//    @Router        /api/followRecord/update [put]
func (fr *FollowRecordApi) Update(c *gin.Context) {
    var params request.UpdateFollowRecord
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    errCode, followRecord := checkFollowRecordParams(params.FollowRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    followRecord.Id = params.Id
    errCode = followRecordService.UpdateFollowRecord(followRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// checkFollowRecordParams
// 检查跟进记录参数
func checkFollowRecordParams(followRecord request.FollowRecord) (int, *model.FollowRecord) {
    var followRecordModel model.FollowRecord
    //if followRecord.ClientId != 0 {
    //    // check client exist
    //    if service.CheckClientExist(followRecord.ClientId) != ecode.OK {
    //        return ecode.ClientNotExist, &followRecordModel
    //    }
    //}
    //
    //if followRecord.ContactId != 0 {
    //    // check contact exist
    //    if service.CheckContactExist(followRecord.ContactId) != ecode.OK {
    //        return ecode.ContactNotExist, &followRecordModel
    //    }
    //}
    //
    //if followRecord.SalesLeadsId != 0 {
    //    // check sales leads exist
    //    if service.CheckSalesLeadsExist(followRecord.SalesLeadsId) != ecode.OK {
    //        return ecode.SalesLeadsNotExist, &followRecordModel
    //    }
    //}
    //
    //// check member id
    //if followRecord.MemberId == 0 {
    //    // todo check member exist
    //    return ecode.InvalidParams, &followRecordModel
    //}
    //
    //// check number
    //if followRecord.Number == "" {
    //    return ecode.InvalidParams, &followRecordModel
    //}
    //
    //// check follow content
    //if followRecord.Content == "" {
    //    return ecode.InvalidParams, &followRecordModel
    //}
    // check follow time
    t, err := checkTimeFormat(followRecord.FollowTime)
    if err != nil {
        return ecode.InvalidParams, &followRecordModel
    }
    followRecordModel.FollowTime = t
    t, err = checkTimeFormat(followRecord.NextFollowTime)
    if err != nil {
        return ecode.InvalidParams, &followRecordModel
    }
    followRecordModel.NextFollowTime = t
    followRecordModel.ClientId = followRecord.ClientId
    followRecordModel.Content = followRecord.Content
    followRecordModel.MemberId = followRecord.MemberId
    followRecordModel.Number = followRecord.Number
    followRecordModel.ClientStatusId = followRecord.ClientStatusId
    followRecordModel.ContactId = followRecord.ContactId
    followRecordModel.Topic = followRecord.Topic
    followRecordModel.SalesLeadsId = followRecord.SalesLeadsId
    followRecordModel.SaleChanceId = followRecord.SaleChanceId
    followRecordModel.ContactInformationId = followRecord.ContactInformationId
    followRecordModel.Purpose = followRecord.Purpose
    followRecordModel.Content = followRecord.Content
    followRecordModel.Record = followRecord.Record
    return ecode.OK, &followRecordModel
}
// checkTimeFormat
// 检查时间格式
func checkTimeFormat(t string) (time.Time, error) {
    if t == "" {
        t = "1900-01-01T00:00:00+08:00"
    }
    location, err := time.LoadLocation("Asia/Shanghai")
    if err != nil {
        return time.Time{}, err
    }
    tt, err := time.Parse("2006-01-02T15:04:05.000Z", t)
    if err == nil {
        return tt.In(location), nil
    }
    tt, err = time.Parse("2006-01-02T15:04:05-07:00", t)
    if err == nil {
        return tt.In(location), nil
    }
    return time.Time{}, err
}
// List
//
//    @Tags        FollowRecord
//    @Summary    回访记录列表
//    @Produce    application/json
//    @Param        object    body        request.GetFollowRecordList    true    "参数"
//    @Success    200        {object}    contextx.Response{data=response.FollowRecordResponse}
//    @Router        /api/followRecord/list [post]
func (fr *FollowRecordApi) List(c *gin.Context) {
    var params request.GetFollowRecordList
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    followRecords, total, errCode := followRecordService.GetFollowRecordList(params.Page, params.PageSize, params.Keyword)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.OkWithDetailed(response.FollowRecordResponse{
        List:  followRecords,
        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"
    "time"
)
type FollowRecordApi struct{}
// Add
//
//    @Tags        FollowRecord
//    @Summary    添加跟进记录
//    @Produce    application/json
//    @Param        object    body        request.AddFollowRecord    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}
//    @Router        /api/followRecord/add [post]
func (fr *FollowRecordApi) Add(c *gin.Context) {
    var params request.AddFollowRecord
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    errCode, followRecord := checkFollowRecordParams(params.FollowRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    errCode = followRecordService.AddFollowRecord(followRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// Delete
//
//    @Tags        FollowRecord
//    @Summary    删除跟进记录
//    @Produce    application/json
//    @Param        object    body        request.DeleteFollowRecord true    "查询参数"
//    @Success    200    {object}    contextx.Response{}
//    @Router        /api/followRecord/delete [delete]
func (fr *FollowRecordApi) Delete(c *gin.Context) {
    var params request.DeleteFollowRecord
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    errCode := followRecordService.DeleteFollowRecord(params.Ids)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// Update
//
//    @Tags        FollowRecord
//    @Summary    更新跟进记录
//    @Produce    application/json
//    @Param        object    body        request.UpdateFollowRecord    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}
//    @Router        /api/followRecord/update [put]
func (fr *FollowRecordApi) Update(c *gin.Context) {
    var params request.UpdateFollowRecord
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    errCode, followRecord := checkFollowRecordParams(params.FollowRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    followRecord.Id = params.Id
    errCode = followRecordService.UpdateFollowRecord(followRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
// checkFollowRecordParams
// 检查跟进记录参数
func checkFollowRecordParams(followRecord request.FollowRecord) (int, *model.FollowRecord) {
    var followRecordModel model.FollowRecord
    //if followRecord.ClientId != 0 {
    //    // check client exist
    //    if service.CheckClientExist(followRecord.ClientId) != ecode.OK {
    //        return ecode.ClientNotExist, &followRecordModel
    //    }
    //}
    //
    //if followRecord.ContactId != 0 {
    //    // check contact exist
    //    if service.CheckContactExist(followRecord.ContactId) != ecode.OK {
    //        return ecode.ContactNotExist, &followRecordModel
    //    }
    //}
    //
    //if followRecord.SalesLeadsId != 0 {
    //    // check sales leads exist
    //    if service.CheckSalesLeadsExist(followRecord.SalesLeadsId) != ecode.OK {
    //        return ecode.SalesLeadsNotExist, &followRecordModel
    //    }
    //}
    //
    //// check member id
    //if followRecord.MemberId == 0 {
    //    // todo check member exist
    //    return ecode.InvalidParams, &followRecordModel
    //}
    //
    //// check number
    //if followRecord.Number == "" {
    //    return ecode.InvalidParams, &followRecordModel
    //}
    //
    //// check follow content
    //if followRecord.Content == "" {
    //    return ecode.InvalidParams, &followRecordModel
    //}
    // check follow time
    t, err := checkTimeFormat(followRecord.FollowTime)
    if err != nil {
        return ecode.InvalidParams, &followRecordModel
    }
    followRecordModel.FollowTime = t
    t, err = checkTimeFormat(followRecord.NextFollowTime)
    if err != nil {
        return ecode.InvalidParams, &followRecordModel
    }
    followRecordModel.NextFollowTime = t
    followRecordModel.ClientId = followRecord.ClientId
    followRecordModel.Content = followRecord.Content
    followRecordModel.MemberId = followRecord.MemberId
    followRecordModel.Number = followRecord.Number
    followRecordModel.ClientStatusId = followRecord.ClientStatusId
    followRecordModel.ContactId = followRecord.ContactId
    followRecordModel.Topic = followRecord.Topic
    followRecordModel.SalesLeadsId = followRecord.SalesLeadsId
    followRecordModel.SaleChanceId = followRecord.SaleChanceId
    followRecordModel.ContactInformationId = followRecord.ContactInformationId
    followRecordModel.Purpose = followRecord.Purpose
    followRecordModel.Content = followRecord.Content
    followRecordModel.Record = followRecord.Record
    return ecode.OK, &followRecordModel
}
// checkTimeFormat
// 检查时间格式
func checkTimeFormat(t string) (time.Time, error) {
    if t == "" {
        t = "1900-01-01T00:00:00+08:00"
    }
    location, err := time.LoadLocation("Asia/Shanghai")
    if err != nil {
        return time.Time{}, err
    }
    tt, err := time.Parse("2006-01-02T15:04:05.000Z", t)
    if err == nil {
        return tt.In(location), nil
    }
    tt, err = time.Parse("2006-01-02T15:04:05-07:00", t)
    if err == nil {
        return tt.In(location), nil
    }
    return time.Time{}, err
}
// List
//
//    @Tags        FollowRecord
//    @Summary    回访记录列表
//    @Produce    application/json
//    @Param        object    body        request.GetFollowRecordList    true    "参数"
//    @Success    200        {object}    contextx.Response{data=response.FollowRecordResponse}
//    @Router        /api/followRecord/list [post]
func (fr *FollowRecordApi) List(c *gin.Context) {
    var params request.GetFollowRecordList
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    followRecords, total, errCode := followRecordService.GetFollowRecordList(params.Page, params.PageSize, params.Keyword)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.OkWithDetailed(response.FollowRecordResponse{
        List:  followRecords,
        Count: int(total),
    })
}
api/v1/salesLeads.go
@@ -8,7 +8,6 @@
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "github.com/gin-gonic/gin"
    "strconv"
)
type SalesLeadsApi struct{}
@@ -48,17 +47,17 @@
//    @Tags        SalesLeads
//    @Summary    删除销售线索
//    @Produce    application/json
//    @Param        id    path        int    true    "查询参数"
//    @Param        object    body        request.DeleteSalesLeads true    "查询参数"
//    @Success    200    {object}    contextx.Response{}
//    @Router        /api/salesLeads/delete/{id} [delete]
//    @Router        /api/salesLeads/delete [delete]
func (s *SalesLeadsApi) Delete(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    var params request.DeleteSalesLeads
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    id, _ := strconv.Atoi(c.Param("id"))
    errCode := salesLeadsService.DeleteSalesLeads(id)
    errCode := salesLeadsService.DeleteSalesLeads(params.Ids)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
docs/docs.go
@@ -2106,7 +2106,7 @@
                }
            }
        },
        "/api/followRecord/delete/{id}": {
        "/api/followRecord/delete": {
            "delete": {
                "produces": [
                    "application/json"
@@ -2117,11 +2117,13 @@
                "summary": "删除跟进记录",
                "parameters": [
                    {
                        "type": "string",
                        "description": "跟进记录id",
                        "name": "id",
                        "in": "path",
                        "required": true
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DeleteFollowRecord"
                        }
                    }
                ],
                "responses": {
@@ -4943,7 +4945,7 @@
                }
            }
        },
        "/api/salesLeads/delete/{id}": {
        "/api/salesLeads/delete": {
            "delete": {
                "produces": [
                    "application/json"
@@ -4954,11 +4956,13 @@
                "summary": "删除销售线索",
                "parameters": [
                    {
                        "type": "integer",
                        "description": "查询参数",
                        "name": "id",
                        "in": "path",
                        "required": true
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DeleteSalesLeads"
                        }
                    }
                ],
                "responses": {
@@ -9954,6 +9958,28 @@
                }
            }
        },
        "request.DeleteFollowRecord": {
            "type": "object",
            "properties": {
                "ids": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                }
            }
        },
        "request.DeleteSalesLeads": {
            "type": "object",
            "properties": {
                "ids": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                }
            }
        },
        "request.DeleteUserReq": {
            "type": "object",
            "properties": {
docs/swagger.json
@@ -2094,7 +2094,7 @@
                }
            }
        },
        "/api/followRecord/delete/{id}": {
        "/api/followRecord/delete": {
            "delete": {
                "produces": [
                    "application/json"
@@ -2105,11 +2105,13 @@
                "summary": "删除跟进记录",
                "parameters": [
                    {
                        "type": "string",
                        "description": "跟进记录id",
                        "name": "id",
                        "in": "path",
                        "required": true
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DeleteFollowRecord"
                        }
                    }
                ],
                "responses": {
@@ -4931,7 +4933,7 @@
                }
            }
        },
        "/api/salesLeads/delete/{id}": {
        "/api/salesLeads/delete": {
            "delete": {
                "produces": [
                    "application/json"
@@ -4942,11 +4944,13 @@
                "summary": "删除销售线索",
                "parameters": [
                    {
                        "type": "integer",
                        "description": "查询参数",
                        "name": "id",
                        "in": "path",
                        "required": true
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DeleteSalesLeads"
                        }
                    }
                ],
                "responses": {
@@ -9942,6 +9946,28 @@
                }
            }
        },
        "request.DeleteFollowRecord": {
            "type": "object",
            "properties": {
                "ids": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                }
            }
        },
        "request.DeleteSalesLeads": {
            "type": "object",
            "properties": {
                "ids": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                }
            }
        },
        "request.DeleteUserReq": {
            "type": "object",
            "properties": {
docs/swagger.yaml
@@ -1918,6 +1918,20 @@
        description: 国家ID
        type: integer
    type: object
  request.DeleteFollowRecord:
    properties:
      ids:
        items:
          type: integer
        type: array
    type: object
  request.DeleteSalesLeads:
    properties:
      ids:
        items:
          type: integer
        type: array
    type: object
  request.DeleteUserReq:
    properties:
      userId:
@@ -5372,14 +5386,15 @@
      summary: 添加跟进记录
      tags:
      - FollowRecord
  /api/followRecord/delete/{id}:
  /api/followRecord/delete:
    delete:
      parameters:
      - description: 跟进记录id
        in: path
        name: id
      - description: 查询参数
        in: body
        name: object
        required: true
        type: string
        schema:
          $ref: '#/definitions/request.DeleteFollowRecord'
      produces:
      - application/json
      responses:
@@ -7114,14 +7129,15 @@
      summary: 添加销售线索
      tags:
      - SalesLeads
  /api/salesLeads/delete/{id}:
  /api/salesLeads/delete:
    delete:
      parameters:
      - description: 查询参数
        in: path
        name: id
        in: body
        name: object
        required: true
        type: integer
        schema:
          $ref: '#/definitions/request.DeleteSalesLeads'
      produces:
      - application/json
      responses:
model/followRecord.go
@@ -1,185 +1,189 @@
package model
import (
    "aps_crm/pkg/mysqlx"
    "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"`
        gorm.Model           `json:"-"`
    }
    FollowRecordSearch struct {
        FollowRecord
        Orm      *gorm.DB
        Keyword  string
        OrderBy  string
        PageNum  int
        PageSize int
    }
)
func (FollowRecord) TableName() string {
    return "follow_records"
}
func NewFollowRecordSearch() *FollowRecordSearch {
    return &FollowRecordSearch{
        Orm: mysqlx.GetDB(),
    }
}
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)
    }
    return db
}
func (slf *FollowRecordSearch) First() (*FollowRecord, error) {
    var record = new(FollowRecord)
    err := slf.build().First(record).Error
    return record, err
}
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, total, err
}
func (slf *FollowRecordSearch) Count() (int64, error) {
    var count int64
    err := slf.build().Count(&count).Error
    return count, err
}
func (slf *FollowRecordSearch) Page(page, pageSize int) ([]*FollowRecord, int64, error) {
    var records = make([]*FollowRecord, 0)
    var count int64
    err := slf.build().Count(&count).Error
    if err != nil {
        return records, count, err
    }
    err = slf.build().Offset((page - 1) * pageSize).Limit(pageSize).Find(&records).Error
    return records, count, err
}
func (slf *FollowRecordSearch) Create(record *FollowRecord) error {
    var db = slf.build()
    return db.Create(record).Error
}
func (slf *FollowRecordSearch) Update(record *FollowRecord) error {
    var db = slf.build()
    return db.Updates(record).Error
}
func (slf *FollowRecordSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&slf.FollowRecord).Error
}
func (slf *FollowRecordSearch) SetId(id int) *FollowRecordSearch {
    slf.Id = id
    return slf
}
func (slf *FollowRecordSearch) SetClientId(clientId int) *FollowRecordSearch {
    slf.ClientId = clientId
    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
}
func (slf *FollowRecordSearch) SetOrder(order string) *FollowRecordSearch {
    slf.OrderBy = order
    return slf
}
package model
import (
    "aps_crm/pkg/mysqlx"
    "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"`
        gorm.Model           `json:"-"`
    }
    FollowRecordSearch struct {
        FollowRecord
        Orm      *gorm.DB
        Keyword  string
        OrderBy  string
        PageNum  int
        PageSize int
    }
)
func (FollowRecord) TableName() string {
    return "follow_records"
}
func NewFollowRecordSearch() *FollowRecordSearch {
    return &FollowRecordSearch{
        Orm: mysqlx.GetDB(),
    }
}
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)
    }
    return db
}
func (slf *FollowRecordSearch) First() (*FollowRecord, error) {
    var record = new(FollowRecord)
    err := slf.build().First(record).Error
    return record, err
}
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, total, err
}
func (slf *FollowRecordSearch) Count() (int64, error) {
    var count int64
    err := slf.build().Count(&count).Error
    return count, err
}
func (slf *FollowRecordSearch) Page(page, pageSize int) ([]*FollowRecord, int64, error) {
    var records = make([]*FollowRecord, 0)
    var count int64
    err := slf.build().Count(&count).Error
    if err != nil {
        return records, count, err
    }
    err = slf.build().Offset((page - 1) * pageSize).Limit(pageSize).Find(&records).Error
    return records, count, err
}
func (slf *FollowRecordSearch) Create(record *FollowRecord) error {
    var db = slf.build()
    return db.Create(record).Error
}
func (slf *FollowRecordSearch) Update(record *FollowRecord) error {
    var db = slf.build()
    return db.Updates(record).Error
}
func (slf *FollowRecordSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&slf.FollowRecord).Error
}
func (slf *FollowRecordSearch) SetId(id int) *FollowRecordSearch {
    slf.Id = id
    return slf
}
func (slf *FollowRecordSearch) SetClientId(clientId int) *FollowRecordSearch {
    slf.ClientId = clientId
    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
}
func (slf *FollowRecordSearch) SetOrder(order string) *FollowRecordSearch {
    slf.OrderBy = order
    return slf
}
func (slf *FollowRecordSearch) SetIds(ids []int) *FollowRecordSearch {
    slf.Orm = slf.Orm.Where("id in (?)", ids)
    return slf
}
model/request/followRecord.go
@@ -1,32 +1,36 @@
package request
type AddFollowRecord struct {
    FollowRecord FollowRecord `json:"follow_record" binding:"required"`
}
type FollowRecord struct {
    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           string `json:"follow_time" gorm:"column:follow_time;type:datetime;comment:跟进时间"`
    NextFollowTime       string `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:varchar(255);comment:跟进内容"`
}
type UpdateFollowRecord struct {
    Id           int          `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
    FollowRecord FollowRecord `json:"follow_record" binding:"required"`
}
type GetFollowRecordList struct {
    PageInfo
    Keyword string `json:"keyword"`
}
package request
type AddFollowRecord struct {
    FollowRecord FollowRecord `json:"follow_record" binding:"required"`
}
type FollowRecord struct {
    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           string `json:"follow_time" gorm:"column:follow_time;type:datetime;comment:跟进时间"`
    NextFollowTime       string `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:varchar(255);comment:跟进内容"`
}
type UpdateFollowRecord struct {
    Id           int          `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
    FollowRecord FollowRecord `json:"follow_record" binding:"required"`
}
type GetFollowRecordList struct {
    PageInfo
    Keyword string `json:"keyword"`
}
type DeleteFollowRecord struct {
    Ids []int `json:"ids"`
}
model/request/salesLeads.go
@@ -25,3 +25,7 @@
    PageInfo
    Keyword string `json:"keyword"`
}
type DeleteSalesLeads struct {
    Ids []int `json:"ids"`
}
model/salesLeads.go
@@ -163,3 +163,7 @@
    var db = slf.build()
    return db.Updates(data).Error
}
func (slf *SalesLeadsSearch) SetIds(ids []int) *SalesLeadsSearch {
    slf.Orm = slf.Orm.Where("id in (?)", ids)
    return slf
}
router/followRecord.go
@@ -12,7 +12,7 @@
    followRecordApi := v1.ApiGroup.FollowRecordApi
    {
        followRecordRouter.POST("add", followRecordApi.Add)             // 添加跟进记录
        followRecordRouter.DELETE("delete/:id", followRecordApi.Delete) // 删除跟进记录
        followRecordRouter.DELETE("delete", followRecordApi.Delete)     // 删除跟进记录
        followRecordRouter.PUT("update", followRecordApi.Update)        // 更新跟进记录
        followRecordRouter.POST("list", followRecordApi.List)            // 获取跟进记录列表
    }
router/salesLeads.go
@@ -12,7 +12,7 @@
    salesLeadsApi := v1.ApiGroup.SalesLeadsApi
    {
        salesLeadsRouter.POST("add", salesLeadsApi.Add)             // 添加销售线索
        salesLeadsRouter.DELETE("delete/:id", salesLeadsApi.Delete) // 删除销售线索
        salesLeadsRouter.DELETE("delete", salesLeadsApi.Delete)     // 删除销售线索
        salesLeadsRouter.PUT("update", salesLeadsApi.Update)        // 更新销售线索
        salesLeadsRouter.POST("list", salesLeadsApi.List)            // 获取销售线索列表
    }
service/followRecord.go
@@ -1,65 +1,59 @@
package service
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
)
type FollowRecordService struct{}
func (FollowRecordService) AddFollowRecord(followRecord *model.FollowRecord) int {
    err := model.NewFollowRecordSearch().Create(followRecord)
    if err != nil {
        return ecode.FollowRecordExist
    }
    return ecode.OK
}
func (FollowRecordService) DeleteFollowRecord(id int) int {
    // check followRecord exist
    _, err := model.NewFollowRecordSearch().SetId(id).First()
    if err != nil {
        return ecode.FollowRecordNotExist
    }
    // delete followRecord
    err = model.NewFollowRecordSearch().SetId(id).Delete()
    if err != nil {
        return ecode.FollowRecordDeleteErr
    }
    return ecode.OK
}
// check followRecord exist
func checkFollowRecordExist(id int) int {
    _, err := model.NewFollowRecordSearch().SetId(id).First()
    if err != nil {
        return ecode.FollowRecordNotExist
    }
    return ecode.OK
}
func (FollowRecordService) UpdateFollowRecord(followRecord *model.FollowRecord) int {
    // check followRecord exist
    errCode := checkFollowRecordExist(followRecord.Id)
    if errCode != ecode.OK {
        return errCode
    }
    // update followRecord
    err := model.NewFollowRecordSearch().SetId(followRecord.Id).Update(followRecord)
    if err != nil {
        return ecode.FollowRecordUpdateErr
    }
    return ecode.OK
}
func (FollowRecordService) GetFollowRecordList(page, pageSize int, keyword string) ([]*model.FollowRecord, int64, int) {
    // get contact list
    contacts, total, err := model.NewFollowRecordSearch().SetKeyword(keyword).SetPage(page, pageSize).FindAll()
    if err != nil {
    return nil, 0, ecode.FollowRecordListErr
    }
    return contacts, total, ecode.OK
}
package service
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
)
type FollowRecordService struct{}
func (FollowRecordService) AddFollowRecord(followRecord *model.FollowRecord) int {
    err := model.NewFollowRecordSearch().Create(followRecord)
    if err != nil {
        return ecode.FollowRecordExist
    }
    return ecode.OK
}
// check followRecord exist
func checkFollowRecordExist(id int) int {
    _, err := model.NewFollowRecordSearch().SetId(id).First()
    if err != nil {
        return ecode.FollowRecordNotExist
    }
    return ecode.OK
}
func (FollowRecordService) UpdateFollowRecord(followRecord *model.FollowRecord) int {
    // check followRecord exist
    errCode := checkFollowRecordExist(followRecord.Id)
    if errCode != ecode.OK {
        return errCode
    }
    // update followRecord
    err := model.NewFollowRecordSearch().SetId(followRecord.Id).Update(followRecord)
    if err != nil {
        return ecode.FollowRecordUpdateErr
    }
    return ecode.OK
}
func (FollowRecordService) GetFollowRecordList(page, pageSize int, keyword string) ([]*model.FollowRecord, int64, int) {
    // get contact list
    contacts, total, err := model.NewFollowRecordSearch().SetKeyword(keyword).SetPage(page, pageSize).FindAll()
    if err != nil {
        return nil, 0, ecode.FollowRecordListErr
    }
    return contacts, total, ecode.OK
}
func (FollowRecordService) DeleteFollowRecord(ids []int) int {
    // delete client
    err := model.NewFollowRecordSearch().SetIds(ids).Delete()
    if err != nil {
        return ecode.FollowRecordDeleteErr
    }
    return ecode.OK
}
service/salesLeads.go
@@ -15,21 +15,6 @@
    return ecode.OK
}
func (SalesLeadsService) DeleteSalesLeads(id int) int {
    // check salesLeads exist
    _, err := model.NewSalesLeadsSearch().SetId(id).Find()
    if err != nil {
        return ecode.SalesLeadsNotExist
    }
    // delete salesLeads
    err = model.NewSalesLeadsSearch().SetId(id).Delete()
    if err != nil {
        return ecode.SalesLeadsDeleteErr
    }
    return ecode.OK
}
func (SalesLeadsService) UpdateSalesLeads(salesLeads *model.SalesLeads) int {
    // update salesLeads
    err := model.NewSalesLeadsSearch().SetId(salesLeads.Id).Update(salesLeads)
@@ -80,3 +65,12 @@
    return ecode.OK
}
func (SalesLeadsService) DeleteSalesLeads(ids []int) int {
    // delete client
    err := model.NewSalesLeadsSearch().SetIds(ids).Delete()
    if err != nil {
        return ecode.SalesLeadsDeleteErr
    }
    return ecode.OK
}