货物包增长预测功能代码框架

This commit is contained in:
2026-05-26 08:39:08 +08:00
parent c0fed06eaa
commit 32fce665ea
6 changed files with 521 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
package com.rj.controller;
import com.rj.entity.LbGoods;
import com.rj.service.ILbGoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/lbGoods")
@Tag(name = "LB 货品", description = "lb_goods 增删改查与分页")
public class LbGoodsController {
@Autowired
private ILbGoodsService lbGoodsService;
@PostMapping("/add")
@Operation(summary = "新增")
public ResponseEntity<Map<String, Object>> add(
@Parameter(description = "实体id 为外部商品 id需调用方指定", required = true)
@RequestBody LbGoods entity) {
Map<String, Object> result = lbGoodsService.add(entity);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);
}
return ResponseEntity.badRequest().body(result);
}
@PutMapping("/update")
@Operation(summary = "编辑")
public ResponseEntity<Map<String, Object>> update(
@Parameter(description = "实体", required = true) @RequestBody LbGoods entity) {
Map<String, Object> result = lbGoodsService.update(entity);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);
}
return ResponseEntity.badRequest().body(result);
}
@DeleteMapping("/delete/{id}")
@Operation(summary = "删除")
public ResponseEntity<Map<String, Object>> delete(
@Parameter(description = "主键(商品 id", required = true) @PathVariable Long id) {
Map<String, Object> result = lbGoodsService.deleteById(id);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);
}
return ResponseEntity.badRequest().body(result);
}
@GetMapping("/get/{id}")
@Operation(summary = "根据ID查询")
public ResponseEntity<Map<String, Object>> getById(
@Parameter(description = "商品 id", required = true) @PathVariable Long id) {
LbGoods data = lbGoodsService.getById(id);
if (data == null) {
return ResponseEntity.badRequest().body(Map.of("success", false, "message", "记录不存在"));
}
return ResponseEntity.ok(Map.of("success", true, "message", "查询成功", "data", data));
}
@GetMapping("/list")
@Operation(summary = "分页查询")
public ResponseEntity<Map<String, Object>> list(
@RequestParam(defaultValue = "1") Integer current,
@RequestParam(defaultValue = "10") Integer size,
@RequestParam(required = false) String tenantId,
@RequestParam(required = false) Long oldId,
@RequestParam(required = false) Long userId,
@RequestParam(required = false) String title,
@RequestParam(required = false) Long sellerId,
@Parameter(description = "是否展示0否 1是") @RequestParam(required = false) Integer isShow,
@RequestParam(required = false) Integer status,
@Parameter(description = "创建开始时间格式yyyy-MM-dd HH:mm:ss")
@RequestParam(required = false) String createdAtStart,
@Parameter(description = "创建结束时间格式yyyy-MM-dd HH:mm:ss")
@RequestParam(required = false) String createdAtEnd,
@Parameter(description = "更新开始时间格式yyyy-MM-dd HH:mm:ss")
@RequestParam(required = false) String updatedAtStart,
@Parameter(description = "更新结束时间格式yyyy-MM-dd HH:mm:ss")
@RequestParam(required = false) String updatedAtEnd) {
Map<String, Object> result = lbGoodsService.pageQuery(
current,
size,
tenantId,
oldId,
userId,
title,
sellerId,
isShow,
status,
createdAtStart,
createdAtEnd,
updatedAtStart,
updatedAtEnd);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);
}
if (result.get("message") != null && result.get("message").toString().contains("格式错误")) {
return ResponseEntity.badRequest().body(result);
}
return ResponseEntity.internalServerError().body(result);
}
}

View File

