package com.trade.controller; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; import com.trade.model.Custom; import com.trade.model.Employee; import com.trade.model.Product; import com.trade.model.Page; import com.trade.model.Sales; import com.trade.service.CustomService; import com.trade.service.ProductService; import com.trade.service.SalesService; import com.trade.util.HighChartJson; import com.trade.util.PageUtils; @Controller public class ProductController { @Autowired private ProductService productService; @Autowired private CustomService customService; @Autowired private SalesService salesService; @RequestMapping("queryProductBypage.do") public String queryProductBypage(Product product,Page page,ModelMap model){ page.setMaxRows(5); List<Product> list = productService.queryByProduct(product); page.setStart(PageUtils.getPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows())); page.setTotalPage(PageUtils.getTotalPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows())); List<Product> productList = productService.queryByList(page, product); model.put("page", page); model.put("productList", productList); model.put("product", product); List<Custom> customList = customService.queryByCustom(new Custom()); model.put("customList", customList); return "/productList"; } @RequestMapping("queryProductBypage1.do") public String queryProductBypage1(Page page,ModelMap model){ Product product = new Product(); page.setMaxRows(5); List<Product> list = productService.queryByProduct(product); page.setStart(PageUtils.getPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows())); page.setTotalPage(PageUtils.getTotalPage(page.getPageNumber(), page.getTotalPage(), list.size(), page.getMaxRows())); List<Product> productList = productService.queryByList(page, product); model.put("page", page); model.put("productList", productList); model.put("product", product); List<Custom> customList = customService.queryByCustom(new Custom()); model.put("customList", customList); return "/productList"; } @RequestMapping("checkAll.do") @ResponseBody public String checkAll(ModelMap model){ List<Product> list = productService.queryByProduct(new Product()); List<String> categories = new ArrayList<String>(); List<Integer> data = new ArrayList<Integer>(); for(Product p : list){ categories.add(p.getProductname()); data.add(p.getProductnum()); } HighChartJson json = new HighChartJson(categories, data); Gson gson = new Gson(); gson.toJson(json); return gson.toJson(json).toString(); } @RequestMapping("statistical.do") @ResponseBody public String statistical(ModelMap model){ List<Product> list = productService.queryByProduct(new Product()); List<String> categories = new ArrayList<String>(); List<Integer> data = new ArrayList<Integer>(); for(Product p : list){ categories.add(p.getProductname()); data.add(p.getProductsnum()); } HighChartJson json = new HighChartJson(categories, data); Gson gson = new Gson(); gson.toJson(json); return gson.toJson(json).toString(); } @RequestMapping("addProduct.do") public String addProduct(Product product,HttpServletRequest request){ productService.save(product); return "redirect:/queryProductBypage.do"; } @RequestMapping("updateProduct.do") public String updateProduct(Product product,HttpServletRequest request){ productService.update(product); return "redirect:/queryProductBypage.do"; } @RequestMapping("deleteProductByid.do") public String deleteProductByid(Product product, ModelMap model){ Sales sales = new Sales(); sales.setProductid(product.getProductid()); List<Sales> salesList = salesService.queryBySales(sales); if(salesList.size() != 0){ model.put("resultMessage", "商品有关联销售订单,不能删除"); }else{ model.put("resultMessage", "删除成功"); productService.deleteById(product.getProductid()); } return "forward:/queryProductBypage1.do"; } @RequestMapping("sale.do") public String sale(Sales sales,HttpSession session,ModelMap model){ Employee emp = (Employee) session.getAttribute("emp"); if(emp == null){ model.put("resultMessage", "请登陆后再执行此操作"); return "redirect:/queryProductBypage.do"; } Product product = productService.queryById(sales.getProductid()); if(product.getProductnum() < sales.getSalesnum()){ model.put("resultMessage", "库存不足,添加销售单失败"); return "redirect:/queryProductBypage.do"; } product.setProductnum(product.getProductnum() - sales.getSalesnum()); product.setProductsnum(product.getProductsnum() + sales.getSalesnum()); productService.update(product); sales.setEmployeeid(emp.getEmployeeid()); sales.setSalesstatus("待审核"); sales.setSalesdate(new Date()); salesService.save(sales); model.put("resultMessage", "操作成功,请到销售管理查看详情"); return "forward:/queryProductBypage1.do"; } }
最近下载更多