123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- package com.imed.costaccount.service.impl;
- 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.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.AccountShareCopyDto;
- 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 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.*;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.stream.Collectors;
- @Service("costAccountShareService")
- public class CostAccountShareServiceImpl extends ServiceImpl<CostAccountShareMapper, CostAccountShare> 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<CostAccountShare> costAccountSharePage = new Page<>(current, pageSize);
- Page<CostAccountShare> pages = this.page(costAccountSharePage, new QueryWrapper<CostAccountShare>().lambda()
- .eq(!StringUtils.isEmpty(hospId), CostAccountShare::getHospId, hospId)
- .like(!StringUtils.isEmpty(name), CostAccountShare::getResponsibilityName, name)
- .orderByAsc(CostAccountShare::getShareLevel));
- List<CostAccountShare> costAccountShareList = pages.getRecords();
- List<CostAccountShareVO> costAccountShareVOList = BeanUtil.convertList(costAccountShareList, CostAccountShareVO.class);
- costAccountShareVOList.forEach(i->{
- String accountingIds = i.getAccountingIds();
- if (StrUtil.isNotBlank(accountingIds)){
- List<String> list = Arrays.asList(accountingIds.split(StrUtil.COMMA));
- i.setAccountingIdList(list);
- }
- });
- // getMessage(hospId, costAccountShareVOList);
- PageUtils pageUtils = new PageUtils(pages);
- pageUtils.setList(costAccountShareVOList);
- return pageUtils;
- }
- // private void getMessage(Long hospId, List<CostAccountShareVO> costAccountShareList) {
- // // 设置责任中心的数据 与 会计科目的数据从对应的id里面获取
- // List<Responsibility> list = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
- // List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
- // Map<Long, List<Responsibility>> resMap = list.stream().collect(Collectors.groupingBy(Responsibility::getId));
- // Map<Long, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
- // costAccountShareList.forEach(i->{
- // Long id = i.getResponsibilityId();
- // List<Responsibility> 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<Accounting> 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);
- 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<Responsibility>().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());
- List<CostAccountShare> costAccountShareList = baseMapper.selectList(new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId,hospId).eq(CostAccountShare::getResponsibilityId,costAccountShareSaveDto.getResponsibilityId()));
- if (!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingIds())){
- // 这个责任中心允许输入会计科目
- List<Long> accountIdList = Arrays.stream(costAccountShareSaveDto.getAccountingIds().split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList());
- List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId).in(Accounting::getId, accountIdList).orderByDesc(Accounting::getCreateTime));
- if (CollectionUtils.isEmpty(accountingList)){
- throw new CostException(500,"输入的会计科目不存在");
- }else {
- String accountIds = accountingList.stream().map(Accounting::getId).collect(Collectors.toList()).stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
- costAccountShareSaveDto.setAccountingIds(accountIds);
- String accountNames = accountingList.stream().map(Accounting::getAccountingName).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
- costAccountShareSaveDto.setAccountingNames(accountNames);
- String accountCodes = accountingList.stream().map(Accounting::getAccountingCode).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
- costAccountShareSaveDto.setAccountingCodes(accountCodes);
- }
- // Map<String, List<CostAccountShare>> costAccountMap = costAccountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode));
- // List<CostAccountShare> list = costAccountMap.get(costAccountShareSaveDto.getResponsibilityCode());
- if (!CollectionUtils.isEmpty(costAccountShareList)){
- costAccountShareList.forEach(i->{
- String accountingIds = i.getAccountingIds();
- Integer isShareCost = i.getIsShareCost();
- List<Long> accountIdListRequest = Arrays.stream(accountingIds.split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList());
- if (!Collections.disjoint(accountIdListRequest,accountIdList)){
- throw new CostException(500,"存在重复的数据");
- }if (NumberConstant.ONE.equals(isShareCost) && NumberConstant.ONE.equals(costAccountShareSaveDto.getIsShareCost())){
- throw new CostException(500,"责任中心已包含分摊成本");
- }
- });
- }
- }else {
- // 此时是
- // 责任中心只允许出现一次
- if (!CollectionUtils.isEmpty(costAccountShareList)){
- throw new CostException(500,"该责任中心合并计算已存在");
- }
- if (NumberConstant.ONE.equals(costAccountShareSaveDto.getIsShareCost())){
- throw new CostException(500,"合并计算责任中心必须要设置包含分摊参数");
- }
- }
- // 检验输入的这个责任中心是否允许输入会计科目
- Long shareId = responsibility.getShareId();
- CostShareLevel costShareLevel = costShareLevelService.getOne(new QueryWrapper<CostShareLevel>().lambda()
- .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getId, shareId));
- if (Objects.nonNull(costShareLevel)){
- if ((!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingIds())) && NumberConstant.ZERO.equals(costShareLevel.getCalcType())){
- throw new CostException(500,"合并计算不允许选择会计科目");
- }
- if ((StringUtils.isEmpty(costAccountShareSaveDto.getAccountingIds())) && 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<ShareParamProportionVO> shareParamProportionVOList1 = shareParamEditDto.getShareParamProportionVOList();
- User user = (User) SecurityUtils.getSubject().getPrincipal();
- Long hospId = user.getHospId();
- Long id = shareParamEditDto.getId();
- CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
- .eq(CostAccountShare::getHospId, hospId)
- .eq(CostAccountShare::getId, id));
- if (Objects.isNull(costAccountShare)){
- throw new CostException(500,"分摊参数对应数据不存在");
- }
- if (!CollectionUtils.isEmpty(shareParamProportionVOList1)){
- List<ShareParamProportionVO> shareParamProportionVOList = shareParamEditDto.getShareParamProportionVOList();
- // 检验输入的成本分摊参数是否存在
- List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
- Map<Long, List<CostShareParamVO>> 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<Integer> 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<Long, ShareParamProportionVO> 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<ShareParamProportionVO> selectShareParamById(Integer id, Long hospId) {
- CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().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<CostShareParamStatusVO> getAllShareParamStatusById(Integer id, Long hospId) {
- List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
- CostAccountShare costAccountShare = baseMapper.selectById(id);
- if (Objects.isNull(costAccountShare)){
- throw new CostException(500,"成本分摊对应数据不存在");
- }
- String paramList = costAccountShare.getParamList();
- List<CostShareParamStatusVO> costShareParamStatusVOList = BeanUtil.convertList(costShareParamServiceAll, CostShareParamStatusVO.class);
- if (!StringUtils.isEmpty(paramList)) {
- List<ShareParamProportionVO> shareParamProportionVOList = JsonUtil.toList(paramList, ShareParamProportionVO.class);
- Map<Long, List<ShareParamProportionVO>> 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;
- }
- /**
- * 拷贝
- *
- * @param accountShareCopyDto 赋值数据
- * @param hospId 医院Id
- */
- @Override
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
- public void copyAccountShareData(AccountShareCopyDto accountShareCopyDto, Long hospId) {
- Long id = accountShareCopyDto.getId();
- CostAccountShare costAccountShare = this.getById(id);
- if (Objects.isNull(costAccountShare)){
- throw new CostException(500,"成本分摊参数设置对应数据不存在");
- }
- List<CostAccountShare> accountShareList = new ArrayList<>();
- List<Long> responsibilityIds = accountShareCopyDto.getResponsibilityIds();
- Map<Long, Responsibility> responsibilityMap = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
- .eq(Responsibility::getHospId, hospId)).stream().collect(Collectors.toMap(Responsibility::getId, synOne->synOne));
- long millis = System.currentTimeMillis();
- responsibilityIds.forEach(i->{
- CostAccountShare accountShareRequest = BeanUtil.convertObj(costAccountShare, CostAccountShare.class);
- accountShareRequest.setId(null);
- accountShareRequest.setCreateTime(millis);
- Responsibility responsibility = responsibilityMap.get(i);
- accountShareRequest.setResponsibilityId(i);
- accountShareRequest.setResponsibilityCode(responsibility.getResponsibilityCode());
- accountShareRequest.setResponsibilityName(responsibility.getResponsibilityName());
- accountShareRequest.setShareLevel(responsibility.getShareLevel());
- accountShareList.add(accountShareRequest);
- });
- this.saveBatch(accountShareList);
- }
- /**
- * 获取指定类型的责任中心集合 分开计算还是合并计算
- *
- *
- * @param accountShareId
- * @param hospId
- * @return
- */
- @Override
- public List<Responsibility> getResponsibilityCalType(Long accountShareId, Long hospId) {
- CostAccountShare costAccountShare = this.getById(accountShareId);
- if (Objects.isNull(costAccountShare)){
- throw new CostException(500,"成本分摊参数设置对应数据不存在");
- }
- Long responsibilityId = costAccountShare.getResponsibilityId();
- Responsibility responsibility = responsibilityService.getById(responsibilityId);
- Long shareId = responsibility.getShareId();
- CostShareLevel costShareLevel = costShareLevelService.getById(shareId);
- Integer calcType = costShareLevel.getCalcType();
- // 分摊参数设置对饮的责任中心的id集合
- List<Long> responsibilityIds = this.list(new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId, hospId)).stream().map(CostAccountShare::getResponsibilityId).collect(Collectors.toList());
- // 执行类型的分摊级别的Id的集合
- List<Long> costShareIds = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
- .eq(CostShareLevel::getHospId,hospId).eq(CostShareLevel::getCalcType, calcType))
- .stream().map(CostShareLevel::getId).collect(Collectors.toList());
- // 筛选责任中心里面符合指定类型的责任中心
- // 筛选指定类型的责任中心 并且当前没有设置的责任中心
- List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
- .eq(Responsibility::getHospId, hospId)).stream()
- .filter(i -> costShareIds.contains(i.getShareId()) && !responsibilityIds.contains(i.getId())).collect(Collectors.toList());
- return responsibilityList;
- }
- }
|