CostAccountShareServiceImpl.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. package com.kcim.service.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.kcim.common.constants.NumberConstant;
  9. import com.kcim.common.exception.CostException;
  10. import com.kcim.common.util.BeanUtil;
  11. import com.kcim.common.util.JacksonUtil;
  12. import com.kcim.common.util.PageUtils;
  13. import com.kcim.common.util.UserContext;
  14. import com.kcim.dao.mapper.CostAccountShareMapper;
  15. import com.kcim.dao.model.*;
  16. import com.kcim.dao.model.dto.AccountShareCopyDto;
  17. import com.kcim.dao.model.dto.CostAccountShareEditDto;
  18. import com.kcim.dao.model.dto.CostAccountShareSaveDto;
  19. import com.kcim.dao.model.dto.ShareParamEditDto;
  20. import com.kcim.dao.repository.CostAccountShareDetailRepository;
  21. import com.kcim.dao.repository.CostAccountShareParamRepository;
  22. import com.kcim.dao.repository.CostAccountShareTargetRepository;
  23. import com.kcim.service.*;
  24. import com.kcim.vo.*;
  25. import lombok.AllArgsConstructor;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Propagation;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.util.CollectionUtils;
  31. import org.springframework.util.ObjectUtils;
  32. import org.springframework.util.StringUtils;
  33. import java.math.BigDecimal;
  34. import java.util.*;
  35. import java.util.concurrent.atomic.AtomicReference;
  36. import java.util.stream.Collectors;
  37. @Slf4j
  38. @AllArgsConstructor
  39. @Service("costAccountShareService")
  40. public class CostAccountShareServiceImpl extends ServiceImpl<CostAccountShareMapper, CostAccountShare> implements CostAccountShareService {
  41. private final ResponsibilityService responsibilityService;
  42. private final AccountingService accountingService;
  43. private final CostShareLevelService costShareLevelService;
  44. private final CostShareParamService costShareParamService;
  45. private final CostAccountShareDetailRepository costAccountShareDetailRepository;
  46. private final CostAccountShareParamRepository costAccountShareParamRepository;
  47. private final CostAccountShareTargetRepository costAccountShareTargetRepository;
  48. // public CostAccountShareServiceImpl(CostAccountShareParamService costAccountShareParamService,CostAccountShareDetailService costAccountShareDetailService,ResponsibilityService responsibilityService, AccountingService accountingService, CostShareLevelService costShareLevelService, CostShareParamService costShareParamService) {
  49. // this.responsibilityService = responsibilityService;
  50. // this.accountingService = accountingService;
  51. // this.costShareLevelService = costShareLevelService;
  52. // this.costShareParamService = costShareParamService;
  53. // this.costAccountShareDetailService = costAccountShareDetailService;
  54. // this.costAccountShareParamService = costAccountShareParamService;
  55. // }
  56. /**
  57. * 分页查询责任中心成本对照相关数据
  58. *
  59. * @param current
  60. * @param pageSize
  61. * @param name
  62. * @return
  63. */
  64. @Override
  65. public PageUtils queryList(Integer current, Integer pageSize, String name, Long hospId) {
  66. Page<CostAccountShare> costAccountSharePage = new Page<>(current, pageSize);
  67. Page<CostAccountShare> pages = this.page(costAccountSharePage, new QueryWrapper<CostAccountShare>().lambda()
  68. .eq(!StringUtils.isEmpty(hospId), CostAccountShare::getHospId, hospId)
  69. .like(!StringUtils.isEmpty(name), CostAccountShare::getResponsibilityName, name)
  70. .orderByAsc(CostAccountShare::getShareLevel));
  71. List<CostAccountShare> costAccountShareList = pages.getRecords();
  72. List<Responsibility> responsibilities = responsibilityService.list(
  73. new LambdaQueryWrapper<Responsibility>()
  74. .eq(Responsibility::getHospId, UserContext.getHospId())
  75. .orderByDesc(Responsibility::getCreateTime)
  76. );
  77. Map<Long, String> map = new HashMap<>();
  78. if (!CollectionUtils.isEmpty(responsibilities)) {
  79. map = responsibilities.stream().collect(Collectors.toMap(Responsibility::getId, Responsibility::getShareName, (a, b) -> b));
  80. }
  81. List<CostAccountShareVO> costAccountShareVOList = BeanUtil.convertList(costAccountShareList, CostAccountShareVO.class);
  82. for (CostAccountShareVO i : costAccountShareVOList) {
  83. String accountingIds = i.getAccountingIds();
  84. if (StrUtil.isNotBlank(accountingIds)) {
  85. List<String> list = Arrays.asList(accountingIds.split(StrUtil.COMMA));
  86. i.setAccountingIdList(list);
  87. } else {
  88. i.setAccountingIdList(null);
  89. }
  90. if (!CollectionUtils.isEmpty(map)) {
  91. i.setShareName(map.get(i.getResponsibilityId()));
  92. }
  93. if (!StringUtils.isEmpty(i.getParamList())) {
  94. List<AccountShareVO> accountShareVOs = JacksonUtil.str2ObjList(i.getParamList(), List.class, AccountShareVO.class);
  95. if (!CollectionUtils.isEmpty(accountShareVOs)) {
  96. StringBuilder shareParamBuilder = new StringBuilder();
  97. for (AccountShareVO accountShareVO : accountShareVOs) {
  98. shareParamBuilder.append(accountShareVO.getShareParamName()).append("x").append(accountShareVO.getShareParamPopout()).append("%").append("|");
  99. }
  100. i.setShareParamMap(org.apache.commons.lang3.StringUtils.removeEnd(shareParamBuilder.toString(), "|"));
  101. }
  102. }
  103. }
  104. // getMessage(hospId, costAccountShareVOList);
  105. PageUtils pageUtils = new PageUtils(pages);
  106. pageUtils.setList(costAccountShareVOList);
  107. return pageUtils;
  108. }
  109. // private void getMessage(Long hospId, List<CostAccountShareVO> costAccountShareList) {
  110. // // 设置责任中心的数据 与 会计科目的数据从对应的id里面获取
  111. // List<Responsibility> list = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
  112. // List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId));
  113. // Map<Long, List<Responsibility>> resMap = list.stream().collect(Collectors.groupingBy(Responsibility::getId));
  114. // Map<Long, List<Accounting>> accountMap = accountingList.stream().collect(Collectors.groupingBy(Accounting::getId));
  115. // costAccountShareList.forEach(i->{
  116. // Long id = i.getResponsibilityId();
  117. // List<Responsibility> responsibilities = resMap.get(id);
  118. // if (!CollectionUtils.isEmpty(responsibilities)){
  119. // i.setResponsibilityId(id);
  120. // i.setResponsibilityName(responsibilities.get(0).getResponsibilityName());
  121. // i.setResponsibilityCode(responsibilities.get(0).getResponsibilityCode());
  122. //
  123. // }
  124. // Long accountingId = i.getAccountingId();
  125. // if (accountingId>0){
  126. // List<Accounting> accountingList1 = accountMap.get(accountingId);
  127. // if (!CollectionUtils.isEmpty(accountingList1)){
  128. // i.setAccountingId(accountingId);
  129. // i.setAccountingName(accountingList1.get(0).getAccountingName());
  130. // i.setAccountingCode(accountingList1.get(0).getAccountingCode());
  131. // i.setAllParentIds(accountingList1.get(0).getAllParentIds());
  132. // }
  133. // }
  134. // });
  135. // }
  136. /**
  137. * 保存成本参数对应成本对照表
  138. *
  139. * @param costAccountShareSaveDto {@link CostAccountShareSaveDto}
  140. */
  141. @Override
  142. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  143. public void addCostAccountShare(CostAccountShareSaveDto costAccountShareSaveDto) {
  144. Long hospId = UserContext.getCurrentLoginHospId();
  145. // 检验输入的数据的合理性
  146. checkAccountShare(costAccountShareSaveDto, hospId,null);
  147. CostAccountShare costAccountShareRequest = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShare.class);
  148. costAccountShareRequest.setHospId(hospId);
  149. costAccountShareRequest.setCreateTime(System.currentTimeMillis());
  150. baseMapper.insert(costAccountShareRequest);
  151. //提交分摊设置对应的会计科目
  152. applyShareParamProportion(costAccountShareRequest.getId(),costAccountShareRequest.getAccountingCodes());
  153. }
  154. /**
  155. * 检验输入数据的合理性
  156. *
  157. * @param costAccountShareSaveDto
  158. * @param hospId
  159. */
  160. private void checkAccountShare(CostAccountShareSaveDto costAccountShareSaveDto, Long hospId,CostAccountShare oldCostAccountShare) {
  161. Long responsibilityId = costAccountShareSaveDto.getResponsibilityId();
  162. Responsibility responsibility = responsibilityService.getOne(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId).eq(Responsibility::getId, responsibilityId));
  163. if (Objects.isNull(responsibility)) {
  164. throw new CostException(500, "输入的责任不存在");
  165. }
  166. costAccountShareSaveDto.setResponsibilityId(responsibilityId);
  167. costAccountShareSaveDto.setResponsibilityCode(responsibility.getResponsibilityCode());
  168. costAccountShareSaveDto.setResponsibilityName(responsibility.getResponsibilityName());
  169. costAccountShareSaveDto.setShareLevel(responsibility.getShareLevel());
  170. //获取责任中心的其他设置记录
  171. List<CostAccountShare> costAccountShareList = getResponsibilityOtherRecords(costAccountShareSaveDto,hospId,oldCostAccountShare);
  172. if (!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingIds())) {
  173. // 这个责任中心允许输入会计科目
  174. List<Long> accountIdList = Arrays.stream(costAccountShareSaveDto.getAccountingIds().split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList());
  175. List<Accounting> accountingList = accountingService.list(new QueryWrapper<Accounting>().lambda().eq(Accounting::getHospId, hospId).in(Accounting::getId, accountIdList).orderByDesc(Accounting::getCreateTime));
  176. if (CollectionUtils.isEmpty(accountingList)) {
  177. throw new CostException(500, "输入的会计科目不存在");
  178. } else {
  179. String accountIds = accountingList.stream().map(Accounting::getId).collect(Collectors.toList()).stream().map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
  180. costAccountShareSaveDto.setAccountingIds(accountIds);
  181. String accountNames = accountingList.stream().map(Accounting::getAccountingName).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
  182. costAccountShareSaveDto.setAccountingNames(accountNames);
  183. String accountCodes = accountingList.stream().map(Accounting::getAccountingCode).map(String::valueOf).collect(Collectors.joining(StrUtil.COMMA));
  184. costAccountShareSaveDto.setAccountingCodes(accountCodes);
  185. }
  186. // Map<String, List<CostAccountShare>> costAccountMap = costAccountShareList.stream().collect(Collectors.groupingBy(CostAccountShare::getResponsibilityCode));
  187. // List<CostAccountShare> list = costAccountMap.get(costAccountShareSaveDto.getResponsibilityCode());
  188. if (!CollectionUtils.isEmpty(costAccountShareList)) {
  189. costAccountShareList.forEach(i -> {
  190. String accountingIds = i.getAccountingIds();
  191. Integer isShareCost = i.getIsShareCost();
  192. List<Long> accountIdListRequest = Arrays.stream(accountingIds.split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList());
  193. if (!Collections.disjoint(accountIdListRequest, accountIdList)) {
  194. throw new CostException(500, "存在相同的会计科目数据");
  195. }
  196. if (NumberConstant.ONE.equals(isShareCost) && NumberConstant.ONE.equals(costAccountShareSaveDto.getIsShareCost())) {
  197. throw new CostException(500, "同个责任中心只允许一条设置包含分摊成本");
  198. }
  199. });
  200. }
  201. } else {
  202. // 此时是
  203. // 责任中心只允许出现一次
  204. if (!CollectionUtils.isEmpty(costAccountShareList)) {
  205. throw new CostException(500, "该责任中心合并计算已存在");
  206. }
  207. if (!NumberConstant.ONE.equals(costAccountShareSaveDto.getIsShareCost())) {
  208. throw new CostException(500, "合并计算责任中心必须要设置成包含分摊参数");
  209. }
  210. }
  211. // 检验输入的这个责任中心是否允许输入会计科目
  212. Long shareId = responsibility.getShareId();
  213. CostShareLevel costShareLevel = costShareLevelService.getOne(new QueryWrapper<CostShareLevel>().lambda()
  214. .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getId, shareId));
  215. if (Objects.nonNull(costShareLevel)) {
  216. if ((!StringUtils.isEmpty(costAccountShareSaveDto.getAccountingIds())) && NumberConstant.ZERO.equals(costShareLevel.getCalcType())) {
  217. throw new CostException(500, "合并计算不允许选择会计科目");
  218. }
  219. if ((StringUtils.isEmpty(costAccountShareSaveDto.getAccountingIds())) && NumberConstant.ONE.equals(costShareLevel.getCalcType())) {
  220. throw new CostException(500, "分开计算的责任中心需要选择会计科目");
  221. }
  222. } else {
  223. throw new CostException(500, "对不起该责任中心没有对应分摊层级");
  224. }
  225. //合并计算的配置将责任中心名称作为会计科目名称(会计科目名称不能为空,不然后续报表会报错)
  226. if(NumberConstant.ZERO.equals(costShareLevel.getCalcType())){
  227. costAccountShareSaveDto.setAccountingCodes(costAccountShareSaveDto.getResponsibilityCode());
  228. costAccountShareSaveDto.setAccountingNames(costAccountShareSaveDto.getResponsibilityName());
  229. }
  230. }
  231. /**
  232. * 获取责任中心的其他设置记录
  233. * @param costAccountShareSaveDto
  234. * @param hospId
  235. * @param oldCostAccountShare
  236. * @return
  237. */
  238. public List<CostAccountShare> getResponsibilityOtherRecords(CostAccountShareSaveDto costAccountShareSaveDto, Long hospId,CostAccountShare oldCostAccountShare){
  239. LambdaQueryWrapper<CostAccountShare> costAccountShareQuery = new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId, hospId).eq(CostAccountShare::getResponsibilityId, costAccountShareSaveDto.getResponsibilityId());
  240. //修改时需排除掉自己
  241. if(!ObjectUtils.isEmpty(oldCostAccountShare)){
  242. costAccountShareQuery=costAccountShareQuery.ne(CostAccountShare::getId,oldCostAccountShare.getId());
  243. }
  244. List<CostAccountShare> costAccountShareList = baseMapper.selectList(costAccountShareQuery);
  245. return costAccountShareList;
  246. }
  247. /**
  248. * 修改成本分摊参数对应表
  249. *
  250. * @param costAccountShareEditDto
  251. */
  252. @Override
  253. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  254. public void updateByCostAccountShare(CostAccountShareEditDto costAccountShareEditDto) {
  255. Long hospId = UserContext.getCurrentLoginHospId();
  256. Long id = costAccountShareEditDto.getId();
  257. CostAccountShare costAccountShare = baseMapper.selectById(id);
  258. if (Objects.isNull(costAccountShare)) {
  259. throw new CostException(500, "成本分摊参数对应数据不存在");
  260. }
  261. // baseMapper.deleteById(id);
  262. // 新增责任中心成本对照数据
  263. CostAccountShareSaveDto costAccountShareSaveDto = BeanUtil.convertObj(costAccountShareEditDto, CostAccountShareSaveDto.class);
  264. // 检验输入的数据是否符合规则
  265. checkAccountShare(costAccountShareSaveDto, hospId,costAccountShare);
  266. CostAccountShareEditDto accountShareEditDto = BeanUtil.convertObj(costAccountShareSaveDto, CostAccountShareEditDto.class);
  267. BeanUtil.convertObj(accountShareEditDto, costAccountShare);
  268. costAccountShare.setId(costAccountShareEditDto.getId());
  269. baseMapper.updateById(costAccountShare);
  270. //提交分摊设置对应的会计科目
  271. applyShareParamProportion(costAccountShare.getId(),costAccountShare.getAccountingCodes());
  272. }
  273. /**
  274. * 提交分摊设置对应的会计科目
  275. * @param accountShareId
  276. * @param accountingCodes
  277. */
  278. public void applyShareParamProportion(Long accountShareId , String accountingCodes ){
  279. if(StringUtils.isEmpty(accountingCodes)){
  280. //先作废上次的数据
  281. costAccountShareDetailRepository.delCostAccountShareDetail(accountShareId);
  282. return;
  283. }
  284. List<String> accountingCodeList = Arrays.stream(accountingCodes.split(StrUtil.COMMA)).collect(Collectors.toList());
  285. applyAccountShareDetail(accountShareId,accountingCodeList);
  286. }
  287. /**
  288. * 提交分摊设置对应的会计科目
  289. * @param accountShareId
  290. * @param accountingCodeList
  291. */
  292. public void applyAccountShareDetail(Long accountShareId , List<String> accountingCodeList){
  293. //先作废上次的数据
  294. costAccountShareDetailRepository.delCostAccountShareDetail(accountShareId);
  295. //保存新的分摊参数
  296. List<CostAccountShareDetail> costAccountShareDetailList = accountingCodeList.stream().map(accountingCode ->
  297. createCostAccountShareDetail(accountShareId,accountingCode)).collect(Collectors.toList());
  298. costAccountShareDetailRepository.saveBatch(costAccountShareDetailList);
  299. }
  300. /**
  301. * 修改成本分摊参数的设置
  302. *
  303. * @param shareParamEditDto
  304. */
  305. @Override
  306. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  307. public void updateShareParam(ShareParamEditDto shareParamEditDto) {
  308. List<ShareParamProportionVO> shareParamProportionVOList1 = shareParamEditDto.getShareParamProportionVOList();
  309. Long hospId = UserContext.getHospId();
  310. Long id = shareParamEditDto.getId();
  311. CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
  312. .eq(CostAccountShare::getHospId, hospId)
  313. .eq(CostAccountShare::getId, id));
  314. if (Objects.isNull(costAccountShare)) {
  315. throw new CostException(500, "分摊参数对应数据不存在");
  316. }
  317. if (!CollectionUtils.isEmpty(shareParamProportionVOList1)) {
  318. List<ShareParamProportionVO> shareParamProportionVOList = shareParamEditDto.getShareParamProportionVOList();
  319. // 检验输入的成本分摊参数是否存在
  320. List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
  321. Map<Long, List<CostShareParamVO>> listMap = costShareParamServiceAll.stream().collect(Collectors.groupingBy(CostShareParamVO::getId));
  322. shareParamProportionVOList.forEach(i -> {
  323. if (CollectionUtils.isEmpty(listMap.get(i.getId()))) {
  324. throw new CostException(500, "分摊名称:" + i.getShareParamName() + "未找到");
  325. }
  326. });
  327. // 检验输入的数据的和是否是100
  328. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.00"));
  329. shareParamProportionVOList.forEach(i -> {
  330. sum.updateAndGet(v -> v.add(i.getShareParamPopout()));
  331. });
  332. if (sum.get().compareTo(new BigDecimal("100.00"))!=0) {
  333. throw new CostException(500, "分摊比例的和不是100");
  334. }
  335. // 判断添加的分摊是否存在一样的
  336. HashMap<Long, ShareParamProportionVO> hashMap = new HashMap<>();
  337. shareParamProportionVOList.forEach(i -> {
  338. Long paramId = i.getId();
  339. if (hashMap.containsKey(paramId)) {
  340. throw new CostException(500, "不可以添加相同的分摊参数");
  341. }
  342. hashMap.put(paramId, i);
  343. });
  344. //
  345. // String paramList = JsonUtil.toJSONString(shareParamProportionVOList);
  346. String paramList = JacksonUtil.obj2StrPretty(shareParamProportionVOList);
  347. costAccountShare.setParamList(paramList);
  348. //提交分摊参数
  349. applyShareParamProportion(shareParamEditDto);
  350. } else {
  351. costAccountShare.setParamList(null);
  352. }
  353. baseMapper.updateById(costAccountShare);
  354. }
  355. /**
  356. * 提交分摊参数
  357. * @param shareParamEditDto
  358. */
  359. public void applyShareParamProportion(ShareParamEditDto shareParamEditDto){
  360. //先作废上次的数据
  361. costAccountShareParamRepository.delCostAccountParamDetail(shareParamEditDto.getId());
  362. //保存新的分摊参数
  363. List<ShareParamProportionVO> newShareParamList = shareParamEditDto.getShareParamProportionVOList();
  364. List<CostAccountShareParam> costAccountShareParamList = newShareParamList.stream().map(newShareParam ->
  365. createCostAccountShareParam(shareParamEditDto.getId(),newShareParam.getShareParamCode(),newShareParam.getShareParamPopout())).collect(Collectors.toList());
  366. costAccountShareParamRepository.saveBatch(costAccountShareParamList);
  367. }
  368. /**
  369. * 获取责任中心成本表的分摊参数的集合
  370. *
  371. * @param id
  372. * @param hospId
  373. * @return
  374. */
  375. @Override
  376. public List<ShareParamProportionVO> selectShareParamById(Integer id, Long hospId) {
  377. CostAccountShare costAccountShare = baseMapper.selectOne(new QueryWrapper<CostAccountShare>().lambda()
  378. .eq(CostAccountShare::getHospId, hospId).eq(CostAccountShare::getId, id));
  379. if (Objects.isNull(costAccountShare)) {
  380. throw new CostException(500, "责任中心成本不存在");
  381. }
  382. String paramList = costAccountShare.getParamList();
  383. // return JsonUtil.toList(paramList, ShareParamProportionVO.class);
  384. // return JacksonUtil.string2ObjList(paramList, List.class, ShareParamProportionVO.class);
  385. return JacksonUtil.str2ObjList(paramList, new TypeReference<List<ShareParamProportionVO>>() {
  386. });
  387. }
  388. /**
  389. * 成本分摊参数中被分摊参数对应选中的状态
  390. *
  391. * @param id
  392. * @param hospId
  393. * @return
  394. */
  395. @Override
  396. public List<CostShareParamStatusVO> getAllShareParamStatusById(Integer id, Long hospId) {
  397. List<CostShareParamVO> costShareParamServiceAll = costShareParamService.getAll(hospId);
  398. CostAccountShare costAccountShare = baseMapper.selectById(id);
  399. if (Objects.isNull(costAccountShare)) {
  400. throw new CostException(500, "成本分摊对应数据不存在");
  401. }
  402. String paramList = costAccountShare.getParamList();
  403. List<CostShareParamStatusVO> costShareParamStatusVOList = BeanUtil.convertList(costShareParamServiceAll, CostShareParamStatusVO.class);
  404. if (!StringUtils.isEmpty(paramList)) {
  405. // List<ShareParamProportionVO> shareParamProportionVOList = JsonUtil.toList(paramList, ShareParamProportionVO.class);
  406. List<ShareParamProportionVO> shareParamProportionVOList = JacksonUtil.str2ObjList(paramList, new TypeReference<List<ShareParamProportionVO>>() {
  407. });
  408. Map<Long, List<ShareParamProportionVO>> map = shareParamProportionVOList.stream().collect(Collectors.groupingBy(ShareParamProportionVO::getId));
  409. costShareParamStatusVOList.forEach(i -> {
  410. Long paramId = i.getId();
  411. if (!CollectionUtils.isEmpty(map.get(paramId))) {
  412. i.setShareParamStatus(NumberConstant.ONE);
  413. }
  414. });
  415. }
  416. return costShareParamStatusVOList;
  417. }
  418. /**
  419. * 拷贝
  420. *
  421. * @param accountShareCopyDto 赋值数据
  422. * @param hospId 医院Id
  423. */
  424. @Override
  425. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  426. public void copyAccountShareData(AccountShareCopyDto accountShareCopyDto, Long hospId) {
  427. Long id = accountShareCopyDto.getId();
  428. CostAccountShare costAccountShare = this.getById(id);
  429. if (Objects.isNull(costAccountShare)) {
  430. throw new CostException(500, "成本分摊参数设置对应数据不存在");
  431. }
  432. List<CostAccountShare> accountShareList = new ArrayList<>();
  433. List<Long> responsibilityIds = accountShareCopyDto.getResponsibilityIds();
  434. Map<Long, Responsibility> responsibilityMap = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  435. .eq(Responsibility::getHospId, hospId)).stream().collect(Collectors.toMap(Responsibility::getId, synOne -> synOne));
  436. long millis = System.currentTimeMillis();
  437. responsibilityIds.forEach(i -> {
  438. CostAccountShare accountShareRequest = BeanUtil.convertObj(costAccountShare, CostAccountShare.class);
  439. accountShareRequest.setId(null);
  440. accountShareRequest.setCreateTime(millis);
  441. Responsibility responsibility = responsibilityMap.get(i);
  442. accountShareRequest.setResponsibilityId(i);
  443. accountShareRequest.setResponsibilityCode(responsibility.getResponsibilityCode());
  444. accountShareRequest.setResponsibilityName(responsibility.getResponsibilityName());
  445. accountShareRequest.setShareLevel(responsibility.getShareLevel());
  446. accountShareList.add(accountShareRequest);
  447. });
  448. this.saveBatch(accountShareList);
  449. //主表向明细表同步
  450. synMainCostAccountShare(accountShareList);
  451. //复制目标责任中心设置
  452. copyCostAccountShareTarget(accountShareCopyDto,accountShareList);
  453. }
  454. /**
  455. * 获取指定类型的责任中心集合 分开计算还是合并计算
  456. *
  457. * @param accountShareId
  458. * @param hospId
  459. * @return
  460. */
  461. @Override
  462. public List<Responsibility> getResponsibilityCalType(Long accountShareId, Long hospId) {
  463. CostAccountShare costAccountShare = this.getById(accountShareId);
  464. if (Objects.isNull(costAccountShare)) {
  465. throw new CostException(500, "成本分摊参数设置对应数据不存在");
  466. }
  467. Long responsibilityId = costAccountShare.getResponsibilityId();
  468. Responsibility responsibility = responsibilityService.getById(responsibilityId);
  469. Long shareId = responsibility.getShareId();
  470. CostShareLevel costShareLevel = costShareLevelService.getById(shareId);
  471. Integer calcType = costShareLevel.getCalcType();
  472. // 分摊参数设置对饮的责任中心的id集合
  473. // List<Long> responsibilityIds = this.list(new QueryWrapper<CostAccountShare>().lambda().eq(CostAccountShare::getHospId, hospId)).stream().map(CostAccountShare::getResponsibilityId).distinct().collect(Collectors.toList());
  474. // 执行类型的分摊级别的Id的集合
  475. List<Long> costShareIds = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
  476. .eq(CostShareLevel::getHospId, hospId).eq(CostShareLevel::getCalcType, calcType))
  477. .stream().map(CostShareLevel::getId).distinct().collect(Collectors.toList());
  478. String accountingIds = costAccountShare.getAccountingIds();
  479. // List<Long> responsibilityIdList=null;
  480. // if (StrUtil.isNotBlank(accountingIds)){
  481. // List<String> accountIdList = Arrays.asList(accountingIds.split(StrUtil.COMMA));
  482. // responsibilityIdList= baseMapper.getResponsibilityIdsByAccountId(accountIdList);
  483. // }
  484. // 筛选指定类型的责任中心 并且当前没有设置的责任中心
  485. // List<Long> finalResponsibilityIdList = responsibilityIdList;
  486. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  487. .eq(Responsibility::getHospId, hospId)
  488. .ne(Responsibility::getShareId, NumberConstant.ZERO))
  489. .stream().filter(i -> costShareIds.contains(i.getShareId()) && !responsibilityId.equals(i.getId())).collect(Collectors.toList());
  490. // && (CollUtil.isNotEmpty(finalResponsibilityIdList) && !finalResponsibilityIdList.contains(i.getId()))
  491. return responsibilityList;
  492. }
  493. /**
  494. * 通过责任中心得到对应的成本分摊参数对应
  495. *
  496. * @param responsibilityCode 责任中心code
  497. * @param hospId 医院id
  498. * @return 成本分摊参数对应列表
  499. */
  500. @Override
  501. public List<CostAccountShare> getByResponsibility(String responsibilityCode, Long hospId) {
  502. List<CostAccountShare> list = this.list(
  503. new LambdaQueryWrapper<CostAccountShare>()
  504. .eq(CostAccountShare::getResponsibilityCode, responsibilityCode)
  505. .eq(CostAccountShare::getHospId, hospId)
  506. );
  507. return list;
  508. }
  509. @Override
  510. public List<CostAccountShare> getByShareLevelSort(List<Integer> levelSorts, Long hospId) {
  511. return this.list(
  512. new LambdaQueryWrapper<CostAccountShare>()
  513. .eq(CostAccountShare::getHospId, hospId)
  514. .in(CostAccountShare::getShareLevel, levelSorts)
  515. );
  516. }
  517. /**
  518. * 删除成本分摊参数对应数据
  519. *
  520. * @param idList id集合
  521. */
  522. @Override
  523. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  524. public void deleteByIds(List<Long> idList) {
  525. this.removeByIds(idList);
  526. costAccountShareDetailRepository.delCostAccountShareDetailList(idList);
  527. costAccountShareParamRepository.delCostAccountShareParamList(idList);
  528. }
  529. @Override
  530. public List<CostAccountShare> getAll() {
  531. return this.list(
  532. new LambdaQueryWrapper<CostAccountShare>()
  533. .eq(CostAccountShare::getHospId, UserContext.getHospId())
  534. );
  535. }
  536. /**
  537. *
  538. * @param direction 1主表向明细表同步 2明细表向主表同步
  539. * @param accountShareId
  540. */
  541. @Override
  542. @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
  543. public void synCostAccountShare(Integer direction, Long accountShareId) {
  544. if(NumberConstant.ONE.equals(direction)){
  545. synMainCostAccountShare(direction,accountShareId);
  546. }
  547. }
  548. /**
  549. * 主表向明细表同步
  550. * @param direction
  551. * @param accountShareId
  552. */
  553. public void synMainCostAccountShare(Integer direction, Long accountShareId) {
  554. List<CostAccountShare> costAccountShareList = getCostAccountShareList(accountShareId);
  555. if (CollectionUtils.isEmpty(costAccountShareList)) {
  556. return;
  557. }
  558. synMainCostAccountShare(costAccountShareList);
  559. }
  560. /**
  561. * 主表向明细表同步
  562. * @param costAccountShareList
  563. */
  564. public void synMainCostAccountShare(List<CostAccountShare> costAccountShareList){
  565. //生成分摊设置对应的会计科目对象
  566. List<CostAccountShareDetail> fullCostAccountShareDetailList=new ArrayList<>();
  567. //生成分摊设置对应的分摊参数对象
  568. List<CostAccountShareParam> fullCostAccountShareParamList=new ArrayList<>();
  569. //按主表的数据逐个转成会计科目对象和分摊参数对象
  570. costAccountShareList.stream().forEach(costAccountShare->{
  571. //处理会计科目
  572. String accountingCodes = costAccountShare.getAccountingCodes();
  573. if(!StringUtils.isEmpty(accountingCodes)){
  574. List<String> accountingCodeList = Arrays.stream(accountingCodes.split(StrUtil.COMMA)).collect(Collectors.toList());
  575. List<CostAccountShareDetail> costAccountShareDetailList = accountingCodeList.stream().map(accountingCode ->
  576. createCostAccountShareDetail(costAccountShare.getId(),accountingCode)).collect(Collectors.toList());
  577. fullCostAccountShareDetailList.addAll(costAccountShareDetailList);
  578. }
  579. //处理分摊参数
  580. String paramList = costAccountShare.getParamList();
  581. if(!StringUtils.isEmpty(paramList)) {
  582. List<ShareParamProportionVO> shareParamProportionVOS = JacksonUtil.str2ObjList(paramList, new TypeReference<List<ShareParamProportionVO>>() {
  583. });
  584. List<CostAccountShareParam> costAccountShareParamList = shareParamProportionVOS.stream().map(shareParamProportion ->
  585. createCostAccountShareParam(costAccountShare.getId(), shareParamProportion.getShareParamCode(), shareParamProportion.getShareParamPopout())).
  586. collect(Collectors.toList());
  587. fullCostAccountShareParamList.addAll(costAccountShareParamList);
  588. }
  589. });
  590. //作废原来的数据
  591. List<Long> accountShareIdList = costAccountShareList.stream().map(CostAccountShare::getId).collect(Collectors.toList());
  592. costAccountShareDetailRepository.delCostAccountShareDetailList(accountShareIdList);
  593. costAccountShareParamRepository.delCostAccountShareParamList(accountShareIdList);
  594. //保存会计科目对象
  595. if (!CollectionUtils.isEmpty(fullCostAccountShareDetailList)) {
  596. costAccountShareDetailRepository.saveBatch(fullCostAccountShareDetailList,NumberConstant.ONE_HUNDRED);
  597. }
  598. //保存分摊参数对象
  599. if (!CollectionUtils.isEmpty(fullCostAccountShareParamList)) {
  600. costAccountShareParamRepository.saveBatch(fullCostAccountShareParamList,NumberConstant.ONE_HUNDRED);
  601. }
  602. }
  603. /**
  604. * 创建分摊设置对应的会计科目对象
  605. * @param accountShareId
  606. * @param accountingCode
  607. * @return
  608. */
  609. public CostAccountShareDetail createCostAccountShareDetail(Long accountShareId,String accountingCode){
  610. CostAccountShareDetail costAccountShareDetail = new CostAccountShareDetail();
  611. costAccountShareDetail.setHospId(UserContext.getHospId());
  612. costAccountShareDetail.setAccountShareId(accountShareId);
  613. costAccountShareDetail.setAccountingCode(accountingCode);
  614. costAccountShareDetail.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  615. costAccountShareDetail.setCreateTime(new Date());
  616. return costAccountShareDetail;
  617. }
  618. /**
  619. * 创建分摊设置对应的分摊参数对象
  620. * @param accountShareId
  621. * @param shareParamCode
  622. * @param shareParamProportion
  623. * @return
  624. */
  625. public CostAccountShareParam createCostAccountShareParam(Long accountShareId,String shareParamCode,BigDecimal shareParamProportion) {
  626. CostAccountShareParam costAccountShareParam = new CostAccountShareParam();
  627. costAccountShareParam.setHospId(UserContext.getHospId());
  628. costAccountShareParam.setAccountShareId(accountShareId);
  629. costAccountShareParam.setShareParamCode(shareParamCode);
  630. costAccountShareParam.setShareParamProportion(shareParamProportion);
  631. costAccountShareParam.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  632. costAccountShareParam.setCreateTime(new Date());
  633. return costAccountShareParam;
  634. }
  635. /**
  636. * 获取所有的分摊参数配置
  637. * @return
  638. */
  639. public List<CostAccountShare> getCostAccountShareList(Long accountShareId){
  640. LambdaQueryWrapper<CostAccountShare> queryWrapper = new LambdaQueryWrapper<>();
  641. queryWrapper.eq(CostAccountShare::getHospId, UserContext.getHospId());
  642. queryWrapper.eq(CostAccountShare::getDeleteTime, NumberConstant.ZERO);
  643. if(!ObjectUtils.isEmpty(accountShareId)){
  644. queryWrapper.eq(CostAccountShare::getId, accountShareId);
  645. }
  646. return baseMapper.selectList(queryWrapper);
  647. }
  648. /**
  649. * 复制目标责任中心设置
  650. * @param accountShareCopyDto
  651. * @param accountShareList
  652. */
  653. public void copyCostAccountShareTarget(AccountShareCopyDto accountShareCopyDto,List<CostAccountShare> accountShareList){
  654. //没有指定的复制目标
  655. if(CollectionUtils.isEmpty(accountShareList)){
  656. return;
  657. }
  658. List<CostAccountShareTarget> costAccountShareTargetList = costAccountShareTargetRepository.getCostAccountShareTarget(accountShareCopyDto.getId());
  659. //没有设置指定的分摊目标责任中心
  660. if(CollectionUtils.isEmpty(costAccountShareTargetList)){
  661. return;
  662. }
  663. //逐个分摊设置复制目标责任中心
  664. List<CostAccountShareTarget> copyCostAccountShareTargetList=new ArrayList<>();
  665. for (CostAccountShare costAccountShare : accountShareList) {
  666. List<CostAccountShareTarget> copyTargetList = costAccountShareTargetList.stream().map(target -> createCostAccountShareTarget(costAccountShare.getId(), target.getTargetResponsibilityCode())).collect(Collectors.toList());
  667. copyCostAccountShareTargetList.addAll(copyTargetList);
  668. }
  669. //保存复制的数据
  670. if(CollectionUtils.isEmpty(copyCostAccountShareTargetList)) {
  671. costAccountShareTargetRepository.saveBatch(copyCostAccountShareTargetList);
  672. }
  673. }
  674. @Override
  675. public void applyAccountShareTarget(ShareTargetMapVo shareTargetMapVo) {
  676. applyAccountShareTarget(shareTargetMapVo.getAccountShareId(),shareTargetMapVo.getTargetRespCodeList());
  677. }
  678. /**
  679. * 提交分摊设置对应的目标责任中心
  680. * @param accountShareId
  681. * @param respCodeList
  682. */
  683. public void applyAccountShareTarget(Long accountShareId , List<String> respCodeList){
  684. //先作废上次的数据
  685. costAccountShareTargetRepository.delCostAccountShareTarget(accountShareId);
  686. //没有新的目标责任中心时不用处理
  687. if(CollectionUtils.isEmpty(respCodeList)){
  688. return;
  689. }
  690. //保存新的目标责任中心
  691. List<CostAccountShareTarget> costAccountShareTargetList = respCodeList.stream().map(respCode ->
  692. createCostAccountShareTarget(accountShareId, respCode)).collect(Collectors.toList());
  693. costAccountShareTargetRepository.saveBatch(costAccountShareTargetList);
  694. }
  695. /**
  696. * 生成分摊设置对应的目标责任中心对象
  697. * @param accountShareId
  698. * @param targetResponsibilityCode
  699. * @return
  700. */
  701. public CostAccountShareTarget createCostAccountShareTarget(Long accountShareId,String targetResponsibilityCode) {
  702. CostAccountShareTarget costAccountShareTarget = new CostAccountShareTarget();
  703. costAccountShareTarget.setHospId(UserContext.getHospId());
  704. costAccountShareTarget.setAccountShareId(accountShareId);
  705. costAccountShareTarget.setTargetResponsibilityCode(targetResponsibilityCode);
  706. costAccountShareTarget.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  707. costAccountShareTarget.setCreateTime(new Date());
  708. return costAccountShareTarget;
  709. }
  710. }