package com.ywt.alipaympapi.service.impl;

import com.ywt.alipaympapi.core.utils.*;
import com.ywt.alipaympapi.models.BaseResponse;
import com.ywt.alipaympapi.models.enums.LastChoiceEnum;
import com.ywt.alipaympapi.models.vo.InpatientVo;
import com.ywt.core.exception.AppMessageException;
import com.ywt.gapi.Result;
import com.ywt.gapi.ResultCode;
import com.ywt.gapi.order.CreateDepositRequest;
import com.ywt.gapi.order.CreateDepositResponse;
import com.ywt.gapi.order.OrderServiceGrpc;
import com.ywt.gapi.ordercenter.CreateAlipayOrderRequest;
import com.ywt.gapi.ordercenter.CreateAlipayOrderResponse;
import com.ywt.gapi.ordercenter.OrderCenterServiceGrpc;
import com.ywt.gapi.third.taihe.*;
import com.ywt.gapi.user.GetMedicalCardListRequest;
import com.ywt.gapi.user.GetMedicalCardListResponse;
import com.ywt.gapi.user.MedicalCard;
import com.ywt.gapi.user.UserServiceGrpc;
import com.ywt.request.inpatient.DailyBillDetailsRequest;
import com.ywt.request.inpatient.DailyBillRequest;
import com.ywt.request.inpatient.DepositTradeDetailsRequest;
import com.ywt.request.inpatient.DepositTradeListRequest;
import com.ywt.request.inpatient.DepositTradeRequest;
import com.ywt.request.inpatient.DepositTradeResultRequest;
import com.ywt.request.inpatient.InpatientCheckInRequest;
import com.ywt.request.inpatient.InpatientDeleteRequest;
import com.ywt.request.inpatient.InpatientDetailsRequest;
import com.ywt.request.inpatient.InpatientListRequest;
import com.ywt.request.inpatient.InpatientNoticeRequest;
import com.ywt.request.inpatient.InpatientUpdateRequest;
import com.ywt.request.inpatient.NoticeAuthRequest;
import com.ywt.request.inpatient.SettlementAuthRequest;
import com.ywt.request.inpatient.SettlementDetailsRequest;
import com.ywt.request.inpatient.SettlementExecuteRequest;
import com.ywt.request.inpatient.SettlementListRequest;
import com.ywt.request.inpatient.SettlementQueryRequest;
import com.ywt.response.inpatient.DailyBillDetailsResponseData;
import com.ywt.response.inpatient.DailyBillResponseData;
import com.ywt.response.inpatient.DepositTradeDetailsResponseData;
import com.ywt.response.inpatient.DepositTradeListResponseData;
import com.ywt.response.inpatient.DepositTradeResponseData;
import com.ywt.response.inpatient.DepositTradeResultResponseData;
import com.ywt.response.inpatient.InpatientCheckInResponseData;
import com.ywt.response.inpatient.InpatientDeleteResponseData;
import com.ywt.response.inpatient.InpatientDetailsResponseData;
import com.ywt.response.inpatient.InpatientListResponseData;
import com.ywt.response.inpatient.InpatientNoticeResponseData;
import com.ywt.response.inpatient.InpatientUpdateResponseData;
import com.ywt.response.inpatient.NoticeAuthResponseData;
import com.ywt.response.inpatient.SettlementAuthResponseData;
import com.ywt.response.inpatient.SettlementDetailsResponseData;
import com.ywt.response.inpatient.SettlementExecuteResponseData;
import com.ywt.response.inpatient.SettlementListResponseData;
import com.ywt.response.inpatient.SettlementQueryResponseData;
import com.ywt.alipaympapi.service.InpatientService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.util.calendar.BaseCalendar;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author
 */
@Slf4j
@Service
public class InpatientServiceImpl implements InpatientService {


    @Autowired
    private TaiheServiceGrpc.TaiheServiceBlockingStub taiheServiceBlockingStub;
    @Autowired
    private UserServiceGrpc.UserServiceBlockingStub userServiceBlockingStub;
    @Autowired
    private OrderServiceGrpc.OrderServiceBlockingStub orderServiceBlockingStub;

