|
@@ -0,0 +1,135 @@
|
|
|
+package com.imed.costaccount.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.imed.costaccount.mapper.AccountingMapper;
|
|
|
+import com.imed.costaccount.mapper.AccountingProductMapper;
|
|
|
+import com.imed.costaccount.mapper.ProductMapper;
|
|
|
+import com.imed.costaccount.model.*;
|
|
|
+import com.imed.costaccount.model.dto.AccountProductSaveDTO;
|
|
|
+import com.imed.costaccount.model.dto.ProductAccountDTO;
|
|
|
+import com.imed.costaccount.model.vo.AccountProductVO;
|
|
|
+import com.imed.costaccount.model.vo.ProductVO;
|
|
|
+import com.imed.costaccount.service.AccountingProductService;
|
|
|
+import com.imed.costaccount.utils.BeanUtil;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+@Service("accountingProductService")
|
|
|
+public class AccountingProductServiceImpl extends ServiceImpl<AccountingProductMapper, AccountingProduct> implements AccountingProductService {
|
|
|
+
|
|
|
+ private final AccountingMapper accountingMapper;
|
|
|
+
|
|
|
+ private final ProductMapper productMapper;
|
|
|
+
|
|
|
+ public AccountingProductServiceImpl(AccountingMapper accountingMapper, ProductMapper productMapper) {
|
|
|
+ this.accountingMapper = accountingMapper;
|
|
|
+ this.productMapper = productMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表
|
|
|
+ *
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<AccountProductVO> selectList(User user) {
|
|
|
+ List<Accounting> list = accountingMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<Accounting>()
|
|
|
+ .eq(Accounting::getHospId, user.getHospId())
|
|
|
+ );
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<AccountProductVO> accountProductVOS = BeanUtil.convertList(list, AccountProductVO.class);
|
|
|
+ // 所有的
|
|
|
+ List<AccountProductVO> all = accountProductVOS.stream().map(i -> BeanUtil.convertObj(i, AccountProductVO.class)).collect(Collectors.toList());
|
|
|
+ // 顶层的
|
|
|
+ List<AccountProductVO> parents = all.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
|
|
|
+ List<AccountProductVO> accountVOS = new ArrayList<>();
|
|
|
+ for (AccountProductVO parent : parents) {
|
|
|
+ List<AccountProductVO> accountTree = this.getAccountTree(parent, all);
|
|
|
+ accountVOS.addAll(accountTree);
|
|
|
+ }
|
|
|
+ return accountVOS;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归处理
|
|
|
+ *
|
|
|
+ * @param accountVO
|
|
|
+ * @param list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<AccountProductVO> getAccountTree(AccountProductVO accountVO, List<AccountProductVO> list) {
|
|
|
+ List<AccountProductVO> accountVOS = new ArrayList<>();
|
|
|
+ for (AccountProductVO account : list) {
|
|
|
+ // 如果是父子关系
|
|
|
+ if (accountVO.getId().equals(account.getParentId())) {
|
|
|
+ List<AccountingProduct> accountingProducts = baseMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<AccountingProduct>().select(AccountingProduct::getId, AccountingProduct::getProductId)
|
|
|
+ .eq(AccountingProduct::getAccountingId, account.getId())
|
|
|
+ );
|
|
|
+ if (CollUtil.isNotEmpty(accountingProducts)) {
|
|
|
+ List<Integer> productIds = accountingProducts.stream().map(AccountingProduct::getProductId).collect(Collectors.toList());
|
|
|
+ List<Product> products = productMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<Product>().in(Product::getId, productIds)
|
|
|
+ );
|
|
|
+ if (CollUtil.isNotEmpty(products)) {
|
|
|
+ List<ProductVO> productVOS = BeanUtil.convertList(products, ProductVO.class);
|
|
|
+ account.setProductVOs(productVOS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<AccountProductVO> child = accountVO.getChild();
|
|
|
+ if (CollUtil.isEmpty(child)) {
|
|
|
+ child = new ArrayList<>();
|
|
|
+ }
|
|
|
+ child.add(account);
|
|
|
+ accountVO.setChild(child);
|
|
|
+ // 处理子节点
|
|
|
+ this.getAccountTree(account, list);
|
|
|
+
|
|
|
+ accountVOS.add(accountVO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return accountVOS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存或修改会计科目对照
|
|
|
+ *
|
|
|
+ * @param accountProductSaveDTO
|
|
|
+ * @param user
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
+ public void saveAccountProduct(AccountProductSaveDTO accountProductSaveDTO, User user) {
|
|
|
+ // 删除所有原来的关系
|
|
|
+ Integer accountId = accountProductSaveDTO.getId();
|
|
|
+ this.remove(new LambdaQueryWrapper<AccountingProduct>().eq(AccountingProduct::getAccountingId, accountId));
|
|
|
+
|
|
|
+ List<Integer> products = accountProductSaveDTO.getProducts();
|
|
|
+ List<AccountingProduct> accountingProducts = products.stream().map(i -> {
|
|
|
+ AccountingProduct accountingProduct = new AccountingProduct();
|
|
|
+ accountingProduct.setAccountingId(accountId)
|
|
|
+ .setProductId(i)
|
|
|
+ .setHospId(user.getHospId())
|
|
|
+ .setCreateTime(System.currentTimeMillis());
|
|
|
+ return accountingProduct;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ this.saveBatch(accountingProducts);
|
|
|
+ }
|
|
|
+}
|