| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | 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; |
| | |
| | | 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(); |
| | | BigDecimal quantity = storageAsset.getQuantity(); |
| | | Integer quantity = storageAsset.getQuantity(); |
| | | |
| | | // 根据数量生成对应条数的资产记录 |
| | | int assetCount = quantity != null ? quantity.intValue() : 1; |
| | | 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); |
| | | BeanUtil.copyProperties(storageAsset, assetMain, "id"); |
| | | |
| | | // 为每个资产生成编码 |
| | | String assetCode = generateAssetCode(storageAsset.getAssetTypeId(), LocalDate.now()); |
| | | // 生成资产编码,使用递增的序号 |
| | | 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(BigDecimal.ONE); |
| | | assetMain.setQuantity(1); |
| | | // 单个资产的总价值等于单价 |
| | | assetMain.setTotalValue(storageAsset.getUnitPrice()); |
| | | |
| | |
| | | assetMain.setCreateBy(SecurityUtils.getUsername()); |
| | | assetMain.setDisabled(false); |
| | | |
| | | // 保存资产主表 |
| | | assetMainMapper.insert(assetMain); |
| | | assetMainList.add(assetMain); |
| | | |
| | | // 2. 根据资产类型保存扩展信息 |
| | | if (assetMainType != null) { |
| | | if (assetMainType == 1) { |
| | | // 房产资产 - 保存房产扩展信息 |
| | | savePropertyExtension(storageAsset.getId(), assetMain.getId()); |
| | | } else if (assetMainType == 2) { |
| | | // 车辆资产 - 保存车辆扩展信息 |
| | | saveVehicleExtension(storageAsset.getId(), assetMain.getId()); |
| | | } |
| | | // assetMainType == 0 为通用资产,无需保存扩展信息 |
| | | } |
| | | // 记录storageAssetId和资产类型的映射关系,用于后续创建扩展信息 |
| | | storageAssetIdToAssetTypeMap.put(storageAsset.getId(), assetMainType); |
| | | } |
| | | |
| | | // 更新计数器,确保下一个storageAsset的编码序号正确 |
| | | if (currentCount != null) { |
| | | assetTypeCountMap.put(assetTypeId, currentCount + assetCount); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存房产资产扩展信息 |
| | | */ |
| | | private void savePropertyExtension(Integer storageAssetId, Integer assetMainId) { |
| | | // 查询房产扩展信息 |
| | | OaApprovalApplicationStorageAssetPropertyExt propertyExt = oaApprovalApplicationStorageAssetPropertyExtMapper |
| | | .selectOne(new LambdaQueryWrapper<OaApprovalApplicationStorageAssetPropertyExt>() |
| | | .eq(OaApprovalApplicationStorageAssetPropertyExt::getStorageAssetId, storageAssetId)); |
| | | |
| | | if (propertyExt != null) { |
| | | AssetPropertyExt assetPropertyExt = new AssetPropertyExt(); |
| | | BeanUtil.copyProperties(propertyExt, assetPropertyExt); |
| | | assetPropertyExt.setAssetMainId(assetMainId); |
| | | assetPropertyExt.setCreateTime(LocalDateTime.now()); |
| | | assetPropertyExt.setCreateBy(SecurityUtils.getUsername()); |
| | | assetPropertyExt.setDisabled(false); |
| | | // 批量保存资产主表 |
| | | if (!assetMainList.isEmpty()) { |
| | | this.saveBatch(assetMainList); |
| | | |
| | | assetPropertyExtMapper.insert(assetPropertyExt); |
| | | // 建立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 saveVehicleExtension(Integer storageAssetId, Integer assetMainId) { |
| | | // 查询车辆扩展信息 |
| | | OaApprovalApplicationStorageAssetVehicleExt vehicleExt = oaApprovalApplicationStorageAssetVehicleExtMapper |
| | | .selectOne(new QueryWrapper<OaApprovalApplicationStorageAssetVehicleExt>() |
| | | .eq("storage_asset_id", storageAssetId)); |
| | | private void saveExtensionInfoBatch(Map<Integer, Integer> storageAssetIdToAssetMainIdMap, Map<Integer, Integer> storageAssetIdToAssetTypeMap) { |
| | | List<AssetPropertyExt> propertyExtList = new ArrayList<>(); |
| | | List<AssetVehicleExt> vehicleExtList = new ArrayList<>(); |
| | | |
| | | if (vehicleExt != null) { |
| | | AssetVehicleExt assetVehicleExt = new AssetVehicleExt(); |
| | | BeanUtil.copyProperties(vehicleExt, assetVehicleExt); |
| | | assetVehicleExt.setAssetMainId(assetMainId); |
| | | assetVehicleExt.setCreateTime(LocalDateTime.now()); |
| | | assetVehicleExt.setCreateBy(SecurityUtils.getUsername()); |
| | | assetVehicleExt.setDisabled(false); |
| | | // 遍历所有需要扩展信息的storageAsset |
| | | for (Map.Entry<Integer, Integer> entry : storageAssetIdToAssetMainIdMap.entrySet()) { |
| | | Integer storageAssetId = entry.getKey(); |
| | | Integer assetMainId = entry.getValue(); |
| | | Integer assetMainType = storageAssetIdToAssetTypeMap.get(storageAssetId); |
| | | |
| | | assetVehicleExtMapper.insert(assetVehicleExt); |
| | | 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)) { |