You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
8.2 KiB

5 years ago
package action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import model.Classify;
import model.PageBean;
import model.Product;
import model.User;
import model.UserAndAdmin;
import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import service.IUserService;
import util.AddJson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
IUserService iUserService;
public void setiUserService(IUserService iUserService) {
this.iUserService = iUserService;
}
Product product=new Product();
@Override
public Product getModel() {
return product;
}
HttpServletRequest req=ServletActionContext.getRequest();
AddJson json=new AddJson();
private int currPage=1;//当前页
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
private File proPictureFile;
private String proPictureFileFileName;
private String proPictureContentType;
private final int BUFFER_SIZE=16*1024;
private Integer classifyId;
public Integer getClassifyId() {
return classifyId;
}
public void setClassifyId(Integer classifyId) {
this.classifyId = classifyId;
}
public File getProPictureFile() {
return proPictureFile;
}
public void setProPictureFile(File proPictureFile) {
this.proPictureFile = proPictureFile;
}
public String getProPictureContentType() {
return proPictureContentType;
}
public void setProPictureContentType(String proPictureContentType) {
this.proPictureContentType = proPictureContentType;
}
public String getProPictureFileFileName() {
return proPictureFileFileName;
}
public void setProPictureFileFileName(String proPictureFileFileName) {
this.proPictureFileFileName = proPictureFileFileName;
}
/**
*
*/
public String addProduct() throws Exception{
String newFileName=new Date().getTime()+getExtention(proPictureFileFileName);
String path=ServletActionContext.getServletContext().getRealPath("/upload");
File picFile=new File(path);
if(!picFile.exists()){
picFile.mkdir();
}
FileUtil.copyFile(proPictureFile, new File(picFile,newFileName));
//copy(proPictureFile,picFile);将上传的图片存储到picFile
product.setPicture(newFileName);
product.setCreateTime(new Date());
Classify c=this.iUserService.getClassifyById(product.getClassifyId());
User u=this.iUserService.getUserById(product.getCreatorId());
product.setClassify(c);
product.setUser(u);
this.iUserService.saveProduct(product);
this.addActionMessage("发布成功!");
return "uploadSuccess";
}
/**
* io
* @param src
* @param dst
* @throws Exception
*/
private void copy(File src, File dst)throws Exception {
InputStream in=null;
OutputStream out=null;
try {
in=new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
out=new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] buffer=new byte[BUFFER_SIZE];
while(in.read(buffer)>0){
out.write(buffer);
}
} catch (Exception e) {
throw e;
}finally{
try {
in.close();
} catch (Exception e2) {
}
try {
out.close();
} catch (Exception e2) {
}
}
}
/**
*
* @param filename
* @return
*/
private String getExtention(String filename) {
int pos=filename.lastIndexOf(".");
return filename.substring(pos);
}
/**
*
* @throws IOException
*/
public void searchClassifyList() throws IOException{
List<Classify> list=this.iUserService.searchClassifyList();
this.json.toJsonArray(list);
}
/**
*
*/
public void searchProductList() throws Exception{
String keyword=req.getParameter("keyword");//输入框关键字
String cid=req.getParameter("cid");//判断分类
String conditon=req.getParameter("condition");//判断按什么查询
Map<Object,String> map=new HashMap<Object,String>();
if(keyword!=null&&keyword!=""){
map.put("keyword", keyword);
}
if(cid!=null&&cid!=""){
map.put("cid", cid);
}
if(conditon!=null&&conditon!=""){
map.put("conditon", conditon);
}
map.put("currPage", currPage+"");
PageBean<Product> proList=this.iUserService.searchProductList(map);
this.json.toJsonObj(proList);
}
/**
*
*/
public void getProductDetail() throws Exception{
String id=req.getParameter("id");
Product product=this.iUserService.getProductDetail(id);
this.json.toJson(product);
/*int clickNum=product.getProClicknum();
product.setProClicknum(clickNum+1);
this.iUserService.updateProduct(product);*/
}
/**
*
* @throws Exception
*/
public void searchMyProductByPage() throws Exception{
User user=(User) req.getSession().getAttribute("User");
if(user==null){
throw new Exception("用户帐户为空,请重新登录!");
}else{
Map<Object, String> map=new HashMap<Object, String>();
map.put("currPage", currPage+"");
map.put("userId", user.getUid()+"");
PageBean<Product> myproList=this.iUserService.searchMyProductByPage(map);
this.json.toJson(myproList);
}
}
private String repContent;
private String content;
public String getRepContent() {
return repContent;
}
public void setRepContent(String repContent) {
this.repContent = repContent;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
/**
*
* <p>Description: </p>
* @throws Exception
*/
public void deleteMyProductById() throws Exception{
User user=(User) req.getSession().getAttribute("User");
if(user==null){
throw new Exception("用户帐户为空,请重新登录!");
}else{
int pid=Integer.parseInt(req.getParameter("id"));
this.iUserService.delectProductById(pid);
}
}
/**
*
* @throws Exception
* @param flag 0,1
*/
public String searchMessageByPage() throws Exception{
User user=(User) req.getSession().getAttribute("User");
String flag="";
if(user==null){
throw new Exception("用户帐户为空,请重新登录!");
}else{
flag=req.getParameter("flag");//0系统消息,1用户消息
Map<Object, String> map=new HashMap<Object, String>();
map.put("currPage", currPage+"");
map.put("userId", user.getUid()+"");
map.put("flag", flag);
PageBean<UserAndAdmin> message=this.iUserService.searchMessageByPage(map);
if(message!=null){
ActionContext.getContext().getValueStack().push(message);
}
}
if(flag=="0"||"0".equals(flag)){
return "sysmessage";
}else{
return "usermessage";
}
}
private String flag;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
/**
*
* @return
* @throws Exception
*/
public String deleteMessage() throws Exception{
User user=(User) req.getSession().getAttribute("User");
//String flag=req.getParameter("flag");
if(user==null){
throw new Exception("用户帐户为空,请重新登录!");
}else{
String id=req.getParameter("id");
this.iUserService.deleteMessage(Integer.parseInt(id));
}
if(flag=="0"||"0".equals(flag)){
return "sysmsgDelete";
}else{
return "usermsgDelete";
}
}
}