package thyyxxk.webserver.service.zhuyuanyiji;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.zhuyuanyiji.YiJiFeiYongLuRuDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

/**
 * <p>
 * 描述： 住院医技项目录入
 * </p>
 *
 * @author xc
 * @date 2021-09-13 15:42
 */
@Service
@Slf4j
public class YiJiFeiYongLuRuServer {

    private final YiJiFeiYongLuRuDao dao;

    private final PublicServer publicServer;

    public YiJiFeiYongLuRuServer(YiJiFeiYongLuRuDao dao, PublicServer publicServer) {
        this.dao = dao;
        this.publicServer = publicServer;
    }

    /**
     * @param inpatientNo    住院号
     * @param zaiYuanChuYuan 出院号
     * @return 返回患者信息
     */
    public ResultVo<ZyActpatient> getHuanZheXinXi(String inpatientNo, Integer zaiYuanChuYuan, Integer admissTimes) {
        log.info("医技查询患者信息：住院号{}，是否在院{},住院次数：{}", inpatientNo, zaiYuanChuYuan, admissTimes);
        if (StringUtil.isBlank(inpatientNo)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "住院号不能为空。");
        }
        String tableName;
        if (zaiYuanChuYuan == 0) {
            tableName = "zy_actpatient";
        } else {
            if (admissTimes == 0) {
                admissTimes = dao.maxAdmissTimes(inpatientNo);
            }
            tableName = "zy_inactpatient";
        }
        ZyActpatient zyActpatient = dao.getHuanZheXinXi(inpatientNo, tableName, admissTimes);
        if (zyActpatient == null) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        if (zaiYuanChuYuan == 0) {
            Integer jieSuan = dao.getHuanZheSFJieSuan(publicServer.getInpatientNo(inpatientNo), zyActpatient.getAdmissTimes());
            if (jieSuan == null || jieSuan != 0) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者已经结算。");
            }
        }
        return ResultVoUtil.success(zyActpatient);
    }

}
