package com.qmth.exam.reserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.exam.reserve.bean.examsite.ExamSiteInfo;
import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
import com.qmth.exam.reserve.dao.ExamSiteDao;
import com.qmth.exam.reserve.entity.ExamSiteEntity;
import com.qmth.exam.reserve.service.ExamSiteService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamSiteServiceImpl extends ServiceImpl<ExamSiteDao, ExamSiteEntity> implements ExamSiteService {

    @Override
    public List<CategoryVO> listExamSite(Long teachingId) {
        QueryWrapper<ExamSiteEntity> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ExamSiteEntity> lw = wrapper.lambda();
        lw.eq(ExamSiteEntity::getEnable, Boolean.TRUE);
        lw.eq(ExamSiteEntity::getCategoryId, teachingId);
        List<ExamSiteEntity> list = this.baseMapper.selectList(wrapper);
        List<CategoryVO> categoryList = new ArrayList<>();
        for (ExamSiteEntity site : list) {
            CategoryVO vo = new CategoryVO();
            vo.setId(site.getId());
            vo.setName(site.getName());
            categoryList.add(vo);
        }
        return categoryList;
    }

    @Override
    public List<ExamSiteInfo> getExamSiteListForStudent(Long categoryId) {
        LambdaQueryWrapper<ExamSiteEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(ExamSiteEntity::getId, ExamSiteEntity::getName, ExamSiteEntity::getAddress);
        wrapper.eq(ExamSiteEntity::getCategoryId, categoryId);
        wrapper.eq(ExamSiteEntity::getEnable, Boolean.TRUE);
        List<ExamSiteEntity> entities = baseMapper.selectList(wrapper);

        List<ExamSiteInfo> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(entities)) {
            for (ExamSiteEntity entity : entities) {
                ExamSiteInfo info = new ExamSiteInfo();
                info.setExamSiteId(entity.getId());
                info.setExamSiteName(entity.getName());
                info.setExamSiteAddress(entity.getAddress());
                list.add(info);
            }
        }

        return list;
    }

    /**
     * 获取当前考点总容量
     */
    @Override
    public int countExamSiteCapacityById(Long examSiteId) {
        // 当前考点总容量 = ∑考场机房容量
        Integer capacity = baseMapper.countExamSiteCapacityById(examSiteId);
        return capacity != null ? capacity : 0;
    }

}