    @Autowired
    private OrderCenterServiceGrpc.OrderCenterServiceBlockingStub orderCenterServiceBlockingStub;

    @Override
    public SettlementQueryResponseData informTradeResult(SettlementQueryRequest request) {
        return null;
    }

    @Override
    public InpatientCheckInResponseData checkIn(InpatientCheckInRequest request) {
        return null;
    }

    @Override
    public InpatientUpdateResponseData updateInpatient(InpatientUpdateRequest request) {
        return null;
    }

    @Override
    public List<InpatientListResponseData> inpatientList(InpatientListRequest request) throws Exception {
        int userId = Checker.parseInt(request.getUserId());
        List<InpatientListResponseData> list = new ArrayList<>();
        if (userId <= 0) return list; // 这个接口进入小程序首页就会调用；如果用户没有授权，返回空数据，而不是弹出获取用户信息授权（会违反小程序审核规则）；

        int hospId = BizUtil.getCurrentHospitalId();
        GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
                .setHospitalId(hospId)
                .setUserId(userId)
                .setFlag(0)
                .build();
        GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
        Result result = getMedicalCardListResponse.getResult();
        if (result.getCode() == ResultCode.SUCCEED) {
            for (MedicalCard medicalCard : getMedicalCardListResponse.getMedicalCardsList()) {
                InpatientListResponseData data = new InpatientListResponseData();
                data.setPatientId(medicalCard.getPatientId());
                data.setInpatientId(medicalCard.getPatientId());
                data.setInpatientName(medicalCard.getPatientName());
                data.setSex(medicalCard.getSex() == 1 ? "男" : "女");
                data.setInpatientAge(Integer.parseInt(getAgeFromMedicalCard(medicalCard)));
                InpatientVo vo = getInpatientVo(userId, medicalCard.getPatientId());
                data.setAdmNo(Checker.getStringValue(vo.getAdmNo()));
                list.add(data);
            }
            return list;
        }
        return list;
    }

    @Override
    public InpatientDeleteResponseData deleteInpatient(InpatientDeleteRequest request) {
        return null;
    }

    @Override
    public List<DailyBillResponseData> dailyBillList(DailyBillRequest request) throws Exception {
        int userId = Checker.parseInt(request.getUserId());
        List<DailyBillResponseData> dailyBillList = new ArrayList<>();
        if (userId <= 0) return dailyBillList; // 这个接口进入小程序首页就会调用；如果用户没有授权，返回空数据，而不是弹出获取用户信息授权（会违反小程序审核规则）；

        String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
        int hospId = BizUtil.getCurrentHospitalId();
        List<MedicalCard> userMedCardList = getUserMedCardList(userId, hospId);
        if (Checker.isNone(userMedCardList)) {
            return dailyBillList;
        }
        for (MedicalCard m : userMedCardList) {
            DailyBillResponseData data = new DailyBillResponseData();
            String hisPatientId = m.getHisPatientId();
            InpatientVo vo = getInpatientVo(userId, hisPatientId);
            String totalAmount = Checker.getStringValue(vo.getTotalAmount());
            String depositBalance = Checker.getStringValue(vo.getDepositBalance());
            String admNo = Checker.getStringValue(vo.getAdmNo());
            GetDayFeeListRequest req = GetDayFeeListRequest.newBuilder()
                    .setPatientId(hisPatientId)
                    .setBillDate(date)
                    .setHospitalId(hospId)
                    .setAdmNo(admNo)
                    .build();
            GetDayFeeListResponse resp = taiheServiceBlockingStub.getDayFeeList(req);
            int todaySpend = 0;
            if (resp.getCode() == BaseResponse.SUCCEED) {
                List<DayFeeListItem> list = resp.getListList();
                for (DayFeeListItem item : list) {
                    todaySpend += item.getTotalAmt();
                }
                String patientName = Checker.getStringValue(resp.getPatientName());
                data.setDepositBalance(depositBalance);
                data.setSpendTotal(totalAmount);
                data.setInpatientName(patientName);
                data.setInpatientId(Checker.getStringValue(hisPatientId));
                data.setCheckInDate(Checker.getStringValue(""));
                data.setSpendToday(String.format("%.2f", todaySpend / 100d));
                data.setId(Checker.getStringValue(""));
                dailyBillList.add(data);
            }
        }
        return dailyBillList;
    }


