package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbSchool;
import com.ruoyi.system.domain.TbSchoolMajor;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.TbSchoolMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbSchoolUploadMaterialMapper;
import com.ruoyi.system.domain.TbSchoolUploadMaterial;
import com.ruoyi.system.service.ITbSchoolUploadMaterialService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学校上传的材料Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-10
 */
@Service
public class TbSchoolUploadMaterialServiceImpl implements ITbSchoolUploadMaterialService 
{
    @Autowired
    private TbSchoolUploadMaterialMapper tbSchoolUploadMaterialMapper;

    @Autowired
    private TbSchoolMapper tbSchoolMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询学校上传的材料
     * 
     * @param id 学校上传的材料主键
     * @return 学校上传的材料
     */
    @Override
    public TbSchoolUploadMaterial selectTbSchoolUploadMaterialById(Long id)
    {
        TbSchoolUploadMaterial tsum=tbSchoolUploadMaterialMapper.selectTbSchoolUploadMaterialById(id);
        TbSchool ts=tbSchoolMapper.selectTbSchoolById(tsum.getSchoolId());
        tsum.setSchoolName(ts.getSchoolCnName());
        return tsum;
    }

    /**
     * 查询学校上传的材料列表
     * 
     * @param tbSchoolUploadMaterial 学校上传的材料
     * @return 学校上传的材料
     */
    @Override
    public List<TbSchoolUploadMaterial> selectTbSchoolUploadMaterialList(TbSchoolUploadMaterial tbSchoolUploadMaterial)
    {
        List<TbSchoolUploadMaterial> list=tbSchoolUploadMaterialMapper.selectTbSchoolUploadMaterialList(tbSchoolUploadMaterial);
        list.stream().filter(tsum -> {
            TbSchool ts=tbSchoolMapper.selectTbSchoolById(tsum.getSchoolId());
            tsum.setSchoolName(ts.getSchoolCnName());
            convertUserIDToString(tsum);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

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

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

    /**
     * 新增学校上传的材料
     * 
     * @param tbSchoolUploadMaterial 学校上传的材料
     * @return 结果
     */
    @Override
    public int insertTbSchoolUploadMaterial(TbSchoolUploadMaterial tbSchoolUploadMaterial)
    {
        tbSchoolUploadMaterial.setCreateTime(DateUtils.getNowDate());
        return tbSchoolUploadMaterialMapper.insertTbSchoolUploadMaterial(tbSchoolUploadMaterial);
    }

    /**
     * 修改学校上传的材料
     * 
     * @param tbSchoolUploadMaterial 学校上传的材料
     * @return 结果
     */
    @Override
    public int updateTbSchoolUploadMaterial(TbSchoolUploadMaterial tbSchoolUploadMaterial)
    {
        tbSchoolUploadMaterial.setUpdateTime(DateUtils.getNowDate());
        return tbSchoolUploadMaterialMapper.updateTbSchoolUploadMaterial(tbSchoolUploadMaterial);
    }

    /**
     * 批量删除学校上传的材料
     * 
     * @param ids 需要删除的学校上传的材料主键
     * @return 结果
     */
    @Override
    public int deleteTbSchoolUploadMaterialByIds(String ids)
    {
        return tbSchoolUploadMaterialMapper.deleteTbSchoolUploadMaterialByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学校上传的材料信息
     * 
     * @param id 学校上传的材料主键
     * @return 结果
     */
    @Override
    public int deleteTbSchoolUploadMaterialById(Long id)
    {
        return tbSchoolUploadMaterialMapper.deleteTbSchoolUploadMaterialById(id);
    }
}
