package com.mobileserver.dao;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.mobileserver.domain.Teacher_QQ287307421;
import com.mobileserver.util.DB;

public class Teacher_QQ287307421DAO {

	public List<Teacher_QQ287307421> QueryTeacher_QQ287307421(String teacherNumber,String teacherName,Timestamp teacherBirthday,Timestamp teacherArriveDate) {
		List<Teacher_QQ287307421> teacher_QQ287307421List = new ArrayList<Teacher_QQ287307421>();
		DB db = new DB();
		String sql = "select * from Teacher_QQ287307421 where 1=1";
		if (!teacherNumber.equals(""))
			sql += " and teacherNumber like '%" + teacherNumber + "%'";
		if (!teacherName.equals(""))
			sql += " and teacherName like '%" + teacherName + "%'";
		if(teacherBirthday!=null)
			sql += " and teacherBirthday='" + teacherBirthday + "'";
		if(teacherArriveDate!=null)
			sql += " and teacherArriveDate='" + teacherArriveDate + "'";
		try {
			ResultSet rs = db.executeQuery(sql);
			while (rs.next()) {
				Teacher_QQ287307421 teacher_QQ287307421 = new Teacher_QQ287307421();
				teacher_QQ287307421.setTeacherNumber(rs.getString("teacherNumber"));
				teacher_QQ287307421.setTeacherName(rs.getString("teacherName"));
				teacher_QQ287307421.setTeacherPassword(rs.getString("teacherPassword"));
				teacher_QQ287307421.setTeacherSex(rs.getString("teacherSex"));
				teacher_QQ287307421.setTeacherBirthday(rs.getTimestamp("teacherBirthday"));
				teacher_QQ287307421.setTeacherArriveDate(rs.getTimestamp("teacherArriveDate"));
				teacher_QQ287307421.setTeacherCardNumber(rs.getString("teacherCardNumber"));
				teacher_QQ287307421.setTeacherPhone(rs.getString("teacherPhone"));
				teacher_QQ287307421.setTeacherPhoto(rs.getString("teacherPhoto"));
				teacher_QQ287307421.setTeacherAddress(rs.getString("teacherAddress"));
				teacher_QQ287307421.setTeacherMemo(rs.getString("teacherMemo"));
				teacher_QQ287307421List.add(teacher_QQ287307421);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.all_close();
		}
		return teacher_QQ287307421List;
	}
	/* 传入教师信息对象，进行教师信息的添加业务 */
	public String AddTeacher_QQ287307421(Teacher_QQ287307421 teacher_QQ287307421) {
		DB db = new DB();
		String result = "";
		try {
			/* 构建sql执行插入新教师信息 */
			String sqlString = "insert into Teacher_QQ287307421(teacherNumber,teacherName,teacherPassword,teacherSex,teacherBirthday,teacherArriveDate,teacherCardNumber,teacherPhone,teacherPhoto,teacherAddress,teacherMemo) values (";
			sqlString += "'" + teacher_QQ287307421.getTeacherNumber() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherName() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherPassword() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherSex() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherBirthday() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherArriveDate() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherCardNumber() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherPhone() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherPhoto() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherAddress() + "',";
			sqlString += "'" + teacher_QQ287307421.getTeacherMemo() + "'";
			sqlString += ")";
			db.executeUpdate(sqlString);
			result = "教师信息添加成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "教师信息添加失败";
		} finally {
			db.all_close();
		}
		return result;
	}
	/* 删除教师信息 */
	public String DeleteTeacher_QQ287307421(String teacherNumber) {
		DB db = new DB();
		String result = "";
		try {
			String sqlString = "delete from Teacher_QQ287307421 where teacherNumber='" + teacherNumber + "'";
			db.executeUpdate(sqlString);
			result = "教师信息删除成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "教师信息删除失败";
		} finally {
			db.all_close();
		}
		return result;
	}

	/* 根据教师编号获取到教师信息 */
	public Teacher_QQ287307421 GetTeacher_QQ287307421(String teacherNumber) {
		Teacher_QQ287307421 teacher_QQ287307421 = null;
		DB db = new DB();
		String sql = "select * from Teacher_QQ287307421 where teacherNumber='" + teacherNumber + "'";
		try {
			ResultSet rs = db.executeQuery(sql);
			if (rs.next()) {
				teacher_QQ287307421 = new Teacher_QQ287307421();
				teacher_QQ287307421.setTeacherNumber(rs.getString("teacherNumber"));
				teacher_QQ287307421.setTeacherName(rs.getString("teacherName"));
				teacher_QQ287307421.setTeacherPassword(rs.getString("teacherPassword"));
				teacher_QQ287307421.setTeacherSex(rs.getString("teacherSex"));
				teacher_QQ287307421.setTeacherBirthday(rs.getTimestamp("teacherBirthday"));
				teacher_QQ287307421.setTeacherArriveDate(rs.getTimestamp("teacherArriveDate"));
				teacher_QQ287307421.setTeacherCardNumber(rs.getString("teacherCardNumber"));
				teacher_QQ287307421.setTeacherPhone(rs.getString("teacherPhone"));
				teacher_QQ287307421.setTeacherPhoto(rs.getString("teacherPhoto"));
				teacher_QQ287307421.setTeacherAddress(rs.getString("teacherAddress"));
				teacher_QQ287307421.setTeacherMemo(rs.getString("teacherMemo"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.all_close();
		}
		return teacher_QQ287307421;
	}
	/* 更新教师信息 */
	public String UpdateTeacher_QQ287307421(Teacher_QQ287307421 teacher_QQ287307421) {
		DB db = new DB();
		String result = "";
		try {
			String sql = "update Teacher_QQ287307421 set ";
			sql += "teacherName='" + teacher_QQ287307421.getTeacherName() + "',";
			sql += "teacherPassword='" + teacher_QQ287307421.getTeacherPassword() + "',";
			sql += "teacherSex='" + teacher_QQ287307421.getTeacherSex() + "',";
			sql += "teacherBirthday='" + teacher_QQ287307421.getTeacherBirthday() + "',";
			sql += "teacherArriveDate='" + teacher_QQ287307421.getTeacherArriveDate() + "',";
			sql += "teacherCardNumber='" + teacher_QQ287307421.getTeacherCardNumber() + "',";
			sql += "teacherPhone='" + teacher_QQ287307421.getTeacherPhone() + "',";
			sql += "teacherPhoto='" + teacher_QQ287307421.getTeacherPhoto() + "',";
			sql += "teacherAddress='" + teacher_QQ287307421.getTeacherAddress() + "',";
			sql += "teacherMemo='" + teacher_QQ287307421.getTeacherMemo() + "'";
			sql += " where teacherNumber='" + teacher_QQ287307421.getTeacherNumber() + "'";
			db.executeUpdate(sql);
			result = "教师信息更新成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "教师信息更新失败";
		} finally {
			db.all_close();
		}
		return result;
	}
}
