package com.ywt.alipaympapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ywt.alipaympapi.core.utils.*;
import com.ywt.alipaympapi.models.BaseResponse;
import com.ywt.alipaympapi.models.enums.RelationshipEnum;
import com.ywt.alipaympapi.models.enums.SexEnum;
import com.ywt.alipaympapi.models.enums.SmsCodeTypeEnum;
import com.ywt.alipaympapi.service.AuthService;
import com.ywt.alipaympapi.service.IsvMedicCardService;
import com.ywt.alipaympapi.service.services.MessageSrv;
import com.ywt.core.exception.AppMessageException;
import com.ywt.gapi.Result;
import com.ywt.gapi.ResultCode;
import com.ywt.gapi.ResultInt;
import com.ywt.gapi.taihe.register.CreateMedicalCardRequest;
import com.ywt.gapi.taihe.register.CreateMedicalCardResponse;
import com.ywt.gapi.taihe.register.TaiheRegisterServiceGrpc;
import com.ywt.gapi.third.taihe.GetPatientInfoRequest;
import com.ywt.gapi.third.taihe.GetPatientInfoResponse;
import com.ywt.gapi.third.taihe.TaiheServiceGrpc;
import com.ywt.gapi.user.*;
import com.ywt.model.PageVO;
import com.ywt.model.Pagination;
import com.ywt.request.card.UpdatePatientRequest;
import com.ywt.request.card.*;
import com.ywt.response.card.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class IsvMedicCardServiceImpl implements IsvMedicCardService {
    @Autowired
    private TaiheRegisterServiceGrpc.TaiheRegisterServiceBlockingStub taiheRegisterServiceBlockingStub;
    @Autowired
    private UserServiceGrpc.UserServiceBlockingStub userServiceBlockingStub;
    @Autowired
    private MessageSrv messageSrv;
    @Autowired
    private TaiheServiceGrpc.TaiheServiceBlockingStub taiheServiceBlockingStub;

    public static final int CARD_TYPE_NORMAL = 0; // 普通卡
    public static final int CARD_TYPE_MED_INSU = 1; // 医保卡
    public static final String CARD_TYPE_STR_NORMAL = "0"; // 普通卡
    public static final String CARD_TYPE_STR_MED_INSU = "1"; // 医保卡

    @Override
    public CreateOrBindCardResponseData createOrBindMedicCard(CreateOrBindCardRequest request) throws Exception {
        //用户唯一id
        // String userId = request.getUserId();
        //标志新建or绑定操作; 0=新建 1=绑定
        Integer type = request.getType();
        //就诊卡卡号，仅绑定操作时有效
        String cardNum = request.getCardNum();
        //性别; 男|女
        String sex = request.getSex();
        //生日（yyyy-MM-dd）
        String birthDay = request.getBirthDay();
        //验证码
        String authCode = request.getAuthCode();
        //关系
        String relationShip = request.getRelationShip();
        //是否默认（1-是，2-否）
        String defaultCard = request.getDefaultCard();
        //手机号
        String phoneNo = request.getPhoneNumber();
        //身份证号码
        String idNo = request.getIdCardNo();
        //姓名
        String patientName = Checker.getStringValue(request.getName());
        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        CreateOrBindCardResponseData resp = new CreateOrBindCardResponseData();
        // type: 标志新建or绑定操作; 0=新建 1=绑定
        if (type == 0) {
            String city = "广州市";
            String address = "默认详情地址";
            String isInsu = "";
            if (!IdCardUtil.verify(idNo)) {
                throw new AppMessageException(String.format("身份证不合法：%s", idNo));
            }
            CheckUtil.ensureValidPhoneNumber(phoneNo, String.format("手机号不合法：%s", phoneNo));
            CheckUtil.ensureNotEmpty(patientName, "请完善姓名");
            CreateMedicalCardResponse response = taiheRegisterServiceBlockingStub.createMedicalCard(CreateMedicalCardRequest.newBuilder()
                    .setPhoneNo(phoneNo)
                    .setIdNo(idNo)
                    .setPatientName(patientName)
                    .setCity(city)
                    .setAddress(address)
                    .setIsInsu(isInsu)
                    .setHospitalId(hospitalId)
                    .setUserId(userId)
                    .build());
            if (response.getCode() == BaseResponse.SUCCEED) {
                resp.setUserId(request.getUserId());
                resp.setCardNum(response.getMedicalCardNo());
                resp.setName(patientName);
                resp.setSex(sex);
                resp.setBirthDay(birthDay);
                resp.setIdCardNo(idNo);
                resp.setPhoneNumber(phoneNo);
                resp.setBalance("0.00");
            } else {
                throw new AppMessageException(response.getInfo());
            }
            return resp;
        }
        // type: 标志新建or绑定操作; 0=新建 1=绑定
        if (type == 1) {
            if (hospitalId < 1) {
                throw new AppMessageException("请选择医院");
            }
            if (StringHelper.isNullOrWhiteSpace(cardNum)) {
                throw new AppMessageException("请填写就诊卡号");
            }
            if (StringHelper.isNullOrWhiteSpace(patientName)) {
                throw new AppMessageException("请填写真实的姓名");
            }
            if (StringHelper.isNullOrWhiteSpace(phoneNo)) {
                throw new AppMessageException("请填写在医院预留的手机号");
            }
            if (StringHelper.isNullOrWhiteSpace(authCode)) {
                throw new AppMessageException("请输入验证码");
            }
            BaseResponse response = messageSrv.checkSmsCode(phoneNo, authCode, SmsCodeTypeEnum.Medical_Card_Bind.getValue());
            if (response.getCode() != BaseResponse.SUCCEED) {
                throw new AppMessageException(response.getInfo());
            }
            // 通过枚举类的方法，将文字转换成数字
            int relationshipInt = RelationshipEnum.valueOf(relationShip).getValue();
            // defaultCardInt: 1-是，0-否
            int defaultCardInt = "1".equals(defaultCard) ? 1 : 0;
            AddMedicalCardRequest addMedicalCardRequest = AddMedicalCardRequest.newBuilder()
                    .setUserId(userId)
                    .setHospitalId(hospitalId)
                    .setCardNo(cardNum)
                    .setRealName(patientName)
                    .setMobile(phoneNo)
                    .setRelationship(relationshipInt)
                    .setDefaultCard(defaultCardInt)
                    .build();
            ResultInt resultInt = userServiceBlockingStub.addMedicalCard(addMedicalCardRequest);
            if (resultInt.getCode() == ResultCode.SUCCEED) {
                int cent = Checker.parseInt(resultInt.getInfo());
                double balance = cent / 100d;
                resp.setUserId(request.getUserId());
                resp.setCardNum(request.getCardNum());
                resp.setName(patientName);
                resp.setSex(sex);
                resp.setBirthDay(birthDay);
                resp.setIdCardNo(idNo);
                resp.setPhoneNumber(phoneNo);
                resp.setBalance(String.format("%.2f", balance));
            } else {
                throw new AppMessageException(response.getInfo());
            }
        }
        return resp;
    }

    @Override
    public UpdateCardResponseData updateMedicCard(UpdateCardRequest request) throws Exception {
        throw new AppMessageException("暂不支持修改诊疗卡功能");
    }

    @Override
    public QuerySingleCardResponseData querySingleMedicCard(QuerySingleCardRequest request) throws Exception {
        // 此处传诊疗卡ID和userId
        String cardNum = request.getCardNum();
        CheckUtil.ensureNotEmpty(cardNum, "诊疗卡号不能为空");
        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        List<MedicalCard> list = getUserMedCardList(userId, hospitalId).stream()
                .filter(c -> Checker.getStringValue(c.getCardNo()).equals(cardNum))
                .collect(Collectors.toList());
        if (list.isEmpty()) return null;
        MedicalCard medicalCard = list.get(0);
        double balance = medicalCard.getBalance() / 100d;
        QuerySingleCardResponseData item = new QuerySingleCardResponseData();
        item.setCardNum(medicalCard.getCardNo());//就诊卡卡号/医保卡卡号
        item.setType(CARD_TYPE_STR_NORMAL);// 标记查询卡类型
        item.setName(medicalCard.getPatientName());//姓名
        item.setSex(SexEnum.getSexDisplayName(medicalCard.getSex()));//性别
        item.setBirthDay(medicalCard.getBirthday());//生日
        item.setIdCardNo(medicalCard.getIdNo());//身份证号码
        item.setPhoneNumber(medicalCard.getMobile());//手机号
        item.setBalance(balance + "");//卡内余额
        item.setRelationShip(RelationshipEnum.valueOf(medicalCard.getRelationship()).getDisplayName()); // 关系
        item.setDefaultCard(medicalCard.getIsDefault() ? "是" : "否");// FIXME: 是否是默认就诊卡
        item.setId(medicalCard.getCardId());
        return item;
    }

    // 查询用户默认就诊卡，获取用户就诊卡列表，用身份证匹配
    @Override
    public QueryDefaultCardResponseData queryDefaultMedicCard(QueryDefaultCardRequest request) throws Exception {
        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        // 获取用户身份证
        User user = getUserInfoById(userId);
        String idNo = Checker.getStringValue(user.getIdno());
        MedicalCard medicalCard = getUserMedCardList(userId, hospitalId).stream()
                .filter(c -> idNo.equals(c.getIdNo()))
                .findFirst()
                .orElse(null);
        QueryDefaultCardResponseData item = new QueryDefaultCardResponseData();
        if (medicalCard == null) return item;
        double balance = medicalCard.getBalance() / 100d;
        item.setCardNum(medicalCard.getCardNo());//就诊卡卡号/医保卡卡号
        item.setType(CARD_TYPE_STR_NORMAL);// 标记查询卡类型，默认普通卡
        item.setName(medicalCard.getPatientName());//姓名
        item.setSex(SexEnum.getSexDisplayName(medicalCard.getSex()));//性别
        item.setBirthDay(medicalCard.getBirthday());//生日
        item.setIdCardNo(medicalCard.getIdNo());//身份证号码
        item.setPhoneNumber(medicalCard.getMobile());//手机号
        item.setBalance(balance + "");//卡内余额
        item.setRelationShip(RelationshipEnum.valueOf(medicalCard.getRelationship()).getDisplayName()); // 关系
        item.setDefaultCard(medicalCard.getIsDefault() ? "是" : "否");//是否是默认就诊卡
        item.setPatientId("");//如果是医保卡，传参保城市
        item.setId(medicalCard.getCardId());
        item.setHisPatientId(medicalCard.getHisPatientId());
        return item;
    }

    @Override
    public PageVO<QueryListCardResponseData> queryListMedicCard(QueryListCardRequest request) throws Exception {
        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        int total;
        List<QueryListCardResponseData> lst = new LinkedList<>();
        List<MedicalCard> medicalCards = getUserMedCardList(userId, hospitalId);
        total = medicalCards.size();
        for (MedicalCard medicalCard : medicalCards) {
            double balance = medicalCard.getBalance() / 100d;
            QueryListCardResponseData item = new QueryListCardResponseData();
            item.setCardNum(medicalCard.getCardNo());//就诊卡卡号/医保卡卡号
            item.setType(CARD_TYPE_NORMAL);//标记查询卡类型
            item.setName(medicalCard.getPatientName());//姓名
            item.setSex(SexEnum.getSexDisplayName(medicalCard.getSex()));//性别
            item.setBirthDay(medicalCard.getBirthday());//生日
            item.setIdCardNo(medicalCard.getIdNo());//身份证号码
            item.setPhoneNumber(medicalCard.getMobile());//手机号
            item.setBalance(balance + "");//卡内余额
            item.setIsDefault(medicalCard.getIsDefault());//是否是默认就诊卡
            item.setCity("");//如果是医保卡，传参保城市
            item.setCardOrgName("");//如果是医保卡，传发卡机构
            lst.add(item);
        }
        Pagination pagination = new Pagination(1, 1, total);
        return new PageVO<>(pagination, lst);
    }

    @Override
    public DeleteCardResponseData deleteMedicCard(DeleteCardRequest request) throws Exception {
        int cardId = Checker.parseInt(request.getCardId());
        CheckUtil.ensureLargerThanZero(cardId, "诊疗卡 id 不正确");
        int userId = ContextHelper.getCurrentUserIdWrapped();
        DeleteCardResponseData responseData = new DeleteCardResponseData();
        DeleteMedicalCardRequest deleteMedicalCardRequest = DeleteMedicalCardRequest.newBuilder()
                .setCardId(cardId)
                .setUserId(userId)
                .build();
        Result result = userServiceBlockingStub.deleteMedicalCard(deleteMedicalCardRequest);
        responseData.setIsSuccess(result.getCode() == ResultCode.SUCCEED);
        responseData.setInfo(result.getInfo());
        return responseData;
    }

    @Override
    public BindMedInsuranceCardResponseData bindMedInsuranceCard(BindMedInsuranceCardRequest request) throws Exception {
        BindMedInsuranceCardResponseData resp = new BindMedInsuranceCardResponseData();
        String patientName = request.getName();//姓名
        String sex = request.getSex();//性别
        String birthDay = request.getBirthDay();//生日
        String idCardNo = request.getIdCardNo();//身份证号码
        String phoneNo = request.getPhoneNumber();//手机号
        String cardNum = request.getMedInsuranceNum();//医保卡识别号

        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        if (hospitalId < 1) {
            throw new AppMessageException("请选择医院");
        }
        if (StringHelper.isNullOrWhiteSpace(cardNum)) {
            throw new AppMessageException("请填写就诊卡号");
        }
        if (StringHelper.isNullOrWhiteSpace(patientName)) {
            throw new AppMessageException("请填写真实的姓名");
        }
        if (StringHelper.isNullOrWhiteSpace(phoneNo)) {
            throw new AppMessageException("请填写在医院预留的手机号");
        }

        // 通过枚举类的方法，将文字转换成数字
        int relationshipInt = 0;
        // defaultCardInt: 1-是，0-否
        int defaultCardInt = 0;
        AddMedicalCardRequest addMedicalCardRequest = AddMedicalCardRequest.newBuilder()
                .setUserId(userId)
                .setHospitalId(hospitalId)
                .setCardNo(cardNum)
                .setRealName(patientName)
                .setMobile(phoneNo)
                .setRelationship(relationshipInt)
                .setDefaultCard(defaultCardInt)
                .build();
        ResultInt resultInt = userServiceBlockingStub.addMedicalCard(addMedicalCardRequest);
        if (resultInt.getCode() == ResultCode.SUCCEED) {
            resp.setUserId(request.getUserId());
            resp.setCardNum(request.getMedInsuranceNum());
            resp.setName(patientName);
            resp.setSex(sex);
            resp.setBirthDay(birthDay);
            resp.setIdCardNo(idCardNo);
            resp.setPhoneNumber(phoneNo);
        } else {
            throw new AppMessageException(resultInt.getInfo());
        }
        return resp;
    }

    // 就诊人列表
    @Override
    public List<QueryPatientListResponse> queryPatientList(QueryPatientListRequest request) throws Exception {
        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        return getUserMedCardList(userId, hospitalId).stream()
                .map(c -> {
                    QueryPatientListResponse r = new QueryPatientListResponse();
                    List<MedicCard> medicCards = new ArrayList<>();
                    r.setId(String.valueOf(c.getCardId()));
                    r.setName(c.getPatientName());
                    r.setAge(getAgeFromMedicalCard(c));
                    r.setSex(SexEnum.getSexDisplayName(c.getSex()));
                    r.setPhoneNumber(BizUtil.masked(c.getMobile()));
                    r.setBirthDay(c.getBirthday());
                    r.setIdCardNo(BizUtil.masked(c.getIdNo()));
                    r.setRelationShip(RelationshipEnum.valueOf(c.getRelationship()).getDisplayName());
                    r.setDefaultPatient(false); // TODO: 怎么确定默认就诊人
                    r.setBindCardNum(c.getCardNo());
                    r.setHisPatientId(c.getHisPatientId());
                    MedicCard card = new MedicCard();
                    card.setCardNum(c.getCardNo());
                    card.setType(CARD_TYPE_NORMAL);
                    card.setBalance(String.valueOf(c.getBalance() / 100d));
                    card.setHisPatientId(c.getHisPatientId());
                    medicCards.add(card);
                    r.setMedicCards(medicCards);
                    return r;
                })
                .collect(Collectors.toList());
    }

    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 PatientBindCardResponse bindMedicCard(PatientBindCardRequest request) throws Exception {

        return null;
    }

    // 删除就诊人
    @Override
    public DeletePatientResponse deletePatient(DeletePatientRequest request) throws Exception {
        int cardId = Checker.parseInt(request.getId()); // 诊疗卡 id
        CheckUtil.ensureLargerThanZero(cardId, "诊疗卡 id 不正确");
        int userId = ContextHelper.getCurrentUserIdWrapped();
        DeletePatientResponse responseData = new DeletePatientResponse();
        DeleteMedicalCardRequest deleteMedicalCardRequest = DeleteMedicalCardRequest.newBuilder()
                .setCardId(cardId)
                .setUserId(userId)
                .build();
        Result result = userServiceBlockingStub.deleteMedicalCard(deleteMedicalCardRequest);
        responseData.setIsSuccess(result.getCode() == ResultCode.SUCCEED);
        responseData.setInfo(result.getInfo());
        return responseData;
    }

    // 添加就诊人
    @Override
    public AddPatientResponse addPatient(AddPatientRequest request) throws Exception {
        String name = Checker.getStringValue(request.getName());
        String idCardType = Checker.getStringValue(request.getIdCardType()); // 目前小程序只支持身份证
        String idCardNo = Checker.getStringValue(request.getIdCardNo());
        String sex = Checker.getStringValue(request.getSex());
        String phoneNumber = Checker.getStringValue(request.getPhoneNumber());
        String relationShip = Checker.getStringValue(request.getRelationShip());
        int userId = ContextHelper.getCurrentUserIdWrapped();
        int hospitalId = BizUtil.getCurrentHospitalId();
        boolean isSelf = relationShip.equals(RelationshipEnum.MYSELF.getDisplayName());
        if (!isSelf && (StringHelper.isNullOrEmpty(idCardNo) || StringHelper.isNullOrEmpty(name))) {
            // 非本人，必须填写身份证和姓名
            throw new AppMessageException("添加非本人就诊卡必须填写身份证或姓名");
        }
        // 根据身份证和姓名从 HIS 获取 hisPatientId
        if (isSelf) {
            // 添加本人，前端没有传入姓名和身份证，需要查询
            User user = getUserInfoById(userId);
            if (user == null) throw new AppMessageException("用户状态异常，请联系管理员");
            name = Checker.getStringValue(user.getRealname());
            idCardNo = Checker.getStringValue(user.getIdno());
            phoneNumber = Checker.getStringValue(user.getMobile());
        }
        GetPatientInfoResponse response = taiheServiceBlockingStub.getPatientInfo(GetPatientInfoRequest.newBuilder()
                .setPatientName(name)
                .setIdNo(idCardNo)
                .setHospitalId(hospitalId)
                .build());
        if (response.getCode() != ResultCode.SUCCEED_VALUE) throw new AppMessageException(response.getMsg());
        String hisPatientId = response.getPatientInfo().getPatientId();
        if (StringHelper.isNullOrEmpty(hisPatientId)) throw new AppMessageException("无法从 HIS 获取患者 id");
        ResultInt result = userServiceBlockingStub.addTaiheMedicalCard(AddTaiheMedicalCardRequest.newBuilder()
                .setHisPatientId(hisPatientId)
                .setUserId(userId)
                .setHospitalId(hospitalId)
                .setIdNo(idCardNo)
                .setMobile(phoneNumber)
                .setRealName(name)
                .setRelationship(RelationshipEnum.getByName(relationShip).getValue())
                .build());
        if (result.getCode() != ResultCode.SUCCEED) throw new AppMessageException(result.getInfo());
        AddPatientResponse r = new AddPatientResponse();
        r.setIsSuccess(true);
        return r;
    }

    // 修改就诊人
    @Override
    public UpdatePatientResponse updatePatient(UpdatePatientRequest request) throws Exception {
        int cardId = Checker.parseInt(request.getId()); // 支付宝就诊人 id 对应系统的诊疗卡 id
        CheckUtil.ensureLargerThanZero(cardId, "就诊人 id 有误: " + request.getId());
        String mobile = Checker.getStringValue(request.getPhoneNumber());
        String relationship = Checker.getStringValue(request.getRelationShip());
        int relInt = 1;
        if (Checker.isNone(request.getRelationShip())){
            relInt = 0;
        }
        Result result = userServiceBlockingStub.updateMedicalCardRequest(UpdateMedicalCardRequest.newBuilder()
                .setCardId(cardId)
                .setUserId(ContextHelper.getCurrentUserIdWrapped())
                .setMobile(mobile)
                .setRelationship(relInt == 0 ? 0:RelationshipEnum.getByName(relationship).getValue())
                .build());
        if (result.getCode() != ResultCode.SUCCEED) throw new AppMessageException(result.getInfo());
        UpdatePatientResponse response = new UpdatePatientResponse();
        response.setIsSuccess(true);
        return response;
    }

    // 查看就诊人详情
    @Override
    public QueryPatientDetailResponse querySinglePatient(QueryPatientDetailRequest request) throws Exception {
        String id = request.getId(); // 对应 medicalCard 的 id
        int userId = ContextHelper.getCurrentUserIdWrapped();
        int hospitalId = BizUtil.getCurrentHospitalId();
        // 如果不传诊疗卡 id，则查询本人诊疗卡
        MedicalCard c;
        if (StringHelper.isNullOrEmpty(id)) {
            // 获取用户身份证
            User user = getUserInfoById(userId);
            String idNo = Checker.getStringValue(user.getIdno());
            c = getUserMedCardList(userId, hospitalId).stream()
                    .filter(i -> idNo.equals(i.getIdNo()))
                    .findFirst()
                    .orElse(null);
        } else {
            c = getUserMedCardList(userId, hospitalId).stream()
                    .filter(i -> i.getCardId() == Checker.parseInt(id))
                    .findFirst()
                    .orElse(null);
        }

        if (c == null) throw new AppMessageException("找不到诊疗卡信息");
        QueryPatientDetailResponse r = new QueryPatientDetailResponse();
        List<MedicCard> medicCards = new ArrayList<>();
        r.setId(String.valueOf(c.getCardId()));
        r.setName(c.getPatientName());
        r.setAge(getAgeFromMedicalCard(c));
        r.setSex(SexEnum.getSexDisplayName(c.getSex()));
        r.setPhoneNumber(BizUtil.masked(c.getMobile()));
        r.setBirthDay(c.getBirthday());
        r.setIdCardNo(BizUtil.masked(c.getIdNo()));
        r.setRelationShip(RelationshipEnum.valueOf(c.getRelationship()).getDisplayName());
        r.setBindCardNum(c.getCardNo());
        r.setHisPatientId(c.getHisPatientId());
        MedicCard card = new MedicCard();
        card.setCardNum(c.getCardNo());
        card.setType(CARD_TYPE_NORMAL);
        card.setBalance(String.valueOf(c.getBalance() / 100d));
        card.setHisPatientId(c.getHisPatientId());
        medicCards.add(card);
        r.setMedicCards(medicCards);
        return r;
    }

    // 查询登录用户就诊卡列表
    @Override
    public List<MyCardListResponse> myCardList(MyCardListRequest request) throws Exception {
        int hospitalId = BizUtil.getCurrentHospitalId();
        int userId = ContextHelper.getCurrentUserIdWrapped();
        // 获取用户身份证
        User user = getUserInfoById(userId);
        String idNo = Checker.getStringValue(user.getIdno());
        return getUserMedCardList(userId, hospitalId).stream()
                .filter(c -> idNo.equals(c.getIdNo()))
                .map(c -> {
                    MyCardListResponse r = new MyCardListResponse();
                    r.setCardNum(c.getCardNo());
                    r.setBalance(String.valueOf(c.getBalance() / 100d));
                    r.setType(CARD_TYPE_NORMAL);
                    r.setCardId(c.getCardId());
                    r.setHisPatientId(c.getHisPatientId());
                    return r;
                })
                .collect(Collectors.toList());
    }


    /**
     * 根据 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();
    }

    /**
     * 根据用户 id 获取用户信息
     */
    public User getUserInfoById(int userId) throws AppMessageException {
        UserResponse response = userServiceBlockingStub.findByUserId(UserRequest.newBuilder().setUserid(userId).build());
        if (response.getResult().getCode() != ResultCode.SUCCEED)
            throw new AppMessageException("无法获取用户信息：" + response.getResult().getInfo());
        return response.getUser();
    }
}