    /**
     * 根据 userId & hospitalId 获取用户可用就诊卡列表
     */
    public List<MedicalCard> getUserMedCardList(int userId, int hospitalId) throws AppMessageException {
        GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
                .setHospitalId(hospitalId)
                .setUserId(userId)
                .build();
        GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
        Result result = getMedicalCardListResponse.getResult();
        if (result.getCode() != ResultCode.SUCCEED) throw new AppMessageException(result.getInfo());
        return getMedicalCardListResponse.getMedicalCardsList();
    }


    private InpatientVo getInpatientVo(int userId, String hisPatientId) throws Exception {
        InpatientVo vo = new InpatientVo();
        GetInPatInfoRequest req = GetInPatInfoRequest.newBuilder()
                .setPatientId(hisPatientId)
                .setHospitalId(BizUtil.getCurrentHospitalId())
                .build();
        GetInPatInfoResponse resp = taiheServiceBlockingStub.getInPatInfo(req);
        if (resp.getCode() == BaseResponse.SUCCEED) {
            String totalAmount = String.format("%.2f", Checker.getDoubleValue(resp.getTotalAmount() / 100d));
            String depositBalance = String.format("%.2f", Checker.getDoubleValue(resp.getDepositBalance() / 100d));
            vo.setAdmNo(resp.getAdmNo());
            vo.setTotalAmount(totalAmount);
            vo.setDepositBalance(depositBalance);
            return vo;
        }
        return vo;
    }


    /**
     * 根据用户ID获取用户诊疗卡信息（选择上一次选择的诊疗卡，或者是第一张）
     *
     * @return {@link MedicalCard}
     */
    private MedicalCard getMedicalCardByHisPatientId(int userId) {
        MedicalCard medicalCard = null;
        // 则获取当前用户的第一张诊疗卡，或者是上一次选择的诊疗卡（优先）
        GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
                .setUserId(userId)
                .build();
        GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
        Result result = getMedicalCardListResponse.getResult();
        if (result.getCode() == ResultCode.SUCCEED) {
            List<MedicalCard> cardList = getMedicalCardListResponse.getMedicalCardsList();
            if (!Checker.isNone(cardList)) {
                medicalCard = cardList.stream().filter(p -> p.getLastChoice() == LastChoiceEnum.True.getValue()).findFirst().orElse(null);
                if (Checker.isNone(medicalCard)) {
                    medicalCard = cardList.get(0);
                }
            }
        }
        return medicalCard;
    }

