IncomeCollectionServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.json.JSONUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.imed.costaccount.common.exception.CostException;
  8. import com.imed.costaccount.common.util.PageUtils;
  9. import com.imed.costaccount.common.util.RedisLock;
  10. import com.imed.costaccount.mapper.CostIncomeGroupMapper;
  11. import com.imed.costaccount.mapper.IncomeCollectionMapper;
  12. import com.imed.costaccount.model.*;
  13. import com.imed.costaccount.model.dto.CollectDTO;
  14. import com.imed.costaccount.model.vo.CodeAndNameVO;
  15. import com.imed.costaccount.model.vo.CollectDataFormVO;
  16. import com.imed.costaccount.model.vo.CollectedVO;
  17. import com.imed.costaccount.model.vo.CollectionVO;
  18. import com.imed.costaccount.service.AccountingService;
  19. import com.imed.costaccount.service.IncomeCollectionService;
  20. import com.imed.costaccount.service.ResponsibilityService;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Propagation;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import java.math.BigDecimal;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. import static com.imed.costaccount.common.constants.Constant.LIMIT;
  29. @Slf4j
  30. @Service("incomeCollectionService")
  31. public class IncomeCollectionServiceImpl
  32. extends ServiceImpl<IncomeCollectionMapper, IncomeCollection>
  33. implements IncomeCollectionService {
  34. private final RedisLock redisLock;
  35. private final CostIncomeGroupMapper incomeGroupMapper;
  36. private final ResponsibilityService responsibilityService;
  37. private final AccountingService accountingService;
  38. public IncomeCollectionServiceImpl(RedisLock redisLock, CostIncomeGroupMapper incomeGroupMapper, ResponsibilityService responsibilityService, AccountingService accountingService) {
  39. this.redisLock = redisLock;
  40. this.incomeGroupMapper = incomeGroupMapper;
  41. this.responsibilityService = responsibilityService;
  42. this.accountingService = accountingService;
  43. }
  44. /**
  45. * 获取收入归集分页列表
  46. *
  47. * @param current 当前页
  48. * @param pageSize 页码数据大小
  49. * @param date 日期 yyyyMM
  50. * @param hospId 医院id
  51. * @return {@link PageUtils} 分页对象
  52. */
  53. @Override
  54. public PageUtils getCollections(Integer current, Integer pageSize, String date, Long hospId) {
  55. current = current - 1;
  56. Integer startIndex = current * pageSize;
  57. List<CollectionVO> list = incomeGroupMapper.getCollections(startIndex, pageSize, date, hospId);
  58. int count = incomeGroupMapper.getCollectionCount(date, hospId);
  59. // 设置是否归集字段
  60. list.forEach(i -> {
  61. i.setIsCollection(true);
  62. IncomeCollection one = this.getOne(
  63. new LambdaQueryWrapper<IncomeCollection>().select(IncomeCollection::getId)
  64. .eq(IncomeCollection::getYear, i.getYear())
  65. .eq(IncomeCollection::getMonth, i.getMonth())
  66. .last(LIMIT)
  67. );
  68. if (Objects.isNull(one)) {
  69. i.setIsCollection(false);
  70. }
  71. });
  72. return new PageUtils(list, count, pageSize, current + 1);
  73. }
  74. /**
  75. * 按年月归集数据
  76. *
  77. * @param year 年 数字类型
  78. * @param month 月 数字
  79. * @param hospId 医院id
  80. */
  81. @Override
  82. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  83. public void collect(Integer year, Integer month, Long hospId) {
  84. // todo 可能几十万次数据 需要分段异步多线程处理,
  85. // 1个小时
  86. String key = "collect" + hospId;
  87. String timestamp = DateUtil.offsetMillisecond(DateUtil.date(), 60 * 60).getTime() + "";
  88. boolean lock = redisLock.lock(key, timestamp);
  89. try {
  90. if (lock) {
  91. List<CostIncomeGroup> costIncomeGroups = incomeGroupMapper.selectList(
  92. new LambdaQueryWrapper<CostIncomeGroup>()
  93. .eq(CostIncomeGroup::getDateYear, year)
  94. .eq(CostIncomeGroup::getDateMonth, month)
  95. .eq(CostIncomeGroup::getHospId, hospId)
  96. );
  97. if (costIncomeGroups.isEmpty()) {
  98. String format = StrUtil.format("{}年{}月数据不存在,请先导入数据", year, month);
  99. throw new CostException(format);
  100. }
  101. // 删掉已归集的数据
  102. this.remove(
  103. new LambdaQueryWrapper<IncomeCollection>().eq(IncomeCollection::getYear, year).eq(IncomeCollection::getMonth, month).eq(IncomeCollection::getHospId, hospId)
  104. );
  105. // 根据会计科目和成本项目归纳所有数据
  106. Map<String, List<CostIncomeGroup>> collectMap = costIncomeGroups.stream()
  107. .collect(Collectors.groupingBy(i -> i.getAccountCode() + "cost" + i.getProductCode()));
  108. // 遍历map 组装成List保存
  109. List<IncomeCollection> list = new LinkedList<>();
  110. Set<Map.Entry<String, List<CostIncomeGroup>>> entries = collectMap.entrySet();
  111. entries.stream().map(Map.Entry::getValue).flatMap(Collection::stream).forEach(costIncomeGroup -> {
  112. IncomeCollection incomeCollection = new IncomeCollection();
  113. incomeCollection.setYear(year);
  114. incomeCollection.setMonth(month);
  115. incomeCollection.setAccountingCode(costIncomeGroup.getAccountCode());
  116. incomeCollection.setAccountingName(costIncomeGroup.getAccountName());
  117. incomeCollection.setProductName(costIncomeGroup.getProductName());
  118. incomeCollection.setProductCode(costIncomeGroup.getProductCode());
  119. incomeCollection.setAmount(costIncomeGroup.getAmount());
  120. incomeCollection.setHospId(hospId);
  121. // todo 来源id暂时不确定
  122. incomeCollection.setFileId(costIncomeGroup.getFileId());
  123. incomeCollection.setCreateTime(System.currentTimeMillis());
  124. String afterIncomeGroupStr = costIncomeGroup.getAfterIncomeGroup();
  125. AfterIncomegroup afterIncomegroup = JSONUtil.toBean(afterIncomeGroupStr, AfterIncomegroup.class);
  126. if (Objects.isNull(afterIncomegroup)) {
  127. throw new CostException("未能正确归集对应的....");
  128. }
  129. // 转换一下其他的
  130. if (afterIncomegroup.getDirectStatus() == 2) {
  131. afterIncomegroup.setDirectStatus(0);
  132. }
  133. incomeCollection.setIsDirectIncome(afterIncomegroup.getDirectStatus());
  134. String responsibilityCode = afterIncomegroup.getResponsibilityCode();
  135. if (StrUtil.isBlank(responsibilityCode)) {
  136. responsibilityCode = "-";
  137. afterIncomegroup.setResponsibilityName("-");
  138. }
  139. // 开单中心的数据
  140. incomeCollection.setDepartmentCode(afterIncomegroup.getOpenDepartmentCode());
  141. incomeCollection.setDepartmentName(afterIncomegroup.getOpenDepartmentName());
  142. incomeCollection.setResponsibilityCode(responsibilityCode);
  143. incomeCollection.setResponsibilityName(afterIncomegroup.getResponsibilityName());
  144. list.add(incomeCollection);
  145. // 执行科室数据
  146. String startDepartmentCode = afterIncomegroup.getStartDepartmentCode();
  147. incomeCollection.setDepartmentCode(startDepartmentCode);
  148. incomeCollection.setDepartmentName(afterIncomegroup.getStartDepartmentName());
  149. incomeCollection.setResponsibilityCode(responsibilityCode);
  150. incomeCollection.setResponsibilityName(afterIncomegroup.getResponsibilityName());
  151. list.add(incomeCollection);
  152. });
  153. // TODO: 2021/8/10 几十万条数据如何处理 待测试处理
  154. this.saveBatch(list);
  155. } else {
  156. throw new CostException("已有人在操作,请稍后重试");
  157. }
  158. } finally {
  159. redisLock.release(key, timestamp);
  160. }
  161. }
  162. /**
  163. * 按年月撤销归集
  164. *
  165. * @param year 年 数字类型
  166. * @param month 月 数字
  167. * @param hospId 医院id
  168. */
  169. @Override
  170. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  171. public void cancelCollect(Integer year, Integer month, Long hospId) {
  172. LambdaQueryWrapper<IncomeCollection> wrapper = new LambdaQueryWrapper<IncomeCollection>()
  173. .eq(IncomeCollection::getYear, year)
  174. .eq(IncomeCollection::getMonth, month)
  175. .eq(IncomeCollection::getHospId, hospId);
  176. this.remove(wrapper);
  177. }
  178. /**
  179. * 归集后数据分页列表
  180. *
  181. * @param collectDTO {@link CollectDTO} 查询相关参数
  182. * @return {@link PageUtils}
  183. */
  184. @Override
  185. public PageUtils collectList(CollectDTO collectDTO) {
  186. // 分页数据初始化
  187. Integer current = collectDTO.getCurrent();
  188. current = (Objects.isNull(current) || current == 0) ? 0 : current - 1;
  189. Integer pageSize = collectDTO.getPageSize();
  190. pageSize = Objects.isNull(pageSize) ? 10 : pageSize;
  191. collectDTO.setCurrent(current * pageSize);
  192. collectDTO.setPageSize(pageSize);
  193. // 查询需要的数据 DB
  194. List<CollectedVO> list = baseMapper.getCollectList(collectDTO);
  195. int count = baseMapper.getCollectListCount(collectDTO);
  196. BigDecimal totalAmount = baseMapper.getTotalAmount(collectDTO);
  197. return new PageUtils(list, count, pageSize, current + 1, totalAmount);
  198. }
  199. /**
  200. * 数据报表
  201. *
  202. * @param collectDTO
  203. * @return
  204. */
  205. @Override
  206. public CollectDataFormVO collectDataForm(CollectDTO collectDTO) {
  207. Long hospId = collectDTO.getHospId();
  208. String date = collectDTO.getDate();
  209. // 1. 得到所有的标题栏 责任中心代码
  210. List<CodeAndNameVO> responsibilities = this.baseMapper.getResponsibility(hospId, collectDTO.getResponsibilityCode(), date).stream().distinct().collect(Collectors.toList());
  211. if (responsibilities.isEmpty()) {
  212. return new CollectDataFormVO();
  213. }
  214. List<String> responsibilityCodes = responsibilities.stream().map(CodeAndNameVO::getCode).collect(Collectors.toList());
  215. responsibilities.add(0, new CodeAndNameVO("#", "#"));
  216. responsibilities.add(responsibilities.size(), new CodeAndNameVO("合计", "合计"));
  217. List<String> titleData = responsibilities.stream().map(CodeAndNameVO::getName).collect(Collectors.toList());
  218. Map<Integer, String> titleMap = new HashMap<>();
  219. for (int i = 0; i < titleData.size(); i++) {
  220. titleMap.put(i + 1, titleData.get(i));
  221. }
  222. // 所有的数据格式
  223. List<CodeAndNameVO> realData = this.baseMapper.getAccount(hospId, collectDTO.getAccountingCode(), date);
  224. if (realData.isEmpty()) {
  225. return new CollectDataFormVO();
  226. }
  227. // realData [#,123,123,"合计"]
  228. List<Map<Integer, Object>> realDatas = new ArrayList<>();
  229. List<CodeAndNameVO> accounts = realData.stream().distinct().collect(Collectors.toList());
  230. for (CodeAndNameVO account : accounts) {
  231. Map<Integer, Object> map = new HashMap<>();
  232. for (int i = 0; i < responsibilities.size(); i++) {
  233. if (i == 0) {
  234. map.put(i + 1, account.getName());
  235. continue;
  236. } else if (i == responsibilities.size() - 1) {
  237. // TODO: 2021/8/12 合计
  238. BigDecimal amount = this.baseMapper.getCountAccountAndResponsibilities(hospId, date, account.getCode(), responsibilityCodes);
  239. map.put(i + 1, amount);
  240. continue;
  241. }
  242. List<IncomeCollection> list = this.list(
  243. new LambdaQueryWrapper<IncomeCollection>().select(IncomeCollection::getAmount)
  244. .eq(IncomeCollection::getHospId, hospId)
  245. .eq(IncomeCollection::getYear, date.substring(0, 4))
  246. .eq(IncomeCollection::getMonth, date.substring(4))
  247. .eq(IncomeCollection::getAccountingCode, account.getCode())
  248. .eq(IncomeCollection::getResponsibilityCode, responsibilities.get(i).getCode())
  249. );
  250. if (list.isEmpty()) {
  251. map.put(i + 1, new BigDecimal("0.0000"));
  252. } else {
  253. BigDecimal reduce = list.stream().map(IncomeCollection::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  254. map.put(i + 1, reduce);
  255. }
  256. }
  257. realDatas.add(map);
  258. }
  259. //
  260. // 合计栏计算
  261. // 长度一致的
  262. List<String> accountingCodes = realData.stream().map(CodeAndNameVO::getCode).collect(Collectors.toList());
  263. Map<Integer, Object> map = new HashMap<>();
  264. for (int i = 0; i < responsibilities.size(); i++) {
  265. if (i == 0) {
  266. map.put(i + 1, "合计");
  267. continue;
  268. } else if (i == responsibilities.size() - 1) {
  269. // 统计问题
  270. BigDecimal bigDecimal = this.baseMapper.getCountByResponsibilitiesAndAccounts(responsibilityCodes, accountingCodes, hospId, date);
  271. map.put(i + 1, bigDecimal);
  272. continue;
  273. }
  274. BigDecimal bigDecimal = this.baseMapper.getCountByResponseAndAccounts(responsibilities.get(i).getCode(), accountingCodes, hospId, date);
  275. map.put(i + 1, bigDecimal);
  276. }
  277. return new CollectDataFormVO(titleMap, realDatas, map);
  278. }
  279. }