WebsiteProjectService.java 26.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
package com.aigeo.website.service;

import com.aigeo.common.context.TenantContext;
import com.aigeo.common.exception.BusinessException;
import com.aigeo.common.result.ResultCode;
import com.aigeo.website.entity.WebsiteProject;
import com.aigeo.website.entity.WebsiteProjectStatus;
import com.aigeo.website.repository.WebsiteProjectRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 网站项目服务层
 * 
 * 负责管理企业网站项目的业务逻辑,包括网站创建、模板管理、
 * 内容管理、SEO优化、域名配置、部署发布等功能。
 * 支持多种网站类型、响应式设计、内容管理系统等
 *
 * @author AIGEO Team
 * @since 1.0.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class WebsiteProjectService {

    private final WebsiteProjectRepository websiteProjectRepository;

    /**
     * 获取所有网站项目
     * @return 所有项目列表
     */
    public List<WebsiteProject> getAllProjects() {
        log.debug("获取所有网站项目");
        return websiteProjectRepository.findAll();
    }

    /**
     * 根据公司ID获取网站项目列表
     * @param companyId 公司ID
     * @return 该公司的项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<WebsiteProject> getProjectsByCompanyId(Integer companyId) {
        log.debug("根据公司ID获取网站项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.findByCompanyId(companyId);
    }

    /**
     * 根据用户ID获取网站项目列表
     * @param userId 用户ID
     * @return 该用户创建的项目列表
     * @throws BusinessException 当用户ID为空时抛出
     */
    public List<WebsiteProject> getProjectsByUserId(Integer userId) {
        log.debug("根据用户ID获取网站项目,userId: {}", userId);
        if (userId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "用户ID不能为空");
        }
        return websiteProjectRepository.findByUserId(userId);
    }

    /**
     * 根据项目状态获取网站项目列表
     * @param status 项目状态
     * @return 指定状态的项目列表
     * @throws BusinessException 当状态为空时抛出
     */
    public List<WebsiteProject> getProjectsByStatus(WebsiteProjectStatus status) {
        log.debug("根据状态获取网站项目,status: {}", status);
        if (status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目状态不能为空");
        }
        return websiteProjectRepository.findByStatus(status);
    }

    /**
     * 根据公司ID和状态获取项目列表
     * @param companyId 公司ID
     * @param status 项目状态
     * @return 匹配条件的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<WebsiteProject> getProjectsByCompanyIdAndStatus(Integer companyId, WebsiteProjectStatus status) {
        log.debug("根据公司和状态获取项目,companyId: {}, status: {}", companyId, status);
        if (companyId == null || status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和状态不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndStatus(companyId, status);
    }

    /**
     * 分页查询公司的网站项目
     * @param companyId 公司ID
     * @param pageable 分页参数
     * @return 分页结果
     * @throws BusinessException 当公司ID为空时抛出
     */
    public Page<WebsiteProject> getProjectsByCompanyIdWithPaging(Integer companyId, Pageable pageable) {
        log.debug("分页查询公司网站项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.findByCompanyIdOrderByCreatedAtDesc(companyId, pageable);
    }

    /**
     * 分页查询公司指定状态的项目
     * @param companyId 公司ID
     * @param status 项目状态
     * @param pageable 分页参数
     * @return 分页结果
     * @throws BusinessException 当参数为空时抛出
     */
    public Page<WebsiteProject> getProjectsByCompanyIdAndStatusWithPaging(Integer companyId, 
                                                                          WebsiteProjectStatus status, 
                                                                          Pageable pageable) {
        log.debug("分页查询公司指定状态项目,companyId: {}, status: {}", companyId, status);
        if (companyId == null || status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和状态不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndStatusOrderByCreatedAtDesc(companyId, status, pageable);
    }

    /**
     * 根据ID获取网站项目
     * @param id 项目ID
     * @return 项目对象的Optional包装
     * @throws BusinessException 当ID为空时抛出
     */
    public Optional<WebsiteProject> getProjectById(Integer id) {
        log.debug("根据ID获取网站项目,id: {}", id);
        if (id == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID不能为空");
        }
        return websiteProjectRepository.findById(id);
    }

    /**
     * 根据项目名称模糊搜索
     * @param companyId 公司ID
     * @param name 项目名称关键词
     * @return 匹配的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<WebsiteProject> searchProjectsByName(Integer companyId, String name) {
        log.debug("根据名称搜索项目,companyId: {}, name: {}", companyId, name);
        if (companyId == null || !StringUtils.hasText(name)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和项目名称不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndProjectNameContainingIgnoreCase(companyId, name);
    }

    /**
     * 根据域名搜索项目
     * @param companyId 公司ID
     * @param domain 域名关键词
     * @return 匹配的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<WebsiteProject> searchProjectsByDomain(Integer companyId, String domain) {
        log.debug("根据域名搜索项目,companyId: {}, domain: {}", companyId, domain);
        if (companyId == null || !StringUtils.hasText(domain)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和域名不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndDomainContainingIgnoreCase(companyId, domain);
    }

    /**
     * 根据网站类型搜索项目
     * @param companyId 公司ID
     * @param type 网站类型
     * @return 匹配的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<WebsiteProject> searchProjectsByType(Integer companyId, String type) {
        log.debug("根据类型搜索项目,companyId: {}, type: {}", companyId, type);
        if (companyId == null || !StringUtils.hasText(type)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和网站类型不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndTypeContainingIgnoreCase(companyId, type);
    }

    /**
     * 保存网站项目
     * @param project 项目对象
     * @return 保存后的项目
     * @throws BusinessException 当项目对象为空或保存失败时抛出
     */
    @Transactional
    public WebsiteProject saveProject(WebsiteProject project) {
        log.debug("保存网站项目,name: {}", project != null ? project.getName() : "null");
        if (project == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目对象不能为空");
        }

        // 设置当前租户ID
        if (project.getCompanyId() == null) {
            project.setCompanyId(TenantContext.getCurrentTenantId());
        }

        // 验证必要字段
        if (!StringUtils.hasText(project.getName())) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目名称不能为空");
        }

        // 检查项目名称是否已存在
        if (project.getId() == null) {
            boolean nameExists = websiteProjectRepository.existsByCompanyIdAndProjectName(
                project.getCompanyId(), project.getName());
            if (nameExists) {
                throw new BusinessException(ResultCode.DATA_ALREADY_EXISTS, "项目名称已存在");
            }
        }

        // 检查域名是否已被使用(如果提供了域名)
        if (StringUtils.hasText(project.getDomain()) && project.getId() == null) {
            boolean domainExists = websiteProjectRepository.existsByCompanyIdAndDomain(
                project.getCompanyId(), project.getDomain());
            if (domainExists) {
                throw new BusinessException(ResultCode.DATA_ALREADY_EXISTS, "域名已被使用");
            }
        }

        // 新建项目时设置默认状态
        if (project.getId() == null && project.getStatus() == null) {
            project.setStatus(WebsiteProjectStatus.DEVELOPMENT);
        }

        try {
            WebsiteProject savedProject = websiteProjectRepository.save(project);
            log.info("网站项目保存成功,id: {}, name: {}", savedProject.getId(), savedProject.getName());
            return savedProject;
        } catch (Exception e) {
            log.error("网站项目保存失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "项目保存失败");
        }
    }

    /**
     * 批量保存网站项目
     * @param projects 项目列表
     * @return 保存后的项目列表
     * @throws BusinessException 当项目列表为空或保存失败时抛出
     */
    @Transactional
    public List<WebsiteProject> batchSaveProjects(List<WebsiteProject> projects) {
        log.debug("批量保存网站项目,数量: {}", projects != null ? projects.size() : 0);
        if (projects == null || projects.isEmpty()) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目列表不能为空");
        }

        Integer companyId = TenantContext.getCurrentTenantId();
        for (WebsiteProject project : projects) {
            if (project.getCompanyId() == null) {
                project.setCompanyId(companyId);
            }
            if (!StringUtils.hasText(project.getName())) {
                throw new BusinessException(ResultCode.PARAM_ERROR, "项目名称不能为空");
            }
            if (project.getStatus() == null) {
                project.setStatus(WebsiteProjectStatus.DEVELOPMENT);
            }
        }

        try {
            List<WebsiteProject> savedProjects = websiteProjectRepository.saveAll(projects);
            log.info("批量保存网站项目成功,数量: {}", savedProjects.size());
            return savedProjects;
        } catch (Exception e) {
            log.error("批量保存网站项目失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "批量保存项目失败");
        }
    }

    /**
     * 更新项目状态
     * @param id 项目ID
     * @param status 新状态
     * @return 更新后的项目
     * @throws BusinessException 当参数为空或项目不存在时抛出
     */
    @Transactional
    public WebsiteProject updateProjectStatus(Integer id, WebsiteProjectStatus status) {
        log.debug("更新项目状态,id: {}, status: {}", id, status);
        if (id == null || status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID和状态不能为空");
        }

        WebsiteProject project = websiteProjectRepository.findById(id)
            .orElseThrow(() -> new BusinessException(ResultCode.DATA_NOT_FOUND, "项目不存在"));

        project.setStatus(status);
        project.setUpdatedAt(LocalDateTime.now());

        try {
            WebsiteProject updatedProject = websiteProjectRepository.save(project);
            log.info("项目状态更新成功,id: {}, status: {}", id, status);
            return updatedProject;
        } catch (Exception e) {
            log.error("项目状态更新失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "项目状态更新失败");
        }
    }

    /**
     * 开始开发项目
     * @param id 项目ID
     * @return 更新后的项目
     * @throws BusinessException 当项目不存在时抛出
     */
    @Transactional
    public WebsiteProject startDevelopment(Integer id) {
        log.debug("开始开发项目,id: {}", id);
        return updateProjectStatus(id, WebsiteProjectStatus.DEVELOPMENT);
    }

    /**
     * 项目进入测试阶段
     * @param id 项目ID
     * @return 更新后的项目
     * @throws BusinessException 当项目不存在时抛出
     */
    @Transactional
    public WebsiteProject startTesting(Integer id) {
        log.debug("项目进入测试,id: {}", id);
        return updateProjectStatus(id, WebsiteProjectStatus.TESTING);
    }

    /**
     * 发布项目
     * @param id 项目ID
     * @return 更新后的项目
     * @throws BusinessException 当项目不存在时抛出
     */
    @Transactional
    public WebsiteProject publishProject(Integer id) {
        log.debug("发布项目,id: {}", id);
        return updateProjectStatus(id, WebsiteProjectStatus.LIVE);
    }

    /**
     * 暂停项目
     * @param id 项目ID
     * @return 更新后的项目
     * @throws BusinessException 当项目不存在时抛出
     */
    @Transactional
    public WebsiteProject pauseProject(Integer id) {
        log.debug("暂停项目,id: {}", id);
        return updateProjectStatus(id, WebsiteProjectStatus.PAUSED);
    }

    /**
     * 归档项目
     * @param id 项目ID
     * @return 更新后的项目
     * @throws BusinessException 当项目不存在时抛出
     */
    @Transactional
    public WebsiteProject archiveProject(Integer id) {
        log.debug("归档项目,id: {}", id);
        return updateProjectStatus(id, WebsiteProjectStatus.ARCHIVED);
    }

    /**
     * 更新项目域名
     * @param id 项目ID
     * @param domain 新域名
     * @return 更新后的项目
     * @throws BusinessException 当参数为空、项目不存在或域名已被使用时抛出
     */
    @Transactional
    public WebsiteProject updateProjectDomain(Integer id, String domain) {
        log.debug("更新项目域名,id: {}, domain: {}", id, domain);
        if (id == null || !StringUtils.hasText(domain)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID和域名不能为空");
        }

        WebsiteProject project = websiteProjectRepository.findById(id)
            .orElseThrow(() -> new BusinessException(ResultCode.DATA_NOT_FOUND, "项目不存在"));

        // 检查域名是否已被其他项目使用
        if (!domain.equals(project.getDomain())) {
            boolean domainExists = websiteProjectRepository.existsByCompanyIdAndDomain(
                project.getCompanyId(), domain);
            if (domainExists) {
                throw new BusinessException(ResultCode.DATA_ALREADY_EXISTS, "域名已被使用");
            }
        }

        project.setDomain(domain);
        project.setUpdatedAt(LocalDateTime.now());

        try {
            WebsiteProject updatedProject = websiteProjectRepository.save(project);
            log.info("项目域名更新成功,id: {}, domain: {}", id, domain);
            return updatedProject;
        } catch (Exception e) {
            log.error("项目域名更新失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "项目域名更新失败");
        }
    }

    /**
     * 更新项目访问统计
     * @param id 项目ID
     * @param viewCount 访问量
     * @param pageViews 页面浏览量
     * @return 更新后的项目
     * @throws BusinessException 当参数无效或项目不存在时抛出
     */
    @Transactional
    public WebsiteProject updateProjectStats(Integer id, Integer viewCount, Integer pageViews) {
        log.debug("更新项目统计,id: {}, viewCount: {}, pageViews: {}", id, viewCount, pageViews);
        if (id == null || viewCount == null || pageViews == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID和统计数据不能为空");
        }
        if (viewCount < 0 || pageViews < 0) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "统计数据不能为负数");
        }

        WebsiteProject project = websiteProjectRepository.findById(id)
            .orElseThrow(() -> new BusinessException(ResultCode.DATA_NOT_FOUND, "项目不存在"));

        project.setViewCount(viewCount);
        project.setPageViews(pageViews);
        project.setUpdatedAt(LocalDateTime.now());

        try {
            WebsiteProject updatedProject = websiteProjectRepository.save(project);
            log.info("项目统计更新成功,id: {}, viewCount: {}, pageViews: {}", 
                    id, viewCount, pageViews);
            return updatedProject;
        } catch (Exception e) {
            log.error("项目统计更新失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "项目统计更新失败");
        }
    }

    /**
     * 删除网站项目
     * @param id 项目ID
     * @throws BusinessException 当ID为空或项目不存在时抛出
     */
    @Transactional
    public void deleteProject(Integer id) {
        log.debug("删除网站项目,id: {}", id);
        if (id == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID不能为空");
        }

        if (!websiteProjectRepository.existsById(id)) {
            throw new BusinessException(ResultCode.DATA_NOT_FOUND, "项目不存在");
        }

        try {
            websiteProjectRepository.deleteById(id);
            log.info("网站项目删除成功,id: {}", id);
        } catch (Exception e) {
            log.error("网站项目删除失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "项目删除失败");
        }
    }

    /**
     * 批量删除项目
     * @param ids 项目ID列表
     * @throws BusinessException 当ID列表为空时抛出
     */
    @Transactional
    public void batchDeleteProjects(List<Integer> ids) {
        log.debug("批量删除项目,数量: {}", ids != null ? ids.size() : 0);
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID列表不能为空");
        }

        try {
            websiteProjectRepository.deleteAllById(ids);
            log.info("批量删除项目成功,数量: {}", ids.size());
        } catch (Exception e) {
            log.error("批量删除项目失败", e);
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "批量删除项目失败");
        }
    }

    /**
     * 统计公司项目总数
     * @param companyId 公司ID
     * @return 项目总数
     * @throws BusinessException 当公司ID为空时抛出
     */
    public long countByCompanyId(Integer companyId) {
        log.debug("统计公司项目总数,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.countByCompanyId(companyId);
    }

    /**
     * 统计公司指定状态的项目数量
     * @param companyId 公司ID
     * @param status 项目状态
     * @return 项目数量
     * @throws BusinessException 当参数为空时抛出
     */
    public long countByCompanyIdAndStatus(Integer companyId, WebsiteProjectStatus status) {
        log.debug("统计公司指定状态项目数量,companyId: {}, status: {}", companyId, status);
        if (companyId == null || status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和状态不能为空");
        }
        return websiteProjectRepository.countByCompanyIdAndStatus(companyId, status);
    }

    /**
     * 查找最近更新的项目
     * @param companyId 公司ID
     * @param since 起始时间
     * @return 最近更新的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<WebsiteProject> getRecentlyUpdatedProjects(Integer companyId, LocalDateTime since) {
        log.debug("查找最近更新的项目,companyId: {}, since: {}", companyId, since);
        if (companyId == null || since == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和时间不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndUpdatedAtAfterOrderByUpdatedAtDesc(companyId, since);
    }

    /**
     * 查找开发中的项目
     * @param companyId 公司ID
     * @return 开发中的项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<WebsiteProject> getDevelopmentProjects(Integer companyId) {
        log.debug("查找开发中的项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndStatus(companyId, WebsiteProjectStatus.DEVELOPMENT);
    }

    /**
     * 查找测试中的项目
     * @param companyId 公司ID
     * @return 测试中的项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<WebsiteProject> getTestingProjects(Integer companyId) {
        log.debug("查找测试中的项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndStatus(companyId, WebsiteProjectStatus.TESTING);
    }

    /**
     * 查找已上线的项目
     * @param companyId 公司ID
     * @return 已上线的项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<WebsiteProject> getLiveProjects(Integer companyId) {
        log.debug("查找已上线的项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndStatus(companyId, WebsiteProjectStatus.LIVE);
    }

    /**
     * 查找已归档的项目
     * @param companyId 公司ID
     * @return 已归档的项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<WebsiteProject> getArchivedProjects(Integer companyId) {
        log.debug("查找已归档的项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return websiteProjectRepository.findByCompanyIdAndStatus(companyId, WebsiteProjectStatus.ARCHIVED);
    }

    /**
     * 检查项目名称是否存在
     * @param companyId 公司ID
     * @param name 项目名称
     * @return 是否存在
     * @throws BusinessException 当参数为空时抛出
     */
    public boolean existsByName(Integer companyId, String name) {
        log.debug("检查项目名称是否存在,companyId: {}, name: {}", companyId, name);
        if (companyId == null || !StringUtils.hasText(name)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和项目名称不能为空");
        }
        return websiteProjectRepository.existsByCompanyIdAndProjectName(companyId, name);
    }

    /**
     * 检查域名是否被使用
     * @param companyId 公司ID
     * @param domain 域名
     * @return 是否被使用
     * @throws BusinessException 当参数为空时抛出
     */
    public boolean existsByDomain(Integer companyId, String domain) {
        log.debug("检查域名是否被使用,companyId: {}, domain: {}", companyId, domain);
        if (companyId == null || !StringUtils.hasText(domain)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和域名不能为空");
        }
        return websiteProjectRepository.existsByCompanyIdAndDomain(companyId, domain);
    }

    /**
     * 根据网站类型统计项目数量
     * @param companyId 公司ID
     * @param type 网站类型
     * @return 项目数量
     * @throws BusinessException 当参数为空时抛出
     */
    public long countByCompanyIdAndType(Integer companyId, String type) {
        log.debug("根据类型统计项目数量,companyId: {}, type: {}", companyId, type);
        if (companyId == null || !StringUtils.hasText(type)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID和网站类型不能为空");
        }
        return websiteProjectRepository.countByCompanyIdAndType(companyId, type);
    }

    /**
     * 获取公司最受欢迎的项目(按访问量排序)
     * @param companyId 公司ID
     * @param limit 返回数量限制
     * @return 最受欢迎的项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<WebsiteProject> getMostPopularProjects(Integer companyId, Integer limit) {
        log.debug("获取最受欢迎的项目,companyId: {}, limit: {}", companyId, limit);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        if (limit == null || limit <= 0) {
            limit = 10; // 默认返回前10个
        }
        
        return websiteProjectRepository.findTopByCompanyIdOrderByViewCountDesc(companyId)
            .stream()
            .limit(limit)
            .toList();
    }
}