package org.springblade.modules.martial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.modules.martial.pojo.entity.MartialAthlete;
import org.springblade.modules.martial.pojo.entity.MartialCompetition;
import org.springblade.modules.martial.pojo.entity.MartialProject;
import org.springblade.modules.martial.pojo.entity.MartialRegistrationOrder;
import org.springblade.modules.martial.pojo.vo.MartialRegistrationOrderVO;
import org.springblade.modules.martial.mapper.MartialRegistrationOrderMapper;
import org.springblade.modules.martial.service.IMartialAthleteService;
import org.springblade.modules.martial.service.IMartialCompetitionService;
import org.springblade.modules.martial.service.IMartialProjectService;
import org.springblade.modules.martial.service.IMartialRegistrationOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * RegistrationOrder 服务实现类
 *
 * @author BladeX
 */
@Service
@AllArgsConstructor
public class MartialRegistrationOrderServiceImpl extends ServiceImpl<MartialRegistrationOrderMapper, MartialRegistrationOrder> implements IMartialRegistrationOrderService {

	private final IMartialCompetitionService competitionService;
	private final IMartialAthleteService athleteService;
	private final IMartialProjectService projectService;

	@Override
	public MartialRegistrationOrderVO getDetailWithRelations(Long id) {
		// 获取订单基本信息
		MartialRegistrationOrder order = this.getById(id);
		if (order == null) {
			return null;
		}

		// 创建VO对象
		MartialRegistrationOrderVO vo = new MartialRegistrationOrderVO();
		BeanUtils.copyProperties(order, vo);

		// 获取赛事信息
		if (order.getCompetitionId() != null) {
			MartialCompetition competition = competitionService.getById(order.getCompetitionId());
			if (competition != null) {
				vo.setCompetitionName(competition.getCompetitionName());
				vo.setLocation(competition.getLocation());
				vo.setStartTime(competition.getCompetitionStartTime() != null ? competition.getCompetitionStartTime().toString() : null);
				vo.setEndTime(competition.getCompetitionEndTime() != null ? competition.getCompetitionEndTime().toString() : null);
			}
		}

		// 获取该订单的所有选手
		LambdaQueryWrapper<MartialAthlete> athleteWrapper = new LambdaQueryWrapper<>();
		athleteWrapper.eq(MartialAthlete::getOrderId, id);
		List<MartialAthlete> athletes = athleteService.list(athleteWrapper);

		if (!athletes.isEmpty()) {
			// 设置选手名称列表
			String athleteNames = athletes.stream()
					.map(MartialAthlete::getPlayerName)
					.collect(Collectors.joining("、"));
			vo.setAthleteNames(athleteNames);

			// 设置选手详细列表
			List<MartialRegistrationOrderVO.AthleteInfo> athleteList = athletes.stream()
					.map(athlete -> {
						MartialRegistrationOrderVO.AthleteInfo info = new MartialRegistrationOrderVO.AthleteInfo();
						info.setId(athlete.getId());
						info.setPlayerName(athlete.getPlayerName());
						info.setGender(athlete.getGender());
						info.setIdCard(athlete.getIdCard());
						return info;
					})
					.collect(Collectors.toList());
			vo.setAthleteList(athleteList);

			// 获取项目信息（从选手的项目ID中去重）
			List<Long> projectIds = athletes.stream()
					.map(MartialAthlete::getProjectId)
					.filter(projectId -> projectId != null)
					.distinct()
					.collect(Collectors.toList());

			if (!projectIds.isEmpty()) {
				List<MartialProject> projects = projectService.listByIds(projectIds);

				// 设置项目名称列表
				String projectNames = projects.stream()
						.map(MartialProject::getProjectName)
						.collect(Collectors.joining("、"));
				vo.setProjectNames(projectNames);

				// 设置项目详细列表
				List<MartialRegistrationOrderVO.ProjectInfo> projectList = projects.stream()
						.map(project -> {
							MartialRegistrationOrderVO.ProjectInfo info = new MartialRegistrationOrderVO.ProjectInfo();
							info.setId(project.getId());
							info.setProjectName(project.getProjectName());
							//类型(1-个人,2-双人,3-集体)
							Integer type = project.getType();
							String strType = type == 1 ? "个人":"集体";
							info.setProjectType(strType);
							return info;
						})
						.collect(Collectors.toList());
				vo.setProjectList(projectList);
			}
		}

		return vo;
	}

}
