package thyyxxk.webserver.service.reports; 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.dao.his.reports.IllegalChargesAnalysisDao; import thyyxxk.webserver.entity.ResultVo; import thyyxxk.webserver.entity.datamodify.YzActOrder; import thyyxxk.webserver.entity.dictionary.PureCodeName; import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeData; import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeTemplate; import thyyxxk.webserver.entity.reports.illegalchargesanalysis.SearchChargeItem; import thyyxxk.webserver.utils.ListUtil; import thyyxxk.webserver.utils.ResultVoUtil; import thyyxxk.webserver.utils.StringUtil; import thyyxxk.webserver.utils.TokenUtil; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @description: 违规收费项目分析实现类 * @author: DingJie * @create: 2021-04-15 16:47:34 **/ @Slf4j @Service public class IllegalChargesAnalysisService { private final IllegalChargesAnalysisDao dao; private static final String DASH = "_"; private static final String ADMIN = "admin"; private static final String ADMIN_2 = "admin_2"; @Autowired public IllegalChargesAnalysisService(IllegalChargesAnalysisDao dao) { this.dao = dao; } public ResultVo> fetchMyTemplates() { String code = TokenUtil.getTokenUserId() + "%"; List tempList = dao.selectMyTemplates(code); List resultList = new ArrayList<>(); Map treeMap = new HashMap<>(tempList.size()); for (IllegalChargeTemplate item : tempList) { item.analyzeValue(); treeMap.put(item.getId(), item); if (null == item.getParent()) { resultList.add(item); } } for (IllegalChargeTemplate item : tempList) { IllegalChargeTemplate template = treeMap.get(item.getParent()); if (null != template) { if (null == template.getChildren()) { template.setChildren(new ArrayList<>()); } template.getChildren().add(item); } } return ResultVoUtil.success(resultList); } public ResultVo insertNewTemplate(IllegalChargeTemplate template) { if (StringUtil.isBlank(template.getLabel())) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "模板名称不能为空!"); } if (template.getPersonal() == 0) { dao.insertNewTemplate(template); return ResultVoUtil.success("创建成功。"); } StringBuilder id = new StringBuilder(template.getId()); if (!id.toString().contains(DASH)) { template.setId(TokenUtil.getTokenUserId() + DASH + id); } else { if (id.toString().startsWith(ADMIN)) { id = new StringBuilder(id.toString().replace(ADMIN, TokenUtil.getTokenUserId())); template.setId(id.toString()); } } if (template.getParent().startsWith(ADMIN) && !template.getParent().equals(ADMIN_2)) { template.setParent(template.getParent().replace("admin", TokenUtil.getTokenUserId())); } id = new StringBuilder(template.getId()); String[] idArr = id.toString().split("-"); String lastBit = idArr[idArr.length - 1]; int position = Integer.parseInt(lastBit); Integer exist = dao.selectCurrentId(id.toString()); while (exist != null) { position++; idArr[idArr.length - 1] = String.valueOf(position); id = new StringBuilder(); for (int i = 0; i < idArr.length - 1; i++) { id.append(idArr[i]).append("-"); } id.append(position); exist = dao.selectCurrentId(id.toString()); } template.setId(id.toString()); dao.insertNewTemplate(template); log.info("创建模板:{}", template); return ResultVoUtil.success("创建成功。"); } public ResultVo deleteTemplate(IllegalChargeTemplate template) { String id = template.getId() + "%"; dao.deleteTemplate(id); return ResultVoUtil.success("删除成功。"); } public ResultVo> searchChargeItem(SearchChargeItem param) { String content = "%" + param.getContent().toUpperCase() + "%"; String method; switch (param.getMethod()) { case "alpha": method = "py_code"; break; case "code": method = "code"; break; default: method = "name"; break; } String include = param.getIncludeDeactivate() ? "like '%%'" : "!=1"; return ResultVoUtil.success(dao.selectChargeItem(method, content, include)); } public ResultVo saveTemplateChanges(IllegalChargeTemplate template) { if (template.getType() == 0) { dao.updateTemplate(template); return ResultVoUtil.success(); } if (null == template.getMainCharges() || template.getMainCharges().isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "收费主体不能为空!"); } StringBuilder value = genTemplateValue(template.getMainCharges()).append("$"); if (template.getAttribute() == 1) { if (null == template.getConflictCharges() || template.getConflictCharges().isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "冲突收费不能为空!"); } value.append(genTemplateValue(template.getConflictCharges())); } else { if (null == template.getMaxChargeNum()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "超量收费上限不能为空!"); } value.append(template.getMaxChargeNum()).append("|").append(template.getMaxChargeUnit()); } template.setValue(value.toString()); dao.updateTemplate(template); return ResultVoUtil.success(); } private StringBuilder genTemplateValue(List charges) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < charges.size(); i++) { PureCodeName charge = charges.get(i); builder.append(charge.getCode()).append("|").append(charge.getName()); if (i < charges.size() - 1) { builder.append("#"); } } return builder; } public ResultVo> analyzeTargetData(IllegalChargeTemplate template) { template.setStart(template.getStart() + " 00:00:00"); template.setEnd(template.getEnd() + " 23:59:59"); log.info("违规收费分析:{}", template); if (template.getAttribute() == 1) { return sameTimeCharge(template); } return overLimitCharge(template); } private ResultVo> sameTimeCharge(IllegalChargeTemplate template) { List resultList = new ArrayList<>(); for (PureCodeName main : template.getMainCharges()) { List mainList = dao.selectChargeDataForSameTime(template.getStart(), template.getEnd(), main.getCode()); if (ListUtil.notBlank(template.getMedtypes())) { mainList.removeIf(item -> !template.getMedtypes().contains(item.getMedtype())); } for (PureCodeName cflct : template.getConflictCharges()) { List cflctList = dao.selectChargeDataForSameTime(template.getStart(), template.getEnd(), cflct.getCode()); if (ListUtil.notBlank(template.getMedtypes())) { cflctList.removeIf(item -> !template.getMedtypes().contains(item.getMedtype())); } Map map = new HashMap<>(mainList.size()); for (IllegalChargeData item : mainList) { String key = item.getInpatientNo() + "_" + item.getChargeDay(); map.put(key, item); } for (IllegalChargeData item : cflctList) { String key = item.getInpatientNo() + "_" + item.getChargeDay(); if (map.containsKey(key)) { IllegalChargeData data = map.get(key); data.setConflictChargeCode(item.getChargeCode()); data.setConflictChargeAmount(item.getChargeAmount()); data.setConflictChargeDate(item.getChargeDate()); data.setConflictChargeFee(item.getChargeFee()); data.setConflictChargeName(item.getChargeName()); data.setConflictDetailSn(item.getDetailSn()); data.setConflictOrderNo(item.getOrderNo()); resultList.add(data); } } } } return ResultVoUtil.success(resultList); } private ResultVo> overLimitCharge(IllegalChargeTemplate template) { List resultList = new ArrayList<>(); for (PureCodeName main : template.getMainCharges()) { resultList.addAll(dao.selectChargeDataForOverLimit(template.getStart(), template.getEnd(), main.getCode(), template.getMaxChargeNum())); } if (resultList.isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST); } return ResultVoUtil.success(resultList); } public ResultVo> selectOrderPair(BigDecimal orderNo, BigDecimal conflictOrderNo) { List list = dao.selectYzActOrder(orderNo, conflictOrderNo); if (list.isEmpty()) { list = dao.selectYzInActOrder(orderNo, conflictOrderNo); } if (list.isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST); } if (list.size() == 1 || list.get(0).getActOrderNo().equals(orderNo)) { return ResultVoUtil.success(list); } List tmp = new ArrayList<>(); tmp.add(list.get(1)); tmp.add(list.get(0)); return ResultVoUtil.success(tmp); } public ResultVo> queryHuanZheLiangCiRuYuanRiQi(IllegalChargeTemplate param) { List list = dao.queryHuanZheLiangCiRuYuanRiQi(param.getStart(), param.getEnd(), param.getMedtypes()); if (list.isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST); } List illegalChargeData = dao.queryZhenDuan(list); for (IllegalChargeData chargeData : list) { for (IllegalChargeData illegalChargeDatum : illegalChargeData) { if (chargeData.getInpatientNo().equals(illegalChargeDatum.getInpatientNo()) && chargeData.getAdmissTimes().equals(illegalChargeDatum.getAdmissTimes())) { chargeData.setDisDiag(illegalChargeDatum.getDisDiag()); chargeData.setDisDiagComment(illegalChargeDatum.getDisDiagComment()); break; } } } return ResultVoUtil.success(list); } public ResultVo> chaXunHuanZheZhuYuanJianGe(String startTime, String endTime, Integer tianShu) { return ResultVoUtil.success(dao.zhuYuanTianShu(startTime, endTime, tianShu)); } }