package com.ruoyi.kaohe.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.kaohe.domain.KhPcEmp;
import com.ruoyi.kaohe.mapper.KhPcEmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kaohe.mapper.KhPingceMapper;
import com.ruoyi.kaohe.domain.KhPingce;
import com.ruoyi.kaohe.service.IKhPingceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 考核评测Service业务层处理
 * 
 * @author hs
 * @date 2025-06-17
 */
@Service
public class KhPingceServiceImpl implements IKhPingceService 
{
    @Autowired
    private KhPingceMapper khPingceMapper;
    @Autowired
    private KhPcEmpMapper khPcEmpMapper;

    /**
     * 查询考核评测
     * 
     * @param id 考核评测主键
     * @return 考核评测
     */
    @Override
    public KhPingce selectKhPingceById(Long id)
    {
        return khPingceMapper.selectKhPingceById(id);
    }

    /**
     * 查询考核评测列表
     * 
     * @param khPingce 考核评测
     * @return 考核评测
     */
    @Override
    public List<KhPingce> selectKhPingceList(KhPingce khPingce)
    {
        return khPingceMapper.selectKhPingceList(khPingce);
    }

    /**
     * 新增考核评测
     * 
     * @param khPingce 考核评测
     * @return 结果
     */
    @Override
    public int insertKhPingce(KhPingce khPingce)
    {
        khPingce.setCreateTime(DateUtils.getNowDate());
        return khPingceMapper.insertKhPingce(khPingce);
    }

    /**
     * 修改考核评测
     * 
     * @param khPingce 考核评测
     * @return 结果
     */
    @Override
    public int updateKhPingce(KhPingce khPingce)
    {
        khPingce.setUpdateTime(DateUtils.getNowDate());
        return khPingceMapper.updateKhPingce(khPingce);
    }

    /**
     * 批量删除考核评测
     * 
     * @param ids 需要删除的考核评测主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteKhPingceByIds(Long[] ids)
    {
        int r = khPingceMapper.deleteKhPingceByIds(ids);
        for (Long id : ids) {
            //删除原来关联
            KhPcEmp query = new KhPcEmp();
            query.setPcId(id);
            List<KhPcEmp> temItems = khPcEmpMapper.selectKhPcEmpList(query);
            List<Long> pcEmpIds = temItems.stream().map(KhPcEmp::getId).collect(Collectors.toList());
            if (pcEmpIds != null && pcEmpIds.size() > 0) {
                khPcEmpMapper.deleteKhPcEmpByIds(pcEmpIds.toArray(new Long[pcEmpIds.size()]));
            }
        }
        return r;
    }

    /**
     * 删除考核评测信息
     * 
     * @param id 考核评测主键
     * @return 结果
     */
    @Override
    public int deleteKhPingceById(Long id)
    {
        return khPingceMapper.deleteKhPingceById(id);
    }
}
