package com.fs.company.service.impl;

import com.fs.aicall.domain.param.EditDialogDomain;
import com.fs.aicall.domain.result.EditDialogResult;
import com.fs.aicall.service.AiCallService;
import com.fs.company.domain.CompanyVoiceDialog;
import com.fs.company.mapper.CompanyVoiceDialogMapper;
import com.fs.company.service.ICompanyVoiceDialogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AI外呼话术Service业务层处理
 *
 * @author fs
 * @date 2024-12-04
 */
@Service
public class CompanyVoiceDialogServiceImpl implements ICompanyVoiceDialogService
{
    @Autowired
    private CompanyVoiceDialogMapper companyVoiceDialogMapper;

    @Autowired(required = false)
    private AiCallService aiCallService;

    /**
     * 查询AI外呼话术
     *
     * @param id AI外呼话术ID
     * @return AI外呼话术
     */
    @Override
    public CompanyVoiceDialog selectCompanyVoiceDialogById(Long id)
    {
        return companyVoiceDialogMapper.selectCompanyVoiceDialogById(id);
    }

    /**
     * 查询AI外呼话术列表
     *
     * @param companyVoiceDialog AI外呼话术
     * @return AI外呼话术
     */
    @Override
    public List<CompanyVoiceDialog> selectCompanyVoiceDialogList(CompanyVoiceDialog companyVoiceDialog)
    {
        return companyVoiceDialogMapper.selectCompanyVoiceDialogList(companyVoiceDialog);
    }

    /**
     * 新增AI外呼话术
     *
     * @param companyVoiceDialog AI外呼话术
     * @return 结果
     */
    @Override
    public int insertCompanyVoiceDialog(CompanyVoiceDialog companyVoiceDialog){
        // 调取三方接口添加话术
        EditDialogResult editDialogResult = aiCallService.editDialog(EditDialogDomain.builder()
                .id(-1L)
                .name(companyVoiceDialog.getName())
                .remark(companyVoiceDialog.getRemark()).build());
        // 话术回调
        companyVoiceDialog.setResultId(editDialogResult.getId());
        companyVoiceDialog.setResultName(editDialogResult.getName());
        companyVoiceDialog.setCallNum(editDialogResult.getCallNum());
        return companyVoiceDialogMapper.insertCompanyVoiceDialog(companyVoiceDialog);
    }

    /**
     * 修改AI外呼话术
     *
     * @param companyVoiceDialog AI外呼话术
     * @return 结果
     */
    @Override
    public int updateCompanyVoiceDialog(CompanyVoiceDialog companyVoiceDialog){
        // 调取三方接口修改话术
        aiCallService.editDialog(EditDialogDomain.builder()
                .id(companyVoiceDialog.getResultId())
                .name(companyVoiceDialog.getName())
                .remark(companyVoiceDialog.getRemark()).build());
        return companyVoiceDialogMapper.updateCompanyVoiceDialog(companyVoiceDialog);
    }

    /**
     * 批量删除AI外呼话术
     *
     * @param ids 需要删除的AI外呼话术ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceDialogByIds(Long[] ids)
    {
        return companyVoiceDialogMapper.deleteCompanyVoiceDialogByIds(ids);
    }

    /**
     * 删除AI外呼话术信息
     *
     * @param id AI外呼话术ID
     * @return 结果
     */
    @Override
    public int deleteCompanyVoiceDialogById(Long id)
    {
        return companyVoiceDialogMapper.deleteCompanyVoiceDialogById(id);
    }

    @Override
    public String getConfigUrl(Long id) {
        CompanyVoiceDialog info = selectCompanyVoiceDialogById(id);
        return aiCallService.getDialogUrl() + "?m=common&c=loginTransition&f=login1&token="+aiCallService.getToken()+"&dialog=" + info.getResultId();
    }
}
