123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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.CostAccountShareService;
- import com.imed.costaccount.utils.BeanUtil;
- import org.apache.shiro.SecurityUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- 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<CostAccountShareMapper, CostAccountShare> implements CostAccountShareService {
- @Autowired
- private ResponsibilityServiceImpl responsibilityService;
- @Autowired
- private AccountingServiceImpl accountingService;
- @Autowired
- private CostShareLevelServiceImpl costShareLevelService;
- @Autowired
- private CostShareParamServiceImpl costShareParamService;
- /**
- * 分页查询责任中心成本对照相关数据
- *
- * @param page
- * @param pageSize
- * @param name
- * @return
- */
- @Override
- public PageUtils queryList(Integer page, Integer pageSize, String name, Integer hospId) {
- Page<CostAccountShare> costAccountSharePage = new Page<>(page, 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);
- getMessage(hospId, costAccountShareVOList);
- //
- PageUtils pageUtils = new PageUtils(pages);
- pageUtils.setList(costAccountShareVOList);
- return pageUtils;
- }
- private void getMessage(Integer 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<Integer, List<Responsibility>> resMap = list.stream().collect(Collectors.groupingBy(Responsibility::getId));
- Map<Integer, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
- costAccountShareList.forEach(i->{
- Integer 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());
- }
- Integer 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();
- Integer 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, Integer hospId) {
- Integer 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());
- if (costAccountShareSaveDto.getAccountingId() > 0){
- // 如果输入成本科目的情况下
- Accounting accounting = accountingService.getOne(new QueryWrapper<Accounting>().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<CostAccountShare> costAccountShareList = baseMapper.selectList(new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId,hospId));
- Map<String, List<CostAccountShare>> costAccountMap = costAccountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode));
- List<CostAccountShare> list = costAccountMap.get(costAccountShareSaveDto.getResponsibilityCode());
- if (!CollectionUtils.isEmpty(list)){
- String accountingCode = list.get(0).getAccountingCode();
- if (accountingCode.equals(costAccountShareSaveDto.getAccountingCode())){
- throw new CostException(500,"输入的责任中心对应的成本科目已存在");
- }
- }
- }
- // 检验输入的这个责任中心是否允许输入成本科目
- Integer shareLevel = responsibility.getShareLevel();
- CostShareLevel costShareLevel = costShareLevelService.getOne(new QueryWrapper<CostShareLevel>().lambda()
- .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getId, shareLevel));
- if (Objects.nonNull(costShareLevel)){
- if (costAccountShareSaveDto.getAccountingId()>0 && NumberConstant.ZERO.equals(costShareLevel.getCalcType())){
- throw new CostException(500,"合并计算不允许选择成本科目");
- }
- if (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();
- Integer hospId = user.getHospId();
- Integer 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);
- CostAccountShare costAccountShareRequest = BeanUtil.convertObj(costAccountShareEditDto, 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) {
- User user = (User) SecurityUtils.getSubject().getPrincipal();
- Integer hospId = user.getHospId();
- Integer 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,"责任中心成本数据不存在");
- }
- List<ShareParamProportionVO> shareParamProportionVOList = shareParamEditDto.getShareParamProportionVOList();
- // 检验输入的成本分摊参数是否存在
- List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
- Map<Integer, 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<Integer, ShareParamProportionVO> hashMap = new HashMap<>();
- shareParamProportionVOList.forEach(i->{
- Integer paramId = i.getId();
- if (hashMap.containsKey(paramId)){
- throw new CostException(500,"不可以添加相同的分摊参数");
- }
- hashMap.put(paramId,i);
- });
- // TODO 暂时还考虑不到如何筛选选中的
- String paramList = JsonUtil.toJSONString(shareParamProportionVOList);
- costAccountShare.setParamList(paramList);
- baseMapper.updateById(costAccountShare);
- }
- /**
- * 获取责任中心成本表的分摊参数的集合
- *
- * @param id
- * @param hospId
- * @return
- */
- @Override
- public List<ShareParamProportionVO> selectShareParamById(Integer id, Integer 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, Integer 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<Integer, List<ShareParamProportionVO>> map = shareParamProportionVOList.stream().collect(Collectors.groupingBy(ShareParamProportionVO::getId));
- costShareParamStatusVOList.forEach(i -> {
- Integer paramId = i.getId();
- if (!CollectionUtils.isEmpty(map.get(paramId))) {
- i.setShareParamStatus(NumberConstant.ONE);
- }
- });
- }
- return costShareParamStatusVOList;
- }
- }
|