package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IZhxyShyjService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 智慧校园_审核意见Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-30
 */
@Service
public class ZhxyShyjServiceImpl implements IZhxyShyjService 
{
    @Autowired
    private ZhxyShyjMapper zhxyShyjMapper;
    @Autowired
    private RegisterSchoolMapper registerSchoolMapper;
    @Autowired
    private RegisterSchoolFileMapper registerSchoolFileMapper;
    @Autowired
    private RegisterTeacherMapper registerTeacherMapper;
    @Autowired
    private RegisterTeacherClassMapper registerTeacherClassMapper;
    @Autowired
    private RegisterParentsMapper registerParentsMapper;
    @Autowired
    private RegisterParentsStudentMapper registerParentsStudentMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysUserDeptMapper sysUserDeptMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;
    @Autowired
    private FormalParentsStudentMapper formalParentsStudentMapper;
    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;


    /**
     * 查询智慧校园_审核意见
     * 
     * @param id 智慧校园_审核意见主键
     * @return 智慧校园_审核意见
     */
    @Override
    public ZhxyShyj selectZhxyShyjById(Long id)
    {
        return zhxyShyjMapper.selectZhxyShyjById(id);
    }

    /**
     * 查询智慧校园_审核意见列表
     * 
     * @param zhxyShyj 智慧校园_审核意见
     * @return 智慧校园_审核意见
     */
    @Override
    public List<ZhxyShyj> selectZhxyShyjList(ZhxyShyj zhxyShyj)
    {
        return zhxyShyjMapper.selectZhxyShyjList(zhxyShyj);
    }

