|  |  |  | 
|---|
|  |  |  | package com.dsh.account.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alipay.api.response.AlipayTradeQueryResponse; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.dsh.account.entity.TAppUser; | 
|---|
|  |  |  | import com.dsh.account.entity.VipPayment; | 
|---|
|  |  |  | import com.dsh.account.feignclient.activity.UserConponClient; | 
|---|
|  |  |  | import com.dsh.account.mapper.VipPaymentMapper; | 
|---|
|  |  |  | import com.dsh.account.model.IncomeQuery; | 
|---|
|  |  |  | import com.dsh.account.service.IVipPaymentService; | 
|---|
|  |  |  | import com.dsh.account.service.TAppUserService; | 
|---|
|  |  |  | import com.dsh.account.util.PayMoneyUtil; | 
|---|
|  |  |  | import com.dsh.account.util.RedisUtil; | 
|---|
|  |  |  | import com.dsh.account.util.ResultUtil; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.StringUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.Calendar; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PayMoneyUtil payMoneyUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private UserConponClient userConponClient; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisUtil redisUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 购买年度会员 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param uid | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | 
|---|
|  |  |  | public ResultUtil addVipPayment(Integer uid, Integer payType) throws Exception { | 
|---|
|  |  |  | VipPayment vipPayment = new VipPayment(); | 
|---|
|  |  |  | vipPayment.setCode(VipPayment.CODE()); | 
|---|
|  |  |  | vipPayment.setAmount(199D); | 
|---|
|  |  |  | vipPayment.setAmount(0.01D); | 
|---|
|  |  |  | vipPayment.setAppUserId(uid); | 
|---|
|  |  |  | vipPayment.setInsertTime(new Date()); | 
|---|
|  |  |  | vipPayment.setPayStatus(1); | 
|---|
|  |  |  | vipPayment.setState(1); | 
|---|
|  |  |  | this.baseMapper.insert(vipPayment); | 
|---|
|  |  |  | if(payType == 1){//微信 | 
|---|
|  |  |  | if (payType == 1) {//微信 | 
|---|
|  |  |  | return weixinpay(vipPayment); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(payType == 2){//支付宝 | 
|---|
|  |  |  | if (payType == 2) {//支付宝 | 
|---|
|  |  |  | return alipay(vipPayment); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ResultUtil.success(); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 微信 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param vipPayment | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public ResultUtil weixinpay(VipPayment vipPayment) throws Exception{ | 
|---|
|  |  |  | public ResultUtil weixinpay(VipPayment vipPayment) throws Exception { | 
|---|
|  |  |  | String code = vipPayment.getCode(); | 
|---|
|  |  |  | ResultUtil weixinpay = payMoneyUtil.weixinpay("购买年度会员", "", code, vipPayment.getAmount().toString(), | 
|---|
|  |  |  | "/base/appUser/addVipPaymentWeChatCallback", "APP", ""); | 
|---|
|  |  |  | if(weixinpay.getCode() == 200){ | 
|---|
|  |  |  | if (weixinpay.getCode() == 200) { | 
|---|
|  |  |  | new Thread(new Runnable() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | int num = 1; | 
|---|
|  |  |  | int wait = 0; | 
|---|
|  |  |  | while (num <= 10){ | 
|---|
|  |  |  | while (num <= 10) { | 
|---|
|  |  |  | int min = 5000; | 
|---|
|  |  |  | wait += (min * num); | 
|---|
|  |  |  | VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code)); | 
|---|
|  |  |  | if(vipPayment1.getPayStatus() != 1){ | 
|---|
|  |  |  | if (vipPayment1.getPayStatus() == 2) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * SUCCESS--支付成功 | 
|---|
|  |  |  | * REFUND--转入退款 | 
|---|
|  |  |  | 
|---|
|  |  |  | * ACCEPT--已接收,等待扣款 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(code, ""); | 
|---|
|  |  |  | if(resultUtil.getCode() == 200){ | 
|---|
|  |  |  | if (resultUtil.getCode() == 200) { | 
|---|
|  |  |  | Map<String, String> map = resultUtil.getData(); | 
|---|
|  |  |  | String trade_type = map.get("trade_type"); | 
|---|
|  |  |  | String trade_state = map.get("trade_state"); | 
|---|
|  |  |  | String transaction_id = map.get("transaction_id"); | 
|---|
|  |  |  | if("REFUND".equals(trade_state) || "NOTPAY".equals(trade_state) || "CLOSED".equals(trade_state) || "REVOKED".equals(trade_state) || "PAYERROR".equals(trade_state)){ | 
|---|
|  |  |  | if ("REFUND".equals(trade_state) || "NOTPAY".equals(trade_state) || "CLOSED".equals(trade_state) || "REVOKED".equals(trade_state) || "PAYERROR".equals(trade_state)) { | 
|---|
|  |  |  | vipPayment1.setState(3); | 
|---|
|  |  |  | VipPaymentServiceImpl.this.updateById(vipPayment1); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if("SUCCESS".equals(trade_state)){ | 
|---|
|  |  |  | if ("SUCCESS".equals(trade_state)) { | 
|---|
|  |  |  | vipPayment1.setPayStatus(2); | 
|---|
|  |  |  | vipPayment1.setPayTime(new Date()); | 
|---|
|  |  |  | vipPayment1.setOrderNumber(transaction_id); | 
|---|
|  |  |  | VipPaymentServiceImpl.this.updateById(vipPayment1); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId()); | 
|---|
|  |  |  | Date vipEndTime = appUser.getVipEndTime(); | 
|---|
|  |  |  | if(null == vipEndTime){ | 
|---|
|  |  |  | if (null == vipEndTime) { | 
|---|
|  |  |  | vipEndTime = new Date(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(vipEndTime); | 
|---|
|  |  |  | calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1); | 
|---|
|  |  |  | appUser.setIsVip(1); | 
|---|
|  |  |  | appUser.setVipEndTime(calendar.getTime()); | 
|---|
|  |  |  | appUserService.updateById(appUser); | 
|---|
|  |  |  | // 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限 | 
|---|
|  |  |  | List<Long> longs = userConponClient.queryCouponByUser(appUser.getId()); | 
|---|
|  |  |  | redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if("USERPAYING".equals(trade_state) || "ACCEPT".equals(trade_state)){ | 
|---|
|  |  |  | if ("USERPAYING".equals(trade_state) || "ACCEPT".equals(trade_state)) { | 
|---|
|  |  |  | Thread.sleep(wait); | 
|---|
|  |  |  | num++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Thread.sleep(wait); | 
|---|
|  |  |  | num++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(10 == num){ | 
|---|
|  |  |  | if (10 == num) { | 
|---|
|  |  |  | vipPayment1.setState(3); | 
|---|
|  |  |  | VipPaymentServiceImpl.this.updateById(vipPayment1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 支付宝 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param vipPayment | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public ResultUtil alipay(VipPayment vipPayment) throws Exception{ | 
|---|
|  |  |  | private String smid = "2088330203191220";//平台支付宝商户号 | 
|---|
|  |  |  | public ResultUtil alipay(VipPayment vipPayment) throws Exception { | 
|---|
|  |  |  | String code = vipPayment.getCode(); | 
|---|
|  |  |  | ResultUtil alipay = payMoneyUtil.alipay("购买年度会员", "", "", code, vipPayment.getAmount().toString(), | 
|---|
|  |  |  | String amount = vipPayment.getAmount().toString(); | 
|---|
|  |  |  | ResultUtil alipay = payMoneyUtil.alipay | 
|---|
|  |  |  | (smid,"购买年度会员", "购买年度会员", | 
|---|
|  |  |  | vipPayment.getAppUserId() + "", | 
|---|
|  |  |  | code, vipPayment.getAmount().toString(), | 
|---|
|  |  |  | "/base/appUser/addVipPaymentAliCallback"); | 
|---|
|  |  |  | if(alipay.getCode() == 200){ | 
|---|
|  |  |  | if (alipay.getCode() == 200) { | 
|---|
|  |  |  | new Thread(new Runnable() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | int num = 1; | 
|---|
|  |  |  | int wait = 0; | 
|---|
|  |  |  | while (num <= 10){ | 
|---|
|  |  |  | while (num <= 10) { | 
|---|
|  |  |  | int min = 5000; | 
|---|
|  |  |  | wait += (min * num); | 
|---|
|  |  |  | VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code)); | 
|---|
|  |  |  | if(vipPayment1.getPayStatus() != 1){ | 
|---|
|  |  |  | if (vipPayment1.getPayStatus() != 1) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * TRADE_SUCCESS(交易支付成功)、 | 
|---|
|  |  |  | * TRADE_FINISHED(交易结束,不可退款) | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryALIOrder(code); | 
|---|
|  |  |  | if(resultUtil.getCode() == 200){ | 
|---|
|  |  |  | Map<String, String> map = resultUtil.getData(); | 
|---|
|  |  |  | String tradeStatus = map.get("tradeStatus"); | 
|---|
|  |  |  | String tradeNo = map.get("tradeNo"); | 
|---|
|  |  |  | if("TRADE_CLOSED".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)){ | 
|---|
|  |  |  | AlipayTradeQueryResponse resultUtil = payMoneyUtil.queryALIOrder(code); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (resultUtil.getCode().equals("10000")) { | 
|---|
|  |  |  | String tradeNo = resultUtil.getTradeNo(); | 
|---|
|  |  |  | String tradeStatus = resultUtil.getTradeStatus(); | 
|---|
|  |  |  | System.out.println("ssssss" + tradeStatus); | 
|---|
|  |  |  | if ("TRADE_CLOSED".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)) { | 
|---|
|  |  |  | vipPayment1.setState(3); | 
|---|
|  |  |  | VipPaymentServiceImpl.this.updateById(vipPayment1); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if("TRADE_SUCCESS".equals(tradeStatus)){ | 
|---|
|  |  |  | if ("TRADE_SUCCESS".equals(tradeStatus)) { | 
|---|
|  |  |  | vipPayment1.setPayStatus(2); | 
|---|
|  |  |  | vipPayment1.setPayTime(new Date()); | 
|---|
|  |  |  | vipPayment1.setOrderNumber(tradeNo); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId()); | 
|---|
|  |  |  | Date vipEndTime = appUser.getVipEndTime(); | 
|---|
|  |  |  | if(null == vipEndTime){ | 
|---|
|  |  |  | appUser.setIsVip(1); | 
|---|
|  |  |  | if (null == vipEndTime) { | 
|---|
|  |  |  | vipEndTime = new Date(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | 
|---|
|  |  |  | calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1); | 
|---|
|  |  |  | appUser.setVipEndTime(calendar.getTime()); | 
|---|
|  |  |  | appUserService.updateById(appUser); | 
|---|
|  |  |  | // 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限 | 
|---|
|  |  |  | List<Long> longs = userConponClient.queryCouponByUser(appUser.getId()); | 
|---|
|  |  |  | redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600); | 
|---|
|  |  |  | // 如果交易结束 将当前订单金额分配到商户 | 
|---|
|  |  |  | payMoneyUtil.confirm(smid,vipPayment.getCode(),tradeNo,amount); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if("WAIT_BUYER_PAY".equals(tradeStatus)){ | 
|---|
|  |  |  | if ("WAIT_BUYER_PAY".equals(tradeStatus)) { | 
|---|
|  |  |  | Thread.sleep(wait); | 
|---|
|  |  |  | num++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Thread.sleep(wait); | 
|---|
|  |  |  | num++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(10 == num){ | 
|---|
|  |  |  | if (10 == num) { | 
|---|
|  |  |  | vipPayment1.setState(3); | 
|---|
|  |  |  | VipPaymentServiceImpl.this.updateById(vipPayment1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 购买年度会员支付回调处理 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param code | 
|---|
|  |  |  | * @param orderNumber | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public ResultUtil addVipPaymentCallback(String code, String orderNumber) throws Exception { | 
|---|
|  |  |  | public synchronized ResultUtil addVipPaymentCallback(String code, String orderNumber) throws Exception { | 
|---|
|  |  |  | VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code)); | 
|---|
|  |  |  | if(vipPayment1.getPayStatus() != 1){ | 
|---|
|  |  |  | if (vipPayment1.getPayStatus() == 2) { | 
|---|
|  |  |  | return ResultUtil.success(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | vipPayment1.setPayStatus(2); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId()); | 
|---|
|  |  |  | Date vipEndTime = appUser.getVipEndTime(); | 
|---|
|  |  |  | if(null == vipEndTime){ | 
|---|
|  |  |  | if (null == vipEndTime) { | 
|---|
|  |  |  | vipEndTime = new Date(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(vipEndTime); | 
|---|
|  |  |  | calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1); | 
|---|
|  |  |  | appUser.setIsVip(1); | 
|---|
|  |  |  | appUser.setVipEndTime(calendar.getTime()); | 
|---|
|  |  |  | appUserService.updateById(appUser); | 
|---|
|  |  |  | // 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限 | 
|---|
|  |  |  | List<Long> longs = userConponClient.queryCouponByUser(appUser.getId()); | 
|---|
|  |  |  | redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600); | 
|---|
|  |  |  | return ResultUtil.success(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|