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.

363 lines
9.4 KiB

package dao.impl;
import java.util.List;
import java.util.Map;
import model.Classify;
import model.Goods;
import model.User;
//import model.UserAdmin;
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 saveGood(Goods good) {
this.getHibernateTemplate().saveOrUpdate (good);
}
/**
* 查询分类列表
*/
@SuppressWarnings("unchecked")
@Override
public List<Classify> searchClassifyList() {
String hql="from Classify";
return this.getHibernateTemplate().find(hql);
}
/**
* 查询商品
*/
@SuppressWarnings("unchecked")
@Override
public List<Goods> searchLostGoodList(Map<Object, String> map) {
String cid=map.get("cid");
DetachedCriteria dc=DetachedCriteria.forClass(Goods.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)));
}
dc.add(Restrictions.eq("goodsStatus",0));
/*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("proPrice"));
break;
case 3:
dc.addOrder(Order.desc("proPrice"));
break;
}
}*/
List<Goods> list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize")));
return list;
}
/**
* 查询商品数量
*/
@SuppressWarnings("unchecked")
@Override
public int searchLostGoodCount(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 goodsstatus='0'";
/*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){
int con=Integer.parseInt(condition);
order.append(" order by createTime DESC");
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 List<Goods> searchFoundGoodList(Map<Object, String> map) {
String cid=map.get("cid");
DetachedCriteria dc=DetachedCriteria.forClass(Goods.class);
if(cid!=null){
dc.add(Restrictions.eq("classify.cid",Integer.parseInt(cid)));
}
dc.add(Restrictions.eq("goodsStatus",0));
List<Goods> list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize")));
return list;
}
/**
* 查询商品数量
*/
@SuppressWarnings("unchecked")
@Override
public int searchFoundGoodCount(Map<Object, String> map) {
StringBuffer br=new StringBuffer();
StringBuffer order=new StringBuffer();
String cid=map.get("cid");
String count_hql="select count(*) from Product where goodsstatus='1'";
if(cid!=null){
br.append(" and classify.cid='"+Integer.parseInt(cid)+"'");
}
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 Goods getLostGoodDetail(String id) {
int pid=0;
if(id!=null&&id!=""){
pid=Integer.parseInt(id);
}
String hql=" from Product where id=?";
List<Goods> list=this.getHibernateTemplate().find(hql,pid);
if(list.size()>0){
Goods p=list.get(0);
return p;
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public Goods getFoundGoodDetail(String id) {
int pid=0;
if(id!=null&&id!=""){
pid=Integer.parseInt(id);
}
String hql=" from Product where id=?";
List<Goods> list=this.getHibernateTemplate().find(hql,pid);
if(list.size()>0){
Goods 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
*/
@SuppressWarnings("unchecked")
@Override
public int searchMyLostGoodCount(int uid) {
String hql="select count(*) from Product where user.uid = ? and goodsstatus='0'";
List<Long> list=this.getHibernateTemplate().find(hql,uid);
if(list.size()>0){
return list.get(0).intValue();
}
return 0;
}
@SuppressWarnings("unchecked")
@Override
public int searchMyFoundGoodCount(int uid) {
String hql="select count(*) from Product where user.uid = ? and goodsstatus='1'";
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
*/
@SuppressWarnings("unchecked")
@Override
public List<Goods> getMyLostGoodList(Map<Object, String> map) {
DetachedCriteria dc=DetachedCriteria.forClass(model.Goods.class);
dc.add(Restrictions.eq("user.uid",Integer.parseInt(map.get("userId"))));
dc.add(Restrictions.eq("goodsstatus",0));
dc.addOrder(Order.desc("createTime"));
List<model.Goods> list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize")));
return list;
}
@SuppressWarnings("unchecked")
@Override
public List<Goods> getMyFoundGoodList(Map<Object, String> map) {
DetachedCriteria dc=DetachedCriteria.forClass(model.Goods.class);
dc.add(Restrictions.eq("user.uid",Integer.parseInt(map.get("userId"))));
dc.add(Restrictions.eq("goodsstatus",1));
dc.addOrder(Order.desc("createTime"));
List<model.Goods> 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 delectGoodById(int pid) {
Goods good=this.getHibernateTemplate().get(Goods.class, pid);
if(good!=null){
this.getHibernateTemplate().delete(good);
}
}
/**
* <p>Description: 保存用户消息</p>
* @param map
* @return
*/
/*@Override
public void saveUserMessage(UserAdmin uaa) {
this.getHibernateTemplate().save(uaa);
}
@SuppressWarnings("unchecked")
@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;
}
@SuppressWarnings("unchecked")
@Override
public List<UserAdmin> getMessageList(Map<Object, String> map) {
DetachedCriteria dc=DetachedCriteria.forClass(model.UserAdmin.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.UserAdmin> 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();
}
}