package cn.bdqn.ss2m.action;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.bdqn.ss2m.constant.Const;
import cn.bdqn.ss2m.entity.Standard;
import cn.bdqn.ss2m.service.StandardService;
import cn.bdqn.ss2m.utility.PageBean;
import net.sf.json.JSONObject;
public class StandardAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private StandardService standardService;
private String queryCondition;
private String pageIndex="1";
private PageBean<Standard> standards;
private Standard standard;
//标准号
private String stdNum;
private String result;
//文件上传
private File pic;
private String picContentType;
private String picFileName;
//修改标准号Id
private String stdId;
//准备删除标准号Id
private String stdIds;
public void setStandardService(StandardService standardService) {
this.standardService = standardService;
}
public String getQueryCondition() {
return queryCondition;
}
public void setQueryCondition(String queryCondition) {
this.queryCondition = queryCondition;
}
public String getPageIndex() {
return pageIndex;
}
public void setPageIndex(String pageIndex) {
this.pageIndex = pageIndex;
}
public PageBean<Standard> getStandards() {
return standards;
}
public void setStandards(PageBean<Standard> standards) {
this.standards = standards;
}
public Standard getStandard() {
return standard;
}
public void setStandard(Standard standard) {
this.standard = standard;
}
public String getStdNum() {
return stdNum;
}
public void setStdNum(String stdNum) {
this.stdNum = stdNum;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public String getStdId() {
return stdId;
}
public void setStdId(String stdId) {
this.stdId = stdId;
}
public String getStdIds() {
return stdIds;
}
public void setStdIds(String stdIds) {
this.stdIds = stdIds;
}
public String listStandard() throws Exception {
standards=standardService.getStandardList(queryCondition, Integer.parseInt(pageIndex));
return SUCCESS;
}
public String saveStandard() {
//文件上传
try {
String rootPath=ServletActionContext.getServletContext().getRealPath(File.separator);
String fileEx=picFileName.substring(picFileName.lastIndexOf("."),picFileName.length());
String fileName=System.currentTimeMillis()+fileEx;
File destFile=new File(rootPath+File.separatorChar+Const.UPLOAD_PATH+File.separatorChar+fileName);
FileUtils.copyFile(pic, destFile);
standard.setPackagePath(fileName);
HttpSession session=ServletActionContext.getRequest().getSession();
if(standardService.saveStandard(standard)) {
session.setAttribute("hint", "增加成功");
}else {
session.setAttribute("hint", "增加失败");
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public String updateStandard() {
try {
if(null!=pic && pic.length()>0) {
String rootPath=ServletActionContext.getServletContext().getRealPath(File.separator);
String fileEx=picFileName.substring(picFileName.lastIndexOf("."),picFileName.length());
String fileName=System.currentTimeMillis()+fileEx;
File destFile=new File(rootPath+File.separatorChar+Const.UPLOAD_PATH+File.separatorChar+fileName);
FileUtils.copyFile(pic, destFile);
standard.setPackagePath(fileName);
}
HttpSession session=ServletActionContext.getRequest().getSession();
if(standardService.updateStandard(standard)) {
session.setAttribute("hint", "修改成功");
}else {
session.setAttribute("hint", "修改失败");
}
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public String deleteStandard() {
HttpSession session=ServletActionContext.getRequest().getSession();
String[] delId=stdIds.split(",");
for (int i = 0; i < delId.length; i++) {
if(standardService.deleteStandard(Integer.parseInt(delId[i]))) {
session.setAttribute("hint", "删除成功");
}else {
session.setAttribute("hint", "删除失败");
}
}
return SUCCESS;
}
public String validateStandard() {
boolean haveStandard=standardService.haveStandardStdNum(stdNum);
Map<String, Object> map=new HashMap<String, Object>();
map.put("result", haveStandard);
JSONObject json=JSONObject.fromObject(map);
result=json.toString();
return SUCCESS;
}
public String viewStandard() {
Integer id=Integer.parseInt(stdId);
standard=standardService.getStandardById(id);
return SUCCESS;
}
}