123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package com.imed.costaccount.service.impl;
- import cn.hutool.core.collection.CollUtil;
- 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.mapper.ReportFormMapper;
- import com.imed.costaccount.mapper.ReportRelationMapper;
- import com.imed.costaccount.model.ReportForm;
- import com.imed.costaccount.model.ReportRelation;
- import com.imed.costaccount.model.dto.ReportRelationDTO;
- import com.imed.costaccount.model.vo.RelationVO;
- import com.imed.costaccount.service.ReportRelationService;
- 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.List;
- import java.util.Objects;
- import java.util.stream.Collectors;
- @Slf4j
- @Service("reportRelationService")
- public class ReportRelationServiceImpl extends ServiceImpl<ReportRelationMapper, ReportRelation> implements ReportRelationService {
- private final ReportFormMapper reportFormMapper;
- public ReportRelationServiceImpl(ReportFormMapper reportFormMapper) {
- this.reportFormMapper = reportFormMapper;
- }
- /**
- * 报表项目关联的会计科目对象
- *
- * @param reportId 报表项目id
- * @param hospId 医院id
- * @return {@link RelationVO}
- */
- @Override
- public List<RelationVO> getAccountRelation(Long reportId, Long hospId) {
- return baseMapper.getAccountRelation(reportId, hospId);
- }
- /**
- * 报表项目关联的分摊层级对象
- *
- * @param reportId 报表项目id
- * @param hospId 医院id
- * @return {@link RelationVO}
- */
- @Override
- public List<RelationVO> getShareLevel(Long reportId, Long hospId) {
- return baseMapper.getShareParam(reportId, hospId);
- }
- /**
- * 根据关系类型,获取可绑定的关系数据(包含回显)
- *
- * @param reportId 报表项目id
- * @param relation 1.对应会计科目设置,2.对应分摊参数设置,根据列表中showAddRelation字段是否存在判断
- * @param hospId 医院id
- * @return 返回所有列表 并便是已选择的元素
- */
- @Override
- public List<RelationVO> getRelationList(Long reportId, Integer relation, Long hospId) {
- List<RelationVO> list = new ArrayList<>();
- if (relation == 1) {
- list = this.getAccountRelation(reportId, hospId);
- // List<Accounting> accounts = accountingService.list(new LambdaQueryWrapper<Accounting>().select(Accounting::getAccountingCode, Accounting::getAccountingName).eq(Accounting::getHospId, hospId));
- // if (accounts.isEmpty()) {
- // return list;
- // }
- // list = accounts.stream().map(i -> {
- // RelationVO relationVO = new RelationVO();
- // relationVO.setIsSelect(false);
- // relationVO.setRelation(1);
- // relationVO.setCode(i.getAccountingCode());
- // relationVO.setName(i.getAccountingName());
- // return relationVO;
- // }).collect(Collectors.toList());
- // for (RelationVO relationVO : accountRelation) {
- // for (RelationVO vo : list) {
- // if (vo.getCode().equals(relationVO.getCode())) {
- // vo.setIsSelect(true);
- // }
- // }
- // }
- } else if (relation == 2) {
- list = this.getShareLevel(reportId, hospId);
- // List<CostShareParam> accounts = shareParamService.list(new LambdaQueryWrapper<CostShareParam>().select(CostShareParam::getShareParamCode, CostShareParam::getShareParamName).eq(CostShareParam::getHospId, hospId));
- // if (accounts.isEmpty()) {
- // return list;
- // }
- // list = accounts.stream().map(i -> {
- // RelationVO relationVO = new RelationVO();
- // relationVO.setIsSelect(false);
- // relationVO.setRelation(1);
- // relationVO.setCode(i.getShareParamCode());
- // relationVO.setName(i.getShareParamName());
- // return relationVO;
- // }).collect(Collectors.toList());
- // for (RelationVO relationVO : accountRelation) {
- // for (RelationVO vo : list) {
- // if (vo.getCode().equals(relationVO.getCode())) {
- // vo.setIsSelect(true);
- // }
- // }
- // }
- }
- return list;
- }
- /**
- * 编辑相关关系
- *
- * @param reportRelationDTO {@link ReportRelationDTO}
- * @param hospId
- */
- @Override
- @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
- public void saveReportRelation(ReportRelationDTO reportRelationDTO, Long hospId) {
- Integer relation = reportRelationDTO.getRelation();
- List<String> relationCodes = reportRelationDTO.getRelationCodes();
- Long reportId = reportRelationDTO.getReportId();
- // 校验这个report是否能绑定
- ReportForm byId = reportFormMapper.selectById(reportId);
- if (Objects.isNull(byId)) {
- throw new CostException("选择的报表项目不存在");
- }
- Integer calcType = byId.getCalcType();
- if (calcType != 1 && calcType != 2) {
- throw new CostException("选择的报表项目不能绑定关联关系");
- }
- List<ReportRelation> list = this.list(
- new LambdaQueryWrapper<ReportRelation>().eq(ReportRelation::getReportId, reportId).eq(ReportRelation::getHospId, hospId)
- );
- if (!list.isEmpty()) {
- baseMapper.deleteBatchIds(list.stream().map(ReportRelation::getId).collect(Collectors.toList()));
- }
- List<ReportRelation> reportRelations = relationCodes.stream().map(code -> {
- ReportRelation reportRelation = new ReportRelation();
- reportRelation.setRelation(relation);
- reportRelation.setReportId(reportId);
- reportRelation.setHospId(hospId);
- reportRelation.setRelationCode(code);
- reportRelation.setCreateTime(System.currentTimeMillis());
- return reportRelation;
- }).collect(Collectors.toList());
- // 校验 关联关系的id 是否已绑定过其他的报表项目
- checkIfExist(hospId, relation, relationCodes);
- this.saveBatch(reportRelations);
- }
- /**
- * 校验 关联关系的id 是否已绑定过其他的报表项目
- *
- * @param hospId 医院id
- * @param relation 关系
- * @param relationCodes 所有需要绑定的code 列表
- */
- private void checkIfExist(Long hospId, Integer relation, List<String> relationCodes) {
- List<ReportRelation> relations = this.list(
- new LambdaQueryWrapper<ReportRelation>().select(ReportRelation::getRelationCode)
- .in(ReportRelation::getRelationCode, relationCodes)
- .eq(ReportRelation::getRelation, relation)
- .eq(ReportRelation::getHospId, hospId)
- );
- if (CollUtil.isNotEmpty(relations)) {
- throw new CostException(500, "编码为:" +
- StrUtil.join(StrUtil.COMMA, relations.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList())) +
- "已被其他项目代码绑定");
- }
- }
- }
|