package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.params.TeachStudentBatchParam;
import com.qmth.distributed.print.business.bean.result.TeachStudentResult;
import com.qmth.teachcloud.common.entity.BasicExamStudent;
import com.qmth.distributed.print.business.entity.TeachClazz;
import com.qmth.distributed.print.business.entity.TeachStudent;
import com.qmth.distributed.print.business.mapper.TeachStudentMapper;
import com.qmth.distributed.print.business.service.BasicExamStudentService;
import com.qmth.distributed.print.business.service.TeachClazzService;
import com.qmth.distributed.print.business.service.TeachStudentService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 教学学生服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-02-09
 */
@Service
public class TeachStudentServiceImpl extends ServiceImpl<TeachStudentMapper, TeachStudent> implements TeachStudentService {

    @Resource
    private BasicExamStudentService basicExamStudentService;

    @Resource
    private TeachClazzService teachClazzService;

    @Override
    public IPage<TeachStudentResult> teachStudentPage(Long teachClazzId, String studentInfo, int pageNumber, int pageSize) {
        studentInfo = SystemConstant.translateSpecificSign(studentInfo);
        Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
        return this.baseMapper.findTeachStudentPage(new Page<>(pageNumber, pageSize), teachClazzId, studentInfo,
                schoolId);
    }

    @Deprecated
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean createTeachStudent(String studentName, String studentCode, Long teachClazzId, SysUser requestUser) {
        throw ExceptionResultEnum.ERROR.exception("废弃的方法");
        //        if (SystemConstant.isOneNull(studentName, studentCode, teachClazzId)) {
        //            throw ExceptionResultEnum.ERROR.exception("参数不完整");
        //        }
        //        Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
        //        BasicStudent basicStudent = basicStudentService.getOne(new QueryWrapper<BasicStudent>()
        //                .lambda()
        //                .eq(BasicStudent::getSchoolId, schoolId)
        //                .eq(BasicStudent::getStudentCode, studentCode)
        //                .eq(BasicStudent::getStudentName, studentName));
        //        if (Objects.isNull(basicStudent)) {
        //            throw ExceptionResultEnum.ERROR.exception("学生姓名[" + studentName + "]，学号[" + studentCode + "]的学生在学生管理中不存在，请先创建该学生");
        //        }
        //        if (this.checkTeachStudentRepeat(basicStudent.getId(), teachClazzId, schoolId)) {
        //            throw ExceptionResultEnum.ERROR.exception("学生姓名[" + studentName + "]，学号为[" + studentCode + "]的学生在该班级已存在");
        //        }
        //        List<BasicStudent> basicStudentList = new ArrayList<>();
        //        basicStudentList.add(basicStudent);
        //        return this.createTeachStudentBatch(basicStudentList, teachClazzId, schoolId, requestUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createTeachStudentBatch(TeachStudentBatchParam teachStudentBatchParam, SysUser requestUser) {
        Long teachClazzId = teachStudentBatchParam.getTeachClazzId();
        List<Long> basicExamStudentIdList = teachStudentBatchParam.getBasicExamStudentIdList();

        List<BasicExamStudent> basicExamStudentList = basicExamStudentService.listByIds(basicExamStudentIdList);
        if (basicExamStudentList.size() > 0) {
            this.createTeachStudentBatch(basicExamStudentList, teachClazzId, requestUser);
        }
    }

