package data import ( consts "cid/consts/data" dao "cid/dao/data" dto "cid/model/dto/data" entity "cid/model/entity/data" "context" "errors" "github.com/gogf/gf/v2/util/gconv" ) type platformService struct{} // Platform 平台服务 var Platform = new(platformService) // Create 创建平台 func (s *platformService) Create(ctx context.Context, req *dto.CreatePlatformReq) (res *dto.CreatePlatformRes, err error) { // 检查平台名称是否重复 count, err := dao.Platform.Count(ctx, &dto.ListPlatformReq{Name: req.Name}) if err != nil { return } if count > 0 { return nil, errors.New("平台名称已存在") } // 检查平台类型是否重复 count, err = dao.Platform.Count(ctx, &dto.ListPlatformReq{Type: req.Type}) if err != nil { return } if count > 0 { return nil, errors.New("该类型平台已存在") } // 插入数据库 id, err := dao.Platform.Insert(ctx, req) if err != nil { return } res = &dto.CreatePlatformRes{ Id: id, } return } // List 获取平台列表 func (s *platformService) List(ctx context.Context, req *dto.ListPlatformReq) (res *dto.ListPlatformRes, err error) { platformList, total, err := dao.Platform.List(ctx, req) if err != nil { return } // 组装响应数据 list := make([]dto.PlatformItem, 0, len(platformList)) for _, item := range platformList { list = append(list, dto.PlatformItem{ Id: item.Id, Name: item.Name, 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.ListPlatformRes{ List: list, Total: total, } return } // GetOne 获取单个平台 func (s *platformService) GetOne(ctx context.Context, req *dto.GetPlatformReq) (res *dto.GetPlatformRes, err error) { platform, err := dao.Platform.GetOne(ctx, req) if err != nil { return } var platformEntity *entity.Platform if err = gconv.Struct(platform, &platformEntity); err != nil { return } return &dto.GetPlatformRes{ Platform: platformEntity, }, nil } // Update 更新平台 func (s *platformService) Update(ctx context.Context, req *dto.UpdatePlatformReq) (err error) { // 检查平台是否存在 exist, err := dao.Platform.GetOne(ctx, &dto.GetPlatformReq{Id: req.Id}) if err != nil || exist == nil { return errors.New("平台不存在") } // 如果修改了名称,检查新名称是否重复 if req.Name != "" && req.Name != exist.Name { count, err := dao.Platform.Count(ctx, &dto.ListPlatformReq{Name: req.Name}) if err != nil { return err } if count > 0 { return errors.New("平台名称已存在") } } // 如果修改了类型,检查新类型是否重复 if req.Type != "" && req.Type != exist.Type { count, err := dao.Platform.Count(ctx, &dto.ListPlatformReq{Type: req.Type}) if err != nil { return err } if count > 0 { return errors.New("该类型平台已存在") } } _, err = dao.Platform.Update(ctx, req) return } // UpdateStatus 更新平台状态 func (s *platformService) UpdateStatus(ctx context.Context, req *dto.UpdatePlatformStatusReq) (err error) { _, err = dao.Platform.UpdateStatus(ctx, req.Id, req.Status.String()) return } // Delete 删除平台 func (s *platformService) Delete(ctx context.Context, req *dto.DeletePlatformReq) (err error) { // 检查是否存在关联的接口 interfaces, _, err := dao.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{ PlatformId: req.Id, }) if err != nil { return err } if len(interfaces) > 0 { return errors.New("该平台下存在接口,无法删除") } _, err = dao.Platform.Delete(ctx, req) return } // GetByType 根据类型获取平台 func (s *platformService) GetByType(ctx context.Context, platformType consts.SyncPlatform) (res *entity.Platform, err error) { return dao.Platform.GetByType(ctx, platformType.String()) } // getTypeName 获取类型名称 func (s *platformService) getTypeName(platformType consts.SyncPlatform) string { typeNames := map[consts.SyncPlatform]string{ consts.PlatformTaobao: "淘宝", consts.PlatformJD: "京东", consts.PlatformKuaishou: "快手", consts.PlatformDouyin: "抖音", consts.PlatformXhs: "小红书", consts.PlatformPdd: "拼多多", consts.PlatformXianyu: "闲鱼", consts.PlatformTmall: "天猫", consts.PlatformWechat: "微信", consts.PlatformCustom: "自定义", } if name, ok := typeNames[platformType]; ok { return name } return string(platformType) } // getStatusName 获取状态名称 func (s *platformService) getStatusName(status consts.PlatformStatus) string { statusNames := map[consts.PlatformStatus]string{ consts.PlatformStatusActive: "启用", consts.PlatformStatusInactive: "停用", } if name, ok := statusNames[status]; ok { return name } return string(status) }