package com.imed.costaccount.service.impl; 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.JsonUtil; import com.imed.costaccount.common.util.PageUtils; import com.imed.costaccount.constants.NumberConstant; import com.imed.costaccount.mapper.CostAccountShareMapper; import com.imed.costaccount.model.*; import com.imed.costaccount.model.dto.CostAccountShareEditDto; import com.imed.costaccount.model.dto.CostAccountShareSaveDto; import com.imed.costaccount.model.dto.ShareParamEditDto; import com.imed.costaccount.model.vo.CostAccountShareVO; import com.imed.costaccount.model.vo.CostShareParamStatusVO; import com.imed.costaccount.model.vo.CostShareParamVO; import com.imed.costaccount.model.vo.ShareParamProportionVO; import com.imed.costaccount.service.*; import com.imed.costaccount.common.util.BeanUtil; import org.apache.shiro.SecurityUtils; 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.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @Service("costAccountShareService") public class CostAccountShareServiceImpl extends ServiceImpl implements CostAccountShareService { private final ResponsibilityService responsibilityService; private final AccountingService accountingService; private final CostShareLevelService costShareLevelService; private final CostShareParamService costShareParamService; public CostAccountShareServiceImpl(ResponsibilityService responsibilityService, AccountingService accountingService, CostShareLevelService costShareLevelService, CostShareParamService costShareParamService) { this.responsibilityService = responsibilityService; this.accountingService = accountingService; this.costShareLevelService = costShareLevelService; this.costShareParamService = costShareParamService; } /** * 分页查询责任中心成本对照相关数据 * * @param current * @param pageSize * @param name * @return */ @Override public PageUtils queryList(Integer current, Integer pageSize, String name, Long hospId) { Page costAccountSharePage = new Page<>(current, pageSize); Page pages = this.page(costAccountSharePage, new QueryWrapper().lambda() .eq(!StringUtils.isEmpty(hospId), CostAccountShare::getHospId, hospId) .like(!StringUtils.isEmpty(name), CostAccountShare::getResponsibilityName, name) .orderByAsc(CostAccountShare::getShareLevel)); List costAccountShareList = pages.getRecords(); List costAccountShareVOList = BeanUtil.convertList(costAccountShareList, CostAccountShareVO.class); getMessage(hospId, costAccountShareVOList); // PageUtils pageUtils = new PageUtils(pages); pageUtils.setList(costAccountShareVOList); return pageUtils; } private void getMessage(Long hospId, List costAccountShareList) { // 设置责任中心的数据 与 会计科目的数据从对应的id里面获取 List list = responsibilityService.list(new QueryWrapper().lambda().eq(Responsibility::getHospId, hospId)); List accountingList = accountingService.list(new QueryWrapper().lambda().eq(Accounting::getHospId, hospId)); Map> resMap = list.stream().collect(Collectors.groupingBy(Responsibility::getId)); Map> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId)); costAccountShareList.forEach(i->{ Long id = i.getResponsibilityId(); List responsibilities = resMap.get(id); if (!CollectionUtils.isEmpty(responsibilities)){ i.setResponsibilityId(id); i.setResponsibilityName(responsibilities.get(0).getResponsibilityName()); i.setResponsibilityCode(responsibilities.get(0).getResponsibilityCode()); } Long accountingId = i.getAccountingId(); if (accountingId>0){ List accountingList1 = accountMap.get(accountingId); if (!CollectionUtils.isEmpty(accountingList1)){ i.setAccountingId(accountingId); i.setAccountingName(accountingList1.get(0).getAccountingName()); i.setAccountingCode(accountingList1.get(0).getAccountingCode()); i.setAllParentIds(accountingList1.get(0).getAllParentIds()); } } }); } /** * 保存成本参数对应成本对照表 * * @param costAccountShareSaveDto */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void addCostAccountShare(CostAccountShareSaveDto costAccountShareSaveDto) { User user = (User) SecurityUtils.getSubject().getPrincipal(); Long hospId = user.getHospId(); // 检验输入的数据的合理性 checkAccountShare(costAccountShareSaveDto, hospId); // 检验输入的责任中心是否存在 List costAccountShareList = baseMapper.selectList(new QueryWrapper().lambda() .eq(CostAccountShare::getHospId, hospId)); Map map = costAccountShareList.stream().collect(Collectors.toMap(CostAccountShare::getResponsibilityId,synOe->synOe)); if (Objects.nonNull(map.get(costAccountShareSaveDto.getResponsibilityId()))){ throw new CostException(500,"添加的责任中心已存在"); } CostAccountShare costAccountShareRequest = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShare.class); costAccountShareRequest.setHospId(hospId); costAccountShareRequest.setCreateTime(System.currentTimeMillis()); baseMapper.insert(costAccountShareRequest); } /** * 检验输入数据的合理性 * @param costAccountShareSaveDto * @param hospId */ private void checkAccountShare(CostAccountShareSaveDto costAccountShareSaveDto, Long hospId) { Long responsibilityId = costAccountShareSaveDto.getResponsibilityId(); Responsibility responsibility = responsibilityService.getOne(new QueryWrapper().lambda().eq(Responsibility::getHospId,hospId).eq(Responsibility::getId, responsibilityId)); if (Objects.isNull(responsibility)){ throw new CostException(500,"输入的责任不存在"); } costAccountShareSaveDto.setResponsibilityId(responsibilityId); costAccountShareSaveDto.setResponsibilityCode(responsibility.getResponsibilityCode()); costAccountShareSaveDto.setResponsibilityName(responsibility.getResponsibilityName()); costAccountShareSaveDto.setShareLevel(responsibility.getShareLevel()); if (!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingId()) && costAccountShareSaveDto.getAccountingId() > 0){ // 如果输入成本科目的情况下 Accounting accounting = accountingService.getOne(new QueryWrapper().lambda().eq(Accounting::getHospId, hospId).eq(Accounting::getId, costAccountShareSaveDto.getAccountingId())); if (Objects.isNull(accounting)){ throw new CostException(500,"输入的成本科目不存在"); }else { costAccountShareSaveDto.setAccountingId(accounting.getId()); costAccountShareSaveDto.setAccountingName(accounting.getAccountingName()); costAccountShareSaveDto.setAccountingCode(accounting.getAccountingCode()); } // 检验输入的责任中心与匹配的成本科目是否存在 List costAccountShareList = baseMapper.selectList(new QueryWrapper().lambda().eq(CostAccountShare::getHospId,hospId)); Map> costAccountMap = costAccountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode)); List list = costAccountMap.get(costAccountShareSaveDto.getResponsibilityCode()); if (!CollectionUtils.isEmpty(list)){ String accountingCode = list.get(0).getAccountingCode(); if (accountingCode.equals(costAccountShareSaveDto.getAccountingCode())){ throw new CostException(500,"输入的责任中心对应的成本科目已存在"); } } } // 检验输入的这个责任中心是否允许输入成本科目 Long shareId = responsibility.getShareId(); CostShareLevel costShareLevel = costShareLevelService.getOne(new QueryWrapper().lambda() .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getId, shareId)); if (Objects.nonNull(costShareLevel)){ if ((!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingId()) && costAccountShareSaveDto.getAccountingId() > 0) && NumberConstant.ZERO.equals(costShareLevel.getCalcType())){ throw new CostException(500,"合并计算不允许选择成本科目"); } if ((!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingId()) && costAccountShareSaveDto.getAccountingId() <= 0) && NumberConstant.ONE.equals(costShareLevel.getCalcType())){ throw new CostException(500,"分开计算的责任中心需要选择成本科目"); } }else { throw new CostException(500,"对不起该责任中心没有对应分摊层级"); } } /** * 修改成本中心责任对照表 * * @param costAccountShareEditDto */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void updateByCostAccountShare(CostAccountShareEditDto costAccountShareEditDto) { User user = (User) SecurityUtils.getSubject().getPrincipal(); Long hospId = user.getHospId(); Long id = costAccountShareEditDto.getId(); CostAccountShare costAccountShare = baseMapper.selectById(id); if (Objects.isNull(costAccountShare)){ throw new CostException(500,"责任中心成本数据不存在"); } baseMapper.deleteById(id); // 新增责任中心成本对照数据 CostAccountShareSaveDto costAccountShareSaveDto = BeanUtil.convertObj(costAccountShareEditDto, CostAccountShareSaveDto.class); // 检验输入的数据是否符合规则 checkAccountShare(costAccountShareSaveDto,hospId); CostAccountShareEditDto accountShareEditDto = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShareEditDto.class); CostAccountShare costAccountShareRequest = BeanUtil.convertObj(accountShareEditDto, CostAccountShare.class); costAccountShareRequest.setId(null); costAccountShareRequest.setHospId(hospId); costAccountShareRequest.setParamList(costAccountShare.getParamList()); costAccountShareRequest.setCreateTime(System.currentTimeMillis()); baseMapper.insert(costAccountShareRequest); } /** * 修改成本分摊参数的设置 * * @param shareParamEditDto */ @Override @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) public void updateShareParam(ShareParamEditDto shareParamEditDto) { List shareParamProportionVOList1 = shareParamEditDto.getShareParamProportionVOList(); User user = (User) SecurityUtils.getSubject().getPrincipal(); Long hospId = user.getHospId(); Long id = shareParamEditDto.getId(); CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper().lambda() .eq(CostAccountShare::getHospId, hospId) .eq(CostAccountShare::getId, id)); if (Objects.isNull(costAccountShare)){ throw new CostException(500,"分摊参数对应数据不存在"); } if (!CollectionUtils.isEmpty(shareParamProportionVOList1)){ List shareParamProportionVOList = shareParamEditDto.getShareParamProportionVOList(); // 检验输入的成本分摊参数是否存在 List costShareParamServiceAll = costShareParamService.getAll(hospId); Map> listMap = costShareParamServiceAll.stream().collect(Collectors.groupingBy(CostShareParamVO::getId)); shareParamProportionVOList.forEach(i->{ if (CollectionUtils.isEmpty(listMap.get(i.getId()))){ throw new CostException(500,"分摊名称:"+i.getShareParamName()+"未找到"); } }); // 检验输入的数据的和是否是100 AtomicReference sum= new AtomicReference<>(0); shareParamProportionVOList.forEach(i->{ sum.updateAndGet(v -> v + i.getShareParamPopout()); }); int max = Integer.parseInt(sum.toString()); if (!NumberConstant.ONE_HUNDRED.equals(max)){ throw new CostException(500,"分摊比例的和不是100"); } // 判断添加的分摊是否存在一样的 HashMap hashMap = new HashMap<>(); shareParamProportionVOList.forEach(i->{ Long paramId = i.getId(); if (hashMap.containsKey(paramId)){ throw new CostException(500,"不可以添加相同的分摊参数"); } hashMap.put(paramId,i); }); // String paramList = JsonUtil.toJSONString(shareParamProportionVOList); costAccountShare.setParamList(paramList); }else{ costAccountShare.setParamList(null); } baseMapper.updateById(costAccountShare); } /** * 获取责任中心成本表的分摊参数的集合 * * @param id * @param hospId * @return */ @Override public List selectShareParamById(Integer id, Long hospId) { CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper().lambda() .eq(CostAccountShare::getHospId, hospId).eq(CostAccountShare::getId, id)); if (Objects.isNull(costAccountShare)){ throw new CostException(500,"责任中心成本不存在"); } String paramList = costAccountShare.getParamList(); return JsonUtil.toList(paramList, ShareParamProportionVO.class); } /** * 成本分摊参数中被分摊参数对应选中的状态 * * @param id * @param hospId * @return */ @Override public List getAllShareParamStatusById(Integer id, Long hospId) { List costShareParamServiceAll = costShareParamService.getAll(hospId); CostAccountShare costAccountShare = baseMapper.selectById(id); if (Objects.isNull(costAccountShare)){ throw new CostException(500,"成本分摊对应数据不存在"); } String paramList = costAccountShare.getParamList(); List costShareParamStatusVOList = BeanUtil.convertList(costShareParamServiceAll, CostShareParamStatusVO.class); if (!StringUtils.isEmpty(paramList)) { List shareParamProportionVOList = JsonUtil.toList(paramList, ShareParamProportionVO.class); Map> map = shareParamProportionVOList.stream().collect(Collectors.groupingBy(ShareParamProportionVO::getId)); costShareParamStatusVOList.forEach(i -> { Long paramId = i.getId(); if (!CollectionUtils.isEmpty(map.get(paramId))) { i.setShareParamStatus(NumberConstant.ONE); } }); } return costShareParamStatusVOList; } }