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.CourseInfo_QQ254540457DAO;
import com.shuangyulin.domain.CourseInfo_QQ254540457;
import com.shuangyulin.dao.Teacher_QQ287307421DAO;
import com.shuangyulin.domain.Teacher_QQ287307421;
import com.shuangyulin.utils.ExportExcelUtil;

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

    /*界面层需要查询的属性: 课程编号*/
    private String courseNumber;
    public void setCourseNumber(String courseNumber) {
        this.courseNumber = courseNumber;
    }
    public String getCourseNumber() {
        return this.courseNumber;
    }

    /*界面层需要查询的属性: 课程名称*/
    private String courseName;
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public String getCourseName() {
        return this.courseName;
    }

    /*界面层需要查询的属性: 上课老师*/
    private Teacher_QQ287307421 courseTeacher;
    public void setCourseTeacher(Teacher_QQ287307421 courseTeacher) {
        this.courseTeacher = courseTeacher;
    }
    public Teacher_QQ287307421 getCourseTeacher() {
        return this.courseTeacher;
    }

    /*当前第几页*/
    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 Teacher_QQ287307421DAO teacher_QQ287307421DAO;
    @Resource CourseInfo_QQ254540457DAO courseInfo_QQ254540457DAO;

    /*待操作的CourseInfo_QQ254540457对象*/
    private CourseInfo_QQ254540457 courseInfo_QQ254540457;
    public void setCourseInfo_QQ254540457(CourseInfo_QQ254540457 courseInfo_QQ254540457) {
        this.courseInfo_QQ254540457 = courseInfo_QQ254540457;
    }
    public CourseInfo_QQ254540457 getCourseInfo_QQ254540457() {
        return this.courseInfo_QQ254540457;
    }

    /*跳转到添加CourseInfo_QQ254540457视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的Teacher_QQ287307421信息*/
        List<Teacher_QQ287307421> teacher_QQ287307421List = teacher_QQ287307421DAO.QueryAllTeacher_QQ287307421Info();
        ctx.put("teacher_QQ287307421List", teacher_QQ287307421List);
        return "add_view";
    }

    /*添加CourseInfo_QQ254540457信息*/
    @SuppressWarnings("deprecation")
    public String AddCourseInfo_QQ254540457() {
        ActionContext ctx = ActionContext.getContext();
        /*验证课程编号是否已经存在*/
        String courseNumber = courseInfo_QQ254540457.getCourseNumber();
        CourseInfo_QQ254540457 db_courseInfo_QQ254540457 = courseInfo_QQ254540457DAO.GetCourseInfo_QQ254540457ByCourseNumber(courseNumber);
        if(null != db_courseInfo_QQ254540457) {
            ctx.put("error",  java.net.URLEncoder.encode("该课程编号已经存在!"));
            return "error";
        }
        try {
            if(true) {
            Teacher_QQ287307421 courseTeacher = teacher_QQ287307421DAO.GetTeacher_QQ287307421ByTeacherNumber(courseInfo_QQ254540457.getCourseTeacher().getTeacherNumber());
            courseInfo_QQ254540457.setCourseTeacher(courseTeacher);
            }
            courseInfo_QQ254540457DAO.AddCourseInfo_QQ254540457(courseInfo_QQ254540457);
            ctx.put("message",  java.net.URLEncoder.encode("CourseInfo_QQ254540457添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CourseInfo_QQ254540457添加失败!"));
            return "error";
        }
    }

    /*查询CourseInfo_QQ254540457信息*/
    public String QueryCourseInfo_QQ254540457() {
        if(currentPage == 0) currentPage = 1;
        if(courseNumber == null) courseNumber = "";
        if(courseName == null) courseName = "";
        List<CourseInfo_QQ254540457> courseInfo_QQ254540457List = courseInfo_QQ254540457DAO.QueryCourseInfo_QQ254540457Info(courseNumber, courseName, courseTeacher, currentPage);
        /*计算总的页数和总的记录数*/
        courseInfo_QQ254540457DAO.CalculateTotalPageAndRecordNumber(courseNumber, courseName, courseTeacher);
        /*获取到总的页码数目*/
        totalPage = courseInfo_QQ254540457DAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = courseInfo_QQ254540457DAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("courseInfo_QQ254540457List",  courseInfo_QQ254540457List);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("courseNumber", courseNumber);
        ctx.put("courseName", courseName);
        ctx.put("courseTeacher", courseTeacher);
        List<Teacher_QQ287307421> teacher_QQ287307421List = teacher_QQ287307421DAO.QueryAllTeacher_QQ287307421Info();
        ctx.put("teacher_QQ287307421List", teacher_QQ287307421List);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryCourseInfo_QQ254540457OutputToExcel() { 
        if(courseNumber == null) courseNumber = "";
        if(courseName == null) courseName = "";
        List<CourseInfo_QQ254540457> courseInfo_QQ254540457List = courseInfo_QQ254540457DAO.QueryCourseInfo_QQ254540457Info(courseNumber,courseName,courseTeacher);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "CourseInfo_QQ254540457信息记录"; 
        String[] headers = { "课程编号","课程名称","上课老师","上课时间","上课地点","课程学分"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<courseInfo_QQ254540457List.size();i++) {
        	CourseInfo_QQ254540457 courseInfo_QQ254540457 = courseInfo_QQ254540457List.get(i); 
        	dataset.add(new String[]{courseInfo_QQ254540457.getCourseNumber(),courseInfo_QQ254540457.getCourseName(),courseInfo_QQ254540457.getCourseTeacher().getTeacherName(),
courseInfo_QQ254540457.getCourseTime(),courseInfo_QQ254540457.getCoursePlace(),courseInfo_QQ254540457.getCourseScore() + ""});
        }
        /*
        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="+"CourseInfo_QQ254540457.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;
    }
    /*前台查询CourseInfo_QQ254540457信息*/
    public String FrontQueryCourseInfo_QQ254540457() {
        if(currentPage == 0) currentPage = 1;
        if(courseNumber == null) courseNumber = "";
        if(courseName == null) courseName = "";
        List<CourseInfo_QQ254540457> courseInfo_QQ254540457List = courseInfo_QQ254540457DAO.QueryCourseInfo_QQ254540457Info(courseNumber, courseName, courseTeacher, currentPage);
        /*计算总的页数和总的记录数*/
        courseInfo_QQ254540457DAO.CalculateTotalPageAndRecordNumber(courseNumber, courseName, courseTeacher);
        /*获取到总的页码数目*/
        totalPage = courseInfo_QQ254540457DAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = courseInfo_QQ254540457DAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("courseInfo_QQ254540457List",  courseInfo_QQ254540457List);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("courseNumber", courseNumber);
        ctx.put("courseName", courseName);
        ctx.put("courseTeacher", courseTeacher);
        List<Teacher_QQ287307421> teacher_QQ287307421List = teacher_QQ287307421DAO.QueryAllTeacher_QQ287307421Info();
        ctx.put("teacher_QQ287307421List", teacher_QQ287307421List);
        return "front_query_view";
    }

    /*查询要修改的CourseInfo_QQ254540457信息*/
    public String ModifyCourseInfo_QQ254540457Query() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键courseNumber获取CourseInfo_QQ254540457对象*/
        CourseInfo_QQ254540457 courseInfo_QQ254540457 = courseInfo_QQ254540457DAO.GetCourseInfo_QQ254540457ByCourseNumber(courseNumber);

        List<Teacher_QQ287307421> teacher_QQ287307421List = teacher_QQ287307421DAO.QueryAllTeacher_QQ287307421Info();
        ctx.put("teacher_QQ287307421List", teacher_QQ287307421List);
        ctx.put("courseInfo_QQ254540457",  courseInfo_QQ254540457);
        return "modify_view";
    }

    /*查询要修改的CourseInfo_QQ254540457信息*/
    public String FrontShowCourseInfo_QQ254540457Query() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键courseNumber获取CourseInfo_QQ254540457对象*/
        CourseInfo_QQ254540457 courseInfo_QQ254540457 = courseInfo_QQ254540457DAO.GetCourseInfo_QQ254540457ByCourseNumber(courseNumber);

        List<Teacher_QQ287307421> teacher_QQ287307421List = teacher_QQ287307421DAO.QueryAllTeacher_QQ287307421Info();
        ctx.put("teacher_QQ287307421List", teacher_QQ287307421List);
        ctx.put("courseInfo_QQ254540457",  courseInfo_QQ254540457);
        return "front_show_view";
    }

    /*更新修改CourseInfo_QQ254540457信息*/
    public String ModifyCourseInfo_QQ254540457() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            Teacher_QQ287307421 courseTeacher = teacher_QQ287307421DAO.GetTeacher_QQ287307421ByTeacherNumber(courseInfo_QQ254540457.getCourseTeacher().getTeacherNumber());
            courseInfo_QQ254540457.setCourseTeacher(courseTeacher);
            }
            courseInfo_QQ254540457DAO.UpdateCourseInfo_QQ254540457(courseInfo_QQ254540457);
            ctx.put("message",  java.net.URLEncoder.encode("CourseInfo_QQ254540457信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CourseInfo_QQ254540457信息更新失败!"));
            return "error";
       }
   }

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

}
