From d4e4dc4a180cea919dfc86a1a3e5af0abe8b6d36 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期一, 20 十月 2025 18:48:14 +0800
Subject: [PATCH] 无形广告资产接口
---
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetMainServiceImpl.java | 303 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 300 insertions(+), 3 deletions(-)
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetMainServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetMainServiceImpl.java
index cc1dd58..323ecf7 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetMainServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AssetMainServiceImpl.java
@@ -1,24 +1,321 @@
package com.ruoyi.system.service.impl;
+import cn.hutool.core.bean.BeanUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.core.domain.entity.TDept;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.system.emums.ApprovalTypeEnum;
import com.ruoyi.system.mapper.AssetMainMapper;
-import com.ruoyi.system.model.AssetMain;
+import com.ruoyi.system.mapper.AssetPropertyExtMapper;
+import com.ruoyi.system.mapper.AssetVehicleExtMapper;
+import com.ruoyi.system.mapper.OaApprovalApplicationStorageAssetMapper;
+import com.ruoyi.system.mapper.OaApprovalApplicationStorageAssetPropertyExtMapper;
+import com.ruoyi.system.mapper.OaApprovalApplicationStorageAssetVehicleExtMapper;
+import com.ruoyi.system.model.*;
import com.ruoyi.system.query.AssetMainPageQuery;
-import com.ruoyi.system.service.AssetMainService;
+import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.asset.AssetMainPageVO;
+import com.ruoyi.system.vo.asset.AssetMainVO;
+import lombok.RequiredArgsConstructor;
+import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
/**
* 资产主表 服务实现类
*/
@Service
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class AssetMainServiceImpl extends ServiceImpl<AssetMainMapper, AssetMain> implements AssetMainService {
-
+ private final AssetTypeService assetTypeService;
+ private final AssetMainService assetMainService;
+ private final AssetInventoryRecordService assetInventoryRecordService;
+ private final OaApprovalApplicationStorageAssetService oaApprovalApplicationStorageAssetService;
+ private final OaApprovalApplicationStorageService oaApprovalApplicationStorageService;
+ private final OaApprovalApplicationStorageAssetPropertyExtService oaApprovalApplicationStorageAssetPropertyExtService;
+ private final OaApprovalApplicationStorageAssetVehicleExtService oaApprovalApplicationStorageAssetVehicleExtService;
+ private final AssetVehicleExtService assetVehicleExtService;
+ private final AssetPropertyExtService assetPropertyExtService;
+
+ private final AssetMainMapper assetMainMapper;
+ private final OaApprovalApplicationStorageAssetMapper oaApprovalApplicationStorageAssetMapper;
+ private final OaApprovalApplicationStorageAssetPropertyExtMapper oaApprovalApplicationStorageAssetPropertyExtMapper;
+ private final OaApprovalApplicationStorageAssetVehicleExtMapper oaApprovalApplicationStorageAssetVehicleExtMapper;
+ private final AssetPropertyExtMapper assetPropertyExtMapper;
+ private final AssetVehicleExtMapper assetVehicleExtMapper;
+ private final TDeptService tDeptService;
+ private final AssetWarehouseService assetWarehouseService;
@Override
public IPage<AssetMainPageVO> getApprovedPageList(AssetMainPageQuery pageQuery) {
Page<AssetMainPageVO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
return this.baseMapper.selectApprovedAssetPage(page, pageQuery);
}
+ /**
+ * 生成资产编码
+ * 格式:资产类型一级分类简写+资产类型子类简写+入库日期+【-】+数量顺序编号(4位)
+ * 例如:GDFC20250917-0001
+ */
+ @Override
+ public String generateAssetCode(Integer assetTypeId, LocalDate storageDate) {
+ // 根据资产类型ID获取资产编码前缀
+ String typeCodePrefix = assetTypeService.getAssetCodePrefix(assetTypeId);
+
+ String dateStr = storageDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+ String prefix = typeCodePrefix + dateStr + "-";
+
+ // 查询当天该类型资产的数量
+ Long count = this.lambdaQuery()
+ .like(AssetMain::getAssetCode, prefix)
+ .ge(AssetMain::getCreateTime, storageDate.atStartOfDay())
+ .lt(AssetMain::getCreateTime, storageDate.plusDays(1).atStartOfDay())
+ .count();
+
+ int sequence = (count != null ? count.intValue() : 0) + 1;
+ String sequenceStr = String.format("%04d", sequence);
+
+ return prefix + sequenceStr;
+ }
+
+ @Override
+ public void saveAssetByApprovalApplicationId(Integer approvalApplicationId) {
+ // 1. 根据approvalApplicationId查询审批通过的资产列表
+ List<OaApprovalApplicationStorageAsset> storageAssets = oaApprovalApplicationStorageAssetMapper
+ .selectList(new LambdaQueryWrapper<OaApprovalApplicationStorageAsset>()
+ .eq(OaApprovalApplicationStorageAsset::getApprovalApplicationId, approvalApplicationId));
+
+ if (CollectionUtils.isEmpty(storageAssets)) {
+ return;
+ }
+
+ List<AssetMain> assetMainList = new ArrayList<>();
+ Map<Integer, Integer> storageAssetIdToAssetMainIdMap = new HashMap<>();
+ Map<Integer, Integer> storageAssetIdToAssetTypeMap = new HashMap<>();
+
+ // 预查询当天已存在的资产编码数量
+ LocalDate currentDate = LocalDate.now();
+ Map<Integer, Long> assetTypeCountMap = new HashMap<>();
+
+ for (OaApprovalApplicationStorageAsset storageAsset : storageAssets) {
+ Integer assetTypeId = storageAsset.getAssetTypeId();
+ Integer assetMainType = storageAsset.getAssetMainType();
+ Integer quantity = storageAsset.getQuantity();
+
+ // 根据数量生成对应条数的资产记录
+ int assetCount = quantity != null ? quantity : 1;
+
+ // 获取该类型资产的编码前缀和当前数量
+ String typeCodePrefix = assetTypeService.getAssetCodePrefix(assetTypeId);
+ String dateStr = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+ String prefix = typeCodePrefix + dateStr + "-";
+
+ // 获取当天该类型资产的当前数量(只查询一次)
+ Long currentCount = assetTypeCountMap.computeIfAbsent(assetTypeId, k ->
+ this.lambdaQuery()
+ .like(AssetMain::getAssetCode, prefix)
+ .ge(AssetMain::getCreateTime, currentDate.atStartOfDay())
+ .lt(AssetMain::getCreateTime, currentDate.plusDays(1).atStartOfDay())
+ .count()
+ );
+
+ for (int i = 0; i < assetCount; i++) {
+ // 创建资产主表记录
+ AssetMain assetMain = new AssetMain();
+ BeanUtil.copyProperties(storageAsset, assetMain, "id");
+
+ // 生成资产编码,使用递增的序号
+ int sequence = (currentCount != null ? currentCount.intValue() : 0) + i + 1;
+ String sequenceStr = String.format("%04d", sequence);
+ String assetCode = prefix + sequenceStr;
+ assetMain.setAssetCode(assetCode);
+
+ // 单个资产的数量设为1
+ assetMain.setQuantity(1);
+ // 单个资产的总价值等于单价
+ assetMain.setTotalValue(storageAsset.getUnitPrice());
+
+ // 设置创建信息
+ assetMain.setCreateTime(LocalDateTime.now());
+ assetMain.setCreateBy(SecurityUtils.getUsername());
+ assetMain.setDisabled(false);
+
+ assetMainList.add(assetMain);
+
+ // 记录storageAssetId和资产类型的映射关系,用于后续创建扩展信息
+ storageAssetIdToAssetTypeMap.put(storageAsset.getId(), assetMainType);
+ }
+
+ // 更新计数器,确保下一个storageAsset的编码序号正确
+ if (currentCount != null) {
+ assetTypeCountMap.put(assetTypeId, currentCount + assetCount);
+ }
+ }
+
+ // 批量保存资产主表
+ if (!assetMainList.isEmpty()) {
+ this.saveBatch(assetMainList);
+ saveAssetRecord(0, ApprovalTypeEnum.IN_STOCK.getDesc(),assetMainList.stream().map(AssetMain::getId)
+ .collect(Collectors.toList()));
+ // 建立storageAssetId到assetMainId的映射关系
+ int assetMainIndex = 0;
+ for (OaApprovalApplicationStorageAsset storageAsset : storageAssets) {
+ int quantity = storageAsset.getQuantity() != null ? storageAsset.getQuantity() : 1;
+ for (int i = 0; i < quantity; i++) {
+ if (assetMainIndex < assetMainList.size()) {
+ storageAssetIdToAssetMainIdMap.put(storageAsset.getId(), assetMainList.get(assetMainIndex).getId());
+ assetMainIndex++;
+ }
+ }
+ }
+
+ // 准备并保存扩展信息
+ saveExtensionInfoBatch(storageAssetIdToAssetMainIdMap, storageAssetIdToAssetTypeMap);
+ }
+ }
+ private void saveAssetRecord(Integer type, String approvalType,List<Integer> assetMainIds) {
+ List<AssetInventoryRecord> res = new ArrayList<>();
+ Map<Integer, Integer> warehouseMap = assetMainService.list(new LambdaQueryWrapper<AssetMain>()
+ .isNotNull(AssetMain::getWarehouseId)).stream().collect(Collectors.toMap(AssetMain::getId, AssetMain::getWarehouseId));
+ for (Integer assetMainId : assetMainIds) {
+ AssetInventoryRecord assetInventoryRecord = new AssetInventoryRecord();
+ assetInventoryRecord.setAssetMainId(assetMainId);
+ assetInventoryRecord.setWarehouseId(warehouseMap.get(assetMainId));
+ assetInventoryRecord.setType(type);
+ assetInventoryRecord.setRemarks(approvalType);
+ res.add(assetInventoryRecord);
+ }
+ assetInventoryRecordService.saveBatch(res);
+ }
+ /**
+ * 批量保存扩展信息
+ */
+ private void saveExtensionInfoBatch(Map<Integer, Integer> storageAssetIdToAssetMainIdMap, Map<Integer, Integer> storageAssetIdToAssetTypeMap) {
+ List<AssetPropertyExt> propertyExtList = new ArrayList<>();
+ List<AssetVehicleExt> vehicleExtList = new ArrayList<>();
+
+ // 遍历所有需要扩展信息的storageAsset
+ for (Map.Entry<Integer, Integer> entry : storageAssetIdToAssetMainIdMap.entrySet()) {
+ Integer storageAssetId = entry.getKey();
+ Integer assetMainId = entry.getValue();
+ Integer assetMainType = storageAssetIdToAssetTypeMap.get(storageAssetId);
+
+ if (assetMainType == null) {
+ continue;
+ }
+
+ if (assetMainType == 1) {
+ // 房产资产 - 查询并准备房产扩展信息
+ OaApprovalApplicationStorageAssetPropertyExt propertyExt = oaApprovalApplicationStorageAssetPropertyExtMapper
+ .selectOne(new LambdaQueryWrapper<OaApprovalApplicationStorageAssetPropertyExt>()
+ .eq(OaApprovalApplicationStorageAssetPropertyExt::getStorageAssetId, storageAssetId));
+
+ if (propertyExt != null) {
+ AssetPropertyExt assetPropertyExt = new AssetPropertyExt();
+ BeanUtil.copyProperties(propertyExt, assetPropertyExt,"id");
+ assetPropertyExt.setAssetMainId(assetMainId);
+ assetPropertyExt.setCreateTime(LocalDateTime.now());
+ assetPropertyExt.setCreateBy(SecurityUtils.getUsername());
+ assetPropertyExt.setDisabled(false);
+
+ propertyExtList.add(assetPropertyExt);
+ }
+ } else if (assetMainType == 2) {
+ // 车辆资产 - 查询并准备车辆扩展信息
+ OaApprovalApplicationStorageAssetVehicleExt vehicleExt = oaApprovalApplicationStorageAssetVehicleExtMapper
+ .selectOne(new QueryWrapper<OaApprovalApplicationStorageAssetVehicleExt>()
+ .eq("storage_asset_id", storageAssetId));
+
+ if (vehicleExt != null) {
+ AssetVehicleExt assetVehicleExt = new AssetVehicleExt();
+ BeanUtil.copyProperties(vehicleExt, assetVehicleExt,"id");
+ assetVehicleExt.setAssetMainId(assetMainId);
+ assetVehicleExt.setCreateTime(LocalDateTime.now());
+ assetVehicleExt.setCreateBy(SecurityUtils.getUsername());
+ assetVehicleExt.setDisabled(false);
+
+ vehicleExtList.add(assetVehicleExt);
+ }
+ }
+ // assetMainType == 0 为通用资产,无需保存扩展信息
+ }
+
+ // 批量保存扩展信息
+ if (!propertyExtList.isEmpty()) {
+ assetPropertyExtService.saveBatch(propertyExtList);
+ }
+ if (!vehicleExtList.isEmpty()) {
+ assetVehicleExtService.saveBatch(vehicleExtList);
+ }
+ }
+
+
+ @Override
+ public List<AssetMainVO> getListByIds(List<Integer> assetMainIds) {
+ if (!CollectionUtils.isEmpty(assetMainIds)) {
+ List<AssetMain> assetMains = this.lambdaQuery()
+ .in(AssetMain::getId, assetMainIds)
+ .list();
+ //查询资产类型名称
+ List<Integer> assetTypeIds = assetMains.stream().map(AssetMain::getAssetTypeId).distinct().collect(Collectors.toList());
+ Map<Integer, String> typeNameMap = assetTypeIds.isEmpty() ? Collections.emptyMap() : assetTypeService.listByIds(assetTypeIds).stream()
+ .collect(Collectors.toMap(AssetType::getId, AssetType::getTypeName));
+ //查询批量查询部门名称
+ //使用部门
+ Set<Integer> useDeptIds = assetMains.stream().filter(item -> item.getAddressType().equals(0))
+ .map(AssetMain::getUseDeptId).collect(Collectors.toSet());
+ //归属部门
+ Set<Integer> deptIds = assetMains.stream().map(AssetMain::getOwnershipDeptId).collect(Collectors.toSet());
+ deptIds.addAll(useDeptIds);
+ Map<Integer, String> deptNameMap = deptIds.isEmpty() ? Collections.emptyMap() : tDeptService.listByIds(deptIds).stream()
+ .collect(Collectors.toMap(TDept::getId, TDept::getDeptName));
+ //查询资产入库时间
+ List<Integer> applicationIds = assetMains.stream().map(AssetMain::getApprovalApplicationId).distinct().collect(Collectors.toList());
+ Map<Integer, LocalDate> storageTimeMap = oaApprovalApplicationStorageService.lambdaQuery().in(OaApprovalApplicationStorage::getApprovalApplicationId, applicationIds).list().stream().collect(Collectors.toMap(OaApprovalApplicationStorage::getApprovalApplicationId, OaApprovalApplicationStorage::getStorageTime));
+ //查询使用仓库
+ Set<Integer> warehouseIds = assetMains.stream().filter(item -> item.getAddressType().equals(1)).map(AssetMain::getWarehouseId).collect(Collectors.toSet());
+ Map<Integer, String> warehouseNameMap = warehouseIds.isEmpty() ? Collections.emptyMap() : assetWarehouseService.listByIds(warehouseIds).stream()
+ .collect(Collectors.toMap(AssetWarehouse::getId, AssetWarehouse::getWarehouseName));
+ for (AssetMain assetMain : assetMains) {
+ switch (assetMain.getAddressType()) {
+ case 0:
+ assetMain.setUseDeptOrLocation(deptNameMap.get(assetMain.getUseDeptId()));
+ break;
+ case 1:
+ assetMain.setUseDeptOrLocation(warehouseNameMap.get(assetMain.getWarehouseId()));
+ break;
+ case 2:
+ assetMain.setUseDeptOrLocation(assetMain.getAddress());
+ break;
+ }
+ }
+ List<AssetMainVO> voList = BeanUtil.copyToList(assetMains, AssetMainVO.class);
+ for (AssetMainVO assetMainVO : voList) {
+ assetMainVO.setAssetTypeName(typeNameMap.get(assetMainVO.getAssetTypeId()));
+ assetMainVO.setOwnershipDeptName(deptNameMap.get(assetMainVO.getOwnershipDeptId()));
+ assetMainVO.setStorageTime(storageTimeMap.get(assetMainVO.getApprovalApplicationId()));
+ }
+ return voList;
+ }
+ return Collections.emptyList();
+ }
+
+ @Override
+ public IPage<AssetMainPageVO> getAssetPageList(AssetMainPageQuery pageQuery) {
+ Page<AssetMainPageVO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
+ return this.baseMapper.getAssetPageList(page, pageQuery);
+ }
}
--
Gitblit v1.7.1