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.StringUtils;
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.TbSchool;
import com.ruoyi.system.service.ITbSchoolService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学校信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-29
 */
@Service
public class TbSchoolServiceImpl implements ITbSchoolService 
{
    @Autowired
    private TbSchoolMapper tbSchoolMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TbSchoolMajorMapper tbSchoolMajorMapper;

    @Autowired
    private TbSchoolEntranceRequirementsMapper tbSchoolEntranceRequirementsMapper;

    @Autowired
    private TbSchoolFacilitiesMapper tbSchoolFacilitiesMapper;

    @Autowired
    private TbSchoolPartnersMapper tbSchoolPartnersMapper;

    @Autowired
    private TbSchoolUploadMaterialMapper tbSchoolUploadMaterialMapper;

    @Autowired
    private TbSchoolAtlasMapper tbSchoolAtlasMapper;

    @Autowired
    private TbEnrollmentTimeMapper tbEnrollmentTimeMapper;

    /**
     * 查询学校信息
     * 
     * @param id 学校信息主键
     * @return 学校信息
     */
    @Override
    public TbSchool selectTbSchoolById(Long id)
    {
        TbSchool tbSchool=tbSchoolMapper.selectTbSchoolById(id);
        List<SysDictData> dictDataList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_SCHOOL_TYPE);
        convertSchoolTypeToString(tbSchool,dictDataList);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        convertCheckStatusToString(tbSchool,checkStatusList);
        convertUserIDToString(tbSchool);
        return tbSchool;
    }

    /**
     * 查询学校信息列表
     * 
     * @param tbSchool 学校信息
     * @return 学校信息
     */
    @Override
    public List<TbSchool> selectTbSchoolList(TbSchool tbSchool)
    {
        List<TbSchool> list=tbSchoolMapper.selectTbSchoolList(tbSchool);
        List<SysDictData> dictDataList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_SCHOOL_TYPE);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        list.stream().filter(tbSchool1 -> {
            convertSchoolTypeToString(tbSchool1,dictDataList);
            convertCheckStatusToString(tbSchool1,checkStatusList);
            convertUserIDToString(tbSchool1);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * @Author : fanxiaolong
     * @Description : 查询所有未通过审核或待审核的记录
     * @DateTime : 2022/9/2 17:07
     * @param : tbSchool
     * @return : java.util.List<com.ruoyi.system.domain.TbSchool>
     */
    @Override
    public List<TbSchool> selectTbSchoolNotPassList(TbSchool tbSchool){

        List<TbSchool> list=tbSchoolMapper.selectTbSchoolNotPassList(tbSchool);
        List<SysDictData> dictDataList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_SCHOOL_TYPE);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        list.stream().filter(tbSchool1 -> {
            convertSchoolTypeToString(tbSchool1,dictDataList);
            convertCheckStatusToString(tbSchool1,checkStatusList);
            convertUserIDToString(tbSchool1);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * @Author : fanxiaolong
     * @Description : 学校类型转换成文字
     * @DateTime : 2022/9/2 16:53
     * @param :
     * @return : void
     */
    private void convertSchoolTypeToString(TbSchool tbSchool,List<SysDictData> dictDataList){
        dictDataList.stream().forEach(sysDictData -> {
            if(StringUtils.compare(String.valueOf(tbSchool.getSchoolType()),sysDictData.getDictValue())==0){
                tbSchool.setSchoolTypeDesc(sysDictData.getDictLabel());
            }
        });
    }

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

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

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

    @Override
    public List<TbSchool> selectAllTbSchoolList(){
        List<TbSchool> list=tbSchoolMapper.selectAllTbSchoolList();
        list.stream().filter(s -> {
            s.setSchoolCnName(s.getSchoolCnName()+"("+s.getSchoolCampus()+")");
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增学校信息
     * 
     * @param tbSchool 学校信息
     * @return 结果
     */
    @Override
    public int insertTbSchool(TbSchool tbSchool)
    {
        return tbSchoolMapper.insertTbSchool(tbSchool);
    }

    /**
     * 修改学校信息
     * 
     * @param tbSchool 学校信息
     * @return 结果
     */
    @Override
    public int updateTbSchool(TbSchool tbSchool)
    {
        return tbSchoolMapper.updateTbSchool(tbSchool);
    }

    /**
     * @Author : fanxiaolong
     * @Description : 更新审核状态和结果
     * @DateTime : 2022/9/2 15:49
     * @param : tbSchool
     * @return : int
     */
    @Override
    public int updateCheckStatus(TbSchool tbSchool){
        return tbSchoolMapper.updateCheckStatus(tbSchool);
    }

    /**
     * 批量删除学校信息
     * 
     * @param ids 需要删除的学校信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTbSchoolByIds(String ids)
    {
        tbSchoolMapper.deleteTbPerCollectBySchoolIds(Convert.toStrArray(ids));
        tbSchoolMapper.deleteTbPerCollectByMajorIds(Convert.toStrArray(ids));
        tbSchoolMajorMapper.deleteTbSchoolMajorBySchoolIds(Convert.toStrArray(ids));
        tbSchoolEntranceRequirementsMapper.deleteTbSchoolEntranceRequirementsBySchoolIds(Convert.toStrArray(ids));
        tbSchoolFacilitiesMapper.deleteTbSchoolFacilitiesBySchoolIds(Convert.toStrArray(ids));
        tbSchoolPartnersMapper.deleteTbSchoolPartnersBySchoolIds(Convert.toStrArray(ids));
        tbSchoolUploadMaterialMapper.deleteTbSchoolUploadMaterialBySchoolIds(Convert.toStrArray(ids));
        tbSchoolAtlasMapper.deleteTbSchoolAtlasBySchoolIds(Convert.toStrArray(ids));
        tbEnrollmentTimeMapper.deleteTbEnrollmentTimeBySchoolIds(Convert.toStrArray(ids));
        return tbSchoolMapper.deleteTbSchoolByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学校信息信息
     * 
     * @param id 学校信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTbSchoolById(Long id)
    {
        tbSchoolMajorMapper.deleteTbSchoolMajorBySchoolId(id);
        tbSchoolEntranceRequirementsMapper.deleteTbSchoolEntranceRequirementsBySchoolId(id);
        tbSchoolFacilitiesMapper.deleteTbSchoolFacilitiesBySchoolId(id);
        tbSchoolPartnersMapper.deleteTbSchoolPartnersBySchoolId(id);
        tbSchoolUploadMaterialMapper.deleteTbSchoolUploadMaterialBySchoolId(id);
        tbSchoolAtlasMapper.deleteTbSchoolAtlasBySchoolId(id);
        tbEnrollmentTimeMapper.deleteTbEnrollmentTimeBySchoolId(id);
        tbSchoolMapper.deleteTbPerCollectBySchoolId(id);
        tbSchoolMapper.deleteTbPerCollectByMajorId(id);
        return tbSchoolMapper.deleteTbSchoolById(id);
    }
}
