|
|
|
@ -0,0 +1,140 @@
|
|
|
|
|
package com.example.api.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.example.api.model.entity.Commodity; // 导入Commodity实体类,代表商品信息
|
|
|
|
|
import com.example.api.model.entity.Inventory; // 导入Inventory实体类,代表库存信息
|
|
|
|
|
import com.example.api.model.entity.InventoryRecord; // 导入InventoryRecord实体类,代表库存记录信息
|
|
|
|
|
import com.example.api.model.vo.CommodityChartVo; // 导入CommodityChartVo类,代表商品图表视图对象
|
|
|
|
|
import com.example.api.repository.CommodityRepository; // 导入CommodityRepository接口,用于访问商品数据
|
|
|
|
|
import com.example.api.repository.InventoryRecordRepository; // 导入InventoryRecordRepository接口,用于访问库存记录数据
|
|
|
|
|
import com.example.api.repository.InventoryRepository; // 导入InventoryRepository接口,用于访问库存数据
|
|
|
|
|
import com.example.api.service.InventoryRecordService; // 导入InventoryRecordService接口,定义库存记录服务
|
|
|
|
|
import com.example.api.utils.DataTimeUtil; // 导入DataTimeUtil工具类,用于处理日期和时间
|
|
|
|
|
import org.springframework.stereotype.Service; // 导入Service注解,标识服务组件
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; // 注解,用于注入Spring管理的Bean
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 库存记录服务实现类,提供库存记录相关的业务逻辑。
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class InventoryRecordServiceImpl implements InventoryRecordService {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private InventoryRepository inventoryRepository; // 注入库存仓库,用于数据库操作
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private CommodityRepository commodityRepository; // 注入商品仓库,用于数据库操作
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private InventoryRecordRepository recordRepository; // 注入库存记录仓库,用于数据库操作
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分析商品数据。
|
|
|
|
|
* @param type 库存记录类型,例如入库或出库
|
|
|
|
|
* @return 商品图表视图对象列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<CommodityChartVo> analyzeCommodity(Integer type) {
|
|
|
|
|
List<CommodityChartVo> result = new ArrayList<>();
|
|
|
|
|
List<InventoryRecord> all = recordRepository.findAllByType(type); // 根据类型查询所有库存记录
|
|
|
|
|
Map<String, Integer> map = new HashMap<>();
|
|
|
|
|
for (InventoryRecord r : all) {
|
|
|
|
|
// 统计每种商品的数量
|
|
|
|
|
if (map.containsKey(r.getName())) {
|
|
|
|
|
map.put(r.getName(), map.get(r.getName()) + r.getCount());
|
|
|
|
|
} else {
|
|
|
|
|
map.put(r.getName(), r.getCount());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (String key : map.keySet()) {
|
|
|
|
|
// 将统计结果转换为图表视图对象
|
|
|
|
|
result.add(new CommodityChartVo(map.get(key), key));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据仓库ID查询所有库存记录。
|
|
|
|
|
* @param wid 仓库ID
|
|
|
|
|
* @return 库存记录列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<InventoryRecord> findAllByWarehouseId(String wid) {
|
|
|
|
|
return recordRepository.findAllByWid(wid); // 根据仓库ID查询所有库存记录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据商品ID查询所有库存记录。
|
|
|
|
|
* @param cid 商品ID
|
|
|
|
|
* @return 库存记录列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<InventoryRecord> findAllByCommodityId(String cid) {
|
|
|
|
|
return recordRepository.findAllByCid(cid); // 根据商品ID查询所有库存记录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理出库操作。
|
|
|
|
|
* @param record 出库记录对象
|
|
|
|
|
* @return 保存后的出库记录对象
|
|
|
|
|
* @throws Exception 如果出库操作失败,则抛出异常
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public InventoryRecord out(InventoryRecord record) throws Exception {
|
|
|
|
|
// 查找当前商品在该仓库的库存
|
|
|
|
|
Inventory inventory = inventoryRepository.findByWidAndCid(record.getWid(), record.getCid());
|
|
|
|
|
// 查询结果为空或库存不足
|
|
|
|
|
if (inventory == null || inventory.getCount() < record.getCount()) {
|
|
|
|
|
throw new Exception("仓库内不存在该商品或库存数量不足");
|
|
|
|
|
}
|
|
|
|
|
// 更新商品和库存信息
|
|
|
|
|
Optional<Commodity> optional = commodityRepository.findById(record.getCid());
|
|
|
|
|
if (optional == null) {
|
|
|
|
|
throw new Exception("不存在的商品id");
|
|
|
|
|
}
|
|
|
|
|
Commodity commodity = optional.get();
|
|
|
|
|
commodity.setCount(commodity.getCount() - record.getCount());
|
|
|
|
|
commodityRepository.save(commodity);
|
|
|
|
|
inventory.setCount(inventory.getCount() - record.getCount());
|
|
|
|
|
inventoryRepository.save(inventory);
|
|
|
|
|
// 保存出库记录
|
|
|
|
|
record.setCreateAt(DataTimeUtil.getNowTimeString());
|
|
|
|
|
record.setType(-1);
|
|
|
|
|
return recordRepository.save(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理入库操作。
|
|
|
|
|
* @param record 入库记录对象
|
|
|
|
|
* @return 保存后的入库记录对象
|
|
|
|
|
* @throws Exception 如果入库操作失败,则抛出异常
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public InventoryRecord in(InventoryRecord record) throws Exception {
|
|
|
|
|
Optional<Commodity> optional = commodityRepository.findById(record.getCid());
|
|
|
|
|
if (optional == null) {
|
|
|
|
|
throw new Exception("不存在的商品id");
|
|
|
|
|
}
|
|
|
|
|
Commodity commodity = optional.get();
|
|
|
|
|
commodity.setCount(commodity.getCount() + record.getCount());
|
|
|
|
|
commodityRepository.save(commodity);
|
|
|
|
|
// 查找当前商品在该仓库的库存
|
|
|
|
|
Inventory inventory = inventoryRepository.findByWidAndCid(record.getWid(), record.getCid());
|
|
|
|
|
// 查询结果为空则新建库存信息
|
|
|
|
|
if (inventory == null) {
|
|
|
|
|
inventory = new Inventory();
|
|
|
|
|
inventory.setCid(record.getCid());
|
|
|
|
|
inventory.setWid(record.getWid());
|
|
|
|
|
inventory.setCount(0);
|
|
|
|
|
inventory.setName(record.getName());
|
|
|
|
|
}
|
|
|
|
|
inventory.setCount(inventory.getCount() + record.getCount());
|
|
|
|
|
inventoryRepository.save(inventory);
|
|
|
|
|
// 保存入库记录
|
|
|
|
|
record.setCreateAt(DataTimeUtil.getNowTimeString());
|
|
|
|
|
record.setType(+1);
|
|
|
|
|
return recordRepository.save(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|