package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ClassUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.StudentInfo;
import com.ruoyi.system.mapper.StudentInfoMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StudentLiveOldMapper;
import com.ruoyi.system.domain.StudentLiveOld;
import com.ruoyi.system.service.IStudentLiveOldService;

/**
 * 学生生活历史信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class StudentLiveOldServiceImpl implements IStudentLiveOldService 
{
    @Autowired
    private StudentLiveOldMapper studentLiveOldMapper;
    @Autowired
    private StudentInfoMapper studentInfoMapper;

    /**
     * 查询学生生活历史信息
     * 
     * @param liveId 学生生活历史信息主键
     * @return 学生生活历史信息
     */
    @Override
    public StudentLiveOld selectStudentLiveOldByLiveId(Long liveId)
    {
        return studentLiveOldMapper.selectStudentLiveOldByLiveId(liveId);
    }

    /**
     * 查询学生生活历史信息列表
     * 
     * @param studentLiveOld 学生生活历史信息
     * @return 学生生活历史信息
     */
    @Override
    public List<StudentLiveOld> selectStudentLiveOldList(StudentLiveOld studentLiveOld)
    {
        return studentLiveOldMapper.selectStudentLiveOldList(studentLiveOld);
    }

    /**
     * 新增学生生活历史信息
     * 
     * @param studentLiveOld 学生生活历史信息
     * @return 结果
     */
    @SneakyThrows
    @Override
    public int insertStudentLiveOld(StudentLiveOld studentLiveOld)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        StudentInfo student = studentInfoMapper.selectStudentInfoByStudentId(studentLiveOld.getStudentId());
        if(student==null){
            throw new ServiceException("请先填写学生档案信息");
        }
        student.setHeight(studentLiveOld.getHeight());
        student.setWeight(studentLiveOld.getWeight());
        student.setUpdateTime(DateUtils.getNowDate());
        studentInfoMapper.updateStudentInfo(student);

        ClassUtils.copyProperties(student, studentLiveOld);
        studentLiveOld.setCreateTime(DateUtils.getNowDate());
        studentLiveOld.setUpdateTime(DateUtils.getNowDate());
        studentLiveOld.setCreateBy(user.getUserName());
        return studentLiveOldMapper.insertStudentLiveOld(studentLiveOld);
    }

    /**
     * 修改学生生活历史信息
     * 
     * @param studentLiveOld 学生生活历史信息
     * @return 结果
     */
    @Override
    public int updateStudentLiveOld(StudentLiveOld studentLiveOld)
    {
        studentLiveOld.setUpdateTime(DateUtils.getNowDate());
        return studentLiveOldMapper.updateStudentLiveOld(studentLiveOld);
    }

    /**
     * 批量删除学生生活历史信息
     * 
     * @param liveIds 需要删除的学生生活历史信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentLiveOldByLiveIds(Long[] liveIds)
    {
        return studentLiveOldMapper.deleteStudentLiveOldByLiveIds(liveIds);
    }

    /**
     * 删除学生生活历史信息信息
     * 
     * @param liveId 学生生活历史信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentLiveOldByLiveId(Long liveId)
    {
        return studentLiveOldMapper.deleteStudentLiveOldByLiveId(liveId);
    }
}