    @Override
    public DailyBillDetailsResponseData dailyBillDetail(DailyBillDetailsRequest request) throws Exception {
        DailyBillDetailsResponseData data = new DailyBillDetailsResponseData();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        int hospitalId = BizUtil.getCurrentHospitalId();
        String date = request.getBillDate();
        List<MedicalCard> userMedCardList = new ArrayList<>();
        userMedCardList = getUserMedCardList(userId, hospitalId);
        if (Checker.isNone(userMedCardList)) {
            throw new AppMessageException("该用户没有绑定诊疗卡");
        }
        for (MedicalCard m : userMedCardList) {
            if (m.getPatientId().equals(request.getHisPatientId())) {
                InpatientVo vo = null;
                String admNo = "";
                String hisPatientId = m.getHisPatientId();
                vo = getInpatientVo(userId, hisPatientId);
                String totalAmount = Checker.getStringValue(vo.getTotalAmount());
                String depositBalance = Checker.getStringValue(vo.getDepositBalance());
                admNo = Checker.getStringValue(vo.getAdmNo());
                GetDayFeeListRequest req = GetDayFeeListRequest.newBuilder()
                        .setPatientId(hisPatientId)
                        .setBillDate(request.getBillDate())
                        .setHospitalId(hospitalId)
                        .setAdmNo(admNo)
                        .build();
                GetDayFeeListResponse resp = taiheServiceBlockingStub.getDayFeeList(req);
                int todaySpend = 0;
                if (resp.getCode() == BaseResponse.SUCCEED) {
                    List<SettlementQueryResponseData.Item> payItemList = new ArrayList<>();
                    List<DayFeeListItem> list = resp.getListList();
                    for (DayFeeListItem item : list) {
                        SettlementQueryResponseData.Item it = new SettlementQueryResponseData.Item();
                        it.setItemName(Checker.getStringValue(item.getItemName()));
                        it.setItemCount(Checker.getIntegerValue(Integer.parseInt(Checker.getStringValue(item.getQuantity()))));
                        it.setItemPrice(String.format("%.2f", item.getPrice() / 100d));
                        it.setItemAmount(String.format("%.2f", item.getTotalAmt() / 100d));
                        todaySpend += item.getTotalAmt();
                        payItemList.add(it);
                    }
                    data.setBillDate(request.getBillDate());
                    data.setDepositBalance(depositBalance);
                    data.setInpatientName(m.getPatientName());
                    data.setPayItemList(payItemList);
                    data.setSpendTotal(totalAmount);
                    data.setSpendToday(String.format("%.2f", todaySpend / 100d));
                    data.setCheckInDate(Checker.getStringValue(""));
                    data.setInpatientId(resp.getPatientId());
                    data.setId(Checker.getStringValue(""));
                }
            }

        }
        return data;
    }

    // 押金缴纳（创建支付宝订单，返回支付宝交易号）
    @Override
    public DepositTradeResponseData depositTrade(DepositTradeRequest request) throws AppMessageException {
        String hisPatientId = Checker.getStringValue(request.getInpatientId());
        String strAmount = Checker.getStringValue(request.getAmount());
        String admNo = Checker.getStringValue(request.getAdmNo());
        CheckUtil.ensureNotEmpty(admNo, "当前就诊人非住院病人，不能缴费");
        int userId = ContextHelper.getCurrentUserIdWrapped();
        int amount = 0;
        try {
            amount = BizUtil.yuan2Cent(strAmount);
        } catch (NumberFormatException e) {
            throw new AppMessageException("金额有误：" + strAmount);
        }
        CheckUtil.ensureLargerThanZero(amount, "充值金额需大于 0");
        // 创建医务通系统订单
        CreateDepositRequest cdr = CreateDepositRequest.newBuilder()
                .setUserId(userId)
                .setHisPatientId(hisPatientId)
                .setAdmNo(admNo)
                .setAmount(amount)
                .setTerminal(ContextHelper.getCurrentTerminalWrapped())
                .build();
        CreateDepositResponse res = orderServiceBlockingStub.createDeposit(cdr);
        if (res.getCode() != ResultCode.SUCCEED_VALUE) throw new AppMessageException(res.getMsg());
        // 支付宝下单
        int orderId = res.getOrderId();
        String orderNo = res.getOrderNo();
        CreateAlipayOrderResponse response = orderCenterServiceBlockingStub.createAlipayOrder(CreateAlipayOrderRequest.newBuilder()
                .setBody("住院押金")
                .setAlipayUid(request.getAlipayUid())
                .setOrderId(orderId)
                .setOrderNo(orderNo)
                .setAmount(amount)
                .setUserId(userId)
                .setTerminal(ContextHelper.getCurrentTerminalWrapped())
                .build());
        if (response.getCode() != ResultCode.SUCCEED_VALUE)
            throw new AppMessageException("支付宝下单：" + response.getInfo());

        DepositTradeResponseData data = new DepositTradeResponseData();
        data.setDepositId(String.valueOf(orderId));
        data.setOutTradeNo(orderNo);
        data.setTradeNo(response.getTradeNo());
        return data;
    }

    @Override
    public DepositTradeResultResponseData queryTradeResult(DepositTradeResultRequest request) {
        return null;
    }

