package com.action; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; import com.dao.GoodsDao; import com.dao.InGoodsDao; import com.dao.OutGoodsDao; import com.dao.SaveGoodsDao; import com.model.Goods; import com.model.PageBean; import com.util.DbUtil; import com.util.ExcelUtil; import com.util.JsonUtil; import com.util.ResponseUtil; import com.util.StringToInt; import com.util.StringUtil; public class GoodsAction extends ActionSupport implements ServletRequestAware{ private static final long serialVersionUID = 1L; private Goods goods; private String goodscode; private String goodsname; private String supplyid; private String typeid; private String goodsothers; private String page; private String rows; private String delIds; private String goodsid; private File goodsUploadFile; public File getGoodsUploadFile() { return goodsUploadFile; } public void setGoodsUploadFile(File goodsUploadFile) { this.goodsUploadFile = goodsUploadFile; } HttpServletRequest request; public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } public String getSupplyid() { return supplyid; } public void setSupplyid(String supplyid) { this.supplyid = supplyid; } public String getTypeid() { return typeid; } public void setTypeid(String typeid) { this.typeid = typeid; } public String getGoodscode() { return goodscode; } public void setGoodscode(String goodscode) { this.goodscode = goodscode; } public String getGoodsname() { return goodsname; } public void setGoodsname(String goodsname) { this.goodsname = goodsname; } public String getGoodsothers() { return goodsothers; } public void setGoodsothers(String goodsothers) { this.goodsothers = goodsothers; } public String getGoodsid() { return goodsid; } public void setGoodsid(String goodsid) { this.goodsid = goodsid; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public String getRows() { return rows; } public void setRows(String rows) { this.rows = rows; } public String getDelIds() { return delIds; } public void setDelIds(String delIds) { this.delIds = delIds; } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } DbUtil dbUtil = new DbUtil(); GoodsDao goodsdao = new GoodsDao(); public String execute() throws Exception { PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows)); Connection con = null; if (goods == null) { goods = new Goods(); } goods.setGoodscode(goodscode); goods.setGoodsname(goodsname); if (StringUtil.isNotEmpty(supplyid)) { goods.setSupplyid(Integer.parseInt(supplyid)); } if (StringUtil.isNotEmpty(typeid)) { goods.setTypeid(Integer.parseInt(typeid)); } try { con = dbUtil.getCon(); JSONObject result = new JSONObject(); JSONArray jsonArray = JsonUtil.formatRsToJsonArray(goodsdao.list( con, pageBean, goods)); int total = goodsdao.Count(con, goods); result.put("rows", jsonArray); result.put("total", total); ResponseUtil.write(ServletActionContext.getResponse(), result); } catch (Exception e) { e.printStackTrace(); } finally { dbUtil.closeCon(con); } return null; } InGoodsDao ingoodsdao=new InGoodsDao(); OutGoodsDao outgoodsdao=new OutGoodsDao(); SaveGoodsDao savegoodsdao=new SaveGoodsDao(); public String delete() throws Exception { Connection con = null; try { con = dbUtil.getCon(); JSONObject result = new JSONObject(); String str[]=delIds.split(","); for(int i=0; i<str.length;i++){ boolean f=ingoodsdao.getGoodsIdByInGoods(con, str[i]); if(f){ result.put("errorIndex", i); result.put("errorMsg","入库表下面有货物,不能删除"); ResponseUtil.write(ServletActionContext.getResponse(), result); return null; } } for(int i=0; i<str.length;i++){ boolean f=outgoodsdao.getGoodsIdByInGoods(con, str[i]); if(f){ result.put("errorIndex", i); result.put("errorMsg","出库表下面有货物,不能删除"); ResponseUtil.write(ServletActionContext.getResponse(), result); return null; } } for(int i=0; i<str.length;i++){ boolean f=savegoodsdao.getGoodsIdByInGoods(con, str[i]); if(f){ result.put("errorIndex", i); result.put("errorMsg","库存表下面有货物,不能删除"); ResponseUtil.write(ServletActionContext.getResponse(), result); return null; } } int delNums = goodsdao.delete(con, delIds); if (delNums > 0) { result.put("success", "true"); result.put("delNums", delNums); } else { result.put("errorMsg", "删除失败"); } ResponseUtil.write(ServletActionContext.getResponse(), result); } catch (Exception e) { e.printStackTrace(); } finally { try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } public String save() throws Exception { if (StringUtil.isNotEmpty(goodsid)) { goods.setGoodsid(Integer.parseInt(goodsid)); } Connection con = null; try { con = dbUtil.getCon(); int saveNums = 0; JSONObject result = new JSONObject(); if (StringUtil.isNotEmpty(goodsid)) { saveNums = goodsdao.modify(con, goods); } else { saveNums = goodsdao.add(con, goods); } if (saveNums > 0) { result.put("success", "true"); } else { result.put("success", "true"); result.put("errorMsg", "保存失败"); } ResponseUtil.write(ServletActionContext.getResponse(), result); } catch (Exception e) { e.printStackTrace(); } finally { try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; } public String export() throws Exception { Connection con =null; if (goods == null) { goods = new Goods(); } goods.setGoodscode(goodscode); goods.setGoodsname(goodsname); if (StringUtil.isNotEmpty(supplyid)) { goods.setSupplyid(Integer.parseInt(supplyid)); } if (StringUtil.isNotEmpty(typeid)) { goods.setTypeid(Integer.parseInt(typeid)); } try{ con=dbUtil.getCon(); ResultSet rs=goodsdao.list(con, null, goods); Workbook wb =ExcelUtil.fillExcelDataWithTemplate(rs, "goods.xls"); ResponseUtil.export(ServletActionContext.getResponse(), wb, "利用模板导出exel.xls"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } public String upload()throws Exception{ POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(goodsUploadFile)); HSSFWorkbook wb =new HSSFWorkbook(fs); HSSFSheet hssfSheet =wb.getSheetAt(0); if(hssfSheet!=null){ for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){ HSSFRow hssfRow =hssfSheet.getRow(rowNum); if(hssfRow==null){ continue; } Goods goods =new Goods(); goods.setGoodscode(ExcelUtil.formatCell(hssfRow.getCell(1))); goods.setGoodsname(ExcelUtil.formatCell(hssfRow.getCell(2))); goods.setSupplyid(StringToInt.StringToInt(ExcelUtil.formatCell(hssfRow.getCell(3)))); goods.setTypeid(StringToInt.StringToInt(ExcelUtil.formatCell(hssfRow.getCell(4)))); goods.setGoodsothers(ExcelUtil.formatCell(hssfRow.getCell(5))); Connection con=null; try{ con=dbUtil.getCon(); int saveNums = 0; JSONObject result = new JSONObject(); if (goodsdao.UniqueGoods(con, goods)) { result.put("success", "true"); result.put("errorMsg", "code不能重复上传失败"); ResponseUtil.write(ServletActionContext.getResponse(), result); return null; } else { saveNums = goodsdao.add(con, goods); } if (saveNums > 0) { result.put("success", "true"); } else { result.put("success", "true"); result.put("errorMsg", "上传失败"); } ResponseUtil.write(ServletActionContext.getResponse(), result); }catch(Exception e){ e.printStackTrace(); }finally{ dbUtil.closeCon(con); } } } JSONObject result =new JSONObject(); result.put("success","true"); ResponseUtil.write(ServletActionContext.getResponse(), result); return null; } public String getgoodsbytid() throws Exception { // request.setAttribute("mainPage", "index.jsp"); Connection con=null; con=dbUtil.getCon(); String sql="select * from goods t1 where t1.typeid="+typeid+" order by t1.goodsid desc ";//sql语句部分大小写 ArrayList<Goods> goodslist=goodsdao.getGoodsList(con, sql); request.setAttribute("goodslist", goodslist); return "index"; } public String goodsComboList(){ Connection con=null; try{ con=dbUtil.getCon(); JSONArray jsonArray =new JSONArray(); JSONObject jsonObject=new JSONObject(); jsonObject.put("goodsid", ""); jsonObject.put("goodsname","请选择..."); jsonArray.add(jsonObject); jsonArray.addAll(JsonUtil.formatRsToJsonArray(goodsdao.list(con, null,null))); ResponseUtil.write(ServletActionContext.getResponse(), jsonArray); }catch(Exception e){ e.printStackTrace(); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }
最近下载更多
微信网友_6442962125967360 LV2
2023年5月9日
teamdragon LV6
2023年4月13日
sandihha LV9
2022年11月10日
WM2020 LV4
2022年6月20日
jwfadacai LV8
2022年4月30日
skook7 LV2
2022年4月20日
邬松桥 LV11
2022年4月5日
yscccc LV1
2022年2月22日
哈哈哈哈哈a LV10
2022年2月15日
wanglinddad LV55
2021年12月12日
最近浏览更多
wangqi_sheng LV1
8月14日
liulian666 LV2
7月22日
ma406805131 LV15
6月29日
王佩琪 LV3
6月14日
lilitu LV6
5月26日
hongdongdong LV14
5月7日
微信网友_6512020493586432 LV2
4月9日
微信网友_6904205432819712 LV1
3月12日
FishYu11
1月19日
暂无贡献等级
hxy19991216 LV4
2023年12月29日