admin
2021-04-09 8a66d7d040d10d03c32062ae7bb7e1fad836871f
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
package com.yeshi.buwan.service.imp.vip;
 
import com.yeshi.buwan.dao.vip.UserVIPInfoDao;
import com.yeshi.buwan.domain.vip.UserVIPInfo;
import com.yeshi.buwan.service.inter.vip.VIPService;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class VIPServiceImpl implements VIPService {
 
    Logger logger = LoggerFactory.getLogger(VIPServiceImpl.class);
 
    @Resource
    private UserVIPInfoDao userVIPInfoDao;
 
    @Override
    public UserVIPInfo getVIPInfo(String uid) {
        return userVIPInfoDao.find(UserVIPInfo.class, uid);
    }
 
    @Transactional
    @Override
    public void initUserVipInfo(String loginUid) {
        Session session = userVIPInfoDao.getSession();
        UserVIPInfo userVIPInfo = (UserVIPInfo) session.get(UserVIPInfo.class, loginUid);
        if (userVIPInfo == null) {
            userVIPInfo = new UserVIPInfo();
            userVIPInfo.setUid(loginUid);
            userVIPInfo.setCreateTime(new Date());
            userVIPInfoDao.save(userVIPInfo);
        }
    }
 
    @Override
    public List<UserVIPInfo> listVIPUser(Date minVIPExpireDate, Date maxVIPExpireDate, int page, int pageSize) {
        List<Serializable> list = new ArrayList<>();
        String hql = "from UserVIPInfo vip where 1=1";
        if (minVIPExpireDate != null) {
            hql += " and vip.expireDate>=?";
            list.add(minVIPExpireDate);
        }
 
        if (maxVIPExpireDate != null) {
            hql += " and vip.expireDate<?";
            list.add(maxVIPExpireDate);
        }
        hql += " order by vip.createTime desc";
        Serializable[] params = new Serializable[list.size()];
        list.toArray(params);
        return userVIPInfoDao.list(hql, (page - 1) * pageSize, pageSize, params);
 
    }
 
    @Override
    public long countVIPUser(Date minVIPExpireDate, Date maxVIPExpireDate) {
        List<Serializable> list = new ArrayList<>();
        String hql = "from UserVIPInfo vip where 1=1";
        if (minVIPExpireDate != null) {
            hql += " and vip.expireDate>=?";
            list.add(minVIPExpireDate);
        }
 
        if (maxVIPExpireDate != null) {
            hql += " and vip.expireDate<?";
            list.add(maxVIPExpireDate);
        }
        hql += " order by vip.createTime desc";
        Serializable[] params = new Serializable[list.size()];
        list.toArray(params);
        return userVIPInfoDao.getCount(hql, params);
    }
 
    @Override
    public boolean isVIP(String loginUid) {
        UserVIPInfo userVIPInfo = userVIPInfoDao.find(UserVIPInfo.class, loginUid);
        if (userVIPInfo == null)
            return false;
        if (userVIPInfo.getExpireDate() == null)
            return false;
 
        if (userVIPInfo.getExpireDate().getTime() < System.currentTimeMillis())
            return false;
        return true;
    }
}