From cb5ab0ae37c8d0c709c2483b3ad5a6cc2c487ba7 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 17 Dec 2021 17:33:20 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E7=BD=91?= =?UTF-8?q?=E7=AB=99=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=A1=88?= =?UTF-8?q?=E4=BE=8B=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/website/api/service.go | 38 +++++ app/api/website/controller/service.go | 152 ++++++++++++++++++ .../website/model/service_solution_case.go | 48 ++++++ .../model/service_solution_case_detail.go | 11 ++ app/api/website/model/sys_banner.go | 11 ++ app/common/init.go | 3 +- app/common/model/service_solution_case.go | 34 ++++ .../model/service_solution_case_detail.go | 23 +++ app/common/model/sys_banner.go | 15 ++ router/address.go | 8 + utils/encrypt.go | 6 +- 11 files changed, 347 insertions(+), 2 deletions(-) create mode 100644 app/api/website/api/service.go create mode 100644 app/api/website/controller/service.go create mode 100644 app/api/website/model/service_solution_case.go create mode 100644 app/api/website/model/service_solution_case_detail.go create mode 100644 app/api/website/model/sys_banner.go create mode 100644 app/common/model/service_solution_case.go create mode 100644 app/common/model/service_solution_case_detail.go create mode 100644 app/common/model/sys_banner.go diff --git a/app/api/website/api/service.go b/app/api/website/api/service.go new file mode 100644 index 0000000..802615a --- /dev/null +++ b/app/api/website/api/service.go @@ -0,0 +1,38 @@ +package api + +import ( + "SciencesServer/app/api/website/controller" + "SciencesServer/app/basic/api" + "github.com/gin-gonic/gin" +) + +type Service struct{} + +func (*Service) SolutionCase(c *gin.Context) { + data, err := controller.NewService()(nil, "").SolutionCase() + api.APIResponse(err, data)(c) +} + +func (*Service) SolutionCaseList(c *gin.Context) { + form := &struct { + api.IDStringForm + api.PageForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewService()(nil, "").SolutionCaseList(form.Convert(), form.Page, form.PageSize) + api.APIResponse(err, data)(c) +} + +func (*Service) SolutionCaseDetail(c *gin.Context) { + form := new(api.IDStringForm) + + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewService()(nil, "").SolutionCaseDetail(form.Convert()) + api.APIResponse(err, data)(c) +} diff --git a/app/api/website/controller/service.go b/app/api/website/controller/service.go new file mode 100644 index 0000000..01aa5b8 --- /dev/null +++ b/app/api/website/controller/service.go @@ -0,0 +1,152 @@ +package controller + +import ( + "SciencesServer/app/api/manage/controller" + "SciencesServer/app/api/website/model" + model2 "SciencesServer/app/common/model" + "SciencesServer/app/session" + "SciencesServer/config" + "errors" + "gorm.io/gorm" + "time" +) + +type Service struct { + *session.Enterprise + local string +} + +type ServiceHandle func(session *session.Enterprise, local string) *Service + +type ( + // ServiceSolutionCase 服务解决方案案例信息 + ServiceSolutionCase struct { + ID string `json:"id"` + MarkID uint64 `json:"-"` + Kind model2.ServiceSolutionCaseKind `json:"kind"` + Title string `json:"title"` + Image string `json:"image"` + Children []*ServiceSolutionCaseInfo `json:"children"` + } + // ServiceSolutionCaseInfo 基本信息 + ServiceSolutionCaseInfo struct { + ID string `json:"id"` + Title string `json:"title"` + Image string `json:"image"` + Description string `json:"description"` + } + // ServiceSolutionCaseDetail 详细信息 + ServiceSolutionCaseDetail struct { + ServiceSolutionCaseInfo + Visits int `json:"visits"` + Content string `json:"content"` + CreatedAt time.Time `json:"created_at"` + } +) + +// SolutionCase 服务解决方案案例 +func (c *Service) SolutionCase() ([]*ServiceSolutionCase, error) { + mServiceSolutionCase := model.NewServiceSolutionCase() + out, err := mServiceSolutionCase.SolutionCase(2) + + if err != nil { + return nil, err + } + ret := make([]*ServiceSolutionCase, 0) + + for _, v := range out { + isExist := false + + detail := &ServiceSolutionCaseInfo{ + ID: (&model2.Model{ID: v.DetailID}).GetEncodeID(), Title: v.DetailTitle, + Image: (&model2.Image{Image: v.DetailImage}).Analysis(config.SettingInfo.Domain), + Description: v.DetailDescription, + } + for _, val := range ret { + if v.ID == val.MarkID { + val.Children = append(val.Children, detail) + isExist = true + break + } + } + if !isExist { + ret = append(ret, &ServiceSolutionCase{ + ID: v.GetEncodeID(), + MarkID: v.ID, + Kind: v.Kind, + Title: v.Title, + Image: v.Image.Analysis(config.SettingInfo.Domain), + Children: []*ServiceSolutionCaseInfo{detail}, + }) + } + } + return ret, nil +} + +// SolutionCaseList 列表信息 +func (c *Service) SolutionCaseList(id uint64, page, pageSize int) (*controller.ReturnPages, error) { + mServiceSolutionCaseDetail := model.NewServiceSolutionCaseDetail() + + out := make([]*model2.ServiceSolutionCaseDetail, 0) + + var count int64 + + err := model2.PagesFields(mServiceSolutionCaseDetail.ServiceSolutionCaseDetail, &out, []string{"id", "title", + "image", "description"}, page, pageSize, &count, + &model2.ModelWhereOrder{ + Where: model2.NewWhere("solution_case_id", id), + Order: model2.NewOrder("sort", model2.OrderModeToDesc), + }, &model2.ModelWhereOrder{ + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }) + if err != nil { + return nil, err + } + list := make([]*ServiceSolutionCaseInfo, 0) + + for _, v := range out { + list = append(list, &ServiceSolutionCaseInfo{ + ID: v.GetEncodeID(), + Title: v.Title, + Image: v.Image.Analysis(config.SettingInfo.Domain), + Description: v.Description, + }) + } + return &controller.ReturnPages{Data: list, Count: count}, nil +} + +// SolutionCaseDetail 详细信息 +func (c *Service) SolutionCaseDetail(id uint64) (*ServiceSolutionCaseDetail, error) { + mServiceSolutionCaseDetail := model.NewServiceSolutionCaseDetail() + mServiceSolutionCaseDetail.ID = id + + if isExist, err := model2.FirstField(mServiceSolutionCaseDetail.ServiceSolutionCaseDetail, []string{"id", "title", + "image", "description", "content", "visits", "created_at"}); err != nil { + return nil, err + } else if !isExist { + return nil, errors.New("操作错误,服务解决案例信息不存在或已被删除") + } + _ = model2.Updates(mServiceSolutionCaseDetail.ServiceSolutionCaseDetail, map[string]interface{}{ + "visits": gorm.Expr("visits + ?", 1), "updated_at": time.Now(), + }) + return &ServiceSolutionCaseDetail{ + ServiceSolutionCaseInfo: ServiceSolutionCaseInfo{ + ID: mServiceSolutionCaseDetail.GetEncodeID(), + Title: mServiceSolutionCaseDetail.Title, + Image: mServiceSolutionCaseDetail.Image.Analysis(config.SettingInfo.Domain), + Description: mServiceSolutionCaseDetail.Description, + }, + Visits: mServiceSolutionCaseDetail.Visits, + Content: mServiceSolutionCaseDetail.Content, + CreatedAt: mServiceSolutionCaseDetail.CreatedAt, + }, nil +} + +func NewService() ServiceHandle { + return func(session *session.Enterprise, local string) *Service { + return &Service{ + Enterprise: session, + local: local, + } + } +} diff --git a/app/api/website/model/service_solution_case.go b/app/api/website/model/service_solution_case.go new file mode 100644 index 0000000..4e0cffc --- /dev/null +++ b/app/api/website/model/service_solution_case.go @@ -0,0 +1,48 @@ +package model + +import ( + "SciencesServer/app/common/model" + "SciencesServer/serve/orm" + "fmt" +) + +type ServiceSolutionCase struct { + *model.ServiceSolutionCase +} + +// ServiceSolutionCaseInfo 解决方案案例信息 +type ServiceSolutionCaseInfo struct { + model.Model + Kind model.ServiceSolutionCaseKind `json:"kind"` + Title string `json:"title"` + model.Image + DetailID uint64 `json:"detail_id"` + DetailTitle string `json:"detail_title"` + DetailImage string `json:"detail_image"` + DetailDescription string `json:"detail_description"` +} + +// SolutionCase 解决方案案例信息 +func (m *ServiceSolutionCase) SolutionCase(limit int) ([]*ServiceSolutionCaseInfo, error) { + mServiceSolutionCaseDetail := model.NewServiceSolutionCaseDetail() + + db := orm.GetDB().Table(m.TableName()+" AS s"). + Select("s.id", "s.kind", "s.image", + "d.id AS detail_id", "d.title AS detail_title", "d.image AS detail_image", "d.description AS detail_description"). + Joins(fmt.Sprintf(`INNER JOIN (SELECT id, solution_case_id, title, image, description FROM %s AS a WHERE +(SELECT count( b.id ) FROM %s AS b WHERE a.solution_case_id = b.solution_case_id AND a.id < b.id AND b.is_deleted = %d) < %d +AND a.is_deleted = %d ORDER BY a.sort DESC, a.id DESC) AS d ON s.id = d.solution_case_id`, + mServiceSolutionCaseDetail.TableName(), mServiceSolutionCaseDetail.TableName(), model.DeleteStatusForNot, limit, model.DeleteStatusForNot)). + Where("s.is_deleted = ?", model.DeleteStatusForNot). + Order("s.sort " + model.OrderModeToDesc) + + out := make([]*ServiceSolutionCaseInfo, 0) + + err := db.Scan(&out).Error + + return out, err +} + +func NewServiceSolutionCase() *ServiceSolutionCase { + return &ServiceSolutionCase{model.NewServiceSolutionCase()} +} diff --git a/app/api/website/model/service_solution_case_detail.go b/app/api/website/model/service_solution_case_detail.go new file mode 100644 index 0000000..3675ef3 --- /dev/null +++ b/app/api/website/model/service_solution_case_detail.go @@ -0,0 +1,11 @@ +package model + +import "SciencesServer/app/common/model" + +type ServiceSolutionCaseDetail struct { + *model.ServiceSolutionCaseDetail +} + +func NewServiceSolutionCaseDetail() *ServiceSolutionCaseDetail { + return &ServiceSolutionCaseDetail{model.NewServiceSolutionCaseDetail()} +} diff --git a/app/api/website/model/sys_banner.go b/app/api/website/model/sys_banner.go new file mode 100644 index 0000000..24d2560 --- /dev/null +++ b/app/api/website/model/sys_banner.go @@ -0,0 +1,11 @@ +package model + +import "SciencesServer/app/common/model" + +type SysBanner struct { + *model.SysBanner +} + +func NewSysBanner() *SysBanner { + return &SysBanner{model.NewSysBanner()} +} diff --git a/app/common/init.go b/app/common/init.go index 19b3279..2b09ca7 100644 --- a/app/common/init.go +++ b/app/common/init.go @@ -130,7 +130,8 @@ func initModel() { &synchronized{iModel: model.NewTechnologyAchievement()}, &synchronized{iModel: model.NewTechnologyDemand()}, &synchronized{iModel: model.NewTechnologyPaper()}, &synchronized{iModel: model.NewTechnologyProduct()}, &synchronized{iModel: model.NewTechnologyProject()}, &synchronized{iModel: model.NewTechnologyTopic()}, - &synchronized{iModel: model.NewServiceDocking()}, + &synchronized{iModel: model.NewServiceDocking()}, &synchronized{iModel: model.NewServiceMessage()}, + &synchronized{iModel: model.NewServiceSolutionCase()}, &synchronized{iModel: model.NewServiceSolutionCaseDetail()}, // 活动管理 &synchronized{iModel: model.NewActivityInstance()}, &synchronized{iModel: model.NewActivityApply()}, &synchronized{iModel: model.NewActivityExamine()}, &synchronized{iModel: model.NewActivityJoin()}, diff --git a/app/common/model/service_solution_case.go b/app/common/model/service_solution_case.go new file mode 100644 index 0000000..b9a2065 --- /dev/null +++ b/app/common/model/service_solution_case.go @@ -0,0 +1,34 @@ +package model + +// ServiceSolutionCase 服务解决案例数据模型 +type ServiceSolutionCase struct { + Model + Kind ServiceSolutionCaseKind `gorm:"column:kind;type:tinyint(3);default:0;comment:类型" json:"kind"` + Title string `gorm:"column:title;type:varchar(20);default:'';comment:标题" json:"title"` + Image + Sort int `gorm:"column:sort;type:tinyint(3);default:0;comment:排序,数值越大,优先排序" json:"sort"` + ModelDeleted + ModelAt +} + +// ServiceSolutionCaseKind 服务解决方案类型 +type ServiceSolutionCaseKind int + +const ( + // ServiceSolutionCaseKindForSmallCompany 中小型企业 + ServiceSolutionCaseKindForSmallCompany ServiceSolutionCaseKind = iota + 101 + // ServiceSolutionCaseKindForBigCompany 大型企业 + ServiceSolutionCaseKindForBigCompany + // ServiceSolutionCaseKindForGovernment 政府单位 + ServiceSolutionCaseKindForGovernment + // ServiceSolutionCaseKindForResearch 科研机构 + ServiceSolutionCaseKindForResearch +) + +func (m *ServiceSolutionCase) TableName() string { + return "service_solution_case" +} + +func NewServiceSolutionCase() *ServiceSolutionCase { + return &ServiceSolutionCase{} +} diff --git a/app/common/model/service_solution_case_detail.go b/app/common/model/service_solution_case_detail.go new file mode 100644 index 0000000..0d6e445 --- /dev/null +++ b/app/common/model/service_solution_case_detail.go @@ -0,0 +1,23 @@ +package model + +// ServiceSolutionCaseDetail 服务解决案例数据模型 +type ServiceSolutionCaseDetail struct { + Model + SolutionCaseID uint64 `json:"solution_case_id"` + Title string `gorm:"column:title;type:varchar(100);default:'';comment:标题" json:"title"` + Image + Description string `gorm:"column:description;type:varchar(255);comment:描述" json:"description"` + Content string `gorm:"column:content;type:text;comment:内容" json:"content"` + Visits int `gorm:"column:visits;type:int(6);default:0;comment:浏览数" json:"visits"` + Sort int `gorm:"column:sort;type:tinyint(3);default:0;comment:排序,数值越大,优先排序" json:"sort"` + ModelDeleted + ModelAt +} + +func (m *ServiceSolutionCaseDetail) TableName() string { + return "service_solution_case_detail" +} + +func NewServiceSolutionCaseDetail() *ServiceSolutionCaseDetail { + return &ServiceSolutionCaseDetail{} +} diff --git a/app/common/model/sys_banner.go b/app/common/model/sys_banner.go new file mode 100644 index 0000000..89fa771 --- /dev/null +++ b/app/common/model/sys_banner.go @@ -0,0 +1,15 @@ +package model + +type SysBanner struct { + Model + ModelDeleted + ModelAt +} + +func (m *SysBanner) TableName() string { + return "sys_banner" +} + +func NewSysBanner() *SysBanner { + return &SysBanner{} +} diff --git a/router/address.go b/router/address.go index 75a9976..b3dfd49 100644 --- a/router/address.go +++ b/router/address.go @@ -48,6 +48,14 @@ func registerAPI(app *gin.Engine) { _api := new(api2.Message) messageV1.POST("/launch", _api.Launch) } + // ServiceV1 服务信息管理 + serviceV1 := v1.Group("/service") + { + _api := new(api2.Service) + serviceV1.GET("/solution_case", _api.SolutionCase) + serviceV1.POST("/solution_case/list", _api.SolutionCaseList) + serviceV1.POST("/solution_case/detail", _api.SolutionCaseDetail) + } } // registerAdminAPI 注册API diff --git a/utils/encrypt.go b/utils/encrypt.go index b9eb776..5077a4e 100644 --- a/utils/encrypt.go +++ b/utils/encrypt.go @@ -70,6 +70,10 @@ func HASHIDDecode(src string) int { hd := hashids.NewData() hd.Salt = salt h := hashids.NewWithData(hd) - e, _ := h.DecodeWithError(src) + e, err := h.DecodeWithError(src) + + if err != nil { + return 0 + } return e[0] }