package com.imed.costaccount.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.PageUtils; import com.imed.costaccount.constants.NumberConstant; import com.imed.costaccount.mapper.CostShareParamMapper; import com.imed.costaccount.model.CostShareParam; import com.imed.costaccount.model.dto.CostShareParamAccountDto; import com.imed.costaccount.model.dto.CostShareParamEditDto; import com.imed.costaccount.model.dto.CostShareParamSaveDto; import com.imed.costaccount.model.vo.CostShareParamVO; import com.imed.costaccount.service.CostShareParamService; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.util.*; import java.util.stream.Collectors; @Service("costShareParamService") public class CostShareParamServiceImpl extends ServiceImpl implements CostShareParamService { /** * 分页查询相关的分摊参数数据 * * @param current * @param pageSize * @param name * @return */ @Override public PageUtils queryList(Integer current, Integer pageSize, String name,Long hospId) { Page costShareParamPage = new Page<>(current, pageSize); Page pages = this.page(costShareParamPage, new QueryWrapper().lambda() .eq(!StringUtils.isEmpty(hospId), CostShareParam::getHospId, hospId) .like(!StringUtils.isEmpty(name), CostShareParam::getShareParamName, name).orderByDesc(CostShareParam::getCreateTime)) ; List records = pages.getRecords(); List costShareParamVOList = BeanUtil.convertList(records, CostShareParamVO.class); PageUtils pageUtils = new PageUtils(pages); pageUtils.setList(costShareParamVOList); return pageUtils; } /** * 保存分摊参数 * 保存的时候需要校验分摊参数 * @param costShareParamSaveDto */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void addCostShareParam(CostShareParamSaveDto costShareParamSaveDto) { //检验分摊参数是存在 getCostShareParamByCode(costShareParamSaveDto); CostShareParam costShareParam = BeanUtil.convertObj(costShareParamSaveDto, CostShareParam.class); costShareParam.setCreateTime(System.currentTimeMillis()); baseMapper.insert(costShareParam); } /** * 检验分摊参数是存在 */ private void getCostShareParamByCode(CostShareParamSaveDto costShareParamSaveDto) { List costShareParamList = baseMapper.selectList(new QueryWrapper().lambda() .eq(CostShareParam::getHospId, costShareParamSaveDto.getHospId())); // 检验添加的分摊参数是否存在 if (!CollectionUtils.isEmpty(costShareParamList)){ Map> costShareMap = costShareParamList.stream().collect(Collectors.groupingBy(CostShareParam::getShareParamCode)); if (!CollectionUtils.isEmpty(costShareMap.get(costShareParamSaveDto.getShareParamCode()))){ throw new CostException(500,"分摊参数已存在"); } } } /** * 修改分摊参数 * * @param costShareParamEditDto */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void updateCostShareParam(CostShareParamEditDto costShareParamEditDto) { Long id = costShareParamEditDto.getId(); CostShareParam costShareParam = baseMapper.selectById(id); if (Objects.isNull(costShareParam)){ throw new CostException(500,"对不起分摊参数不存在"); } baseMapper.deleteById(id); // 判断当前输入的Code以最初的Code是否一样 List costShareParamList = baseMapper.selectList(new QueryWrapper().lambda().select(CostShareParam::getShareParamCode).eq(CostShareParam::getHospId, costShareParamEditDto.getHospId())); Map> map = costShareParamList.stream().collect(Collectors.groupingBy(CostShareParam::getShareParamCode)); if (!CollectionUtils.isEmpty(map.get(costShareParamEditDto.getShareParamCode()))){ throw new CostException(500,"对不起分摊参数代码已存在"); } CostShareParam costShareParamRequest = BeanUtil.convertObj(costShareParamEditDto, CostShareParam.class); costShareParamRequest.setId(null); costShareParamRequest.setCreateTime(System.currentTimeMillis()); if (NumberConstant.ONE.equals( costShareParamEditDto.getShareParamCode()) && NumberConstant.ONE.equals(costShareParamEditDto.getShareParamCode())){ costShareParamRequest.setAccountingId(costShareParam.getAccountingId()); } baseMapper.insert(costShareParamRequest); } /** * 为按照科目的计算方式添加成本科目 * * @param costShareParamAccountDto * @return */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public CostShareParam updateCostShareParamByAccountId(CostShareParamAccountDto costShareParamAccountDto) { Long costShareParamId = costShareParamAccountDto.getCostShareParamId(); CostShareParam costShareParam = baseMapper.selectById(costShareParamId); // 判断当前操作的分层参数的计算方式是不是按科目计算 if (NumberConstant.TWO.equals(costShareParam.getCalcType())){ String[] accountIds = costShareParamAccountDto.getAccountIds(); List accountList = Arrays.asList(accountIds); if (!CollectionUtil.isEmpty(accountList)){ String accounds = accountList.stream().map(String::valueOf).collect(Collectors.joining(",")); costShareParam.setAccountingId(accounds); }else { costShareParam.setAccountingId(null); } baseMapper.updateById(costShareParam); }else { throw new CostException(500,"计算方式不是按会计科目计算"); } return null; } /** * 获取所有分摊参数的集合信息 * * @param hospId * @return */ @Override @ApiOperation("获取所有的分摊参数的集合数据") public List getAll(Long hospId) { List costShareParamList = baseMapper.selectList(new QueryWrapper().lambda() .eq(CostShareParam::getHospId, hospId)); List costShareParamVOList = BeanUtil.convertList(costShareParamList, CostShareParamVO.class); return costShareParamVOList; } /** * 根据医院的Id分摊参数的ID获取对应的分摊参数的数据 * * @param id * @param hospId * @return */ @Override public CostShareParamVO getByHospIdAndAPramId(Integer id, Long hospId) { CostShareParam costShareParam = baseMapper.selectOne(new QueryWrapper().lambda() .eq(CostShareParam::getHospId, hospId) .eq(CostShareParam::getId, id)); CostShareParamVO costShareParamVO = BeanUtil.convertObj(costShareParam, CostShareParamVO.class); return costShareParamVO; } @Override public List selectIsSelect(Integer shareParamId) { CostShareParam byId = this.getById(shareParamId); if (Objects.isNull(byId)) { return Collections.emptyList(); } String accountingId = byId.getAccountingId(); if (!StringUtils.isEmpty(accountingId)){ String[] split = accountingId.split(StrUtil.COMMA); List collect = Arrays.stream(split).map(Integer::valueOf).collect(Collectors.toList()); return collect; }else { return Collections.emptyList(); } } }