    @Override
    public List<DepositTradeListResponseData> queryTradeList(DepositTradeListRequest request) throws Exception {
        String hisPatientId = request.getInpatientId();
        List<DepositTradeListResponseData> depositList = new ArrayList<>();
        if (Checker.isNone(hisPatientId)) {
            throw new AppMessageException("该用户没有绑定诊疗卡");
        }
        GetInPatInfoRequest req = GetInPatInfoRequest.newBuilder()
                .setPatientId(hisPatientId)
                .setHospitalId(BizUtil.getCurrentHospitalId())
                .build();
        GetInPatInfoResponse resp = taiheServiceBlockingStub.getInPatInfo(req);
        if (resp.getCode() == BaseResponse.SUCCEED) {
            String admNo = Checker.getStringValue(resp.getAdmNo());
            GetCashListRequest requ = GetCashListRequest.newBuilder()
                    .setPatientId(hisPatientId)
                    .setAdmNo(admNo)
                    .setHospitalId(BizUtil.getCurrentHospitalId())
                    .build();
            GetCashListResponse res = taiheServiceBlockingStub.getCashList(requ);
            if (res.getCode() == BaseResponse.SUCCEED) {
                List<CashListItem> list =  res.getListList();
                for (int size = list.size(), i = 0; i < size; i++) {
                    CashListItem item = list.get(size - i - 1);
                    DepositTradeListResponseData da = new DepositTradeListResponseData();
                    da.setInpatientName(Checker.getStringValue(request.getName()));
                    da.setAmount(String.format("%.2f", item.getPayAmout() / 100d));
                    da.setOutTradeNo(item.getReceiptNo());
                    da.setPayTime(item.getPayDate() + "  " + item.getPayTime());
                    da.setTradeNo(Checker.getStringValue(""));
                    da.setDepositId(Checker.getStringValue(""));
                    String depositBalance = String.format("%.2f", resp.getDepositBalance() / 100d);
                    da.setDepositBalance(depositBalance);
                    da.setStatus(Checker.getStringValue(""));
                    da.setDepositId(Checker.getStringValue(item.getReceiptNo()));
                    da.setAdmNo(Checker.getStringValue(admNo));
                    da.setHisPatientId(hisPatientId);
                    depositList.add(da);
                }
                return depositList;
            }
            return depositList;
        }
        return depositList;
    }

    @Override
    public DepositTradeDetailsResponseData queryTradeDetails(DepositTradeDetailsRequest request) throws Exception {
        String hisPatientId = request.getHisPatientId();
        if (Checker.isNone(hisPatientId)) {
            throw new AppMessageException("该用户没有绑定诊疗卡");
        }
        if (Checker.isNone(request.getReceiptNo())) {
            throw new AppMessageException("没有押金缴纳记录");
        }
        GetInPatInfoRequest req = GetInPatInfoRequest.newBuilder()
                .setPatientId(hisPatientId)
                .setHospitalId(BizUtil.getCurrentHospitalId())
                .build();
        GetInPatInfoResponse resp = taiheServiceBlockingStub.getInPatInfo(req);
        if (resp.getCode() == BaseResponse.SUCCEED) {
            String admNo = Checker.getStringValue(resp.getAdmNo());
            GetCashListRequest requ = GetCashListRequest.newBuilder()
                    .setPatientId(hisPatientId)
                    .setAdmNo(admNo)
                    .setHospitalId(BizUtil.getCurrentHospitalId())
                    .build();
            GetCashListResponse res = taiheServiceBlockingStub.getCashList(requ);
            DepositTradeDetailsResponseData data = new DepositTradeDetailsResponseData();
            if (res.getCode() == BaseResponse.SUCCEED) {
                List<CashListItem> list = res.getListList();
                DepositTradeDetailsResponseData da = new DepositTradeDetailsResponseData();
                for (CashListItem i : list) {
                    if (request.getReceiptNo().equals(i.getReceiptNo())) {
                        da = new DepositTradeDetailsResponseData();
                        da.setInpatientName(Checker.getStringValue(request.getName()));
                        da.setAmount(String.format("%.2f", i.getPayAmout() / 100d));
                        da.setOutTradeNo(i.getReceiptNo());
                        da.setPayTime(i.getPayDate() + "    " + i.getPayTime());
                        da.setTradeNo(Checker.getStringValue(""));
                        da.setDepositId(Checker.getStringValue(""));
                        String depositBalance = String.format("%.2f", resp.getDepositBalance() / 100d);
                        da.setDepositBalance(depositBalance);
                        da.setStatus(Checker.getStringValue(i.getPayFlag()));
                        da.setInpatientNo(hisPatientId);
                        da.setAdmNo(admNo);
                        return da;

                    }
                }
                throw new AppMessageException("没有押金缴纳记录");
            }
            throw new AppMessageException("没有押金缴纳记录");
        }
        throw new AppMessageException("没有押金缴纳记录");
    }