@@ -0,0 +1,83 @@
package com.rj.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("lb_goods")
@Schema(description = "货品表")
@JsonIgnoreProperties(ignoreUnknown = true)
public class LbGoods implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.INPUT)
@Schema(description = "商品ID外部系统主键")
private Long id;
@TableField("tenant_id")
@Schema(description = "租户ID")
private String tenantId;
@TableField("old_id")
@Schema(description = "旧商品ID迁移/关联用)")
private Long oldId;
@TableField("user_id")
@Schema(description = "所属用户ID")
private Long userId;
@TableField("title")
@Schema(description = "商品标题")
private String title;
@TableField("image")
@Schema(description = "商品图片路径")
private String image;
@TableField("price")
@Schema(description = "单价")
private BigDecimal price;
@TableField("total_money")
@Schema(description = "总金额")
private BigDecimal totalMoney;
@TableField("quantity")
@Schema(description = "数量描述≈6盒")
private String quantity;
@TableField("seller_id")
@Schema(description = "卖家用户ID")
private Long sellerId;
@TableField("is_show")
@Schema(description = "是否展示0否 1是")
private Integer isShow;
@TableField("status")
@Schema(description = "状态")
private Integer status;
@TableField("created_at")
@Schema(description = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
@TableField("updated_at")
@Schema(description = "更新时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
}

View File

@@ -0,0 +1,9 @@
package com.rj.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rj.entity.LbGoods;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface LbGoodsMapper extends BaseMapper<LbGoods> {
}

View File

@@ -0,0 +1,30 @@
package com.rj.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.rj.entity.LbGoods;
import java.util.Map;
public interface ILbGoodsService extends IService<LbGoods> {
Map<String, Object> add(LbGoods entity);
Map<String, Object> update(LbGoods entity);
Map<String, Object> deleteById(Long id);
Map<String, Object> pageQuery(
Integer current,
Integer size,
String tenantId,
Long oldId,
Long userId,
String title,
Long sellerId,
Integer isShow,
Integer status,
String createdAtStart,
String createdAtEnd,
String updatedAtStart,
String updatedAtEnd);
}

View File

@@ -0,0 +1,254 @@
package com.rj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.entity.LbGoods;
import com.rj.mapper.LbGoodsMapper;
import com.rj.service.ILbGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> implements ILbGoodsService {
private static final DateTimeFormatter DATETIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public Map<String, Object> add(LbGoods entity) {
Map<String, Object> result = new HashMap<>();
try {
if (entity.getId() == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
if (entity.getTenantId() == null || entity.getTenantId().trim().isEmpty()) {
result.put("success", false);
result.put("message", "tenantId不能为空");
return result;
}
entity.setTenantId(entity.getTenantId().trim());
if (this.getById(entity.getId()) != null) {
result.put("success", false);
result.put("message", "该商品 id 已存在");
return result;
}
applyDefaults(entity);
LocalDateTime now = LocalDateTime.now();
if (entity.getCreatedAt() == null) {
entity.setCreatedAt(now);
}
if (entity.getUpdatedAt() == null) {
entity.setUpdatedAt(now);
}
boolean ok = this.save(entity);
result.put("success", ok);
result.put("message", ok ? "新增成功" : "新增失败");
if (ok) {
result.put("data", this.getById(entity.getId()));
}
return result;
} catch (Exception e) {
log.error("货品新增异常", e);
result.put("success", false);
result.put("message", "新增异常:" + e.getMessage());
return result;
}
}
@Override
public Map<String, Object> update(LbGoods entity) {
Map<String, Object> result = new HashMap<>();
try {
if (entity.getId() == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
if (this.getById(entity.getId()) == null) {
result.put("success", false);
result.put("message", "记录不存在");
return result;
}
if (entity.getTenantId() != null) {
entity.setTenantId(entity.getTenantId().trim());
}
entity.setUpdatedAt(LocalDateTime.now());
boolean ok = this.updateById(entity);
result.put("success", ok);
result.put("message", ok ? "编辑成功" : "编辑失败");
if (ok) {
result.put("data", this.getById(entity.getId()));
}
return result;
} catch (Exception e) {
log.error("货品编辑异常", e);
result.put("success", false);
result.put("message", "编辑异常:" + e.getMessage());
return result;
}
}
@Override
public Map<String, Object> deleteById(Long id) {
Map<String, Object> result = new HashMap<>();
try {
if (id == null) {
result.put("success", false);
result.put("message", "id不能为空");
return result;
}
boolean ok = this.removeById(id);
result.put("success", ok);
result.put("message", ok ? "删除成功" : "删除失败");
return result;
} catch (Exception e) {
log.error("货品删除异常", e);
result.put("success", false);
result.put("message", "删除异常:" + e.getMessage());
return result;
}
}
@Override
public Map<String, Object> pageQuery(
Integer current,
Integer size,
String tenantId,
Long oldId,
Long userId,
String title,
Long sellerId,
Integer isShow,
Integer status,
String createdAtStart,
String createdAtEnd,
String updatedAtStart,
String updatedAtEnd) {
Map<String, Object> result = new HashMap<>();
try {
if (current == null || current < 1) {
current = 1;
}
if (size == null || size < 1) {
size = 10;
}
LambdaQueryWrapper<LbGoods> w = new LambdaQueryWrapper<>();
if (tenantId != null && !tenantId.trim().isEmpty()) {
w.eq(LbGoods::getTenantId, tenantId.trim());
}
if (oldId != null) {
w.eq(LbGoods::getOldId, oldId);
}
if (userId != null) {
w.eq(LbGoods::getUserId, userId);
}
if (title != null && !title.trim().isEmpty()) {
w.like(LbGoods::getTitle, title.trim());
}
if (sellerId != null) {
w.eq(LbGoods::getSellerId, sellerId);
}
if (isShow != null) {
w.eq(LbGoods::getIsShow, isShow);
}
if (status != null) {
w.eq(LbGoods::getStatus, status);
}
LocalDateTime createdStart = parseDateTime(createdAtStart);
if (createdAtStart != null && !createdAtStart.trim().isEmpty() && createdStart == null) {
result.put("success", false);
result.put("message", "createdAtStart 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
LocalDateTime createdEnd = parseDateTime(createdAtEnd);
if (createdAtEnd != null && !createdAtEnd.trim().isEmpty() && createdEnd == null) {
result.put("success", false);
result.put("message", "createdAtEnd 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
if (createdStart != null) {
w.ge(LbGoods::getCreatedAt, createdStart);
}
if (createdEnd != null) {
w.le(LbGoods::getCreatedAt, createdEnd);
}
LocalDateTime updatedStart = parseDateTime(updatedAtStart);
if (updatedAtStart != null && !updatedAtStart.trim().isEmpty() && updatedStart == null) {
result.put("success", false);
result.put("message", "updatedAtStart 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
LocalDateTime updatedEnd = parseDateTime(updatedAtEnd);
if (updatedAtEnd != null && !updatedAtEnd.trim().isEmpty() && updatedEnd == null) {
result.put("success", false);
result.put("message", "updatedAtEnd 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
return result;
}
if (updatedStart != null) {
w.ge(LbGoods::getUpdatedAt, updatedStart);
}
if (updatedEnd != null) {
w.le(LbGoods::getUpdatedAt, updatedEnd);
}
w.orderByDesc(LbGoods::getUpdatedAt).orderByDesc(LbGoods::getId);
Page<LbGoods> page = this.page(new Page<>(current, size), w);
result.put("success", true);
result.put("message", "查询成功");
result.put("data", page.getRecords());
result.put("total", page.getTotal());
result.put("current", page.getCurrent());
result.put("size", page.getSize());
result.put("pages", page.getPages());
return result;
} catch (Exception e) {
log.error("货品分页查询异常", e);
result.put("success", false);
result.put("message", "查询异常:" + e.getMessage());
return result;
}
}
private static void applyDefaults(LbGoods entity) {
if (entity.getPrice() == null) {
entity.setPrice(BigDecimal.ZERO);
}
if (entity.getTotalMoney() == null) {
entity.setTotalMoney(BigDecimal.ZERO);
}
if (entity.getIsShow() == null) {
entity.setIsShow(1);
}
if (entity.getStatus() == null) {
entity.setStatus(1);
}
}
private static LocalDateTime parseDateTime(String text) {
if (text == null || text.trim().isEmpty()) {
return null;
}
try {
return LocalDateTime.parse(text.trim(), DATETIME_FMT);
} catch (DateTimeParseException e) {
return null;
}
}
}

32
src/main/sql/lb_goods.sql Normal file
View File

@@ -0,0 +1,32 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `lb_goods`;
CREATE TABLE `lb_goods` (
`id` BIGINT NOT NULL COMMENT '商品ID外部系统主键',
`tenant_id` CHAR(36) DEFAULT NULL COMMENT '租户id',
`old_id` BIGINT DEFAULT NULL COMMENT '旧商品ID迁移/关联用)',
`user_id` BIGINT DEFAULT NULL COMMENT '所属用户ID',
`title` VARCHAR(200) DEFAULT NULL COMMENT '商品标题',
`image` VARCHAR(512) DEFAULT NULL COMMENT '商品图片路径',
`price` DECIMAL(12, 2) NOT NULL DEFAULT 0.00 COMMENT '单价',
`total_money` DECIMAL(12, 2) NOT NULL DEFAULT 0.00 COMMENT '总金额',
`quantity` VARCHAR(64) DEFAULT NULL COMMENT '数量描述≈6盒',
`seller_id` BIGINT DEFAULT NULL COMMENT '卖家用户ID',
`is_show` TINYINT NOT NULL DEFAULT 1 COMMENT '是否展示0否 1是',
`status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态',
`created_at` DATETIME DEFAULT NULL COMMENT '创建时间',
`updated_at` DATETIME DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_old_id` (`old_id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE,
KEY `idx_seller_id` (`seller_id`) USING BTREE,
KEY `idx_status_is_show` (`status`, `is_show`) USING BTREE,
KEY `idx_created_at` (`created_at`) USING BTREE
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '货品表'
ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;