package com.shiny.ertao.action;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shiny.ertao.dao.IBuser;
import com.shiny.ertao.dao.ICategory;
import com.shiny.ertao.dao.IGood;
import com.shiny.ertao.dao.INav;
import com.shiny.ertao.model.Buser;
import com.shiny.ertao.model.Category;
import com.shiny.ertao.model.Good;
import com.shiny.ertao.model.GoodsFile;
import com.shiny.ertao.model.Nav;
import com.shiny.ertao.model.Skip;
import com.shiny.ertao.utils.FileUploadUtil;
import com.shiny.ertao.utils.JXPageUtil;
import com.shiny.ertao.utils.RandCodeUtils;
public class GoodAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request=ServletActionContext.getRequest();
private HttpServletResponse response = ServletActionContext.getResponse();
private HttpSession session = request.getSession();
private ServletContext app = ServletActionContext.getServletContext();
private Skip skip;
String result="succ";
private int total = 0;
private int pagesize = 5;
private int currpage = 1;
private Good good;
private GoodsFile goodsFile;
private IGood goodImpl = null;
private ICategory categoryImpl=null;
private INav navImpl =null;
private IBuser buserImpl = null;
private String basePath= app.getRealPath("/")+"images/goods";
public void deleteGood(){
int gid = Integer.parseInt(request.getParameter("gid"));
good = goodImpl.getGoodById(gid);
File file = new File(basePath+"\\"+good.getGpic());
if(file.isFile()){
file.delete();
}
goodImpl.deleteGood(good);
}
public String updateGood(){
if(goodsFile!=null){
System.out.println("重新上传了图片=======");
System.out.println(basePath+"\\"+good.getGpic());
//将原来的图片删掉
File file = new File(basePath+"\\"+good.getGpic());
if(file.isFile()){
file.delete();
System.out.println(good.getGpic()+"删除成功 !");
}
//上传现在的图片
String gpic = FileUploadUtil.getUploadFile(goodsFile, basePath);
good.setGpic(gpic);
//重新设置图片名称
System.out.println("上传之后:"+good.getGpic());
}else{
System.out.println("--------图片没有修改------------");
}
goodImpl.updateGood(good);
skip = new Skip();
skip.setSkipTitle("成功提示页面");
skip.setSkipMsg("恭喜您,修改商品信息成功!");
skip.setSkipStyle("green");
skip.setSkipTime("3");
//session.setAttribute("gname", good.getGname());
//session.setAttribute("gno", good.getGno());
skip.setSkipUrl("goodAction_queryGoodsSplit.action");
request.setAttribute("skip", skip);
return "skip";
}
public String queryGoodsByGname(){
request.setAttribute("gname", good.getGname());
request.setAttribute("goods", goodImpl.queryGoodByGname(good.getGname()));
return "likeGoods";
}
public String queryOneGood(){
good = goodImpl.getGoodById(good.getGid());
Category c = categoryImpl.getCategoryById(good.getCategory().getCid());
Nav n = navImpl.getNavById(c.getNav().getNid());
request.setAttribute("navname", n.getNavname());
request.setAttribute("cname", c.getCname());
request.setAttribute("good", good);
return "goodDetails";
}
public String getOneGood(){
good = goodImpl.getGoodById(good.getGid());
int cid = good.getCategory().getCid();
Category c = categoryImpl.getCategoryById(cid);
int nid = c.getNav().getNid();
System.out.println(c.getNav().getNid());
List<Category> cs = categoryImpl.queryCategorys(nid);
List<Nav> navs = navImpl.getAllNavs();
request.setAttribute("nid", nid);
request.setAttribute("categorys", cs);
request.setAttribute("navs", navs);
request.setAttribute("good", good);
return "update";
}
public String getGoodsByNid(){
Map<String,List<Good>> goodMap = new HashMap<String,List<Good>>();
int nid = Integer.parseInt(request.getParameter("nid"));
List<Category> cs = categoryImpl.queryCategorys(nid);
if(cs.size()>0){
for(int i=0;i<cs.size();i++){
String cname = cs.get(i).getCname();
int cid = cs.get(i).getCid();
List<Good> goods = goodImpl.queryGoodByCid(cid);
if(goods.size()>0){
goodMap.put(cname, goods);
}else{
goodMap.put(cname, null);
}
}
}
request.setAttribute("goodList", goodMap);
return "navGoods";
}
public String getGoodsByCidSplit(){
int pagesize=1;
int currpage =1;
int total =0;
int cid =0;
cid =(Integer) session.getAttribute("cid");
request.setAttribute("cname", categoryImpl.getCategoryById(cid).getCname());
if(request.getParameter("currpage")!=null){
currpage = Integer.parseInt(request.getParameter("currpage"));
}
else if(session.getAttribute("currpage")!=null){
currpage = (Integer) session.getAttribute("currpage");
}
total = (Integer)session.getAttribute("total");
request.setAttribute("goods", goodImpl.queryGoodByCid(cid,currpage,pagesize));
JXPageUtil jx = new JXPageUtil("goodAction_getGoodsByCidSplit.action",total,currpage,pagesize);
jx.setBothnum(3);
String numpage = jx.showpages();
currpage = jx.getCurrpage();
session.setAttribute("currpage", currpage);
session.setAttribute("total", total);
session.setAttribute("cid", cid);
request.setAttribute("numpage", numpage);
return "categoryGoods";
}
public String getGoodsByCid(){
int currpage =1;
int pagesize =8;
int total=0;
int cid = Integer.parseInt(request.getParameter("cid"));
request.setAttribute("cname", categoryImpl.getCategoryById(cid).getCname());
total = goodImpl.tatal(cid);
System.out.println(total);
if(total>0){
request.setAttribute("goods", goodImpl.queryGoodByCid(cid,currpage,pagesize));
}else{
request.setAttribute("goods", null);
}
JXPageUtil jx = new JXPageUtil("goodAction_getGoodsByCidSplit.action",total,currpage,pagesize);
jx.setBothnum(3);
String numpage = jx.showpages();
currpage = jx.getCurrpage();
session.setAttribute("currpage", currpage);
session.setAttribute("total", total);
session.setAttribute("cid", cid);
request.setAttribute("numpage", numpage);
return "categoryGoods";
}
public String queryGoods(){
List<Object> objs = null;
int nid =0;
int cid =0;
String gname="";
String gno ="";
String blogin ="";
if(request.getParameter("nid")!=null) nid = Integer.parseInt(request.getParameter("nid"));
if(good.getCategory().getCid()!=null) cid = good.getCategory().getCid();
if(good.getGname()!=null) gname = good.getGname();
if(good.getGno()!=null) gno = good.getGno();
if(good.getBuser().getBlogin()!=null) blogin =good.getBuser().getBlogin();
String cids = this.getCids(nid, cid);
if(!cids.equals("no")){
total = goodImpl.tatal(gname, gno, blogin, cids);
if(total!=0){
objs= goodImpl.queryGoods(gname, gno, blogin, cids, currpage, pagesize);
}
}
JXPageUtil jx = new JXPageUtil("goodAction_queryGoodsSplit.action",total,currpage,pagesize);
jx.setBothnum(3);
String numpage = jx.showNumPage();
currpage = jx.getCurrpage();
session.setAttribute("blogin", blogin);
session.setAttrib