From 6999d4114eb6d64d0775e2f9ff00572b0e60ee31 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期五, 26 九月 2025 17:31:15 +0800
Subject: [PATCH] 代码提交

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java |  299 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 292 insertions(+), 7 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java
index 7c928c0..20e3869 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetTypeServiceImpl.java
@@ -3,23 +3,39 @@
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.basic.PageInfo;
+import com.ruoyi.common.core.domain.entity.TDept;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.system.dto.asset.AssetTypeDTO;
 import com.ruoyi.system.mapper.AssetTypeMapper;
+import com.ruoyi.system.model.AssetInventoryRecord;
 import com.ruoyi.system.model.AssetMain;
 import com.ruoyi.system.model.AssetType;
+import com.ruoyi.system.query.AssetInventoryListQuery;
+import com.ruoyi.system.query.AssetStatisticsListDetailQuery;
+import com.ruoyi.system.query.AssetStatisticsListQuery;
+import com.ruoyi.system.service.AssetInventoryRecordService;
 import com.ruoyi.system.service.AssetMainService;
 import com.ruoyi.system.service.AssetTypeService;
+import com.ruoyi.system.vo.AssetInventoryVO;
+import com.ruoyi.system.vo.AssetStatisticsDetailVO;
+import com.ruoyi.system.vo.AssetStatisticsVO;
 import com.ruoyi.system.vo.asset.AssetTypeTreeVO;
+import com.ruoyi.system.vo.system.NotificationVO;
+import io.swagger.annotations.ApiModelProperty;
+import io.swagger.models.auth.In;
 import lombok.RequiredArgsConstructor;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -31,10 +47,12 @@
  * @since 2025-09-15
  */
 @Service
