package com.gree.mall.manager.controller.common;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.common.CallRecordOrderVo;
import com.gree.mall.manager.bean.common.CallRecordVo;
import com.gree.mall.manager.bean.workorder.IncreVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.unicom.UnicomLogic;
import com.gree.mall.manager.plus.entity.CallRecord;
import com.gree.mall.manager.plus.entity.CallSendRecord;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "云呼API", tags ={"云呼API"} )
@RequestMapping("unCall")
public class CallRecordController {

    @Autowired
    UnicomLogic unicomLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "AI拨号记录")
    public ResponseHelper<IPage<CallRecordVo>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CallRecordVo> increVOIPage = unicomLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(increVOIPage, new TypeReference<CallRecordVo>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "AI拨号记录导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CallRecordVo> increVOIPage = unicomLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(increVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("unicome/config/save")
    @ApiOperation(value = "云呼-保存配置")
    public ResponseHelper saveUnicomeConfig(@ApiParam(value = "联通云呼integratedid") @RequestParam String unicomIntegratedId,
                                            @ApiParam(value = "联通云呼ExtenType（支持三种方式Local（直线方式），sip（软电话），gateway（语音网关/IP话机）") @RequestParam String unicomIntegratedType) {
        unicomLogic.saveConfig(unicomIntegratedId, unicomIntegratedType);
        return ResponseHelper.success();
    }

    @PostMapping("unicome/call")
    @ApiOperation(value = "云呼-拨打")
    public ResponseHelper callUicome(
            @ApiParam(value = "服务单号") @RequestParam String orderBaseId,
            @ApiParam(value = "手机号") @RequestParam String phone,
            @ApiParam(value = "A=回访",required = false) @RequestParam(required = false) String flag
    ){
        unicomLogic.commonlnte(phone,orderBaseId,flag);
        return ResponseHelper.success();
    }



    @ZfireList
    @PostMapping("/listOrder")
    @ApiOperation(value = "工单通话记录列表")
    public ResponseHelper<IPage<CallRecordOrderVo>> listOrder(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CallRecordOrderVo> increVOIPage = unicomLogic.listOrder(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(increVOIPage, new TypeReference<CallRecordOrderVo>() {});
    }

    @PostMapping("/list/orderExport")
    @ApiOperation(value = "工单通话记录列表导出")
    public void listOrderExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CallRecordOrderVo> increVOIPage = unicomLogic.listOrder(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(increVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }
}
