src/main/java/com/ruoyi/procurementrecord/controller/CustomStorageWarningRecordController.java
@@ -9,6 +9,15 @@
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import com.ruoyi.procurementrecord.pojo.CustomStorageWarningRecord;
import com.ruoyi.procurementrecord.pojo.CustomStorage;
import com.ruoyi.procurementrecord.mapper.CustomStorageWarningRecordMapper;
import com.ruoyi.procurementrecord.mapper.CustomStorageMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -20,10 +29,65 @@
    @Autowired
    private CustomStorageWarningRecordService customStorageWarningRecordService;
    @Autowired
    private CustomStorageWarningRecordMapper warningRecordMapper;
    @Autowired
    private CustomStorageMapper customStorageMapper;
    @GetMapping("/pageList")
    @Log(title = "仓储物流-库存预警-分页查询", businessType = BusinessType.OTHER)
    public AjaxResult pageList(Page page, ProcurementPageDto dto){
        return AjaxResult.success(customStorageWarningRecordService.listPage(page,dto));
    }
}
    /**
     * 生成采购建议 / 滞销处理提示(并记录到预警事件)
     */
    @PostMapping("/generateSuggestion")
    @Log(title = "仓储物流-库存预警-生成建议", businessType = BusinessType.UPDATE)
    public AjaxResult generateSuggestion(@RequestBody Map<String, Object> body) {
        Object idObj = body.get("id");
        if (idObj == null) return AjaxResult.error("参数不能为空");
        String type = body.get("type") == null ? "采购建议" : body.get("type").toString();
        Long targetId = Long.valueOf(idObj.toString());
        CustomStorageWarningRecord record = warningRecordMapper.selectById(targetId);
        if (record == null) {
            com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CustomStorageWarningRecord> w =
                    new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<>();
            w.eq(CustomStorageWarningRecord::getCustomStorageId, targetId.intValue()).last("limit 1");
            record = warningRecordMapper.selectOne(w);
        }
        if (record == null) {
            record = new CustomStorageWarningRecord();
            record.setCustomStorageId(targetId.intValue());
            record.setWarningStatus("已预警");
            record.setWarningTime(LocalDateTime.now());
            try {
                record.setTenantId(com.ruoyi.common.utils.SecurityUtils.getLoginUser().getTenantId());
            } catch (Exception ignored) {
            }
            warningRecordMapper.insert(record);
        }
        CustomStorage storage = record.getCustomStorageId() == null ? null : customStorageMapper.selectById(record.getCustomStorageId());
        String product = storage == null ? "未知商品"
                : (storage.getProductCategory() == null ? "" : storage.getProductCategory())
                  + (storage.getSpecificationModel() == null ? "" : " " + storage.getSpecificationModel());
        BigDecimal current = (storage == null || storage.getInboundNum() == null) ? BigDecimal.ZERO : storage.getInboundNum();
        BigDecimal warn = storage == null ? null : storage.getWarnNum();
        String content;
        if (type.contains("采购")) {
            BigDecimal suggest = (warn == null ? BigDecimal.ZERO : warn).subtract(current);
            if (suggest.compareTo(BigDecimal.ZERO) <= 0) suggest = BigDecimal.valueOf(10);
            content = String.format("【采购建议】%s 当前库存 %s,最低库存阈值 %s,建议采购 %s(建议结合近30天消耗量补货)",
                    product, current.toPlainString(), warn == null ? "-" : warn.toPlainString(), suggest.toPlainString());
        } else {
            content = String.format("【滞销处理提示】%s 当前库存 %s,周转缓慢/库存偏高,建议:①促销或内部领用 ②转作其他用途 ③与供应商协商退换货",
                    product, current.toPlainString());
        }
        record.setSuggestionType(type);
        record.setSuggestionContent(content);
        record.setSuggestionTime(LocalDateTime.now());
        warningRecordMapper.updateById(record);
        return AjaxResult.success(content);
    }}