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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.student.entity.BaseStudent;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.entity.SchoolRollTransferStudent;
import com.xjrsoft.module.student.mapper.BaseStudentMapper;
import com.xjrsoft.module.student.mapper.BaseStudentSchoolRollMapper;
import com.xjrsoft.module.student.mapper.SchoolRollTransferStudentMapper;
import com.xjrsoft.module.student.service.ISchoolRollTransferStudentService;
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 SchoolRollTransferStudentServiceImpl extends MPJBaseServiceImpl<SchoolRollTransferStudentMapper, SchoolRollTransferStudent> implements ISchoolRollTransferStudentService {
    private final XjrUserMapper xjrUserMapper;
    private final BaseStudentSchoolRollMapper baseStudentSchoolRollMapper;
    private final BaseStudentMapper baseStudentMapper;
    private final SchoolRollTransferStudentMapper schoolRollTransferStudentMapper;

    /**
     * 1、添加到xjr_user表中
     * 2、添加到base_student_school_roll表中
     * 3、添加到base_student表中
     */
    @Override
    public Boolean dataCache(Long dataId) {
        SchoolRollTransferStudent rollTransferStudent = schoolRollTransferStudentMapper.selectOne(
            Wrappers.lambdaQuery(SchoolRollTransferStudent.class).eq(SchoolRollTransferStudent::getId, dataId)
        );
        XjrUser xjrUser = BeanUtil.toBean(rollTransferStudent, XjrUser.class);
        int userCount = xjrUserMapper.insert(xjrUser);

        if(userCount > 0){
            BaseStudentSchoolRoll schoolRoll = BeanUtil.toBean(rollTransferStudent, BaseStudentSchoolRoll.class);
            schoolRoll.setUserId(xjrUser.getId());
            baseStudentSchoolRollMapper.insert(schoolRoll);

            BaseStudent student = BeanUtil.toBean(rollTransferStudent, BaseStudent.class);
            student.setUserId(xjrUser.getId());
            baseStudentMapper.insert(student);
        }

        return true;
    }
}
