You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
7.5 KiB

5 years ago
package dao.impl;
import java.util.List;
import java.util.Map;
import model.Classify;
import model.Product;
import model.User;
import model.UserAndAdmin;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.IUserDao;
public class UserDao extends HibernateDaoSupport implements IUserDao {
/**
*
*/
@Override
public void saveProduct(Product product) {
this.getHibernateTemplate().saveOrUpdate (product);
}
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public List<Classify> searchClassifyList() {
String hql="from Classify";
return this.getHibernateTemplate().find(hql);
}
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public List<Product> searchProductList(Map<Object, String> map) {
String proName=map.get("keyword");//输入框输入
String cid=map.get("cid");//分类
String condition=map.get("conditon");//按人气或价格升、降或供、求查询
//DetachedCriteria 是离线的,创建时无需 SessionDetachedCriteria 提供了 2 个静态方法 forClass(Class) 或 forEntityName(Name) 进行DetachedCriteria 实例的创建。
//Spring 的框架提供了getHibernateTemplate ().findByCriteria(detachedCriteria) 方法可以很方便地根据DetachedCriteria 来返回查询结 果。
DetachedCriteria dc=DetachedCriteria.forClass(Product.class);
//dc.addOrder(Order.desc("createTime"));
//dc.add(Restrictions.ne("proHassum", 0));
if(proName!=null){
proName="%"+map.get("keyword")+"%";
dc.add(Restrictions.like("proName",proName, MatchMode.ANYWHERE));
}
if(cid!=null){
dc.add(Restrictions.eq("classify.cid",Integer.parseInt(cid)));
}
if(condition!=null){
int con=Integer.parseInt(condition);
switch(con){
case 5:
dc.add(Restrictions.eq("type",1));
break;
case 4:
dc.add(Restrictions.eq("type",0));
break;
/*case "1":
dc.addOrder(Order.desc("proClicknum"));
break;*/
case 2:
dc.addOrder(Order.asc("sep"));
break;
case 3:
dc.addOrder(Order.desc("sep"));
break;
}
}
List<Product> list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize")));
return list;
}
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public int searchProductCount(Map<Object, String> map) {
StringBuffer br=new StringBuffer();
StringBuffer order=new StringBuffer();
String proName=map.get("keyword");
String cid=map.get("cid");
String condition=map.get("conditon");
String count_hql="select count(*) from Product where 1=1";
if(proName!=null){
proName="%"+map.get("keyword")+"%";
br.append(" and proName like '"+proName+"'");
}
if(cid!=null){
br.append(" and classify.cid='"+Integer.parseInt(cid)+"'");
}
if(condition!=null){
order.append(" order by createTime DESC");
int con=Integer.parseInt(condition);
switch(con){
/*case 1:
order.append(" ,proClicknum DESC");
break;*/
case 2:
order.append(" ,proPrice ASC");
break;
case 3:
order.append(" ,proPrice DESC");
break;
case 4:
br.append(" and type=0");
break;
case 5:
br.append(" and type=1");
break;
}
}
List<Long> list=this.getHibernateTemplate().find(count_hql+br.toString()+order.toString());
if(list.size()>0){
return list.get(0).intValue();
}
return 0;
}
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public Product getProductDetail(String id) {
int pid=0;
if(id!=null&&id!=""){
pid=Integer.parseInt(id);
}
String hql=" from Product where id=?";
List<Product> list=this.getHibernateTemplate().find(hql,pid);
if(list.size()>0){
Product p=list.get(0);
return p;
}
return null;
}
@Override
public Classify getClassifyById(Integer classifyId) {
String hql="from Classify where cid=?";
@SuppressWarnings("unchecked")
List<Classify> c=this.getHibernateTemplate().find(hql, classifyId);
if(c.size()>0){
Classify cla=c.get(0);
return cla;
}
return null;
}
@Override
public User getUserById(Integer creatorId) {
String hql="from User where uid=?";
@SuppressWarnings("unchecked")
List<User> u=this.getHibernateTemplate().find(hql, creatorId);
if(u.size()>0){
User user=u.get(0);
return user;
}
return null;
}
@Override
public void updateProduct(Product product) {
this.getHibernateTemplate().update(product);
}
/**
* <p>Description: </p>
* @param parseInt
* @return
*/
@Override
public int searchMyProductCount(int uid) {
String hql="select count(*) from Product where user.uid = ?";
List<Long> list=this.getHibernateTemplate().find(hql,uid);
if(list.size()>0){
return list.get(0).intValue();
}
return 0;
}
/**
* <p>Description: </p>
* @param map
* @return
*/
@Override
public List<Product> getMyProductList(Map<Object, String> map) {
DetachedCriteria dc=DetachedCriteria.forClass(model.Product.class);
dc.add(Restrictions.eq("user.uid",Integer.parseInt(map.get("userId"))));
dc.addOrder(Order.desc("createTime"));
List<model.Product> list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize")));
return list;
}
/**
* <p>Description: </p>
* @param pid
*/
@Override
public void delectProductById(int pid) {
Product product=this.getHibernateTemplate().get(Product.class, pid);
if(product!=null){
this.getHibernateTemplate().delete(product);
}
}
/**
* <p>Description: </p>
* @param map
* @return
*/
@Override
public void saveUserMessage(UserAndAdmin uaa) {
this.getHibernateTemplate().save(uaa);
}
@Override
public int searchMessageCount(int uid, String flag) {
StringBuffer sb=new StringBuffer();
String hql="select count(*) from UserAndAdmin where 1=1 ";
if(flag=="0"||"0".equals(flag)){
sb.append(" and status='0' and userId="+uid);
}else{
sb.append(" and status='1' and userId="+uid);
}
List<Long> list=this.getHibernateTemplate().find(hql+sb.toString());
if(list.size()>0){
return list.get(0).intValue();
}
return 0;
}
@Override
public List<UserAndAdmin> getMessageList(Map<Object, String> map) {
DetachedCriteria dc=DetachedCriteria.forClass(model.UserAndAdmin.class);
String flag=map.get("flag");
if(flag=="0"||"0".equals(flag)){
dc.add(Restrictions.eq("userId",Integer.parseInt( map.get("userId"))));
dc.add(Restrictions.eq("status",0));
}else{
dc.add(Restrictions.eq("userId",Integer.parseInt( map.get("userId"))));
dc.add(Restrictions.eq("status",1));
}
List<model.UserAndAdmin> list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize")));
return list;
}
/**
* id
*/
@Override
public void deleteMessage(int id) {
String hql="delete from UserAndAdmin where id="+id;
SessionFactory factory=this.getHibernateTemplate().getSessionFactory();
Session session=factory.openSession();
Query query=session.createQuery(hql);
query.executeUpdate();
session.close();
}
}