package com.openhis.yb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.common.exception.ServiceException;
import com.core.common.utils.StringUtils;
import com.openhis.common.constant.YbCommonConstants;
import com.openhis.financial.domain.Contract;
import com.openhis.yb.domain.InfoPerson;
import com.openhis.yb.service.IPerinfoService;
import com.openhis.yb.mapper.PerinfoMapper;

import org.springframework.stereotype.Service;

/**
 * @Description: 人员基础信息
 * @Author: jeecg-boot
 * @Date:   2021-08-27
 * @Version: V1.0
 */
@Service
public class PerinfoServiceImpl extends ServiceImpl<PerinfoMapper, InfoPerson> implements IPerinfoService {
    @Override
    public InfoPerson getPerInfoByIdCard(String idCard,Integer tenantId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<InfoPerson>()
                .eq(InfoPerson::getCertno, idCard).eq(InfoPerson::getTenantId, tenantId)
                .orderByDesc(InfoPerson::getCreateTime).last(YbCommonConstants.sqlConst.LIMIT1));
    }

    @Override
    public InfoPerson getByPsnNo(String psnNo) {
        return baseMapper.selectOne(new LambdaQueryWrapper<InfoPerson>()
                .eq(InfoPerson::getPsnNo, psnNo).orderByDesc(InfoPerson::getCreateTime)
                .last(YbCommonConstants.sqlConst.LIMIT1));
    }

    @Override
    public boolean assessInterCrossRegionalSettlement(String psnNo, String admVs) {
        InfoPerson infoPerson = getByPsnNo(psnNo);
        if(infoPerson==null){
            throw new ServiceException("未查询到身份信息");
        }
        //参保地
        String insuplcAdmdvs = infoPerson.getInsuplcAdmdvs();
        return admVs.equals(insuplcAdmdvs);
    }

    @Override
    public boolean assessIntraCrossRegionalSettlement(String psnNo, String admVs){
        InfoPerson infoPerson = getByPsnNo(psnNo);
        if(infoPerson==null){
            throw new ServiceException("未查询到身份信息");
        }
        //参保地
        String insuplcAdmdvs = infoPerson.getInsuplcAdmdvs();
        if(StringUtils.isEmpty(insuplcAdmdvs)){
            throw new ServiceException("该患者查询不到参保地信息");
        }
        return insuplcAdmdvs.startsWith(admVs.substring(0,2));
    }

    public boolean assessCrossRegionalSettlement(String psnNo, String admVs){
        return assessIntraCrossRegionalSettlement(psnNo,admVs)||assessIntraCrossRegionalSettlement(psnNo,admVs);
    }
}
