package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TbSchool;
import com.ruoyi.system.domain.TbSchoolMajor;
import com.ruoyi.system.mapper.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.TbEnrollmentTime;
import com.ruoyi.system.service.ITbEnrollmentTimeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 入学时间Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-05
 */
@Service
public class TbEnrollmentTimeServiceImpl implements ITbEnrollmentTimeService 
{
    @Autowired
    private TbEnrollmentTimeMapper tbEnrollmentTimeMapper;

    @Autowired
    private TbSchoolMapper tbSchoolMapper;

    @Autowired
    private TbSchoolMajorMapper tbSchoolMajorMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询入学时间
     * 
     * @param id 入学时间主键
     * @return 入学时间
     */
    @Override
    public TbEnrollmentTime selectTbEnrollmentTimeById(Long id)
    {
        TbEnrollmentTime tb=tbEnrollmentTimeMapper.selectTbEnrollmentTimeById(id);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        convertUserIDToString(tb);
        convertCheckStatusToString(tb,checkStatusList);
        return tb;
    }

    /**
     * 查询入学时间列表
     *
     * @param tbEnrollmentTime 入学时间
     * @return 入学时间
     */
    @Override
    public List<TbEnrollmentTime> selectTbEnrollmentTimeList(TbEnrollmentTime tbEnrollmentTime)
    {
        List<TbEnrollmentTime> tbEnrollmentTimes = tbEnrollmentTimeMapper.selectTbEnrollmentTimeList(tbEnrollmentTime);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        tbEnrollmentTimes.stream().filter(tbEnrollmentTime1 -> {
            convertUserIDToString(tbEnrollmentTime1);
            convertCheckStatusToString(tbEnrollmentTime1,checkStatusList);
            return true;
        }).collect(Collectors.toList());
        return tbEnrollmentTimes;
    }

    /**
     * @Author : fanxiaolong
     * @Description : 查询未通过审核或待审核的记录
     * @DateTime : 2022/9/7 16:03
     * @param : tbEnrollmentTime
     * @return : java.util.List<com.ruoyi.system.domain.TbEnrollmentTime>
     */
    @Override
    public List<TbEnrollmentTime> selectEnrollmentTimeNotPassList(TbEnrollmentTime tbEnrollmentTime){
        List<TbEnrollmentTime> tbEnrollmentTimes = tbEnrollmentTimeMapper.selectEnrollmentTimeNotPassList(tbEnrollmentTime);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        tbEnrollmentTimes.stream().filter(tbEnrollmentTime1 -> {
            convertUserIDToString(tbEnrollmentTime1);
            convertCheckStatusToString(tbEnrollmentTime1,checkStatusList);
            return true;
        }).collect(Collectors.toList());
        return tbEnrollmentTimes;
    }

    /**
     * @Author : fanxiaolong
     * @Description :  转换userID为loginName
     * @DateTime : 2022/9/5 10:48
     * @param : tbMaterialDemo
     * @return : void
     */
    private void convertUserIDToString(TbEnrollmentTime tbMaterialDemo){

        SysUser su=null;
        su=sysUserMapper.selectUserById(tbMaterialDemo.getCreator());
        tbMaterialDemo.setCreatorDesc(su.getLoginName());
        su=sysUserMapper.selectUserById(tbMaterialDemo.getModifiedBy());
        if(ObjectUtils.isNotEmpty(su)){
            tbMaterialDemo.setModifiedByDesc(su.getLoginName());
        }
        su=sysUserMapper.selectUserById(tbMaterialDemo.getChecker());
        if(ObjectUtils.isNotEmpty(su)){
            tbMaterialDemo.setCheckerDesc(su.getLoginName());
        }
    }

    /**
     * @Author : fanxiaolong
     * @Description :  材料审核状态转换成文字
     * @DateTime : 2022/9/2 16:51
     * @param : tbMaterialDemo
     * @param : dictDataList
     * @return : void
     */
    private void convertCheckStatusToString(TbEnrollmentTime tbMaterialDemo, List<SysDictData> dictDataList){
        dictDataList.stream().forEach(sysDictData -> {
            if(StringUtils.compare(String.valueOf(tbMaterialDemo.getCheckStatus()),sysDictData.getDictValue())==0){
                tbMaterialDemo.setCheckStatusDesc(sysDictData.getDictLabel());
            }
        });
    }

    /**
     * 新增入学时间
     * 
     * @param tbEnrollmentTime 入学时间
     * @return 结果
     */
    @Override
    public int insertTbEnrollmentTime(TbEnrollmentTime tbEnrollmentTime)
    {
        tbEnrollmentTime.setCreateTime(DateUtils.getNowDate());
        return tbEnrollmentTimeMapper.insertTbEnrollmentTime(tbEnrollmentTime);
    }

    /**
     * 修改入学时间
     * 
     * @param tbEnrollmentTime 入学时间
     * @return 结果
     */
    @Override
    public int updateTbEnrollmentTime(TbEnrollmentTime tbEnrollmentTime)
    {
        tbEnrollmentTime.setUpdateTime(DateUtils.getNowDate());
        return tbEnrollmentTimeMapper.updateTbEnrollmentTime(tbEnrollmentTime);
    }

    @Override
    public int updateCheckStatus(TbEnrollmentTime tbEnrollmentTime){
        return tbEnrollmentTimeMapper.updateCheckStatus(tbEnrollmentTime);
    }

    /**
     * 批量删除入学时间
     * 
     * @param ids 需要删除的入学时间主键
     * @return 结果
     */
    @Override
    public int deleteTbEnrollmentTimeByIds(String ids)
    {
        return tbEnrollmentTimeMapper.deleteTbEnrollmentTimeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入学时间信息
     * 
     * @param id 入学时间主键
     * @return 结果
     */
    @Override
    public int deleteTbEnrollmentTimeById(Long id)
    {
        return tbEnrollmentTimeMapper.deleteTbEnrollmentTimeById(id);
    }

    /**
     * @Author : fanxiaolong
     * @Description : 查询学校校区
     * @DateTime : 2022/9/7 10:46
     * @param : id
     * @return : java.util.List<java.lang.String>
     */
    @Override
    public List<TbSchool> selectSchoolCampusByID(Long id){
        return tbSchoolMapper.selectSchoolCampusByID(id);
    }

    @Override
    public List<TbSchoolMajor> selectSchoolMajorBySchoolId(Long id){
        return tbSchoolMajorMapper.selectSchoolMajorBySchoolId(id);
    }

    @Override
    public List<TbSchoolMajor> selectDegreeBySchooIdAndMajorName(TbSchoolMajor tbSchoolMajor){
        return tbSchoolMajorMapper.selectDegreeBySchooIdAndMajorName(tbSchoolMajor);
    }
}
