package com.fs.voice.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fs.call.config.CallApiConfig;
import com.fs.call.domain.Call;
import com.fs.call.domain.CallAuthentication;
import com.fs.call.domain.CallRequest;
import com.fs.call.domain.CallResponse;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.sign.Md5Utils;
import com.fs.company.domain.*;
import com.fs.company.service.*;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.domain.CrmCustomerContacts;
import com.fs.crm.service.ICrmCustomerContactsService;
import com.fs.crm.service.ICrmCustomerService;
import com.fs.his.domain.FsPackageOrder;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.service.IFsStoreService;
import com.fs.his.vo.FsPackageOrderVO;
import com.fs.system.config.SystemVoiceConfig;
import com.fs.system.service.ISysConfigService;
import com.fs.voice.service.IVoiceService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.fs.his.utils.PhoneUtil.decryptPhone;

@Service
public class VoiceServiceImpl implements IVoiceService
{
    @Autowired
    ICompanyVoiceLogsService logsService;

    @Autowired
    ISysConfigService sysConfigService;

    @Autowired
    private ICompanyVoiceMobileService mobileService;
    @Autowired
    private ICompanyUserService userService;
    @Autowired
    private ICompanyVoiceCallerService callerService;

    @Autowired
    private ICompanyVoiceBlacklistService blacklistService;
    @Autowired
    private ICompanyVoiceConfigService companyVoiceConfigService;
    @Autowired
    private ICompanyVoiceService companyVoiceService;
    @Autowired
    private ICompanyVoiceApiService companyVoiceApiService;
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ICrmCustomerService crmCustomerService;

    @Autowired
    private IFsStoreOrderService orderService;

    @Autowired
    private IFsPackageOrderService packageOrderService;


