package com.briup.run.service.impl;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.briup.run.common.exception.MemberServiceException;
import com.briup.run.common.transaction.HibernateTransaction;
import com.briup.run.common.util.BeanFactory;
import com.briup.run.common.util.MD5;
import com.briup.run.common.util.RandomChar;
import com.briup.run.dao.IMemberDao;
import com.briup.run.dao.bean.GradeRecord;
import com.briup.run.dao.bean.MemberInfo;
import com.briup.run.dao.bean.PointAction;
import com.briup.run.dao.bean.PointRecord;
import com.briup.run.dao.bean.Province;
import com.briup.run.service.IMemberService;
public class MemberServiceImpl implements IMemberService {
public MemberInfo findMemberinfoBynickname(String nickname)
throws MemberServiceException {
// 在查询的时候加上事务可以提高查询的效率,这都是hibernate自动实现的:
MemberInfo member = null;
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
try {
member = memberDao.findMemberByNickName(nickname);
if (member == null) {
throw new MemberServiceException("没有找到该用户");
} else {
return member;
}
} catch (Exception e) {
// e.printStackTrace();
throw new MemberServiceException("查找用户出错");
}
}
@SuppressWarnings("unchecked")
public List findMembersBynum(int num) throws MemberServiceException {
//查找积分榜前10名:
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
List list = null;
try {
list = memberDao.findMembersByNum(num);
} catch (Exception e) {
throw new MemberServiceException("查找积分榜失败");
}
return list;
}
public String findNewPassword(String loginname, String question,
String answer) throws MemberServiceException {
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
try {
//得到用户名:
MemberInfo member = memberDao.findMemberByNickName(loginname);
if (member == null) {
throw new MemberServiceException("你提供的用户名有误");
} else {
//得到用户密码提问:
String passwdquestion = member.getPasswdQuestion();
//得到用户提供的密码答案:
String passwdanswer = member.getPasswdAnswer();
String newpasswd= null;
if(passwdquestion.equals(question)&& passwdanswer.equals(answer))
{
HibernateTransaction tran = new HibernateTransaction();
try{
//为该用户提供新的密码:
newpasswd = RandomChar.getChars(4,8);
MD5 md5 = null;
md5 = MD5.getInstance();
String md5passwd = md5.getMD5ofStr(newpasswd);
member.setPassword(md5.getMD5ofStr(newpasswd));
//将此更新update到数据库:
tran.beginTransaction();
memberDao.saveOrUpdateMemberInfo(member);
tran.commit();
}catch(Exception e){
tran.rollback();
throw new MemberServiceException("找回密码失败");
}
}else{
throw new MemberServiceException("你提供的问题和答案有误!");
}
return newpasswd;
}
} catch (Exception e) {
// e.printStackTrace();
throw new MemberServiceException(e.getMessage());
}
//找回密码需要的步骤如下:
//1.判断输入的用户是否存在:
//2.如果该用户存在,要判断密码提示问题与答案同时正确:
//3.产生一个新的密码()
//4.将新的密码加密后保存到数据库中
//5.把新的密码返回给用会:
}
public Integer findNumMemberInfo() throws MemberServiceException {
return null;
}
public Integer findOnlineMemberinfo() throws MemberServiceException {
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
Integer num;
try {
num = memberDao.findOnlineMemberInfo();
//System.out.println("num"+num);
return num;
} catch (Exception e) {
e.printStackTrace();
throw new MemberServiceException("查找在线人数失败了");
}
}
@SuppressWarnings("unchecked")
public List findProvince() throws MemberServiceException {
List province = null;
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
try {
province = memberDao.findProvince();
if (province == null) {
throw new MemberServiceException("没有找到省份");
} else {
return province;
}
} catch (Exception e) {
e.printStackTrace();
throw new MemberServiceException("没有找到省份");
}
}
public Province findProvinceByCode(String code)
throws MemberServiceException {
Province province = null;
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
try {
province = memberDao.findProvinceByCode(code);
if (province == null) {
throw new MemberServiceException("没有找到省份");
} else {
return province;
}
} catch (Exception e) {
e.printStackTrace();
throw new MemberServiceException("查找省份失败");
}
}
//得到总数量:
public Integer getTotalNum(String age, String gender, String provincecity)
throws MemberServiceException {
// TODO Auto-generated method stub
return null;
}
public boolean issameDate(Date date){
Calendar calendar= Calendar.getInstance();
int today = calendar.get(Calendar.DAY_OF_YEAR);
calendar.setTime(date);
int lastDate = calendar.get(Calendar.DAY_OF_YEAR);
return today == lastDate;
}
//登录
public MemberInfo login(String loginname, String password)
throws MemberServiceException {
IMemberDao memberDao = (IMemberDao) BeanFactory.getBean("memberDao");
PointAction action = null;
GradeRecord gradeRecord = null;
PointRecord record = new PointRecord();
MemberInfo memberinfo = null;
try {
// 首先根据用户名查找该用户是否存在
memberinfo = memberDao.findMemberByNickName(loginname);
System.out.println("memberinfo="+memberinfo);
if (memberinfo != null) {
// 判断该用户是否被注销
if (memberinfo.getStatus() != 1) {
// 判断该用户是否在线
if (memberinfo.getIsOnline() != 1) {
// 判断输入的密码是否正确(MD5加密以后比较)
MD5 md5 = null;
md5 = MD5.getInstance();
String md5passwd = md5.getMD5ofStr(password);
if (md5passwd.equals(memberinfo.getPassword())) {
// 通过"LOGIN"来查找Pointaction,给该用户加上积分前提是两次登录的时间不在同一天
if(true){
HibernateTransaction tran = new HibernateTransaction();
System.out.println("into check .....");
try {
tran.beginTransaction();
// 给该会员加分,可以登录
Long addpoint = memberinfo.getPoint()
+ memberDao.findPointActionByName(
"LOGIN").getPoint();
memberinfo.setPoint(addpoint);
// 将加分的过程记录下来
// 保存获取积分的过程:
record.setNickName(memberinfo.getNickName());
record.setPointAction(action);
record.setReceiveDate(new Date());
memberDao.savePointRecord(record);
// 设置对应的级别:
gradeRecord = memberDao
.findGradeRecordByPoint(memberinfo
.getPoint());
memberinfo.setGradeRecord(gradeRecord);
// 设置isonline属性为1.表示在线
memberinfo.setIsOnline(1L);
// 设置上次登录时间为现在时间,以便下次登录的时候看到的上次登录时间是现在时间.
memberinfo.setLatestDate(new Date());
// 将该对象信息update到数据库
memberDao.saveOrUpdateMemberInfo(memberinfo);
// 注意要使用事务
tran.commit();
} catch (Exception e) {
e