package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.vo.FormalTeacherClassVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FormalTeacherClassMapper;
import com.ruoyi.common.core.domain.entity.FormalTeacherClass;
import com.ruoyi.system.service.IFormalTeacherClassService;

/**
 * 老师-班级（学科）(审核通过之后数据)Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-30
 */
@Service
public class FormalTeacherClassServiceImpl implements IFormalTeacherClassService 
{
    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;

    /**
     * 查询老师-班级（学科）(审核通过之后数据)
     * 
     * @param id 老师-班级（学科）(审核通过之后数据)主键
     * @return 老师-班级（学科）(审核通过之后数据)
     */
    @Override
    public FormalTeacherClass selectFormalTeacherClassById(Long id)
    {
        return formalTeacherClassMapper.selectFormalTeacherClassById(id);
    }

    /**
     * 查询老师-班级（学科）(审核通过之后数据)列表
     * 
     * @param formalTeacherClass 老师-班级（学科）(审核通过之后数据)
     * @return 老师-班级（学科）(审核通过之后数据)
     */
    @Override
    public List<FormalTeacherClass> selectFormalTeacherClassList(FormalTeacherClass formalTeacherClass)
    {
        return formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
    }

    /**
     * 新增老师-班级（学科）(审核通过之后数据)
     * 
     * @param formalTeacherClass 老师-班级（学科）(审核通过之后数据)
     * @return 结果
     */
    @Override
    public int insertFormalTeacherClass(FormalTeacherClass formalTeacherClass)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        formalTeacherClass.setTeacherId(user.getUserId());
        formalTeacherClass.setTeacherName(user.getNickName());
        formalTeacherClass.setCreateTime(DateUtils.getNowDate());
        return formalTeacherClassMapper.insertFormalTeacherClass(formalTeacherClass);
    }

    /**
     * 修改老师-班级（学科）(审核通过之后数据)
     * 
     * @param formalTeacherClass 老师-班级（学科）(审核通过之后数据)
     * @return 结果
     */
    @Override
    public int updateFormalTeacherClass(FormalTeacherClass formalTeacherClass)
    {
        formalTeacherClass.setUpdateTime(DateUtils.getNowDate());
        return formalTeacherClassMapper.updateFormalTeacherClass(formalTeacherClass);
    }

    /**
     * 批量删除老师-班级（学科）(审核通过之后数据)
     * 
     * @param ids 需要删除的老师-班级（学科）(审核通过之后数据)主键
     * @return 结果
     */
    @Override
    public int deleteFormalTeacherClassByIds(Long[] ids)
    {
        return formalTeacherClassMapper.deleteFormalTeacherClassByIds(ids);
    }

    /**
     * 删除老师-班级（学科）(审核通过之后数据)信息
     * 
     * @param id 老师-班级（学科）(审核通过之后数据)主键
     * @return 结果
     */
    @Override
    public int deleteFormalTeacherClassById(Long id)
    {
        return formalTeacherClassMapper.deleteFormalTeacherClassById(id);
    }

    @Override
    public List<FormalTeacherClass> getTeacherClass(String teacherId) {
        return formalTeacherClassMapper.getTeacherClass(teacherId);
    }

    @Override
    public AjaxResult updateFormalTeacherClassList(FormalTeacherClassVo formalTeacherClassVo) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<FormalTeacherClass> pormalTeacherClassList = formalTeacherClassVo.getPormalTeacherClassList();
        if(pormalTeacherClassList==null || pormalTeacherClassList.size()<=0){
            return AjaxResult.error("参数错误");
        }
        //先删除
        formalTeacherClassMapper.deleteFormalTeacherClassByTeacherId(user.getUserId());
        for (FormalTeacherClass formalTeacherClass : pormalTeacherClassList) {
            formalTeacherClass.setTeacherId(user.getUserId());
            formalTeacherClass.setTeacherName(user.getNickName());
            formalTeacherClass.setUpdateTime(DateUtils.getNowDate());
            formalTeacherClass.setCreateTime(DateUtils.getNowDate());
            formalTeacherClassMapper.insertFormalTeacherClass(formalTeacherClass);
        }
        return AjaxResult.success();
    }
}
