package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IRegisterSchoolService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注册-学校Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-24
 */
@Service
public class RegisterSchoolServiceImpl implements IRegisterSchoolService 
{
    @Autowired
    private RegisterSchoolMapper registerSchoolMapper;
    @Autowired
    private RegisterSchoolFileMapper registerSchoolFileMapper;
    @Autowired
    private RegisterTeacherMapper registerTeacherMapper;
    @Autowired
    private RegisterParentsMapper registerParentsMapper;

    @Autowired
    private RegisterTeacherClassMapper registerTeacherClassMapper;
    @Autowired
    private RegisterParentsStudentMapper registerParentsStudentMapper;

    /**
     * 查询注册-学校
     * 
     * @param id 注册-学校主键
     * @return 注册-学校
     */
    @Override
    public RegisterSchool selectRegisterSchoolById(Long id)
    {
        RegisterSchool registerSchool = registerSchoolMapper.selectRegisterSchoolById(id);
        RegisterSchoolFile registerSchoolFile = new RegisterSchoolFile();
        registerSchoolFile.setSchoolId(id);
        List<RegisterSchoolFile> registerSchoolFileList = registerSchoolFileMapper.selectRegisterSchoolFileList(registerSchoolFile);
        registerSchool.setRegisterSchoolFileList(registerSchoolFileList);

        return registerSchool;
    }

    /**
     * 查询注册-学校列表
     * 
     * @param registerSchool 注册-学校
     * @return 注册-学校
     */
    @Override
    public List<RegisterSchool> selectRegisterSchoolList(RegisterSchool registerSchool)
    {
        List<RegisterSchool> registerSchoolList = registerSchoolMapper.selectRegisterSchoolList(registerSchool);
        if(registerSchoolList!=null && registerSchoolList.size()>0){
            for (RegisterSchool school : registerSchoolList) {
                RegisterSchoolFile registerSchoolFile = new RegisterSchoolFile();
                registerSchoolFile.setSchoolId(school.getId());
                List<RegisterSchoolFile> registerSchoolFileList = registerSchoolFileMapper.selectRegisterSchoolFileList(registerSchoolFile);
                school.setRegisterSchoolFileList(registerSchoolFileList);
            }
        }
        //List<Long> ids = registerSchoolList.stream().map(RegisterSchool::getId).collect(Collectors.toList());
        //List<RegisterSchoolFile> registerSchoolFile = registerSchoolFileMapper
        return registerSchoolList;
    }

    /**
     * 新增注册-学校
     * 
     * @param registerSchool 注册-学校
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertRegisterSchool(RegisterSchool registerSchool)
    {
        //查询是否已经注册过
        RegisterSchool registerSchoolOld = registerSchoolMapper.selectRegisterSchooByOppenId(registerSchool.getOpenId());
        if(registerSchoolOld!=null){
            if(registerSchoolOld.getIsPass().equals("1")){
                return AjaxResult.error("该微信已注册当前身份，信息正在审核中。",registerSchoolOld);
            }else if(registerSchoolOld.getIsPass().equals("2")){
                return AjaxResult.error("该微信已注册当前身份，审核已通过，请登录。",registerSchoolOld);
            }else{
                return AjaxResult.error("该微信已注册当前身份，审核未通过。",registerSchoolOld);
            }
        }
        registerSchool.setCreateTime(DateUtils.getNowDate());
        registerSchool.setUpdateTime(DateUtils.getNowDate());
        registerSchool.setIsPass("1");
        int count = registerSchoolMapper.insertRegisterSchool(registerSchool);
        if(registerSchool.getRegisterSchoolFileList()!=null && registerSchool.getRegisterSchoolFileList().size()>0){
            List<RegisterSchoolFile> registerSchoolFile = registerSchool.getRegisterSchoolFileList();
            for (RegisterSchoolFile schoolFile : registerSchoolFile) {
                schoolFile.setSchoolId(registerSchool.getId());
                registerSchoolFileMapper.insertRegisterSchoolFile(schoolFile);
            }
        }
        return count > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改注册-学校
     * 
     * @param registerSchool 注册-学校
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRegisterSchool(RegisterSchool registerSchool)
    {
        registerSchool.setIsPass("1");
        registerSchool.setUpdateTime(DateUtils.getNowDate());
        //先删除附件
        if(registerSchool.getRegisterSchoolFileList()!=null && registerSchool.getRegisterSchoolFileList().size()>0){
            registerSchoolFileMapper.deleteRegisterSchoolFileBySchoolId(registerSchool.getId());
            List<RegisterSchoolFile> registerSchoolFile = registerSchool.getRegisterSchoolFileList();
            for (RegisterSchoolFile schoolFile : registerSchoolFile) {
                schoolFile.setSchoolId(registerSchool.getId());
                registerSchoolFileMapper.insertRegisterSchoolFile(schoolFile);
            }
        }

        return registerSchoolMapper.updateRegisterSchool(registerSchool);
    }

    /**
     * 批量删除注册-学校
     * 
     * @param ids 需要删除的注册-学校主键
     * @return 结果
     */
    @Override
    public int deleteRegisterSchoolByIds(Long[] ids)
    {
        return registerSchoolMapper.deleteRegisterSchoolByIds(ids);
    }

