FreeCostServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package com.kcim.service.impl;
  2. import com.kcim.common.constants.NumberConstant;
  3. import com.kcim.common.exception.CostException;
  4. import com.kcim.common.util.BeanUtil;
  5. import com.kcim.common.util.SnowflakeUtil;
  6. import com.kcim.common.util.UserContext;
  7. import com.kcim.dao.model.CostCostingGroup;
  8. import com.kcim.dao.model.FreeCostMap;
  9. import com.kcim.dao.model.ShareParamValue;
  10. import com.kcim.dao.repository.*;
  11. import com.kcim.service.FreeCostService;
  12. import com.kcim.service.ResponsibilityService;
  13. import lombok.AllArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.util.CollectionUtils;
  17. import java.math.BigDecimal;
  18. import java.math.RoundingMode;
  19. import java.util.*;
  20. import java.util.concurrent.atomic.AtomicReference;
  21. import java.util.stream.Collectors;
  22. /**
  23. * @program: CostAccount
  24. * @description:
  25. * @author: Wang.YS
  26. * @create: 2024-04-03 14:19
  27. **/
  28. @Service("FreeCostService")
  29. @Slf4j
  30. @AllArgsConstructor
  31. public class FreeCostServiceImpl implements FreeCostService {
  32. FreeCostMapRepository repository;
  33. AccountingRepository accountingRepository;
  34. CostShareParamRepository costShareParamRepository;
  35. CostCostingGroupRepository costCostingGroupRepository;
  36. ShareParamValueRepository shareParamValueRepository;
  37. ResponsibilityService responsibilityService;
  38. @Override
  39. public Object getList(String filter) {
  40. return repository.getList(filter);
  41. }
  42. /**
  43. * @param freeCostMap
  44. * @return
  45. */
  46. @Override
  47. public void addFreeCost(FreeCostMap freeCostMap) {
  48. freeCostMap.setHospId(UserContext.getHospId());
  49. freeCostMap.setCreateTime(new Date());
  50. freeCostMap.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  51. repository.save(freeCostMap);
  52. }
  53. /**
  54. * @param freeCostMap
  55. */
  56. @Override
  57. public void editFreeCost(FreeCostMap freeCostMap) {
  58. Integer id = freeCostMap.getId();
  59. if (id == null) {
  60. throw new CostException("编辑时主键必填");
  61. }
  62. FreeCostMap costMap = repository.getById(id);
  63. BeanUtil.convertObj(freeCostMap, costMap);
  64. costMap.setUpdateTime(new Date());
  65. costMap.setUpdateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  66. repository.updateById(costMap);
  67. }
  68. /**
  69. * @param id
  70. */
  71. @Override
  72. public void deleteFreeCost(Integer id) {
  73. if (id == null) {
  74. throw new CostException("删除时主键必填");
  75. }
  76. FreeCostMap costMap = repository.getById(id);
  77. costMap.setDeleteTime(new Date());
  78. costMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
  79. repository.updateById(costMap);
  80. repository.removeById(id);
  81. }
  82. @Override
  83. public Object getAccountList(String filter) {
  84. return accountingRepository.getBaseCostList(filter);
  85. }
  86. @Override
  87. public Object getShareParamList(String filter) {
  88. return costShareParamRepository.getList(filter);
  89. }
  90. @Override
  91. public void computeFreeCost(String computeDate) {
  92. /*
  93. 1 查询导入的成本数据 有 计算 无退出
  94. 2 通过闲置成本配置表找到对应成本项目数据 其他不计算
  95. 3 判断找到的闲置成本项目是否在当月已计算过 计算过合计需相加 未计算 取原成本项目
  96. 4 总额=闲置成本项目金额+设备成本项目金额
  97. 5 设备负荷系数=实际检查时间参数数值/可检查时间参数数值
  98. 6 设备成本项目金额=总额*设备负荷系数
  99. 7 闲置成本项目金额=总额-设备成本项目金额
  100. 8 未计算过 原数据更新 闲置成本添加 已计算过所有数据更新
  101. 9 原数据进行拆分更新
  102. */
  103. List<CostCostingGroup> list = costCostingGroupRepository.getList(computeDate);
  104. if (CollectionUtils.isEmpty(list)) {
  105. //成本数据未导入
  106. throw new CostException("未导入成本数据,无法计算闲置成本");
  107. }
  108. //获取所有闲置成本配置
  109. List<FreeCostMap> freeCostMaps = repository.getList(null);
  110. if (CollectionUtils.isEmpty(freeCostMaps)) {
  111. throw new CostException("未进行闲置成本对照,无法计算闲置成本");
  112. }
  113. List<ShareParamValue> shareParamValues = shareParamValueRepository.getList(computeDate);
  114. Map<String, Map<String, BigDecimal>> shareParamValueMap = new HashMap<>();
  115. if (!CollectionUtils.isEmpty(shareParamValues)) {
  116. Map<String, List<ShareParamValue>> collect = shareParamValues.stream().collect(Collectors.groupingBy(ShareParamValue::getResponsibilityCode));
  117. for (String s : collect.keySet()) {
  118. List<ShareParamValue> shareParamValues1 = collect.get(s);
  119. Map<String, BigDecimal> map = shareParamValues1.stream().collect(Collectors.toMap(ShareParamValue::getShareParamCode, ShareParamValue::getValueNum, (a, b) -> b));
  120. shareParamValueMap.put(s, map);
  121. }
  122. }
  123. //对照的会计科目为主键获取负荷系数
  124. //提取所有设置成本 项目code
  125. List<String> accountCodes = freeCostMaps.stream().map(FreeCostMap::getAccountCode).collect(Collectors.toList());
  126. List<CostCostingGroup> collect = list.stream().filter(f -> f.getComputeType().equals(NumberConstant.ONE)).collect(Collectors.toList());
  127. if (!CollectionUtils.isEmpty(collect)) {
  128. //已计算过 需要多一步处理原数据和现数据加起来计算总额
  129. //上次计算过的数据
  130. List<CostCostingGroup> lastCompute = list.stream().filter(f -> f.getComputeConnectId() != null).collect(Collectors.toList());
  131. if (!CollectionUtils.isEmpty(lastCompute)) {
  132. list.removeAll(lastCompute);
  133. //其他数据按原未计算过时计算
  134. List<CostCostingGroup> collect1 = list.stream().filter(f -> accountCodes.contains(f.getAccountCode())).collect(Collectors.toList());
  135. if (!CollectionUtils.isEmpty(collect1)) {
  136. Map<String, List<CostCostingGroup>> costAccountGroup = collect1.stream().collect(Collectors.groupingBy(CostCostingGroup::getAccountCode));
  137. List<CostCostingGroup> saveList = new ArrayList<>();
  138. List<CostCostingGroup> updateList = new ArrayList<>();
  139. for (FreeCostMap freeCostMap : freeCostMaps) {
  140. List<CostCostingGroup> costCostingGroups = costAccountGroup.get(freeCostMap.getAccountCode());
  141. if (!CollectionUtils.isEmpty(costCostingGroups)) {
  142. for (CostCostingGroup costCostingGroup : costCostingGroups) {
  143. if (!CollectionUtils.isEmpty(shareParamValueMap)) {
  144. Map<String, BigDecimal> map = shareParamValueMap.get(costCostingGroup.getResponsibilityCode());
  145. if (CollectionUtils.isEmpty(map)) {
  146. continue;
  147. }
  148. //可检查时间参数
  149. BigDecimal can = map.get(freeCostMap.getCanParamCode());
  150. if (can == null||can.compareTo(BigDecimal.ZERO)==0) {
  151. continue;
  152. }
  153. Long id = SnowflakeUtil.getId();
  154. //实际检查时间参数
  155. BigDecimal actual = map.get(freeCostMap.getActualParamCode());
  156. BigDecimal divide = actual.divide(can, 4, RoundingMode.HALF_UP);
  157. BigDecimal amount = costCostingGroup.getAmount();
  158. BigDecimal multiply = amount;
  159. if(divide.compareTo(BigDecimal.ONE)<0){
  160. multiply = amount.multiply(divide);
  161. }
  162. CostCostingGroup freeCost = BeanUtil.convertObj(costCostingGroup, CostCostingGroup.class);
  163. costCostingGroup.setAmount(multiply);
  164. costCostingGroup.setRate(divide);
  165. costCostingGroup.setComputeConnectId(id);
  166. updateList.add(costCostingGroup);
  167. freeCost.setAmount(amount.subtract(multiply));
  168. freeCost.setComputeType(NumberConstant.ONE);
  169. freeCost.setComputeConnectId(id);
  170. freeCost.setRate(BigDecimal.ONE.subtract(divide));
  171. freeCost.setId(null);
  172. freeCost.setAccountCode(freeCostMap.getFreeAccountCode());
  173. freeCost.setAccountName(freeCostMap.getFreeAccountName());
  174. freeCost.setResponsibilityCode(freeCostMap.getFreeResponsibilityCode());
  175. freeCost.setResponsibilityName(freeCostMap.getFreeResponsibilityName());
  176. saveList.add(freeCost);
  177. // }else {
  178. }
  179. }
  180. }
  181. }
  182. if (!CollectionUtils.isEmpty(updateList)) {
  183. costCostingGroupRepository.updateBatchById(updateList);
  184. }
  185. if (!CollectionUtils.isEmpty(saveList)) {
  186. costCostingGroupRepository.saveBatch(saveList);
  187. }
  188. }
  189. //已计算过的数据 单独方法处理
  190. Map<Long, List<CostCostingGroup>> lastComputeGroup = lastCompute.stream().collect(Collectors.groupingBy(CostCostingGroup::getComputeConnectId));
  191. //取出合计金额
  192. Map<Long, BigDecimal> totalMap = new HashMap<>();
  193. for (Long aLong : lastComputeGroup.keySet()) {
  194. List<CostCostingGroup> costCostingGroups = lastComputeGroup.get(aLong);
  195. AtomicReference<BigDecimal> total = new AtomicReference<>(new BigDecimal("0.0000"));
  196. costCostingGroups.forEach(m -> {
  197. total.updateAndGet(v -> v.add(m.getAmount()));
  198. });
  199. totalMap.put(aLong, total.get());
  200. }
  201. Map<String, FreeCostMap> accountMap = new HashMap<>();
  202. // Map<String, FreeCostMap> freeAccountMap = new HashMap<>();
  203. freeCostMaps.forEach(freeCostMap -> {
  204. accountMap.put(freeCostMap.getAccountCode(), freeCostMap);
  205. // freeAccountMap.put(freeCostMap.getAccountCode(), freeCostMap);
  206. });
  207. List<CostCostingGroup> lastAccount = lastCompute.stream().filter(f -> f.getComputeType().equals(NumberConstant.ZERO)).collect(Collectors.toList());
  208. List<CostCostingGroup> lastFreeAccount = lastCompute.stream().filter(f -> f.getComputeType().equals(NumberConstant.ONE)).collect(Collectors.toList());
  209. Map<Long, BigDecimal> freeAmount = new HashMap<>();
  210. Map<Long, BigDecimal> freeRate = new HashMap<>();
  211. List<CostCostingGroup> lastUpdateList = new ArrayList<>();
  212. for (CostCostingGroup costCostingGroup : lastAccount) {
  213. if (!CollectionUtils.isEmpty(shareParamValueMap)) {
  214. Map<String, BigDecimal> map = shareParamValueMap.get(costCostingGroup.getResponsibilityCode());
  215. if (CollectionUtils.isEmpty(map)) {
  216. continue;
  217. }
  218. FreeCostMap freeCostMap = accountMap.get(costCostingGroup.getAccountCode());
  219. if(Objects.isNull(freeCostMap)){
  220. continue;
  221. }
  222. //可检查时间参数
  223. BigDecimal can = map.get(freeCostMap.getCanParamCode());
  224. if (can == null||can.compareTo(BigDecimal.ZERO)==0) {
  225. continue;
  226. }
  227. //实际检查时间参数
  228. BigDecimal actual = map.get(freeCostMap.getActualParamCode());
  229. BigDecimal divide = actual.divide(can, 4, RoundingMode.HALF_UP);
  230. BigDecimal amount = totalMap.get(costCostingGroup.getComputeConnectId());
  231. BigDecimal multiply = amount;
  232. if(divide.compareTo(BigDecimal.ONE)<0){
  233. multiply = amount.multiply(divide);
  234. }
  235. costCostingGroup.setAmount(multiply);
  236. costCostingGroup.setRate(divide);
  237. lastUpdateList.add(costCostingGroup);
  238. freeAmount.put(costCostingGroup.getComputeConnectId(),amount.subtract(multiply));
  239. freeRate.put(costCostingGroup.getComputeConnectId(),BigDecimal.ONE.subtract(divide));
  240. }
  241. }
  242. for (CostCostingGroup costCostingGroup : lastFreeAccount) {
  243. costCostingGroup.setAmount(freeAmount.get(costCostingGroup.getComputeConnectId()));
  244. costCostingGroup.setRate(freeRate.get(costCostingGroup.getComputeConnectId()));
  245. lastUpdateList.add(costCostingGroup);
  246. }
  247. if (!CollectionUtils.isEmpty(lastUpdateList)) {
  248. costCostingGroupRepository.updateBatchById(lastUpdateList);
  249. }
  250. }
  251. } else {
  252. //未计算过直接到原数据作为总额
  253. //符合条件会计科目的数据 未配置 不计算
  254. List<CostCostingGroup> collect1 = list.stream().filter(f -> accountCodes.contains(f.getAccountCode())).collect(Collectors.toList());
  255. if (!CollectionUtils.isEmpty(collect1)) {
  256. Map<String, List<CostCostingGroup>> costAccountGroup = collect1.stream().collect(Collectors.groupingBy(CostCostingGroup::getAccountCode));
  257. List<CostCostingGroup> saveList = new ArrayList<>();
  258. List<CostCostingGroup> updateList = new ArrayList<>();
  259. for (FreeCostMap freeCostMap : freeCostMaps) {
  260. List<CostCostingGroup> costCostingGroups = costAccountGroup.get(freeCostMap.getAccountCode());
  261. if (!CollectionUtils.isEmpty(costCostingGroups)) {
  262. for (CostCostingGroup costCostingGroup : costCostingGroups) {
  263. if (!CollectionUtils.isEmpty(shareParamValueMap)) {
  264. Map<String, BigDecimal> map = shareParamValueMap.get(costCostingGroup.getResponsibilityCode());
  265. if (CollectionUtils.isEmpty(map)) {
  266. continue;
  267. }
  268. //可检查时间参数
  269. BigDecimal can = map.get(freeCostMap.getCanParamCode());
  270. if (can == null||can.compareTo(BigDecimal.ZERO)==0) {
  271. continue;
  272. }
  273. Long id = SnowflakeUtil.getId();
  274. //实际检查时间参数
  275. BigDecimal actual = map.get(freeCostMap.getActualParamCode());
  276. BigDecimal divide = actual.divide(can, 4, RoundingMode.HALF_UP);
  277. BigDecimal amount = costCostingGroup.getAmount();
  278. BigDecimal multiply = amount;
  279. if(divide.compareTo(BigDecimal.ONE)<0){
  280. multiply = amount.multiply(divide);
  281. }
  282. CostCostingGroup freeCost = BeanUtil.convertObj(costCostingGroup, CostCostingGroup.class);
  283. costCostingGroup.setAmount(multiply);
  284. costCostingGroup.setRate(divide);
  285. costCostingGroup.setComputeConnectId(id);
  286. updateList.add(costCostingGroup);
  287. freeCost.setAmount(amount.subtract(multiply));
  288. freeCost.setComputeType(NumberConstant.ONE);
  289. freeCost.setComputeConnectId(id);
  290. freeCost.setRate(BigDecimal.ONE.subtract(divide));
  291. freeCost.setId(null);
  292. freeCost.setAccountCode(freeCostMap.getFreeAccountCode());
  293. freeCost.setAccountName(freeCostMap.getFreeAccountName());
  294. freeCost.setResponsibilityCode(freeCostMap.getFreeResponsibilityCode());
  295. freeCost.setResponsibilityName(freeCostMap.getFreeResponsibilityName());
  296. saveList.add(freeCost);
  297. // }else {
  298. }
  299. }
  300. }
  301. }
  302. if (!CollectionUtils.isEmpty(updateList)) {
  303. costCostingGroupRepository.updateBatchById(updateList);
  304. }
  305. if (!CollectionUtils.isEmpty(saveList)) {
  306. costCostingGroupRepository.saveBatch(saveList);
  307. }
  308. }
  309. }
  310. //
  311. }
  312. @Override
  313. public Object getResponsibilityList(String filter) {
  314. return responsibilityService.getList(filter);
  315. }
  316. }