AccountingItemMapServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package com.kcim.service.impl;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.kcim.common.constants.NumberConstant;
  4. import com.kcim.common.exception.CostException;
  5. import com.kcim.common.util.PageUtils;
  6. import com.kcim.common.util.UserContext;
  7. import com.kcim.dao.model.Accounting;
  8. import com.kcim.dao.model.AccountingItemMap;
  9. import com.kcim.dao.model.OrderItemMap;
  10. import com.kcim.dao.repository.AccountingItemMapRepository;
  11. import com.kcim.dao.repository.AccountingRepository;
  12. import com.kcim.service.AccountingItemMapService;
  13. import com.kcim.service.DrugService;
  14. import com.kcim.service.ItemService;
  15. import com.kcim.service.MaterialService;
  16. import com.kcim.web.request.AccountingItemMapRequest;
  17. import lombok.AllArgsConstructor;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Propagation;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.CollectionUtils;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. /**
  26. * @program: CostAccount
  27. * @description:
  28. * @author: Wang.YS
  29. * @create: 2023-11-27 17:52
  30. **/
  31. @Service("AccountingItemMapService")
  32. @Slf4j
  33. @AllArgsConstructor
  34. public class AccountingItemMapServiceImpl implements AccountingItemMapService {
  35. AccountingItemMapRepository repository;
  36. AccountingRepository accountingRepository;
  37. DrugService drugService;
  38. MaterialService materialService;
  39. ItemService itemService;
  40. /**
  41. * 获取会计科目收入列表
  42. * @param accountingName 会计科目名称
  43. * @param type 筛选类型 0全部 1已对照 2未对照
  44. * @return 列表
  45. */
  46. @Override
  47. public Object getAccountingList(String accountingName, Integer type) {
  48. List<Accounting> accountingList = accountingRepository.getAccountIncome(accountingName);
  49. List<Accounting> allIncome = accountingRepository.getAllIncome();
  50. if(CollectionUtils.isEmpty(accountingList)){
  51. return new ArrayList<>();
  52. }
  53. List<AccountingItemMap> itemMaps = repository.getList();
  54. if(CollectionUtils.isEmpty(itemMaps)){
  55. //无对照数据 返回会计科目全部
  56. List<Accounting> collect = accountingList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  57. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  58. List<Accounting> parentAccounting = new ArrayList<>();
  59. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  60. if(!CollectionUtils.isEmpty(parentAccounting)){
  61. accountingList.addAll(parentAccounting);
  62. }
  63. List<Accounting> accounting = accountingList.stream().distinct().collect(Collectors.toList());
  64. return accounting.stream().filter(o -> o.getParentId() == 0)
  65. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  66. .collect(Collectors.toList());
  67. }else {
  68. if(type.equals(NumberConstant.ONE)){
  69. //已对照
  70. List<String> accountCodes = itemMaps.stream().map(AccountingItemMap::getAccountingCode).distinct().collect(Collectors.toList());
  71. List<Accounting> returnList = new ArrayList<>();
  72. for (Accounting accountVO : accountingList) {
  73. if(accountCodes.contains(accountVO.getAccountingCode())){
  74. returnList.add(accountVO);
  75. }
  76. }
  77. if(!CollectionUtils.isEmpty(returnList)){
  78. //找出父类不是0的数据 递归找到父类组装成树
  79. List<Accounting> collect = returnList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  80. List<Accounting> parentAccounting = new ArrayList<>();
  81. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  82. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  83. if(!CollectionUtils.isEmpty(parentAccounting)){
  84. returnList.addAll(parentAccounting);
  85. }
  86. List<Accounting> accounting = returnList.stream().distinct().collect(Collectors.toList());
  87. return accounting.stream().filter(o -> o.getParentId() == 0)
  88. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  89. .collect(Collectors.toList());
  90. }
  91. } else if (type.equals(NumberConstant.TWO)) {
  92. //未对照
  93. List<String> accountCodes = itemMaps.stream().map(AccountingItemMap::getAccountingCode).distinct().collect(Collectors.toList());
  94. List<Accounting> returnList = new ArrayList<>();
  95. for (Accounting accountVO : accountingList) {
  96. if(!accountCodes.contains(accountVO.getAccountingCode())){
  97. returnList.add(accountVO);
  98. }
  99. }
  100. if(!CollectionUtils.isEmpty(returnList)){
  101. //找出父类不是0的数据 递归找到父类组装成树
  102. List<Accounting> collect = returnList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  103. List<Accounting> parentAccounting = new ArrayList<>();
  104. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  105. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  106. if(!CollectionUtils.isEmpty(parentAccounting)){
  107. returnList.addAll(parentAccounting);
  108. }
  109. List<Accounting> accounting = returnList.stream().distinct().collect(Collectors.toList());
  110. return accounting.stream().filter(o -> o.getParentId() == 0)
  111. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  112. .collect(Collectors.toList());
  113. }
  114. }else {
  115. //全部
  116. List<Accounting> collect = accountingList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  117. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  118. List<Accounting> parentAccounting = new ArrayList<>();
  119. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  120. if(!CollectionUtils.isEmpty(parentAccounting)){
  121. accountingList.addAll(parentAccounting);
  122. }
  123. List<Accounting> accounting = accountingList.stream().distinct().collect(Collectors.toList());
  124. return accounting.stream().filter(o -> o.getParentId() == 0)
  125. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  126. .collect(Collectors.toList());
  127. }
  128. }
  129. return new ArrayList<>();
  130. }
  131. /**
  132. * 递归查找父类代码
  133. * @param accounting 当前会计科目
  134. * @param accountingMap 所有会计科目
  135. * @param parentAccounting 父类会计科目
  136. */
  137. private void getParentAccount(Accounting accounting,Map<Long,Accounting> accountingMap,List<Accounting> parentAccounting ){
  138. Accounting parentAccount = accountingMap.get(accounting.getParentId());
  139. parentAccounting.add(parentAccount);
  140. if(!parentAccount.getParentId().equals(NumberConstant.ZERO_L)){
  141. getParentAccount(parentAccount, accountingMap, parentAccounting );
  142. }
  143. }
  144. private List<Accounting> getAccountTree(Accounting accountVO, List<Accounting> list) {
  145. return list.stream().filter(o -> o.getParentId().equals(accountVO.getId()))
  146. .peek(o -> o.setChildren(this.getAccountTree(o, list)))
  147. .collect(Collectors.toList());
  148. }
  149. /**
  150. * 获取收费项目字典
  151. * @param type 类型 1 药品 2 材料 3 项目
  152. * @param name 项目名称
  153. * @return 字典列表
  154. */
  155. @Override
  156. public Object getAccountingItemDictList(Integer type, String name) {
  157. List<OrderItemMap> orderItemMaps = new ArrayList<>();
  158. if(type.equals(NumberConstant.ONE)){
  159. //药品
  160. orderItemMaps = drugService.getItemDict(name);
  161. } else if (type.equals(NumberConstant.TWO)) {
  162. //材料
  163. orderItemMaps = materialService.getItemDict(name);
  164. } else {
  165. orderItemMaps = itemService.getItemDict(name);
  166. }
  167. //获取已对照的项目
  168. List<AccountingItemMap> itemMaps = repository.getList();
  169. if(!CollectionUtils.isEmpty(itemMaps)){
  170. List<String> list = itemMaps.stream().map(AccountingItemMap::getItemCode).collect(Collectors.toList());
  171. List<OrderItemMap> removeList = orderItemMaps.stream().filter(orderItemMap -> list.contains(orderItemMap.getItemCode())).collect(Collectors.toList());
  172. if(!CollectionUtils.isEmpty(removeList)){
  173. orderItemMaps.removeAll(removeList);
  174. }
  175. }
  176. return orderItemMaps;
  177. }
  178. /**
  179. * 获取会计科目
  180. * @param current 当前页
  181. * @param pageSize 页容量
  182. * @param accountingCode 会计科目代码
  183. * @param itemName 项目名称
  184. * @return 对照列表
  185. */
  186. @Override
  187. public Object getAccountingItemMap(Integer current, Integer pageSize, String accountingCode, String itemName) {
  188. Page<AccountingItemMap> page = repository.getPage(current, pageSize, accountingCode, itemName);
  189. List<AccountingItemMap> records = page.getRecords();
  190. if(CollectionUtils.isEmpty(records)){
  191. return new PageUtils(new ArrayList<>(),NumberConstant.ZERO,pageSize,current);
  192. }
  193. for (AccountingItemMap record : records) {
  194. //"药品_1","材料_2","项目_3"
  195. String itemType = record.getType();
  196. switch (itemType) {
  197. case "1":
  198. record.setTypeName("药品");
  199. break;
  200. case "2":
  201. record.setTypeName("材料");
  202. break;
  203. case "3":
  204. record.setTypeName("项目");
  205. break;
  206. default:
  207. record.setTypeName("未知");
  208. break;
  209. }
  210. }
  211. return new PageUtils(records, Math.toIntExact(page.getTotal()),pageSize,current);
  212. }
  213. /**
  214. * 添加会计科目项目对照
  215. * @param map 入参
  216. */
  217. @Override
  218. @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
  219. public void addAccountingItemMap(AccountingItemMapRequest map) {
  220. List<AccountingItemMap> itemMaps = map.getItemMaps();
  221. if(CollectionUtils.isEmpty(itemMaps)){
  222. throw new CostException("未找到需要保存的数据");
  223. }
  224. for (AccountingItemMap itemMap : itemMaps) {
  225. itemMap.setHospId(UserContext.getHospId());
  226. itemMap.setCreateTime(new Date());
  227. itemMap.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  228. itemMap.setAccountingCode(map.getAccountingCode());
  229. itemMap.setAccountingName(map.getAccountingName());
  230. }
  231. repository.saveBatch(itemMaps);
  232. }
  233. /**
  234. * 单个删除
  235. * @param id 主键
  236. */
  237. @Override
  238. @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
  239. public void deleteAccountingItemMap(Integer id) {
  240. repository.batchDelete(Collections.singletonList(id));
  241. }
  242. /**
  243. * 批量删除
  244. * @param ids 主键
  245. */
  246. @Override
  247. @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
  248. public void batchDeleteAccountingItemMap(List<Integer> ids) {
  249. repository.batchDelete(ids);
  250. }
  251. }