package com.fs.store.controller.common;

import cn.hutool.json.JSONUtil;
import com.fs.call.config.CallApiConfig;
import com.fs.call.domain.CallNotifyResponse;
import com.fs.call.domain.CallResult;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyVoiceApi;
import com.fs.company.domain.CompanyVoiceLogs;
import com.fs.company.service.*;
import com.fs.voice.service.IVoiceService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/app/voiceApi")
public class AppNotifyController {
    private static final Logger log = LoggerFactory.getLogger(AppNotifyController.class);
    @Autowired
    private ICompanyVoiceLogsService companyVoiceLogsService;

    @Autowired
    private ICompanyVoiceApiService companyVoiceApiService;
    @Autowired
    private ICompanyVoiceService companyVoiceService;
    @Autowired
    private ICompanyVoiceMobileService companyVoiceMobileService;
    @ApiOperation("外呼回调")
    @PostMapping(value="/callNotify",consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = {"application/json; charset=UTF-8"})
    @Transactional
    public CallResult callNotify(@RequestBody String json) throws Exception
    {
        log.info(json);
        CallNotifyResponse notifyResponse= JSONUtil.toBean(json, CallNotifyResponse.class);

        CompanyVoiceLogs logs = companyVoiceLogsService.selectCompanyVoiceLogsBySessionId(notifyResponse.getNotify().getUserData());
        if (logs != null) {
            if(StringUtils.isNotEmpty(notifyResponse.getNotify().getRecordFile())) {
                CompanyVoiceApi api = companyVoiceApiService.selectCompanyVoiceApiById(logs.getApiId());
                CallApiConfig apiConfig = JSONUtil.toBean(api.getApiJson(), CallApiConfig.class);
                if (StringUtils.isNotEmpty(notifyResponse.getNotify().getRecordFile())) {
                    String url=apiConfig.getUrl().replace("/openapi/V2.0.6","");
                    logs.setVoiceUrl(url + notifyResponse.getNotify().getRecordFile());
                }
                Long remain = notifyResponse.getNotify().getTimeLength() % 60;
                Long times = notifyResponse.getNotify().getTimeLength() / 60;
                if (remain > 0) {
                    times++;
                }
                logs.setStatus(1);
                logs.setTimes(notifyResponse.getNotify().getTimeLength());
                logs.setBillingTime(times);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                logs.setStartTime(df.parse(notifyResponse.getNotify().getAnswerTime()));
                logs.setFinishTime(df.parse(notifyResponse.getNotify().getByeTime()));
                companyVoiceLogsService.updateCompanyVoiceLogs(logs);
                companyVoiceService.updateCompanyVoiceRemainTimes(logs.getCompanyId(), -times);

            }
            else{
                logs.setStatus(-1);
                companyVoiceLogsService.updateCompanyVoiceLogs(logs);
            }

        }
//        CompanyVoiceMobile mobile=companyVoiceMobileService.selectCompanyVoiceMobileByMobile(logs.getDisplayCalleeNumber());
//        if(mobile!=null){
//            mobile.setStatus(1);
//        }
//        mobileService.updateCompanyVoiceMobile(mobile);
        CallResult result=new CallResult();
        result.setMsg("");
        result.setError("");
        return result;
    }
}
