package com.admin.major.service.impl;

import java.util.List;

import com.admin.common.core.service.BaseService;
import com.admin.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.admin.common.utils.SecurityUtils;
import com.admin.common.utils.uuid.IdUtils;
import com.admin.major.mapper.StudentInfoMapper;
import com.admin.major.domain.StudentInfo;
import com.admin.major.service.IStudentInfoService;

/**
 * 学生信息Service业务层处理
 * 
 * @author zewei
 * @date 2022-03-28
 */
@Service
@Slf4j
public class StudentInfoServiceImpl extends BaseService<StudentInfo> implements IStudentInfoService
{
    @Autowired
    private StudentInfoMapper studentInfoMapper;


    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    
    /**
     * 查询学生信息
     * 
     * @param studentId 学生信息主键
     * @return 学生信息
     */
    @Override
    public StudentInfo selectStudentInfoByStudentId(String studentId)
    {
        return studentInfoMapper.selectStudentInfoByStudentId(studentId);
    }

    /**
     * 查询学生信息列表
     * 
     * @param studentInfo 学生信息
     * @return 学生信息
     */
    @Override
    public List<StudentInfo> selectStudentInfoList(StudentInfo studentInfo)
    {
        return studentInfoMapper.selectStudentInfoList(studentInfo);
    }



    /**
     * 查询学生信息
     *
     * @param studentInfo 学生信息
     * @return 学生信息
     */
    @Override
    public StudentInfo selectOneStudentInfo(StudentInfo studentInfo)
    {
        return studentInfoMapper.selectStudentInfoList(studentInfo)
                .stream()
                .findFirst()
                .orElse(null);
    }

    /**
     * 新增学生信息
     *
     * @param studentInfo 学生信息
     * @return 结果
     */

    @Override
    public int insertStudentInfo(StudentInfo studentInfo)
    {
        studentInfo.setStudentId(IdUtils.simpleUUID());
        studentInfo.setCreateTime(DateUtils.getNowDate());
        studentInfo.setCreateUserId(SecurityUtils.getUserId());
        studentInfo.setUpdateUserId(SecurityUtils.getUserId());
        return studentInfoMapper.insertStudentInfo(studentInfo);
    }

    /**
     * 修改学生信息
     * 
     * @param studentInfo 学生信息
     * @return 结果
     */
    @Override
    public int updateStudentInfo(StudentInfo studentInfo)
    {
        studentInfo.setUpdateTime(DateUtils.getNowDate());
        studentInfo.setUpdateUserId(SecurityUtils.getUserId());
        return studentInfoMapper.updateStudentInfo(studentInfo);
    }

    /**
     * 批量删除学生信息
     * 
     * @param studentIds 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentInfoByStudentIds(String[] studentIds)
    {
        return studentInfoMapper.deleteStudentInfoByStudentIds(studentIds);
    }

    /**
     * 删除学生信息信息
     * 
     * @param studentId 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentInfoByStudentId(String studentId)
    {
        return studentInfoMapper.deleteStudentInfoByStudentId(studentId);
    }

    @Override
    public boolean saveBatch(List<StudentInfo> userList) {
        return super.executeBatch(userList, user->{
            if( user.getStudentId() == null ){
                user.setStudentId(IdUtils.simpleUUID());
            }
            log.debug("保存学生数据 {}", user);
            studentInfoMapper.replaceStudentInfo(user);
        });
    }
}
