|
|
|
@ -16,6 +16,7 @@ import com.hai.model.HighUserModel; |
|
|
|
|
import com.hai.service.*; |
|
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Isolation; |
|
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
@ -68,6 +69,9 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe |
|
|
|
|
@Resource |
|
|
|
|
private UserCenter userCenter; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void insertCodeList(List<HighDiscountAgentCode> discountAgentCodeList) { |
|
|
|
|
highDiscountAgentCodeMapper.insertListCode(discountAgentCodeList); |
|
|
|
@ -181,7 +185,7 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Transactional(rollbackFor=Exception.class,isolation = Isolation.SERIALIZABLE,propagation= Propagation.REQUIRES_NEW, timeout = 30) |
|
|
|
|
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW, timeout = 30) |
|
|
|
|
public void useDiscount(Long discountAgentCodeId) throws Exception { |
|
|
|
|
HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class); |
|
|
|
|
if (userModel == null) { |
|
|
|
@ -198,49 +202,70 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe |
|
|
|
|
if (!discountAgentCode.getStatus().equals(2)) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态错误"); |
|
|
|
|
} |
|
|
|
|
if (StringUtils.isBlank(discountAgentCode.getExt2())) { |
|
|
|
|
HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); |
|
|
|
|
if (rel == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商关联关系"); |
|
|
|
|
} |
|
|
|
|
HighDiscount discount = highDiscountService.getDiscountById(rel.getDiscountId()); |
|
|
|
|
if (discount == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券"); |
|
|
|
|
} |
|
|
|
|
List<HighDiscountCouponRel> couponRelList = highDiscountCouponRelService.getRelByDiscount(discount.getId()); |
|
|
|
|
if (couponRelList.size() == 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置卡券"); |
|
|
|
|
HighDiscountUserRel discountUserRel = highDiscountUserRelService.getRelByAgentCodeId(discountAgentCodeId); |
|
|
|
|
if (discountUserRel != null) { |
|
|
|
|
if (!userModel.getHighUser().getId().equals(discountUserRel.getUserId())) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法使用,领取人和使用人不一致。"); |
|
|
|
|
} |
|
|
|
|
List<HighDiscountCouponRel> couponList = couponRelList.stream().filter(o -> o.getHighCoupon().getCouponSource().equals(5)).collect(Collectors.toList()); |
|
|
|
|
if (couponList.size() == 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置重庆中石油卡券"); |
|
|
|
|
} |
|
|
|
|
String tradeId = discountAgentCode.getId()+""+System.currentTimeMillis()+""; |
|
|
|
|
// 给用户发码
|
|
|
|
|
JSONObject response = ChongQingCNPCService.sendCNPCTicket( |
|
|
|
|
couponList.get(0).getHighCoupon().getCouponKey(), |
|
|
|
|
tradeId, |
|
|
|
|
1, |
|
|
|
|
userModel.getHighUser().getPhone()); |
|
|
|
|
JSONObject couponDetail = response.getJSONObject("ticketDetail"); |
|
|
|
|
JSONArray codeList = response.getJSONArray("codeList"); |
|
|
|
|
|
|
|
|
|
for (Object data : codeList) { |
|
|
|
|
HighCouponCodeOther couponCodeOther = new HighCouponCodeOther(); |
|
|
|
|
couponCodeOther.setType(3); |
|
|
|
|
couponCodeOther.setDiscountAgentCodeId(discountAgentCode.getId()); |
|
|
|
|
couponCodeOther.setCouTypeCode(couponDetail.getString("requestCode")); |
|
|
|
|
couponCodeOther.setCouNo(ChongQingCNPCService.decryptCouponCode(String.valueOf(data))); |
|
|
|
|
couponCodeOther.setActiveTime(new Date()); |
|
|
|
|
couponCodeOther.setValidStartDate(DateUtil.format(couponDetail.getString("effectiveTime"), "yyyy-MM-dd")); |
|
|
|
|
couponCodeOther.setValidEndDate(DateUtil.format(couponDetail.getString("expiredDate"), "yyyy-MM-dd")); |
|
|
|
|
couponCodeOther.setStatus(20); |
|
|
|
|
couponCodeOther.setCreateTime(new Date()); |
|
|
|
|
highCouponCodeOtherMapper.insert(couponCodeOther); |
|
|
|
|
} |
|
|
|
|
String key = "DiscountAgentId_" + discountAgentCode.getDiscountAgentId(); |
|
|
|
|
try { |
|
|
|
|
// 分布式锁占坑
|
|
|
|
|
Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, discountAgentCode.getId()); |
|
|
|
|
if(lock) { |
|
|
|
|
if (StringUtils.isBlank(discountAgentCode.getExt2())) { |
|
|
|
|
HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); |
|
|
|
|
if (rel == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商关联关系"); |
|
|
|
|
} |
|
|
|
|
HighDiscount discount = highDiscountService.getDiscountById(rel.getDiscountId()); |
|
|
|
|
if (discount == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券"); |
|
|
|
|
} |
|
|
|
|
List<HighDiscountCouponRel> couponRelList = highDiscountCouponRelService.getRelByDiscount(discount.getId()); |
|
|
|
|
if (couponRelList.size() == 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置卡券"); |
|
|
|
|
} |
|
|
|
|
List<HighDiscountCouponRel> couponList = couponRelList.stream().filter(o -> o.getHighCoupon().getCouponSource().equals(5)).collect(Collectors.toList()); |
|
|
|
|
if (couponList.size() == 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置重庆中石油卡券"); |
|
|
|
|
} |
|
|
|
|
String tradeId = discountAgentCode.getId()+""+System.currentTimeMillis()+""; |
|
|
|
|
// 给用户发码
|
|
|
|
|
JSONObject response = ChongQingCNPCService.sendCNPCTicket( |
|
|
|
|
couponList.get(0).getHighCoupon().getCouponKey(), |
|
|
|
|
tradeId, |
|
|
|
|
1, |
|
|
|
|
userModel.getHighUser().getPhone()); |
|
|
|
|
JSONObject couponDetail = response.getJSONObject("ticketDetail"); |
|
|
|
|
JSONArray codeList = response.getJSONArray("codeList"); |
|
|
|
|
|
|
|
|
|
for (Object data : codeList) { |
|
|
|
|
HighCouponCodeOther couponCodeOther = new HighCouponCodeOther(); |
|
|
|
|
couponCodeOther.setType(3); |
|
|
|
|
couponCodeOther.setDiscountAgentCodeId(discountAgentCode.getId()); |
|
|
|
|
couponCodeOther.setCouTypeCode(couponDetail.getString("requestCode")); |
|
|
|
|
couponCodeOther.setCouNo(ChongQingCNPCService.decryptCouponCode(String.valueOf(data))); |
|
|
|
|
couponCodeOther.setActiveTime(new Date()); |
|
|
|
|
couponCodeOther.setValidStartDate(DateUtil.format(couponDetail.getString("effectiveTime"), "yyyy-MM-dd")); |
|
|
|
|
couponCodeOther.setValidEndDate(DateUtil.format(couponDetail.getString("expiredDate"), "yyyy-MM-dd")); |
|
|
|
|
couponCodeOther.setStatus(20); |
|
|
|
|
couponCodeOther.setCreateTime(new Date()); |
|
|
|
|
highCouponCodeOtherMapper.insert(couponCodeOther); |
|
|
|
|
} |
|
|
|
|
discountAgentCode.setExt2(tradeId); |
|
|
|
|
updateCode(discountAgentCode); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// 加锁失败,重试
|
|
|
|
|
Thread.sleep(100); |
|
|
|
|
useDiscount(discountAgentCodeId); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
|
|
discountAgentCode.setExt2(tradeId); |
|
|
|
|
updateCode(discountAgentCode); |
|
|
|
|
} finally { |
|
|
|
|
// 删除key,释放锁
|
|
|
|
|
redisTemplate.delete(key); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|