package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TestStudentsScores;
import com.ruoyi.system.mapper.TestStudentsMapper;
import com.ruoyi.system.domain.TestStudents;
import com.ruoyi.system.service.ITestStudentsService;

/**
 * 学生Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-18
 */
@Service
public class TestStudentsServiceImpl implements ITestStudentsService 
{
    @Autowired
    private TestStudentsMapper testStudentsMapper;

    /**
     * 查询学生
     * 
     * @param studentId 学生主键
     * @return 学生
     */
    @Override
    public TestStudents selectTestStudentsByStudentId(Long studentId)
    {
        return testStudentsMapper.selectTestStudentsByStudentId(studentId);
    }

    /**
     * 查询学生列表
     * 
     * @param testStudents 学生
     * @return 学生
     */
    @Override
    public List<TestStudents> selectTestStudentsList(TestStudents testStudents)
    {
        return testStudentsMapper.selectTestStudentsList(testStudents);
    }

    /**
     * 新增学生
     * 
     * @param testStudents 学生
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTestStudents(TestStudents testStudents)
    {
        int rows = testStudentsMapper.insertTestStudents(testStudents);
        insertTestStudentsScores(testStudents);
        return rows;
    }

    /**
     * 修改学生
     * 
     * @param testStudents 学生
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTestStudents(TestStudents testStudents)
    {
        testStudentsMapper.deleteTestStudentsScoresByStudentId(testStudents.getStudentId());
        insertTestStudentsScores(testStudents);
        return testStudentsMapper.updateTestStudents(testStudents);
    }

    /**
     * 批量删除学生
     * 
     * @param studentIds 需要删除的学生主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestStudentsByStudentIds(Long[] studentIds)
    {
        testStudentsMapper.deleteTestStudentsScoresByStudentIds(studentIds);
        return testStudentsMapper.deleteTestStudentsByStudentIds(studentIds);
    }

    /**
     * 删除学生信息
     * 
     * @param studentId 学生主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestStudentsByStudentId(Long studentId)
    {
        testStudentsMapper.deleteTestStudentsScoresByStudentId(studentId);
        return testStudentsMapper.deleteTestStudentsByStudentId(studentId);
    }

    /**
     * 新增学生成绩信息
     * 
     * @param testStudents 学生对象
     */
    public void insertTestStudentsScores(TestStudents testStudents)
    {
        List<TestStudentsScores> testStudentsScoresList = testStudents.getTestStudentsScoresList();
        Long studentId = testStudents.getStudentId();
        if (StringUtils.isNotNull(testStudentsScoresList))
        {
            List<TestStudentsScores> list = new ArrayList<TestStudentsScores>();
            for (TestStudentsScores testStudentsScores : testStudentsScoresList)
            {
                testStudentsScores.setStudentId(studentId);
                list.add(testStudentsScores);
            }
            if (list.size() > 0)
            {
                testStudentsMapper.batchTestStudentsScores(list);
            }
        }
    }
}
