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 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 getAccountRelation(Long reportId, Long hospId) { return baseMapper.getAccountRelation(reportId, hospId); } /** * 报表项目关联的分摊层级对象 * * @param reportId 报表项目id * @param hospId 医院id * @return {@link RelationVO} */ @Override public List 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 getRelationList(Long reportId, Integer relation, Long hospId) { List list = new ArrayList<>(); if (relation == 1) { list = this.getAccountRelation(reportId, hospId); // List accounts = accountingService.list(new LambdaQueryWrapper().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 accounts = shareParamService.list(new LambdaQueryWrapper().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 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 list = this.list( new LambdaQueryWrapper().eq(ReportRelation::getReportId, reportId).eq(ReportRelation::getHospId, hospId) ); if (!list.isEmpty()) { baseMapper.deleteBatchIds(list.stream().map(ReportRelation::getId).collect(Collectors.toList())); } List 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 relationCodes) { List relations = this.list( new LambdaQueryWrapper().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())) + "已被其他项目代码绑定"); } } }