ShareParamValueServiceImpl.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.druid.util.StringUtils;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.imed.costaccount.common.exception.CostException;
  11. import com.imed.costaccount.common.util.*;
  12. import com.imed.costaccount.constants.NumberConstant;
  13. import com.imed.costaccount.common.enums.DateStyleEnum;
  14. import com.imed.costaccount.mapper.ShareParamValueMapper;
  15. import com.imed.costaccount.model.*;
  16. import com.imed.costaccount.model.dto.CopyShareParamValueDTO;
  17. import com.imed.costaccount.model.dto.ShareParamValueEditDTO;
  18. import com.imed.costaccount.model.dto.ShareParamValueSaveDTO;
  19. import com.imed.costaccount.model.dto.ShareParamValueVO;
  20. import com.imed.costaccount.model.vo.IncomeErrorMessage;
  21. import com.imed.costaccount.service.*;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Propagation;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.util.CollectionUtils;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import java.math.BigDecimal;
  29. import java.util.*;
  30. import java.util.concurrent.ConcurrentHashMap;
  31. import java.util.stream.Collectors;
  32. import static cn.hutool.core.date.DatePattern.PURE_DATE_PATTERN;
  33. import static com.imed.costaccount.common.constants.Constant.LIMIT;
  34. @Slf4j
  35. @Service("shareParamValueService")
  36. public class ShareParamValueServiceImpl extends ServiceImpl<ShareParamValueMapper, ShareParamValue> implements ShareParamValueService {
  37. private final CostIncomeGroupServiceImpl costIncomeGroupService;
  38. private final CostShareParamService costShareParamService;
  39. private final CostIncomeFileService costIncomeFileService;
  40. private final ResponsibilityService responsibilityService;
  41. private final IncomeCollectionService incomeCollectionService;
  42. public ShareParamValueServiceImpl(CostIncomeGroupServiceImpl costIncomeGroupService,
  43. CostShareParamService costShareParamService,
  44. CostIncomeFileService costIncomeFileService,
  45. ResponsibilityService responsibilityService, IncomeCollectionService incomeCollectionService) {
  46. this.costIncomeGroupService = costIncomeGroupService;
  47. this.costShareParamService = costShareParamService;
  48. this.costIncomeFileService = costIncomeFileService;
  49. this.responsibilityService = responsibilityService;
  50. this.incomeCollectionService = incomeCollectionService;
  51. }
  52. /**
  53. * 添加分摊参数值
  54. *
  55. * @param shareParamValueSaveDTO {@link ShareParamValueSaveDTO}
  56. * @param hospId 医院id
  57. */
  58. @Override
  59. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  60. public void addData(ShareParamValueSaveDTO shareParamValueSaveDTO, Long hospId) {
  61. ShareParamValue paramValue = BeanUtil.convertObj(shareParamValueSaveDTO, ShareParamValue.class);
  62. DateTime dateTime = DateUtil.parse(shareParamValueSaveDTO.getDate(), PURE_DATE_PATTERN);
  63. // 校验是否唯一
  64. Boolean flag = checkUniq(shareParamValueSaveDTO.getShareParamCode(), shareParamValueSaveDTO.getResponsibilityCode(), hospId, dateTime);
  65. if (flag) {
  66. throw new CostException("本月已存在对应责任中心和分摊参数中心,请直接修改");
  67. }
  68. // 校验两个code是否存在
  69. checkCodeIsExist(shareParamValueSaveDTO.getShareParamCode(), shareParamValueSaveDTO.getResponsibilityCode(), hospId);
  70. paramValue.setCreateTime(System.currentTimeMillis())
  71. .setDateYear(DateUtil.year(dateTime))
  72. .setDateMonth(DateUtil.month(dateTime) + 1)
  73. .setHospId(hospId)
  74. .setDataSourceType(0);
  75. this.save(paramValue);
  76. }
  77. /**
  78. * 校验两个code是否存在
  79. *
  80. * @param shareParamCode 成本分摊参数code
  81. * @param responsibilityCode 责任中心代码
  82. * @param hospId 医院id
  83. */
  84. @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
  85. public void checkCodeIsExist(String shareParamCode, String responsibilityCode, Long hospId) {
  86. List<CostShareParam> shareParams = costShareParamService.list(new LambdaQueryWrapper<CostShareParam>().select(CostShareParam::getId)
  87. .eq(CostShareParam::getShareParamCode, shareParamCode).eq(CostShareParam::getHospId, hospId));
  88. if (shareParams.isEmpty()) {
  89. throw new CostException("分摊参数code非法");
  90. }
  91. List<Responsibility> responsibilities = responsibilityService.list(
  92. new LambdaQueryWrapper<Responsibility>().select(Responsibility::getId)
  93. .eq(Responsibility::getResponsibilityCode, responsibilityCode).eq(Responsibility::getHospId, hospId)
  94. );
  95. if (responsibilities.isEmpty()) {
  96. throw new CostException("责任中心code非法");
  97. }
  98. }
  99. /**
  100. * 校验是否已存在
  101. *
  102. * @param shareParamCode 分摊参数代码
  103. * @param responsibilityCode 责任中心代码
  104. * @param hospId 医院id
  105. * @param date 时间
  106. */
  107. private Boolean checkUniq(String shareParamCode, String responsibilityCode, Long hospId, DateTime date) {
  108. List<ShareParamValue> list = this.list(
  109. new LambdaQueryWrapper<ShareParamValue>()
  110. .eq(ShareParamValue::getHospId, hospId)
  111. .eq(ShareParamValue::getShareParamCode, shareParamCode)
  112. .eq(ShareParamValue::getResponsibilityCode, responsibilityCode)
  113. .eq(ShareParamValue::getDateYear, DateUtil.year(date))
  114. .eq(ShareParamValue::getDateMonth, DateUtil.month(date) + 1)
  115. );
  116. return !list.isEmpty();
  117. }
  118. /**
  119. * 分摊参数值列表
  120. *
  121. * @param current 当前页
  122. * @param pageSize 每页数据量大小
  123. * @param date 日期
  124. * @param shareParamCode 分摊参数代码
  125. * @param responsibilityCode 责任中心代码
  126. * @param hospId 医院id
  127. * @return {@link PageUtils} 分页对象
  128. */
  129. @Override
  130. public PageUtils selectList(Integer current, Integer pageSize, String date, String shareParamCode, String responsibilityCode, Long hospId) {
  131. Integer startIndex = (current - 1) * pageSize;
  132. if (startIndex < 0) {
  133. startIndex = 0;
  134. }
  135. List<ShareParamValueVO> list = baseMapper.getValues(startIndex, pageSize, date, shareParamCode, responsibilityCode, hospId);
  136. int count = baseMapper.getValuesCount(startIndex, pageSize, date, shareParamCode, responsibilityCode, hospId);
  137. return new PageUtils(list, count, pageSize, current);
  138. }
  139. /**
  140. * 复制数据从某年月到某年月
  141. *
  142. * @param copyShareParamValueDTO {@link CopyShareParamValueDTO}
  143. * @param hospId 医院id
  144. */
  145. @Override
  146. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  147. public void copyData(CopyShareParamValueDTO copyShareParamValueDTO, Long hospId) {
  148. // TODO 复制数据 多个医院同时使用的情况下启用线程池处理
  149. String fromDate = copyShareParamValueDTO.getFromDate();
  150. String toDate = copyShareParamValueDTO.getToDate();
  151. Integer dataSourceType = copyShareParamValueDTO.getDataSourceType();
  152. LambdaQueryWrapper<ShareParamValue> wrapper = new LambdaQueryWrapper<ShareParamValue>()
  153. .eq(ShareParamValue::getDateYear, DateUtil.year(DateUtil.parse(fromDate, PURE_DATE_PATTERN)))
  154. .eq(ShareParamValue::getDateMonth, DateUtil.month(DateUtil.parse(fromDate, PURE_DATE_PATTERN)) + 1);
  155. if (dataSourceType == 0) {
  156. wrapper.eq(ShareParamValue::getDataSourceType, 0);
  157. } else if (dataSourceType == 1) {
  158. wrapper.eq(ShareParamValue::getDataSourceType, 1);
  159. } else {
  160. wrapper.in(ShareParamValue::getDataSourceType, CollUtil.newArrayList(0, 1));
  161. }
  162. // 拿到复制的数据
  163. List<ShareParamValue> list = this.list(wrapper);
  164. if (list.isEmpty()) {
  165. throw new CostException("复制日期" + fromDate + "数据不存在");
  166. }
  167. DateTime toDateTime = DateUtil.parse(toDate, PURE_DATE_PATTERN);
  168. int year = DateUtil.year(toDateTime);
  169. int month = DateUtil.month(toDateTime) + 1;
  170. // 删除之前的数据
  171. wrapper.clear();
  172. this.remove(
  173. wrapper.eq(ShareParamValue::getDateYear, year)
  174. .eq(ShareParamValue::getDateMonth, month)
  175. .eq(ShareParamValue::getHospId, hospId)
  176. );
  177. list.forEach(i -> {
  178. i.setId(null);
  179. i.setCreateTime(System.currentTimeMillis());
  180. i.setHospId(hospId);
  181. i.setDateYear(year);
  182. i.setDateMonth(month);
  183. });
  184. this.saveBatch(list);
  185. }
  186. /**
  187. * 编辑某条数据
  188. *
  189. * @param shareParamValueEditDTO {@link ShareParamValueEditDTO}
  190. * @param hospId 医院id
  191. */
  192. @Override
  193. @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
  194. public void editParamValue(ShareParamValueEditDTO shareParamValueEditDTO, Long hospId) {
  195. Long id = shareParamValueEditDTO.getId();
  196. this.removeById(id);
  197. DateTime dateTime = DateUtil.parse(shareParamValueEditDTO.getDate(), PURE_DATE_PATTERN);
  198. Boolean flag = checkUniq(shareParamValueEditDTO.getShareParamCode(), shareParamValueEditDTO.getResponsibilityCode(), hospId, dateTime);
  199. if (flag) {
  200. throw new CostException("本月已存在对应责任中心和分摊参数中心,请直接修改");
  201. }
  202. checkCodeIsExist(shareParamValueEditDTO.getShareParamCode(), shareParamValueEditDTO.getResponsibilityCode(), hospId);
  203. ShareParamValue shareParamValue = BeanUtil.convertObj(shareParamValueEditDTO, ShareParamValue.class);
  204. shareParamValue
  205. .setId(null)
  206. .setCreateTime(System.currentTimeMillis())
  207. .setDateYear(DateUtil.year(DateUtil.parse(shareParamValueEditDTO.getDate(), PURE_DATE_PATTERN)))
  208. .setDateMonth(DateUtil.month(DateUtil.parse(shareParamValueEditDTO.getDate(), PURE_DATE_PATTERN)) + 1)
  209. .setHospId(hospId)
  210. .setDataSourceType(0);
  211. this.save(shareParamValue);
  212. }
  213. /**
  214. * 成本分摊参数导入
  215. *
  216. * @param list
  217. * @param user
  218. * @param file
  219. * @param dateTime
  220. * @param fileType
  221. * @return
  222. */
  223. @Override
  224. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  225. public Result importShareParamGroup(List<List<Object>> list, User user, MultipartFile file, String dateTime, Integer fileType) {
  226. // 先检验当前年月是否存在数据
  227. int year = 0;
  228. int month = 0;
  229. Date date = DateUtils.StringToDate(dateTime, DateStyleEnum.YYYY_MM);
  230. if (StrUtil.isNotBlank(dateTime)) {
  231. year = DateUtil.year(date);
  232. month = DateUtil.month(date) + 1;
  233. }
  234. Long hospId = user.getHospId();
  235. List<ShareParamValue> groups = this.list(new QueryWrapper<ShareParamValue>().lambda().eq(ShareParamValue::getHospId, hospId)
  236. .eq(ShareParamValue::getDateYear, year).eq(ShareParamValue::getDateMonth, month));
  237. if (!CollectionUtils.isEmpty(groups)) {
  238. throw new CostException(500, year + "年" + month + "月数据已存在");
  239. }
  240. for (int i = list.size() - 1; i >= 0; i--) {
  241. if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
  242. list.remove(list.get(i));
  243. }
  244. }
  245. List<IncomeErrorMessage> incomeErrorMessageList = new ArrayList<>();
  246. Map<String, Department> departmentMap = costIncomeGroupService.getDepartmentByCodeNameMap(hospId);
  247. Map<Long, Responsibility> responsibilityMap = costIncomeGroupService.getResponsibilityIdResponsibilityMap(hospId);
  248. List<ResponsibilityDepartment> responsibilityDepartmentList = costIncomeGroupService.getResponsibilityDepartments(hospId);
  249. if (CollectionUtils.isEmpty(responsibilityDepartmentList)) {
  250. throw new CostException(500, "没有科室责任中心对照数据");
  251. }
  252. Map<Long, Long> responsibilityDepMap = costIncomeGroupService.getDepartmentIdResponsibilityIdMap(responsibilityDepartmentList);
  253. Map<String, CostShareParam> shareParamMap = costShareParamService.list(new QueryWrapper<CostShareParam>().lambda().eq(CostShareParam::getHospId, hospId)).stream().collect(Collectors.toMap(k -> k.getShareParamCode() + k.getShareParamName(), synOe -> synOe));
  254. // 要保存的数据
  255. List<ShareParamValue> shareParamValues = new LinkedList<>();
  256. // List<CostShareParamGroup> costShareParamGroupList = new LinkedList<>();
  257. List<Object> departmentCodes = list.get(0);
  258. List<Object> departmentNames = list.get(1);
  259. //检验数据是否准确 list包含了科室Code行与Name行 所以要从list的第三行开始
  260. checkShareParamGroupData(list, year, month, incomeErrorMessageList, departmentMap, responsibilityMap, responsibilityDepMap, shareParamMap, shareParamValues, departmentCodes, departmentNames);
  261. // 文件上传
  262. String uploadFile = costIncomeGroupService.uploadFile(file, UserContext.getCurrentUser());
  263. // 上传记录保存
  264. if (StrUtil.isBlank(uploadFile)) {
  265. throw new CostException(500, "文件上传异常");
  266. }
  267. // 记录文件上传记录
  268. CostIncomeFile costIncomeFile = costIncomeFileService.saveCostIncomeFile(list, user, file, hospId, incomeErrorMessageList, uploadFile, fileType, year, month);
  269. Long id = costIncomeFile.getId();
  270. shareParamValues.forEach(i -> {
  271. // 设置文件Id
  272. i.setFileId(id);
  273. i.setDataSourceType(1);
  274. });
  275. // 保存数据的唯一性
  276. Map<String, List<ShareParamValue>> map = shareParamValues.stream().collect(Collectors.groupingBy(k -> k.getShareParamCode().trim() + "code" + k.getResponsibilityCode().trim() + k.getDateYear().toString().trim() + "code" + k.getDateMonth().toString().trim()
  277. ));
  278. LinkedList<ShareParamValue> realList = new LinkedList<>();
  279. Set<String> strings = map.keySet();
  280. for (String str : strings) {
  281. List<ShareParamValue> shareParamValuesList = map.get(str);
  282. if (CollUtil.isNotEmpty(shareParamValuesList)) {
  283. BigDecimal reduce = shareParamValuesList.stream().map(ShareParamValue::getValueNum).reduce(BigDecimal.ZERO, BigDecimal::add);
  284. ShareParamValue shareParamValue = shareParamValuesList.get(0);
  285. shareParamValue.setValueNum(reduce);
  286. realList.add(shareParamValue);
  287. }
  288. }
  289. if (CollectionUtils.isEmpty(incomeErrorMessageList)) {
  290. this.saveBatch(realList);
  291. return Result.build(200, "数据导入成功", null);
  292. } else {
  293. return Result.build(200, "数据未成功导入", null);
  294. }
  295. }
  296. private void checkShareParamGroupData(List<List<Object>> list, int year, int month,
  297. List<IncomeErrorMessage> incomeErrorMessageList, Map<String, Department> departmentMap,
  298. Map<Long, Responsibility> responsibilityMap, Map<Long, Long> responsibilityDepMap,
  299. Map<String, CostShareParam> shareParamMap, List<ShareParamValue> shareParamValues,
  300. List<Object> departmentCodes, List<Object> departmentNames) {
  301. for (int i = 2; i < list.size(); i++) {
  302. int row = i + 4;
  303. List<Object> data = list.get(i);
  304. // 检验成本分摊参数是否正确
  305. ShareParamValue shareParamValueRequest = new ShareParamValue();
  306. String shareParamCode = data.get(0).toString().trim();
  307. String shareParamName = data.get(1).toString().trim();
  308. CostShareParam costShareParam = shareParamMap.get(shareParamCode + shareParamName);
  309. if (Objects.nonNull(costShareParam)) {
  310. shareParamValueRequest.setShareParamCode(shareParamCode);
  311. } else {
  312. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  313. incomeErrorMessage.setTotal(i);
  314. incomeErrorMessage.setErrMessage("成本分摊代码:" + shareParamCode + " 名称" + shareParamName + "不存在");
  315. incomeErrorMessageList.add(incomeErrorMessage);
  316. }
  317. for (int j = 2; j < departmentCodes.size(); j++) {
  318. int column = j + 1;
  319. if (data.size() > j) {
  320. if (Objects.isNull(data.get(j))) {
  321. data.set(j, NumberConstant.ZERO);
  322. } else if (data.get(j).toString().contains("-") || !StringUtils.isNumber(data.get(j).toString())) {
  323. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  324. incomeErrorMessage.setTotal(row);
  325. incomeErrorMessage.setErrMessage("第" + row + "行 第" + column + "列数据不符合规范");
  326. incomeErrorMessageList.add(incomeErrorMessage);
  327. } else {
  328. data.set(j, Double.parseDouble(data.get(j).toString()));
  329. }
  330. } else {
  331. data.add(NumberConstant.ZERO);
  332. }
  333. }
  334. if (CollectionUtils.isEmpty(incomeErrorMessageList)) {
  335. incomeErrorMessageList = new ArrayList<>();
  336. // 检验数据是否全为0
  337. int zeroStatus = 0;
  338. for (int j = 2; j < data.size(); j++) {
  339. if (!NumberConstant.ZERO.equals(data.get(j))) {
  340. zeroStatus = 1;
  341. break;
  342. }
  343. }
  344. if (NumberConstant.ONE.equals(zeroStatus)) {
  345. for (int j = 2; j < data.size(); j++) {
  346. ShareParamValue shareParamValue = BeanUtil.convertObj(shareParamValueRequest, ShareParamValue.class);
  347. // 检验科室信息是否准确
  348. String departmentCode = departmentCodes.get(j).toString();
  349. String departmentName = departmentNames.get(j).toString();
  350. Department department = departmentMap.get(departmentCode + departmentName);
  351. if (Objects.nonNull(department)) {
  352. // 检测责任中心是否存在
  353. Long id = department.getId();
  354. Long responsibilityId = responsibilityDepMap.get(id);
  355. if (Objects.nonNull(responsibilityId)) {
  356. Responsibility responsibility = responsibilityMap.get(responsibilityId);
  357. if (Objects.nonNull(responsibility)) {
  358. shareParamValue.setResponsibilityCode(responsibility.getResponsibilityCode());
  359. } else {
  360. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  361. incomeErrorMessage.setTotal(j);
  362. incomeErrorMessage.setErrMessage("第" + j + "列科室信息对应的责任中心不存在");
  363. incomeErrorMessageList.add(incomeErrorMessage);
  364. }
  365. } else {
  366. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  367. incomeErrorMessage.setTotal(j);
  368. incomeErrorMessage.setErrMessage("第" + j + "列科室信息不存在对应的责任中心");
  369. incomeErrorMessageList.add(incomeErrorMessage);
  370. }
  371. } else {
  372. IncomeErrorMessage incomeErrorMessage = new IncomeErrorMessage();
  373. incomeErrorMessage.setTotal(j);
  374. incomeErrorMessage.setErrMessage("第" + j + "列科室信息不存在");
  375. incomeErrorMessageList.add(incomeErrorMessage);
  376. }
  377. shareParamValue.setValueNum(BigDecimal.valueOf(Double.parseDouble(("0".equals(data.get(j).toString()) || StrUtil.isBlank(data.get(j).toString())) ? "0.00" : data.get(j).toString())));
  378. shareParamValue.setHospId(UserContext.getHospId());
  379. shareParamValue.setCreateTime(System.currentTimeMillis());
  380. shareParamValue.setDateYear(year);
  381. shareParamValue.setDateMonth(month);
  382. shareParamValues.add(shareParamValue);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. /**
  389. * 计算数值
  390. *
  391. * @param date 日期 yyyyMM
  392. * @param hospId 医院id
  393. */
  394. @Override
  395. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  396. public void calcData(String date, Long hospId) {
  397. DateTime parse = DateUtil.parse(date, PURE_DATE_PATTERN);
  398. int year = DateUtil.year(parse);
  399. int month = DateUtil.month(parse) + 1;
  400. // 拿到这些年月的数据
  401. List<ShareParamValue> list = this.list(
  402. new LambdaQueryWrapper<ShareParamValue>()
  403. .eq(ShareParamValue::getDateYear, year).eq(ShareParamValue::getDateMonth, month).eq(ShareParamValue::getHospId, hospId)
  404. );
  405. if (list.isEmpty()) {
  406. throw new CostException(500, parse + "数据未录入");
  407. }
  408. // 得到这些会计科目代码
  409. List<String> accountCodes = new ArrayList<>();
  410. list.forEach(i -> {
  411. String shareParamCode = i.getShareParamCode();
  412. CostShareParam one = costShareParamService.getOne(
  413. new LambdaQueryWrapper<CostShareParam>()
  414. .eq(CostShareParam::getHospId, hospId)
  415. .eq(CostShareParam::getShareParamCode, shareParamCode)
  416. .last(LIMIT)
  417. );
  418. if (Objects.isNull(one)) {
  419. return;
  420. }
  421. String accountingCodes = one.getAccountingCodes();
  422. if (StrUtil.isBlank(accountingCodes)) {
  423. return;
  424. }
  425. String[] split = StrUtil.split(accountingCodes, StrUtil.COMMA);
  426. ArrayList<String> accountCodeList = CollUtil.newArrayList(split);
  427. accountCodes.addAll(accountCodeList);
  428. List<String> calcList = new ArrayList<>();
  429. accountCodeList.forEach(j -> {
  430. calcList.add(i.getResponsibilityCode() + "cost" + j);
  431. });
  432. i.setCalcList(calcList);
  433. });
  434. log.info("list={}", accountCodes);
  435. List<IncomeCollection> incomeCollections = incomeCollectionService.list(
  436. new LambdaQueryWrapper<IncomeCollection>()
  437. .eq(IncomeCollection::getYear, year)
  438. .eq(IncomeCollection::getMonth, month)
  439. .eq(IncomeCollection::getHospId, hospId)
  440. .in(IncomeCollection::getAccountingCode, accountCodes.stream().distinct().collect(Collectors.toList()))
  441. );
  442. if (incomeCollections.isEmpty()) {
  443. List<IncomeCollection> collections = incomeCollectionService.list(
  444. new LambdaQueryWrapper<IncomeCollection>()
  445. .eq(IncomeCollection::getYear, year)
  446. .eq(IncomeCollection::getMonth, month)
  447. .eq(IncomeCollection::getHospId, hospId));
  448. if (collections.isEmpty()) {
  449. throw new CostException("本月未归集数据,请先归集数据");
  450. }
  451. return ;
  452. }
  453. Map<String, List<IncomeCollection>> collectMap = incomeCollections.stream()
  454. .collect(Collectors.groupingBy(i -> i.getResponsibilityCode() + "cost" + i.getAccountingCode()));
  455. Set<String> strings = collectMap.keySet();
  456. Map<String, BigDecimal> map = new ConcurrentHashMap<>();
  457. strings.forEach(str -> {
  458. List<IncomeCollection> collections = collectMap.get(str);
  459. if (CollUtil.isNotEmpty(collections)) {
  460. BigDecimal reduce = collections.stream().map(IncomeCollection::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  461. map.put(str, reduce);
  462. }
  463. });
  464. list.forEach(i -> {
  465. map.keySet().forEach(j -> {
  466. if (CollUtil.isNotEmpty(i.getCalcList()) && i.getCalcList().contains(j)) {
  467. i.setValueNum(map.get(j));
  468. }
  469. });
  470. });
  471. this.updateBatchById(list);
  472. }
  473. /**
  474. * 得到这个月导入的成本分摊参数值列表 聚合过
  475. *
  476. * @param year 年
  477. * @param month 月
  478. * @param hospId 医院id
  479. * @return 某月成本分摊参数值
  480. */
  481. @Override
  482. public List<ShareParamValue> getListByYearAndMonth(Integer year, Integer month, Long hospId) {
  483. return baseMapper.getListByYearAndMonth(year, month, hospId);
  484. }
  485. /**
  486. * 成本分摊参数值批量删除
  487. *
  488. * @param asList id集合
  489. */
  490. @Override
  491. @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
  492. public void deleteByIds(List<Long> asList) {
  493. this.removeByIds(asList);
  494. }
  495. }