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.SecurityUtils;
import com.ruoyi.system.domain.ProcessConfiguration;
import com.ruoyi.system.mapper.ProcessConfigurationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CardReplacementRecordMapper;
import com.ruoyi.system.domain.CardReplacementRecord;
import com.ruoyi.system.service.ICardReplacementRecordService;

/**
 * 补卡记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-08
 */
@Service
public class CardReplacementRecordServiceImpl implements ICardReplacementRecordService 
{
    @Autowired
    private CardReplacementRecordMapper cardReplacementRecordMapper;

    @Autowired
    private ProcessConfigurationMapper processConfigurationMapper;

    /**
     * 查询补卡记录
     * 
     * @param id 补卡记录主键
     * @return 补卡记录
     */
    @Override
    public CardReplacementRecord selectCardReplacementRecordById(Long id)
    {
        return cardReplacementRecordMapper.selectCardReplacementRecordById(id);
    }

    /**
     * 查询补卡记录列表
     * 
     * @param cardReplacementRecord 补卡记录
     * @return 补卡记录
     */
    @Override
    public List<CardReplacementRecord> selectCardReplacementRecordList(CardReplacementRecord cardReplacementRecord)
    {
        return cardReplacementRecordMapper.selectCardReplacementRecordList(cardReplacementRecord);
    }

    /**
     * 新增补卡记录
     * 
     * @param cardReplacementRecord 补卡记录
     * @return 结果
     */
    @Override
    public int insertCardReplacementRecord(CardReplacementRecord cardReplacementRecord)
    {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        cardReplacementRecord.setUserId(user.getUserId());
        cardReplacementRecord.setUserName(user.getNickName());
        cardReplacementRecord.setDeptId(user.getDeptId());
        cardReplacementRecord.setDeptName(user.getDept().getDeptName());
        return cardReplacementRecordMapper.insertCardReplacementRecord(cardReplacementRecord);
    }

    /**
     * 修改补卡记录
     * 
     * @param cardReplacementRecord 补卡记录
     * @return 结果
     */
    @Override
    public int updateCardReplacementRecord(CardReplacementRecord cardReplacementRecord)
    {
        return cardReplacementRecordMapper.updateCardReplacementRecord(cardReplacementRecord);
    }

    /**
     * 批量删除补卡记录
     * 
     * @param ids 需要删除的补卡记录主键
     * @return 结果
     */
    @Override
    public int deleteCardReplacementRecordByIds(Long[] ids)
    {
        return cardReplacementRecordMapper.deleteCardReplacementRecordByIds(ids);
    }

    /**
     * 删除补卡记录信息
     * 
     * @param id 补卡记录主键
     * @return 结果
     */
    @Override
    public int deleteCardReplacementRecordById(Long id)
    {
        return cardReplacementRecordMapper.deleteCardReplacementRecordById(id);
    }

    @Override
    public int putPass(CardReplacementRecord cardReplacementRecord) {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        cardReplacementRecord.setExaminersId(user.getUserId());
        cardReplacementRecord.setExaminersName(user.getNickName());
        return cardReplacementRecordMapper.updateCardReplacementRecord(cardReplacementRecord);
    }

    @Override
    public List<CardReplacementRecord> presentList(CardReplacementRecord cardReplacementRecord) {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //获取当前人员补卡配置所在的部门id
        ProcessConfiguration processConfiguration = new ProcessConfiguration();
        processConfiguration.setUserId(user.getUserId());
        List<ProcessConfiguration> list = processConfigurationMapper.selectProcessConfigurationList(processConfiguration);
        List<Long> deptIds = list.stream().map(ProcessConfiguration::getDeptId).collect(Collectors.toList());
        cardReplacementRecord.setDeptIds(deptIds);
        return cardReplacementRecordMapper.selectCardReplacementRecordList(cardReplacementRecord);
    }
}
