package com.shuangyulin.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.text.SimpleDateFormat;
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 java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shuangyulin.dao.Student_QQ287307421DAO;
import com.shuangyulin.domain.Student_QQ287307421;
import com.shuangyulin.dao.ClassInfoDAO;
import com.shuangyulin.domain.ClassInfo;
import com.shuangyulin.utils.ExportExcelUtil;

@Controller @Scope("prototype")
public class Student_QQ287307421Action extends ActionSupport {

/*图片字段studentPhoto参数接收*/
	 private File studentPhotoFile;
	 private String studentPhotoFileFileName;
	 private String studentPhotoFileContentType;
	 public File getStudentPhotoFile() {
		return studentPhotoFile;
	}
	public void setStudentPhotoFile(File studentPhotoFile) {
		this.studentPhotoFile = studentPhotoFile;
	}
	public String getStudentPhotoFileFileName() {
		return studentPhotoFileFileName;
	}
	public void setStudentPhotoFileFileName(String studentPhotoFileFileName) {
		this.studentPhotoFileFileName = studentPhotoFileFileName;
	}
	public String getStudentPhotoFileContentType() {
		return studentPhotoFileContentType;
	}
	public void setStudentPhotoFileContentType(String studentPhotoFileContentType) {
		this.studentPhotoFileContentType = studentPhotoFileContentType;
	}
    /*界面层需要查询的属性: 学号*/
    private String studentNumber;
    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }
    public String getStudentNumber() {
        return this.studentNumber;
    }

    /*界面层需要查询的属性: 姓名*/
    private String studentName;
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getStudentName() {
        return this.studentName;
    }

    /*界面层需要查询的属性: 所在班级*/
    private ClassInfo studentClassNumber;
    public void setStudentClassNumber(ClassInfo studentClassNumber) {
        this.studentClassNumber = studentClassNumber;
    }
    public ClassInfo getStudentClassNumber() {
        return this.studentClassNumber;
    }

    /*界面层需要查询的属性: 出生日期*/
    private String studentBirthday;
    public void setStudentBirthday(String studentBirthday) {
        this.studentBirthday = studentBirthday;
    }
    public String getStudentBirthday() {
        return this.studentBirthday;
    }

    /*当前第几页*/
    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 ClassInfoDAO classInfoDAO;
    @Resource Student_QQ287307421DAO student_QQ287307421DAO;

    /*待操作的Student_QQ287307421对象*/
    private Student_QQ287307421 student_QQ287307421;
    public void setStudent_QQ287307421(Student_QQ287307421 student_QQ287307421) {
        this.student_QQ287307421 = student_QQ287307421;
    }
    public Student_QQ287307421 getStudent_QQ287307421() {
        return this.student_QQ287307421;
    }

    /*跳转到添加Student_QQ287307421视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的ClassInfo信息*/
        List<ClassInfo> classInfoList = classInfoDAO.QueryAllClassInfoInfo();
        ctx.put("classInfoList", classInfoList);
        return "add_view";
    }

    /*添加Student_QQ287307421信息*/
    @SuppressWarnings("deprecation")
    public String AddStudent_QQ287307421() {
        ActionContext ctx = ActionContext.getContext();
        /*验证学号是否已经存在*/
        String studentNumber = student_QQ287307421.getStudentNumber();
        Student_QQ287307421 db_student_QQ287307421 = student_QQ287307421DAO.GetStudent_QQ287307421ByStudentNumber(studentNumber);
        if(null != db_student_QQ287307421) {
            ctx.put("error",  java.net.URLEncoder.encode("该学号已经存在!"));
            return "error";
        }
        try {
            if(true) {
            ClassInfo studentClassNumber = classInfoDAO.GetClassInfoByClassNumber(student_QQ287307421.getStudentClassNumber().getClassNumber());
            student_QQ287307421.setStudentClassNumber(studentClassNumber);
            }
            String path = ServletActionContext.getServletContext().getRealPath("/upload"); 
            /*处理图片上传*/
            String studentPhotoFileName = ""; 
       	 	if(studentPhotoFile != null) {
       	 		InputStream is = new FileInputStream(studentPhotoFile);
       			String fileContentType = this.getStudentPhotoFileContentType();
       			if(fileContentType.equals("image/jpeg")  || fileContentType.equals("image/pjpeg"))
       				studentPhotoFileName = UUID.randomUUID().toString() +  ".jpg";
       			else if(fileContentType.equals("image/gif"))
       				studentPhotoFileName = UUID.randomUUID().toString() +  ".gif";
       			else {
       				ctx.put("error",  java.net.URLEncoder.encode("上传图片格式不正确!"));
       				return "error";
       			}
       			File file = new File(path, studentPhotoFileName);
       			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(studentPhotoFile != null)
            	student_QQ287307421.setStudentPhoto("upload/" + studentPhotoFileName);
            else
            	student_QQ287307421.setStudentPhoto("upload/NoImage.jpg");
            student_QQ287307421DAO.AddStudent_QQ287307421(student_QQ287307421);
            ctx.put("message",  java.net.URLEncoder.encode("Student_QQ287307421添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("Student_QQ287307421添加失败!"));
            return "error";
        }
    }

    /*查询Student_QQ287307421信息*/
    public String QueryStudent_QQ287307421() {
        if(currentPage == 0) currentPage = 1;
        if(studentNumber == null) studentNumber = "";
        if(studentName == null) studentName = "";
        if(studentBirthday == null) studentBirthday = "";
        List<Student_QQ287307421> student_QQ287307421List = student_QQ287307421DAO.QueryStudent_QQ287307421Info(studentNumber, studentName, studentClassNumber, studentBirthday, currentPage);
        /*计算总的页数和总的记录数*/
        student_QQ287307421DAO.CalculateTotalPageAndRecordNumber(studentNumber, studentName, studentClassNumber, studentBirthday);
        /*获取到总的页码数目*/
        totalPage = student_QQ287307421DAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = student_QQ287307421DAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("student_QQ287307421List",  student_QQ287307421List);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("studentNumber", studentNumber);
        ctx.put("studentName", studentName);
        ctx.put("studentClassNumber", studentClassNumber);
        List<ClassInfo> classInfoList = classInfoDAO.QueryAllClassInfoInfo();
        ctx.put("classInfoList", classInfoList);
        ctx.put("studentBirthday", studentBirthday);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryStudent_QQ287307421OutputToExcel() { 
        if(studentNumber == null) studentNumber = "";
        if(studentName == null) studentName = "";
        if(studentBirthday == null) studentBirthday = "";
        List<Student_QQ287307421> student_QQ287307421List = student_QQ287307421DAO.QueryStudent_QQ287307421Info(studentNumber,studentName,studentClassNumber,studentBirthday);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "Student_QQ287307421信息记录"; 
        String[] headers = { "学号","姓名","性别","所在班级","出生日期","政治面貌","学生照片","联系电话","学生邮箱","联系qq"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<student_QQ287307421List.size();i++) {
        	Student_QQ287307421 student_QQ287307421 = student_QQ287307421List.get(i); 
        	dataset.add(new String[]{student_QQ287307421.getStudentNumber(),student_QQ287307421.getStudentName(),student_QQ287307421.getStudentSex(),student_QQ287307421.getStudentClassNumber().getClassName(),
new SimpleDateFormat("yyyy-MM-dd").format(student_QQ287307421.getStudentBirthday()),student_QQ287307421.getStudentState(),student_QQ287307421.getStudentPhoto(),student_QQ287307421.getStudentTelephone(),student_QQ287307421.getStudentEmail(),student_QQ287307421.getStudentQQ()});
        }
        /*
        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="+"Student_QQ287307421.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;
    }
    /*前台查询Student_QQ287307421信息*/
    public String FrontQueryStudent_QQ287307421() {
        if(currentPage == 0) currentPage = 1;
        if(studentNumber == null) studentNumber = "";
        if(studentName == null) studentName = "";
        if(studentBirthday == null) studentBirthday = "";
        List<Student_QQ287307421> student_QQ287307421List = student_QQ287307421DAO.QueryStudent_QQ287307421Info(studentNumber, studentName, studentClassNumber, studentBirthday, currentPage);
        /*计算总的页数和总的记录数*/
        student_QQ287307421DAO.CalculateTotalPageAndRecordNumber(studentNumber, studentName, studentClassNumber, studentBirthday);
        /*获取到总的页码数目*/
        totalPage = student_QQ287307421DAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = student_QQ287307421DAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("student_QQ287307421List",  student_QQ287307421List);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("studentNumber", studentNumber);
        ctx.put("studentName", studentName);
        ctx.put("studentClassNumber", studentClassNumber);
        List<ClassInfo> classInfoList = classInfoDAO.QueryAllClassInfoInfo();
        ctx.put("classInfoList", classInfoList);
        ctx.put("studentBirthday", studentBirthday);
        return "front_query_view";
    }

    /*查询要修改的Student_QQ287307421信息*/
    public String ModifyStudent_QQ287307421Query() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键studentNumber获取Student_QQ287307421对象*/
        Student_QQ287307421 student_QQ287307421 = student_QQ287307421DAO.GetStudent_QQ287307421ByStudentNumber(studentNumber);

        List<ClassInfo> classInfoList = classInfoDAO.QueryAllClassInfoInfo();
        ctx.put("classInfoList", classInfoList);
        ctx.put("student_QQ287307421",  student_QQ287307421);
        return "modify_view";
    }

    /*查询要修改的Student_QQ287307421信息*/
    public String FrontShowStudent_QQ287307421Query() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键studentNumber获取Student_QQ287307421对象*/
        Student_QQ287307421 student_QQ287307421 = student_QQ287307421DAO.GetStudent_QQ287307421ByStudentNumber(studentNumber);

        List<ClassInfo> classInfoList = classInfoDAO.QueryAllClassInfoInfo();
        ctx.put("classInfoList", classInfoList);
        ctx.put("student_QQ287307421",  student_QQ287307421);
        return "front_show_view";
    }

    /*更新修改Student_QQ287307421信息*/
    public String ModifyStudent_QQ287307421() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            ClassInfo studentClassNumber = classInfoDAO.GetClassInfoByClassNumber(student_QQ287307421.getStudentClassNumber().getClassNumber());
            student_QQ287307421.setStudentClassNumber(studentClassNumber);
            }
            String path = ServletActionContext.getServletContext().getRealPath("/upload"); 
            /*处理图片上传*/
            String studentPhotoFileName = ""; 
       	 	if(studentPhotoFile != null) {
       	 		InputStream is = new FileInputStream(studentPhotoFile);
       			String fileContentType = this.getStudentPhotoFileContentType();
       			if(fileContentType.equals("image/jpeg") || fileContentType.equals("image/pjpeg"))
       				studentPhotoFileName = UUID.randomUUID().toString() +  ".jpg";
       			else if(fileContentType.equals("image/gif"))
       				studentPhotoFileName = UUID.randomUUID().toString() +  ".gif";
       			else {
       				ctx.put("error",  java.net.URLEncoder.encode("上传图片格式不正确!"));
       				return "error";
       			}
       			File file = new File(path, studentPhotoFileName);
       			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();
            student_QQ287307421.setStudentPhoto("upload/" + studentPhotoFileName);
       	 	}
            student_QQ287307421DAO.UpdateStudent_QQ287307421(student_QQ287307421);
            ctx.put("message",  java.net.URLEncoder.encode("Student_QQ287307421信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("Student_QQ287307421信息更新失败!"));
            return "error";
       }
   }

    /*删除Student_QQ287307421信息*/
    public String DeleteStudent_QQ287307421() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            student_QQ287307421DAO.DeleteStudent_QQ287307421(studentNumber);
            ctx.put("message",  java.net.URLEncoder.encode("Student_QQ287307421删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("Student_QQ287307421删除失败!"));
            return "error";
        }
    }

}
