package com.bysj.action;
 
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.util.ArrayList;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.bysj.utils.ExportExcelUtil;
import com.bysj.dao.Bo1DAO;
import com.bysj.domain.Bo1;
import com.bysj.dao.BoTDAO;
import com.bysj.domain.BoT;
@Controller @Scope("prototype")
public class Bo1Action extends ActionSupport {

/*图片字段bookPhoto参数接收*/
	 private File bookPhotoFile;
	 private String bookPhotoFileFileName;
	 private String bookPhotoFileContentType;
	 public File getBookPhotoFile() {
		return bookPhotoFile;
	}
	public void setBookPhotoFile(File bookPhotoFile) {
		this.bookPhotoFile = bookPhotoFile;
	}
	public String getBookPhotoFileFileName() {
		return bookPhotoFileFileName;
	}
	public void setBookPhotoFileFileName(String bookPhotoFileFileName) {
		this.bookPhotoFileFileName = bookPhotoFileFileName;
	}
	public String getBookPhotoFileContentType() {
		return bookPhotoFileContentType;
	}
	public void setBookPhotoFileContentType(String bookPhotoFileContentType) {
		this.bookPhotoFileContentType = bookPhotoFileContentType;
	}
    /*界面层需要查询的属性:图书条形码*/
    private String barcode;
    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public String getBarcode() {
        return this.barcode;
    }
    /*界面层需要查询的属性:图书名称*/
    private String bookName;
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookName() {
        return this.bookName;
    }
    /*界面层需要查询的属性:图书所在类别*/
    private BoT boT;
    public void setBoT(BoT boT) {
        this.boT = boT;
    }

