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.

355 lines
9.8 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.Goods;
import model.User;
//import model.UserAdmin;
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 GoodsAction extends ActionSupport implements ModelDriven<Goods>{
IUserService iUserService;
public void setiUserService(IUserService iUserService) {
this.iUserService = iUserService;
}
Goods good=new Goods();
@Override
public Goods getModel() {
return good;
}
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 addLostGood() 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);
good.setPicture(newFileName);
good.setCreateTime(new Date());
Classify c=this.iUserService.getClassifyById(good.getClassifyId());
User u=this.iUserService.getUserById(good.getCreatorId());
good.setClassify(c);
good.setUser(u);
good.setGoodsstatus(0);
this.iUserService.saveGood(good);
this.addActionMessage("发布成功!");
return "uploadSuccess";
}
public String addFoundGood() 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);
good.setPicture(newFileName);
good.setCreateTime(new Date());
Classify c=this.iUserService.getClassifyById(good.getClassifyId());
User u=this.iUserService.getUserById(good.getCreatorId());
good.setClassify(c);
good.setUser(u);
good.setGoodsstatus(1);
this.iUserService.saveGood(good);
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 searchLostGoodList() 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<Goods> proList=this.iUserService.searchLostGoodList(map);
this.json.toJsonObj(proList);
}
public void searchFoundGoodList() throws Exception{
String cid=req.getParameter("cid");
Map<Object,String> map=new HashMap<Object,String>();
if(cid!=null&&cid!=""){
map.put("cid", cid);
}
map.put("currPage", currPage+"");
PageBean<Goods> proList=this.iUserService.searchFoundGoodList(map);
this.json.toJsonObj(proList);
}
/**
*
*/
public void getLostGoodDetail() throws Exception{
String id=req.getParameter("id");
Goods good=this.iUserService.getLostGoodDetail(id);
this.json.toJson(good);
}
public void getFoundGoodDetail() throws Exception{
String id=req.getParameter("id");
Goods good=this.iUserService.getFoundGoodDetail(id);
this.json.toJson(good);
}
/**
*
* @throws Exception
*/
public void searchMyLostProductByPage() 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.getId()+"");
PageBean<Goods> myproList=this.iUserService.searchMyLostGoodByPage(map);
this.json.toJson(myproList);
}
}
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.getId()+"");
PageBean<Goods> myproList=this.iUserService.searchMyFoundGoodByPage(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 deleteMyGoodById() 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.delectGoodById(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.getId()+"");
map.put("flag", flag);
PageBean<UserAdmin> 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";
}
}*/
}