package thyyxxk.webserver.service.medicalinsurance;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.sidicts.*;
import thyyxxk.webserver.constants.YesOrNo;
import thyyxxk.webserver.dao.his.medicalinsurance.*;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.log.SiLog;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.*;
import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetldetail;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;
import thyyxxk.webserver.entity.yibao.dismiss.YbSettleFee;
import thyyxxk.webserver.entity.yibao.patient.Overview;
import thyyxxk.webserver.service.externalhttp.SiZySrvc;
import thyyxxk.webserver.service.yibao.DismissService;
import thyyxxk.webserver.utils.*;
import java.util.Date;
import java.util.List;
/**
* @description: 住院医保交易
* @author: DingJie
* @create: 2021-05-14 16:28:16
*
* 联通云:
* http://10.136.68.156:20005/isp-api/powercsb/1101
* api_access_key=FqSRO1tkKe4PTsZS8mJkN3zs12U6Zkkwez6Qhy
* api_secreKey=SKklCVOoGDag5ys4zkSE2qHYo8iU4FsG7DNyJQYI
*
* 省政府二院:
* http://10.90.0.11:8081/isp-api/powercsb/1101
* api_access_key=sJAXxQd10aQLWZFAMH3PT9pHCGGqXPzz4Hv4Ei
* api_secreKey=SK1BtAloxIaQUgv9m0gjqrQJ0NJikkPPUTlRGOzp
**/
@Slf4j
@Service
public class SiZyService {
private static final String RESULT_CODE = "infcode";
private static final String ERROR_MESSAGE = "err_msg";
private static final String OUTPUT = "output";
private final SiZyDao dao;
private final SiQueryDao qrydao;
private final SiSetlinfoDao setlinfodao;
private final SiSetldetailDao setldetldao;
private final SiChargeTempDao chrgtmpdao;
private final SiLogDao logDao;
private final ExecService exec;
private final DismissService dismissService;
private final SetlListUpldService setlListUpldService;
private final SiZySrvc zySrvc;
@Autowired
public SiZyService(SiZyDao dao, SiQueryDao qrydao, SiSetlinfoDao setlinfoDao,
SiSetldetailDao setldetldao, SiChargeTempDao chrgtmpdao, SiLogDao logDao, ExecService exec,
DismissService dismissService, SetlListUpldService setlListUpldService, SiZySrvc zySrvc) {
this.dao = dao;
this.qrydao = qrydao;
this.setlinfodao = setlinfoDao;
this.setldetldao = setldetldao;
this.chrgtmpdao = chrgtmpdao;
this.logDao = logDao;
this.exec = exec;
this.dismissService = dismissService;
this.setlListUpldService = setlListUpldService;
this.zySrvc = zySrvc;
}
public ResultVo admissRegister(ZyPatientInfo p) {
if (p.getInsuplcAdmdvs().equals("430182")) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "宁乡患者无法在我院办理住院业务。");
}
AdmMdtrtinfo admMdtrtinfo = dao.selectAdmMdtrtinfo(p.getInpatientNo(), p.getLedgerSn());
if (null == admMdtrtinfo) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的参保信息!");
}
if (StringUtil.isBlank(admMdtrtinfo.getChfpdrName())) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "管床医生不能为空!");
}
List admDiseinfos = dao.selectAdmDiags(p.getInpatientNo(), p.getAdmissTimes());
if (null == admDiseinfos || admDiseinfos.isEmpty()) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断!");
}
ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
if (readCardBizType == ReadCardBizType.ADMISSION) {
MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
admMdtrtinfo.setMdtrtCertType(mdtrtCertType.getCode());
if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
String[] out = p.getReadCardResult().split("\\|");
if (!p.getName().trim().equals(out[4].trim())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "社保卡姓名与HIS姓名不一致,请确认是否人证相符。");
}
admMdtrtinfo.setMdtrtCertNo(out[2]);
admMdtrtinfo.setCardSn(out[3]);
} else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
if (!p.getName().trim().equals(qrinfo.getString("userName").trim())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证姓名与HIS姓名不一致,请确认是否人证相符。");
}
if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
}
admMdtrtinfo.setMdtrtCertNo(qrinfo.getString("ecToken"));
}
}
admMdtrtinfo.setInsutype(p.getInsutype());
admMdtrtinfo.setAdmDiagDscr(admDiseinfos.get(0).getDiagName());
admMdtrtinfo.setDscgMaindiagCode(admDiseinfos.get(0).getDiagCode());
admMdtrtinfo.setDscgMaindiagName(admDiseinfos.get(0).getDiagName());
admMdtrtinfo.setDiseCodg(admDiseinfos.get(0).getDiagCode());
admMdtrtinfo.setDiseName(admDiseinfos.get(0).getDiagName());
admMdtrtinfo.setMatnType(p.getMatnType());
admMdtrtinfo.setLatechbFlag(p.getLatechbFlag());
admMdtrtinfo.setPretFlag(p.getPretFlag());
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.ADMISSION_REGISTRATION, p.getInsuplcAdmdvs());
String ref = JSONObject.toJSONStringWithDateFormat(admMdtrtinfo, "yyyy-MM-dd HH:mm:ss");
input.getJSONObject("input").put("mdtrtinfo", JSONObject.parseObject(ref));
String diseRef = JSONArray.toJSONStringWithDateFormat(admDiseinfos, "yyyy-MM-dd HH:mm:ss");
input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
JSONObject result = exec.executeTrade(input, SiFunction.ADMISSION_REGISTRATION);
log.info("【操作员:{}】,医保入院登记:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
if (null == infcode) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (result.getIntValue(RESULT_CODE) == 0) {
JSONObject output = result.getJSONObject(OUTPUT);
String mdtrtId = output.getJSONObject("result").getString("mdtrt_id");
p.setMdtrtId(mdtrtId);
p.setAdmRegMsgid(input.getString("msgid"));
dao.afterAdmissRegister(p);
String restype = dao.selectResponceType(admMdtrtinfo.getMedType());
dao.updateResponceType(restype, admMdtrtinfo.getMedType(), p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
return ResultVoUtil.success("入院登记成功,就诊号为:" + mdtrtId);
}
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
public ResultVo modifyAdmissionInfo(ZyPatientInfo p) {
SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者医保不在院!");
}
List admDiseinfos = dao.selectAdmDiags(p.getInpatientNo(), p.getAdmissTimes());
if (null == admDiseinfos || admDiseinfos.isEmpty()) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断!");
}
admDiseinfos.forEach(item -> item.setMdtrtId(siPatInfo.getMdtrtId()));
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.MODIFY_ADMISSION_INFO, siPatInfo.getInsuplcAdmdvs());
JSONObject adminfo = new JSONObject();
adminfo.put("mdtrt_id", siPatInfo.getMdtrtId());
adminfo.put("psn_no", siPatInfo.getPsnNo());
adminfo.put("begntime", DateUtil.formatDatetime(p.getYbRegisterDate()));
adminfo.put("mdtrt_cert_type", MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode());
adminfo.put("med_type", siPatInfo.getMedType());
adminfo.put("ipt_otp_no", p.getInpatientNo());
adminfo.put("atddr_no", p.getReferPhysician());
adminfo.put("chfpdr_name", p.getReferPhysicianName());
adminfo.put("adm_diag_dscr", admDiseinfos.get(0).getDiagCode());
adminfo.put("adm_dept_codg", p.getSmallDept());
adminfo.put("adm_dept_name", p.getSmallDeptName());
adminfo.put("adm_bed", p.getBedNo());
adminfo.put("dscg_maindiag_code", admDiseinfos.get(0).getDiagCode());
adminfo.put("dscg_maindiag_name", admDiseinfos.get(0).getDiagName());
input.getJSONObject("input").put("adminfo", adminfo);
String diseRef = JSONArray.toJSONStringWithDateFormat(admDiseinfos, "yyyy-MM-dd HH:mm:ss");
input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
JSONObject result = exec.executeTrade(input, SiFunction.MODIFY_ADMISSION_INFO);
log.info("【操作员:{}】,入院信息变更:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
return SiUtil.makeReturnWithoutOutput(result, "变更住院信息成功。");
}
public ResultVo revokeAdmission(ZyPatientInfo p) {
SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
}
revokeUploadFees(p);
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_ADMISSION, siPatInfo.getInsuplcAdmdvs());
JSONObject data = new JSONObject();
data.put("mdtrt_id", siPatInfo.getMdtrtId());
data.put("psn_no", siPatInfo.getPsnNo());
input.getJSONObject("input").put("data", data);
JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_ADMISSION);
log.info("【操作员:{}】,取消入院登记:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
if (infcode == 0) {
qrydao.clearMdtrtIdForZy(siPatInfo.getPatNo(), siPatInfo.getTimes(), siPatInfo.getLedgerSn(), null);
dao.updateResponceType("01", null, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
return ResultVoUtil.success("取消入院登记成功。");
}
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
public ResultVo uploadFeeDetail(Overview o) {
o.setStaffId(TokenUtil.getTokenUserId());
return zySrvc.uploadFeeDetail(o);
}
public ResultVo hospitalizationPreSettlement(ZyPatientInfo p) {
PreSetlmt preSetlmt = dao.selectPreSetlmt(p.getInpatientNo(), p.getAdmissTimes(),
p.getLedgerSn(), DateUtil.getTodayEndTime());
if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
}
preSetlmt.setMdtrtCertType(MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode());
preSetlmt.setPsnSetlway(p.getDbg() ? PsnSetlWay.SETTLE_BY_QUOTA.getCode() : PsnSetlWay.SETTLE_BY_ITEMS.getCode());
preSetlmt.setAcctUsedFlag(YesOrNo.NO.getCodeStr());
preSetlmt.setMidSetlFlag(YesOrNo.NO.getCodeStr());
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.HOSPITALIZATION_PRE_SETTLEMENT, preSetlmt.getInsuplcAdmdvs());
String ref = JSONObject.toJSONString(preSetlmt);
input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
JSONObject result = exec.executeTrade(input, SiFunction.HOSPITALIZATION_PRE_SETTLEMENT);
log.info("预结算:\n参数:{},\n结果:{}", input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
if (infcode == 0) {
JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
String fundPay = setlinfo.getString("fund_pay_sumamt");
dao.updateFundPay(fundPay, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
String message = "患者【" + p.getName() + "】院内总费用与医保中心总费用一致,医保报销金额为:¥ " + fundPay + "。";
return ResultVoUtil.success(message);
}
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
public ResultVo revokeUploadFees(ZyPatientInfo p) {
SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
}
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS, siPatInfo.getInsuplcAdmdvs());
JSONArray data = new JSONArray();
if (null == p.getDetailSns() || p.getDetailSns().isEmpty()) {
JSONObject item = new JSONObject();
item.put("feedetl_sn", "0000");
item.put("mdtrt_id", siPatInfo.getMdtrtId());
item.put("psn_no", siPatInfo.getPsnNo());
data.add(item);
} else {
p.getDetailSns().forEach(detailSn -> {
JSONObject item = new JSONObject();
item.put("feedetl_sn", detailSn);
item.put("mdtrt_id", siPatInfo.getMdtrtId());
item.put("psn_no", siPatInfo.getPsnNo());
data.add(item);
});
}
input.getJSONObject("input").put("data", data);
JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS);
log.info("【操作员:{}】撤销已上传的费用:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
if (infcode == 0) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("pat_no", p.getInpatientNo());
wrapper.eq("times", p.getAdmissTimes());
wrapper.eq("ledger_sn", p.getLedgerSn());
if (null == p.getDetailSns() || p.getDetailSns().isEmpty()) {
dao.revokeAllUploadFee(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
chrgtmpdao.delete(wrapper);
} else {
wrapper.in("detail_sn", p.getDetailSns());
chrgtmpdao.delete(wrapper);
dao.revokePartUploadFee(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), p.getDetailSns());
}
return ResultVoUtil.success("撤销费用上传成功。");
}
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
public ResultVo dischargeProcessing(ZyPatientInfo p) {
if (null == p.getLedgerSn()) {
p.setLedgerSn(dao.selectMaxLedgerSn(p.getInpatientNo(), p.getAdmissTimes()));
}
Dscginfo dscginfo;
if (p.getMidSetl()) {
dscginfo = dao.selectDscginfoForMidSetl(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
dscginfo.setEndtime(p.getZjdzDatetime());
} else {
dscginfo = dao.selectDscginfo(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
}
if (null == dscginfo || StringUtil.isBlank(dscginfo.getMdtrtId())) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
}
List setlDises = dao.selectSetlDises(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
if (null == setlDises || setlDises.isEmpty()) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者医保出院诊断不能为空,请联系医生在病案首页填写。");
}
if (null == p.getStaffId()) {
p.setStaffId(TokenUtil.getTokenUserId());
}
dscginfo.setDiseCodg(setlDises.get(0).getDiagCode());
dscginfo.setDiseName(setlDises.get(0).getDiagName());
dscginfo.setDscgWay(DscgWay.ORDER_TO_LEAVE_HOSPITAL.getCode());
String ref = JSONObject.toJSONStringWithDateFormat(dscginfo, "yyyy-MM-dd HH:mm:ss");
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.DISCHARGE_PROCESSING,
dao.selectAdmdvs(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn()));
input.getJSONObject("input").put("dscginfo", JSONObject.parseObject(ref));
String diseRef = JSONArray.toJSONStringWithDateFormat(setlDises, "yyyy-MM-dd HH:mm:ss");
input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
JSONObject result = exec.executeTrade(input, SiFunction.DISCHARGE_PROCESSING);
log.info("【操作员:{}】,医保出院办理:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
if (infcode == 0) {
p.setDismissDate(dscginfo.getEndtime());
return inpatientSettlement(p);
}
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
public ResultVo revokeDischarge(ZyPatientInfo p) {
SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
transbackTables(p);
return ResultVoUtil.success("取消出院办理成功。");
}
p.setMdtrtId(siPatInfo.getMdtrtId());
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_DISCHARGE, siPatInfo.getInsuplcAdmdvs());
JSONObject data = new JSONObject();
data.put("mdtrt_id", siPatInfo.getMdtrtId());
data.put("psn_no", siPatInfo.getPsnNo());
input.getJSONObject("input").put("data", data);
JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_DISCHARGE);
log.info("【操作员:{}】取消医保出院办理:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
if (infcode == 0) {
dao.deleteSetlInfo(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
dao.deleteSetlDetail(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
transbackTables(p);
return ResultVoUtil.success("取消医保出院办理成功。");
}
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
private ResultVo inpatientSettlement(ZyPatientInfo p) {
PreSetlmt preSetlmt;
Date begntime = dismissService.getBegntime(p.getInpatientNo(), p.getAdmissTimes(), "zy_actpatient");
if (p.getMidSetl()) {
preSetlmt = dao.selectPreSetlmtForMidSetl(p.getInpatientNo(), p.getAdmissTimes(),
p.getLedgerSn(), begntime, p.getZjdzDatetime());
} else {
preSetlmt = dao.selectPreSetlmt(p.getInpatientNo(), p.getAdmissTimes(),
p.getLedgerSn(), "2999-12-31 23:59:59.999");
}
if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
}
ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
if (readCardBizType == ReadCardBizType.SETTLEMENT) {
MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
preSetlmt.setMdtrtCertType(mdtrtCertType.getCode());
if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
String[] out = p.getReadCardResult().split("\\|");
if (!p.getName().trim().equals(out[4].trim())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "社保卡姓名与HIS姓名不一致,请确认是否人证相符。");
}
preSetlmt.setMdtrtCertNo(out[2]);
preSetlmt.setCardSn(out[3]);
} else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
if (!p.getName().trim().equals(qrinfo.getString("userName").trim())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证姓名与HIS姓名不一致,请确认是否人证相符。");
}
if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
}
preSetlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
}
}
preSetlmt.setPsnSetlway(p.getDbg() ? PsnSetlWay.SETTLE_BY_QUOTA.getCode() : PsnSetlWay.SETTLE_BY_ITEMS.getCode());
preSetlmt.setAcctUsedFlag(YesOrNo.NO.getCodeStr());
preSetlmt.setMidSetlFlag(YesOrNo.NO.getCodeStr());
preSetlmt.setInvono(SnowFlakeId.instance().nextId());
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.HOSPITALIZATION_SETTLEMENT, preSetlmt.getInsuplcAdmdvs());
String ref = JSONObject.toJSONString(preSetlmt);
input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
JSONObject result = exec.executeTrade(input, SiFunction.HOSPITALIZATION_SETTLEMENT);
log.info("【操作员:{}】,医保出院结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
if (null == result) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
}
if (infcode == 0) {
setlListUpldService.yiBaoZhiFuFangShi(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
SiSetlinfo setlEntity = JSONObject.parseObject(setlinfo.toJSONString(), SiSetlinfo.class);
setlEntity.setPatNo(p.getInpatientNo());
setlEntity.setTimes(p.getAdmissTimes());
setlEntity.setLedgerSn(p.getLedgerSn());
setlEntity.setStaffId(TokenUtil.getTokenUserId());
setlEntity.setRevoked(YesOrNo.NO.getCode());
setlEntity.setSetlType(ClrType.INPATIENT.getCode());
setlEntity.setInsuplcAdmdvs(preSetlmt.getInsuplcAdmdvs());
setlEntity.setBegntime(begntime);
setlEntity.setEndtime(p.getDismissDate());
setlinfodao.insert(setlEntity);
setlinfodao.updateSiZyInfoSetlId(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(),
setlEntity.getSetlId(), setlEntity.getMedinsSetlId(), input.getString("msgid"));
setlinfodao.updateApplySettled(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), YesOrNo.YES.getCode());
JSONArray setldetail = result.getJSONObject(OUTPUT).getJSONArray("setldetail");
for (int i = 0; i < setldetail.size(); i++) {
SiSetldetail setldetailEntity = JSONObject.parseObject(setldetail.getJSONObject(i).toJSONString(), SiSetldetail.class);
setldetailEntity.setPatNo(p.getInpatientNo());
setldetailEntity.setTimes(p.getAdmissTimes());
setldetailEntity.setLedgerSn(p.getLedgerSn());
setldetldao.insert(setldetailEntity);
}
YbSettleFee settleFee = new YbSettleFee();
if (p.getMidSetl()) {
settleFee.setZjdzDatetime(p.getZjdzDatetime());
}
settleFee.setMidSetl(p.getMidSetl());
settleFee.setStaffId(TokenUtil.getTokenUserId());
settleFee.setInpatientNo(p.getInpatientNo());
settleFee.setAdmissTimes(p.getAdmissTimes());
settleFee.setBedNo(p.getBedNo());
settleFee.setWardCode(p.getAdmissWard());
settleFee.setDeptCode(p.getAdmissDept());
settleFee.setTotalCost(setlinfo.getString("medfee_sumamt"));
settleFee.setTczf(setlinfo.getString("fund_pay_sumamt"));
settleFee.setHospitalPay(setlinfo.getString("hosp_part_amt"));
settleFee.setGrzhzf(setlinfo.getString("acct_pay"));
settleFee.setDbzf(setlinfo.getString("hifmi_pay"));
settleFee.setXjzf(setlinfo.getString("psn_cash_pay"));
settleFee.setTable(p.getTable());
return dismissService.doDismiss(settleFee);
}
revokeDischarge(p);
return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
}
public ResultVo revokeSettlement(ZyPatientInfo p) {
SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
if (null == p.getStaffId()) {
p.setStaffId(TokenUtil.getTokenUserId());
}
if (null == p.getLedgerSn()) {
p.setLedgerSn(dao.selectMaxLedgerSn(p.getInpatientNo(), p.getAdmissTimes()));
}
if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getSetlId())) {
return revokeDischarge(p);
}
p.setMdtrtId(siPatInfo.getMdtrtId());
JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_SETTLEMENT, siPatInfo.getInsuplcAdmdvs());
JSONObject data = new JSONObject();
data.put("mdtrt_id", siPatInfo.getMdtrtId());
data.put("setl_id", siPatInfo.getSetlId());
data.put("psn_no", siPatInfo.getPsnNo());
input.getJSONObject("input").put("data", data);
JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_SETTLEMENT);
log.info("【操作员:{}】取消医保结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
if (null == result) {
return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
}
Integer infcode = result.getInteger(RESULT_CODE);
logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
if (null == infcode) {
return revokeDischarge(p);
}
if (infcode == 0) {
dao.updateRvkSetlMsgid(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), input.getString("msgid"));
setlinfodao.updateApplySettled(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), YesOrNo.NO.getCode());
transbackTables(p);
}
return revokeDischarge(p);
}
private void transbackTables(ZyPatientInfo p) {
dao.deleteSiSetlFeeDetl(p.getMdtrtId());
dao.deleteZyLedgerFileYb(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
dao.deleteZyReceipt(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
dao.updateSettleType(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
dao.updateChargeStatus(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
}
}