|
@@ -1,28 +1,31 @@
|
|
|
package com.imed.costaccount.service.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ReUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.imed.costaccount.common.exception.CostException;
|
|
|
+import com.imed.costaccount.common.util.BeanUtil;
|
|
|
+import com.imed.costaccount.common.util.CommonUtil;
|
|
|
import com.imed.costaccount.mapper.ReportFormMapper;
|
|
|
import com.imed.costaccount.model.ReportForm;
|
|
|
import com.imed.costaccount.model.User;
|
|
|
+import com.imed.costaccount.model.dto.CopyReportDTO;
|
|
|
import com.imed.costaccount.model.dto.ReportFormEditDTO;
|
|
|
import com.imed.costaccount.model.dto.ReportFormSaveDTO;
|
|
|
import com.imed.costaccount.model.vo.RelationVO;
|
|
|
import com.imed.costaccount.model.vo.ReportFormVO;
|
|
|
import com.imed.costaccount.service.ReportFormService;
|
|
|
import com.imed.costaccount.service.ReportRelationService;
|
|
|
-import com.imed.costaccount.common.util.BeanUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.imed.costaccount.common.constants.NumberConstant.THREE;
|
|
@@ -67,7 +70,6 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
-
|
|
|
List<ReportFormVO> roots = reportFormVOS.stream().filter(i -> i.getParentId() == 0L).collect(Collectors.toList());
|
|
|
for (ReportFormVO root : roots) {
|
|
|
List<ReportFormVO> children = root.getChildren();
|
|
@@ -85,29 +87,6 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
return roots;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 填充对应的关联关系
|
|
|
- *
|
|
|
- * @param reportFormVO {@link ReportFormVO}
|
|
|
- * @param hospId
|
|
|
- */
|
|
|
- private void setRelation(ReportFormVO reportFormVO, Long hospId) {
|
|
|
- Integer showAddRelation = reportFormVO.getShowAddRelation();
|
|
|
- if (Objects.isNull(showAddRelation)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (showAddRelation == 1) {
|
|
|
- List<RelationVO> list = reportRelationService.getAccountRelation(reportFormVO.getId(), hospId);
|
|
|
- reportFormVO.setReportRelations(list);
|
|
|
- return;
|
|
|
- } else if (showAddRelation == 2) {
|
|
|
- List<RelationVO> list = reportRelationService.getShareLevel(reportFormVO.getId(), hospId);
|
|
|
- reportFormVO.setReportRelations(list);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 新增一个报表项目
|
|
@@ -119,8 +98,7 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
public void saveReport(ReportFormSaveDTO reportFormDTO, Long hospId) {
|
|
|
ReportForm reportForm = BeanUtil.convertObj(reportFormDTO, ReportForm.class);
|
|
|
- Integer num = this.createNum(hospId);
|
|
|
- // TODO: 2021/8/4 控制唯一性
|
|
|
+ Integer num = this.createNum(hospId,reportFormDTO.getReportType());
|
|
|
reportForm.setHospId(hospId)
|
|
|
.setNum(num)
|
|
|
.setCreateTime(System.currentTimeMillis());
|
|
@@ -134,12 +112,13 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
|
|
|
/**
|
|
|
* 根据是否父节点设置必要的数据
|
|
|
- * ***后台默认处理报表节点不一致的问题***
|
|
|
- * @param hospId 医院id
|
|
|
+ * ***后台默认处理报表节点不一致的问题***
|
|
|
+ *
|
|
|
+ * @param hospId 医院id
|
|
|
* @param reportForm 需要构建的实体
|
|
|
- * @param parentId 父级id
|
|
|
+ * @param parentId 父级id
|
|
|
*/
|
|
|
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
public void setDataByParentId(Long hospId, ReportForm reportForm, Long parentId) {
|
|
|
if (parentId == 0L) {
|
|
|
reportForm.setCalcType(0);
|
|
@@ -157,20 +136,69 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
} else {
|
|
|
// 父子节点报表类型必须一致
|
|
|
ReportForm byId = this.getById(parentId);
|
|
|
- checkExist(byId,"选择的父报表层级不正确");
|
|
|
+ checkExist(byId, "选择的父报表层级不正确");
|
|
|
reportForm.setReportType(byId.getReportType());
|
|
|
// 如果子节点 小计只能存在一个
|
|
|
if (reportForm.getCalcType() == 3) {
|
|
|
this.isSubtotal(parentId, hospId);
|
|
|
}
|
|
|
+ if (reportForm.getCalcType() == 4) {
|
|
|
+ // 校验计算公式合法性 [7]+[11]-[22]
|
|
|
+ this.checkCalcFormula(reportForm.getCalcFormula(),hospId,reportForm.getReportType());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 校验表达式中的内容是否存在
|
|
|
+ * @param calcFormula 表达式 [7]+[11]-[22]
|
|
|
+ */
|
|
|
+ private void checkCalcFormula(String calcFormula,Long hospId,Integer reportType) {
|
|
|
+ // 校验公式不存在单独的'[' 或 ']',中括号未闭合
|
|
|
+ boolean flag = CommonUtil.whetherStringClose(calcFormula);
|
|
|
+ if (!flag) {
|
|
|
+ throw new CostException("计算公式错误,英文中括号未正确闭合");
|
|
|
+ }
|
|
|
+ // 以下校验公式编号是否合法
|
|
|
+ Pattern pattern = Pattern.compile("(\\[[^\\]]*\\])");
|
|
|
+ Matcher m = pattern.matcher(calcFormula);
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ while (m.find()) {
|
|
|
+ if (StrUtil.isNotBlank(m.group())) {
|
|
|
+ list.add(m.group());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (list.size() == 0) {
|
|
|
+ throw new CostException("计算公式错误,请使用英文输入状态下括号标识");
|
|
|
+ }
|
|
|
+ // 校验所有编号不是同一个
|
|
|
+ Set<String> set = new HashSet<>(list);
|
|
|
+ if (set.size() != list.size()) {
|
|
|
+ throw new CostException("公式中编号有重复");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验编号是否存在
|
|
|
+ list.forEach(i -> {
|
|
|
+ String sub = StrUtil.sub(i, i.indexOf("[") + 1, i.lastIndexOf("]"));
|
|
|
+ ReportForm one = this.getOne(
|
|
|
+ new LambdaQueryWrapper<ReportForm>().select(ReportForm::getId)
|
|
|
+ .eq(ReportForm::getNum, sub)
|
|
|
+ .eq(ReportForm::getHospId, hospId)
|
|
|
+ .eq(ReportForm::getReportType, reportType)
|
|
|
+ .last(LIMIT_ONE)
|
|
|
+ );
|
|
|
+ if (Objects.isNull(one)) {
|
|
|
+ throw new CostException("计算公式中编号不存在");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 子类型中小计计算类型只能存在一个
|
|
|
- * 判断是否存在小计的计算类型,存在抛出异常
|
|
|
+ * 判断是否存在小计的计算类型,存在抛出异常
|
|
|
+ *
|
|
|
* @param parentId 父级id
|
|
|
- * @param hospId 医院id
|
|
|
+ * @param hospId 医院id
|
|
|
*/
|
|
|
private void isSubtotal(Long parentId, Long hospId) {
|
|
|
List<ReportForm> list = this.list(
|
|
@@ -187,12 +215,14 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
* 创建最新的num 获取最大的num + 1
|
|
|
*
|
|
|
* @param hospId 医院id
|
|
|
+ * @param reportType 计算类型
|
|
|
* @return 返回本医院最新编号
|
|
|
*/
|
|
|
- private Integer createNum(Long hospId) {
|
|
|
+ private Integer createNum(Long hospId,Integer reportType) {
|
|
|
ReportForm one = this.getOne(
|
|
|
new LambdaQueryWrapper<ReportForm>().select(ReportForm::getNum)
|
|
|
.eq(ReportForm::getHospId, hospId)
|
|
|
+ .eq(ReportForm::getReportType,reportType)
|
|
|
.orderByDesc(ReportForm::getNum).last(LIMIT_ONE)
|
|
|
);
|
|
|
if (Objects.isNull(one)) {
|
|
@@ -211,7 +241,7 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
public void updateReport(ReportFormEditDTO formEditDTO) {
|
|
|
Long id = formEditDTO.getId();
|
|
|
ReportForm byId = this.getById(id);
|
|
|
- checkExist(byId,"选择的报表项目不存在");
|
|
|
+ checkExist(byId, "选择的报表项目不存在");
|
|
|
|
|
|
BeanUtil.convertObj(formEditDTO, byId);
|
|
|
|
|
@@ -224,12 +254,70 @@ public class ReportFormServiceImpl extends ServiceImpl<ReportFormMapper, ReportF
|
|
|
|
|
|
/**
|
|
|
* 抛出异常
|
|
|
- * @param byId POJO类
|
|
|
+ *
|
|
|
+ * @param byId POJO类
|
|
|
* @param errorMsg 抛出信息
|
|
|
*/
|
|
|
- private void checkExist(ReportForm byId,String errorMsg) {
|
|
|
+ private void checkExist(ReportForm byId, String errorMsg) {
|
|
|
if (Objects.isNull(byId)) {
|
|
|
throw new CostException(errorMsg);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 辅助数据
|
|
|
+ *
|
|
|
+ * @param copyReportDTO {@link CopyReportDTO}
|
|
|
+ * @param hospId 医院id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
+ public void copyReport(CopyReportDTO copyReportDTO, Long hospId) {
|
|
|
+ Integer toReportType = copyReportDTO.getToReportType();
|
|
|
+ Integer fromReportType = copyReportDTO.getFromReportType();
|
|
|
+ List<ReportForm> list = this.list(
|
|
|
+ new LambdaQueryWrapper<ReportForm>()
|
|
|
+ .eq(ReportForm::getHospId, hospId)
|
|
|
+ .eq(ReportForm::getReportType, fromReportType)
|
|
|
+ );
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ throw new CostException("复制的报表类型下不存在数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除掉需要复制到的数据
|
|
|
+ this.remove(new LambdaQueryWrapper<ReportForm>().eq(ReportForm::getHospId, hospId).eq(ReportForm::getReportType, toReportType));
|
|
|
+
|
|
|
+ // 先添加父层级数据
|
|
|
+ list.forEach(i -> {
|
|
|
+ if (i.getParentId() == 0) {
|
|
|
+ // 旧id
|
|
|
+ i.setOldId(i.getId());
|
|
|
+ i.setId(null);
|
|
|
+ i.setReportType(toReportType);
|
|
|
+ i.setCreateTime(System.currentTimeMillis());
|
|
|
+ // 父节点不存在计算公式问题
|
|
|
+ this.save(i);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 处理子节点
|
|
|
+ List<ReportForm> childrenForms = list.stream().filter(i -> i.getParentId() != 0).collect(Collectors.toList());
|
|
|
+ List<ReportForm> newParentForms = this.list(
|
|
|
+ new LambdaQueryWrapper<ReportForm>()
|
|
|
+ .select(ReportForm::getOldId)
|
|
|
+ .eq(ReportForm::getHospId, hospId)
|
|
|
+ .eq(ReportForm::getReportType, toReportType)
|
|
|
+ );
|
|
|
+ childrenForms.forEach(i -> {
|
|
|
+ newParentForms.forEach(j -> {
|
|
|
+ if (j.getOldId() != 0 && j.getOldId().equals(i.getParentId())) {
|
|
|
+ i.setParentId(j.getId());
|
|
|
+ i.setCreateTime(System.currentTimeMillis());
|
|
|
+ i.setReportType(toReportType);
|
|
|
+ this.save(i);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
}
|