package com.bookstore.admin.login.handler;
import com.bookstore.admin.login.service.ILoginService;
import com.bookstore.commons.beans.Product;
import com.bookstore.commons.beans.Product2;
import com.bookstore.commons.beans.ProductList;
import com.bookstore.commons.beans.User;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/admin/login")
public class LoginHandler {
@Autowired
private ILoginService loginService;
@RequestMapping("/download")
public void download(String year, String month, HttpServletResponse response,HttpServletRequest request) throws IOException {
List<ProductList> productLists = loginService.findProdutSales(year, month);
String filename = year + "年" + month + "月销售榜单";
String sheetName = month + "月销售榜单";
String titlename = year + "年" + month + "月销售榜单";
String[] columnName = {"商品名称", "商品销量"};
String[][] dataList = new String[productLists.size()][2]; //二维数字必须指定长度
for (int i = 0; i < productLists.size(); i++) {
dataList[i][0] = productLists.get(i).getName();
dataList[i][1] = productLists.get(i).getSales();
}
//创建一个excel文件
HSSFWorkbook excel =new HSSFWorkbook();
//创建一个表
HSSFSheet sheet = excel.createSheet(sheetName);
//创建表的第一行
HSSFRow row1=sheet.createRow(0);
//创建第一行的第一个单元格
HSSFCell cell=row1.createCell(0);
//合并第一行的两个单元格 起始行 终止行 起始列 终止列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
//给第一行的的一个合并后的单元格赋值
cell.setCellValue(titlename);
//创建第二行
HSSFRow row=sheet.createRow(1);
for(int i=0;i<2;i++){
row.createCell(i).setCellValue(columnName[i]);
}
//创建第三行 循环,都是数据
for(int i=0;i<dataList.length;i++){ //二维数组的长度是行的长度,与列无关
row=sheet.createRow(i+2);
for(int j=0;j<2;j++){
row.createCell(j).setCellValue(dataList[i][j]);
}
}
filename=filename+".xls";
response.setContentType("application/ms-excel;charset=UTF-8");
//excel文件作为附件响应回去attachment
//content-Disposition内容类型 attachment附件
response.setHeader("content-Disposition","attachment;filename="+processFileName(request,filename));
//响应 输出流输出
OutputStream out=response.getOutputStream();
//excel文件写到输出流
excel.write(out);
}
//IE、chrom、Firefox文件中文乱码问题
public String processFileName(HttpServletRequest request, String fileNames) {
String codedfilename = null;
try {
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE") || null != agent &&
-1 != agent.indexOf("Trident")) {//ie
String name = java.net.URLEncoder.encode(fileNames, "UTF8");
codedfilename = name;
} else if (null != agent&&-1!=agent.indexOf("Mozilla")) {//火狐谷歌等
codedfilename=new String(fileNames.getBytes("UTF-8"),"iso8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return codedfilename;
}
@RequestMapping("/addProduct")
private String addProduct(Product product, HttpServletRequest request) throws IOException {
String path = request.getServletContext().getRealPath("/productImg");
String filename = UUID.randomUUID() + product.getUpload().getOriginalFilename();
File file = new File(path);
if (!product.getUpload().isEmpty()) {
file.mkdir();
}
product.getUpload().transferTo(new File(path, filename));
product.setId(UUID.randomUUID() + "");
product.setImgurl("/productImg/" + filename);
loginService.addProduct(product);
return "redirect:/admin/login/products";
}
@RequestMapping("/deleteProduct")
public String deleteProduct(String id) {
Product product = loginService.findProductById(id);
// String path = request.getServletContext().getRealPath("/productImg");
String path = "/productImg";
System.out.println(product);
String[] split = product.getImgurl().split("/");
//图片删除
File file = new File(path, split[2]);
file.delete();
loginService.removeProduct(id);
return "redirect:/admin/login/products";
}
@RequestMapping("/editProduct")
private String editProduct(HttpServletRequest request, Product product, Model model) throws IOException {
//获取在服务器上的路径
String path = request.getServletContext().getRealPath("/productImg");
System.out.println(path);
File file = new File(path);
// System.out.println(file.exists());
if (!file.exists()) {
file.mkdir();
}
if (!product.getUpload().isEmpty()) {
File targetfile = new File(product.getImgurl());
if (targetfile.exists()) {
targetfile.delete();
}
String filename = UUID.randomUUID() + product.getUpload().getOriginalFilename();
// System.out.println(filename);
product.setImgurl("/productImg/" + filename);
// System.out.println(product);
product.getUpload().transferTo(new File(path, filename));
}
// System.out.println(product);
int row = loginService.modifyProduct(product);
if (row > 0) {
return "redirect:/admin/login/products";
} else {
model.addAttribute("errormsg", "编辑信息失败,请返回重试!");
return "/error.jsp";
}
}
@RequestMapping("/findProductById")
public String findProductById(String id, Model model) {
Product product = loginService.findProductById(id);
model.addAttribute("p", product);
return "/admin/products/edit.jsp";
}
@RequestMapping("/products")
public String products(Model model, Product2 product2) {
List<Product> products = loginService.findAllProduct(product2);
model.addAttribute("products", products);
model.addAttribute("product2", product2);
return "/admin/products/list.jsp";
}
@RequestMapping("/checklLogin")
public String Login(User user, Model model, HttpSession session) {
// System.out.println(user);
User user1 = loginService.findUser(user);
if (user1 != null) {
if ("普通用户".equals(user1.getRole())) {
model.addAttribute("loginerror", "抱歉,您不是管理员用户,无法登录!");
return "/admin/login/login.jsp";
} else {
session.setAttribute("login_admin", user);
return "/admin/login/home.jsp";
}
} else {
model.addAttribute("loginerror", "用户名或密码错误,请重新登录");
return "/admin/login/login.jsp";
}
}
@RequestMapping("/loginout")
public String loginout(HttpSession session) {
session.removeAttribute("login_admin");
return "/admin/login/login.jsp";
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43842093/article/details/122015015
内容来源于网络,如有侵权,请联系作者删除!