package com.example.system.service.impl;

import com.example.system.mapper.CompetitionMapper;
import com.example.system.mapper.RegistrationMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.model.RegistrationTable;
import com.example.system.service.RegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CompetitionMapper competitionMapper;

    //根据id修改报名状态
    @Override
    public Boolean updateStatus(Long registrationId, String status) {
        return 1 == registrationMapper.updateStatus(registrationId, status);
    }

    //学生报名比赛
    @Override
    @Transactional
    public Boolean enroll(RegistrationTable registration) {
        return registrationMapper.creatData(registration) == 1;
    }
    @Override
    //获取所有的报名信息
    public Object getAll() {
        List<RegistrationTable> registrationTables = registrationMapper.getRegistration();
        for (RegistrationTable registrationTable: registrationTables) {
            String studentName = userMapper.getNameById(registrationTable.getStudentId());
            registrationTable.setStudentName(studentName);
            String competitionName = competitionMapper.getNameById(registrationTable.getCompetitionId());
            registrationTable.setCompetitionName(competitionName);
            String teamLeaderName = userMapper.getNameById(registrationTable.getTeamLeaderId());
            registrationTable.setTeamLeaderName(teamLeaderName);
        }
        return registrationTables;
    }

    //教师修改获奖等级和证书路径和附加信息
    @Override
    @Transactional
    public Object update(RegistrationTable table) {
        return 1 == registrationMapper.update(table);
    }

    //获取获奖人员的参赛记录
    @Override
    public Object getWinInfo() {
        List<RegistrationTable> list = registrationMapper.getWinInfo();
        for (RegistrationTable table : list) {
            table.setStudentName(userMapper.getNameById(table.getStudentId()));
            table.setCompetitionName(competitionMapper.getNameById(table.getCompetitionId()));
            table.setTeamLeaderName(userMapper.getNameById(table.getTeamLeaderId()));
            log.info(":{}",table);
        }
        return list;
    }

    //获取参赛总人数
    @Override
    public Object getCount() {
        return registrationMapper.getCount();
    }

    //获取成功报名并获奖的总人数
    @Override
    public Object getWinCount() {
        return registrationMapper.getWinCount();
    }

    //根据参赛类型获取参赛人数
    @Override
    public Object getCountByCompetitionType(String competitionType) {
        return registrationMapper.getCountByCompetitionType(competitionType);
    }

    //根据获奖等级获取参赛人数
    @Override
    public Object getCountByLevel(String level) {
        return registrationMapper.getCountByLevel(level);
    }
}