2026-06-04 feb86efc4e8a8d2da00e4832bcd81825726617bf
src/main/java/com/ruoyi/stock/controller/StockInventoryController.java
@@ -1,7 +1,32 @@
package com.ruoyi.stock.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.device.service.IDeviceLedgerService;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.http.service.impl.RealTimeEnergyConsumptionServiceImpl;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockIotRealtimeDto;
import com.ruoyi.stock.execl.StockInventoryExportData;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.service.StockInventoryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -13,6 +38,169 @@
 */
@RestController
@RequestMapping("/stockInventory")
@Tag(name = "库存表")
@AllArgsConstructor
public class StockInventoryController {
    private StockInventoryService stockInventoryService;
    private IDeviceLedgerService deviceLedgerService;
    private RealTimeEnergyConsumptionServiceImpl realTimeEnergyConsumptionService;
    @GetMapping("/pagestockInventory")
    @Operation(summary = "分页查询库存")
    public R pagestockInventory(Page page, StockInventoryDto stockInventoryDto) {
        IPage<StockInventoryDto> stockInventoryDtoIPage = stockInventoryService.pagestockInventory(page, stockInventoryDto);
        return R.ok(stockInventoryDtoIPage);
    }
    @GetMapping("/pageListCombinedStockInventory")
    @Operation(summary = "分页查询联合库存列表")
    public R pageListCombinedStockInventory(Page page, StockInventoryDto stockInventoryDto) {
        IPage<StockInventoryDto> stockInventoryDtoIPage = stockInventoryService.pageListCombinedStockInventory(page, stockInventoryDto);
        return R.ok(stockInventoryDtoIPage);
    }
    /**
     * 查询对应批号和数量
     * @param page
     * @param stockInventoryDto
     * @return
     */
    @GetMapping("/getBatchNoQty")
    @Operation(summary = "查询对应批号和数量")
    public R getBatchNoQty(Page page, StockInventoryDto stockInventoryDto) {
        IPage<StockInventoryDto> stockInventoryDtoIPage = stockInventoryService.getBatchNoQty(page, stockInventoryDto);
        return R.ok(stockInventoryDtoIPage);
    }
    @PostMapping("/addstockInventory")
    @Operation(summary = "新增库存")
    public R addstockInventory(@RequestBody StockInventoryDto stockInventoryDto) {
        stockInventoryDto.setRecordType(String.valueOf(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode()));
        stockInventoryDto.setRecordId(0L);
        return R.ok(stockInventoryService.addstockInventory(stockInventoryDto));
    }
    @PostMapping("/subtractStockInventory")
    @Operation(summary = "扣减库存")
    public R subtractStockInventory(@RequestBody StockInventoryDto stockInventoryDto) {
        stockInventoryDto.setRecordType(String.valueOf(StockOutQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_OUT.getCode()));
        stockInventoryDto.setRecordId(0L);
        return R.ok(stockInventoryService.subtractStockInventory(stockInventoryDto));
    }
    @PostMapping("/addStockInRecordOnly")
    @Operation(summary = "新增入库记录(仅创建记录,不调整库存)")
    public R addStockInRecordOnly(@RequestBody StockInventoryDto stockInventoryDto) {
        stockInventoryDto.setRecordType(String.valueOf(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode()));
        stockInventoryDto.setRecordId(0L);
        return R.ok(stockInventoryService.addStockInRecordOnly(stockInventoryDto));
    }
    @PostMapping("/addStockOutRecordOnly")
    @Operation(summary = "新增出库记录(仅创建记录,不调整库存)")
    public R addStockOutRecordOnly(@RequestBody StockInventoryDto stockInventoryDto) {
        stockInventoryDto.setRecordType(String.valueOf(StockOutQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_OUT.getCode()));
        stockInventoryDto.setRecordId(0L);
        return R.ok(stockInventoryService.addStockOutRecordOnly(stockInventoryDto));
    }
    @PostMapping("/importStockInventory")
    @Operation(summary = "导入库存")
    public R importStockInventory(MultipartFile file) {
        return stockInventoryService.importStockInventory(file);
    }
    @Log(title = "下载库存导入模板", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadStockInventory")
    public void downloadStockInventory(HttpServletResponse response) {
        List<StockInventoryExportData> list = new ArrayList<>();
        ExcelUtil<StockInventoryExportData> util = new ExcelUtil<>(StockInventoryExportData.class);
        util.exportExcel(response, list, "库存模板");
    }
    @PostMapping("/exportStockInventory")
    @Operation(summary = "导出库存")
    public void exportStockInventory(HttpServletResponse response, StockInventoryDto stockInventoryDto) {
        stockInventoryService.exportStockInventory(response, stockInventoryDto);
    }
    @GetMapping("/stockInventoryPage")
    @Operation(summary = "库存报表查询")
    public R stockInventoryPage(Page page, StockInventoryDto stockInventoryDto) {
        return R.ok(stockInventoryService.stockInventoryPage(stockInventoryDto,page));
    }
    @GetMapping("/stockInAndOutRecord")
    @Operation(summary = "统计各个产品的入库和出库记录")
    public R stockInAndOutRecord(StockInventoryDto stockInventoryDto,Page page) {
        return R.ok(stockInventoryService.stockInAndOutRecord(stockInventoryDto,page));
    }
    @PostMapping("/frozenStock")
    @Operation(summary = "冻结库存")
    public R frozenStock(@RequestBody StockInventoryDto stockInventoryDto) {
        return R.ok(stockInventoryService.frozenStock(stockInventoryDto));
    }
    @PostMapping("/thawStock")
    @Operation(summary = "解冻库存")
    public R thawStock(@RequestBody StockInventoryDto stockInventoryDto) {
        return R.ok(stockInventoryService.thawStock(stockInventoryDto));
    }
    @GetMapping("/getByModelId")
    @Operation(summary = "根据产品规格ID获取入库记录")
    public R getByModelId(Long productModelId) {
        return R.ok(stockInventoryService.getByModelId(productModelId));
    }
    @PostMapping("/iotRealtime")
    @Operation(summary = "获取库存绑定的物联设备实时数采数据")
    @Log(title = "库存物联设备实时数采", businessType = BusinessType.OTHER)
    public R iotRealtime(@RequestBody List<Long> ids) {
        StockIotRealtimeDto result = new StockIotRealtimeDto();
        if (ids.isEmpty()) {
            result.setDevices(Collections.emptyList());
            return R.ok(result);
        }
        List<DeviceLedger> devices = deviceLedgerService.listByIds(ids);
        List<String> guidList = devices.stream()
                .map(DeviceLedger::getExternalCode)
                .filter(StringUtils::isNotEmpty)
                .collect(Collectors.toList());
        Map<String, Map<String, String>> realTimeDataMap = new HashMap<>();
        if (!guidList.isEmpty()) {
            List<Map<String, String>> realTimeList = realTimeEnergyConsumptionService.getRealData(guidList);
            for (Map<String, String> item : realTimeList) {
                String guid = item.get("guid");
                if (StringUtils.isNotEmpty(guid)) {
                    realTimeDataMap.put(guid.trim(), item);
                }
            }
        }
        List<Map<String, String>> deviceDataList = devices.stream().map(device -> {
            Map<String, String> data = new LinkedHashMap<>();
            data.put("deviceId", String.valueOf(device.getId()));
            data.put("deviceName", device.getDeviceName() != null ? device.getDeviceName() : "");
            data.put("deviceModel", device.getDeviceModel() != null ? device.getDeviceModel() : "");
            data.put("externalCode", device.getExternalCode() != null ? device.getExternalCode() : "");
            data.put("storageLocation", device.getStorageLocation() != null ? device.getStorageLocation() : "");
            Map<String, String> rt = realTimeDataMap.getOrDefault(
                    device.getExternalCode() != null ? device.getExternalCode().trim() : "",
                    Collections.emptyMap());
            data.putAll(rt);
            return data;
        }).collect(Collectors.toList());
        result.setDevices(deviceDataList);
        return R.ok(result);
    }
}