admin
2021-12-09 f609ca35ee2946acd0ff04b7ac1aa61f75a2e4a1
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.ks.app.service.impl.vip;
 
import com.ks.app.dao.vip.UserVIPInfoDao;
import com.ks.app.entity.vip.UserVIPInfo;
import com.ks.app.service.inter.vip.VIPService;
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.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * @author hxh
 * @title: VIPServiceImpl
 * @description: TODO
 * @date 2021/11/17 17:13
 */
@Service
public class VIPServiceImpl implements VIPService {
 
    Logger logger = LoggerFactory.getLogger(VIPServiceImpl.class);
 
    @Resource
    private UserVIPInfoDao userVIPInfoDao;
 
    @Override
    public UserVIPInfo getVIPInfo(Long uid) {
        return userVIPInfoDao.selectByPrimaryKey(uid);
    }
 
    @Override
    public void initUserVipInfo(Long uid) {
        UserVIPInfo userVIPInfo = userVIPInfoDao.selectByPrimaryKey(uid);
        if (userVIPInfo == null) {
            userVIPInfo = new UserVIPInfo();
            userVIPInfo.setUid(uid);
            userVIPInfo.setCreateTime(new Date());
            userVIPInfoDao.insertSelective(userVIPInfo);
        }
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addExpireTime(Long uid, long time) {
        UserVIPInfo userVIPInfo = userVIPInfoDao.selectByPrimaryKeyForUpdate(uid);
        if (userVIPInfo == null) {
            //新增
            UserVIPInfo vip = new UserVIPInfo();
            vip.setUid(uid);
            vip.setCreateTime(new Date());
            vip.setExpireDate(new Date(System.currentTimeMillis() + time));
            userVIPInfoDao.insertSelective(vip);
        } else {
            //修改
            UserVIPInfo update = new UserVIPInfo();
            update.setUid(uid);
            if (userVIPInfo.getExpireDate() != null) {
 
                //预计到期时间比现在的时间大
                if (userVIPInfo.getExpireDate().getTime() > System.currentTimeMillis()) {
                    update.setExpireDate(new Date(userVIPInfo.getExpireDate().getTime() + time));
                } else {
                    update.setExpireDate(new Date(System.currentTimeMillis() + time));
                }
            } else {
                update.setExpireDate(new Date(System.currentTimeMillis() + time));
            }
            update.setUpdateTime(new Date());
            userVIPInfoDao.updateByPrimaryKeySelective(update);
        }
    }
 
    @Override
    public List<UserVIPInfo> listVIPUser(Date minVIPExpireDate, Date maxVIPExpireDate, int page, int pageSize) {
        UserVIPInfoDao.DaoQuery daoQuery = new UserVIPInfoDao.DaoQuery();
        daoQuery.maxExpireDate = maxVIPExpireDate;
        daoQuery.minExpireDate = minVIPExpireDate;
        daoQuery.start = (page - 1) * pageSize;
        daoQuery.count = pageSize;
        daoQuery.sortList = Arrays.asList(new String[]{"create_time desc"});
        return userVIPInfoDao.list(daoQuery);
    }
 
    @Override
    public long countVIPUser(Date minVIPExpireDate, Date maxVIPExpireDate) {
        UserVIPInfoDao.DaoQuery daoQuery = new UserVIPInfoDao.DaoQuery();
        daoQuery.maxExpireDate = maxVIPExpireDate;
        daoQuery.minExpireDate = minVIPExpireDate;
        return userVIPInfoDao.count(daoQuery);
    }
 
    @Override
    public boolean isVIP(Long uid) {
        UserVIPInfo userVIPInfo = userVIPInfoDao.selectByPrimaryKey(uid);
        if (userVIPInfo == null) {
            return false;
        }
        if (userVIPInfo.getExpireDate() == null) {
            return false;
        }
        if (userVIPInfo.getExpireDate().getTime() < System.currentTimeMillis()) {
            return false;
        }
        return true;
    }
 
 
}