package com.fs.aicall.service.impl;

import com.fs.aicall.domain.CompanyBindAiModel;
import com.fs.aicall.mapper.CompanyBindAiModelMapper;
import com.fs.aicall.service.ICompanyBindAiModelService;
import com.fs.aicall.utils.StringUtils;
import com.fs.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 销售公司与AI模型绑定表Service实现类
 * 
 * @author ruoyi
 * @date 2026-03-20
 */
@Service
public class CompanyBindAiModelServiceImpl implements ICompanyBindAiModelService
{
    @Autowired
    private CompanyBindAiModelMapper companyBindAiModelMapper;

    /**
     * 查询销售公司与AI模型绑定表
     * 
     * @param id 销售公司与AI模型绑定表主键
     * @return 销售公司与AI模型绑定表
     */
    @Override
    public CompanyBindAiModel selectCompanyBindAiModelById(Long id)
    {
        return companyBindAiModelMapper.selectCompanyBindAiModelById(id);
    }

    /**
     * 查询销售公司与AI模型绑定表列表
     * 
     * @param companyBindAiModel 销售公司与AI模型绑定表
     * @return 销售公司与AI模型绑定表集合
     */
    @Override
    public List<CompanyBindAiModel> selectCompanyBindAiModelList(CompanyBindAiModel companyBindAiModel)
    {
        return companyBindAiModelMapper.selectCompanyBindAiModelList(companyBindAiModel);
    }

    /**
     * 新增销售公司与AI模型绑定表
     * 
     * @param companyBindAiModel 销售公司与AI模型绑定表
     * @return 结果
     */
    @Override
    public int insertCompanyBindAiModel(CompanyBindAiModel companyBindAiModel)
    {
        companyBindAiModel.setCreateTime(new Date());
        companyBindAiModel.setUpdateTime(new Date());
        return companyBindAiModelMapper.insertCompanyBindAiModel(companyBindAiModel);
    }

    /**
     * 修改销售公司与AI模型绑定表
     * 
     * @param companyBindAiModel 销售公司与AI模型绑定表
     * @return 结果
     */
    @Override
    public int updateCompanyBindAiModel(CompanyBindAiModel companyBindAiModel)
    {
        companyBindAiModel.setUpdateTime(new Date());
        return companyBindAiModelMapper.updateCompanyBindAiModel(companyBindAiModel);
    }

    /**
     * 删除销售公司与AI模型绑定表
     * 
     * @param id 销售公司与AI模型绑定表主键
     * @return 结果
     */
    @Override
    public int deleteCompanyBindAiModelById(Long id)
    {
        return companyBindAiModelMapper.deleteCompanyBindAiModelById(id);
    }

    /**
     * 批量删除销售公司与AI模型绑定表
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteCompanyBindAiModelByIds(Long[] ids)
    {
        return companyBindAiModelMapper.deleteCompanyBindAiModelByIds(ids);
    }

    /**
     * 根据公司ID查询绑定的模型ID列表
     * 
     * @param companyId 公司ID
     * @return 模型ID列表
     */
    @Override
    public List<Long> selectModelIdsByCompanyId(Long companyId)
    {
        return companyBindAiModelMapper.selectModelIdsByCompanyId(companyId);
    }

    /**
     * 根据模型ID删除绑定关系
     * 
     * @param modelId 模型ID
     * @return 结果
     */
    @Override
    public int deleteCompanyBindAiModelByModelId(Long modelId)
    {
        return companyBindAiModelMapper.deleteCompanyBindAiModelByModelId(modelId);
    }

    /**
     * 批量插入绑定关系
     * 
     * @param companyId 公司ID
     * @param modelIds 模型ID列表
     * @return 结果
     */
    @Override
    public int batchInsertCompanyBindAiModel(Long companyId, List<Long> modelIds)
    {
        return companyBindAiModelMapper.batchInsertCompanyBindAiModel(companyId, modelIds);
    }

    /**
     * 为模型添加公司绑定
     * 
     * @param modelId 模型ID
     * @param companyId 公司ID
     * @return 结果
     */
    @Override
    public int bindCompanyToModel(Long modelId, Long companyId)
    {
        CompanyBindAiModel companyBindAiModel = new CompanyBindAiModel();
        companyBindAiModel.setModelId(modelId);
        companyBindAiModel.setCompanyId(companyId);
        companyBindAiModel.setCreateTime(new Date());
        companyBindAiModel.setUpdateTime(new Date());
        return companyBindAiModelMapper.insertCompanyBindAiModel(companyBindAiModel);
    }

    /**
     * 为模型批量添加公司绑定
     * 
     * @param modelId 模型ID
     * @param companyIds 公司ID列表
     * @return 结果
     */
    @Override
    public int batchBindCompaniesToModel(Long modelId, List<Long> companyIds)
    {
        int result = 0;
        for (Long companyId : companyIds) {
            result += bindCompanyToModel(modelId, companyId);
        }
        return result;
    }

    @Override
    public void deleteBindAiModelByCompanyIdAndModelIds(Long companyId, String modelIds) {
        companyBindAiModelMapper.deleteBindAiModelByCompanyIdAndModelIds(companyId, Convert.toStrArray(modelIds));
    }
}