    @Override
    public List<SettlementListResponseData> settlementList(SettlementListRequest request) {
        return null;
    }

    @Override
    public SettlementAuthResponseData settlementAuth(SettlementAuthRequest request) {
        return null;
    }

    @Override
    public SettlementExecuteResponseData settlementExecute(SettlementExecuteRequest request) {
        return null;
    }

    @Override
    public SettlementDetailsResponseData settlementDetails(SettlementDetailsRequest request) {
        return null;
    }

    @Override
    public InpatientNoticeResponseData inpatientNotice(InpatientNoticeRequest request) {
        return null;
    }

    @Override
    public NoticeAuthResponseData noticeAuth(NoticeAuthRequest request) {
        return null;
    }

    private String getAgeFromMedicalCard(MedicalCard card) {
        int age = -1;
        String idNo = card.getIdNo();
        String birthday = card.getBirthday();
        if (!StringHelper.isNullOrEmpty(idNo)) {
            try {
                age = IdCardUtil.getCurrentAge(idNo);
            } catch (Exception e) {
                log.error("IsvMedicCardServiceImpl#getAgeFromMedicalCard(idNo = {}): 无法获取年龄 {}", idNo, e.getMessage(), e);
            }
        } else if (!StringHelper.isNullOrEmpty(birthday)) {
            try {
                age = IdCardUtil.getCurrentAgeByBirthday(birthday, IdCardUtil.DATE_PATTERN2);
            } catch (Exception e) {
                log.error("IsvMedicCardServiceImpl#getAgeFromMedicalCard(birthday = {}): 无法获取年龄 {}", birthday, e.getMessage(), e);
            }
        }
        // 不返回年龄为 "-1"
        if (age < 0) {
            return "";
        }
        return String.valueOf(age);
    }

    @Override
    public InpatientDetailsResponseData inpatientDetails(InpatientDetailsRequest request) throws Exception {
        int userId = Checker.parseInt(request.getUserId());

        if (userId <= 0) {
            throw new AppMessageException("userId不能为空");
        }
        ;
        int hospId = BizUtil.getCurrentHospitalId();
        if (Checker.isNone(request.getInpatientId())) {
            throw new AppMessageException("hisPationId不能为空");
        }
        String hisPationId = Checker.getStringValue(request.getInpatientId());
        GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
                .setHospitalId(hospId)
                .setUserId(userId)
                .setFlag(0)
                .build();
        GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
        Result result = getMedicalCardListResponse.getResult();
        InpatientDetailsResponseData data = new InpatientDetailsResponseData();
        if (result.getCode() == ResultCode.SUCCEED) {
            for (MedicalCard medicalCard : getMedicalCardListResponse.getMedicalCardsList()) {
                if (medicalCard.getPatientId().equals(hisPationId)) {
                    data = new InpatientDetailsResponseData();
                    data.setPatientId(hisPationId);
                    data.setInpatientId(hisPationId);
                    data.setInpatientName(medicalCard.getPatientName());
                    data.setSex(medicalCard.getSex() == 1 ? "男" : "女");
                    Date date = DateUtil.stringToDate(medicalCard.getBirthday(), DateUtil.DADE_FROMAT_YMD);
                    data.setInpatientAge(Integer.parseInt(getAgeFromMedicalCard(medicalCard)));
                    InpatientVo vo = getInpatientVo(userId, medicalCard.getPatientId());
                    return data;
                }
            }
            return data;
        }
        return data;
    }
}