| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.entity.CoalField; |
| | | import com.ruoyi.basic.entity.CoalValue; |
| | | import com.ruoyi.basic.mapper.CoalFieldMapper; |
| | | import com.ruoyi.basic.mapper.CoalValueMapper; |
| | | import com.ruoyi.business.dto.PendingInventoryDto; |
| | | import com.ruoyi.business.entity.OfficialInventory; |
| | | import com.ruoyi.business.entity.PendingInventory; |
| | | import com.ruoyi.business.mapper.OfficialInventoryMapper; |
| | | import com.ruoyi.business.mapper.PendingInventoryMapper; |
| | | import com.ruoyi.business.service.PendingInventoryService; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | public class PendingInventoryServiceImpl extends ServiceImpl<PendingInventoryMapper, PendingInventory> implements PendingInventoryService { |
| | | |
| | | private final PendingInventoryMapper pendingInventoryMapper; |
| | | |
| | | private final OfficialInventoryMapper officialInventoryMapper; |
| | | |
| | | private final CoalValueMapper coalValueMapper; |
| | | |
| | |
| | | // 执行批量逻辑删除 |
| | | return pendingInventoryMapper.update(null, updateWrapper); |
| | | } |
| | | |
| | | @Transactional |
| | | @Override |
| | | public int addOrEditCoalValue(PendingInventoryDto pendingInventoryDto) { |
| | | // Step 1: 删除原有 planId 相关数据 |
| | | coalValueMapper.delete(new LambdaQueryWrapper<CoalValue>() |
| | | .eq(CoalValue::getPlanId, pendingInventoryDto.getPId())); |
| | | // Step 2: 构建基础 CoalValue 对象 |
| | | CoalValue coalValue = new CoalValue(); |
| | | BeanUtils.copyProperties(coalValue, pendingInventoryDto); |
| | | coalValue.setPlanId(pendingInventoryDto.getPId()); |
| | | List<Map<String, String>> fieldValue = pendingInventoryDto.getFieldValue(); |
| | | if (CollectionUtils.isEmpty(fieldValue)) { |
| | | throw new BaseException("字段值不能为空"); |
| | | } |
| | | // Step 3: 提前获取所有 field -> CoalField 映射,避免重复查库 |
| | | Set<String> allFields = fieldValue.stream() |
| | | .flatMap(map -> map.keySet().stream()) |
| | | .collect(Collectors.toSet()); |
| | | List<CoalField> coalFields = coalFieldMapper.selectList( |
| | | new LambdaQueryWrapper<CoalField>().in(CoalField::getFields, allFields)); |
| | | Map<String, String> fieldMap = coalFields.stream() |
| | | .collect(Collectors.toMap( |
| | | CoalField::getFields, |
| | | CoalField::getFieldName)); |
| | | // Step 4: 批量插入 |
| | | int i = 0; |
| | | for (Map<String, String> fieldMapEntry : fieldValue) { |
| | | for (Map.Entry<String, String> entry : fieldMapEntry.entrySet()) { |
| | | String key = entry.getKey(); |
| | | String value = entry.getValue(); |
| | | String fieldName = fieldMap.get(key); |
| | | if (fieldName == null) { |
| | | throw new BaseException("字段名不存在"); |
| | | } |
| | | coalValue.setId(null); // 清空 id,确保每次都是新记录 |
| | | coalValue.setCoalValue(value); |
| | | coalValue.setFields(key); |
| | | coalValue.setFieldName(fieldName); |
| | | i = coalValueMapper.insert(coalValue); |
| | | } |
| | | } |
| | | if (i > 0) { |
| | | BigDecimal quantity = pendingInventoryDto.getInventoryQuantity(); |
| | | PendingInventory pendingInventory = pendingInventoryMapper.selectById(pendingInventoryDto.getPId()); |
| | | if (pendingInventory == null) { |
| | | throw new BaseException("待入库记录不存在"); |
| | | } |
| | | BigDecimal left = pendingInventory.getInventoryQuantity().subtract(quantity); |
| | | if (left.compareTo(BigDecimal.ZERO) > 0) { |
| | | pendingInventory.setInventoryQuantity(left); |
| | | pendingInventoryMapper.updateById(pendingInventory); |
| | | } else { |
| | | pendingInventoryMapper.deleteById(pendingInventoryDto.getPId()); |
| | | } |
| | | officialInventoryMapper.delete(new LambdaQueryWrapper<OfficialInventory>().eq(OfficialInventory::getPendingId, pendingInventoryDto.getPId())); |
| | | OfficialInventory officialInventory = new OfficialInventory(); |
| | | BeanUtils.copyProperties(pendingInventory, officialInventory); |
| | | officialInventory.setId(null); |
| | | officialInventory.setPendingId(pendingInventoryDto.getPId()); |
| | | officialInventory.setInventoryQuantity(quantity); |
| | | officialInventoryMapper.insert(officialInventory); |
| | | } |
| | | return i; |
| | | } |
| | | } |