    @Deprecated
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String importTeachStudent(MultipartFile file, Long teachClazzId, Long schoolId, SysUser requestUser) throws IOException, NoSuchFieldException {
        throw ExceptionResultEnum.ERROR.exception("废弃的方法");
        //        if (SystemConstant.isOneNull(file, teachClazzId, schoolId)) {
        //            throw ExceptionResultEnum.ERROR.exception("参数缺失");
        //        }
        //
        //        List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(file.getInputStream(), Lists.newArrayList(TeachStudentImportDto.class), (finalExcelList, finalColumnNameList, finalExcelErrorList) -> {
        //            if (finalExcelErrorList.size() > 0) {
        //                throw ExceptionResultEnum.ERROR.exception(JSONObject.toJSONString(finalExcelErrorList));
        //            }
        //            return finalExcelList;
        //        },2);
        //
        //        List<BasicStudent> basicStudentDatasource = basicStudentService.list(new QueryWrapper<BasicStudent>().lambda().eq(BasicStudent::getSchoolId, schoolId));
        //        Set<String> studentCodeSet = new HashSet<>();
        //
        //        if (Objects.nonNull(finalList) && finalList.size() > 0) {
        //            for (int i = 0; i < finalList.size(); i++) {
        //                LinkedMultiValueMap<Integer, Object> map = finalList.get(i);
        //                List<Object> teachStudentImportDtoList = map.get(i);
        //                for (int y = 0; y < Objects.requireNonNull(teachStudentImportDtoList).size(); y++) {
        //                    if (teachStudentImportDtoList.get(y) instanceof TeachStudentImportDto) {
        //                        TeachStudentImportDto teachStudentImportDto = (TeachStudentImportDto) teachStudentImportDtoList.get(y);
        //                        String studentCode = teachStudentImportDto.getStudentCode();
        //                        studentCodeSet.add(studentCode);
        //                    }
        //                }
        //            }
        //        }
        //
        //        // 要导入操作的基础学生数据集合
        //        List<BasicStudent> basicStudentList = basicStudentDatasource.stream()
        //                .filter(e -> studentCodeSet.contains(e.getStudentCode()))
        //                .collect(Collectors.toList());
        //
        //        // 学号不存在
        //        String basicStudentNotFoundStr = studentCodeSet.stream()
        //                .filter(e -> !basicStudentList.stream().map(BasicStudent::getStudentCode).collect(Collectors.toList()).contains(e))
        //                .collect(Collectors.joining(","));
        //
        //        this.createTeachStudentBatch(basicStudentList, teachClazzId, schoolId, requestUser);
        //        String success = "[" + basicStudentList.size() + "]个学生导入成功";
        //        if (SystemConstant.strNotNull(basicStudentNotFoundStr)){
        //            success = success + "，且学号[" + basicStudentNotFoundStr + "]的学生在字典中不存在没有导入";
        //        }
        //        return success;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteTeachStudentBatch(List<Long> idList) {
        return this.removeByIds(idList);
    }

    /**
     * 批量创建教学学生
     *
     * @param basicExamStudentList 要创建教学学生的考生字典集合
     * @param teachClazzId         教学班级id
     * @param requestUser          请求用户
     */
    private void createTeachStudentBatch(List<BasicExamStudent> basicExamStudentList, Long teachClazzId, SysUser requestUser) {
        TeachClazz teachClazz = teachClazzService.getById(teachClazzId);
        if (Objects.isNull(teachClazz)) {
            throw ExceptionResultEnum.ERROR.exception("教学班级不存在");
        }

        List<TeachStudent> teachStudentList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(basicExamStudentList)) {
            for (BasicExamStudent basicExamStudent : basicExamStudentList) {
                Long basicExamStudentId = basicExamStudent.getId();
                // 批量新增重复的课程跳过
                if (this.checkTeachStudentRepeat(basicExamStudentId, teachClazzId)) {
                    continue;
                }

                TeachStudent teachStudent = new TeachStudent();
                teachStudent.setSchoolId(requestUser.getSchoolId());
                teachStudent.setStudentId(basicExamStudent.getId());
                teachStudent.setTeachClazzId(teachClazzId);
                teachStudent.setTeachCourseId(teachClazz.getTeachCourseId());
                teachStudent.setUserId(teachClazz.getUserId());
                teachStudent.insertInfo(requestUser.getId());
                teachStudentList.add(teachStudent);
            }
        }
        this.saveBatch(teachStudentList);
    }

    /**
     * 检验教学学生已存在
     *
     * @param basicExamStudentId 考生字典id
     * @param teachClazzId       教学班级id
     * @return true-已存在不能新增、false-不存在可以新增
     */
    private Boolean checkTeachStudentRepeat(Long basicExamStudentId, Long teachClazzId) {
        TeachStudent teachStudent = this.getOne(
                new QueryWrapper<TeachStudent>().lambda().eq(TeachStudent::getStudentId, basicExamStudentId).eq(TeachStudent::getTeachClazzId, teachClazzId));
        return Objects.nonNull(teachStudent);
    }
}
