|
@@ -1,5 +1,6 @@
|
|
|
package com.imed.costaccount.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
@@ -14,9 +15,7 @@ import com.imed.costaccount.enums.DateStyleEnum;
|
|
|
import com.imed.costaccount.mapper.CostCostingGroupMapper;
|
|
|
import com.imed.costaccount.model.*;
|
|
|
import com.imed.costaccount.model.dto.StartDTO;
|
|
|
-import com.imed.costaccount.model.vo.AllocationVO;
|
|
|
-import com.imed.costaccount.model.vo.CostingGroupStartVO;
|
|
|
-import com.imed.costaccount.model.vo.IncomeErrorMessage;
|
|
|
+import com.imed.costaccount.model.vo.*;
|
|
|
import com.imed.costaccount.service.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -27,6 +26,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@Service("costCostingGroupService")
|
|
@@ -35,6 +35,8 @@ public class CostCostingGroupServiceImpl extends ServiceImpl<CostCostingGroupMap
|
|
|
|
|
|
private final CostIncomeGroupServiceImpl costIncomeGroupService;
|
|
|
|
|
|
+ private final AccountingService accountingService;
|
|
|
+
|
|
|
private final AccountingProductService accountingProductService;
|
|
|
|
|
|
private final CostIncomeFileService costIncomeFileService;
|
|
@@ -43,16 +45,34 @@ public class CostCostingGroupServiceImpl extends ServiceImpl<CostCostingGroupMap
|
|
|
|
|
|
private final CostCostingCollectionService costingCollectionService;
|
|
|
|
|
|
+ private final CostShareLevelService shareLevelService;
|
|
|
+
|
|
|
+ private final CostAccountShareService accountShareService;
|
|
|
+
|
|
|
+ private final ShareParamValueService shareParamValueService;
|
|
|
+
|
|
|
+ private final CostShareParamService shareParamService;
|
|
|
+
|
|
|
public CostCostingGroupServiceImpl(CostIncomeGroupServiceImpl costIncomeGroupService,
|
|
|
+ AccountingService accountingService,
|
|
|
AccountingProductService accountingProductService,
|
|
|
CostIncomeFileService costIncomeFileService,
|
|
|
ResponsibilityService responsibilityService,
|
|
|
- CostCostingCollectionService costingCollectionService) {
|
|
|
+ CostCostingCollectionService costingCollectionService,
|
|
|
+ CostShareLevelService shareLevelService,
|
|
|
+ CostAccountShareService accountShareService,
|
|
|
+ ShareParamValueService shareParamValueService,
|
|
|
+ CostShareParamService shareParamService) {
|
|
|
this.costIncomeGroupService = costIncomeGroupService;
|
|
|
+ this.accountingService = accountingService;
|
|
|
this.accountingProductService = accountingProductService;
|
|
|
this.costIncomeFileService = costIncomeFileService;
|
|
|
this.responsibilityService = responsibilityService;
|
|
|
this.costingCollectionService = costingCollectionService;
|
|
|
+ this.shareLevelService = shareLevelService;
|
|
|
+ this.accountShareService = accountShareService;
|
|
|
+ this.shareParamValueService = shareParamValueService;
|
|
|
+ this.shareParamService = shareParamService;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -131,24 +151,203 @@ public class CostCostingGroupServiceImpl extends ServiceImpl<CostCostingGroupMap
|
|
|
@Override
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
public void startAllocation(StartDTO startDTO, Long hospId) {
|
|
|
- // 先拿到这个月数据 如果数据量很大 优化处理
|
|
|
+ // 这个月成本数据数据
|
|
|
List<CostCostingGroup> list = this.list(
|
|
|
new LambdaQueryWrapper<CostCostingGroup>()
|
|
|
.eq(CostCostingGroup::getDateYear, startDTO.getYear())
|
|
|
.eq(CostCostingGroup::getDateMonth, startDTO.getMonth())
|
|
|
.eq(CostCostingGroup::getHospId, hospId)
|
|
|
);
|
|
|
+ // 没有重新导入
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ throw new CostException("本月成本数据暂未导入");
|
|
|
+ }
|
|
|
|
|
|
- // 删除该年月已经过的数据
|
|
|
- costingCollectionService.remove(
|
|
|
- new LambdaQueryWrapper<CostCostingCollection>()
|
|
|
- .eq(CostCostingCollection::getYear, startDTO.getYear())
|
|
|
- .eq(CostCostingCollection::getMonth, startDTO.getMonth())
|
|
|
- .eq(CostCostingCollection::getHospId, hospId)
|
|
|
- );
|
|
|
- // 得到分摊层级为1 的
|
|
|
-// list.stream().map(get)
|
|
|
+ Map<String, List<CostCostingGroup>> responsibilityCodeMap = list.stream().collect(Collectors.groupingBy(CostCostingGroup::getResponsibilityCode));
|
|
|
+ Set<String> responsibilityCodes = responsibilityCodeMap.keySet();
|
|
|
+ for (String responsibilityCode : responsibilityCodes) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// // 得到这个月导入的成本分摊参数值数据
|
|
|
+// List<ShareParamValue> shareParamValues = shareParamValueService.list(
|
|
|
+// new LambdaQueryWrapper<ShareParamValue>()
|
|
|
+// .eq(ShareParamValue::getHospId, hospId)
|
|
|
+// .eq(ShareParamValue::getDateYear, startDTO.getYear())
|
|
|
+// .eq(ShareParamValue::getDateMonth, startDTO.getMonth())
|
|
|
+// );
|
|
|
+// // 没有重新导入
|
|
|
+// if (shareParamValues.isEmpty()) {
|
|
|
+// throw new CostException("本月分摊参数值数据未导入");
|
|
|
+// }
|
|
|
+// // 通过会计科目聚合月成本数据
|
|
|
+// Map<String, List<CostCostingGroup>> accountMap = list.stream().collect(Collectors.groupingBy(CostCostingGroup::getAccountCode));
|
|
|
+//
|
|
|
+// // 删除该年月已经过的数据
|
|
|
+// costingCollectionService.remove(
|
|
|
+// new LambdaQueryWrapper<CostCostingCollection>()
|
|
|
+// .eq(CostCostingCollection::getYear, startDTO.getYear())
|
|
|
+// .eq(CostCostingCollection::getMonth, startDTO.getMonth())
|
|
|
+// .eq(CostCostingCollection::getHospId, hospId)
|
|
|
+// );
|
|
|
+// // 得到这个医院所有的分摊层级(通过sort_level正序排序的数据)
|
|
|
+// List<CostShareLevelVO> shareLevelVOs = shareLevelService.getAll(hospId);
|
|
|
+// if (CollUtil.isEmpty(shareLevelVOs)) {
|
|
|
+// throw new CostException("请先设置医院分摊层级");
|
|
|
+// }
|
|
|
+// List<CostCostingCollection> costingCollections = new ArrayList<>();
|
|
|
+// // 遍历这个医院的分摊层级
|
|
|
+// for (int i = 0; i < shareLevelVOs.size(); i++) {
|
|
|
+// // 最后一层不分摊
|
|
|
+// if (i == shareLevelVOs.size() - 1) {
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// // 通过分摊层级id 得到对应的责任中心
|
|
|
+// CostShareLevelVO shareLevelVO = shareLevelVOs.get(i);
|
|
|
+// Responsibility responsibility = responsibilityService.getByLevelId(shareLevelVO.getId(), hospId);
|
|
|
+// // 如果这个分摊层级没有责任中心那么不分摊
|
|
|
+// if (Objects.isNull(responsibility)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// // 通过责任中心code去得到分摊参数对应信息 并得到对应的会计科目和对应的分摊参数比例等信息
|
|
|
+// List<CostAccountShare> accountShares = accountShareService.getByResponsibility(responsibility.getResponsibilityCode(), hospId);
|
|
|
+// if (accountShares.isEmpty()) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// // 遍历分摊参数对应信息
|
|
|
+// for (CostAccountShare accountShare : accountShares) {
|
|
|
+// // 得到对应的会计中心code
|
|
|
+// String accountingCodes = accountShare.getAccountingCodes();
|
|
|
+// // 没有会计中心,合并计算未设置好分摊参数比例
|
|
|
+// if (StrUtil.isBlank(accountingCodes) && shareLevelVO.getCalcType().equals(1)) {
|
|
|
+// throw new CostException("责任中心:" + accountShare.getResponsibilityName() + ";会计中心为:" + accountShare.getAccountingNames() + ";未设置分摊参数比例");
|
|
|
+// }
|
|
|
+// // 会计科目不存在且是合并计算,这种情况直接分摊下去
|
|
|
+// String paramList = accountShare.getParamList();
|
|
|
+// if (StrUtil.isBlank(paramList)) {
|
|
|
+// throw new CostException("责任中心:" + accountShare.getResponsibilityName() + ";会计中心为:" + accountShare.getAccountingNames() + (";未设置分摊参数比例值"));
|
|
|
+// }
|
|
|
+// // 分摊参数以及比例集合
|
|
|
+// List<AccountShareVO> accountShareVOs = JacksonUtil.str2ObjList(paramList, List.class, AccountShareVO.class);
|
|
|
+// // 没有会计中心直接分摊的情况
|
|
|
+// if (StrUtil.isBlank(accountingCodes) && shareLevelVO.getCalcType().equals(0)) {
|
|
|
+// // 直接分摊到责任中心去 这情况只有一个分摊计划
|
|
|
+// AccountShareVO accountShareVO = accountShareVOs.get(0);
|
|
|
+// Long shareParamId = accountShareVO.getId();
|
|
|
+// CostShareParam shareParam = shareParamService.getByIdAndCalcType(shareParamId);
|
|
|
+// if (Objects.isNull(shareParam)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// String accountCodeStr = shareParam.getAccountingCodes();
|
|
|
+// if (StrUtil.isBlank(accountCodeStr)) {
|
|
|
+// throw new CostException("成本分摊参数:" + shareParam.getShareParamName() + ";设置会计科目不存在");
|
|
|
+// }
|
|
|
+// ArrayList<String> accountCodes = CollUtil.newArrayList(accountCodeStr.split(StrUtil.COMMA));
|
|
|
+// String shareParamPout = accountShareVO.getShareParamPopout();
|
|
|
+// BigDecimal rate = new BigDecimal("1");
|
|
|
+// if (!"100".equals(shareParamPout)) {
|
|
|
+// rate = new BigDecimal("0." + shareParamPout);
|
|
|
+// }
|
|
|
+// // 计算分摊数值
|
|
|
+// BigDecimal valueNum = shareParamValues.stream().map(ShareParamValue::getValueNum).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+// for (String accountCode : accountCodes) {
|
|
|
+// Accounting account = this.getAccountByCode(accountCode, hospId);
|
|
|
+// List<CostCostingGroup> costCostingGroups = accountMap.get(accountCode);
|
|
|
+// if (CollUtil.isEmpty(costCostingGroups)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// BigDecimal accountCodeAmount = costCostingGroups.stream().map(CostCostingGroup::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+// CostCostingCollection collection = new CostCostingCollection();
|
|
|
+// collection.setYear(startDTO.getYear()).setMonth(startDTO.getMonth()).setResponsibilityCode(accountShare.getResponsibilityCode())
|
|
|
+// .setResponsibilityName(accountShare.getResponsibilityName()).setLeverSort(shareLevelVO.getLeverSort())
|
|
|
+// .setShareLevelName(shareLevelVO.getShareName()).setAccountCode(accountCode).setAccountName(account.getAccountingName())
|
|
|
+// .setIsBaseCost(account.getIsBaseCost()).setAmount(accountCodeAmount).setCreateTime(System.currentTimeMillis())
|
|
|
+// .setHospId(hospId).setShareParam(accountShareVO.getShareParamName()).setShareParamProportion(rate.toString())
|
|
|
+// .setShareValue(valueNum)
|
|
|
+// // 目标分摊层级暂不处理
|
|
|
+// //.setTargetResponsibilityCode().setTargetResponsibilityName()
|
|
|
+// .setShareAmount(rate.multiply(accountCodeAmount));
|
|
|
+// costingCollections.add(collection);
|
|
|
+// }
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// // 责任中心对应的会计科目代码列表
|
|
|
+// ArrayList<String> accountCodeList = CollUtil.newArrayList(accountingCodes.split(StrUtil.COMMA));
|
|
|
+// for (String accountCode : accountCodeList) {
|
|
|
+// // 分摊前数据 通过会计科目得到
|
|
|
+// List<CostCostingGroup> costCostingGroups = accountMap.get(accountCode);
|
|
|
+//
|
|
|
+// if (CollUtil.isEmpty(costCostingGroups)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// BigDecimal accountCodeAmount = costCostingGroups.stream().map(CostCostingGroup::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+// Accounting accounting = this.getAccountByCode(accountCode, hospId);
|
|
|
+// if (StrUtil.isBlank(paramList)) {
|
|
|
+// throw new CostException("未设置分摊参数比例值");
|
|
|
+// }
|
|
|
+// // 分摊比例相关数据
|
|
|
+// if (CollUtil.isEmpty(accountShareVOs)) {
|
|
|
+// throw new CostException("分摊参数对应比例错误");
|
|
|
+// }
|
|
|
+// for (AccountShareVO accountShareVO : accountShareVOs) {
|
|
|
+// CostCostingCollection collection = new CostCostingCollection();
|
|
|
+// String shareParamPout = accountShareVO.getShareParamPopout();
|
|
|
+// BigDecimal rate = new BigDecimal("1");
|
|
|
+// if (!"100".equals(shareParamPout)) {
|
|
|
+// rate = new BigDecimal("0." + shareParamPout);
|
|
|
+// }
|
|
|
+// // 计算分摊数值
|
|
|
+// BigDecimal valueNum = shareParamValues.stream().map(ShareParamValue::getValueNum).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+// collection.setYear(startDTO.getYear()).setMonth(startDTO.getMonth()).setResponsibilityCode(accountShare.getResponsibilityCode())
|
|
|
+// .setResponsibilityName(accountShare.getResponsibilityName()).setLeverSort(shareLevelVO.getLeverSort())
|
|
|
+// .setShareLevelName(shareLevelVO.getShareName()).setAccountCode(accountCode).setAccountName(accounting.getAccountingName())
|
|
|
+// .setIsBaseCost(accounting.getIsBaseCost()).setAmount(accountCodeAmount).setCreateTime(System.currentTimeMillis())
|
|
|
+// .setHospId(hospId).setShareParam(accountShareVO.getShareParamName()).setShareParamProportion(rate.toString())
|
|
|
+// .setShareValue(valueNum)
|
|
|
+// // 目标分摊层级暂不处理
|
|
|
+// //.setTargetResponsibilityCode().setTargetResponsibilityName()
|
|
|
+// .setShareAmount(rate.multiply(accountCodeAmount));
|
|
|
+// costingCollections.add(collection);
|
|
|
+//// costingCollectionService.save(collection);
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// log.info("list:{}", costingCollections);
|
|
|
+// costingCollectionService.saveBatch(costingCollections);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过code获取名称
|
|
|
+ *
|
|
|
+ * @param accountCode
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Accounting getAccountByCode(String accountCode, Long hospId) {
|
|
|
+ Accounting byCode = accountingService.getByCode(accountCode, hospId);
|
|
|
+ if (Objects.isNull(byCode)) {
|
|
|
+ throw new CostException("会计科目不存在");
|
|
|
+ }
|
|
|
+ return byCode;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 是否固定成本
|
|
|
+ *
|
|
|
+ * @param hospId 医院id
|
|
|
+ * @param i {@link CostCostingGroup}
|
|
|
+ * @return 是否固定成本 0.不是,1.是(支出的时候才会存在)
|
|
|
+ */
|
|
|
+ private Integer getIsBaseCost(Long hospId, CostCostingGroup i) {
|
|
|
+ String accountCode = i.getAccountCode();
|
|
|
+ Accounting accounting = accountingService.getByCode(accountCode, hospId);
|
|
|
+ if (Objects.isNull(accountCode)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 是否固定成本 0.不是,1.是(支出的时候才会存在) (成本数据)
|
|
|
+ return accounting.getIsBaseCost();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -426,4 +625,23 @@ public class CostCostingGroupServiceImpl extends ServiceImpl<CostCostingGroupMap
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到这个月的所有导入的成本数据
|
|
|
+ *
|
|
|
+ * @param year 年
|
|
|
+ * @param month 月
|
|
|
+ * @param hospId 医院id
|
|
|
+ * @return List
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<CostCostingGroup> getByYearAndDate(Integer year, Integer month, Long hospId) {
|
|
|
+ return this.list(
|
|
|
+ new LambdaQueryWrapper<CostCostingGroup>()
|
|
|
+ .eq(CostCostingGroup::getDateYear, year)
|
|
|
+ .eq(CostCostingGroup::getDateMonth, month)
|
|
|
+ .eq(CostCostingGroup::getHospId, hospId)
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|