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.SysDictDataMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbSchoolMajorMapper;
import com.ruoyi.system.domain.TbSchoolMajor;
import com.ruoyi.system.service.ITbSchoolMajorService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学校专业Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-29
 */
@Service
public class TbSchoolMajorServiceImpl implements ITbSchoolMajorService 
{
    @Autowired
    private TbSchoolMajorMapper tbSchoolMajorMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

//    @Autowired
//    private SysUserMapper sysUserMapper;

    @Autowired
    private CommonServiceImpl commonService;

    /**
     * 查询学校专业
     * 
     * @param id 学校专业主键
     * @return 学校专业
     */
    @Override
    public TbSchoolMajor selectTbSchoolMajorById(Long id)
    {
        TbSchoolMajor tsm=tbSchoolMajorMapper.selectTbSchoolMajorById(id);
        List<SysDictData> degreeList = sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_DEGREE_TYPE);
        List<SysDictData> majorList = sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_MAJOR_TYPE);
        List<SysDictData> checkList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        covertDegreeTypeToString(degreeList, tsm);
        covertMajorTypeToString(majorList, tsm);
        convertNeedTypeToString(tsm);
        convertCheckStatusToString(tsm,checkList);
//        tsm.setNeedMaterial(String.valueOf(tsm.getNeedMaterial().split(",").length));
        return tsm;
    }

    /**
     * 查询学校专业列表
     * 
     * @param tbSchoolMajor 学校专业
     * @return 学校专业
     */
    @Override
    public List<TbSchoolMajor> selectTbSchoolMajorList(TbSchoolMajor tbSchoolMajor)
    {
//        if(!StringUtils.isEmpty(tbSchoolMajor.getSchoolId())){
//            tbSchoolMajor.setSchoolCnName(tbSchoolMajor.getSchoolId());
//            tbSchoolMajor.setSchoolId(null);
//        }
        List<TbSchoolMajor> list=tbSchoolMajorMapper.selectTbSchoolMajorList(tbSchoolMajor);
        List<SysDictData> degreeList = sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_DEGREE_TYPE);
        List<SysDictData> majorList = sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_MAJOR_TYPE);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        list.stream().filter(tbSchoolMajor1 -> {
            covertDegreeTypeToString(degreeList, tbSchoolMajor1);
            covertMajorTypeToString(majorList, tbSchoolMajor1);
            convertNeedTypeToString(tbSchoolMajor1);
            tbSchoolMajor1.setNeedMaterial(String.valueOf(tbSchoolMajor1.getNeedMaterial().split(",").length));
            convertCheckStatusToString(tbSchoolMajor1,checkStatusList);
            convertUserIDToString(tbSchoolMajor1);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * @Author : fanxiaolong
     * @Description :  查询未通过审核或者待审核状态的内容
     * @DateTime : 2022/9/5 17:20
     * @param : tbSchoolMajor
     * @return : java.util.List<com.ruoyi.system.domain.TbSchoolMajor>
     */
    @Override
    public List<TbSchoolMajor> selectTbSchoolMajorNotPassList(TbSchoolMajor tbSchoolMajor){

//        if(!StringUtils.isEmpty(tbSchoolMajor.getSchoolId())){
//            tbSchoolMajor.setSchoolCnName(tbSchoolMajor.getSchoolId());
//            tbSchoolMajor.setSchoolId(null);
//        }
        List<TbSchoolMajor> list=tbSchoolMajorMapper.selectTbSchoolMajorNotPassList(tbSchoolMajor);
        List<SysDictData> degreeList = sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_DEGREE_TYPE);
        List<SysDictData> majorList = sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_MAJOR_TYPE);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        list.stream().filter(tbSchoolMajor1 -> {
//            covertDegreeTypeToString(degreeList, tbSchoolMajor1);
            tbSchoolMajor1.setDegreeTypeDesc(commonService.convertDictToString(tbSchoolMajor1.getDegreeType(),Constants.BUSINESS_DEGREE_TYPE));
//            covertMajorTypeToString(majorList, tbSchoolMajor1);
            tbSchoolMajor1.setMajorTypeDesc(commonService.convertDictToString(String.valueOf(tbSchoolMajor1.getMajorType()),Constants.BUSINESS_MAJOR_TYPE));
            convertNeedTypeToString(tbSchoolMajor1);
            tbSchoolMajor1.setNeedMaterial(String.valueOf(tbSchoolMajor1.getNeedMaterial().split(",").length));
//            convertCheckStatusToString(tbSchoolMajor1,checkStatusList);
            tbSchoolMajor1.setCheckStatusDesc(commonService.convertDictToString(String.valueOf(tbSchoolMajor1.getCheckStatus()),Constants.BUSINESS_CHECK_STATUS));
            convertUserIDToString(tbSchoolMajor1);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * @Author : fanxiaolong
     * @Description :  学位类型转换成文字
     * @DateTime : 2022/8/30 16:09
     * @param : degreeList
     * @param : tbSchoolMajor1
     * @return : void
     */
    private void covertDegreeTypeToString(List<SysDictData> degreeList, TbSchoolMajor tbSchoolMajor1) {

        degreeList.stream().forEach(dd -> {
            if(StringUtils.compare(dd.getDictValue(), String.valueOf(tbSchoolMajor1.getDegreeType()))==0){
                tbSchoolMajor1.setDegreeTypeDesc(dd.getDictLabel());
            }
        });
    }

    /**
     * @Author : fanxiaolong
     * @Description :  专业类型转换成文字
     * @DateTime : 2022/8/30 16:08
     * @param : majorList
     * @param : tbSchoolMajor1
     * @return : void
     */
    private void covertMajorTypeToString(List<SysDictData> majorList, TbSchoolMajor tbSchoolMajor1) {

        majorList.stream().forEach(mm -> {
            if(StringUtils.compare(mm.getDictValue(), String.valueOf(tbSchoolMajor1.getMajorType()))==0){
                tbSchoolMajor1.setMajorTypeDesc(mm.getDictLabel());
            }
        });
    }

    /**
     * @Author : fanxiaolong
     * @Description :  是否需要学历，是否需要面试，是否需要作品集
     * @DateTime : 2022/8/30 16:09
     * @param : tbSchoolMajor1
     * @return : void
     */
    private void convertNeedTypeToString(TbSchoolMajor tbSchoolMajor1){

        if(tbSchoolMajor1.getNeedEducation().equals("0")){
            tbSchoolMajor1.setNeedEducationDesc("不需要");
        }else{
            tbSchoolMajor1.setNeedEducationDesc("需要");
        }
        if(tbSchoolMajor1.getNeedInterview().equals("0")){
            tbSchoolMajor1.setNeedInterviewDesc("不需要");
        }else{
            tbSchoolMajor1.setNeedInterviewDesc("需要");
        }
        if(tbSchoolMajor1.getNeedWorks().equals("0")){
            tbSchoolMajor1.setNeedWorksDesc("不需要");
        }else{
            tbSchoolMajor1.setNeedWorksDesc("需要");
        }
    }

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

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

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

    /**
     * 新增学校专业
     * 
     * @param tbSchoolMajor 学校专业
     * @return 结果
     */
    @Override
    public int insertTbSchoolMajor(TbSchoolMajor tbSchoolMajor)
    {
        return tbSchoolMajorMapper.insertTbSchoolMajor(tbSchoolMajor);
    }

    /**
     * 修改学校专业
     * 
     * @param tbSchoolMajor 学校专业
     * @return 结果
     */
    @Override
    public int updateTbSchoolMajor(TbSchoolMajor tbSchoolMajor)
    {
        return tbSchoolMajorMapper.updateTbSchoolMajor(tbSchoolMajor);
    }

    @Override
    public int updateTbSchoolMajorCheckResult(TbSchoolMajor tbSchoolMajor){
        return tbSchoolMajorMapper.updateTbSchoolMajorCheckResult(tbSchoolMajor);
    }

    /**
     * 批量删除学校专业
     * 
     * @param ids 需要删除的学校专业主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTbSchoolMajorByIds(String ids)
    {
        tbSchoolMajorMapper.deleteTbPerCollectByMajorIds(Convert.toStrArray(ids));
        return tbSchoolMajorMapper.deleteTbSchoolMajorByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学校专业信息
     * 
     * @param id 学校专业主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTbSchoolMajorById(Long id)
    {
        tbSchoolMajorMapper.deleteTbPerCollectByMajorId(id);
        return tbSchoolMajorMapper.deleteTbSchoolMajorById(id);
    }
}
