/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller.callCenter;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.entity.callCenter.FlowNum;
import com.tofly.xrtygis.custom.entity.callCenter.dto.MissedCallsDto;
import com.tofly.xrtygis.entity.callCenter.PltCalllist1000;
import com.tofly.xrtygis.entity.callCenter.CallSearch;
import com.tofly.xrtygis.service.callCenter.service.PltCalllist1000Service;
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;

/**
 * 通话记录(话单)坐席或分机之间呼叫只产生一条记录
 *
 * @author wangjieliu
 * @date Thu Jun 17 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/pltcall")
@Api(tags = "通话记录接口")
public class PltCalllist1000Controller {

    private final PltCalllist1000Service pltCalllist1000Service;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param search 通话记录(话单)坐席或分机之间呼叫只产生一条记录
     * @return
     */
    @GetMapping("/record")
    @ApiOperation(value = "通话记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "empcode", value = "坐席工号（查看属于该坐席的通话记录）", dataType = "String"),
            @ApiImplicitParam(name = "callType", value = "1:呼入,2:呼出,3:内部呼叫（查看呼叫类型）", dataType = "String"),
            @ApiImplicitParam(name = "telephone", value = "客户电话（查看某客户的通话记录）", dataType = "String"),
            @ApiImplicitParam(name = "sysphone", value = "呼叫电话（查看内部系统电话通话记录）", dataType = "String"),
            @ApiImplicitParam(name = "connectionid", value = "流水号（唯一标识）", dataType = "String")
    })
    public ResultRespone getPltCalllist(Page page, CallSearch search) {
        IPage<PltCalllist1000> iPage = pltCalllist1000Service.getPltCalllist(page, search);
        return ResultRespone.success(iPage);
    }

    /**
     * 流量预警（查看当天呼入个数预警）
     *
     * @return
     */
    @GetMapping("/satisfaction")
    @ApiOperation(value = "坐席满意度")
    public ResultRespone getSatisfaction(String yearMonth) {
        return ResultRespone.success(pltCalllist1000Service.getSatisfaction(yearMonth));
    }


    @GetMapping("/missedCalls")
    @ApiOperation("获取通话信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "empcode",value = "坐席人员编号",dataType = "string"),
            @ApiImplicitParam(name = "phoneNumber",value = "电话号码",dataType = "string"),
            @ApiImplicitParam(name = "result",value = "为0查询符合传入呼叫状态的信息，为1查询除传入呼叫状态外其他状态信息",dataType = "Integer"),
            @ApiImplicitParam(name = "callResult",value = "呼叫状态",dataType = "Integer")
    })
    public ResultRespone missedCalls(MissedCallsDto dto){
        return ResultRespone.success(pltCalllist1000Service.missedCalls(dto));
    }

    /**
     * 流量预警（查看当天呼入个数预警）
     *
     * @return
     */
    @GetMapping("/waring")
    @ApiOperation(value = "流量预警个数（查看当天呼入个数预警）")
    public ResultRespone getCallInCount() {
        return ResultRespone.success(pltCalllist1000Service.getCallInCount());
    }

    /**
     * 录音评分远程调用的，修改是否评分状态
     *
     * @return
     */
    @ApiOperation(value = "评分状态修改")
    @PostMapping("/grade")
    public boolean updateIsGrade(String lyId) {
        boolean state = false;
        try {
            state = pltCalllist1000Service.update(Wrappers.<PltCalllist1000>lambdaUpdate()
                    .eq(PltCalllist1000::getConnectionid, lyId)
                    .set(PltCalllist1000::getIsGrade, "1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return state;

    }

    @GetMapping("/listen")
    @ApiOperation(value = "听取录音")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "connectionid", value = "通话流水号", required = true, dataType = "String")
    })
    public ResultRespone listenRecordFile(String connectionid, HttpServletResponse response) {
        String state = pltCalllist1000Service.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(pltCalllist1000Service.configFlow(flowNumEntity));
    }
}
