Dockerfile
This commit is contained in:
178
service/app/application_service.go
Normal file
178
service/app/application_service.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
consts "cid/consts/app"
|
||||
dao "cid/dao/app"
|
||||
dto "cid/model/dto/app"
|
||||
entity "cid/model/entity/app"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type applicationService struct{}
|
||||
|
||||
// Application 应用服务
|
||||
var Application = new(applicationService)
|
||||
|
||||
// Create 创建应用
|
||||
func (s *applicationService) Create(ctx context.Context, req *dto.CreateApplicationReq) (res *dto.CreateApplicationRes, err error) {
|
||||
// 检查应用名称是否重复
|
||||
count, err := dao.Application.Count(ctx, &dto.ListApplicationReq{Name: req.Name})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
return nil, errors.New("应用名称已存在")
|
||||
}
|
||||
|
||||
// 检查应用编码是否重复
|
||||
count, err = dao.Application.Count(ctx, &dto.ListApplicationReq{AppCode: req.AppCode})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
return nil, errors.New("应用编码已存在")
|
||||
}
|
||||
|
||||
// 插入数据库
|
||||
id, err := dao.Application.Insert(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = &dto.CreateApplicationRes{
|
||||
Id: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取应用列表
|
||||
func (s *applicationService) List(ctx context.Context, req *dto.ListApplicationReq) (res *dto.ListApplicationRes, err error) {
|
||||
applicationList, total, err := dao.Application.List(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装响应数据
|
||||
list := make([]dto.ApplicationItem, 0, len(applicationList))
|
||||
for _, item := range applicationList {
|
||||
list = append(list, dto.ApplicationItem{
|
||||
Id: item.Id,
|
||||
Name: item.Name,
|
||||
AppCode: item.AppCode,
|
||||
Type: item.Type,
|
||||
TypeName: s.getTypeName(item.Type),
|
||||
Status: item.Status,
|
||||
StatusName: s.getStatusName(item.Status),
|
||||
Description: item.Description,
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
UpdatedAt: item.UpdatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
res = &dto.ListApplicationRes{
|
||||
List: list,
|
||||
Total: total,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个应用
|
||||
func (s *applicationService) GetOne(ctx context.Context, req *dto.GetApplicationReq) (res *dto.GetApplicationRes, err error) {
|
||||
application, err := dao.Application.GetOne(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var applicationEntity *entity.Application
|
||||
if err = gconv.Struct(application, &applicationEntity); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return &dto.GetApplicationRes{
|
||||
Application: applicationEntity,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新应用
|
||||
func (s *applicationService) Update(ctx context.Context, req *dto.UpdateApplicationReq) (err error) {
|
||||
// 检查应用是否存在
|
||||
exist, err := dao.Application.GetOne(ctx, &dto.GetApplicationReq{Id: req.Id})
|
||||
if err != nil || exist == nil {
|
||||
return errors.New("应用不存在")
|
||||
}
|
||||
|
||||
// 如果修改了名称,检查新名称是否重复
|
||||
if req.Name != "" && req.Name != exist.Name {
|
||||
count, err := dao.Application.Count(ctx, &dto.ListApplicationReq{Name: req.Name})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("应用名称已存在")
|
||||
}
|
||||
}
|
||||
|
||||
// 如果修改了应用编码,检查新编码是否重复
|
||||
if req.AppCode != "" && req.AppCode != exist.AppCode {
|
||||
count, err := dao.Application.Count(ctx, &dto.ListApplicationReq{AppCode: req.AppCode})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("应用编码已存在")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dao.Application.Update(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新应用状态
|
||||
func (s *applicationService) UpdateStatus(ctx context.Context, req *dto.UpdateApplicationStatusReq) (err error) {
|
||||
_, err = dao.Application.UpdateStatus(ctx, req.Id, req.Status.String())
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除应用
|
||||
func (s *applicationService) Delete(ctx context.Context, req *dto.DeleteApplicationReq) (err error) {
|
||||
// TODO: 检查是否存在关联的数据,防止误删
|
||||
// 例如: 检查该应用是否有关联的广告活动等
|
||||
|
||||
_, err = dao.Application.Delete(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByAppCode 根据应用编码获取应用
|
||||
func (s *applicationService) GetByAppCode(ctx context.Context, appCode string) (res *entity.Application, err error) {
|
||||
return dao.Application.GetByAppCode(ctx, appCode)
|
||||
}
|
||||
|
||||
// getTypeName 获取类型名称
|
||||
func (s *applicationService) getTypeName(appType consts.AppType) string {
|
||||
typeNames := map[consts.AppType]string{
|
||||
consts.AppTypeWeb: "Web应用",
|
||||
consts.AppTypeMobile: "移动应用",
|
||||
consts.AppTypeMiniApp: "小程序",
|
||||
consts.AppTypeH5: "H5应用",
|
||||
consts.AppTypeDesktop: "桌面应用",
|
||||
consts.AppTypeThirdParty: "第三方应用",
|
||||
}
|
||||
if name, ok := typeNames[appType]; ok {
|
||||
return name
|
||||
}
|
||||
return string(appType)
|
||||
}
|
||||
|
||||
// getStatusName 获取状态名称
|
||||
func (s *applicationService) getStatusName(status consts.AppStatus) string {
|
||||
statusNames := map[consts.AppStatus]string{
|
||||
consts.AppStatusActive: "启用",
|
||||
consts.AppStatusInactive: "停用",
|
||||
}
|
||||
if name, ok := statusNames[status]; ok {
|
||||
return name
|
||||
}
|
||||
return string(status)
|
||||
}
|
||||
Reference in New Issue
Block a user