package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.RegisterSchool;
import com.ruoyi.system.domain.RegisterSchoolFile;
import com.ruoyi.system.domain.RegisterTeacherClass;
import com.ruoyi.system.mapper.RegisterTeacherClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegisterTeacherMapper;
import com.ruoyi.system.domain.RegisterTeacher;
import com.ruoyi.system.service.IRegisterTeacherService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注册-老师Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-25
 */
@Service
public class RegisterTeacherServiceImpl implements IRegisterTeacherService 
{
    @Autowired
    private RegisterTeacherMapper registerTeacherMapper;
    @Autowired
    private RegisterTeacherClassMapper registerTeacherClassMapper;

    /**
     * 查询注册-老师
     * 
     * @param id 注册-老师主键
     * @return 注册-老师
     */
    @Override
    public RegisterTeacher selectRegisterTeacherById(Long id)
    {

        RegisterTeacher registerTeacher = registerTeacherMapper.selectRegisterTeacherById(id);
        RegisterTeacherClass registerTeacherClass = new RegisterTeacherClass();
        registerTeacherClass.setTeacherId(id);
        List<RegisterTeacherClass> registerTeacherClassList = registerTeacherClassMapper.selectRegisterTeacherClassList(registerTeacherClass);
        registerTeacher.setRegisterTeacherClassList(registerTeacherClassList);
        return registerTeacher;
    }

    /**
     * 查询注册-老师列表
     * 
     * @param registerTeacher 注册-老师
     * @return 注册-老师
     */
    @Override
    public List<RegisterTeacher> selectRegisterTeacherList(RegisterTeacher registerTeacher)
    {

        List<RegisterTeacher> registerTeacherList = registerTeacherMapper.selectRegisterTeacherList(registerTeacher);
        if(registerTeacherList!=null && registerTeacherList.size()>0){
            for (RegisterTeacher teacher : registerTeacherList) {
                RegisterTeacherClass registerTeacherClass = new RegisterTeacherClass();
                registerTeacherClass.setTeacherId(teacher.getId());
                List<RegisterTeacherClass> registerTeacherClassList = registerTeacherClassMapper.selectRegisterTeacherClassList(registerTeacherClass);
                teacher.setRegisterTeacherClassList(registerTeacherClassList);
            }
        }
        return registerTeacherList;
    }

    /**
     * 新增注册-老师
     * 
     * @param registerTeacher 注册-老师
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertRegisterTeacher(RegisterTeacher registerTeacher)
    {
        //查询是否已经注册过
        RegisterTeacher registerTeacherOld = registerTeacherMapper.selectRegisterTeacherByOppenId(registerTeacher.getOpenId());
        if(registerTeacherOld!=null){
            if(registerTeacherOld.getIsPass().equals("1")){
                return AjaxResult.error("该微信已注册当前身份，信息正在审核中。",registerTeacherOld);
            }else if(registerTeacherOld.getIsPass().equals("2")){
                return AjaxResult.error("该微信已注册当前身份，审核已通过，请登录。",registerTeacherOld);
            }else{
                return AjaxResult.error("该微信已注册当前身份，审核未通过。",registerTeacherOld);
            }
        }
        registerTeacher.setCreateTime(DateUtils.getNowDate());
        registerTeacher.setUpdateTime(DateUtils.getNowDate());
        int rows = registerTeacherMapper.insertRegisterTeacher(registerTeacher);

        List<RegisterTeacherClass> registerTeacherClassList = registerTeacher.getRegisterTeacherClassList();
        if(registerTeacherClassList!=null && registerTeacherClassList.size()>0){
            for (RegisterTeacherClass registerTeacherClass : registerTeacherClassList) {
                registerTeacherClass.setTeacherId(registerTeacher.getId());
                registerTeacherClassMapper.insertRegisterTeacherClass(registerTeacherClass);

            }
        }
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改注册-老师
     * 
     * @param registerTeacher 注册-老师
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRegisterTeacher(RegisterTeacher registerTeacher)
    {
        //删除注册表-老师-班级（学科）表
        List<RegisterTeacherClass> registerTeacherClassList = registerTeacher.getRegisterTeacherClassList();
        if(registerTeacherClassList!=null && registerTeacherClassList.size()>0){
            registerTeacherClassMapper.deleteRegisterTeacherClassByTeacherId(registerTeacher.getId());
            for (RegisterTeacherClass registerTeacherClass : registerTeacherClassList) {
                registerTeacherClass.setTeacherId(registerTeacher.getId());
                registerTeacherClassMapper.insertRegisterTeacherClass(registerTeacherClass);
            }
        }
        registerTeacher.setIsPass("1");
        registerTeacher.setUpdateTime(DateUtils.getNowDate());
        return registerTeacherMapper.updateRegisterTeacher(registerTeacher);
    }

    /**
     * 批量删除注册-老师
     * 
     * @param ids 需要删除的注册-老师主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRegisterTeacherByIds(Long[] ids)
    {
        registerTeacherClassMapper.deleteRegisterTeacherClassByTeacherIds(ids);
        return registerTeacherMapper.deleteRegisterTeacherByIds(ids);
    }

    /**
     * 删除注册-老师信息
     * 
     * @param id 注册-老师主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRegisterTeacherById(Long id)
    {
        registerTeacherClassMapper.deleteRegisterTeacherClassByTeacherId(id);
        return registerTeacherMapper.deleteRegisterTeacherById(id);
    }
}
