package com.ruoyi.business.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.test.domain.TestStudents;
import com.ruoyi.business.test.domain.TestStudentsScores;
import com.ruoyi.business.test.mapper.TestStudentsMapper;
import com.ruoyi.business.test.mapper.TestStudentsScoresMapper;
import com.ruoyi.business.test.service.ITestStudentsService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 学生Service业务层处理
 * 
 * @author Duke_yzl
 * @date 2025-02-18
 */
@Service
public class TestStudentsServiceImpl extends MPJBaseServiceImpl<TestStudentsMapper, TestStudents> implements ITestStudentsService
{
    @Resource
    private TestStudentsMapper testStudentsMapper;

    @Resource
    private TestStudentsScoresMapper testStudentsScoresMapper;


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

    /**
     * 查询学生列表
     * 
     * @param testStudents 学生
     * @return 学生
     */
    @Override
    public List<TestStudents> selectTestStudentsList(TestStudents testStudents)
    {
        MPJLambdaWrapper<TestStudents> selectTestStudentsList = new MPJLambdaWrapper<TestStudents>()
                    .selectAll(TestStudents.class)
                    .like(StringUtils.isNotBlank(testStudents.getName()),TestStudents::getName,testStudents.getName())
                    .eq(StringUtils.isNotNull(testStudents.getAge()),TestStudents::getAge,testStudents.getAge())
                    .eq(StringUtils.isNotBlank(testStudents.getGender()),TestStudents::getGender,testStudents.getGender())
                    .eq(StringUtils.isNotBlank(testStudents.getMajor()),TestStudents::getMajor,testStudents.getMajor())
                    .eq(StringUtils.isNotNull(testStudents.getEnrollmentDate()),TestStudents::getEnrollmentDate,testStudents.getEnrollmentDate())
 ;

        return selectJoinList(TestStudents.class, selectTestStudentsList);
    }

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

    /**
     * 修改学生
     * 
     * @param testStudents 学生
     * @return 结果
     */
    @Transactional
    @Override
    public boolean updateTestStudents(TestStudents testStudents)
    {
        testStudentsScoresMapper.delete(new LambdaQueryWrapper<TestStudentsScores>()
                .eq(TestStudentsScores::getStudentId,testStudents.getStudentId()));
        insertTestStudentsScores(testStudents);
        return updateById(testStudents);
    }

    /**
     * 批量删除学生
     * 
     * @param studentIds 需要删除的学生主键
     * @return 结果
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public boolean deleteTestStudentsByStudentIds(Long[] studentIds)
    {
        testStudentsScoresMapper.delete(new LambdaQueryWrapper<TestStudentsScores>()
            .eq(TestStudentsScores::getStudentId,studentIds));
        return removeBatchByIds(Arrays.asList(studentIds));
    }

    /**
     * 删除学生信息
     * 
     * @param studentId 学生主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteTestStudentsByStudentId(Long studentId)
    {
        testStudentsScoresMapper.delete(new LambdaQueryWrapper<TestStudentsScores>()
            .eq(TestStudentsScores::getStudentId,studentId));
        return removeById(studentId);
    }

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