|
@@ -0,0 +1,183 @@
|
|
|
+package com.imed.costaccount.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.imed.costaccount.common.exception.CostException;
|
|
|
+import com.imed.costaccount.enums.ErrorCodeEnum;
|
|
|
+import com.imed.costaccount.model.User;
|
|
|
+import com.imed.costaccount.model.dto.ResponsibilityEditDTO;
|
|
|
+import com.imed.costaccount.model.dto.ResponsibilitySaveDTO;
|
|
|
+import com.imed.costaccount.model.vo.CostResponsibilityVO;
|
|
|
+import com.imed.costaccount.utils.BeanUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.beanutils.BeanUtilsBean;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+
|
|
|
+import com.imed.costaccount.mapper.ResponsibilityMapper;
|
|
|
+import com.imed.costaccount.model.Responsibility;
|
|
|
+import com.imed.costaccount.service.ResponsibilityService;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service("responsibilityService")
|
|
|
+public class ResponsibilityServiceImpl extends ServiceImpl<ResponsibilityMapper, Responsibility> implements ResponsibilityService {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 责任中心列表不分页
|
|
|
+ * @return
|
|
|
+ * @param user
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<CostResponsibilityVO> getList(User user) {
|
|
|
+ // 1. 获取所有的列表然后组装
|
|
|
+ List<Responsibility> list = this.list(
|
|
|
+ new LambdaQueryWrapper<Responsibility>()
|
|
|
+ .eq(Responsibility::getHospId, user.getHospId())
|
|
|
+ );
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ throw new CostException(ErrorCodeEnum.DATA_NOT_EXIST);
|
|
|
+ }
|
|
|
+ // 拷贝组合
|
|
|
+ List<CostResponsibilityVO> costResponsibilityVOS = BeanUtil.convertList(list, CostResponsibilityVO.class);
|
|
|
+ List<CostResponsibilityVO> parentCostResponsibility = costResponsibilityVOS.stream()
|
|
|
+ .filter(i -> i.getResponsibilityLevel().equals(1)).collect(Collectors.toList());
|
|
|
+
|
|
|
+ parentCostResponsibility.forEach(i -> costResponsibilityVOS.forEach(j -> {
|
|
|
+ if (j.getParentId().equals(i.getId())) {
|
|
|
+ i.getChild().add(j);
|
|
|
+ }
|
|
|
+ }));
|
|
|
+
|
|
|
+ return parentCostResponsibility;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增责任中心
|
|
|
+ * @param responsibilitySaveDTO {@link ResponsibilitySaveDTO }
|
|
|
+ * @param user
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ public void addResponsibilityCenter(ResponsibilitySaveDTO responsibilitySaveDTO, User user) {
|
|
|
+ // 如果是一级分类parentId为0
|
|
|
+ Integer id = responsibilitySaveDTO.getId();
|
|
|
+ if (Objects.isNull(id)) {
|
|
|
+ id = 0;
|
|
|
+ }
|
|
|
+ // 校验责任代码唯一性
|
|
|
+ Responsibility one = this.getOne(
|
|
|
+ new LambdaQueryWrapper<Responsibility>().select(Responsibility::getId)
|
|
|
+ .eq(Responsibility::getResponsibilityCode, responsibilitySaveDTO.getResponsibilityCode())
|
|
|
+ .eq(Responsibility::getHospId, user.getHospId()).last("limit 1")
|
|
|
+ );
|
|
|
+ if (Objects.nonNull(one)) {
|
|
|
+ throw new CostException(ErrorCodeEnum.RESPONSIBILITY_CODE_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ Responsibility center = BeanUtil.convertObj(responsibilitySaveDTO, Responsibility.class);
|
|
|
+ center.setCreateTime(new Date().getTime()).setId(null).setParentId(id);
|
|
|
+ this.save(center);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑责任中心
|
|
|
+ * @param responsibilityEditDTO {@link ResponsibilityEditDTO}
|
|
|
+ * @param user
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ public void editResponsibility(ResponsibilityEditDTO responsibilityEditDTO, User user) {
|
|
|
+ Integer id = responsibilityEditDTO.getId();
|
|
|
+ Responsibility center = this.getById(id);
|
|
|
+ if (Objects.isNull(center)) {
|
|
|
+ throw new CostException(ErrorCodeEnum.DATA_NOT_EXIST);
|
|
|
+ }
|
|
|
+ // 如果修改父节点节点(只有两层的情况)
|
|
|
+ if (center.getResponsibilityLevel() == 1) {
|
|
|
+ this.updateParent(responsibilityEditDTO,center);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.updateCurrent(responsibilityEditDTO,center);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修的是父节点
|
|
|
+ * @param dto
|
|
|
+ * @param responsibility
|
|
|
+ */
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ public void updateParent(ResponsibilityEditDTO dto, Responsibility responsibility) {
|
|
|
+ // 删除原有的父节点数据
|
|
|
+ Integer id = dto.getId();
|
|
|
+ this.removeById(id);
|
|
|
+ // 新增父节点数据
|
|
|
+ Responsibility newResponsibility = BeanUtil.convertObj(dto, Responsibility.class);
|
|
|
+ newResponsibility.setId(null).setHospId(responsibility.getHospId()).setCreateTime(new Date().getTime());
|
|
|
+ this.save(newResponsibility);
|
|
|
+ // 将原来所有父节点下数据关联到新的父节点下
|
|
|
+ List<Responsibility> list = this.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getParentId, id));
|
|
|
+ list.forEach(i -> i.setParentId(newResponsibility.getId()));
|
|
|
+ this.updateBatchById(list);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改当前的节点
|
|
|
+ * @param responsibility
|
|
|
+ * @param dto
|
|
|
+ */
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ public void updateCurrent(ResponsibilityEditDTO dto, Responsibility responsibility) {
|
|
|
+ // 删除原有的父节点数据
|
|
|
+ Integer id = dto.getId();
|
|
|
+ this.removeById(id);
|
|
|
+ // 新增父节点数据
|
|
|
+ Responsibility newResponsibility = BeanUtil.convertObj(dto, Responsibility.class);
|
|
|
+ newResponsibility.setId(null).setHospId(responsibility.getHospId()).setCreateTime(new Date().getTime());
|
|
|
+ this.save(newResponsibility);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除责任中心
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ public void deleteCenter(Integer id) {
|
|
|
+ Responsibility center = this.getById(id);
|
|
|
+ if (Objects.isNull(center)) {
|
|
|
+ throw new CostException(ErrorCodeEnum.DATA_NOT_EXIST);
|
|
|
+ }
|
|
|
+ Integer parentId = center.getParentId();
|
|
|
+ if (parentId == 0) {
|
|
|
+ this.deleteAllSonCenter(id);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ this.removeById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除父节点以及所有子节点责任中心
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
|
|
|
+ public void deleteAllSonCenter(Integer id) {
|
|
|
+ this.removeById(id);
|
|
|
+ this.remove(new LambdaQueryWrapper<Responsibility>().eq(Responsibility::getParentId, id));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|