package cn.smbms.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;
import com.alibaba.fastjson.JSONArray;
/**
* 供应商控制器
*
* @author Administrator
*
*/
@Controller
@RequestMapping("/provider")
public class ProviderController extends BaseController {
private Logger log = Logger.getLogger(ProviderController.class);
@Resource(name = "providerService")
private ProviderService providerService;
// 获取供应商集合
@RequestMapping(value = "/providerList.html", method = RequestMethod.GET)
public String getProviderList(
@RequestParam(value = "providerCode", required = false) String providerCode,
@RequestParam(value = "providerName", required = false) String providerName,
@RequestParam(value = "pageIndex", required = false) String pageIndex,
HttpServletRequest req) {
PageSupport ps = new PageSupport(); // 分页辅助
ps.setTotalCount(providerService.getProCountByData(providerCode,
providerName)); // 设置总页数
int totalPageCount = ps.getTotalPageCount();// 总页数
log.debug("总页数 ---》" + totalPageCount);
// 控制首页和尾页
if (pageIndex == null) { // 默认第一页
pageIndex = "1";
} else if (Integer.valueOf(pageIndex).intValue() < 1) {
pageIndex = "1";
}
ps.setCurrentPageNo(Integer.valueOf(pageIndex).intValue()); // 设置当前页数
int pageNo = (Integer.valueOf(pageIndex).intValue() - 1)
* ps.getPageSize();// 页标
// 调用service方法 查询集合
List<Provider> proList = providerService.getProviderList(providerName,
providerCode, pageNo, ps.getPageSize()); // 获取供应商集合
req.setAttribute("providerList", proList); // 保存到请求作用于
req.setAttribute("totalCount", ps.getTotalCount()); // 总数量
req.setAttribute("currentPageNo", ps.getCurrentPageNo()); // 当前页面
req.setAttribute("totalPageCount", ps.getTotalPageCount()); // 总页数
return "providerlist";
}
// 将填写的供应商保存到实体类
@RequestMapping(value = "/provideradd.html", method = RequestMethod.GET)
public String providerAdd(@ModelAttribute("pro") Provider pro) {
return "provideradd";
}
// org.springframework.validation.BindExcept ion
@RequestMapping(value = "/provideradd.html", method = RequestMethod.POST)
public String providerAdd(
Provider pro,
HttpSession session,
@RequestParam(value = "pic", required = false) MultipartFile attach,
HttpServletRequest req) {
String localtion = "provideradd";
log.debug("编码666===========" + pro.getProCode());
log.debug("文件名:" + attach.getOriginalFilename());
String picPath = "";// 保存到数据库的值
if (!attach.isEmpty()) {// 上次了图片
// 文件操作
String path = req.getSession().getServletContext()
.getRealPath("statics" + File.separator + "uploadfiles");// 图片存放到服务器的位置
String oldName = attach.getOriginalFilename();// 1\获取原文件名
log.debug("原文件名:===========" + oldName);
String suffix = FilenameUtils.getExtension(oldName);// 2\ 获取后缀
int fileSize = 500000;// 最大图片
log.debug("上传的文件大小Size:=============" + attach.getSize());
if (attach.getSize() > fileSize) { // 上传的东西大于指定大小
req.setAttribute("Error", "*上传大小不得超过500KB");
return localtion;
} else if (suffix.equalsIgnoreCase("jpg")
|| suffix.equalsIgnoreCase("png")
|| suffix.equalsIgnoreCase("jpeg")
|| suffix.equalsIgnoreCase("pneg")) {
String fileName = System.currentTimeMillis()
+ RandomUtils.nextInt(1000000) + "_pic.jpg"; // 3\保存的图片名称
File targetFile = new File(path, fileName); // 4\图片保存的位置 等下用户保存
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
attach.transferTo(targetFile); // 保存
} catch (Exception e) {
e.printStackTrace();
req.setAttribute("Error", "*上传失败");
return localtion;
}
picPath = path + File.separator + fileName;// 保存到数据库的值
} else {// 上传格式不正确
req.setAttribute("Error", "*上传大小不得超过500KB");
return localtion;
}
}
User user = (User) session.getAttribute(Constants.USER_SESSION);
pro.setCreatedBy(user.getId());
pro.setCreationDate(new Date());
pro.setCompanyLicPicPath(picPath);
Boolean bool = providerService.add(pro);// 添加供应商
if (!bool) {// 添加失败
req.setAttribute("Error", "*添加失败");
return localtion;
}
return "redirect:/provider/providerList.html";
}
// 查看供应商信息
@RequestMapping(value = "/providerLook/{id}")
public String providerLook(@PathVariable String id, Model model) {
Provider pro = providerService.getProviderById(id);
log.debug("查看供应商方法--供应商id=========" + id);
model.addAttribute(pro); // 添加到模型
return "providerview";
}
// 获取要修改的供应商
@RequestMapping(value = "/providerModify/{id}", method = RequestMethod.GET)
public String providerUpdate(@PathVariable String id, Model model) {
Provider pro = providerService.getProviderById(id);
log.debug("修改供应商之前--供应商id=========" + id);
model.addAttribute(pro); // 添加到模型
return "providermodify";
}
// 提交修改
@RequestMapping(value = "/providerModifySave.html", method = RequestMethod.POST)
public String providerUpdate(Provider pro, HttpSession session,
HttpServletRequest req) {
pro.setModifyBy(((User) session.getAttribute(Constants.USER_SESSION))
.getId());
log.debug("点击修改方法--供应商ID:=========" + pro.getId());
pro.setModifyDate(new Date());
Boolean bool = providerService.modify(pro);
log.debug("名称:" + pro.getProName() + "\n描述:" + pro.getProDesc()
+ "\n联系人:" + pro.getProContact() + "\n电话:" + pro.getProPhone()
+ "\n地址:" + pro.getProAddress() + "\n传真:" + pro.getProFax()
+ "\n修改人:" + pro.getModifyBy() + "\n修改时间:"
+ pro.getModifyDate() + "\n供应商id:" + pro.getId());
if (bool) {
log.debug("****修改成功****");
return "redirect:/provider/providerList.html";
}
log.debug("****修改失败****");
req.setAttribute("error", "error");
return "providermodify";
}
// 异步删除供应商
@RequestMapping(value = "/delProvider", method = RequestMethod.GET)
@ResponseBody
public Object delProvider(@RequestParam("proid") String proid,
HttpSession session) {
HashMap<String, String> resultMap = new HashMap<String, String>();
int row = 0;
log.debug("要删除的供应商ID:==============" + proid);
try {
if (proid == null || "".equals(proid)) {
resultMap.put("delResult", "notexist");// 不存在该供应商
} else {
row = providerService.getBillCountByProviderId(p
评论0