package com.ruoyi.cc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cc.service.ICcParamsService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cc.mapper.CcIvrMapper;
import com.ruoyi.cc.domain.CcIvr;
import com.ruoyi.cc.service.ICcIvrService;
import com.ruoyi.common.core.text.Convert;

/**
 * IVR配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-12-19
 */
@Service
public class CcIvrServiceImpl implements ICcIvrService 
{
    @Autowired
    private CcIvrMapper ccIvrMapper;
    @Autowired
    private ICcParamsService paramsService;

    /**
     * 查询IVR配置
     * 
     * @param id IVR配置主键
     * @return IVR配置
     */
    @Override
    public CcIvr selectCcIvrById(String id)
    {
        return ccIvrMapper.selectCcIvrById(id);
    }

    /**
     * 查询IVR配置列表
     * 
     * @param ccIvr IVR配置
     * @return IVR配置
     */
    @Override
    public List<CcIvr> selectCcIvrList(CcIvr ccIvr)
    {
        return ccIvrMapper.selectCcIvrList(ccIvr);
    }

    /**
     * 新增IVR配置
     * 
     * @param ccIvr IVR配置
     * @return 结果
     */
    @Override
    public int insertCcIvr(CcIvr ccIvr)
    {
        return ccIvrMapper.insertCcIvr(ccIvr);
    }

    /**
     * 修改IVR配置
     * 
     * @param ccIvr IVR配置
     * @return 结果
     */
    @Override
    public int updateCcIvr(CcIvr ccIvr)
    {
        return ccIvrMapper.updateCcIvr(ccIvr);
    }

    /**
     * 批量删除IVR配置
     * 
     * @param ids 需要删除的IVR配置主键
     * @return 结果
     */
    @Override
    public int deleteCcIvrByIds(String ids)
    {
        return ccIvrMapper.deleteCcIvrByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除IVR配置信息
     * 
     * @param id IVR配置主键
     * @return 结果
     */
    @Override
    public int deleteCcIvrById(String id)
    {
        return ccIvrMapper.deleteCcIvrById(id);
    }

    @Override
    public boolean checkDigitExists(String digit, String parentNodeId, Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("digit", digit);
        params.put("parentNodeId", parentNodeId);
        params.put("id", id); // 编辑时排除当前记录

        // 查询除当前记录外是否已存在相同digit
        int count = ccIvrMapper.countByDigitAndParent(params);
        return count > 0;
    }

    @Override
    public JSONObject reloadIvr() {
        String serverPort = paramsService.getParamValueByCode("call-center-server-port", "");
        if(!StringUtils.isEmpty(serverPort)){
            String reloadParamsUrl = String.format("http://127.0.0.1:%s/call-center/api/ivr/reload", serverPort);
            String response = HttpUtils.sendGet(reloadParamsUrl);
            return JSONObject.parseObject(response);
        }
        return null;
    }

    @Override
    public CcIvr selectCcIvrByRootId(String rootId) {
        return selectCcIvrById(rootId);
    }
}
