yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.yeshi.fanli.service.impl.push;
 
import java.io.Serializable;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.config.PushRecordDao;
import com.yeshi.fanli.entity.xinge.PushRecord;
import com.yeshi.fanli.service.inter.push.PushRecordService;
import com.yeshi.fanli.util.Constant;
@Service
public class PushRecordServiceImpl implements PushRecordService {
    
    @Resource
    private PushRecordDao dao;
 
    public void save(PushRecord pushRecord) {
        pushRecord.setCreatetime(System.currentTimeMillis());
        dao.save(pushRecord);
    }
 
    public List<PushRecord> getPushRecordList(String title, int type, int page) {
        int start = (page-1) * Constant.PAGE_SIZE;
        StringBuffer hqlBuf=new StringBuffer("from PushRecord pr where 1=1 ");
        
        if(title !=null && !"".equals(title.trim())){
            hqlBuf.append(" and (pr.title like ? )");
        }
        if(type != 0){
            hqlBuf.append(" and (pr.type = ? )");
        }
        hqlBuf.append(" order by pr.id desc ");
        String hql = hqlBuf.toString();
        if(hql.contains("pr.title") && hql.contains("pr.type")){
            return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{"%"+title+"%",type});
        }else if(hql.contains("pr.title") && !hql.contains("pr.type")){
            return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{"%"+title+"%"});
        }else if(!hql.contains("pr.title") && hql.contains("pr.type")){
            return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{type});
        }else{
            return dao.list(hqlBuf.toString(), start, Constant.PAGE_SIZE, new Serializable[]{});
        }
        
    }
 
    public int getCount(String title, int type, int page) {
        
        StringBuffer hqlBuf=new StringBuffer("select count(*) from PushRecord pr where 1=1 ");
        
        if(title !=null && !"".equals(title.trim())){
            hqlBuf.append(" and (pr.title like ? )");
        }
        if(type != 0){
            hqlBuf.append(" and (pr.type = ? )");
        }
        String hql = hqlBuf.toString();
        if(hql.contains("pr.title") && hql.contains("pr.type")){
            return (int) dao.getCount(hqlBuf.toString(), new Serializable[]{"%"+title+"%",type});
        }else if(hql.contains("pr.title") && !hql.contains("pr.type")){
            return (int) dao.getCount(hqlBuf.toString(),new Serializable[]{"%"+title+"%"});
        }else if(!hql.contains("pr.title") && hql.contains("pr.type")){
            return (int) dao.getCount(hqlBuf.toString(),new Serializable[]{type});
        }else{
            return (int) dao.getCount(hqlBuf.toString(),new Serializable[]{});
        }
    }
 
    public void increaseByAndroid(final String pushId) {
        final String sql="update yeshi_ec_push_record pr set pr.count=pr.count+1 where pr.androidPushId = ? ";
        dao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
                SQLQuery query = session.createSQLQuery(sql);
                query.setParameter(0, pushId);
                return query.executeUpdate();
            }
        });
    }
 
    public void increaseByIOS(final String pushId) {
        final String sql="update yeshi_ec_push_record pr set pr.count=pr.count+1 where pr.iosPushId = ? ";
        dao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
                SQLQuery query = session.createSQLQuery(sql);
                query.setParameter(0, pushId);
                return query.executeUpdate();
            }
        });
    }
}