    public BoT getBoT() {
        return this.boT;
    }
    /*界面层需要查询的属性:出版日期*/
    private String publishDate;
    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }

    public String getPublishDate() {
        return this.publishDate;
    }
    /*当前第几页*/
    private int currentPage;
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }
    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource Bo1DAO bo1DAO;

    @Resource BoTDAO boTDAO;

    /*待操作的Bo1对象*/
    private Bo1 bo1;
    public void setBo1(Bo1 bo1) {
        this.bo1 = bo1;
    }
    public Bo1 getBo1() {
        return this.bo1;
    }
    /*跳转到添加图书视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的BoT信息*/
        List<BoT> boTList = boTDAO.QueryAllBoTInfo();
        ctx.put("boTList", boTList);
        return "add_view";
    }
  /*添加图书信息*/
  @SuppressWarnings("deprecation")
  public String AddBo1() {
      ActionContext ctx = ActionContext.getContext();
      /*验证图书条形码是否已经存在*/
      String barcode = bo1.getBarcode();
      Bo1 db_bo1 = bo1DAO.GetBo1ByBarcode(barcode);
      if(null != db_bo1) {
          ctx.put("error",  java.net.URLEncoder.encode("该图书条形码已经存在!"));
          return "error";
      }
      try {
            if(true) {
            BoT boT = boTDAO.GetBoTByBookTypeId(bo1.getBoT().getBookTypeId());
            bo1.setBoT(boT);
            }
            String path = ServletActionContext.getServletContext().getRealPath("/upload"); 
            /*处理图片上传*/
            String bookPhotoFileName = ""; 
       	 	if(bookPhotoFile != null) {
       	 		InputStream is = new FileInputStream(bookPhotoFile);
       			String fileContentType = this.getBookPhotoFileContentType();
       			if(fileContentType.equals("image/jpeg")  || fileContentType.equals("image/pjpeg"))
       				bookPhotoFileName = UUID.randomUUID().toString() +  ".jpg";
       			else if(fileContentType.equals("image/gif"))
       				bookPhotoFileName = UUID.randomUUID().toString() +  ".gif";
       			else {
       				ctx.put("error",  java.net.URLEncoder.encode("上传图片格式不正确!"));
       				return "error";
       			}
       			File file = new File(path, bookPhotoFileName);
       			OutputStream os = new FileOutputStream(file);
       			byte[] b = new byte[1024];
       			int bs = 0;
       			while ((bs = is.read(b)) > 0) {
       				os.write(b, 0, bs);
       			}
       			is.close();
       			os.close();
       	 	}
            if(bookPhotoFile != null)
            	bo1.setBookPhoto("upload/" + bookPhotoFileName);
            else
            	bo1.setBookPhoto("upload/NoImage.jpg");
          bo1DAO.AddBo1(bo1);
          ctx.put("message",  java.net.URLEncoder.encode("Bo1添加成功!"));
          return "add_success";
      } catch (Exception e) {
          e.printStackTrace();
          ctx.put("error",  java.net.URLEncoder.encode("Bo1添加失败!"));
          return "error";
      }
  }
    /*查询图书*/
    public String QueryBo1() {
        if(currentPage == 0) currentPage = 1;
        if(barcode == null) barcode = "";
        if(bookName == null) bookName = "";
        if(publishDate == null) publishDate = "";
        List<Bo1> bo1List = bo1DAO.QueryBo1Info( barcode, bookName, boT, publishDate, currentPage);
        /*计算总的页数和总的记录数*/
        bo1DAO.CalculateTotalPageAndRecordNumber( barcode, bookName, boT, publishDate);
        /*获取到总的页码数目*/ 
        totalPage = bo1DAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = bo1DAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("bo1List",  bo1List);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("barcode", barcode);
        ctx.put("bookName", bookName);
        ctx.put("boT", boT);
        List<BoT> boTList = boTDAO.QueryAllBoTInfo();
        ctx.put("boTList", boTList);
        ctx.put("publishDate", publishDate);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryBo1OutputToExcel() {
        if(barcode == null) barcode = "";
        if(bookName == null) bookName = "";
        if(publishDate == null) publishDate = "";
        List<Bo1> bo1List = bo1DAO.QueryBo1Info( barcode, bookName, boT, publishDate, currentPage);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "Bo1信息记录";
        String[] headers = {"图书条形码","图书名称","图书所在类别","图书价格","库存","出版日期","出版社","图书简介","图书图片"};
        List<String[]> dataset = new ArrayList<String[]>();
        for(int i=0;i<bo1List.size();i++) {
        	Bo1 bo1 = bo1List.get(i);
        	dataset.add(new String[]{bo1.getBarcode(),bo1.getBookName(),bo1.getBoT().getBookTypeName(),bo1.getPrice() + "",bo1.getCount() + "",bo1.getPublishDate(),bo1.getPublish(),bo1.getIntroduction(),bo1.getBookPhoto()});
        }
        /*
        OutputStream out = null;
		try {
			out = new FileOutputStream("C://output.xls");
			ex.exportExcel(title,headers, dataset, out);
		    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		HttpServletResponse response = null;//创建一个HttpServletResponse对象
		OutputStream out = null;//创建一个输出流对象
		try {
			response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
			out = response.getOutputStream();//
			response.setHeader("Content-disposition","attachment; filename="+"Bo1.xls");//filename是下载的xls的名,建议最好用英文
			response.setContentType("application/msexcel;charset=UTF-8");//设置类型
			response.setHeader("Pragma","No-cache");//设置头
			response.setHeader("Cache-Control","no-cache");//设置头
			response.setDateHeader("Expires", 0);//设置日期头
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			ex.exportExcel(rootPath,title,headers, dataset, out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try{
				if(out!=null){
					out.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		return null;
    }

    /*前台查询图书*/
    public String FrontQueryBo1() {
        if(currentPage == 0) currentPage = 1;
        if(barcode == null) barcode = "";
        if(bookName == null) bookName = "";
        if(publishDate == null) publishDate = "";
        List<Bo1> bo1List = bo1DAO.QueryBo1Info( barcode, bookName, boT, publishDate, currentPage);
        /*计算总的页数和总的记录数*/
        bo1DAO.CalculateTotalPageAndRecordNumber( barcode, bookName, boT, publishDate);
        /*获取到总的页码数目*/ 
        totalPage = bo1DAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = bo1DAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("bo1List",  bo1List);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("barcode", barcode);
        ctx.put("bookName", bookName);
        ctx.put("boT", boT);
        List<BoT> boTList = boTDAO.QueryAllBoTInfo();
        ctx.put("boTList", boTList);
        ctx.put("publishDate", publishDate);
        return "front_query_view";
    }

    /*查询要修改的Bo1信息*/
    public String ModifyBo1Query() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键barcode获取Bo1对象*/
        Bo1 bo1 = bo1DAO.GetBo1ByBarcode(barcode);
        
        List<BoT> boTList = boTDAO.QueryAllBoTInfo();
        ctx.put("boTList", boTList);
        ctx.put("bo1",  bo1);
        return "modify_view";
    }

    /*查询要修改的Bo1信息*/
    public String FrontShowBo1Query() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键barcode获取Bo1对象*/
        Bo1 bo1 = bo1DAO.GetBo1ByBarcode(barcode);
        
        List<BoT> boTList = boTDAO.QueryAllBoTInfo();
        ctx.put("boTList", boTList);
        ctx.put("bo1",  bo1);
        return "front_show_view";
    }


    /*更新修改图书信息*/
    public String ModifyBo1() {
      ActionContext ctx = ActionContext.getContext();
      try {
            if(true) {
            BoT boT = boTDAO.GetBoTByBookTypeId(bo1.getBoT().getBookTypeId());
            bo1.setBoT(boT);
            }
            String path = ServletActionContext.getServletContext().getRealPath("/upload"); 
            /*处理图片上传*/
            String bookPhotoFileName = ""; 
       	 	if(bookPhotoFile != null) {
       	 		InputStream is = new FileInputStream(bookPhotoFile);
       			String fileContentType = this.getBookPhotoFileContentType();
       			if(fileContentType.equals("image/jpeg")  || fileContentType.equals("image/pjpeg"))
       				bookPhotoFileName = UUID.randomUUID().toString() +  ".jpg";
       			else if(fileContentType.equals("image/gif"))
       				bookPhotoFileName = UUID.randomUUID().toString() +  ".gif";
       			else {
       				ctx.put("error",  java.net.URLEncoder.encode("上传图片格式不正确!"));
       				return "error";
       			}
       			File file = new File(path, bookPhotoFileName);
       			OutputStream os = new FileOutputStream(file);
       			byte[] b = new byte[1024];
       			int bs = 0;
       			while ((bs = is.read(b)) > 0) {
       				os.write(b, 0, bs);
       			}
       			is.close();
       			os.close();
            bo1.setBookPhoto("upload/" + bookPhotoFileName);
       	 	}
            bo1DAO.UpdateBo1(bo1);
            ctx.put("message",  java.net.URLEncoder.encode("Bo1信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
             e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("Bo1信息更新失败!"));
            return "error";
       }
    }

    /*删除图书信息*/
    public String DeleteBo1() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            bo1DAO.DeleteBo1(barcode);
            ctx.put("message",  java.net.URLEncoder.encode("Bo1删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("Bo1删除失败!"));
            return "error";
        }
    }
}

最近下载更多
2716804680  LV9 2023年12月6日
ziv5466123  LV7 2023年6月26日
stonerose66  LV1 2023年6月23日
北方菜  LV11 2023年4月16日
2017143155  LV12 2022年11月14日
gch666  LV6 2022年6月8日
hebe灬  LV5 2022年5月21日
isfrand  LV4 2022年4月19日
丁春秋  LV2 2022年3月15日
Lin一点  LV4 2021年12月16日
最近浏览更多
pi-nang  LV2 3月11日
duoduo1234  LV2 1月15日
吴霞霞 2023年12月28日
暂无贡献等级
WBelong  LV7 2023年12月27日
ysugxx  LV9 2023年12月10日
2716804680  LV9 2023年12月6日
冀小鹏 2023年11月29日
暂无贡献等级
uio123  LV2 2023年11月4日
lyq6666666  LV5 2023年10月25日
iddlsdls  LV1 2023年10月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友