package com.mita.version01.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mita.version01.constants.DataType;
import com.mita.version01.constants.SetConstants;
import com.mita.version01.dao.UserDao;
import com.mita.version01.server.Server;
import com.mita.version01.util.DButil;
import com.mita.version01.util.ServerDate;
import com.mita.version01.util.SuperData;
public class UserDaoImpl implements UserDao
{
private String[] tempInfo;
private int tempid;
public UserDaoImpl()
{
this.tempid = Server.idstart;
}
@Override
public int register(SuperData sd)
{
int id;
Connection con = DButil.connect();
tempInfo = dealinfro(sd.getStu());
String sql1 = "insert into user(_name,_password,_time,_id,_userimag,_email) values(?,?,?,?,?,?)";
String sql2 = "select _id from user";
tempid++;
try
{
PreparedStatement ps = con.prepareStatement(sql1);
ps.setString(1, tempInfo[0]);
ps.setString(2, tempInfo[1]);
ps.setString(3, ServerDate.getDateCN());
ps.setInt(4, tempid);
ps.setInt(5, 0);
ps.setString(6, "email");
int res = ps.executeUpdate();
if (res > 0)
{
PreparedStatement ps2 = con.prepareStatement(sql2);
ResultSet rs = ps2.executeQuery();
if (rs.last())
{
id = rs.getInt("_id");
createFriendtable(id);// 注册成功后,创建一个以用户id为表名的表,用于存放好友信息
return id;
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DButil.close(con);
}
return SetConstants.REGISTER_FAIL;
}
@Override
public boolean login(SuperData sd)
{
Connection con = DButil.connect();
tempInfo = dealinfro(sd.getStu());
String sql = "select * from user where _name=? and _password=?";
try
{
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,tempInfo[0]);
ps.setString(2,tempInfo[1]);
ResultSet rs = ps.executeQuery();
if (rs.first())
{
return true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DButil.close(con);
}
return false;
}
//
// /**
// * 查找自己
// */
// public User findMe(int id) {
// User me = new User();
// Connection con = DButil.connect();
// String sql = "select * from user where _id=?";
// PreparedStatement ps;
// try {
// ps = con.prepareStatement(sql);
// ps.setInt(1, id);
// ResultSet rs = ps.executeQuery();
// if (rs.first()) {
// me.setId(rs.getInt("_id"));
// me.setEmail(rs.getString("_email"));
// me.setName(rs.getString("_name"));
// me.setImg(rs.getInt("_img"));
// }
// return me;
// } catch (SQLException e) {
// // e.printStackTrace();
// } finally {
// DButil.close(con);
// }
// return null;
// }
/**
* 获取附近用户列表
* @param paramspu
* @return
*/
@Override
public SuperData getNearUsers(SuperData paramspu)
{
SuperData near_tempsu = new SuperData();
near_tempsu.setDatatype(DataType.Nearuser);
StringBuilder stub = new StringBuilder();
Connection con = DButil.connect();
String tempstr = paramspu.getStu().toString();
System.out.println(tempstr);
System.out.println("^^^^^^^^");
String sql = "select _id,_img from _"+tempstr;
System.out.println(sql);
try
{
PreparedStatement ps = con.prepareStatement(sql);
// ps.setString(1, tempstr);
ResultSet rs = ps.executeQuery();
System.out.println("I am here");
int posation_flag = 0;
while(rs.next())
{
String temp_1 = String.valueOf(rs.getInt("_id"));
System.out.println(temp_1);
String temp_2 = String.valueOf(rs.getInt("_img"));
if(posation_flag!=0)
stub.append(",");
stub.append(temp_1);
stub.append(",");
stub.append(temp_2);
posation_flag = 1;
}
System.out.println(stub.toString());
near_tempsu.setStu(stub);
return near_tempsu;
}
catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
/**
* 刷新好友列表
*/
public ArrayList<SuperData> refresh(int id) {
ArrayList<SuperData> list = new ArrayList<SuperData>();
Connection con = DButil.connect();
String sql = "select * from _? ";
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.first()) {
do {
// User friend = new User();
// friend.setId(rs.getInt("_qq"));
// friend.setName(rs.getString("_name"));
// friend.setIsOnline(rs.getInt("_isOnline"));
// friend.setImg(rs.getInt("_img"));
// friend.setGroup(rs.getInt("_group"));
// list.add(friend);
} while (rs.next());
}
return list;
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
return null;
}
/**
* 设置状态为在线
*
* @param id
*/
public void setOnline(int id) {
Connection con = DButil.connect();
try {
String sql = "update user set _isOnline=1 where _id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
updateAllOn(id);// 更新所有表状态为在线
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
/**
* 注册成功后,创建一个用户表,保存该用户好友
*
* @param id
*/
public void createFriendtable(int id) {
Connection con = DButil.connect();
try {
String sql = "create table _" + id
+ " (_num int auto_increment not null primary key,"
+ "_id int(15) not null ,"
+ "_isOnline int(11) not null default 0,"
+ "_group int(11) not null default 0,"
+ "_locationx int(20) not null default 0,"
+ "_locationy int(20) not null default 0,"
+ "_signature varchar(100) default '请设置个人签名',"
+ "_img int(11) not null default 0)";
PreparedStatement ps = con.prepareStatement(sql);
int res = ps.executeUpdate();
System.out.println(res);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DButil.close(con);
}
}
@Override
/**
* 下线更新状态为离线
*/
public void logout(int id) {
Connection con = DButil.connect();
try {
String sql = "update user set _isOnline=0 where _id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
updateAllOff(id);
// System.out.println(res);
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
/**
* 更新所有用户表状态为离线
*
* @param id
*/
public void updateAllOff(int id) {
Connection con = DButil.connect();
try {
String sql = "update _? set _isOnline=0 where _qq=?";
PreparedStatement ps = con.prepareStatement(sql);
for (int offId : getAllId()) {
ps.setInt(1, offId);
ps.setInt(2, id);
ps.executeUpdate();
}
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
/**
* 更新所有用户状态为上线
*
* @param id
*/
public void updateAllOn(int id) {
Connection con = DButil.connect();
try {
String sql = "update _? set _isOnline=1 where _qq=?";
PreparedStatement ps = con.prepareStatement(sql);
for (int OnId : getAllId()) {
ps.setInt(1, OnId);
ps.setInt(2, id);
ps.executeUpdate();
}
} catch (SQLException e) {
// e.printStackTrace();
} finally {
DButil.close(con);
}
}
public List<Integer> getAllId() {
Connection con = DButil.connect();
List<Integer> list = new ArrayList<Integer>();
try {
String sql = "select _id from user";
Pre