package com.ruoyi.cc.service.impl;

import com.ruoyi.cc.domain.CcInboundCdr;
import com.ruoyi.cc.mapper.CcInboundCdrMapper;
import com.ruoyi.cc.service.ICcInboundCdrService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 呼入记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-22
 */
@Service
public class CcInboundCdrServiceImpl implements ICcInboundCdrService 
{
    @Autowired
    private CcInboundCdrMapper ccInboundCdrMapper;

    /**
     * 查询呼入记录
     * 
     * @param id 呼入记录主键
     * @return 呼入记录
     */
    @Override
    public CcInboundCdr selectCcInboundCdrById(String id)
    {
        return ccInboundCdrMapper.selectCcInboundCdrById(id);
    }

    /**
     * 查询呼入记录列表
     * 
     * @param ccInboundCdr 呼入记录
     * @return 呼入记录
     */
    @Override
    public List<CcInboundCdr> selectCcInboundCdrList(CcInboundCdr ccInboundCdr)
    {
        Map<String, Object> params = ccInboundCdr.getParams();
        if (null == params) {
            params = new HashMap<>();
        }
        if (null != params.get("inboundTimeStart")
                && !"".equals(params.get("inboundTimeStart"))) {
            params.put("inboundTimeStart", DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", (String)params.get("inboundTimeStart")).getTime());
        }
        if (null != params.get("inboundTimeEnd")
                && !"".equals(params.get("inboundTimeEnd"))) {
            params.put("inboundTimeEnd", DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", (String)params.get("inboundTimeEnd")).getTime());
        }
        if (null != params.get("answeredTimeStart")
                && !"".equals(params.get("answeredTimeStart"))) {
            params.put("answeredTimeStart", DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", (String)params.get("answeredTimeStart")).getTime());
        }
        if (null != params.get("answeredTimeEnd")
                && !"".equals(params.get("answeredTimeEnd"))) {
            params.put("answeredTimeEnd", DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", (String)params.get("answeredTimeEnd")).getTime());
        }
        if (null != params.get("hangupTimeStart")
                && !"".equals(params.get("hangupTimeStart"))) {
            params.put("hangupTimeStart", DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", (String)params.get("hangupTimeStart")).getTime());
        }
        if (null != params.get("hangupTimeEnd")
                && !"".equals(params.get("hangupTimeEnd"))) {
            params.put("hangupTimeEnd", DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", (String)params.get("hangupTimeEnd")).getTime());
        }
        // 通话时长（分钟）
        if (null != params.get("timeLenStart")
                && !"".equals(params.get("timeLenStart"))) {
            params.put("timeLenStart", Double.valueOf((String)params.get("timeLenStart")) * 60 * 1000L);
        }
        if (null != params.get("timeLenEnd")
                && !"".equals(params.get("timeLenEnd"))) {
            params.put("timeLenEnd", Double.valueOf((String)params.get("timeLenEnd")) * 60 * 1000L);
        }
        // 通话时长（秒）
        if (null != params.get("timeLenSecondStart")
                && !"".equals(params.get("timeLenSecondStart"))) {
            params.put("timeLenStart", Double.valueOf((String)params.get("timeLenSecondStart")) * 1000L);
        }
        if (null != params.get("timeLenSecondEnd")
                && !"".equals(params.get("timeLenSecondEnd"))) {
            params.put("timeLenEnd", Double.valueOf((String)params.get("timeLenSecondEnd")) * 1000L);
        }
        ccInboundCdr.setParams(params);
        return ccInboundCdrMapper.selectCcInboundCdrList(ccInboundCdr);
    }

    /**
     * 新增呼入记录
     * 
     * @param ccInboundCdr 呼入记录
     * @return 结果
     */
    @Override
    public int insertCcInboundCdr(CcInboundCdr ccInboundCdr)
    {
        return ccInboundCdrMapper.insertCcInboundCdr(ccInboundCdr);
    }

    /**
     * 修改呼入记录
     * 
     * @param ccInboundCdr 呼入记录
     * @return 结果
     */
    @Override
    public int updateCcInboundCdr(CcInboundCdr ccInboundCdr)
    {
        return ccInboundCdrMapper.updateCcInboundCdr(ccInboundCdr);
    }

    /**
     * 批量删除呼入记录
     * 
     * @param ids 需要删除的呼入记录主键
     * @return 结果
     */
    @Override
    public int deleteCcInboundCdrByIds(String ids)
    {
        return ccInboundCdrMapper.deleteCcInboundCdrByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除呼入记录信息
     * 
     * @param id 呼入记录主键
     * @return 结果
     */
    @Override
    public int deleteCcInboundCdrById(String id)
    {
        return ccInboundCdrMapper.deleteCcInboundCdrById(id);
    }
}
