package com.xinelu.manage.controller.signpatientmanageroutenode;

import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.dto.signpatientmanageroutenode.PatientTaskDto;
import com.xinelu.manage.dto.signpatientmanageroutenode.RouteNodeCheckDto;
import com.xinelu.manage.dto.signpatientmanageroutenode.SignPatientManageRouteNodeDto;
import com.xinelu.manage.dto.signpatientmanageroutenode.UploadRobotPublishRecordDto;
import com.xinelu.manage.service.signpatientmanageroutenode.ISignPatientManageRouteNodeService;
import com.xinelu.manage.vo.signpatientmanageroutenode.*;
import com.xinelu.manage.vo.specialdiseasenode.PatientSpecialDiseaseNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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-04-02 15:08
 **/
@Api(tags = "签约患者管理任务路径节点控制器")
@RestController
@RequestMapping("/manage/signnode")
public class SignPatientManageRouteNodeController extends BaseController {

	@Resource
	private ISignPatientManageRouteNodeService signNodeService;

	/**
	 * 根据患者主键查询签约路径及节点
	 */
	@ApiOperation("患者任务列表查询")
	@GetMapping("/tasklist")
	public TableDataInfo tasklist(PatientTaskDto patientTaskDto) {
		startPage();
		List<PatientTaskVo> list = signNodeService.selectPatientTaskList(patientTaskDto);
		return getDataTable(list);
	}

	/**
	 * 查询节点列表
	 */
	@ApiOperation("患者任务列表查询")
	@GetMapping("/getList")
	public R<List<PatientManageNodeListVo>> getList(SignPatientManageRouteNodeDto signPatientManageRouteNodeDto) {
		return R.ok(signNodeService.selectPatientManageNodeList(signPatientManageRouteNodeDto));
	}

	/**
	 * 查询节点详情
	 */
	@ApiOperation("查询节点详情")
	@GetMapping("/getById/{id}")
	public R<PatientManageNodeInfoVo> getById(@PathVariable("id") Long id) {
		return R.ok(signNodeService.selectSignPatientManageRouteNodeById(id));
	}

	/**
	 * 根据节点类型查询节点列表详情
	 */
	@ApiOperation("根据节点类型查询节点列表详情")
	@GetMapping("/getNodeListByPatient")
	public R<List<PatientManageNodeInfoVo>> getNodeListByPatient(PatientTaskDto patientTaskDto) {
		return R.ok(signNodeService.getNodeListByPatient(patientTaskDto));
	}

	/**
	 * @description 患者详情--康复计划及记录
	 * @param signPatientRecordId 签约记录表主键
	 * @return 患者管理任务路径节点
	 * @Author haown
	 * @Date 2024-08-01 15:23
	*/
	@ApiOperation("患者详情--康复计划及记录")
	@GetMapping("/getNodeList/{signPatientRecordId}")
	public R<List<SignPatientTaskVo>> getNodeList(@PathVariable("signPatientRecordId") Long signPatientRecordId) {
		return R.ok(signNodeService.getNodeList(signPatientRecordId));
	}

	/**
	 * 任务审核--查询患者任务路径及节点
	 */
	@ApiOperation("任务审核--查询患者任务路径及节点")
	@GetMapping("/getRouteNodeList")
	public R<List<SignPatientManageRouteNodeVo>> getRouteNodeList(PatientTaskDto patientTaskDto) {
		if (patientTaskDto.getPatientId() == null) {
			throw new ServiceException("请选择患者!");
		}
		if (StringUtils.isBlank(patientTaskDto.getRouteCheckStatus())) {
			throw new ServiceException("请选择审核状态!");
		}
		if (StringUtils.isBlank(patientTaskDto.getTaskCreateType())) {
			throw new ServiceException("请选择任务创建类型!");
		}
		return R.ok(signNodeService.getRouteNodeList(patientTaskDto));
	}

	/**
	 * 审核任务
	 */
	@ApiOperation("审核任务")
	@PostMapping("/audit")
	public R<String> audit(@RequestBody RouteNodeCheckDto routeNodeCheckDto) {
		int flag = signNodeService.audit(routeNodeCheckDto);
		return flag < 0 ? R.fail() : R.ok();
	}

	/**
	 * 查询患者任务节点
	 */
	@ApiOperation("生成数据---任务节点列表")
	@GetMapping("/getSpecialDiseaseNode")
	public R<List<PatientSpecialDiseaseNodeVo>> getSpecialDiseaseNode(PatientTaskDto patientTaskDto) {
		if (patientTaskDto.getPatientId() == null) {
			throw new ServiceException("请选择患者!");
		}
		return R.ok(signNodeService.getSpecialDiseaseNode(patientTaskDto));
	}

	/**
	 * AI通话获取通话记录及录音
	 */
	@ApiOperation("AI通话获取通话记录及录音")
	@GetMapping("/getPhoneDialVideo/{id}")
	public R<AIDialRecordVo> getPhoneDialVideo(@PathVariable("id") Long id) {
		return R.ok(signNodeService.getPhoneDialVideo(id));
	}

	/**
	 * 测试生成患者档案列表数据用
	 */
	@GetMapping("/generateStatistic")
	public R<String> generateStatistic() {
		signNodeService.generateStatistic();
		return R.ok();
	}

    @ApiOperation("外呼记录")
    @GetMapping("/uploadRobotPublishRecord")
    public TableDataInfo uploadRobotPublishRecord(UploadRobotPublishRecordDto uploadRobotPublishRecordDto) {
        startPage();
        List<UploadRobotPublishRecordVo> list = signNodeService.uploadRobotPublishRecord(uploadRobotPublishRecordDto);
        return getDataTable(list);
    }

	@ApiOperation("外呼记录导出")
	@PostMapping("/export")
	public void export(HttpServletResponse response, UploadRobotPublishRecordDto uploadRobotPublishRecordDto) {
		List<UploadRobotPublishRecordVo> list = signNodeService.uploadRobotPublishRecord(uploadRobotPublishRecordDto);
		if (CollectionUtils.isNotEmpty(list)){
			list = list.stream().distinct().collect(Collectors.toList());
		}
		ExcelUtil<UploadRobotPublishRecordVo> util = new ExcelUtil<UploadRobotPublishRecordVo>(UploadRobotPublishRecordVo.class);
		util.exportExcel(response, list, "外呼记录");
	}
}
