153 lines
4.6 KiB
Go
153 lines
4.6 KiB
Go
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,
|
|
}
|
|
}
|
|
}
|