admin
2021-02-04 e8e342cd6c1334f1b8f71d24baa3157637a9ac43
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.yeshi.buwan.service.imp.vip;
 
import com.yeshi.buwan.dao.vip.UserVIPInfoDao;
import com.yeshi.buwan.dao.vip.VIPOrderRecordDao;
import com.yeshi.buwan.domain.vip.UserVIPInfo;
import com.yeshi.buwan.domain.vip.VIPOrderRecord;
import com.yeshi.buwan.domain.vip.VIPPriceType;
import com.yeshi.buwan.exception.vip.VIPException;
import com.yeshi.buwan.service.inter.vip.VIPService;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
@Service
public class VIPServiceImpl implements VIPService {
 
    @Resource
    private VIPOrderRecordDao vipOrderRecordDao;
 
    @Resource
    private UserVIPInfoDao userVIPInfoDao;
 
 
    /**
     * VIP购买记录
     *
     * @param record
     * @throws VIPException
     */
    @Override
    public void addVIPRecord(VIPOrderRecord record) throws VIPException {
        if (record.getMoney() == null || record.getType() == null || record.getUid() == null) {
            throw new VIPException(1, "参数不完整");
        }
 
        if (record.getState() == null) {
            record.setState(VIPOrderRecord.STATE_NOT_PAY);
        }
 
        if (record.getCreateTime() == null) {
            record.setCreateTime(new Date());
        }
        Serializable id = vipOrderRecordDao.save(record);
        record.setId(id.toString());
    }
 
    /**
     * 支付成功
     *
     * @param id
     * @param payWay
     * @param payMoney
     * @param payTime
     * @throws VIPException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void paySuccess(final String id, int payWay, BigDecimal payMoney, Date payTime) throws VIPException {
        VIPException exception = (VIPException) vipOrderRecordDao.excute(new HibernateCallback() {
            @Override
            public VIPException doInHibernate(Session session) throws HibernateException {
                //修改记录
                Query query = session.createQuery("from VIPOrderRecord as r where r.id=? ").setParameter(0, id);
                query.setLockMode("r", LockMode.UPGRADE);
                List<VIPOrderRecord> list = query.list();
 
                if (list != null && list.size() > 0) {
                    if (list.get(0).getState() == VIPOrderRecord.STATE_PAY)
                        return new VIPException(1, "订单已经支付");
 
                    Date[] expireDate = addExpireTime(session, list.get(0).getUid(), payTime, list.get(0).getType());
                    if (expireDate == null) {
                        return new VIPException(2, "添加用户会员时间出错");
                    }
                    session.createQuery("update VIPOrderRecord r set r.payWay=?,r.payMoney=?,r.payTime=?,r.updateTime=?,r.vipStartTime=?,r.vipEndTime=?,r.state=? where r.id=? ")
                            .setParameter(0, payWay)
                            .setParameter(1, payMoney)
                            .setParameter(2, payTime)
                            .setParameter(3, new Date())
                            .setParameter(4, expireDate[0])
                            .setParameter(5, expireDate[1])
                            .setParameter(6, VIPOrderRecord.STATE_PAY)
                            .setParameter(7, id).executeUpdate();
                } else {
                    return new VIPException(10, "订单不存在");
                }
                return null;
            }
        });
 
        if (exception != null)
            throw exception;
    }
 
    /**
     * 续期
     *
     * @param session
     * @param uid
     * @param payTime
     * @param type
     * @return
     */
    private Date[] addExpireTime(Session session, String uid, Date payTime, VIPPriceType type) {
        if (type == null || uid == null)
            return null;
        List list = session.createQuery("from UserVIPInfo i where i.uid=?").setParameter(0, uid).setLockMode("i", LockMode.UPGRADE).list();
        if (list == null || list.size() == 0) {
            //新增
            UserVIPInfo vip = new UserVIPInfo();
            vip.setUid(uid);
            vip.setCreateTime(new Date());
            vip.setExpireDate(getExpireTime(payTime, null, type));
            session.save(vip);
            return new Date[]{payTime, vip.getExpireDate()};
        } else {
            //修改
            UserVIPInfo vipInfo = (UserVIPInfo) list.get(0);
            Date expireDate = getExpireTime(payTime, vipInfo.getExpireDate(), type);
            session.createQuery("update UserVIPInfo i set i.expireDate=? ,i.updateTime=? where i.uid=?").setParameter(0, expireDate).setParameter(1, new Date()).setParameter(2, uid).executeUpdate();
 
            return new Date[]{vipInfo.getExpireDate(), expireDate};
        }
    }
 
    /**
     * 获取到期时间
     *
     * @param payTime
     * @param expireTime
     * @param type
     * @return
     */
    private Date getExpireTime(Date payTime, Date expireTime, VIPPriceType type) {
        Calendar calendar = Calendar.getInstance();
        if (expireTime != null) {
            calendar.setTimeInMillis(expireTime.getTime());
        } else {
            calendar.setTimeInMillis(payTime.getTime());
        }
        if (type == VIPPriceType.day) {
            calendar.add(Calendar.DAY_OF_WEEK, 1);
        } else if (type == VIPPriceType.week) {
            calendar.add(Calendar.WEEK_OF_YEAR, 1);
        } else if (type == VIPPriceType.month) {
            calendar.add(Calendar.MONTH, 1);
        } else if (type == VIPPriceType.season) {
            calendar.add(Calendar.MONTH, 3);
        } else if (type == VIPPriceType.halfYear) {
            calendar.add(Calendar.MONTH, 6);
        } else if (type == VIPPriceType.year) {
            calendar.add(Calendar.YEAR, 1);
        }
        return new Date(calendar.getTimeInMillis());
    }
 
    @Override
    public UserVIPInfo getVIPInfo(String uid) {
        return userVIPInfoDao.find(UserVIPInfo.class, uid);
    }
 
    @Override
    public List<VIPOrderRecord> listOrderRecord(String uid, Integer state, int page, int pageSize) {
 
        VIPOrderRecordDao.DaoQuery query = new VIPOrderRecordDao.DaoQuery();
        query.start = (page - 1) * pageSize;
        query.count = pageSize;
        query.uid = uid;
        query.state = state;
 
        return vipOrderRecordDao.list(query);
    }
 
    @Override
    public long countOrderRecord(String uid, Integer state) {
        VIPOrderRecordDao.DaoQuery query = new VIPOrderRecordDao.DaoQuery();
        query.uid = uid;
        query.state = state;
        return vipOrderRecordDao.count(query);
    }
}