package com.xjrsoft.module.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.ArchivesStatusEnum;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.enums.SchoolChangeType;
import com.xjrsoft.module.student.dto.SchoolRollStudentPageDto;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.entity.SchoolRollStudent;
import com.xjrsoft.module.student.mapper.BaseStudentSchoolRollMapper;
import com.xjrsoft.module.student.mapper.SchoolRollStudentMapper;
import com.xjrsoft.module.student.service.ISchoolRollStudentService;
import com.xjrsoft.module.student.vo.SchoolRollStudentPageVo;
import com.xjrsoft.module.student.vo.SchoolRollStudentVo;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
* @title: 学籍异动学生
* @Author dzx
* @Date: 2023-11-30
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class SchoolRollStudentServiceImpl extends MPJBaseServiceImpl<SchoolRollStudentMapper, SchoolRollStudent> implements ISchoolRollStudentService {
    private final SchoolRollStudentMapper schoolRollStudentMapper;
    private final XjrUserMapper xjrUserMapper;
    private final BaseStudentSchoolRollMapper baseStudentSchoolRollMapper;


    @Override
    public Boolean dataHandle(Long dataId) {
        SchoolRollStudent schoolRollStudent = schoolRollStudentMapper.selectOne(
            Wrappers.lambdaQuery(SchoolRollStudent.class).eq(SchoolRollStudent::getId, dataId)
        );
        String scholarshipSource = schoolRollStudent.getScholarshipSource();
        if(SchoolChangeType.ScOutSchool.getCode().equals(scholarshipSource)){
            scOutSchool(schoolRollStudent);
        }else if(SchoolChangeType.ScOut.getCode().equals(scholarshipSource)){
            scOut(schoolRollStudent);
        }else if(SchoolChangeType.ScRecoverSchool.getCode().equals(scholarshipSource)){
            scRecoverSchool(schoolRollStudent);
        }else if(SchoolChangeType.ScClass.getCode().equals(scholarshipSource)){
            scClass(schoolRollStudent);
        }
        return true;
    }

    @Override
    public Page<SchoolRollStudentPageVo> getPage(Page<SchoolRollStudentPageVo> page, SchoolRollStudentPageDto dto) {
        Page<SchoolRollStudentPageVo> result = schoolRollStudentMapper.getPage(page, dto);
        return result;
    }

    @Override
    public SchoolRollStudentVo getInfo(Long id) {
        return schoolRollStudentMapper.getInfo(id);
    }


    /**
     * 休学:禁用学生账号
     */
    void scOutSchool(SchoolRollStudent schoolRollStudent){
        if(ObjectUtil.isNull(schoolRollStudent.getIsCancelAccount()) || schoolRollStudent.getIsCancelAccount() != 1){
            return;
        }
        XjrUser xjrUser = xjrUserMapper.selectOne(Wrappers.lambdaQuery(XjrUser.class).eq(XjrUser::getId, schoolRollStudent.getStudentId()));
        xjrUser.setEnabledMark(EnabledMark.DISABLED.getCode());
        xjrUserMapper.updateById(xjrUser);

        BaseStudentSchoolRoll studentSchoolRoll = baseStudentSchoolRollMapper.selectOne(
            Wrappers.lambdaQuery(BaseStudentSchoolRoll.class).eq(BaseStudentSchoolRoll::getUserId, schoolRollStudent.getStudentId())
        );
        if(ObjectUtil.isNotNull(studentSchoolRoll)){
            studentSchoolRoll.setArchivesStatus(ArchivesStatusEnum.FB2902.getCode());
            baseStudentSchoolRollMapper.updateById(studentSchoolRoll);
        }
    }

    /**
     * 退学:禁用学生账号
     */
    void scOut(SchoolRollStudent schoolRollStudent){
        if(ObjectUtil.isNull(schoolRollStudent.getIsCancelAccount()) || schoolRollStudent.getIsCancelAccount() != 1){
            return;
        }
        XjrUser xjrUser = xjrUserMapper.selectOne(Wrappers.lambdaQuery(XjrUser.class).eq(XjrUser::getId, schoolRollStudent.getStudentId()));
        xjrUser.setEnabledMark(EnabledMark.DISABLED.getCode());
        xjrUserMapper.updateById(xjrUser);

        BaseStudentSchoolRoll studentSchoolRoll = baseStudentSchoolRollMapper.selectOne(
            Wrappers.lambdaQuery(BaseStudentSchoolRoll.class).eq(BaseStudentSchoolRoll::getUserId, schoolRollStudent.getStudentId())
        );
        if(ObjectUtil.isNotNull(studentSchoolRoll)){
            studentSchoolRoll.setArchivesStatus(ArchivesStatusEnum.FB2904.getCode());
            baseStudentSchoolRollMapper.updateById(studentSchoolRoll);
        }
    }

    /**
     * 复学:恢复学生账号并修改班级
     */
    void scRecoverSchool(SchoolRollStudent schoolRollStudent){
        //启用学生账号
        XjrUser xjrUser = xjrUserMapper.selectOne(Wrappers.lambdaQuery(XjrUser.class).eq(XjrUser::getId, schoolRollStudent.getStudentId()));
        xjrUser.setEnabledMark(EnabledMark.ENABLED.getCode());
        xjrUserMapper.updateById(xjrUser);

        //修改学生的年级和班级
        BaseStudentSchoolRoll baseStudentSchoolRoll = baseStudentSchoolRollMapper.selectOne(
            Wrappers.lambdaQuery(BaseStudentSchoolRoll.class).eq(BaseStudentSchoolRoll::getUserId, schoolRollStudent.getStudentId())
        );
        if(ObjectUtil.isNotNull(baseStudentSchoolRoll)){
            baseStudentSchoolRoll.setClassId(schoolRollStudent.getBackClassId());
            baseStudentSchoolRoll.setGradeId(schoolRollStudent.getBackGradeId());
            baseStudentSchoolRoll.setArchivesStatus(ArchivesStatusEnum.FB2901.getCode());
            baseStudentSchoolRollMapper.updateById(baseStudentSchoolRoll);
        }
    }

    /**
     * 转专业(班级)：修改班级id
     */
    void scClass(SchoolRollStudent schoolRollStudent){
        //修改学生的年级和班级
        BaseStudentSchoolRoll baseStudentSchoolRoll = baseStudentSchoolRollMapper.selectOne(
            Wrappers.lambdaQuery(BaseStudentSchoolRoll.class).eq(BaseStudentSchoolRoll::getUserId, schoolRollStudent.getStudentId())
        );
        baseStudentSchoolRoll.setClassId(schoolRollStudent.getBackClassId());
        baseStudentSchoolRoll.setGradeId(schoolRollStudent.getBackGradeId());
        baseStudentSchoolRollMapper.updateById(baseStudentSchoolRoll);
    }

}
