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.BoTDAO;
import com.bysj.domain.BoT;
@Controller @Scope("prototype")
public class BoTAction extends ActionSupport {

    /*当前第几页*/
    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 bookTypeId;
    public void setBookTypeId(int bookTypeId) {
        this.bookTypeId = bookTypeId;
    }

    public int getBookTypeId() {
        return bookTypeId;
    }
    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource BoTDAO boTDAO;


    /*待操作的BoT对象*/
    private BoT boT;
    public void setBoT(BoT boT) {
        this.boT = boT;
    }
    public BoT getBoT() {
        return this.boT;
    }
    /*跳转到添加图书类型视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        return "add_view";
    }
  /*添加图书类型信息*/
  @SuppressWarnings("deprecation")
  public String AddBoT() {
      ActionContext ctx = ActionContext.getContext();
      try {
          boTDAO.AddBoT(boT);
          ctx.put("message",  java.net.URLEncoder.encode("BoT添加成功!"));
          return "add_success";
      } catch (Exception e) {
          e.printStackTrace();
          ctx.put("error",  java.net.URLEncoder.encode("BoT添加失败!"));
          return "error";
      }
  }
    /*查询图书类型*/
    public String QueryBoT() {
        if(currentPage == 0) currentPage = 1;
        List<BoT> boTList = boTDAO.QueryBoTInfo( currentPage);
        /*计算总的页数和总的记录数*/
        boTDAO.CalculateTotalPageAndRecordNumber();
        /*获取到总的页码数目*/ 
        totalPage = boTDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = boTDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("boTList",  boTList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryBoTOutputToExcel() {
        List<BoT> boTList = boTDAO.QueryBoTInfo( currentPage);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "BoT信息记录";
        String[] headers = {"图书类别","类别名称","可借阅天数"};
        List<String[]> dataset = new ArrayList<String[]>();
        for(int i=0;i<boTList.size();i++) {
        	BoT boT = boTList.get(i);
        	dataset.add(new String[]{boT.getBookTypeId() + "",boT.getBookTypeName(),boT.getDays() + ""});
        }
        /*
        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="+"BoT.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 FrontQueryBoT() {
        if(currentPage == 0) currentPage = 1;
        List<BoT> boTList = boTDAO.QueryBoTInfo( currentPage);
        /*计算总的页数和总的记录数*/
        boTDAO.CalculateTotalPageAndRecordNumber();
        /*获取到总的页码数目*/ 
        totalPage = boTDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = boTDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("boTList",  boTList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        return "front_query_view";
    }

    /*查询要修改的BoT信息*/
    public String ModifyBoTQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键bookTypeId获取BoT对象*/
        BoT boT = boTDAO.GetBoTByBookTypeId(bookTypeId);
        
        ctx.put("boT",  boT);
        return "modify_view";
    }

    /*查询要修改的BoT信息*/
    public String FrontShowBoTQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键bookTypeId获取BoT对象*/
        BoT boT = boTDAO.GetBoTByBookTypeId(bookTypeId);
        
        ctx.put("boT",  boT);
        return "front_show_view";
    }


    /*更新修改图书类型信息*/
    public String ModifyBoT() {
      ActionContext ctx = ActionContext.getContext();
      try {
            boTDAO.UpdateBoT(boT);
            ctx.put("message",  java.net.URLEncoder.encode("BoT信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
             e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("BoT信息更新失败!"));
            return "error";
       }
    }

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

最近下载更多
2716804680  LV8 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  LV8 2023年12月6日
冀小鹏 2023年11月29日
暂无贡献等级
uio123  LV2 2023年11月4日
lyq6666666  LV5 2023年10月25日
iddlsdls  LV1 2023年10月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友