LandingPageProjectService.java 20.9 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
package com.aigeo.landingpage.service;

import com.aigeo.common.context.TenantContext;
import com.aigeo.common.exception.BusinessException;
import com.aigeo.common.result.ResultCode;
import com.aigeo.landingpage.entity.LandingPageProject;
import com.aigeo.landingpage.entity.LandingPageProjectStatus;
import com.aigeo.landingpage.repository.LandingPageProjectRepository;
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 LandingPageProjectService {
    
    private final LandingPageProjectRepository landingPageProjectRepository;

    /**
     * 获取所有落地页项目
     * @return 所有项目列表
     */
    public List<LandingPageProject> getAllProjects() {
        log.debug("获取所有落地页项目");
        return landingPageProjectRepository.findAll();
    }

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

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

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

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

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

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

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

    /**
     * 根据项目名称模糊搜索
     * @param companyId 公司ID
     * @param name 项目名称关键词
     * @return 匹配的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<LandingPageProject> 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 landingPageProjectRepository.findByCompanyIdAndNameContainingIgnoreCase(companyId, name);
    }

    /**
     * 根据域名搜索项目
     * @param companyId 公司ID
     * @param domain 域名关键词
     * @return 匹配的项目列表
     * @throws BusinessException 当参数为空时抛出
     */
    public List<LandingPageProject> 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 landingPageProjectRepository.findByCompanyIdAndDomainContainingIgnoreCase(companyId, domain);
    }

    /**
     * 保存落地页项目
     * @param project 项目对象
     * @return 保存后的项目
     * @throws BusinessException 当项目对象为空或保存失败时抛出
     */
    @Transactional
    public LandingPageProject saveProject(LandingPageProject 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 = landingPageProjectRepository.existsByCompanyIdAndName(
                project.getCompanyId(), project.getName());
            if (nameExists) {
                throw new BusinessException(ResultCode.DATA_ALREADY_EXISTS, "项目名称已存在");
            }
        }

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

        try {
            LandingPageProject savedProject = landingPageProjectRepository.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<LandingPageProject> batchSaveProjects(List<LandingPageProject> projects) {
        log.debug("批量保存落地页项目,数量: {}", projects != null ? projects.size() : 0);
        if (projects == null || projects.isEmpty()) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目列表不能为空");
        }

        Integer companyId = TenantContext.getCurrentTenantId();
        for (LandingPageProject 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(LandingPageProjectStatus.DRAFT);
            }
        }

        try {
            List<LandingPageProject> savedProjects = landingPageProjectRepository.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 LandingPageProject updateProjectStatus(Integer id, LandingPageProjectStatus status) {
        log.debug("更新项目状态,id: {}, status: {}", id, status);
        if (id == null || status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "项目ID和状态不能为空");
        }

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

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

        try {
            LandingPageProject updatedProject = landingPageProjectRepository.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 LandingPageProject publishProject(Integer id) {
        log.debug("发布项目,id: {}", id);
        return updateProjectStatus(id, LandingPageProjectStatus.PUBLISHED);
    }

    /**
     * 下线项目
     * @param id 项目ID
     * @return 更新后的项目
     * @throws BusinessException 当项目不存在时抛出
     */
    @Transactional
    public LandingPageProject unpublishProject(Integer id) {
        log.debug("下线项目,id: {}", id);
        return updateProjectStatus(id, LandingPageProjectStatus.DRAFT);
    }

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

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

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

        project.setViewCount(viewCount);
        project.setConversionCount(conversionCount);
        project.setUpdatedAt(LocalDateTime.now());

        try {
            LandingPageProject updatedProject = landingPageProjectRepository.save(project);
            log.info("项目统计更新成功,id: {}, viewCount: {}, conversionCount: {}", 
                    id, viewCount, conversionCount);
            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 (!landingPageProjectRepository.existsById(id)) {
            throw new BusinessException(ResultCode.DATA_NOT_FOUND, "项目不存在");
        }

        try {
            landingPageProjectRepository.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 {
            landingPageProjectRepository.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 landingPageProjectRepository.countByCompanyId(companyId);
    }

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

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

    /**
     * 查找草稿项目
     * @param companyId 公司ID
     * @return 草稿项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<LandingPageProject> getDraftProjects(Integer companyId) {
        log.debug("查找草稿项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return landingPageProjectRepository.findByCompanyIdAndStatus(companyId, LandingPageProjectStatus.DRAFT);
    }

    /**
     * 查找已发布项目
     * @param companyId 公司ID
     * @return 已发布项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<LandingPageProject> getPublishedProjects(Integer companyId) {
        log.debug("查找已发布项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return landingPageProjectRepository.findByCompanyIdAndStatus(companyId, LandingPageProjectStatus.PUBLISHED);
    }

    /**
     * 查找已归档项目
     * @param companyId 公司ID
     * @return 已归档项目列表
     * @throws BusinessException 当公司ID为空时抛出
     */
    public List<LandingPageProject> getArchivedProjects(Integer companyId) {
        log.debug("查找已归档项目,companyId: {}", companyId);
        if (companyId == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "公司ID不能为空");
        }
        return landingPageProjectRepository.findByCompanyIdAndStatus(companyId, LandingPageProjectStatus.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 landingPageProjectRepository.existsByCompanyIdAndName(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 landingPageProjectRepository.existsByCompanyIdAndDomain(companyId, domain);
    }
}