package com.qst.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.disk.DiskFileItem;
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.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.qst.model.Menu;
import com.qst.service.MenuService;
import com.qst.util.AlipayConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.aliyun.oss.OSSClient;
import com.qst.util.AlipayConfig;
@Controller
@RequestMapping("/menu")
public class MenuController {
@Autowired
@Qualifier("menuService")
private MenuService menuService;
//LK修改
@RequestMapping("/getMenus.do")
public ModelAndView getMenus(Integer id) {
ModelAndView mv = new ModelAndView();
List<Menu> menuList = menuService.getMenus();
if (id == 1)
mv.setViewName("menuList");
else
mv.setViewName("generateList");
mv.addObject("menuList", menuList);
return mv;
}
@RequestMapping("/getMenuById.do")
public ModelAndView getMenuById(Integer id,HttpServletRequest request,HttpServletResponse response){
ModelAndView mv = new ModelAndView();
Menu menu=menuService.getMenuById(id);
String popular=menu.getPopular();
//request.getSession().setAttribute("popular", popular);
if (menu != null) {
mv.addObject("menu", menu);
mv.addObject("popular",menu.getPopular());
mv.addObject("type",menu.getType());
mv.setViewName("updateMenu");
}
return mv;
}
@RequestMapping("/toAddMenus.do")
public ModelAndView toAddMenus(){
ModelAndView mv = new ModelAndView();
mv.setViewName("addMenus");
return mv;
}
@RequestMapping("/delMenus.do")
public ModelAndView delMenus(Integer id){
ModelAndView mv = new ModelAndView();
if (menuService.delMenus(id)) {
mv.setViewName("redirect:/menu/getMenus.do?id=1");
}else{
mv.setViewName("redirect:/menu/getMenus.do?id=1");
}
return mv;
}
@RequestMapping("/menuUpdate.do")
public ModelAndView menuUpdate(Menu menu,@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
//原文件名,同时也是上传OSS所需的Key
String orgName = file.getOriginalFilename();
//从CommonsMultipartFile转换为File,以方便上传到阿里云OSS
DiskFileItem fi = (DiskFileItem)file.getFileItem();
File result = fi.getStoreLocation();
// 创建OSSClient实例。
String endpoint = "http://oss-cn-beijing.aliyuncs.com";
String accessKeyId = "LTAI6FKdJNDdt7oT";
String accessKeySecret = "R7Li46P7eJ97e18KlGSYWcqzr2VASQ";
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String bucketName = "haolahotpot";
//上传文件
ossClient.putObject(bucketName, orgName, result);
// 设置URL过期时间为10年 3600l* 1000*24*365*10
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
// 生成URL
java.net.URL url = ossClient.generatePresignedUrl(bucketName, orgName, expiration);
String picUrl = url.toString();
menu.setPicture(picUrl);
if (menuService.menuUpdate(menu)) {
mv.setViewName("redirect:/menu/getMenus.do?id=1");
}
return mv;
}
@RequestMapping("/addMenus.do")
public ModelAndView addMenus(Menu menu,@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response){
ModelAndView mv = new ModelAndView();
//原文件名,同时也是上传OSS所需的Key
String orgName = file.getOriginalFilename();
//从CommonsMultipartFile转换为File,以方便上传到阿里云OSS
DiskFileItem fi = (DiskFileItem)file.getFileItem();
File result = fi.getStoreLocation();
// 创建OSSClient实例。
String endpoint = "http://oss-cn-beijing.aliyuncs.com";
String accessKeyId = "LTAI6FKdJNDdt7oT";
String accessKeySecret = "R7Li46P7eJ97e18KlGSYWcqzr2VASQ";
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String bucketName = "haolahotpot";
//上传文件
ossClient.putObject(bucketName, orgName, result);
// 设置URL过期时间为10年 3600l* 1000*24*365*10
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
// 生成URL
java.net.URL url = ossClient.generatePresignedUrl(bucketName, orgName, expiration);
String picUrl = url.toString();
menu.setPicture(picUrl);
if (menuService.addMenu(menu)) {
mv.setViewName("addMenus");
}else{
mv.setViewName("addMenus");
}
return mv;
}
//下面由LK编写
@RequestMapping("/putInStorage.do")
public ModelAndView putInStorage(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
List<Menu> menuList = menuService.getMenus();
// int size = Integer.parseInt(request.getParameter("sizeOfMenuList"));
for (int i = 0; i < menuList.size(); i++) {
Menu menu = menuList.get(i);
String add = request.getParameter("addamount" + i);
int addAmount = 0;
int amount = 0;
if (add != "") {
addAmount = Integer.parseInt(add);
}
amount = Integer.parseInt(menu.getAmount());
menu.setAmount(Integer.toString(amount + addAmount));
if (menuService.menuUpByName(menu)) {
mv.setViewName("redirect:/menu/getMenus.do?id=2");
}
}
return mv;
}
// 创建Excel
@RequestMapping("/generate.do")
public ModelAndView createExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelAndView mv = new ModelAndView();
List<Menu> menuList = menuService.getMenus();
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新的sheet对象(excel的表单)
HSSFSheet sheet = wb.createSheet("入库单");
// 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1 = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell = row1.createCell(0);
// 设置单元格内容
cell.setCellValue("入库单");
// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
// 在sh