package com.imti.s2.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import com.imti.s2.dbcon.DataBaseAccessImpl;
import com.imti.s2.dbcon.IDataBaseAccess;
import com.imti.s2.entity.Constant;
import com.imti.s2.entity.Page;
import com.imti.s2.entity.UserInfo;
import com.imti.s2.impl.IUserInfoDao;
public class UserInfoDaoImpl implements IUserInfoDao {
//此在查寻用户时每页最多显示的数据
private final static int SELECT_ALL_PAGE_NP=10;
private IDataBaseAccess dbAccess=null;
private ArrayList<UserInfo> al=null;
private Page page=null;
public UserInfoDaoImpl(){
dbAccess=new DataBaseAccessImpl();
}
// 从数据库中查出所有的用户名单(概要信息)
public ArrayList<UserInfo> selectAll(int p){
page=dbAccess.getCPage("userInfo",null,p,SELECT_ALL_PAGE_NP);
String sqlstr="select rn,id,name,sex,loginDegree,propedom,userlevel,to_char(loginTime,"
+Constant.ORACLE_TO_DATE_FORMAT+")lo from (select rownum rn,ui.* from userInfo ui order by ui.id) where rn>="
+((page.getPage()-1)*SELECT_ALL_PAGE_NP+1)+" and rn<="+(page.getPage()*SELECT_ALL_PAGE_NP);
return arrayToArrayUserInfo(sqlstr);
}
//此方法是查询用户概要信息时使用的
private ArrayList<UserInfo> arrayToArrayUserInfo(String sqlstr) {
al=new ArrayList<UserInfo>();
dbAccess.createConnection();
ArrayList<HashMap> array=(ArrayList<HashMap>)dbAccess.execQuery(sqlstr);
for(HashMap<String,Object> hm:array){
UserInfo ui=new UserInfo();
ui.setId((Integer)hm.get("ID"));
ui.setName((String)hm.get("NAME"));
ui.setSex((String)hm.get("SEX"));
ui.setLoginDegree((Integer)hm.get("LOGINDEGREE"));
ui.setPropedom((String)hm.get("PROPEDOM"));
ui.setUserlevel((Integer)hm.get("USERLEVEL"));
ui.setLoginTime((String)hm.get("LO"));
al.add(ui);
}
return al;
}
// 当用户进入用户信息管理时,可以填写用户详细信息
public boolean userLabor(UserInfo ui){
dbAccess.createConnection();
String sqlstr="update userinfo set sName='"+ui.getSName()
+"',trueName='"+ui.getTrueName()
+"',passMatter='"+ui.getPassMatter()
+"',passResult='"+ui.getPassResult()
+"',cardId='"+ui.getCardId()
+"',post='"+ui.getPost()
+"' where id="+ui.getId();
return dbAccess.execUpdate(sqlstr);
}
// 从数据库中查出某个用户的详细资料,供用户和管理员来查看
public UserInfo selectOne(int id){
return null;
}
// 用户注册成此网站的会员
public boolean addUser(UserInfo ui){
return false;
}
// 判断用户是否存在
public boolean checkUser(String name){
dbAccess.createConnection();
String sqlstr="select count(*) c from userinfo where name='"+name+"'";
ArrayList<HashMap> al=(ArrayList<HashMap>)dbAccess.execQuery(sqlstr);
int count=(Integer)((HashMap<String,Object>)al.get(0)).get("C");
if(count==0)
return true;
else
return false;
}
// 判断用户是否登录成功,返回此用户的权限,来选择是管理员,还是普通会员,,如果失败则返回-1
public int[] loginUser(String name, String pass){
dbAccess.createConnection();
int a[]=new int[2];
String sqlstr="select propedom,id from userinfo where name='"+name+"' and pass='"+pass+"'";
ArrayList<HashMap> al=(ArrayList<HashMap>)dbAccess.execQuery(sqlstr);
for(HashMap<String,Object> hm:al){
a[0]=(Integer)hm.get("PROPEDOM");
a[1]=(Integer)hm.get("ID");
}
return a;
}
// 修改会员的权限
public boolean updateLevel(int id, int level){
dbAccess.createConnection();
String sqlstr="update userinfo set propedom="+level+" where id="+id;
return dbAccess.execUpdate(sqlstr);
}
// 会员修改个人信息,只能是部分信息,有部分信息不能够修改
public boolean updateUser(UserInfo ui){
dbAccess.createConnection();
String sqlstr="update userinfo set sName='"
+ui.getSName()+"',trueName='"
+ui.getTrueName()+"',email='"
+ui.getEmail()+"',"+"birthday=to_date(?,"
+Constant.ORACLE_TO_DATE_FORMAT+"),sex='"
+ui.getSex()+"',passMatter='"
+ui.getPassMatter()+"',"+"passResult='"
+ui.getPassResult()+"',telephone='"
+ui.getTelephone()+"',phone='"
+ui.getPhone()+"',cardId='"
+ui.getCardId()+"',post='"
+ui.getPost()+"' where id="
+ui.getId();
return dbAccess.execUpdate(sqlstr);
}
// 管理员按用户注册时间来查,按注册时间近的排在前面,有一个单选按纽,
// 选近则传来"1",久则传来"0"来查
public ArrayList<UserInfo> selectRegTime(int i, int p){
return null;
}
// 管理员用户名来查,有一个文本框,输入名字的中间一个字或整个用户名来查
// 模糊查询
public ArrayList<UserInfo> selectUserName(String name, int p){
return null;
}
// 管理员根据积分的多少来查,按积分多在前面
public ArrayList<UserInfo> selectIntegral(int p){
return null;
}
// 删除不友好的用户,此时删除此用户的所有在本站的记录
// 需要使用到事务的方式来处理
public boolean deleteUser(int id){
dbAccess.createConnection();
String sql="delete from userinfo where id="+id;
String sql2="delete from IntegralParticularInfo where userId="+id;
String sqlstr[]={sql,sql2};
return dbAccess.execUpdate(sqlstr);
}
// 登录成功的用户,将自己的相关信息获取
public UserInfo selectUser(String name){
return null;
}
// 登录用户修改自己的密码
public boolean updPass(String oldpass, String newpass, int userId){
dbAccess.createConnection();
String sqlstr="update userinfo set pass='"+newpass+"' where id="+userId+" and pass='"+oldpass+"'";
return dbAccess.execUpdate(sqlstr);
}
// 当用户忘记密码时,通过原来设置的密码问题来找回密码
public String selPass(String userName, int passMatter, String passResult){
dbAccess.createConnection();
String sqlstr="select pass from userinfo where name='"
+userName+"' and passMatter="+passMatter+" and passResult='"+passResult+"'";
String pass="";
ArrayList<HashMap> al=(ArrayList<HashMap>)dbAccess.execQuery(sqlstr);
for(HashMap<String,Object> hm:al){
pass=(String)hm.get("PASS");
}
return pass;
}
// 低级会员购买商品时,当购买商品的数量达到了一定数量,则升级为高级会员
// 将原来userLevel的0改为1
public boolean updUserLevel(int userId){
dbAccess.createConnection();
String sqlstr="update userinfo set userLevel=1 where id="+userId;
return dbAccess.execUpdate(sqlstr);
}
// 用户此时与上次登录时间之间的时间差,只精确到小时
public int timeBetween(int userId){
dbAccess.createConnection();
int nl=0;
String sqlstr="select ceil(to_date(to_char(sysdate,'yyymmdd'),'yyymmdd')-to_date(to_char(loginTime,'yyyymmdd'),'yyyymmdd')) as nl from userinfo where id="+userId;
ArrayList<HashMap> al=(ArrayList<HashMap>)dbAccess.execQuery(sqlstr);
for(HashMap<String,Object> hm:al){
nl=(Integer)hm.get("NL");
}
return nl;
}
// 用户上次登录的相关信息
public String loginInformation(int userId){
dbAccess.createConnection();
String sqlstr="select to_char(loginTime,"+Constant.ORACLE_TO_DATE_FORMAT
+")lo,loginIP,nowsLoginIP,to_char(nowsTime,"+Constant.ORACLE_TO_DATE_FORMAT
+")no from userinfo where id="+userId;
String str="";
ArrayList<HashMap> al=(ArrayList<HashMap>)dbAccess.execQuery(sqlstr);
for(HashMap<String,Object> hm:al){
str="你上次登录的时间是:"+(String)hm.get("LO")+
"<br/>上次登录的IP地址是:"+(String)hm.get("LOGINIP")+"<br/>"+
"你本次登录的时间是:"+(String)hm.get("NO")+
"<br/>本次登录的IP地址是:"+(String)hm.get("NOWSLOGINIP")+"<br/>";
}
return str;
}
// 用户今登录成功时,并往数据库中修改登录信息
public boolean updLogin(int userId, String nowsLoginIP){
dbAccess.createConnection();
String sqlstr= "update userinfo set nowsTime=sysdate,nowsLoginIP='"
+nowsLoginIP+"' where id="+userId;
return dbAccess.execUpdate(sqlstr);
}
// 用户退出时,也往数据库中更改登录信息
public boolean upUnbound(int userId){
dbAccess.createConnection();
String sqlstr="update userinfo set loginTime=nowsTime,loginIP=nowsLoginIP where id="+userId;
return dbAccess.execUpdate(sqlstr);
}
// 修改会员登录次数
public boolean updloginDegree(int userId){
dbAccess.createConnection();
String sqlstr="update userinfo set loginDegree=loginDegree+1 where id="+userId;
return dbAccess.execUpdate(sqlstr);
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}