-@RequiredArgsConstructor
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class AssetTypeServiceImpl extends ServiceImpl<AssetTypeMapper, AssetType> implements AssetTypeService {
 
     private final AssetMainService assetMainService;
+    private final AssetTypeService assetTypeService;
+    private final AssetInventoryRecordService assetInventoryRecordService;
 
     @Override
     public List<AssetTypeTreeVO> getAssetTypeTree() {
@@ -52,10 +70,31 @@
         // 转换为VO对象
         List<AssetTypeTreeVO> assetTypeVOs = BeanUtil.copyToList(allAssetTypes, AssetTypeTreeVO.class);
         
+        // 预查询:找出存在资产关联的资产类型ID集合(去重)
+        List<Integer> allTypeIds = allAssetTypes.stream().map(AssetType::getId).collect(Collectors.toList());
+        QueryWrapper<AssetMain> usedTypeQuery = new QueryWrapper<>();
+        usedTypeQuery.select("distinct asset_type_id").in("asset_type_id", allTypeIds);
+        List<AssetMain> usedTypeRows = assetMainService.list(usedTypeQuery);
+        Set<Integer> usedTypeIdSet = usedTypeRows.stream()
+                .map(AssetMain::getAssetTypeId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
         // 按父级ID分组
         Map<Integer, List<AssetTypeTreeVO>> parentIdMap = assetTypeVOs.stream()
                 .collect(Collectors.groupingBy(AssetTypeTreeVO::getParentId));
         
+        // 计算可删除标记:一级仅判断是否有子分类;二级仅判断是否有关联资产
+        for (AssetTypeTreeVO vo : assetTypeVOs) {
+            if (Objects.equals(vo.getLevel(), 1)) {
+                boolean hasChildren = parentIdMap.containsKey(vo.getId()) && CollUtil.isNotEmpty(parentIdMap.get(vo.getId()));
+                vo.setCanDelete(!hasChildren);
+            } else {
+                boolean hasAssets = usedTypeIdSet.contains(vo.getId());
+                vo.setCanDelete(!hasAssets);
+            }
+        }
+
         // 构建树形结构
         List<AssetTypeTreeVO> rootNodes = parentIdMap.get(0);
         if (CollUtil.isEmpty(rootNodes)) {
@@ -191,7 +230,7 @@
         childQueryWrapper.eq(AssetType::getParentId, id);
         long childCount = this.count(childQueryWrapper);
         if (childCount > 0) {
-            throw new ServiceException("该资产类型存在子类型,不能删除");
+            throw new ServiceException(String.format("【%s】存在子类型,不能删除", existingAssetType.getTypeName()));
         }
 
         // 校验是否有关联的资产记录
@@ -199,11 +238,257 @@
         assetMainQueryWrapper.eq(AssetMain::getAssetTypeId, id);
         long assetMainCount = assetMainService.count(assetMainQueryWrapper);
         if (assetMainCount > 0) {
-            throw new ServiceException("该资产类型已关联资产记录,不能删除");
+            throw new ServiceException(String.format("【%s】已关联资产记录,不能删除", existingAssetType.getTypeName()));
         }
 
         // 删除资产类型
         this.removeById(id);
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void batchDeleteAssetType(List<Integer> ids) {
+        // 校验ID列表是否为空
+        if (CollUtil.isEmpty(ids)) {
+            throw new ServiceException("删除的资产类型ID列表不能为空");
+        }
+
+        // 收集删除失败的信息
+        List<String> failedMessages = new ArrayList<>();
+        
+        // 逐个删除资产类型
+        for (Integer id : ids) {
+            try {
+                this.deleteAssetType(id);
+            } catch (ServiceException e) {
+                failedMessages.add(e.getMessage());
+            }
+        }
+        
+        // 如果有删除失败的情况,抛出异常
+        if (CollUtil.isNotEmpty(failedMessages)) {
+            throw new ServiceException("批量删除失败:" + String.join(";", failedMessages));
+        }
+    }
+
+    @Override
+    public String getAssetCodePrefix(Integer assetTypeId) {
+        if (assetTypeId == null) {
+            throw new ServiceException("资产类型ID不能为空");
+        }
+
+        // 查询当前资产类型
+        AssetType currentAssetType = this.getById(assetTypeId);
+        if (currentAssetType == null) {
+            throw new ServiceException("资产类型不存在");
+        }
+
+        String parentTypeCode = "";
+        String subTypeCode = currentAssetType.getTypeCode();
+
+        // 如果是二级分类,需要获取父级分类的简写
+        if (currentAssetType.getLevel() == 2 && currentAssetType.getParentId() != null && currentAssetType.getParentId() != 0) {
+            AssetType parentAssetType = this.getById(currentAssetType.getParentId());
+            if (parentAssetType != null) {
+                parentTypeCode = parentAssetType.getTypeCode();
+            }
+        } else if (currentAssetType.getLevel() == 1) {
+            // 如果是一级分类,父级简写为空,子类简写就是当前类型简写
+            parentTypeCode = currentAssetType.getTypeCode();
+            subTypeCode = "";
+        }
+
+        return parentTypeCode + subTypeCode;
+    }
+
+    @Override
+    public PageInfo<AssetStatisticsVO> pageList(AssetStatisticsListQuery query) {
+        if (StringUtils.hasLength(query.getNameOrCode())){
+            // 查询出资产名称或者资产编号符合条件的code
+            List<Integer> assetTypeIds = assetMainService.lambdaQuery().like(AssetMain::getAssetName, query.getNameOrCode())
+                    .or()
+                    .like(AssetMain::getAssetCode, query.getNameOrCode())
+                    .list()
+                    .stream()
+                    .map(AssetMain::getAssetTypeId)
+                    .collect(Collectors.toList());
+            query.setAssetMainIds(assetTypeIds);
+            if (assetTypeIds.isEmpty()){
+                return new PageInfo<>();
+            }
+        }
+        PageInfo<AssetStatisticsVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
+        List<AssetStatisticsVO> list = this.baseMapper.pageList(query,pageInfo);
+        List<AssetStatisticsVO> listNoLimit = this.baseMapper.pageListNoLimit(query);
+        List<Integer> assetTypeIds = listNoLimit.stream().map(AssetStatisticsVO::getAssetTypeIdSecond).collect(Collectors.toList());
+        if (assetTypeIds.isEmpty()){
+            return new PageInfo<>();
+        }
+        Map<Integer, List<AssetMain>> assetMainMap = assetMainService.lambdaQuery()
+                .in(!query.getDeptIds().isEmpty(),AssetMain::getOwnershipDeptId, query.getDeptIds())
+                .in(AssetMain::getAssetTypeId, assetTypeIds).list()
+                .stream()
+                .collect(Collectors.groupingBy(AssetMain::getAssetTypeId));
+        for (AssetStatisticsVO asset : list) {
+            Integer totalCount= 0;
+            Integer idleCount= 0;
+            Integer useCount= 0;
+            Integer disposeCount= 0;
+            Integer otherCount= 0;
+            BigDecimal totalValue = new BigDecimal("0");
+            List<AssetMain> assetMains = assetMainMap.get(asset.getAssetTypeIdSecond());
+            if (!assetMains.isEmpty()){
+                for (AssetMain assetMain : assetMains) {
+                    if (assetMain.getAssetStatus().contains("闲置")){
+                        idleCount += assetMain.getQuantity();
+                    }else if (assetMain.getAssetStatus().contains("使用中")){
+                        useCount += assetMain.getQuantity();
+                    }else if (assetMain.getAssetStatus().contains("损坏") || assetMain.getAssetStatus().contains("报废")){
+                        disposeCount += assetMain.getQuantity();
+                    }else{
+                        otherCount += assetMain.getQuantity();
+                    }
+                    totalCount+= assetMain.getQuantity();
+                    totalValue = totalValue.add(new BigDecimal(assetMain.getQuantity())
+                            .multiply(assetMain.getUnitPrice()).setScale(2, RoundingMode.HALF_DOWN));
+                }
+            }
+            asset.setIdleCount(idleCount);
+            asset.setUseCount(useCount);
+            asset.setDisposeCount(disposeCount);
+            asset.setOtherCount(otherCount);
+            asset.setTotalCount(totalCount);
+            asset.setTotalValue(totalValue);
+        }
+        pageInfo.setRecords(list);
+        return pageInfo;
+    }
+
+    @Override
+    public PageInfo<AssetStatisticsDetailVO> pageListDetail(AssetStatisticsListDetailQuery query) {
+        String assetTypeName = null;
+        AssetType assetType = assetTypeService.getById(query.getAssetTypeIdSecond());
+        if (assetType!=null){
+            AssetType assetTypeParent = assetTypeService.getById(assetType.getParentId());
+            if (assetTypeParent!=null){
+                assetTypeName = assetTypeParent.getTypeName()+">"+assetType.getTypeName();
+            }
+        }
+
+        if (StringUtils.hasLength(query.getNameOrCode())){
+            // 查询出资产名称或者资产编号符合条件的code
+            List<Integer> assetTypeIds = assetMainService.lambdaQuery().like(AssetMain::getAssetName, query.getNameOrCode())
+                    .or()
+                    .like(AssetMain::getAssetCode, query.getNameOrCode())
+                    .list()
+                    .stream()
+                    .map(AssetMain::getAssetTypeId)
+                    .collect(Collectors.toList());
+            query.setAssetMainIds(assetTypeIds);
+            if (assetTypeIds.isEmpty()){
+                return new PageInfo<>();
+            }
+        }
+        PageInfo<AssetStatisticsDetailVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
+        List<AssetStatisticsDetailVO> list = this.baseMapper.pageListDetail(query,pageInfo);
+        for (AssetStatisticsDetailVO assetStatisticsDetailVO : list) {
+            assetStatisticsDetailVO.setAssetTypeName(assetTypeName);
+        }
+        pageInfo.setRecords(list);
+        return pageInfo;
+    }
+
+    @Override
+    public PageInfo<AssetInventoryVO> pageListInventory(AssetInventoryListQuery query) {
+        AssetType assetType = this.baseMapper.selectById(query.getAssetTypeId());
+        if (assetType.getParentId()==0){
+            List<Integer> assetTypeChild = this.baseMapper.selectList(new LambdaQueryWrapper<AssetType>()
+                            .eq(AssetType::getParentId, assetType.getId())).stream().map(AssetType::getId)
+                    .collect(Collectors.toList());
+            List<Integer> list = new ArrayList<>(assetTypeChild);
+            list.add(assetType.getId());
+            query.setAssetTypeIds( list);
+        }else{
+            query.setAssetTypeIds(Collections.singletonList(assetType.getId()));
+        }
+        PageInfo<AssetInventoryVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
+        List<AssetInventoryVO> list = this.baseMapper.pageListInventory(query,pageInfo);
+        Map<Integer, List<AssetMain>> assetMainMap = assetMainService.lambdaQuery().in(AssetMain::getId, query.getAssetMainIds()).list()
+                .stream().collect(Collectors.groupingBy(AssetMain::getAssetTypeId));
+        List<AssetInventoryRecord> assetInventoryRecords = assetInventoryRecordService.lambdaQuery().in(AssetInventoryRecord::getAssetMainId, query.getAssetMainIds()).list();
+        for (AssetInventoryVO assetInventoryVO : list) {
+            int inCount = 0;
+            int outCount = 0;
+            int inStockCount = 0;
+
+            BigDecimal inStockMoney = new BigDecimal("0");
+            BigDecimal inMoney = new BigDecimal("0");
+            BigDecimal outMoney = new BigDecimal("0");
+            List<AssetMain> assetMains = assetMainMap.get(assetInventoryVO.getAssetTypeIdSecond());
+            for (AssetInventoryRecord assetInventoryRecord : assetInventoryRecords) {
+                if (assetInventoryRecord.getType()==0){
+                    AssetMain assetMain = assetMains.stream().filter(e -> e.getId().equals(assetInventoryRecord.getAssetMainId()))
+                            .findFirst().orElse(null);
+                    if (assetMain!=null){
+                        inCount+=assetMain.getQuantity();
+                        inMoney = inMoney.add(new BigDecimal(assetMain.getQuantity())
+                                .multiply(assetMain.getUnitPrice()).setScale(2, RoundingMode.HALF_DOWN));
+                    }
+                }else{
+                    AssetMain assetMain = assetMains.stream().filter(e -> e.getId().equals(assetInventoryRecord.getAssetMainId()))
+                            .findFirst().orElse(null);
+                    if (assetMain!=null){
+                        outCount+=assetMain.getQuantity();
+                        outMoney = outMoney.add(new BigDecimal(assetMain.getQuantity())
+                                .multiply(assetMain.getUnitPrice()).setScale(2, RoundingMode.HALF_DOWN));
+                    }
+                }
+            }
+            assetInventoryVO.setInCount(inCount);
+            assetInventoryVO.setOutCount(outCount);
+            assetInventoryVO.setInMoney(inMoney);
+            assetInventoryVO.setOutMoney(outMoney);
+            List<AssetMain> inStorage = assetMains.stream().filter(e -> e.getDisposed() == 0 && e.getInUse() == 0 && e.getBorrowed() == 0).collect(Collectors.toList());
+            for (AssetMain assetMain : inStorage) {
+                inStockCount+=assetMain.getQuantity();
+                inStockMoney = inStockMoney.add(new BigDecimal(assetMain.getQuantity())
+                        .multiply(assetMain.getUnitPrice()).setScale(2, RoundingMode.HALF_DOWN));
+            }
+            assetInventoryVO.setInStockCount(inStockCount);
+            assetInventoryVO.setInStockMoney(inStockMoney);
+        }
+        pageInfo.setRecords(list);
+        return pageInfo;
+    }
+
+    @Override
+    public PageInfo<AssetStatisticsDetailVO> pageListInventoryDetail(AssetStatisticsListDetailQuery query) {
+        PageInfo<AssetStatisticsDetailVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
+        List<AssetStatisticsDetailVO> list = this.baseMapper.pageListInventoryDetail(query,pageInfo);
+        List<AssetType> assetTypes = assetTypeService.list();
+        for (AssetStatisticsDetailVO assetStatisticsDetailVO : list) {
+            Integer type = assetStatisticsDetailVO.getType();
+            if (type==0){
+                String typeName = "入库";
+
+                AssetType assetType = assetTypes.stream().filter(e -> e.getId().equals(assetStatisticsDetailVO.getAssetTypeId()))
+                        .findFirst().orElse(null);
+                if (assetType!=null){
+                    typeName =typeName+"("+assetType.getTypeName()+")";
+                }
+                assetStatisticsDetailVO.setTypeName(typeName);
+            }else{
+                String typeName = "出库";
+                AssetType assetType = assetTypes.stream().filter(e -> e.getId().equals(assetStatisticsDetailVO.getAssetTypeId()))
+                        .findFirst().orElse(null);
+                if (assetType!=null){
+                    typeName =typeName+"("+assetType.getTypeName()+")";
+                }
+                assetStatisticsDetailVO.setTypeName(typeName);
+            }
+        }
+        pageInfo.setRecords(list);
+        return pageInfo;
+    }
+
 }

--
Gitblit v1.7.1