package com.tofly.callcenter.custom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author FlashWJL
 * @date 2021/7/7 16:06
 */
@RestController
@AllArgsConstructor
@RequestMapping("/record")
@Api(tags = "获取接口")
public class RecordController {


    private final RecordService recordService;

    @GetMapping("/listen")
    @ApiOperation(value = "听取录音")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "connectionid", value = "通话流水号", required = true, dataType = "String")
    })
    public ResultRespone listenRecordFile(String connectionid, HttpServletResponse response) {
        String state = recordService.listenRecordFile(connectionid, response);
        if ("1".equals(state)) {
            return ResultRespone.failed("没有该通话记录的录音文件");
        } else {
            return ResultRespone.success("");
        }
    }

    @PutMapping("/configFlow")
    @ApiOperation(value = "预警流量个数配置(目前默认是50个)")
    public ResultRespone configFlow(@RequestBody FlowNum flowNumEntity) {
        Long flowNum = flowNumEntity.getFlowNum();
        Long blockCallNum = flowNumEntity.getBlockCallNum();
        if (StringUtil.isNull(flowNum) || StringUtil.isNull(blockCallNum)) {
            throw new ToflyDeniedException("请输入配置的预警个数");
        }
        return ResultRespone.success(recordService.configFlow(flowNumEntity));
    }

    @GetMapping("/queryRecordNum")
    @ApiOperation("获取录音总数")
    public Map<String, Integer> queryRecordNum(String crew, String start, String end) {
        return recordService.queryRecordNum(crew, start, end);
    }

    @GetMapping("/missedCalls")
    @ApiOperation("获取未接来电信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emp", value = "员工", dataType = "string"),
            @ApiImplicitParam(name = "tel", value = "电话，可模糊", dataType = "string")
    })
    public ResultRespone missedCalls(Page page, String emp, String tel) {
        return ResultRespone.success(recordService.missedCalls(page, emp, tel));
    }

    @GetMapping("/missedCallsInfo")
    @ApiOperation("话务清单")
    public ResultRespone missedCallsInfo(Page page, HttpServletResponse response, MissCallInfosDto dto) {
        return ResultRespone.success(recordService.missedCallsInfo(page, response, dto));
    }

}