    /**
     * 新增智慧校园_审核意见
     * 
     * @param zhxyShyj 智慧校园_审核意见
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertZhxyShyj(ZhxyShyj zhxyShyj)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        zhxyShyj.setAuditId(user.getUserId());
        zhxyShyj.setAuditUser(user.getNickName());
        zhxyShyj.setShsj(DateUtils.getNowDate());
        zhxyShyj.setCreateTime(DateUtils.getNowDate());
        zhxyShyj.setCreateTime(DateUtils.getNowDate());
        int index = zhxyShyjMapper.insertZhxyShyj(zhxyShyj);
        //审核类型（1：学校注册，2：老师注册，3：家长注册，4：调课）
        //审核结果（是否通过  1：待审核，2：通过 3:不通过)
        if("1".equals(zhxyShyj.getType())){
            //学校注册
            if("2".equals(zhxyShyj.getShjg())){
                //通过  修改注册表审核结果，生成账号，部门表生成学校， 新增用户与角色管理
                //查询数据信息
                RegisterSchool registerSchool = registerSchoolMapper.selectRegisterSchoolById(zhxyShyj.getDataId());
                //修改注册表审核结果
                registerSchool.setAuditId(user.getUserId());
                registerSchool.setAuditUser(user.getNickName());
                registerSchool.setOpinion(zhxyShyj.getShyj());
                registerSchool.setIsPass(zhxyShyj.getShjg());
                registerSchool.setUpdateTime(DateUtils.getNowDate());
                registerSchoolMapper.updateRegisterSchool(registerSchool);
                //部门表生成学校
                SysDept dept = new SysDept();
                dept.setParentId(0L);
                dept.setAncestors("0");
                dept.setDeptName(registerSchool.getSchoolName());
                    //查询最新的ParentId为100的数据
                SysDept sysDept = deptMapper.selectDeptByParentId(0L);
                dept.setOrderNum(sysDept.getOrderNum()+1);
                dept.setLeader(registerSchool.getUserName());
                dept.setPhone(registerSchool.getUserPhone());
                dept.setCreateTime(DateUtils.getNowDate());
                deptMapper.insertDept(dept);
                //判断是否存在账号
                SysUser use = userMapper.getByOpenId(registerSchool.getOpenId());
                if(use==null){
                    use = new SysUser();
                    //生成账号
                    use.setOpenId(registerSchool.getOpenId());
                    use.setDeptId(dept.getDeptId());
                    use.setUserName(registerSchool.getUserPhone());
                    use.setNickName(registerSchool.getSchoolName());
                    use.setPhonenumber(registerSchool.getUserPhone());
                    use.setPassword(SecurityUtils.encryptPassword("1qaz!QAZ"));
                    use.setCreateTime(DateUtils.getNowDate());
                    userMapper.insertUser(use);
                }else{
                    use.setDeptId(dept.getDeptId());
                    userMapper.updateUser(use);
                }
                //新增用户与角色管理
                Long[] roleIds = {4L};
                sysUserServiceImpl.insertUserRole(use.getUserId(), roleIds);
            }else{
                //不通过  修改注册表审核结果
                //修改注册表审核结果
                RegisterSchool registerSchool = new RegisterSchool();
                registerSchool.setId(zhxyShyj.getDataId());
                registerSchool.setAuditId(user.getUserId());
                registerSchool.setAuditUser(user.getNickName());
                registerSchool.setOpinion(zhxyShyj.getShyj());
                registerSchool.setIsPass(zhxyShyj.getShjg());
                registerSchool.setUpdateTime(DateUtils.getNowDate());
                registerSchoolMapper.updateRegisterSchool(registerSchool);
            }
        }else if("2".equals(zhxyShyj.getType())){
            //老师注册
            if("2".equals(zhxyShyj.getShjg())){
                //通过  修改注册表审核结果，生成账号， 新增用户与角色管理，老师-班级（学科）数据
                //查询数据信息
                RegisterTeacher registerTeacher = registerTeacherMapper.selectRegisterTeacherById(zhxyShyj.getDataId());
                RegisterTeacherClass registerTeacherClass = new RegisterTeacherClass();
                registerTeacherClass.setTeacherId(registerTeacher.getId());
                List<RegisterTeacherClass> registerTeacherClassList = registerTeacherClassMapper.selectRegisterTeacherClassList(registerTeacherClass);
                //修改注册表审核结果
                registerTeacher.setAuditId(user.getUserId());
                registerTeacher.setAuditUser(user.getNickName());
                registerTeacher.setOpinion(zhxyShyj.getShyj());
                registerTeacher.setIsPass(zhxyShyj.getShjg());
                registerTeacher.setUpdateTime(DateUtils.getNowDate());
                registerTeacherMapper.updateRegisterTeacher(registerTeacher);
                //生成账号
                //判断是否存在账号
                SysUser use = userMapper.getByOpenId(registerTeacher.getOpenId());
                if(use==null){
                    use = new SysUser();
                    use.setOpenId(registerTeacher.getOpenId());
                    //use.setDeptId(dept.getDeptId());
                    use.setUserName(registerTeacher.getUserPhone());
                    use.setNickName(registerTeacher.getUserName());
                    use.setPhonenumber(registerTeacher.getUserPhone());
                    use.setPassword(SecurityUtils.encryptPassword("1qaz!QAZ"));
                    use.setCreateTime(DateUtils.getNowDate());
                    userMapper.insertUser(use);
                }

                //新增用户与角色管理
                Long[] roleIds = {3L};
                sysUserServiceImpl.insertUserRole(use.getUserId(), roleIds);
                //老师-班级（学科）数据
                for (RegisterTeacherClass teacherClass : registerTeacherClassList) {
                    FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
                    formalTeacherClass.setTeacherId(use.getUserId());
                    formalTeacherClass.setTeacherName(use.getNickName());
                    formalTeacherClass.setSchoolId(registerTeacher.getSchoolId());
                    formalTeacherClass.setSchoolName(registerTeacher.getSchoolName());
                    formalTeacherClass.setClassId(teacherClass.getClassId());
                    formalTeacherClass.setClassName(teacherClass.getClassName());
                    formalTeacherClass.setDiscipline(teacherClass.getDiscipline());
                    formalTeacherClass.setCreateTime(DateUtils.getNowDate());
                    formalTeacherClassMapper.insertFormalTeacherClass(formalTeacherClass);
                }

            }else{
                //不通过  修改注册表审核结果
                //修改注册表审核结果
                RegisterTeacher registerTeacher = new RegisterTeacher();
                registerTeacher.setId(zhxyShyj.getDataId());
                registerTeacher.setAuditId(user.getUserId());
                registerTeacher.setAuditUser(user.getNickName());
                registerTeacher.setOpinion(zhxyShyj.getShyj());
                registerTeacher.setIsPass(zhxyShyj.getShjg());
                registerTeacher.setUpdateTime(DateUtils.getNowDate());
                registerTeacherMapper.updateRegisterTeacher(registerTeacher);
            }
        }else if("3".equals(zhxyShyj.getType())){
            //审核类型（1：学校注册，2：老师注册，3：家长注册，4：调课）
            //家长注册
            //查询数据信息
            RegisterParentsStudent registerParentsStudent = registerParentsStudentMapper.selectRegisterParentsStudentById(zhxyShyj.getDataId());
            RegisterParents registerParents = registerParentsMapper.selectRegisterParentsById(registerParentsStudent.getParentsId());
            if("2".equals(zhxyShyj.getShjg())){

                //通过  修改注册表审核结果，生成账号， 新增用户与角色管理，家长-学生(审核通过之后数据)
                //查询是否存在账号
                SysUser sysUser = userMapper.getByOpenId(registerParents.getOpenId());

                //修改注册表审核结果
                registerParentsStudent.setAuditId(user.getUserId());
                registerParentsStudent.setAuditUser(user.getNickName());
                registerParentsStudent.setOpinion(zhxyShyj.getShyj());
                registerParentsStudent.setIsPass(zhxyShyj.getShjg());
                registerParentsStudent.setUpdateTime(DateUtils.getNowDate());
                registerParentsStudentMapper.updateRegisterParentsStudent(registerParentsStudent);

                if(sysUser==null){
                    //账号不存在
                    registerParents.setIsPass(zhxyShyj.getShjg());
                    registerParentsMapper.updateRegisterParents(registerParents);
                    //生成账号
                    sysUser = new SysUser();
                    sysUser.setOpenId(registerParents.getOpenId());
                    //use.setDeptId(dept.getDeptId());
                    sysUser.setUserName(registerParents.getUserPhone());
                    sysUser.setNickName(registerParents.getUserName());
                    sysUser.setPhonenumber(registerParents.getUserPhone());
                    sysUser.setPassword(SecurityUtils.encryptPassword("1qaz!QAZ"));
                    sysUser.setCreateTime(DateUtils.getNowDate());
                    userMapper.insertUser(sysUser);
                    //新增用户与角色管理
                    Long[] roleIds = {5L};
                    sysUserServiceImpl.insertUserRole(sysUser.getUserId(), roleIds);
                }else{
                    FormalParentsStudent parentsStudent = new FormalParentsStudent();
                    parentsStudent.setParentsId(sysUser.getUserId());
                    List<FormalParentsStudent> parentsStudentList = formalParentsStudentMapper.selectFormalParentsStudentList(parentsStudent);
                    if(parentsStudentList==null || parentsStudentList.size()<=0){
                        //新增用户与角色管理
                        Long[] roleIds = {5L};
                        sysUserServiceImpl.insertUserRole(sysUser.getUserId(), roleIds);
                    }
                }

                //新增家长-学生(审核通过之后数据)
                FormalParentsStudent formalParentsStudent = new FormalParentsStudent();
                formalParentsStudent.setParentsId(sysUser.getUserId());
                formalParentsStudent.setSchoolId(registerParentsStudent.getSchoolId());
                formalParentsStudent.setSchoolName(registerParentsStudent.getSchoolName());
                formalParentsStudent.setClassId(registerParentsStudent.getClassId());
                formalParentsStudent.setClassName(registerParentsStudent.getClassName());
                formalParentsStudent.setStudentName(registerParentsStudent.getStudentName());
                formalParentsStudent.setStudentNumber(registerParentsStudent.getStudentNumber());
                formalParentsStudent.setCreateTime(DateUtils.getNowDate());
                formalParentsStudent.setCreateBy(user.getNickName());
                formalParentsStudentMapper.insertFormalParentsStudent(formalParentsStudent);

            }else{
                //不通过  修改注册表审核结果
                //修改注册表审核结果
                RegisterParentsStudent registerParentsStudentNo = new RegisterParentsStudent();
                registerParentsStudentNo.setId(zhxyShyj.getDataId());
                registerParentsStudentNo.setAuditId(user.getUserId());
                registerParentsStudentNo.setAuditUser(user.getNickName());
                registerParentsStudentNo.setOpinion(zhxyShyj.getShyj());
                registerParentsStudentNo.setIsPass(zhxyShyj.getShjg());
                registerParentsStudentNo.setUpdateTime(DateUtils.getNowDate());
                registerParentsStudentMapper.updateRegisterParentsStudent(registerParentsStudentNo);
                //判断是否所有的都不通过（查询是否存在待审核和审核通过的数据）
                RegisterParentsStudent parentsStudent = new RegisterParentsStudent();
                parentsStudent.setParentsId(registerParents.getId());
                List<RegisterParentsStudent> registerParentsStudentList = registerParentsStudentMapper.selectRegisterParentsStudentList(parentsStudent);
                Boolean bl = true;
                for (RegisterParentsStudent student : registerParentsStudentList) {
                    if(!"3".equals(student.getIsPass())){
                        bl = false;
                        break;
                    }
                }
                //全部都不通过才改家长表状态
                if(bl){
                    registerParents.setIsPass(zhxyShyj.getShjg());
                    registerParentsMapper.updateRegisterParents(registerParents);
                }

            }
        }
        return index;
    }

    /***
     * 生成班级
     * @param sysDept
     * @return
     */
    private List<SysDept> createClassList(SysDept sysDept) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType("sys_class");
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataList(dictData);

