package com.wetogether.personalCenter.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.wetogether.common.hibernate.pojos.Album;
import com.wetogether.common.hibernate.pojos.Article;
import com.wetogether.common.hibernate.pojos.BBS;
import com.wetogether.common.hibernate.pojos.Comment;
import com.wetogether.common.hibernate.pojos.CommentOfArticle;
import com.wetogether.common.hibernate.pojos.DynamicMessage;
import com.wetogether.common.hibernate.pojos.Friend;
import com.wetogether.common.hibernate.pojos.OriginalSignature;
import com.wetogether.common.hibernate.pojos.Photo;
import com.wetogether.common.hibernate.pojos.User;
import com.wetogether.common.hibernate.pojos.ZanTable;
import com.wetogether.common.util.CodeCst;
import com.wetogether.personalCenter.dao.IPersonalCenterDao;
import com.wetogether.personalCenter.dto.AlbumBean;
import com.wetogether.personalCenter.dto.ArticleVOs;
import com.wetogether.personalCenter.dto.CommentsBean;
import com.wetogether.personalCenter.dto.OSsBean;
import com.wetogether.personalCenter.dto.PhotoBean;
public class PersonalCenterDaoImpl implements IPersonalCenterDao {
private HibernateTemplate hibernateTemplate;
//添加新好友
@Override
public void insertNewFriend(Friend friend) {
hibernateTemplate.save(friend);
}
//新建相册
@Override
public void insertAlbum(Album album) {
this.hibernateTemplate.save(album);
}
@Override
public void insertNewComment(Comment comment) {
// TODO Auto-generated method stub
}
//插入新说说
@Override
public void insertNewOs(OriginalSignature os) {
this.hibernateTemplate.save(os);
}
//获得当前用户下的所有好友信息
@Override
public List<Friend> findAllFriends(Integer currentUserId) {
String hql = "from Friend f where f.currentUser.userId=:userId";
@SuppressWarnings("unchecked")
List<Friend> friends = hibernateTemplate.findByNamedParam(hql, "userId", currentUserId) ;
return friends;
}
@Override
public CommentsBean findAllComments(Integer currentUserId) {
// TODO Auto-generated method stub
return null;
}
//通过姓名关键字搜索当前用户的好友(用于AJAX)
@Override
public List<Friend> findMyFriendByName(String name,Integer currendUserId) {
String hql = "from Friend f where f.currentUser.userId=:userId and f.friendUser.userName like :name";
String [] para = {"userId","name"};
Object[] val = {currendUserId,name+"%"};
@SuppressWarnings("unchecked")
List<Friend> friends = hibernateTemplate.findByNamedParam(hql, para, val);
return friends;
}
//查询当前用户好友的所有说说(order by time)
@Override
public OSsBean findAllOssOfMyFriends(User currentUser,OSsBean oSsBean) {
// List<Friend> friends = this.findAllFriends(currentUser.getUserId());
String hql = "from OriginalSignature o where o.osOwer.userId in "+
"(select f.friendUser.userId from Friend f where f.currentUser.userId =:currentUserId)"+
"order by o.osSubmitDate desc";
Session session = hibernateTemplate.getSessionFactory().openSession();
Query q = session.createQuery(hql);
q.setParameter("currentUserId", currentUser.getUserId());
//获得记录总数
List<OriginalSignature> oss = q.list();
//计算总共多少页
oSsBean.getPage().setTotalPages((oss.size()/oSsBean.getPage().getPageSize())+1);
q.setFirstResult((oSsBean.getPage().getCurrentPage()-1)<<3);
q.setMaxResults(oSsBean.getPage().getPageSize());
oss = q.list();
oSsBean.setOss(oss);
session.close();
return oSsBean;
}
//查询当前用户自身的所有说说(order by time)
@SuppressWarnings("unchecked")
@Override
public OSsBean findAllOssOfMine(User currentUser,OSsBean oSsBean) {
String hql = "from OriginalSignature o where o.osOwer.userId =:id order by o.osSubmitDate desc ";
Session session = hibernateTemplate.getSessionFactory().openSession();
Query q = session.createQuery(hql);
q.setParameter("id", currentUser.getUserId());
//获得记录总数
List<OriginalSignature> oss = q.list();
//计算总共多少页
oSsBean.getPage().setTotalPages((oss.size()/oSsBean.getPage().getPageSize())+1);
q.setFirstResult((oSsBean.getPage().getCurrentPage()-1)<<3);
q.setMaxResults(oSsBean.getPage().getPageSize());
oss = q.list();
oSsBean.setOss(oss);
session.close();
return oSsBean;
}
//上传图片
@Override
public void insertPhoto(Photo p) {
this.hibernateTemplate.save(p);
}
//查询出url未赋值的photo
@SuppressWarnings("unchecked")
@Override
public List<Photo> findDefaltUrlPhoto(Integer id) {
String hql = "from Photo p where p.albumId.albumId=:id and p.photoSrc=:src";
String [] paras = {"id","src"};
Object [] values = {id,CodeCst.PHOTO_DEFAULT_URL};
return this.hibernateTemplate.findByNamedParam(hql, paras, values);
}
//update photo
@Override
public void updatePhoto(Photo p) {
Photo photo = this.hibernateTemplate.get(Photo.class, p.getPhotoId());
photo.setAlbumId(p.getAlbumId());
photo.setPhotoDesc(p.getPhotoDesc());
photo.setPhotoName(p.getPhotoName());
photo.setPhotoSrc(p.getPhotoSrc());
this.hibernateTemplate.update(photo);
}
//查询当前页的所有相册
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<Photo> findAllPhoto(PhotoBean bean) {
String hql = "from Photo p where p.albumId.albumId=:abId order by p.photoId desc";
Session session = hibernateTemplate.getSessionFactory().openSession();
Query q = session.createQuery(hql.toString());
q.setParameter("abId", bean.getAlbum().getAlbumId());
// 获得记录总数
Integer allrows = q.list().size();
bean.getPage().setAllRows(allrows);
// 计算总共多少页
int pageSize = bean.getPage().getPageSize();
if (allrows != 0 && allrows % pageSize == 0) {
bean.getPage().setTotalPages(allrows / pageSize);
} else {
bean.getPage().setTotalPages((allrows / pageSize) + 1);
}
q.setFirstResult((bean.getPage().getCurrentPage()-1)*(bean.getPage().getPageSize()));
q.setMaxResults(bean.getPage().getPageSize());
List result = q.list();
session.close();
return result;
}
//获得当前用户的所有图片
@Override
public List<Photo> myAllPhotos(Integer userId) {
String hql = "from Photo p where p.albumId.albumId in (select a.albumId from Album a where a.albumOwer.userId=:id) order by p.photoId desc";
@SuppressWarnings("unchecked")
List<Photo> photos = this.hibernateTemplate.findByNamedParam(hql, "id", userId);
return photos;
}
//查询当前页的相册
@Override
public List<Album> findAllAlbum(AlbumBean bean) {
String hql = "from Album a where a.albumOwer.userId=:id order by a.createTime desc";
Session session = hibernateTemplate.getSessionFactory().openSession();
Query q = session.createQuery(hql.toString());
q.setParameter("id", bean.getUser().getUserId());
//获得记录总数
Integer allrows = q.list().size();
bean.getPage().setAllRows(allrows);
//计算总共多少页
int pageSize = bean.getPage().getPageSize();
if(allrows!=0&&allrows%pageSize==0){
bean.getPage().setTotalPages(allrows/pageSize);
}else{
bean.getPage().setTotalPages((allrows/pageSize)+1);
}
q.setFirstResult((bean.getPage().getCurrentPage()-1)*(bean.getPage().getPageSize()));
q.setMaxResults(bean.getPage().getPageSize());
List result = q.list();
session.close();
return result;
}
//通过userId 查找User
@Override
public User findUserById(Integer userId) {
String hql = "from User u where u.userId = :userId";
@SuppressWarnings("unchecked")
List<User>users = hibernateTemplate.findByNamedParam(hql, "userId", userId);
if(users.size()>0){
return users.get(0);
}
return null;
}
// 通过好友ID查询好友
@Override
public Friend findFriendByFriendUserId(Integer id,Integer myId) {
String hql = "from Friend f where f.currentUse