package com.ruoyi.cs.service.impl;

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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.cs.domain.CsUserInfo;
import com.ruoyi.cs.domain.dto.CsBusinessMappingDTO;
import com.ruoyi.cs.domain.vo.CsBusinessMappingVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsBusinessMappingMapper;
import com.ruoyi.cs.domain.CsBusinessMapping;
import com.ruoyi.cs.service.ICsBusinessMappingService;

import javax.annotation.Resource;

/**
 * 业务映射Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class CsBusinessMappingServiceImpl implements ICsBusinessMappingService 
{
    @Resource
    private CsBusinessMappingMapper csBusinessMappingMapper;

    /**
     * 查询业务映射
     * 
     * @param mappingId 业务映射主键
     * @return 业务映射
     */
    @Override
    public CsBusinessMapping selectCsBusinessMappingByMappingId(String mappingId)
    {
        return csBusinessMappingMapper.selectCsBusinessMappingByMappingId(mappingId);
    }

    /**
     * 查询业务映射列表
     * 
     * @param csBusinessMapping 业务映射
     * @return 业务映射
     */
    @Override
    public List<CsBusinessMapping> selectCsBusinessMappingList(CsBusinessMapping csBusinessMapping)
    {
        return csBusinessMappingMapper.selectCsBusinessMappingList(csBusinessMapping);
    }

    /**
     * 新增业务映射
     * 
     * @param csBusinessMapping 业务映射
     * @return 结果
     */
    @Override
    public int insertCsBusinessMapping(CsBusinessMapping csBusinessMapping)
    {
        return csBusinessMappingMapper.insertCsBusinessMapping(csBusinessMapping);
    }

    /**
     * 修改业务映射
     * 
     * @param csBusinessMapping 业务映射
     * @return 结果
     */
    @Override
    public int updateCsBusinessMapping(CsBusinessMapping csBusinessMapping)
    {
        return csBusinessMappingMapper.updateCsBusinessMapping(csBusinessMapping);
    }

    /**
     * 批量删除业务映射
     * 
     * @param mappingIds 需要删除的业务映射主键
     * @return 结果
     */
    @Override
    public int deleteCsBusinessMappingByMappingIds(String[] mappingIds)
    {
        return csBusinessMappingMapper.deleteCsBusinessMappingByMappingIds(mappingIds);
    }

    /**
     * 删除业务映射信息
     * 
     * @param mappingId 业务映射主键
     * @return 结果
     */
    @Override
    public int deleteCsBusinessMappingByMappingId(String mappingId)
    {
        return csBusinessMappingMapper.deleteCsBusinessMappingByMappingId(mappingId);
    }


    /**
     * 资源申请_批量新增业务映射信息
     * @param list 映射信息列表
     * @param applyId 申请资源id
     * @param createAt 创建时间
     * @return
     */
    @Override
    public int batchInsertBusinessMapping(List<CsBusinessMappingDTO> list, String applyId, Date createAt) {
        List<CsBusinessMapping> mappingList = list.stream()
                .map(mapping ->{
                    CsBusinessMapping add = new CsBusinessMapping();
                    add = BeanUtil.toBean(mapping,CsBusinessMapping.class);
                    add.setMappingId(IdUtil.getSnowflakeNextIdStr());
                    add.setResId(applyId);
                    add.setResType("0");
                    add.setStatus("0");
                    add.setCreateAt(createAt);
                    add.setDelFlag("0");
                    return add;
                }) // 转换对象类型
                .collect(Collectors.toList());

        //批量新增
        return csBusinessMappingMapper.batchInsertBusinessMapping(mappingList);
    }

    /**
     * 查询业务映射列表
     * @param resId 业务映射相关资源ID
     * @param resType 相关资源类型：0-资源，1-工单，2-
     * @return
     */
    @Override
    public List<CsBusinessMappingVO> selectApplyBusinessMappingList(String resId, String resType) {
        List<CsBusinessMapping> csBusinessMappings = csBusinessMappingMapper.selectApplyBusinessMappingList(resId, resType);
        List<CsBusinessMappingVO> mappingList = csBusinessMappings.stream()
                .map(mapping ->{
                    CsBusinessMappingVO mappingVO = new CsBusinessMappingVO();
                    mappingVO = BeanUtil.toBean(mapping,CsBusinessMappingVO.class);
                    return mappingVO;
                }) // 转换对象类型
                .collect(Collectors.toList());
        return mappingList;
    }

    /**
     * 批量删除申请的业务映射信息
     * @param applyId 申请资源id
     * @return
     */
    @Override
    public int batchCoverBusinessMapping(List<CsBusinessMappingDTO> list, String applyId, Date createAt, String resType) {
        int delMappings = csBusinessMappingMapper.batchDeleteBusinessMapping(applyId, resType);
        return  batchInsertBusinessMapping(list, applyId, createAt);
    }

}
