liaobiaojie_branch
STAR 9 months ago
parent d16f9de2e6
commit 7a187c03db

@ -1,184 +1,219 @@
package com.yeqifu.bus.controller; package com.yeqifu.bus.controller;
// 导入必要的类和注解
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; // 用于条件查询的封装器
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage; // 分页结果封装类
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; // 分页类
import com.yeqifu.bus.entity.Goods; import com.yeqifu.bus.entity.Goods; // 商品实体类
import com.yeqifu.bus.entity.Provider; import com.yeqifu.bus.entity.Provider; // 供应商实体类
import com.yeqifu.bus.service.IGoodsService; import com.yeqifu.bus.service.IGoodsService; // 商品服务接口
import com.yeqifu.bus.service.IProviderService; import com.yeqifu.bus.service.IProviderService; // 供应商服务接口
import com.yeqifu.bus.vo.GoodsVo; import com.yeqifu.bus.vo.GoodsVo; // 商品视图对象
import com.yeqifu.sys.common.AppFileUtils; import com.yeqifu.sys.common.AppFileUtils; // 文件操作工具类
import com.yeqifu.sys.common.Constast; import com.yeqifu.sys.common.Constast; // 常量类
import com.yeqifu.sys.common.DataGridView; import com.yeqifu.sys.common.DataGridView; // 数据网格返回格式类
import com.yeqifu.sys.common.ResultObj; import com.yeqifu.sys.common.ResultObj; // 结果对象类
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils; // 字符串工具类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired; // 自动注入注解
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping; // 映射请求路径注解
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController; // REST 控制器注解
import java.util.List; import java.util.List; // Java 集合类
/** /**
* <p> *
* InnoDB free: 9216 kB; (`providerid`) REFER `warehouse/bus_provider`(`id`) *
* </p>
*
* @author luoyi-
* @since 2019-12-06
*/ */
@RestController @RestController // 标注为 REST 控制器,返回 JSON 数据
@RequestMapping("/goods") @RequestMapping("/goods") // 设置请求路径的统一前缀
public class GoodsController { public class GoodsController {
@Autowired @Autowired // 自动注入商品服务
private IGoodsService goodsService; private IGoodsService goodsService;
@Autowired @Autowired // 自动注入供应商服务
private IProviderService providerService; private IProviderService providerService;
/** /**
* *
* @param goodsVo * @param goodsVo
* @return * @return
*/ */
@RequestMapping("loadAllGoods") @RequestMapping("loadAllGoods") // 请求映射到 /goods/loadAllGoods
public DataGridView loadAllGoods(GoodsVo goodsVo) { public DataGridView loadAllGoods(GoodsVo goodsVo) {
// 创建分页对象,指定当前页和每页大小
IPage<Goods> page = new Page<Goods>(goodsVo.getPage(), goodsVo.getLimit()); IPage<Goods> page = new Page<Goods>(goodsVo.getPage(), goodsVo.getLimit());
// 构造查询条件
QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>(); QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
queryWrapper.eq(goodsVo.getProviderid()!=null&&goodsVo.getProviderid()!=0,"providerid",goodsVo.getProviderid()); queryWrapper.eq(goodsVo.getProviderid() != null && goodsVo.getProviderid() != 0, "providerid", goodsVo.getProviderid()); // 按供应商 ID 查询
queryWrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname()); queryWrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()), "goodsname", goodsVo.getGoodsname()); // 按商品名称模糊查询
queryWrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode()); queryWrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()), "productcode", goodsVo.getProductcode()); // 按商品编码模糊查询
queryWrapper.like(StringUtils.isNotBlank(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode()); queryWrapper.like(StringUtils.isNotBlank(goodsVo.getPromitcode()), "promitcode", goodsVo.getPromitcode()); // 按促销编码模糊查询
queryWrapper.like(StringUtils.isNotBlank(goodsVo.getDescription()),"description",goodsVo.getDescription()); queryWrapper.like(StringUtils.isNotBlank(goodsVo.getDescription()), "description", goodsVo.getDescription()); // 按描述模糊查询
queryWrapper.like(StringUtils.isNotBlank(goodsVo.getSize()),"size",goodsVo.getSize()); queryWrapper.like(StringUtils.isNotBlank(goodsVo.getSize()), "size", goodsVo.getSize()); // 按尺寸模糊查询
queryWrapper.orderByDesc("id"); queryWrapper.orderByDesc("id"); // 按 ID 降序排序
// 执行分页查询
goodsService.page(page, queryWrapper); goodsService.page(page, queryWrapper);
// 获取查询结果
List<Goods> records = page.getRecords(); List<Goods> records = page.getRecords();
for (Goods goods : records) { for (Goods goods : records) {
// 根据商品的供应商 ID 查询供应商信息,并设置供应商名称
Provider provider = providerService.getById(goods.getProviderid()); Provider provider = providerService.getById(goods.getProviderid());
if (null!=provider){ if (provider != null) {
goods.setProvidername(provider.getProvidername()); goods.setProvidername(provider.getProvidername());
} }
} }
// 返回分页数据和商品记录
return new DataGridView(page.getTotal(), page.getRecords()); return new DataGridView(page.getTotal(), page.getRecords());
} }
/** /**
* *
* @param goodsVo * @param goodsVo
* @return * @return
*/ */
@RequestMapping("addGoods") @RequestMapping("addGoods") // 请求映射到 /goods/addGoods
public ResultObj addGoods(GoodsVo goodsVo) { public ResultObj addGoods(GoodsVo goodsVo) {
try { try {
System.out.println("===================================="); System.out.println("====================================");
System.out.println(goodsVo.getGoodsimg()); System.out.println(goodsVo.getGoodsimg()); // 打印上传的商品图片路径
// 如果上传的图片路径是临时文件,则重命名为正式文件
if (goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().endsWith("_temp")) { if (goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().endsWith("_temp")) {
String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg()); String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg());
goodsVo.setGoodsimg(newName); goodsVo.setGoodsimg(newName); // 更新图片路径
} }
// 保存商品信息
goodsService.save(goodsVo); goodsService.save(goodsVo);
// 返回添加成功的结果
return ResultObj.ADD_SUCCESS; return ResultObj.ADD_SUCCESS;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈信息
return ResultObj.ADD_ERROR; return ResultObj.ADD_ERROR; // 返回添加失败的结果
} }
} }
/** /**
* *
* @param goodsVo * @param goodsVo
* @return * @return
*/ */
@RequestMapping("updateGoods") @RequestMapping("updateGoods") // 请求映射到 /goods/updateGoods
public ResultObj updateGoods(GoodsVo goodsVo) { public ResultObj updateGoods(GoodsVo goodsVo) {
try { try {
//商品图片不是默认图片 // 如果商品图片不是默认图片,则处理图片路径
if (!(goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().equals(Constast.DEFAULT_IMG_GOODS))) { if (!(goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().equals(Constast.DEFAULT_IMG_GOODS))) {
if (goodsVo.getGoodsimg().endsWith("_temp")) { if (goodsVo.getGoodsimg().endsWith("_temp")) {
String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg()); String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg()); // 重命名图片文件
goodsVo.setGoodsimg(newName); goodsVo.setGoodsimg(newName);
//删除原先的图片
// 删除原先的图片文件
String oldPath = goodsService.getById(goodsVo.getId()).getGoodsimg(); String oldPath = goodsService.getById(goodsVo.getId()).getGoodsimg();
AppFileUtils.removeFileByPath(oldPath); AppFileUtils.removeFileByPath(oldPath);
} }
} }
// 更新商品信息
goodsService.updateById(goodsVo); goodsService.updateById(goodsVo);
// 返回更新成功的结果
return ResultObj.UPDATE_SUCCESS; return ResultObj.UPDATE_SUCCESS;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈信息
return ResultObj.UPDATE_ERROR; return ResultObj.UPDATE_ERROR; // 返回更新失败的结果
} }
} }
/** /**
* *
* @param id id * @param id ID
* @return * @param goodsimg
* @return
*/ */
@RequestMapping("deleteGoods") @RequestMapping("deleteGoods") // 请求映射到 /goods/deleteGoods
public ResultObj deleteGoods(Integer id, String goodsimg) { public ResultObj deleteGoods(Integer id, String goodsimg) {
try { try {
//删除商品的图片 // 删除商品的图片文件
AppFileUtils.removeFileByPath(goodsimg); AppFileUtils.removeFileByPath(goodsimg);
// goodsService.removeById(id);
// 删除商品信息
goodsService.deleteGoodsById(id); goodsService.deleteGoodsById(id);
// 返回删除成功的结果
return ResultObj.DELETE_SUCCESS; return ResultObj.DELETE_SUCCESS;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈信息
return ResultObj.DELETE_ERROR; return ResultObj.DELETE_ERROR; // 返回删除失败的结果
} }
} }
/** /**
* *
* @return * @return
*/ */
@RequestMapping("loadAllGoodsForSelect") @RequestMapping("loadAllGoodsForSelect") // 请求映射到 /goods/loadAllGoodsForSelect
public DataGridView loadAllGoodsForSelect() { public DataGridView loadAllGoodsForSelect() {
// 构造查询条件,仅查询状态为可用的商品
QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>(); QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
queryWrapper.eq("available", Constast.AVAILABLE_TRUE); queryWrapper.eq("available", Constast.AVAILABLE_TRUE);
// 查询商品列表
List<Goods> list = goodsService.list(queryWrapper); List<Goods> list = goodsService.list(queryWrapper);
for (Goods goods : list) { for (Goods goods : list) {
// 设置供应商名称
Provider provider = providerService.getById(goods.getProviderid()); Provider provider = providerService.getById(goods.getProviderid());
if (null!=provider){ if (provider != null) {
goods.setProvidername(provider.getProvidername()); goods.setProvidername(provider.getProvidername());
} }
} }
// 返回商品列表
return new DataGridView(list); return new DataGridView(list);
} }
/** /**
* ID * ID
* @param providerid ID * @param providerid ID
* @return * @return
*/ */
@RequestMapping("loadGoodsByProviderId") @RequestMapping("loadGoodsByProviderId") // 请求映射到 /goods/loadGoodsByProviderId
public DataGridView loadGoodsByProviderId(Integer providerid) { public DataGridView loadGoodsByProviderId(Integer providerid) {
// 构造查询条件,仅查询状态为可用的商品
QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>(); QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
queryWrapper.eq("available", Constast.AVAILABLE_TRUE); queryWrapper.eq("available", Constast.AVAILABLE_TRUE);
queryWrapper.eq(providerid!=null,"providerid",providerid); queryWrapper.eq(providerid != null, "providerid", providerid); // 按供应商 ID 查询
// 查询商品列表
List<Goods> list = goodsService.list(queryWrapper); List<Goods> list = goodsService.list(queryWrapper);
for (Goods goods : list) { for (Goods goods : list) {
// 设置供应商名称
Provider provider = providerService.getById(goods.getProviderid()); Provider provider = providerService.getById(goods.getProviderid());
if (null!=provider){ if (provider != null) {
goods.setProvidername(provider.getProvidername()); goods.setProvidername(provider.getProvidername());
} }
} }
// 返回商品列表
return new DataGridView(list); return new DataGridView(list);
} }
@RequestMapping("loadAllWarningGoods") /**
*
* @return
*/
@RequestMapping("loadAllWarningGoods") // 请求映射到 /goods/loadAllWarningGoods
public DataGridView loadAllWarningGoods() { public DataGridView loadAllWarningGoods() {
// 查询所有库存预警的商品
List<Goods> goods = goodsService.loadAllWarning(); List<Goods> goods = goodsService.loadAllWarning();
// 返回预警商品的数量和列表
return new DataGridView((long) goods.size(), goods); return new DataGridView((long) goods.size(), goods);
} }
} }

Loading…
Cancel
Save