package com.tofly.callcenter.custom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.callcenter.config.RecordFile;
import com.tofly.callcenter.custom.entity.FlowNum;
import com.tofly.callcenter.custom.entity.dto.MissCallInfosDto;
import com.tofly.callcenter.custom.service.RecordService;
import com.tofly.callcenter.custom.util.RecordAPIUtil;
import com.tofly.callcenter.entity.PltCalllist1000;
import com.tofly.callcenter.mapper.PltCalllist1000Mapper;
import com.tofly.callcenter.service.PltCalllist1000Service;
import com.tofly.callcenter.service.export.ExportCommonCodeService;
import com.tofly.common.core.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FlashWJL
 * @date 2021/7/7 16:37
 */
@Service
@AllArgsConstructor
public class RecordServiceImpl implements RecordService {

    private final RecordFile recordFile;
    private final PltCalllist1000Service pltCalllist1000Service;
    private final PltCalllist1000Mapper pltCalllist1000Mapper;
    private final ExportCommonCodeService exportCommonCodeService;

    @Override
    public String listenRecordFile(String connectionid, HttpServletResponse response) {
        //查询是否有录音文件  语音系统原则上是指 接通了电话，并且服务器没有存满的情况下，就会在服务器保存一份录音文件
        PltCalllist1000 pltCalllist1000 = pltCalllist1000Service.getOne(Wrappers.<PltCalllist1000>lambdaQuery()
                .eq(PltCalllist1000::getConnectionid, connectionid)
                .eq(PltCalllist1000::getCallResult, "1")
                .ne(PltCalllist1000::getRecordFile, "")
                .select(PltCalllist1000::getId, PltCalllist1000::getConnectionid, PltCalllist1000::getRecordFile));
        //如果查询出来是null，则没有录音文件
        if (StringUtil.isNull(pltCalllist1000)) {
            return "1";
        }
        //获取配置文件里的IP地址
        String listenRecordFile = recordFile.getListenRecordFile();
        //拼接听取录音地址
        listenRecordFile = listenRecordFile + connectionid;
        //根据录音地址获取录音文件，并响应给前端
        RecordAPIUtil.getFileByURL(listenRecordFile, response);
        return "2";
    }

    @Override
    public boolean configFlow(FlowNum flowNum) {
        return pltCalllist1000Service.configFlow(flowNum);
    }

    /**
     * 获取录音条数
     *
     * @return list
     */
    @Override
    public Map<String, Integer> queryRecordNum(String crew, String start, String end) {
        Map<String, Integer> map = new HashMap<>(5);

        List<Map<String, Object>> pltCallInfo = pltCalllist1000Mapper.getPltCallInfo(crew, start, end);

        pltCallInfo.forEach(p -> map.put(p.get("code").toString(), Integer.valueOf(p.get("sumNum").toString())));

        return map;
    }

    /**
     * 获取未接来电
     *
     * @param emp 员工
     * @param tel 电话
     * @return page
     */
    @Override
    public Page<Map> missedCalls(Page page, String emp, String tel) {
        return pltCalllist1000Mapper.missedCall(page, emp, tel);
    }

    /**
     * 话务清单
     *
     * @param page page
     * @param dto  dto
     * @return page
     */
    @Override
    public Page missedCallsInfo(Page page, HttpServletResponse response, MissCallInfosDto dto) {
        if (dto.getIsExport() != null && dto.getIsExport() == 1) {
            exportCommonCodeService.exportCode(response, "missCallsService", pltCalllist1000Mapper.missCallInfo(dto));
        }
        return pltCalllist1000Mapper.missCallInfo(page, dto);
    }
}
