| | |
| | | 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 org.hibernate.HibernateException; |
| | | import org.hibernate.LockMode; |
| | | 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.springframework.orm.hibernate4.HibernateCallback; |
| | | 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; |
| | |
| | | @Service |
| | | public class VIPServiceImpl implements VIPService { |
| | | |
| | | Logger logger = LoggerFactory.getLogger(VIPServiceImpl.class); |
| | | |
| | | @Resource |
| | | private VIPOrderRecordDao vipOrderRecordDao; |
| | | |
| | | @Resource |
| | | private UserVIPInfoDao userVIPInfoDao; |
| | | |
| | | @Resource |
| | | private PPTVVipManager pptvVipManager; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | @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(); |
| | | public VIPOrderRecord paySuccess(final String id, int payWay, BigDecimal payMoney, Date payTime) throws VIPException, PPTVException { |
| | | LoggerUtil.getTestLogger().info("支付成功:开始执行{}", id); |
| | | |
| | | if (list != null && list.size() > 0) { |
| | | if (list.get(0).getState() == VIPOrderRecord.STATE_PAY) |
| | | return new VIPException(1, "订单已经支付"); |
| | | Session session = vipOrderRecordDao.getSession(); |
| | | |
| | | 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; |
| | | //修改记录 |
| | | 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, "添加用户会员时间出错"); |
| | | } |
| | | }); |
| | | |
| | | if (exception != null) |
| | | throw exception; |
| | | 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, "订单不存在"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | 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(); |
| | | 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(); |
| | |
| | | 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}; |
| | | return new Date[]{vipInfo.getExpireDate() == null ? new Date() : vipInfo.getExpireDate(), expireDate}; |
| | | } |
| | | } |
| | | |
| | |
| | | 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 |
| | | @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); |
| | | } |
| | | } |