package com.sai.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.sai.dto.Attention;
import com.sai.dto.Userinfo;
import com.sai.global.ConnTool;
/*
* 该dao完成以下功能
* 搜索(根据学历,年龄,性别,地区查询(分页)) query(education sex...)
* 我关注的attentionforme
* 关注我的attentiontome
* 添加关注
* 取消关注
* 推荐异性recommend
* 根据昵称获取用户信息
*/
public class QueryDao {
public static int PAGESIZE = 4;
public static int QUERYSIZE = 10;
public static int RECOMMEND = 4;
//根据学历,年龄,性别,地区查询(分页)
public List<Userinfo> query(String education, int ageMin, int ageMax, String sex, String address, int pageNo){
List<Userinfo> userList = new ArrayList<Userinfo>();
List<Userinfo> userListLast = new ArrayList<Userinfo>();
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
//获取当前年份
int year = c.get(Calendar.YEAR);
//根据年龄算出出生年份
int yearMax = year - ageMin;
// 最大年份
int yearMin = year - ageMax;
System.out.println("学历: " + education);
System.out.println("性别: " + sex);
System.out.println("地址: " + address);
System.out.println("页数: " + pageNo);
System.out.println("年龄区间: " + ageMin + "--" + ageMax);
try {
Connection conn = ConnTool.getConn();
//查询数据库汇总education为输入学历的人
PreparedStatement ppt = null;
ResultSet rs = null;
for(int i = yearMin; i <= yearMax; i++ ){
String sql = "select * from userinfo where education = ? and birthday like ? and sex = ? and address = ? order by username";
ppt = conn.prepareStatement(sql);
ppt.setString(1, education);
ppt.setString(2, i + "%");
ppt.setString(3, sex);
ppt.setString(4, address);
rs = ppt.executeQuery();
while(rs.next()){
String username = rs.getString("username");
String nickname = rs.getString("nickname");
String name = rs.getString("name");
String password = rs.getString("password");
String birthday = rs.getString("birthday");
int height = rs.getInt("height");
String marriage = rs.getString("marriage");
int salary = rs.getInt("salary");
String introduction = rs.getString("introduction");
Userinfo userinfo = new Userinfo(username, sex, nickname, name, password, birthday, height, education, marriage, salary, introduction, address);
userList.add(userinfo);
}
}
if(userList.size() > 0){
if(userList.size() < pageNo * QUERYSIZE){
for(int j = ((pageNo -1) * QUERYSIZE); j <userList.size(); j++){
userListLast.add(userList.get(j));
}
}else{
for(int j = ((pageNo -1) * QUERYSIZE); j < pageNo * QUERYSIZE; j++){
userListLast.add(userList.get(j));
}
}
}
//关流
ConnTool.closeConn(rs, ppt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return userListLast;
}
//根据学历,年龄,性别,地区查询出的用户总数
public int getTotalNum(String education, int ageMin, int ageMax, String sex, String address, int pageNo){
int number = 0;
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
//获取当前年份
int year = c.get(Calendar.YEAR);
//根据年龄算出出生年份
int yearMax = year - ageMin;
// 最大年份
int yearMin = year - ageMax;
try {
Connection conn = ConnTool.getConn();
//查询数据库汇总education为输入学历的人
PreparedStatement ppt = null;
ResultSet rs = null;
for(int i = yearMin; i <= yearMax; i++ ){
String sql = "select count(*) from userinfo where education = ? and birthday like ? and sex = ? and address = ?";
ppt = conn.prepareStatement(sql);
ppt.setString(1, education);
ppt.setString(2, i + "%");
ppt.setString(3, sex);
ppt.setString(4, address);
rs = ppt.executeQuery();
rs.next();
//添加数目
number += rs.getInt(1);
}
//关流
ConnTool.closeConn(rs, ppt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return number;
}
//根据education查询出来的总页数
public int getTotalPage(String education, int ageMin, int ageMax, String sex, String address, int pageNo){
return (getTotalNum(education, ageMin, ageMax, sex, address, pageNo) % QUERYSIZE == 0) ? getTotalNum(education, ageMin, ageMax, sex, address, pageNo)
/ QUERYSIZE
: getTotalNum(education, ageMin, ageMax, sex, address, pageNo) / QUERYSIZE + 1;
}
//根据用户用户名获得用户信息
public Userinfo getUserInfoByUsername(String username){
Userinfo userinfo = null;
try {
Connection conn = ConnTool.getConn();
String sql = "select * from userinfo where username = ?";
PreparedStatement ppt = conn.prepareStatement(sql);
ppt.setString(1, username);
ResultSet rs = ppt.executeQuery();
rs.next();
String sex = rs.getString("sex");
String nickname = rs.getString("nickname");
String name = rs.getString("name");
String password = rs.getString("password");
String birthday = rs.getString("birthday");
int height = rs.getInt("height");
String education = rs.getString("education");
String marriage = rs.getString("marriage");
int salary = rs.getInt("salary");
String introduction = rs.getString("introduction");
String address = rs.getString("address");
userinfo = new Userinfo(username, sex, nickname, name, password, birthday, height, education, marriage, salary, introduction, address);
//关流
ConnTool.closeConn(rs, ppt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return userinfo;
}
//查询是否为用户关注
public boolean checkAttention(String whoAttention, String attentionwho){
boolean flag = false;
try {
Connection conn = ConnTool.getConn();
//查询数据库汇中关注者为输入用户昵称的所有被关注者
String sql = "select count(*) from attention where whoattention = ? and attentionwho = ?";
PreparedStatement ppt = conn.prepareStatement(sql);
ppt.setString(1, whoAttention);
ppt.setString(2, attentionwho);
ResultSet rs = ppt.executeQuery();
rs.next();
int size = rs.getInt(1);
if(size == 1){
flag = true;
}
//关流
ConnTool.closeConn(rs, ppt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//添加关注,成功后返回当前序列值
public int addAttention(String whoAttention, String attentionwho){
int seq = 0;
try {
Connection conn = ConnTool.getConn();
//查询数据库汇中关注者为输入用户昵称的所有被关注者
String sql = "insert into attention values(attentionseq.nextval, ?, ?)";
PreparedStatement ppt = conn.prepareStatement(sql);
ppt.setString(1, whoAttention);
ppt.setString(2, attentionwho);
ppt.execute();
//执行成功后,获取序列号的当前值,即该attentionid
String getSeq = "select attentionseq.currval from dual";
ppt = conn.prepareStatement(getSeq);
ResultSet rs = ppt.executeQuery();
rs.next();
seq = rs.getInt(1);
//关流
ConnTool.closeConn(rs, ppt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return seq;
}
//取消关注
public boolean deleteAttention(int id){
boolean flag = false;
try {
Connection conn = ConnTool.getConn();
//查询数据库汇中关注者为输入用户昵称的所有被关注者
String sql = "delete attention where id = ?";
PreparedStatement ppt = conn.prepareStatement(sql);
ppt.setInt(1, id);
flag = ppt.execute