package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.ProjectMapper;
import com.ycwl.basic.model.pc.project.entity.ProjectEntity;
import com.ycwl.basic.model.pc.project.req.ProjectReqQuery;
import com.ycwl.basic.model.pc.project.resp.ProjectRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.repository.TemplateRepository;
import com.ycwl.basic.service.pc.ProjectService;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 景区项目管理服务实现
 * 
 * @Author: Claude
 * @Date: 2025-01-15
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;
    
    @Autowired
    private ScenicRepository scenicRepository;
    
    @Autowired
    private TemplateRepository templateRepository;

    @Override
    public PageInfo<ProjectRespVO> pageQuery(ProjectReqQuery projectReqQuery) {
        PageHelper.startPage(projectReqQuery.getPageNum(), projectReqQuery.getPageSize());
        List<ProjectRespVO> list = projectMapper.list(projectReqQuery);
        
        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(ProjectRespVO::getScenicId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> scenicNames = scenicRepository.batchGetScenicNames(scenicIds);
        
        // 批量获取模板名称
        List<Long> templateIds = list.stream()
                .map(ProjectRespVO::getTemplateId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> templateNames = new HashMap<>();
        for (Long templateId : templateIds) {
            TemplateRespVO template = templateRepository.getTemplate(templateId);
            if (template != null) {
                templateNames.put(templateId, template.getName());
            }
        }
        
        // 设置景区名称和模板名称
        list.forEach(item -> {
            if (item.getScenicId() != null) {
                item.setScenicName(scenicNames.get(item.getScenicId()));
            }
            if (item.getTemplateId() != null) {
                item.setTemplateName(templateNames.get(item.getTemplateId()));
            }
        });
        
        PageInfo<ProjectRespVO> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<ProjectRespVO> list(ProjectReqQuery projectReqQuery) {
        List<ProjectRespVO> list = projectMapper.list(projectReqQuery);
        
        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(ProjectRespVO::getScenicId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> scenicNames = scenicRepository.batchGetScenicNames(scenicIds);
        
        // 批量获取模板名称
        List<Long> templateIds = list.stream()
                .map(ProjectRespVO::getTemplateId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> templateNames = new HashMap<>();
        for (Long templateId : templateIds) {
            TemplateRespVO template = templateRepository.getTemplate(templateId);
            if (template != null) {
                templateNames.put(templateId, template.getName());
            }
        }
        
        // 设置景区名称和模板名称
        list.forEach(item -> {
            if (item.getScenicId() != null) {
                item.setScenicName(scenicNames.get(item.getScenicId()));
            }
            if (item.getTemplateId() != null) {
                item.setTemplateName(templateNames.get(item.getTemplateId()));
            }
        });
        
        return list;
    }

    @Override
    public ProjectRespVO getById(Long id) {
        ProjectRespVO project = projectMapper.getById(id);
        if (project != null) {
            // 设置景区名称
            if (project.getScenicId() != null) {
                Map<Long, String> scenicNames = scenicRepository.batchGetScenicNames(List.of(project.getScenicId()));
                project.setScenicName(scenicNames.get(project.getScenicId()));
            }
            // 设置模板名称
            if (project.getTemplateId() != null) {
                TemplateRespVO template = templateRepository.getTemplate(project.getTemplateId());
                if (template != null) {
                    project.setTemplateName(template.getName());
                }
            }
        }
        return project;
    }

    @Override
    public int addOrUpdate(ProjectEntity project) {
        Long id = project.getId();
        if (id == null) {
            return projectMapper.add(project);
        } else {
            return projectMapper.update(project);
        }
    }

    @Override
    public int delete(Long id) {
        return projectMapper.deleteById(id);
    }

    @Override
    public int updateStatus(Long id) {
        return projectMapper.updateStatus(id);
    }
}