package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dto.StudentCompetitionRankingDto;
import com.ym.mec.biz.dal.page.StudentCompetitionQueryInfo;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.ym.mec.biz.dal.entity.StudentCompetition;
import com.ym.mec.biz.service.StudentCompetitionService;
import com.ym.mec.biz.dal.dao.StudentCompetitionDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class StudentCompetitionServiceImpl extends BaseServiceImpl<Long, StudentCompetition> implements StudentCompetitionService {
	
	@Autowired
	private StudentCompetitionDao studentCompetitionDao;

	@Override
	public BaseDAO<Long, StudentCompetition> getDAO() {
		return studentCompetitionDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void addStudentCompetition(StudentCompetition studentCompetition) {
		StudentCompetition userCompetition = studentCompetitionDao.getCompetitionWithUser(studentCompetition.getUserId());
		if(Objects.nonNull(userCompetition)){
			throw new BizException("您已有正在审核中的参赛作品");
		}
		if(StringUtils.isBlank(studentCompetition.getSubject())){
			throw new BizException("请选择参赛专业");
		}
		if(StringUtils.isBlank(studentCompetition.getChapter())){
			throw new BizException("请选择参赛曲目");
		}
		if(StringUtils.isBlank(studentCompetition.getVideoUrl())){
			throw new BizException("请指定参赛作品");
		}
		studentCompetition.setIsShow(false);
		studentCompetitionDao.insert(studentCompetition);
	}

	@Override
	public PageInfo<StudentCompetition> queryPage(StudentCompetitionQueryInfo queryInfo) {
		PageInfo<StudentCompetition> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List<StudentCompetition> dataList = null;
		int count = studentCompetitionDao.countStudentCompetitions(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentCompetitionDao.queryStudentCompetitions(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public StudentCompetition checkIsUpload(Integer userId) {
		return studentCompetitionDao.getCompetitionWithUser(userId);
	}

	@Override
	public List<StudentCompetitionRankingDto> getWinnerList() {
		List<StudentCompetition> winnerList = studentCompetitionDao.getWinnerList();
		List<StudentCompetitionRankingDto> winnerSimpleInfoList = new ArrayList<>();
		for (StudentCompetition studentCompetition : winnerList) {
			winnerSimpleInfoList.add(new StudentCompetitionRankingDto(studentCompetition.getId(), studentCompetition.getPrizeLevel(), studentCompetition.getUsername(), studentCompetition.getPictureUrl()));
		}
		return winnerSimpleInfoList;
	}

	@Override
	public void updateCompetition(StudentCompetition studentCompetition) {
		if(Objects.nonNull(studentCompetition.getPrizeLevel())&&studentCompetition.getPrizeLevel()>0){
			Set<Long> competitionsWithPrizeLevel = studentCompetitionDao.getCompetitionsWithPrizeLevel(studentCompetition.getPrizeLevel());
			int maxPrizeLevelUserNum = 0;
			switch (studentCompetition.getPrizeLevel()){
				case 1:
					maxPrizeLevelUserNum = 1;
					break;
				case 2:
				case 3:
					maxPrizeLevelUserNum = 12;
					break;
				case 4:
					maxPrizeLevelUserNum = 24;
					break;
				case 5:
					maxPrizeLevelUserNum = 50;
					break;
			}
			if(competitionsWithPrizeLevel.size()>=maxPrizeLevelUserNum&&!competitionsWithPrizeLevel.contains(studentCompetition.getId())){
				throw new BizException("该奖项名额已达上线");
			}
		}
		studentCompetitionDao.update(studentCompetition);
	}
}
