| | |
| | | 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.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.math.BigDecimal; |
| | | import java.util.Calendar; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class VIPServiceImpl implements VIPService { |
| | | |
| | | @Resource |
| | | private VIPOrderRecordDao vipOrderRecordDao; |
| | | Logger logger = LoggerFactory.getLogger(VIPServiceImpl.class); |
| | | |
| | | @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); |
| | | } |
| | | |
| | | @Transactional |
| | | @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); |
| | | 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 long countOrderRecord(String uid, Integer state) { |
| | | VIPOrderRecordDao.DaoQuery query = new VIPOrderRecordDao.DaoQuery(); |
| | | query.uid = uid; |
| | | query.state = state; |
| | | return vipOrderRecordDao.count(query); |
| | | 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; |
| | | } |
| | | } |