        List<SysDept> sysDeptList = new ArrayList<>();
        String ancestors = "0"+","+String.valueOf(sysDept.getDeptId());
        int index = 1;
        for (SysDictData sysDictData : sysDictDataList) {
            SysDept dept = new SysDept();
            dept.setParentId(sysDept.getDeptId());
            dept.setAncestors(ancestors);
            dept.setDeptName(sysDictData.getDictLabel());
            dept.setOrderNum(index);
            dept.setStatus("1");
            dept.setCreateTime(DateUtils.getNowDate());
            sysDeptList.add(dept);
            index = index+5;
        }

        return sysDeptList;
    }

    /**
     * 修改智慧校园_审核意见
     * 
     * @param zhxyShyj 智慧校园_审核意见
     * @return 结果
     */
    @Override
    public int updateZhxyShyj(ZhxyShyj zhxyShyj)
    {
        zhxyShyj.setUpdateTime(DateUtils.getNowDate());
        return zhxyShyjMapper.updateZhxyShyj(zhxyShyj);
    }

    /**
     * 批量删除智慧校园_审核意见
     * 
     * @param ids 需要删除的智慧校园_审核意见主键
     * @return 结果
     */
    @Override
    public int deleteZhxyShyjByIds(Long[] ids)
    {
        return zhxyShyjMapper.deleteZhxyShyjByIds(ids);
    }

    /**
     * 删除智慧校园_审核意见信息
     * 
     * @param id 智慧校园_审核意见主键
     * @return 结果
     */
    @Override
    public int deleteZhxyShyjById(Long id)
    {
        return zhxyShyjMapper.deleteZhxyShyjById(id);
    }
}
