package com.fs.his.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.his.dto.CPWUserAndAddressAddDTO;
import com.fs.his.dto.CPWUserInfoCollectionAddDTO;
import com.fs.his.validator.cpw.UserInformationValidator;
import com.fs.his.vo.CPWPrescribeDataVO;
import com.fs.his.vo.FsQuestionAndAnswerVO;
import com.fs.his.vo.SubmitUserInformationVO;
import com.fs.hisStore.service.IOpenApiCPWUserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
/**
 * 用户信息采集接口（第三方使用）---超拼网
 * */
@RestController
@RequestMapping("/ysy-api/template")
public class OpenApiFsUserInformationController extends BaseController {

    @Autowired
    private IOpenApiCPWUserInformationService openApiFsUserInformationService;

    @Autowired
    private UserInformationValidator userInformationValidator;
    /**
     * 获取信息采集问题模板
     * */
    @GetMapping("/getInformationTemplate")
    public AjaxResult getInformationTemplate(){
        try {
            FsQuestionAndAnswerVO informationTemplate = openApiFsUserInformationService.getInformationTemplate();
            if (informationTemplate == null) {
                return AjaxResult.error("未找到信息采集模板");
            }
            return AjaxResult.success(informationTemplate);
        } catch (CustomException e) {
            logger.error("获取信息采集模板业务异常：{}", e.getMessage());
            if (e.getCode() != null) {
                return AjaxResult.error(e.getCode(), e.getMessage());
            }
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("获取信息采集模板系统异常：", e);
            return AjaxResult.error("系统处理异常，请稍后重试");
        }
    }

    /**
     * 提交---超拼网---患者待开方信息
     * */
    @PostMapping("/submitPatientPrescriptionInfo")
    public AjaxResult submitPatientPrescriptionInfo(@RequestBody CPWUserInfoCollectionAddDTO collectionAddDTO) {
        try {
            // 参数校验
            userInformationValidator.validateCollection(collectionAddDTO);
            SubmitUserInformationVO result = openApiFsUserInformationService.submitPatientPrescriptionInfo(collectionAddDTO);
            return AjaxResult.success(result);
        } catch (CustomException e) {
            logger.error("提交---超拼网---患者待开方信息业务异常：{}", e.getMessage());
            Map<String, Object> errorDetail = new HashMap<>();
            errorDetail.put("errorCode", e.getCode() != null ? e.getCode() : 500);
            errorDetail.put("errorMessage", e.getMessage());
            errorDetail.put("timestamp", DateUtils.getTime());
            errorDetail.put("path", "/ysy-api/template/submitPatientPrescriptionInfo");

            AjaxResult result = AjaxResult.error(e.getCode() != null ? e.getCode() : 500, e.getMessage());
            result.put("data", errorDetail);

            return result;

        } catch (Exception e) {
            logger.error("提交用户采集信息系统异常：", e);
            return AjaxResult.error("系统繁忙，请稍后重试");
        }
    }

    /**
     * 模拟定时推送处方信息给第三方
     * */
    @PostMapping("/pushPrescription")
    public AjaxResult pushPrescription() {
        List<CPWPrescribeDataVO> cpwPrescribeDataVOS = openApiFsUserInformationService.pushPrescription();
        return AjaxResult.success(cpwPrescribeDataVOS);
    }

    /**
     * 推送处方回执接口
     */
    @PostMapping("/prescriptionReceipt")
    public AjaxResult prescriptionReceipt(@RequestBody List<Long> prescriptionIds) {
        int result = openApiFsUserInformationService.prescriptionReceipt(prescriptionIds);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }





    /**
     * 提交用户采集信息----------已弃用！！！！
     * */
    @PostMapping("/submitUserInformation")
    public AjaxResult submitUserInformation(@RequestBody CPWUserInfoCollectionAddDTO collectionAddDTO) {
        try {
            // 参数校验
            userInformationValidator.validateCollection(collectionAddDTO);
            SubmitUserInformationVO result = openApiFsUserInformationService.submitUserInformation(collectionAddDTO);
            return AjaxResult.success(result);
        } catch (CustomException e) {
            logger.error("提交用户采集信息业务异常：{}", e.getMessage());
            Map<String, Object> errorDetail = new HashMap<>();
            errorDetail.put("errorCode", e.getCode() != null ? e.getCode() : 500);
            errorDetail.put("errorMessage", e.getMessage());
            errorDetail.put("timestamp", DateUtils.getTime());
            errorDetail.put("path", "/ysy-api/template/submitUserInformation");

            AjaxResult result = AjaxResult.error(e.getCode() != null ? e.getCode() : 500, e.getMessage());
            result.put("data", errorDetail);

            return result;

        } catch (Exception e) {
            logger.error("提交用户采集信息系统异常：", e);
            return AjaxResult.error("系统繁忙，请稍后重试");
        }
    }

    /**
     * 创建用户信息与地址--超拼网----------已弃用！！！！
     * */
    @PostMapping("/createUserInformation")
    public AjaxResult createUserInformationCPW(@RequestBody CPWUserAndAddressAddDTO cpwUserAndAddressAddDTO) {
        try {
            // 参数校验
            userInformationValidator.validateBaseInfo(cpwUserAndAddressAddDTO.getUserBaseInfo());
            userInformationValidator.validateUserAndAddress(cpwUserAndAddressAddDTO.getAddress());
            SubmitUserInformationVO result = openApiFsUserInformationService.createUserInformationCPW(cpwUserAndAddressAddDTO);
            return AjaxResult.success(result);
        } catch (CustomException e) {
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch(Exception e) {
            logger.error("创建用户信息与地址业务异常：", e);
            return AjaxResult.error("系统处理异常，请稍后重试");
        }
    }
}