    @Autowired
    private ICrmCustomerContactsService crmCustomerContactsService;
    @Override
    @Transactional
    @Synchronized
    public R voiceCall(Long companyId, Long customerId, Long userId, String calleeNbr) {
        //查询主叫号是不是坐席 查询主叫号是否已过期
        Company company=companyService.selectCompanyById(companyId);
        if(company.getStatus()==0){
            return R.error("公司已停用");
        }
        if(company.getVoiceApiId()==null){
            return R.error("未配置外呼接口");
        }
        CompanyVoiceApi api=companyVoiceApiService.selectCompanyVoiceApiById(company.getVoiceApiId());
        if(api==null){
            return R.error("外呼接口不存在");
        }
        if(api.getStatus().equals(0)){
            return R.error("外呼接口已禁用");
        }
        CompanyUser user=userService.selectCompanyUserById(userId);
        CompanyVoiceCaller caller=callerService.selectCompanyVoiceCallerByUserId(companyId,userId);
        CompanyVoice companyVoice=companyVoiceService.selectCompanyVoiceByCompanyId(user.getCompanyId());
        if(caller==null){
            return R.error("未绑定坐席");
        }
        if(companyVoice.getTimes()<=0){
            return R.error("剩余时长不足,请购买套餐");
        }
        CompanyVoiceMobile voiceMobile=mobileService.selectEmptyCompanyVoiceMobile(companyId,api.getApiId());
        if(voiceMobile==null){
            return R.error("没有空闲号码");
        }
        //黑名单处理
        if(blacklistService.selectCompanyVoiceBlacklistByMobile(caller.getMobile())>0){
            return R.error("主叫号码已被设置为黑名单");
        }
        if(blacklistService.selectCompanyVoiceBlacklistByMobile(calleeNbr)>0){
            return R.error("被叫号码已被设置为黑名单");
        }
        //处理主叫和被叫限制
        CompanyVoiceConfig companyVoiceConfig=companyVoiceConfigService.selectCompanyVoiceConfigByCompanyId(companyId);
//        String config=sysConfigService.selectConfigByKey("sys.voice.config");
//        SystemVoiceConfig voiceConfig=JSONUtil.toBean(config, SystemVoiceConfig.class);
        if(companyVoiceConfig!=null){
            SystemVoiceConfig callerConfig=JSONUtil.toBean(companyVoiceConfig.getCallerJson(), SystemVoiceConfig.class);
            if(callerConfig.getCallerMinute()!=null&&callerConfig.getCallerMinute()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),1)>=callerConfig.getCallerMinute()){
                    return R.error("主叫分钟限制");
                }
            }
            if(callerConfig.getCallerHour()!=null&&callerConfig.getCallerHour()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),2)>=callerConfig.getCallerHour()){
                    return R.error("主叫小时限制");
                }
            }
            if(callerConfig.getCallerDay()!=null&&callerConfig.getCallerDay()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),3)>=callerConfig.getCallerDay()){
                    return R.error("主叫天限制");
                }
            }
            if(callerConfig.getCallerWeek()!=null&&callerConfig.getCallerWeek()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),4)>=callerConfig.getCallerWeek()){
                    return R.error("主叫周限制");
                }
            }
            if(callerConfig.getCallerMonth()!=null&&callerConfig.getCallerMonth()>0){

                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),5)>=callerConfig.getCallerMonth()){
                    return R.error("主叫月限制");
                }
            }
            SystemVoiceConfig calleeConfig=JSONUtil.toBean(companyVoiceConfig.getCalleeJson(), SystemVoiceConfig.class);
            if(calleeConfig.getCalleeMinute()!=null&&calleeConfig.getCalleeMinute()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,1)>=calleeConfig.getCalleeMinute()){
                    return R.error("被叫分钟限制");
                }
            }
            if(calleeConfig.getCalleeHour()!=null&&calleeConfig.getCalleeHour()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,2)>=calleeConfig.getCalleeHour()){
                    return R.error("被叫小时限制");
                }
            }
            if(calleeConfig.getCalleeDay()!=null&&calleeConfig.getCalleeDay()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,3)>=calleeConfig.getCalleeDay()){
                    return R.error("被叫天限制");
                }
            }
            if(calleeConfig.getCalleeWeek()!=null&&calleeConfig.getCalleeWeek()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,4)>=calleeConfig.getCalleeWeek()){
                    return R.error("被叫周限制");
                }
            }
            if(calleeConfig.getCalleeMonth()!=null&&calleeConfig.getCalleeMonth()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,5)>=calleeConfig.getCalleeMonth()){
                    return R.error("被叫月限制");
                }
            }
        }
        String orderSn =  OrderCodeUtils.getOrderSn();
        if(StringUtils.isEmpty(orderSn)){
            return R.error("订单生成失败，请重试");
        }
        Long time=new Date().getTime();
        String seq=time.toString();
        Call call=new Call();
        CallRequest callRequest=new CallRequest();
        callRequest.setCallee(calleeNbr);
        callRequest.setCaller(voiceMobile.getMobile());
        callRequest.setUserData(orderSn);
        callRequest.setSeq(seq);
        callRequest.setAgent(caller.getCallerNo());
        callRequest.setCallType("0");
        call.setRequest(callRequest);
        CallAuthentication authentication=new CallAuthentication();
        CallApiConfig apiConfig=JSONUtil.toBean(api.getApiJson(), CallApiConfig.class);
        String mdsStr= Md5Utils.hash(apiConfig.getAccount()+"@"+time+"@"+seq+"@"+apiConfig.getPassword());
        authentication.setCustomer(apiConfig.getAccount());
        authentication.setTimestamp(time.toString());
        authentication.setSeq(seq);
        authentication.setDigest(mdsStr);
        call.setAuthentication(authentication);
        String param=JSONUtil.toJsonStr(call);
        String result = HttpUtil.post(apiConfig.getUrl()+"/callNumber", param);
        CallResponse response=JSONUtil.toBean(result, CallResponse.class);

        if(response.getResult().getError().equals("0")){
            //写入SESSION
            CompanyVoiceLogs logs=new CompanyVoiceLogs();
            logs.setCompanyId(companyId);
            logs.setVoiceTitle(user.getNickName()+"呼叫："+calleeNbr);
            logs.setStartTime(new Date());
            logs.setVoiceType(1);
            logs.setCalleePhone("+86"+calleeNbr);
            logs.setCallerPhone("+86"+caller.getMobile());
            logs.setDisplayCalleeNumber(voiceMobile.getMobile());
            logs.setDisplayCallerNumber(voiceMobile.getMobile());
            logs.setRemark(user.getNickName()+"发起呼叫"+calleeNbr);
            logs.setCompanyUserId(userId);
            logs.setCustomerId(customerId);
            logs.setApiId(api.getApiId());
            logs.setSessionId(orderSn);
            logs.setCallerNo(caller.getCallerNo());
            logsService.insertCompanyVoiceLogs(logs);

            voiceMobile.setStatus(0);
            mobileService.updateCompanyVoiceMobile(voiceMobile);
            return R.ok().put("logs",logs);
        }
        else{

            return R.error(response.getResult().getMsg());
        }
    }

    @Override
    public R voiceCallOff(Long voiceId) {
        CompanyVoiceLogs logs=logsService.selectCompanyVoiceLogsById(voiceId);
        CompanyVoiceApi api=companyVoiceApiService.selectCompanyVoiceApiById(logs.getApiId());
        CallApiConfig apiConfig=JSONUtil.toBean(api.getApiJson(), CallApiConfig.class);
        Long time=new Date().getTime();
        String seq=time.toString();
        Call call=new Call();
        CallRequest callRequest=new CallRequest();
        callRequest.setUserData(logs.getSessionId());
        callRequest.setSeq(seq);
        callRequest.setAgent(logs.getCallerNo());
        call.setRequest(callRequest);
        CallAuthentication authentication=new CallAuthentication();
        String mdsStr= Md5Utils.hash(apiConfig.getAccount()+"@"+time+"@"+seq+"@"+apiConfig.getPassword());
        authentication.setCustomer(apiConfig.getAccount());
        authentication.setTimestamp(time.toString());
        authentication.setSeq(seq);
        authentication.setDigest(mdsStr);
        call.setAuthentication(authentication);
        String param=JSONUtil.toJsonStr(call);
        String result = HttpUtil.post(apiConfig.getUrl()+"/hangupCall", param);
        CallResponse response=JSONUtil.toBean(result, CallResponse.class);
        if(response.getResult().getError().equals("0")){
            return R.ok("操作成功");
        }
        else{

            return R.error(response.getResult().getMsg());
        }
    }

    @Override
    public R getSipAccount(Long userId) {
        CompanyUser user= userService.selectCompanyUserById(userId);
        Company company=companyService.selectCompanyById(user.getCompanyId());
        CompanyVoiceCaller caller=callerService.selectCompanyVoiceCallerByUserId(company.getCompanyId(),userId);
        if(caller==null){
            return R.error("未绑定坐席");
        }
        CompanyVoiceApi api=companyVoiceApiService.selectCompanyVoiceApiById(company.getVoiceApiId());
        CallApiConfig apiConfig=JSONUtil.toBean(api.getApiJson(), CallApiConfig.class);
        Long time=new Date().getTime();
        String seq=time.toString();
        Call call=new Call();
        CallRequest callRequest=new CallRequest();
        callRequest.setUserData("");
        callRequest.setSeq(seq);
        callRequest.setAgent(caller.getCallerNo());
        call.setRequest(callRequest);
        CallAuthentication authentication=new CallAuthentication();
        String mdsStr= Md5Utils.hash(apiConfig.getAccount()+"@"+time+"@"+seq+"@"+apiConfig.getPassword());
        authentication.setCustomer(apiConfig.getAccount());
        authentication.setTimestamp(time.toString());
        authentication.setSeq(seq);
        authentication.setDigest(mdsStr);
        call.setAuthentication(authentication);
        String param=JSONUtil.toJsonStr(call);
        String result = HttpUtil.post(apiConfig.getUrl()+"/getSIPAccount", param);
        CallResponse response=JSONUtil.toBean(result, CallResponse.class);
        return R.ok().put("data",response);

    }

    @Override
    public R voiceCall(Long companyId, Long customerId, Long contactsId, Long userId,Long orderId,Long packageOrderId) {
        String calleeNbr="";
        if(contactsId!=null&&contactsId>0){
            CrmCustomerContacts contacts=crmCustomerContactsService.selectCrmCustomerContactsById(contactsId);
            calleeNbr=contacts.getMobile().trim();
        }
        else
        if(customerId!=null&&customerId>0){
            CrmCustomer crmCustomer=crmCustomerService.selectCrmCustomerById(customerId);
            calleeNbr=crmCustomer.getMobile().trim();
        }else if (orderId!=null&&orderId>0){
            FsStoreOrder order = orderService.selectFsStoreOrderByOrderId(orderId);
            calleeNbr = order.getUserPhone().trim();
        }else if (packageOrderId!=null&&packageOrderId>0){
            FsPackageOrderVO packageOrderVO = packageOrderService.selectFsPackageOrderByOrderIdVO(packageOrderId);
            calleeNbr = packageOrderVO.getPhone().trim();
        }
        if (calleeNbr.length()>11){
            calleeNbr=decryptPhone(calleeNbr);
        }
        //查询主叫号是不是坐席 查询主叫号是否已过期
        Company company=companyService.selectCompanyById(companyId);
        if(company.getStatus()==0){
            return R.error("公司已停用");
        }
        if(company.getVoiceApiId()==null){
            return R.error("未配置外呼接口");
        }
        CompanyVoiceApi api=companyVoiceApiService.selectCompanyVoiceApiById(company.getVoiceApiId());
        if(api==null){
            return R.error("外呼接口不存在");
        }
        if(api.getStatus().equals(0)){
            return R.error("外呼接口已禁用");
        }
        CompanyUser user=userService.selectCompanyUserById(userId);
        CompanyVoiceCaller caller=callerService.selectCompanyVoiceCallerByUserId(companyId,userId);
        CompanyVoice companyVoice=companyVoiceService.selectCompanyVoiceByCompanyId(user.getCompanyId());
        if(caller==null){
            return R.error("未绑定坐席");
        }
        if(companyVoice.getTimes()<=0){
            return R.error("剩余时长不足,请购买套餐");
        }
//        CompanyVoiceMobile voiceMobile=mobileService.selectEmptyCompanyVoiceMobile(companyId,api.getApiId());
//        if(voiceMobile==null){
//            return R.error("没有空闲号码");
//        }
        //黑名单处理
        if(blacklistService.selectCompanyVoiceBlacklistByMobile(caller.getMobile())>0){
            return R.error("主叫号码已被设置为黑名单");
        }
        if(blacklistService.selectCompanyVoiceBlacklistByMobile(calleeNbr)>0){
            return R.error("被叫号码已被设置为黑名单");
        }
        //处理主叫和被叫限制
        CompanyVoiceConfig companyVoiceConfig=companyVoiceConfigService.selectCompanyVoiceConfigByCompanyId(companyId);
//        String config=sysConfigService.selectConfigByKey("sys.voice.config");
//        SystemVoiceConfig voiceConfig=JSONUtil.toBean(config, SystemVoiceConfig.class);
        if(companyVoiceConfig!=null){
            SystemVoiceConfig callerConfig=JSONUtil.toBean(companyVoiceConfig.getCallerJson(), SystemVoiceConfig.class);
            if(callerConfig.getCallerMinute()!=null&&callerConfig.getCallerMinute()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),1)>=callerConfig.getCallerMinute()){
                    return R.error("主叫分钟限制");
                }
            }
            if(callerConfig.getCallerHour()!=null&&callerConfig.getCallerHour()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),2)>=callerConfig.getCallerHour()){
                    return R.error("主叫小时限制");
                }
            }
            if(callerConfig.getCallerDay()!=null&&callerConfig.getCallerDay()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),3)>=callerConfig.getCallerDay()){
                    return R.error("主叫天限制");
                }
            }
            if(callerConfig.getCallerWeek()!=null&&callerConfig.getCallerWeek()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),4)>=callerConfig.getCallerWeek()){
                    return R.error("主叫周限制");
                }
            }
            if(callerConfig.getCallerMonth()!=null&&callerConfig.getCallerMonth()>0){

                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),5)>=callerConfig.getCallerMonth()){
                    return R.error("主叫月限制");
                }
            }
            SystemVoiceConfig calleeConfig=JSONUtil.toBean(companyVoiceConfig.getCalleeJson(), SystemVoiceConfig.class);
            if(calleeConfig.getCalleeMinute()!=null&&calleeConfig.getCalleeMinute()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,1)>=calleeConfig.getCalleeMinute()){
                    return R.error("被叫分钟限制");
                }
            }
            if(calleeConfig.getCalleeHour()!=null&&calleeConfig.getCalleeHour()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,2)>=calleeConfig.getCalleeHour()){
                    return R.error("被叫小时限制");
                }
            }
            if(calleeConfig.getCalleeDay()!=null&&calleeConfig.getCalleeDay()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,3)>=calleeConfig.getCalleeDay()){
                    return R.error("被叫天限制");
                }
            }
            if(calleeConfig.getCalleeWeek()!=null&&calleeConfig.getCalleeWeek()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,4)>=calleeConfig.getCalleeWeek()){
                    return R.error("被叫周限制");
                }
            }
            if(calleeConfig.getCalleeMonth()!=null&&calleeConfig.getCalleeMonth()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,5)>=calleeConfig.getCalleeMonth()){
                    return R.error("被叫月限制");
                }
            }
        }
        String orderSn =  OrderCodeUtils.getOrderSn();
        if(StringUtils.isEmpty(orderSn)){
            return R.error("订单生成失败，请重试");
        }
        //写入SESSION
        CompanyVoiceLogs logs=new CompanyVoiceLogs();
        logs.setCompanyId(companyId);
        logs.setVoiceTitle(user.getNickName()+"呼叫："+calleeNbr);
        logs.setStartTime(new Date());
        logs.setVoiceType(1);
        logs.setCalleePhone(calleeNbr);
        logs.setCallerPhone(caller.getMobile());
        logs.setDisplayCalleeNumber(calleeNbr);
        logs.setDisplayCallerNumber("");
        logs.setRemark(user.getNickName()+"发起呼叫"+calleeNbr);
        logs.setCompanyUserId(userId);
        logs.setCustomerId(customerId);
        logs.setApiId(api.getApiId());
        logs.setSessionId(orderSn);
        logs.setCallerNo(caller.getCallerNo());
        logsService.insertCompanyVoiceLogs(logs);
