AccountingItemMapServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. for (Accounting accounting1 : collect) {
  60. getParentAccount(accounting1, accountingMap, parentAccounting);
  61. }
  62. if(!CollectionUtils.isEmpty(parentAccounting)){
  63. accountingList.addAll(parentAccounting);
  64. }
  65. List<Accounting> accounting = accountingList.stream().distinct().collect(Collectors.toList());
  66. return accounting.stream().filter(o -> o.getParentId() == 0)
  67. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  68. .collect(Collectors.toList());
  69. }else {
  70. if(type.equals(NumberConstant.ONE)){
  71. //已对照
  72. List<String> accountCodes = itemMaps.stream().map(AccountingItemMap::getAccountingCode).distinct().collect(Collectors.toList());
  73. List<Accounting> returnList = new ArrayList<>();
  74. for (Accounting accountVO : accountingList) {
  75. if(accountCodes.contains(accountVO.getAccountingCode())){
  76. returnList.add(accountVO);
  77. }
  78. }
  79. if(!CollectionUtils.isEmpty(returnList)){
  80. //找出父类不是0的数据 递归找到父类组装成树
  81. List<Accounting> collect = returnList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  82. List<Accounting> parentAccounting = new ArrayList<>();
  83. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  84. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  85. if(!CollectionUtils.isEmpty(parentAccounting)){
  86. returnList.addAll(parentAccounting);
  87. }
  88. List<Accounting> accounting = returnList.stream().distinct().collect(Collectors.toList());
  89. return accounting.stream().filter(o -> o.getParentId() == 0)
  90. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  91. .collect(Collectors.toList());
  92. }
  93. } else if (type.equals(NumberConstant.TWO)) {
  94. //未对照
  95. List<String> accountCodes = itemMaps.stream().map(AccountingItemMap::getAccountingCode).distinct().collect(Collectors.toList());
  96. List<Accounting> returnList = new ArrayList<>();
  97. for (Accounting accountVO : accountingList) {
  98. if(!accountCodes.contains(accountVO.getAccountingCode())){
  99. returnList.add(accountVO);
  100. }
  101. }
  102. if(!CollectionUtils.isEmpty(returnList)){
  103. //找出父类不是0的数据 递归找到父类组装成树
  104. List<Accounting> collect = returnList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  105. List<Accounting> parentAccounting = new ArrayList<>();
  106. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  107. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  108. if(!CollectionUtils.isEmpty(parentAccounting)){
  109. returnList.addAll(parentAccounting);
  110. }
  111. List<Accounting> accounting = returnList.stream().distinct().collect(Collectors.toList());
  112. return accounting.stream().filter(o -> o.getParentId() == 0)
  113. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  114. .collect(Collectors.toList());
  115. }
  116. }else {
  117. //全部
  118. List<Accounting> collect = accountingList.stream().filter(f -> !f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  119. Map<Long, Accounting> accountingMap = allIncome.stream().collect(Collectors.toMap(Accounting::getId, accounting -> accounting, (a, b) -> b));
  120. List<Accounting> parentAccounting = new ArrayList<>();
  121. collect.forEach(accounting -> getParentAccount(accounting, accountingMap, parentAccounting));
  122. if(!CollectionUtils.isEmpty(parentAccounting)){
  123. accountingList.addAll(parentAccounting);
  124. }
  125. List<Accounting> accounting = accountingList.stream().distinct().collect(Collectors.toList());
  126. return accounting.stream().filter(o -> o.getParentId() == 0)
  127. .peek(o -> o.setChildren(this.getAccountTree(o, accounting)))
  128. .collect(Collectors.toList());
  129. }
  130. }
  131. return new ArrayList<>();
  132. }
  133. /**
  134. * 递归查找父类代码
  135. * @param accounting 当前会计科目
  136. * @param accountingMap 所有会计科目
  137. * @param parentAccounting 父类会计科目
  138. */
  139. private void getParentAccount(Accounting accounting,Map<Long,Accounting> accountingMap,List<Accounting> parentAccounting ){
  140. Accounting parentAccount = accountingMap.get(accounting.getParentId());
  141. if(Objects.nonNull(parentAccount)){
  142. parentAccounting.add(parentAccount);
  143. if(!parentAccount.getParentId().equals(NumberConstant.ZERO_L)){
  144. getParentAccount(parentAccount, accountingMap, parentAccounting );
  145. }
  146. }
  147. }
  148. private List<Accounting> getAccountTree(Accounting accountVO, List<Accounting> list) {
  149. return list.stream().filter(o -> o.getParentId().equals(accountVO.getId()))
  150. .peek(o -> o.setChildren(this.getAccountTree(o, list)))
  151. .collect(Collectors.toList());
  152. }
  153. /**
  154. * 获取收费项目字典
  155. * @param type 类型 1 药品 2 材料 3 项目
  156. * @param name 项目名称
  157. * @return 字典列表
  158. */
  159. @Override
  160. public Object getAccountingItemDictList(Integer type, String name) {
  161. List<OrderItemMap> orderItemMaps = new ArrayList<>();
  162. if(type.equals(NumberConstant.ONE)){
  163. //药品
  164. orderItemMaps = drugService.getItemDict(name);
  165. } else if (type.equals(NumberConstant.TWO)) {
  166. //材料
  167. orderItemMaps = materialService.getItemDict(name);
  168. } else {
  169. orderItemMaps = itemService.getItemDict(name);
  170. }
  171. //获取已对照的项目
  172. List<AccountingItemMap> itemMaps = repository.getList();
  173. if(!CollectionUtils.isEmpty(itemMaps)){
  174. List<String> list = itemMaps.stream().map(AccountingItemMap::getItemCode).collect(Collectors.toList());
  175. List<OrderItemMap> removeList = orderItemMaps.stream().filter(orderItemMap -> list.contains(orderItemMap.getItemCode())).collect(Collectors.toList());
  176. if(!CollectionUtils.isEmpty(removeList)){
  177. orderItemMaps.removeAll(removeList);
  178. }
  179. }
  180. return orderItemMaps;
  181. }
  182. /**
  183. * 获取会计科目
  184. * @param current 当前页
  185. * @param pageSize 页容量
  186. * @param accountingCode 会计科目代码
  187. * @param itemName 项目名称
  188. * @return 对照列表
  189. */
  190. @Override
  191. public Object getAccountingItemMap(Integer current, Integer pageSize, String accountingCode, String itemName) {
  192. Page<AccountingItemMap> page = repository.getPage(current, pageSize, accountingCode, itemName);
  193. List<AccountingItemMap> records = page.getRecords();
  194. if(CollectionUtils.isEmpty(records)){
  195. return new PageUtils(new ArrayList<>(),NumberConstant.ZERO,pageSize,current);
  196. }
  197. for (AccountingItemMap record : records) {
  198. //"药品_1","材料_2","项目_3"
  199. String itemType = record.getType();
  200. switch (itemType) {
  201. case "1":
  202. record.setTypeName("药品");
  203. break;
  204. case "2":
  205. record.setTypeName("材料");
  206. break;
  207. case "3":
  208. record.setTypeName("项目");
  209. break;
  210. default:
  211. record.setTypeName("未知");
  212. break;
  213. }
  214. }
  215. return new PageUtils(records, Math.toIntExact(page.getTotal()),pageSize,current);
  216. }
  217. /**
  218. * 添加会计科目项目对照
  219. * @param map 入参
  220. */
  221. @Override
  222. @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
  223. public void addAccountingItemMap(AccountingItemMapRequest map) {
  224. List<AccountingItemMap> itemMaps = map.getItemMaps();
  225. if(CollectionUtils.isEmpty(itemMaps)){
  226. throw new CostException("未找到需要保存的数据");
  227. }
  228. for (AccountingItemMap itemMap : itemMaps) {
  229. itemMap.setHospId(UserContext.getHospId());
  230. itemMap.setCreateTime(new Date());
  231. itemMap.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
  232. itemMap.setAccountingCode(map.getAccountingCode());
  233. itemMap.setAccountingName(map.getAccountingName());
  234. }
  235. repository.saveBatch(itemMaps);
  236. }
  237. /**
  238. * 单个删除
  239. * @param id 主键
  240. */
  241. @Override
  242. @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
  243. public void deleteAccountingItemMap(Integer id) {
  244. repository.batchDelete(Collections.singletonList(id));
  245. }
  246. /**
  247. * 批量删除
  248. * @param ids 主键
  249. */
  250. @Override
  251. @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
  252. public void batchDeleteAccountingItemMap(List<Integer> ids) {
  253. repository.batchDelete(ids);
  254. }
  255. }