SpringBoot电商项目进阶Day5
学习并且完成了对列表元素的校验。@Validated ValidList等
对对于订单状态提示和IP地址写法进行了升级
同时完成了订单数变化趋势图的接口,并且解决了时区异常的情况
package com.imooc.mall.controller;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.Service.ProductService;
import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.common.ValidList;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.model.request.AddProductReq;
import com.imooc.mall.model.request.UpdateProductReq;
import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;
/**
* 描述: 后台商品管理Controller
*/
@RestController
@Validated
public class ProductAdminController {
@Autowired
ProductService productService;
@Value("${file.upload.uri}")
String uri;
@PostMapping("/admin/product/add")
public ApiRestResponse addProduct(@Valid @RequestBody AddProductReq addProductReq) {
productService.add(addProductReq);
return ApiRestResponse.success();
}
@PostMapping("/admin/upload/file")
public ApiRestResponse upload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//生成文件名称UUID
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;
//创建文件
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
createFile(file, fileDirectory, destFile);
String address = uri;
return ApiRestResponse.success("http://"+address+"/images/"+newFileName);
}
private URI getHost(URI uri) {
URI effectiveURI;
try {
effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
} catch (URISyntaxException e) {
effectiveURI=null;
}
return effectiveURI;
}
@ApiOperation("后台更新商品")
@PostMapping("/admin/product/update")
public ApiRestResponse updateProduct(@Valid @RequestBody UpdateProductReq updateProductReq) {
Product product = new Product();
BeanUtils.copyProperties(updateProductReq, product);
productService.update(product);
return ApiRestResponse.success();
}
@ApiOperation("后台删除商品")
@PostMapping("/admin/product/delete")
public ApiRestResponse deleteProduct(@RequestParam Integer id) {
productService.delete(id);
return ApiRestResponse.success();
}
@ApiOperation("后台批量上下架接口")
@PostMapping("/admin/product/batchUpdateSellStatus")
public ApiRestResponse batchUpdateSellStatus(@RequestParam Integer[] ids,@RequestParam Integer sellStatus) {
productService.batchUpdateSellStatus(ids, sellStatus);
return ApiRestResponse.success();
}
@ApiOperation("后台商品列表接口")
@GetMapping("/admin/product/list")
public ApiRestResponse list(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
PageInfo pageInfo = productService.listForAdmin(pageNum, pageSize);
return ApiRestResponse.success(pageInfo);
}
@ApiOperation("后台批量上传商品接口")
@PostMapping("/admin/upload/product")
public ApiRestResponse uploadProduct(@RequestParam("file") MultipartFile multipartFile) throws IOException {
String fileName = multipartFile.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;
//创建文件
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
createFile(multipartFile, fileDirectory, destFile);
productService.addProductByExcel(destFile);
return ApiRestResponse.success();
}
@PostMapping("/admin/upload/image")
public ApiRestResponse uploadImage(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//生成文件名称UUID
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;
//创建文件
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
createFile(file, fileDirectory, destFile);
Thumbnails.of(destFile).size(Constant.IMAGE_SIZE, Constant.IMAGE_SIZE).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(Constant.FILE_UPLOAD_DIR + Constant.WATER_MARK_JPG)), Constant.IMAGE_OPACITY).toFile(new File(Constant.FILE_UPLOAD_DIR + newFileName));
String address = uri;
return ApiRestResponse.success("http://"+address+ "/images/" + newFileName);
}
private void createFile(@RequestParam("file") MultipartFile file, File fileDirectory, File destFile) {
if (!fileDirectory.exists()) {
if (!fileDirectory.mkdir()) {
throw new ImoocMallException(ImoocMallExceptionEnum.MKDIR_FAILED);
}
}
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}
}
@ApiOperation("后台批量更新商品")
@PostMapping("/admin/product/batchUpdate")
public ApiRestResponse batchUpdateProduct(@Valid @RequestBody List<UpdateProductReq> updateProductReqList) {
for (int i = 0; i < updateProductReqList.size(); i++) {
UpdateProductReq updateProductReq = updateProductReqList.get(i);
//方法一,手动校验
if (updateProductReq.getPrice() < 1) {
throw new ImoocMallException(ImoocMallExceptionEnum.PRICE_TOO_LOW);
}
if (updateProductReq.getStock() > 10000) {
throw new ImoocMallException(ImoocMallExceptionEnum.STOCK_TOO_MANY);
}
Product product = new Product();
BeanUtils.copyProperties(updateProductReqList, product);
productService.update(product);
}
return ApiRestResponse.success();
}
@ApiOperation("后台批量更新商品,ValidList验证")
@PostMapping("/admin/product/batchUpdate2")
public ApiRestResponse batchUpdateProduct2(@Valid @RequestBody ValidList<UpdateProductReq> updateProductReqList) {
for (int i = 0; i < updateProductReqList.size(); i++) {
UpdateProductReq updateProductReq = updateProductReqList.get(i);
Product product = new Product();
BeanUtils.copyProperties(updateProductReq, product);
productService.update(product);
}
return ApiRestResponse.success();
}
@ApiOperation("后台批量更新商品,@Validated验证")
@PostMapping("/admin/product/batchUpdate3")
public ApiRestResponse batchUpdateProduct3(@Valid @RequestBody List<UpdateProductReq> updateProductReqList) {
for (int i = 0; i < updateProductReqList.size(); i++) {
UpdateProductReq updateProductReq = updateProductReqList.get(i);
Product product = new Product();
BeanUtils.copyProperties(updateProductReq, product);
productService.update(product);
}
return ApiRestResponse.success();
}
}
字典树Trie
学习了什么是字典树
import java.util.TreeMap;
public class Trie {
private class Node {
public boolean isWord;
public TreeMap<Character, Node> next;
public Node(boolean isWord) {
this.isWord = isWord;
next = new TreeMap<>();
}
public Node() {
this(false);
}
}
private Node root;
private int size;
public Trie() {
root = new Node();
size = 0;
}
//获得Trie中存储的单词数量
public int getSize() {
return size;
}
//向Trie中添加一个新的单词
public void add(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (cur.next.get(c) == null) {
cur.next.put(c, new Node());
}
cur = cur.next.get(c);
}
if(!cur.isWord) {
cur.isWord = true;
size++;
}
}
}