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 com.ruoyi.system.mapper.SysUserMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbMaterialDemoMapper;
import com.ruoyi.system.domain.TbMaterialDemo;
import com.ruoyi.system.service.ITbMaterialDemoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 材料样例Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-31
 */
@Service("materialService")
public class TbMaterialDemoServiceImpl implements ITbMaterialDemoService 
{
    @Autowired
    private TbMaterialDemoMapper tbMaterialDemoMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<TbMaterialDemo> queryAll(){
        return tbMaterialDemoMapper.queryAll();
    }

    /**
     * 查询材料样例
     * 
     * @param id 材料样例主键
     * @return 材料样例
     */
    @Override
    public TbMaterialDemo selectTbMaterialDemoById(Long id)
    {
        TbMaterialDemo tb=tbMaterialDemoMapper.selectTbMaterialDemoById(id);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        convertUserIDToString(tb);
        convertCheckStatusToString(tb,checkStatusList);
        return tb;
    }

    /**
     * 查询材料样例列表
     * 
     * @param tbMaterialDemo 材料样例
     * @return 材料样例
     */
    @Override
    public List<TbMaterialDemo> selectTbMaterialDemoList(TbMaterialDemo tbMaterialDemo)
    {

        List<TbMaterialDemo> list=tbMaterialDemoMapper.selectTbMaterialDemoList(tbMaterialDemo);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        list.stream().filter(para -> {
            convertUserIDToString(para);
            convertCheckStatusToString(para,checkStatusList);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * @Author : fanxiaolong
     * @Description :  查询未通过审核或待审核的材料
     * @DateTime : 2022/9/7 9:09
     * @param : tbMaterialDemo
     * @return : java.util.List<com.ruoyi.system.domain.TbMaterialDemo>
     */
    @Override
    public List<TbMaterialDemo> selectMaterialDemoNotPassList(TbMaterialDemo tbMaterialDemo){

        List<TbMaterialDemo> list=tbMaterialDemoMapper.selectMaterialDemoNotPassList(tbMaterialDemo);
        List<SysDictData> checkStatusList=sysDictDataMapper.selectDictDataByType(Constants.BUSINESS_CHECK_STATUS);
        list.stream().filter(para -> {
            convertUserIDToString(para);
            convertCheckStatusToString(para,checkStatusList);
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * @Author : fanxiaolong
     * @Description :  转换userID为loginName
     * @DateTime : 2022/9/5 10:48
     * @param : tbMaterialDemo
     * @return : void
     */
    private void convertUserIDToString(TbMaterialDemo 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(TbMaterialDemo tbMaterialDemo, List<SysDictData> dictDataList){
        dictDataList.stream().forEach(sysDictData -> {
            if(StringUtils.compare(String.valueOf(tbMaterialDemo.getCheckStatus()),sysDictData.getDictValue())==0){
                tbMaterialDemo.setCheckStatusDesc(sysDictData.getDictLabel());
            }
        });
    }

    /**
     * 新增材料样例
     * 
     * @param tbMaterialDemo 材料样例
     * @return 结果
     */
    @Override
    public int insertTbMaterialDemo(TbMaterialDemo tbMaterialDemo)
    {
        return tbMaterialDemoMapper.insertTbMaterialDemo(tbMaterialDemo);
    }

    /**
     * 修改材料样例
     * 
     * @param tbMaterialDemo 材料样例
     * @return 结果
     */
    @Override
    public int updateTbMaterialDemo(TbMaterialDemo tbMaterialDemo)
    {
        return tbMaterialDemoMapper.updateTbMaterialDemo(tbMaterialDemo);
    }

    /**
     * @Author : fanxiaolong
     * @Description :  修改审核结果和审核状态
     * @DateTime : 2022/9/7 9:16
     * @param : tbMaterialDemo
     * @return : int
     */
    @Override
    public int updateCheckStatus(TbMaterialDemo tbMaterialDemo){

        return tbMaterialDemoMapper.updateCheckStatus(tbMaterialDemo);
    }

    /**
     * 批量删除材料样例
     * 
     * @param ids 需要删除的材料样例主键
     * @return 结果
     */
    @Override
    public int deleteTbMaterialDemoByIds(String ids)
    {
        return tbMaterialDemoMapper.deleteTbMaterialDemoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除材料样例信息
     * 
     * @param id 材料样例主键
     * @return 结果
     */
    @Override
    public int deleteTbMaterialDemoById(Long id)
    {
        return tbMaterialDemoMapper.deleteTbMaterialDemoById(id);
    }
}