//        voiceMobile.setStatus(0);
//        mobileService.updateCompanyVoiceMobile(voiceMobile);
        String displayMobile=calleeNbr.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2");
        return R.ok().put("logs",logs).put("mobile",calleeNbr).put("displayMobile",displayMobile);
    }

    @Override
    public R voiceAppCall(Long companyId, Long customerId, Long userId, String calleeNbr) {
        //查询主叫号是不是坐席 查询主叫号是否已过期
        Company company=companyService.selectCompanyById(companyId);
        if(company.getStatus()==0){
            return R.error("公司已停用");
        }
        if(company.getVoiceApiId()==null){
            return R.error("未配置外呼接口");
        }
        CompanyVoiceApi api=companyVoiceApiService.selectCompanyVoiceApiById(company.getVoiceApiId());
        if(api==null){
            return R.error("外呼接口不存在");
        }
        if(api.getStatus().equals(0)){
            return R.error("外呼接口已禁用");
        }
        CompanyUser user=userService.selectCompanyUserById(userId);
        CompanyVoiceCaller caller=callerService.selectCompanyVoiceCallerByUserId(companyId,userId);
        CompanyVoice companyVoice=companyVoiceService.selectCompanyVoiceByCompanyId(user.getCompanyId());
        if(caller==null){
            return R.error("未绑定坐席");
        }
        if(companyVoice.getTimes()<=0){
            return R.error("剩余时长不足,请购买套餐");
        }
//        CompanyVoiceMobile voiceMobile=mobileService.selectEmptyCompanyVoiceMobile(companyId,api.getApiId());
//        if(voiceMobile==null){
//            return R.error("没有空闲号码");
//        }
        //黑名单处理
        if(blacklistService.selectCompanyVoiceBlacklistByMobile(caller.getMobile())>0){
            return R.error("主叫号码已被设置为黑名单");
        }
        if(blacklistService.selectCompanyVoiceBlacklistByMobile(calleeNbr)>0){
            return R.error("被叫号码已被设置为黑名单");
        }
        //处理主叫和被叫限制
        CompanyVoiceConfig companyVoiceConfig=companyVoiceConfigService.selectCompanyVoiceConfigByCompanyId(companyId);
//        String config=sysConfigService.selectConfigByKey("sys.voice.config");
//        SystemVoiceConfig voiceConfig=JSONUtil.toBean(config, SystemVoiceConfig.class);
        if(companyVoiceConfig!=null){
            SystemVoiceConfig callerConfig=JSONUtil.toBean(companyVoiceConfig.getCallerJson(), SystemVoiceConfig.class);
            if(callerConfig.getCallerMinute()!=null&&callerConfig.getCallerMinute()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),1)>=callerConfig.getCallerMinute()){
                    return R.error("主叫分钟限制");
                }
            }
            if(callerConfig.getCallerHour()!=null&&callerConfig.getCallerHour()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),2)>=callerConfig.getCallerHour()){
                    return R.error("主叫小时限制");
                }
            }
            if(callerConfig.getCallerDay()!=null&&callerConfig.getCallerDay()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),3)>=callerConfig.getCallerDay()){
                    return R.error("主叫天限制");
                }
            }
            if(callerConfig.getCallerWeek()!=null&&callerConfig.getCallerWeek()>0){
                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),4)>=callerConfig.getCallerWeek()){
                    return R.error("主叫周限制");
                }
            }
            if(callerConfig.getCallerMonth()!=null&&callerConfig.getCallerMonth()>0){

                if(logsService.selectCompanyVoiceLogsCountByCallerMobile("+86"+caller.getMobile(),5)>=callerConfig.getCallerMonth()){
                    return R.error("主叫月限制");
                }
            }
            SystemVoiceConfig calleeConfig=JSONUtil.toBean(companyVoiceConfig.getCalleeJson(), SystemVoiceConfig.class);
            if(calleeConfig.getCalleeMinute()!=null&&calleeConfig.getCalleeMinute()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,1)>=calleeConfig.getCalleeMinute()){
                    return R.error("被叫分钟限制");
                }
            }
            if(calleeConfig.getCalleeHour()!=null&&calleeConfig.getCalleeHour()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,2)>=calleeConfig.getCalleeHour()){
                    return R.error("被叫小时限制");
                }
            }
            if(calleeConfig.getCalleeDay()!=null&&calleeConfig.getCalleeDay()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,3)>=calleeConfig.getCalleeDay()){
                    return R.error("被叫天限制");
                }
            }
            if(calleeConfig.getCalleeWeek()!=null&&calleeConfig.getCalleeWeek()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,4)>=calleeConfig.getCalleeWeek()){
                    return R.error("被叫周限制");
                }
            }
            if(calleeConfig.getCalleeMonth()!=null&&calleeConfig.getCalleeMonth()>0){
                if(logsService.selectCompanyVoiceLogsCountByCalleeMobile("+86"+calleeNbr,5)>=calleeConfig.getCalleeMonth()){
                    return R.error("被叫月限制");
                }
            }
        }
        String orderSn =  OrderCodeUtils.getOrderSn();
        if(StringUtils.isEmpty(orderSn)){
            return R.error("订单生成失败，请重试");
        }
        //写入SESSION
        CompanyVoiceLogs logs=new CompanyVoiceLogs();
        logs.setCompanyId(companyId);
        logs.setVoiceTitle(user.getNickName()+"呼叫："+calleeNbr);
        logs.setStartTime(new Date());
        logs.setVoiceType(1);
        logs.setCalleePhone(calleeNbr);
        logs.setCallerPhone(caller.getMobile());
        logs.setDisplayCalleeNumber(calleeNbr);
        logs.setDisplayCallerNumber("");
        logs.setRemark(user.getNickName()+"发起呼叫"+calleeNbr);
        logs.setCompanyUserId(userId);
        logs.setCustomerId(customerId);
        logs.setApiId(api.getApiId());
        logs.setSessionId(orderSn);
        logs.setCallerNo(caller.getCallerNo());
        logsService.insertCompanyVoiceLogs(logs);
//        voiceMobile.setStatus(0);
//        mobileService.updateCompanyVoiceMobile(voiceMobile);
        return R.ok().put("logs",logs);
    }

    @Override
    public R voiceAppCallOff(Long voiceId) {
        CompanyVoiceLogs logs=logsService.selectCompanyVoiceLogsById(voiceId);
        logs.setStatus(-1);
        logs.setFinishTime(new Date());
        if(logsService.updateCompanyVoiceLogs(logs)>0){
            return R.ok("操作成功");
        }
        else{
            return R.error("操作失败");
        }
    }


}
