| | |
| | | package com.yeshi.buwan.service.imp.vip; |
| | | |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.alipay.api.response.AlipayTradeQueryResponse; |
| | | 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.PPTVException; |
| | | import com.yeshi.buwan.exception.vip.VIPException; |
| | | import com.yeshi.buwan.pptv.PPTVVipManager; |
| | | import com.yeshi.buwan.service.inter.vip.VIPService; |
| | | import com.yeshi.buwan.util.log.LoggerUtil; |
| | | import com.yeshi.buwan.util.user.VipUtil; |
| | | import com.yeshi.buwan.util.vip.VIPOrderUtil; |
| | | import org.hibernate.Query; |
| | | import org.hibernate.Session; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.yeshi.utils.alipay.AlipayH5PayUtil; |
| | | import org.yeshi.utils.entity.wx.WXPayOrderInfoV3; |
| | | import org.yeshi.utils.wx.WXPayV3Util; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.Serializable; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | |
| | | Logger logger = LoggerFactory.getLogger(VIPServiceImpl.class); |
| | | |
| | | @Resource |
| | | private VIPOrderRecordDao vipOrderRecordDao; |
| | | |
| | | @Resource |
| | | private UserVIPInfoDao userVIPInfoDao; |
| | | |
| | | @Resource |
| | | private PPTVVipManager pptvVipManager; |
| | | |
| | | |
| | | /** |
| | | * 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 VIPOrderRecord paySuccess(final String id, int payWay, BigDecimal payMoney, Date payTime) throws VIPException, PPTVException { |
| | | LoggerUtil.getTestLogger().info("支付成功:开始执行{}", id); |
| | | |
| | | Session session = vipOrderRecordDao.getSession(); |
| | | |
| | | //修改记录 |
| | | Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(VIPOrderRecord.class).setCacheable(false).setParameter(0, id); |
| | | List<VIPOrderRecord> list = query.list(); |
| | | |
| | | if (list != null && list.size() > 0) { |
| | | VIPOrderRecord record = list.get(0); |
| | | if (record.getState() == VIPOrderRecord.STATE_PAY) |
| | | throw new VIPException(1, "订单已经支付"); |
| | | |
| | | session.createSQLQuery("insert into wk_vip_order_pay_success(id,create_time) value(?,now())").setParameter(0, id).executeUpdate(); |
| | | |
| | | record.setPayWay(payWay); |
| | | record.setPayMoney(payMoney); |
| | | record.setPayTime(payTime); |
| | | record.setUpdateTime(new Date()); |
| | | |
| | | record.setState(VIPOrderRecord.STATE_PAY); |
| | | |
| | | |
| | | Date[] expireDate = addExpireTime(session, record.getUid(), payTime, record.getType()); |
| | | if (expireDate == null) { |
| | | throw new VIPException(2, "添加用户会员时间出错"); |
| | | } |
| | | |
| | | record.setVipStartTime(expireDate[0]); |
| | | record.setVipEndTime(expireDate[1]); |
| | | |
| | | session.update(record); |
| | | |
| | | |
| | | LoggerUtil.getTestLogger().info("支付成功:修改记录{}", id); |
| | | //购买VIP |
| | | pptvVipManager.buyVIP(record); |
| | | LoggerUtil.getTestLogger().info("支付成功:购买VIP成功{}", id); |
| | | return record; |
| | | } else { |
| | | throw new VIPException(10, "订单不存在"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 续期 |
| | | * |
| | | * @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.createSQLQuery("select * from wk_user_vip i where i.uid=? for update").addEntity(UserVIPInfo.class).setParameter(0, uid).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() == null ? 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); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public VIPOrderRecord checkOrderPayState(String id) { |
| | | VIPOrderRecord record = vipOrderRecordDao.find(VIPOrderRecord.class, id); |
| | | if (record.getState() == VIPOrderRecord.STATE_PAY) |
| | | return record; |
| | | |
| | | switch (record.getPayWay()) { |
| | | case VIPOrderRecord.PAY_WAY_ALIPAY: { |
| | | //支付宝 |
| | | AlipayTradeQueryResponse res = null; |
| | | try { |
| | | res = AlipayH5PayUtil.queryOrder(VipUtil.getAlipayApp(), VIPOrderUtil.getOutOrderNo(id), null); |
| | | //支付成功 |
| | | if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) { |
| | | try { |
| | | return paySuccess(id, VIPOrderRecord.PAY_WAY_ALIPAY, new BigDecimal(res.getTotalAmount()), new Date()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } catch (AlipayApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | break; |
| | | case VIPOrderRecord.PAY_WAY_WX: { |
| | | //微信 |
| | | try { |
| | | WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(VIPOrderUtil.getOutOrderNo(id), VipUtil.getWXAPP()); |
| | | if (info != null && info.getTrade_state().equalsIgnoreCase("SUCCESS")) { |
| | | paySuccess(id, record.getPayWay(), new BigDecimal(info.getAmount().getPayer_total()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR), new Date()); |
| | | return record; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | break; |
| | | } |
| | | return record; |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public VIPOrderRecord getOrderRecord(String id) { |
| | | return vipOrderRecordDao.find(VIPOrderRecord.class, id); |
| | | } |
| | | |
| | | @Transactional |
| | |
| | | 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; |
| | | } |
| | | } |