package com.xinelu.manage.controller.aiob;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.manage.dto.aiob.ActualTimeTaskDto;
import com.xinelu.manage.dto.aiob.CreateTaskDto;
import com.xinelu.manage.dto.aiob.TaskCallbackDto;
import com.xinelu.manage.service.aiob.IAIOBService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * @description: 百度客悦·智能外呼平台接口
 * @author: haown
 * @create: 2024-08-22 09:50
 **/
@Api(tags = "百度客悦·智能外呼平台接口")
@RestController
@RequestMapping("/api")
public class AIOBController extends BaseController {
	@Resource
	private IAIOBService aiobService;

	/**
	 * @description 获取外呼平台accessToken
	 * @return 智能外呼平台 accessToken
	 * @Author haown
	 * @Date 2024-8-22 15:30
	 */
	@ApiOperation("获取accessToken")
	@PostMapping("/getAccessToken")
	public R<String> getAccessToken() {
		return R.ok(aiobService.getAccessToken());
	}

	/**
	 * @description 创建任务
	 * @param createTaskDto 创建任务传输对象
	 * @return taskId 客悦·智能外呼平台任务唯一标识
	 * @Author haown
	 * @Date 2024-8-22 15:34
	 */
	@ApiOperation("创建任务")
	@PostMapping("/createTask")
	public R<String> createTask(CreateTaskDto createTaskDto) {
		return R.ok(aiobService.createTask(createTaskDto));
	}

	@ApiOperation("创建实时任务")
	@GetMapping("/createActualTimeTask")
	public R<Long> createActualTimeTask(ActualTimeTaskDto actualTimeTaskDto) {
		actualTimeTaskDto.setRobotId("8e5d5e08-9e82-44a7-ab38-c20f56388b25");
		actualTimeTaskDto.setMobile("15166940975");
		actualTimeTaskDto.setSecretType(2);
		JSONObject dialogVar = new JSONObject();
		dialogVar.fluentPut("patient_name", "张三")
			.fluentPut("department_name", "山东省立医院");
		actualTimeTaskDto.setDialogVar(dialogVar);
		actualTimeTaskDto.setCallBackUrl("http://8.131.93.145:54011/api/taskCallBack");
		actualTimeTaskDto.setExtJson("646");
		return R.ok(aiobService.createActualTimeTask(actualTimeTaskDto));
	}

	/**
	 * @description 任务状态变更
	 * @param taskId 任务id
	 * @param taskStatus 修改状态 2：执行中，3：已暂停，4：已完成（如点击「终止」
	 * @return 状态码
	 * @Author haown
	 * @Date 2024-8-23 11:49
	 */
	@ApiOperation("任务状态变更")
	@PostMapping("/updateTaskStatus")
	public R<String> updateTaskStatus(String taskId, Integer taskStatus) {
		int flag = aiobService.updateTaskStatus(taskId, taskStatus);
		return flag > 0 ? R.ok() : R.fail();
	}

	/**
	 * @description 任务单通电话回调
	 * @param taskCallbackDto 回调数据类型， 0-任务呼叫单通电话回调 1-号码组终态回调 2-任务状态变更回调 3-实时呼叫单通电话回调
	 * @return null
	 * @Author haown
	 * @Date 2024-8-29 11:15
	*/
	@ApiOperation("任务单通电话回调")
	@PostMapping("/taskCallBack")
	public JSONObject taskCallBack(@RequestBody TaskCallbackDto taskCallbackDto, HttpServletRequest request) throws ClientException {

		// logger.info(request.getURI().getHost());
		// logger.info(request.getURI().getRawPath());
		logger.info(request.getRemoteHost());
		// logger.info(request.getURI().getHost());

		return aiobService.taskCallBack(taskCallbackDto.getCallbackType(), taskCallbackDto.getData());
	}

	@ApiOperation("任务单通电话回调")
	@GetMapping("/record")
	public String record() throws ClientException {
		return aiobService.record("1726726696448834_936326");
	}

}
