ShareParamValueServiceImpl.java 28 KB

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