    /**
     * 删除注册-学校信息
     * 
     * @param id 注册-学校主键
     * @return 结果
     */
    @Override
    public int deleteRegisterSchoolById(Long id)
    {
        return registerSchoolMapper.deleteRegisterSchoolById(id);
    }

    @Override
    public AjaxResult queryInfo(String openId) {
        if(StringUtils.isEmpty(openId)){
            throw new ServiceException("参数获取异常，请稍后重试");
        }
        Map<String,Object> map = new HashMap<>();
        //查询学校里是否有注册信息
        RegisterSchool registerSchoolOld = registerSchoolMapper.selectRegisterSchooByOppenId(openId);
        if(registerSchoolOld!=null){
            RegisterSchoolFile registerSchoolFile = new RegisterSchoolFile();
            registerSchoolFile.setSchoolId(registerSchoolOld.getId());
            List<RegisterSchoolFile> registerSchoolFileList = registerSchoolFileMapper.selectRegisterSchoolFileList(registerSchoolFile);
            registerSchoolOld.setRegisterSchoolFileList(registerSchoolFileList);
        }else{
            registerSchoolOld = new RegisterSchool();
        }
        map.put("school",registerSchoolOld);
        //查询老师是否有注册信息
        RegisterTeacher registerTeacher = registerTeacherMapper.selectRegisterTeacherByOppenId(openId);
        if(registerTeacher!=null){
            RegisterTeacherClass registerTeacherClass = new RegisterTeacherClass();
            registerTeacherClass.setTeacherId(registerTeacher.getId());
            List<RegisterTeacherClass> registerTeacherClassList = registerTeacherClassMapper.selectRegisterTeacherClassList(registerTeacherClass);
            registerTeacher.setRegisterTeacherClassList(registerTeacherClassList);
        }else{
            registerTeacher = new RegisterTeacher();
        }

        map.put("teacher",registerTeacher);
        //查询家长注册信息
        RegisterParents registerParents = registerParentsMapper.selectRegisterParentsByOppenId(openId);
        if(registerParents!=null){
            RegisterParentsStudent registerParentsStudent = new RegisterParentsStudent();
            registerParentsStudent.setParentsId(registerParents.getId());
            List<RegisterParentsStudent> registerParentsStudentList = registerParentsStudentMapper.selectRegisterParentsStudentList(registerParentsStudent);
            registerParents.setRegisterParentsStudentList(registerParentsStudentList);
        }else{
            registerParents = new RegisterParents();
        }

        map.put("parents",registerParents);
        return AjaxResult.success(map);
    }
}
