|
|
|
@ -29,12 +29,12 @@ import java.util.UUID;
|
|
|
|
|
public class FlowerController {
|
|
|
|
|
|
|
|
|
|
@Value("${uploadPath}")
|
|
|
|
|
private String uploadPath;
|
|
|
|
|
private String uploadPath; // 注入文件上传路径配置
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private FlowersService flowerService;
|
|
|
|
|
private FlowersService flowerService; // 注入鲜花业务逻辑服务
|
|
|
|
|
@Resource
|
|
|
|
|
private FlowersDao flowersDao;
|
|
|
|
|
private FlowersDao flowersDao; // 注入鲜花数据访问对象
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分页查询鲜花商品
|
|
|
|
@ -42,7 +42,7 @@ public class FlowerController {
|
|
|
|
|
* @param page 页数
|
|
|
|
|
* @param searchKey 查询条件
|
|
|
|
|
* @param searchType 查询类型
|
|
|
|
|
* @return 商品
|
|
|
|
|
* @return 商品分页数据
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/find")
|
|
|
|
|
R find(
|
|
|
|
@ -51,13 +51,13 @@ public class FlowerController {
|
|
|
|
|
@RequestParam("searchType") String searchType
|
|
|
|
|
) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
List<Flower> flowers = flowerService.find(searchKey, searchType);
|
|
|
|
|
List<Flower> flowers = flowerService.find(searchKey, searchType); // 调用服务层查询鲜花列表
|
|
|
|
|
|
|
|
|
|
if (flowers == null) {
|
|
|
|
|
return r.setCode(2000);
|
|
|
|
|
return r.setCode(2000); // 返回查询失败状态码
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getResponse(flowers, page, Constant.SHOW_PAGE_SIZE, r);
|
|
|
|
|
return getResponse(flowers, page, Constant.SHOW_PAGE_SIZE, r); // 构建分页响应数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -70,13 +70,13 @@ public class FlowerController {
|
|
|
|
|
@RequestMapping("/findAll")
|
|
|
|
|
R findAll(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
List<Flower> flowers = flowerService.findAll(searchKey);
|
|
|
|
|
List<Flower> flowers = flowerService.findAll(searchKey); // 调用服务层查询所有鲜花
|
|
|
|
|
|
|
|
|
|
if (flowers == null) {
|
|
|
|
|
return r.setCode(2000);
|
|
|
|
|
return r.setCode(2000); // 返回查询失败状态码
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getResponse(flowers, page, Constant.PAGE_SIZE, r);
|
|
|
|
|
return getResponse(flowers, page, Constant.PAGE_SIZE, r); // 构建分页响应数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -89,12 +89,12 @@ public class FlowerController {
|
|
|
|
|
R create(@RequestBody Flower flower) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
|
|
|
|
|
int ans = flowerService.add(flower);
|
|
|
|
|
int ans = flowerService.add(flower); // 调用服务层添加鲜花
|
|
|
|
|
if (ans == 1) {
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.ADD_FLOWER_OK);
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.ADD_FLOWER_OK); // 返回添加成功信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.ADD_FLOWER_FAILED);
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.ADD_FLOWER_FAILED); // 返回添加失败信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -107,12 +107,12 @@ public class FlowerController {
|
|
|
|
|
R update(@RequestBody Flower flower) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
|
|
|
|
|
int ans = flowerService.update(flower);
|
|
|
|
|
int ans = flowerService.update(flower); // 调用服务层更新鲜花信息
|
|
|
|
|
if (ans >= 0) {
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_FLOWER_OK);
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_FLOWER_OK); // 返回更新成功信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_FLOWER_FAILED);
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_FLOWER_FAILED); // 返回更新失败信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -125,10 +125,10 @@ public class FlowerController {
|
|
|
|
|
R changeState(@RequestBody Flower flower) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
|
|
|
|
|
flowersDao.changeState(flower);
|
|
|
|
|
String msg = flower.getState() == 1 ? HttpMsg.GOODS_UP_OK : HttpMsg.GOODS_DOWN_OK;
|
|
|
|
|
flowersDao.changeState(flower); // 直接调用DAO层修改商品状态
|
|
|
|
|
String msg = flower.getState() == 1 ? HttpMsg.GOODS_UP_OK : HttpMsg.GOODS_DOWN_OK; // 判断上下架状态
|
|
|
|
|
|
|
|
|
|
return r.setCode(2000).setMsg(msg);
|
|
|
|
|
return r.setCode(2000).setMsg(msg); // 返回操作成功信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -144,16 +144,16 @@ public class FlowerController {
|
|
|
|
|
// 只接收 jpg/png 图片
|
|
|
|
|
String filename = file.getOriginalFilename();
|
|
|
|
|
if (!filename.endsWith(".jpg") && !filename.endsWith(".png")) {
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.ERROR_FILE_TYPE);
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.ERROR_FILE_TYPE); // 返回文件类型错误信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String img_guid = UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
|
|
|
|
|
String img_guid = UUID.randomUUID().toString().replaceAll("-", "") + ".jpg"; // 生成唯一图片文件名
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
savePic(file.getInputStream(), img_guid);
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK).setData(img_guid);
|
|
|
|
|
savePic(file.getInputStream(), img_guid); // 保存图片文件
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK).setData(img_guid); // 返回上传成功信息
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED);
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED); // 返回上传失败信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -169,12 +169,12 @@ public class FlowerController {
|
|
|
|
|
R updateImgGuid(@RequestParam("guid") String guid, @RequestParam("id") int id) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
|
|
|
|
|
int ans = flowerService.updateImg(guid, id);
|
|
|
|
|
int ans = flowerService.updateImg(guid, id); // 调用服务层更新图片GUID
|
|
|
|
|
if (ans == 1) {
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK);
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK); // 返回更新成功信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED);
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED); // 返回更新失败信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -187,14 +187,19 @@ public class FlowerController {
|
|
|
|
|
R delete(@RequestParam("id") int id) {
|
|
|
|
|
R r = new R();
|
|
|
|
|
|
|
|
|
|
int ans = flowerService.delete(id);
|
|
|
|
|
int ans = flowerService.delete(id); // 调用服务层删除鲜花
|
|
|
|
|
if (ans == 1) {
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.DELETE_FLOWER_OK);
|
|
|
|
|
return r.setCode(2000).setMsg(HttpMsg.DELETE_FLOWER_OK); // 返回删除成功信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.DELETE_FLOWER_FAILED);
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.DELETE_FLOWER_FAILED); // 返回删除失败信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存图片文件到指定路径
|
|
|
|
|
* @param inputStream 输入流
|
|
|
|
|
* @param fileName 文件名
|
|
|
|
|
*/
|
|
|
|
|
private void savePic(InputStream inputStream, String fileName) {
|
|
|
|
|
OutputStream os = null;
|
|
|
|
|
try {
|
|
|
|
@ -207,38 +212,48 @@ public class FlowerController {
|
|
|
|
|
os = new FileOutputStream(new File(path + fileName));
|
|
|
|
|
// 开始读取
|
|
|
|
|
while ((len = inputStream.read(bs)) != -1) {
|
|
|
|
|
os.write(bs, 0, len);
|
|
|
|
|
os.write(bs, 0, len); // 将数据写入文件
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
|
|
} finally {
|
|
|
|
|
// 完毕,关闭所有链接
|
|
|
|
|
try {
|
|
|
|
|
os.close();
|
|
|
|
|
inputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
e.printStackTrace(); // 打印关闭流异常信息
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构建分页响应数据
|
|
|
|
|
* @param flowers 鲜花列表
|
|
|
|
|
* @param page 当前页码
|
|
|
|
|
* @param pageSize 每页大小
|
|
|
|
|
* @param r 响应对象
|
|
|
|
|
* @return 分页数据
|
|
|
|
|
*/
|
|
|
|
|
private R getResponse(List<Flower> flowers, int page, int pageSize, R r) {
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
// 计算当前页数据
|
|
|
|
|
List<Flower> items = flowers.size() >= page * pageSize ?
|
|
|
|
|
flowers.subList((page - 1) * pageSize, page * pageSize)
|
|
|
|
|
: flowers.subList((page - 1) * pageSize, flowers.size());
|
|
|
|
|
// 计算总页数
|
|
|
|
|
int len = flowers.size() % pageSize == 0 ? flowers.size() / pageSize
|
|
|
|
|
: (flowers.size() / pageSize + 1);
|
|
|
|
|
// 处理图片路径
|
|
|
|
|
for (Flower item : items) {
|
|
|
|
|
if (item.getImg_guid() == null) {
|
|
|
|
|
item.setImg_guid(Constant.DEFAULT_IMG);
|
|
|
|
|
item.setImg_guid(Constant.DEFAULT_IMG); // 设置默认图片
|
|
|
|
|
}
|
|
|
|
|
item.setImg_guid(Constant.IMG_USE_PATH + item.getImg_guid());
|
|
|
|
|
item.setImg_guid(Constant.IMG_USE_PATH + item.getImg_guid()); // 添加图片访问路径前缀
|
|
|
|
|
}
|
|
|
|
|
map.put("items", items);
|
|
|
|
|
map.put("len", len);
|
|
|
|
|
return r.setCode(2000).setData(map);
|
|
|
|
|
map.put("items", items); // 分页数据
|
|
|
|
|
map.put("len", len); // 总页数
|
|
|
|
|
return r.setCode(2000).setData(map); // 返回分页响应
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|