From 045199a75fceedea2e310d8798922c302f317849 Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期五, 07 七月 2023 14:56:19 +0800 Subject: [PATCH] add --- api/v1/index.go | 2 router/saleType.go | 19 + pkg/ecode/code.go | 8 docs/swagger.yaml | 113 ++++++++ model/request/saleType.go | 14 + service/saleType.go | 59 ++++ docs/docs.go | 181 ++++++++++++ docs/swagger.json | 181 ++++++++++++ model/response/response.go | 4 service/index.go | 1 api/v1/saleType.go | 112 ++++++++ model/index.go | 1 router/index.go | 2 model/saleType.go | 84 ++++++ 14 files changed, 781 insertions(+), 0 deletions(-) diff --git a/api/v1/index.go b/api/v1/index.go index 3eb61f6..d14c159 100644 --- a/api/v1/index.go +++ b/api/v1/index.go @@ -27,6 +27,7 @@ FollowRecordApi SaleChanceApi SaleStageApi + SaleTypeApi } var ApiGroup = new(Group) @@ -53,4 +54,5 @@ followRecordService = service.ServiceGroup.FollowRecordService saleChanceService = service.ServiceGroup.SaleChanceService saleStageService = service.ServiceGroup.SaleStageService + saleTypeService = service.ServiceGroup.SaleTypeService ) diff --git a/api/v1/saleType.go b/api/v1/saleType.go new file mode 100644 index 0000000..21fd66b --- /dev/null +++ b/api/v1/saleType.go @@ -0,0 +1,112 @@ +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" +) + +type SaleTypeApi struct{} + +// Add +// +// @Tags SaleType +// @Summary 娣诲姞閿�鍞被鍨� +// @Produce application/json +// @Param object body request.AddSaleType true "鏌ヨ鍙傛暟" +// @Success 200 {object} contextx.Response{} +// @Router /api/saleType/add [post] +func (s *SaleTypeApi) Add(c *gin.Context) { + var params request.AddSaleType + ctx, ok := contextx.NewContext(c, ¶ms) + if !ok { + return + } + + saleType := new(model.SaleType) + saleType.Name = params.Name + + errCode := saleTypeService.AddSaleType(saleType) + if errCode != ecode.OK { + ctx.Fail(errCode) + return + } + + ctx.Ok() +} + +// Delete +// +// @Tags SaleType +// @Summary 鍒犻櫎閿�鍞被鍨� +// @Produce application/json +// @Param id path int true "鏌ヨ鍙傛暟" +// @Success 200 {object} contextx.Response{} +// @Router /api/saleType/delete/{id} [delete] +func (s *SaleTypeApi) Delete(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + + id, _ := strconv.Atoi(c.Param("id")) + errCode := saleTypeService.DeleteSaleType(id) + if errCode != ecode.OK { + ctx.Fail(errCode) + return + } + + ctx.Ok() +} + +// Update +// +// @Tags SaleType +// @Summary 鏇存柊閿�鍞被鍨� +// @Produce application/json +// @Param object body request.UpdateSaleTypes true "鏌ヨ鍙傛暟" +// @Success 200 {object} contextx.Response{} +// @Router /api/saleType/update [put] +func (s *SaleTypeApi) Update(c *gin.Context) { + var params request.UpdateSaleTypes + ctx, ok := contextx.NewContext(c, ¶ms) + if !ok { + return + } + + errCode := saleTypeService.UpdateSaleType(params.SaleTypes) + if errCode != ecode.OK { + ctx.Fail(errCode) + return + } + + ctx.Ok() +} + +// List +// +// @Tags SaleType +// @Summary 鑾峰彇閿�鍞被鍨嬪垪琛� +// @Produce application/json +// @Success 200 {object} contextx.Response{data=response.SaleTypeResponse} +// @Router /api/saleType/list [get] +func (s *SaleTypeApi) List(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + + saleTypes, errCode := saleTypeService.GetSaleTypeList() + if errCode != ecode.OK { + ctx.Fail(errCode) + return + } + + ctx.OkWithDetailed(response.SaleTypeResponse{ + List: saleTypes, + }) +} diff --git a/docs/docs.go b/docs/docs.go index 9139769..f8d51cf 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -2223,6 +2223,125 @@ } } }, + "/api/saleType/add": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "娣诲姞閿�鍞被鍨�", + "parameters": [ + { + "description": "鏌ヨ鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.AddSaleType" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/contextx.Response" + } + } + } + } + }, + "/api/saleType/delete/{id}": { + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "鍒犻櫎閿�鍞被鍨�", + "parameters": [ + { + "type": "integer", + "description": "鏌ヨ鍙傛暟", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/contextx.Response" + } + } + } + } + }, + "/api/saleType/list": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "鑾峰彇閿�鍞被鍨嬪垪琛�", + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/contextx.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/response.SaleTypeResponse" + } + } + } + ] + } + } + } + } + }, + "/api/saleType/update": { + "put": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "鏇存柊閿�鍞被鍨�", + "parameters": [ + { + "description": "鏌ヨ鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.UpdateSaleTypes" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/contextx.Response" + } + } + } + } + }, "/api/salesLeads/add": { "post": { "produces": [ @@ -3378,6 +3497,17 @@ } } }, + "model.SaleType": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + } + }, "model.SalesLeads": { "type": "object", "properties": { @@ -3907,6 +4037,17 @@ } }, "request.AddSaleStage": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "string" + } + } + }, + "request.AddSaleType": { "type": "object", "required": [ "name" @@ -4888,6 +5029,35 @@ } } }, + "request.UpdateSaleType": { + "type": "object", + "required": [ + "id", + "name" + ], + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + } + }, + "request.UpdateSaleTypes": { + "type": "object", + "required": [ + "sale_types" + ], + "properties": { + "sale_types": { + "type": "array", + "items": { + "$ref": "#/definitions/request.UpdateSaleType" + } + } + } + }, "request.UpdateSalesLeads": { "type": "object", "properties": { @@ -5171,6 +5341,17 @@ } } }, + "response.SaleTypeResponse": { + "type": "object", + "properties": { + "list": { + "type": "array", + "items": { + "$ref": "#/definitions/model.SaleType" + } + } + } + }, "response.SalesLeadsResponse": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 0b77f5d..6e7d691 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -2211,6 +2211,125 @@ } } }, + "/api/saleType/add": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "娣诲姞閿�鍞被鍨�", + "parameters": [ + { + "description": "鏌ヨ鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.AddSaleType" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/contextx.Response" + } + } + } + } + }, + "/api/saleType/delete/{id}": { + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "鍒犻櫎閿�鍞被鍨�", + "parameters": [ + { + "type": "integer", + "description": "鏌ヨ鍙傛暟", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/contextx.Response" + } + } + } + } + }, + "/api/saleType/list": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "鑾峰彇閿�鍞被鍨嬪垪琛�", + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/contextx.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/response.SaleTypeResponse" + } + } + } + ] + } + } + } + } + }, + "/api/saleType/update": { + "put": { + "produces": [ + "application/json" + ], + "tags": [ + "SaleType" + ], + "summary": "鏇存柊閿�鍞被鍨�", + "parameters": [ + { + "description": "鏌ヨ鍙傛暟", + "name": "object", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.UpdateSaleTypes" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/contextx.Response" + } + } + } + } + }, "/api/salesLeads/add": { "post": { "produces": [ @@ -3366,6 +3485,17 @@ } } }, + "model.SaleType": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + } + }, "model.SalesLeads": { "type": "object", "properties": { @@ -3895,6 +4025,17 @@ } }, "request.AddSaleStage": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "string" + } + } + }, + "request.AddSaleType": { "type": "object", "required": [ "name" @@ -4876,6 +5017,35 @@ } } }, + "request.UpdateSaleType": { + "type": "object", + "required": [ + "id", + "name" + ], + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + } + }, + "request.UpdateSaleTypes": { + "type": "object", + "required": [ + "sale_types" + ], + "properties": { + "sale_types": { + "type": "array", + "items": { + "$ref": "#/definitions/request.UpdateSaleType" + } + } + } + }, "request.UpdateSalesLeads": { "type": "object", "properties": { @@ -5159,6 +5329,17 @@ } } }, + "response.SaleTypeResponse": { + "type": "object", + "properties": { + "list": { + "type": "array", + "items": { + "$ref": "#/definitions/model.SaleType" + } + } + } + }, "response.SalesLeadsResponse": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index fee26cc..030a27f 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -444,6 +444,13 @@ name: type: string type: object + model.SaleType: + properties: + id: + type: integer + name: + type: string + type: object model.SalesLeads: properties: city: @@ -805,6 +812,13 @@ type: string type: object request.AddSaleStage: + properties: + name: + type: string + required: + - name + type: object + request.AddSaleType: properties: name: type: string @@ -1480,6 +1494,25 @@ required: - sale_stages type: object + request.UpdateSaleType: + properties: + id: + type: integer + name: + type: string + required: + - id + - name + type: object + request.UpdateSaleTypes: + properties: + sale_types: + items: + $ref: '#/definitions/request.UpdateSaleType' + type: array + required: + - sale_types + type: object request.UpdateSalesLeads: properties: city_id: @@ -1663,6 +1696,13 @@ list: items: $ref: '#/definitions/model.SaleStage' + type: array + type: object + response.SaleTypeResponse: + properties: + list: + items: + $ref: '#/definitions/model.SaleType' type: array type: object response.SalesLeadsResponse: @@ -3039,6 +3079,79 @@ summary: 鏇存柊閿�鍞樁娈� tags: - SaleStage + /api/saleType/add: + post: + parameters: + - description: 鏌ヨ鍙傛暟 + in: body + name: object + required: true + schema: + $ref: '#/definitions/request.AddSaleType' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/contextx.Response' + summary: 娣诲姞閿�鍞被鍨� + tags: + - SaleType + /api/saleType/delete/{id}: + delete: + parameters: + - description: 鏌ヨ鍙傛暟 + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/contextx.Response' + summary: 鍒犻櫎閿�鍞被鍨� + tags: + - SaleType + /api/saleType/list: + get: + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/contextx.Response' + - properties: + data: + $ref: '#/definitions/response.SaleTypeResponse' + type: object + summary: 鑾峰彇閿�鍞被鍨嬪垪琛� + tags: + - SaleType + /api/saleType/update: + put: + parameters: + - description: 鏌ヨ鍙傛暟 + in: body + name: object + required: true + schema: + $ref: '#/definitions/request.UpdateSaleTypes' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/contextx.Response' + summary: 鏇存柊閿�鍞被鍨� + tags: + - SaleType /api/salesLeads/add: post: parameters: diff --git a/model/index.go b/model/index.go index 921ba9f..b94d2bf 100644 --- a/model/index.go +++ b/model/index.go @@ -42,6 +42,7 @@ SalesLeads{}, SalesSources{}, SaleStage{}, + SaleType{}, ) return err } diff --git a/model/request/saleType.go b/model/request/saleType.go new file mode 100644 index 0000000..2e86579 --- /dev/null +++ b/model/request/saleType.go @@ -0,0 +1,14 @@ +package request + +type AddSaleType struct { + Name string `json:"name" binding:"required"` +} + +type UpdateSaleType struct { + Id int `json:"id" binding:"required"` + Name string `json:"name" binding:"required"` +} + +type UpdateSaleTypes struct { + SaleTypes []*UpdateSaleType `json:"sale_types" binding:"required"` +} diff --git a/model/response/response.go b/model/response/response.go index b8909b4..f1ee34f 100644 --- a/model/response/response.go +++ b/model/response/response.go @@ -105,4 +105,8 @@ SaleStageResponse struct { List []*model.SaleStage `json:"list"` } + + SaleTypeResponse struct { + List []*model.SaleType `json:"list"` + } ) diff --git a/model/saleType.go b/model/saleType.go new file mode 100644 index 0000000..8370ee7 --- /dev/null +++ b/model/saleType.go @@ -0,0 +1,84 @@ +package model + +import ( + "aps_crm/pkg/mysqlx" + "gorm.io/gorm" +) + +type ( + SaleType struct { + Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` + Name string `json:"name" gorm:"column:name;type:varchar(255);comment:鍟嗘満绫诲瀷鍚嶇О"` + } + + SaleTypeSearch struct { + SaleType + Orm *gorm.DB + } +) + +func (SaleType) TableName() string { + return "sale_type" +} + +func NewSaleTypeSearch() *SaleTypeSearch { + return &SaleTypeSearch{ + Orm: mysqlx.GetDB(), + } +} + +func (slf *SaleTypeSearch) build() *gorm.DB { + var db = slf.Orm.Model(&SaleType{}) + if slf.Id != 0 { + db = db.Where("id = ?", slf.Id) + } + if slf.Name != "" { + db = db.Where("name = ?", slf.Name) + } + + return db +} + +func (slf *SaleTypeSearch) Create(record *SaleType) error { + var db = slf.build() + return db.Create(record).Error +} + +func (slf *SaleTypeSearch) Delete() error { + var db = slf.build() + return db.Delete(&SaleType{}).Error +} + +func (slf *SaleTypeSearch) Update(record *SaleType) error { + var db = slf.build() + return db.Updates(record).Error +} + +func (slf *SaleTypeSearch) Find() (*SaleType, error) { + var db = slf.build() + var record = &SaleType{} + err := db.First(record).Error + return record, err +} + +func (slf *SaleTypeSearch) FindAll() ([]*SaleType, error) { + var db = slf.build() + var records = make([]*SaleType, 0) + err := db.Find(&records).Error + return records, err +} + +func (slf *SaleTypeSearch) SetId(id int) *SaleTypeSearch { + slf.Id = id + return slf +} + +func (slf *SaleTypeSearch) SetName(name string) *SaleTypeSearch { + slf.Name = name + return slf +} + +func (slf *SaleTypeSearch) Updates(data map[string]interface{}) error { + var db = slf.build() + return db.Updates(data).Error +} diff --git a/pkg/ecode/code.go b/pkg/ecode/code.go index d0cdd6e..519dbc2 100644 --- a/pkg/ecode/code.go +++ b/pkg/ecode/code.go @@ -148,4 +148,12 @@ SaleStageNotExist = 2000002 // 閿�鍞樁娈典笉瀛樺湪 SaleStageListErr = 2000003 // 鑾峰彇閿�鍞樁娈靛垪琛ㄥけ璐� SaleStageSetErr = 2000004 // 璁剧疆閿�鍞樁娈靛け璐� + + SaleTypeExist = 2100001 // 閿�鍞被鍨嬪凡瀛樺湪 + SaleTypeNotExist = 2100002 // 閿�鍞被鍨嬩笉瀛樺湪 + SaleTypeListErr = 2100003 // 鑾峰彇閿�鍞被鍨嬪垪琛ㄥけ璐� + SaleTypeSetErr = 2100004 // 璁剧疆閿�鍞被鍨嬪け璐� + SaleTypeUpdateErr = 2100005 // 鏇存柊閿�鍞被鍨嬪け璐� + SaleTypeDeleteErr = 2100006 // 鍒犻櫎閿�鍞被鍨嬪け璐� + ) diff --git a/router/index.go b/router/index.go index 530a686..71d997c 100644 --- a/router/index.go +++ b/router/index.go @@ -33,6 +33,7 @@ FollowRecordRouter SaleChanceRouter SaleStageRouter + SaleTypeRouter } func InitRouter() *gin.Engine { @@ -83,6 +84,7 @@ routerGroup.InitFollowRecordRouter(PrivateGroup) // 娉ㄥ唽followRecord璺敱 routerGroup.InitSaleChanceRouter(PrivateGroup) // 娉ㄥ唽saleChance璺敱 routerGroup.InitSaleStageRouter(PrivateGroup) // 娉ㄥ唽saleStage璺敱 + routerGroup.InitSaleTypeRouter(PrivateGroup) // 娉ㄥ唽saleType璺敱 } return Router } diff --git a/router/saleType.go b/router/saleType.go new file mode 100644 index 0000000..2e5685f --- /dev/null +++ b/router/saleType.go @@ -0,0 +1,19 @@ +package router + +import ( + v1 "aps_crm/api/v1" + "github.com/gin-gonic/gin" +) + +type SaleTypeRouter struct{} + +func (s *SaleTypeRouter) InitSaleTypeRouter(router *gin.RouterGroup) { + saleTypeRouter := router.Group("saleType") + saleTypeApi := v1.ApiGroup.SaleTypeApi + { + saleTypeRouter.POST("add", saleTypeApi.Add) // 娣诲姞閿�鍞被鍨� + saleTypeRouter.DELETE("delete/:id", saleTypeApi.Delete) // 鍒犻櫎閿�鍞被鍨� + saleTypeRouter.PUT("update", saleTypeApi.Update) // 鏇存柊閿�鍞被鍨� + saleTypeRouter.GET("list", saleTypeApi.List) // 鑾峰彇閿�鍞被鍨嬪垪琛� + } +} diff --git a/service/index.go b/service/index.go index 5b17919..4e61453 100644 --- a/service/index.go +++ b/service/index.go @@ -22,6 +22,7 @@ FollowRecordService SaleChanceService SaleStageService + SaleTypeService } var ServiceGroup = new(Group) diff --git a/service/saleType.go b/service/saleType.go new file mode 100644 index 0000000..76ce49e --- /dev/null +++ b/service/saleType.go @@ -0,0 +1,59 @@ +package service + +import ( + "aps_crm/model" + "aps_crm/model/request" + "aps_crm/pkg/ecode" +) + +type SaleTypeService struct{} + +func (SaleTypeService) AddSaleType(saleType *model.SaleType) int { + err := model.NewSaleTypeSearch().Create(saleType) + if err != nil { + return ecode.SaleTypeExist + } + + return ecode.OK +} + +func (SaleTypeService) DeleteSaleType(id int) int { + _, err := model.NewSaleTypeSearch().SetId(id).Find() + if err != nil { + return ecode.SaleTypeNotExist + } + + err = model.NewSaleTypeSearch().SetId(id).Delete() + if err != nil { + return ecode.SaleTypeNotExist + } + return ecode.OK +} + +func (SaleTypeService) GetSaleTypeList() ([]*model.SaleType, int) { + list, err := model.NewSaleTypeSearch().FindAll() + if err != nil { + return nil, ecode.SaleTypeListErr + } + + return list, ecode.OK +} + +func (SaleTypeService) UpdateSaleType(saleTypes []*request.UpdateSaleType) int { + for _, v := range saleTypes { + // check saleType exist + _, err := model.NewSaleTypeSearch().SetId(v.Id).Find() + if err != nil { + return ecode.SaleTypeNotExist + } + + err = model.NewSaleTypeSearch().SetId(v.Id).Updates(map[string]interface{}{ + "name": v.Name, + }) + if err != nil { + return ecode.SaleTypeSetErr + } + } + + return ecode.OK +} -- Gitblit v1.8.0