| @@ -32,6 +32,12 @@ public class WxCouponSend extends BaseEntity { | |||||
| @Transient | @Transient | ||||
| public static final String KEY_MERCHANT_LNVENTORY = "merchantLnventory" ; | public static final String KEY_MERCHANT_LNVENTORY = "merchantLnventory" ; | ||||
| /** | |||||
| * limit 商户注券数 | |||||
| */ | |||||
| @Transient | |||||
| public static final String KEY_MERCHANT_SEND = "merchantSend" ; | |||||
| @Id | @Id | ||||
| protected Long id; | protected Long id; | ||||
| @@ -20,18 +20,18 @@ public interface WxCouponSendMapper extends CommonMapper<WxCouponSend, String> { | |||||
| * 商户注券 库存减少 | * 商户注券 库存减少 | ||||
| * | * | ||||
| * @param id 券ID | * @param id 券ID | ||||
| * @param remainInventory 当前库存 | |||||
| * @param merchantSend 当前已发数 | |||||
| * @param number 减少数量 | * @param number 减少数量 | ||||
| */ | */ | ||||
| Integer reduceMerchantRemainInventory(Long id, Integer remainInventory, Integer number); | |||||
| Integer reduceMerchantSend(Long id, Integer merchantSend, Integer number); | |||||
| /** | /** | ||||
| * 商户注券 库存退回 | * 商户注券 库存退回 | ||||
| * | * | ||||
| * @param id 券ID | * @param id 券ID | ||||
| * @param remainInventory 当前库存 | |||||
| * @param merchantSend 当前已发数 | |||||
| * @param number 回退数量 | * @param number 回退数量 | ||||
| */ | */ | ||||
| Integer backMerchantRemainInventory(Long id, Integer remainInventory, Integer number); | |||||
| Integer backMerchantSend(Long id, Integer merchantSend, Integer number); | |||||
| } | } | ||||
| @@ -77,19 +77,19 @@ public interface WxCouponSendService { | |||||
| * 商户注券 库存减少 | * 商户注券 库存减少 | ||||
| * | * | ||||
| * @param id 券ID | * @param id 券ID | ||||
| * @param remainInventory 当前库存 | |||||
| * @param merchantSend 当前已发数 | |||||
| * @param number 减少数量 | * @param number 减少数量 | ||||
| */ | */ | ||||
| void reduceMerchantRemainInventory(Long id, Integer remainInventory, Integer number); | |||||
| void reduceMerchantSend(Long id, Integer merchantSend, Integer number); | |||||
| /** | /** | ||||
| * 商户注券 库存退回 | * 商户注券 库存退回 | ||||
| * | * | ||||
| * @param id 券ID | * @param id 券ID | ||||
| * @param remainInventory 当前库存 | |||||
| * @param merchantSend 当前已发数 | |||||
| * @param number 回退数量 | * @param number 回退数量 | ||||
| */ | */ | ||||
| void backMerchantRemainInventory(Long id, Integer remainInventory, Integer number); | |||||
| void backMerchantRemainInventory(Long id, Integer merchantSend, Integer number); | |||||
| void handSel(WxCouponSend wxCouponSend, WxCUser user) ; | void handSel(WxCouponSend wxCouponSend, WxCUser user) ; | ||||
| @@ -99,7 +99,8 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| //商户卡库存校验 | //商户卡库存校验 | ||||
| if (Objects.equals(record.getSendType(), EnumCouponSendSendType.MERCHANT.getCode())) { | if (Objects.equals(record.getSendType(), EnumCouponSendSendType.MERCHANT.getCode())) { | ||||
| JSONObject jo = JSONObject.parseObject(record.getConditions()); | JSONObject jo = JSONObject.parseObject(record.getConditions()); | ||||
| if (Objects.isNull(jo) || !jo.containsKey(WxCouponSend.KEY_MERCHANT_LNVENTORY)) { | |||||
| int merchantLnventory = jo.getIntValue(WxCouponSend.KEY_MERCHANT_LNVENTORY); | |||||
| if (0 >= merchantLnventory) { | |||||
| throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY); | throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY); | ||||
| } | } | ||||
| //查询总库存 | //查询总库存 | ||||
| @@ -123,7 +124,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| //if (Objects.isNull(couponMerchant)) { | //if (Objects.isNull(couponMerchant)) { | ||||
| // throw new MallinkException(ErrorCode.COUPON_MERCHANT_NOT_FOUND); | // throw new MallinkException(ErrorCode.COUPON_MERCHANT_NOT_FOUND); | ||||
| //} | //} | ||||
| int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY); | |||||
| //商家购券数不能大于总库存 | //商家购券数不能大于总库存 | ||||
| if (merchantLnventory > wxCouponOfDb.getRemainInventory()) { | if (merchantLnventory > wxCouponOfDb.getRemainInventory()) { | ||||
| throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); | throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); | ||||
| @@ -156,13 +157,19 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); | throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); | ||||
| } | } | ||||
| //查询商家 | //查询商家 | ||||
| JSONObject jo = JSONObject.parseObject(record.getConditions()); | |||||
| if (Objects.isNull(jo) || !jo.containsKey(WxCouponSend.KEY_MERCHANT_LNVENTORY)) { | |||||
| int merchantLnventory = 0, merchantSend = 0; | |||||
| try { | |||||
| JSONObject jo = JSONObject.parseObject(record.getConditions()); | |||||
| merchantLnventory = jo.getIntValue(WxCouponSend.KEY_MERCHANT_LNVENTORY); | |||||
| merchantSend = jo.getIntValue(WxCouponSend.KEY_MERCHANT_SEND); | |||||
| } catch (Exception e) { | |||||
| logger.error(" updateInvalid 解析异常:{}", e.getMessage()); | |||||
| } | |||||
| if (0 >= merchantLnventory || 0 > merchantSend) { | |||||
| throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY); | throw new MallinkException(ErrorCode.COUPON_SEND_LIMIT_VERIFY); | ||||
| } | } | ||||
| int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY); | |||||
| //退回coupon库存 | //退回coupon库存 | ||||
| wxCouponService.backRemainInventory(queryById.getId(), wxCouponOfDb.getRemainInventory(), merchantLnventory); | |||||
| wxCouponService.backRemainInventory(queryById.getId(), wxCouponOfDb.getRemainInventory(), merchantLnventory - merchantSend); | |||||
| } | } | ||||
| wxCouponSendMapper.updateByPrimaryKeySelective(record); | wxCouponSendMapper.updateByPrimaryKeySelective(record); | ||||
| } | } | ||||
| @@ -537,16 +544,16 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| } | } | ||||
| @Override | @Override | ||||
| public void reduceMerchantRemainInventory(Long id, Integer remainInventory, Integer number) { | |||||
| int num = wxCouponSendMapper.reduceMerchantRemainInventory(id, remainInventory, number); | |||||
| public void reduceMerchantSend(Long id, Integer merchantSend, Integer number) { | |||||
| int num = wxCouponSendMapper.reduceMerchantSend(id, merchantSend, number); | |||||
| if (num == 0) { | if (num == 0) { | ||||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | ||||
| } | } | ||||
| } | } | ||||
| @Override | @Override | ||||
| public void backMerchantRemainInventory(Long id, Integer remainInventory, Integer number) { | |||||
| int num = wxCouponSendMapper.backMerchantRemainInventory(id, remainInventory, number); | |||||
| public void backMerchantRemainInventory(Long id, Integer merchantSend, Integer number) { | |||||
| int num = wxCouponSendMapper.backMerchantSend(id, merchantSend, number); | |||||
| if (num == 0) { | if (num == 0) { | ||||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | ||||
| } | } | ||||
| @@ -564,6 +571,9 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| // 发放免费券 | // 发放免费券 | ||||
| WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(user.getId(), cs.getCouponId(), cs); | WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(user.getId(), cs.getCouponId(), cs); | ||||
| wxCouponActionLogService.addOne(user.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId()); | wxCouponActionLogService.addOne(user.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId()); | ||||
| if (couponOrder != null) { | |||||
| sendMsgForSendCoupon(cs.getTenantId(), cs, couponOrder, user); | |||||
| } | |||||
| }); | }); | ||||
| } | } | ||||
| } | } | ||||
| @@ -218,7 +218,7 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||||
| int creditChangeNum = creditChangeNumOrigin ; | int creditChangeNum = creditChangeNumOrigin ; | ||||
| if (record.getCreditType().equals(EnumScoreType.CONSUMPTION.getCode()) || record.getCreditType().equals(EnumScoreType.SPEND_CREDIT.getCode())) { | if (record.getCreditType().equals(EnumScoreType.CONSUMPTION.getCode()) || record.getCreditType().equals(EnumScoreType.SPEND_CREDIT.getCode())) { | ||||
| //等级积分配置 | //等级积分配置 | ||||
| int levelScale = wxLevelConfigMapper.getScale(wxCUser.getTenantId(), wxCUser.getScore()) ; | |||||
| Integer levelScale = wxLevelConfigMapper.getScale(wxCUser.getTenantId(), wxCUser.getScore()); | |||||
| creditChangeNum = CreditUtil.calUserCredit(creditChangeNumOrigin, wxCUser, levelScale, wxCUserBasicInfo, wxScoreRulesService); | creditChangeNum = CreditUtil.calUserCredit(creditChangeNumOrigin, wxCUser, levelScale, wxCUserBasicInfo, wxScoreRulesService); | ||||
| } | } | ||||
| @@ -328,7 +328,7 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||||
| if (Objects.nonNull(userId)) { | if (Objects.nonNull(userId)) { | ||||
| WxCUser wxCUser = wxCUserMapper.selectByPrimaryKey(userId); | WxCUser wxCUser = wxCUserMapper.selectByPrimaryKey(userId); | ||||
| WxCUserBasicInfo wxCUserBasicInfo = wxCUserBasicInfoMapper.selectByPrimaryKey(userId); | WxCUserBasicInfo wxCUserBasicInfo = wxCUserBasicInfoMapper.selectByPrimaryKey(userId); | ||||
| int birthdayScale = wxLevelConfigMapper.getScale(wxCUser.getTenantId(), wxCUser.getScore()) ; | |||||
| Integer birthdayScale = wxLevelConfigMapper.getScale(wxCUser.getTenantId(), wxCUser.getScore()); | |||||
| creditNew = CreditUtil.calUserCredit(credit, wxCUser, birthdayScale, wxCUserBasicInfo, wxScoreRulesService); | creditNew = CreditUtil.calUserCredit(credit, wxCUser, birthdayScale, wxCUserBasicInfo, wxScoreRulesService); | ||||
| } else { | } else { | ||||
| creditNew = credit; | creditNew = credit; | ||||
| @@ -321,9 +321,10 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| JSONObject jo = JSONObject.parseObject(wxCouponSendVo.getConditions()) ; | JSONObject jo = JSONObject.parseObject(wxCouponSendVo.getConditions()) ; | ||||
| int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; | int merchantLnventory = jo.getInteger(WxCouponSend.KEY_MERCHANT_LNVENTORY) ; | ||||
| int merchantSend = jo.getInteger(WxCouponSend.KEY_MERCHANT_SEND); | |||||
| // 检查 优惠券 库存 | // 检查 优惠券 库存 | ||||
| if (merchantLnventory <= 0) { | |||||
| if (merchantLnventory - merchantSend <= 0) { | |||||
| //解锁 | //解锁 | ||||
| redisLock.unlock(couponIdStr, timeStr); | redisLock.unlock(couponIdStr, timeStr); | ||||
| logger.error("此券库存为0, couponId: " + couponIdStr); | logger.error("此券库存为0, couponId: " + couponIdStr); | ||||
| @@ -365,7 +366,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| try { | try { | ||||
| // 减库存 | // 减库存 | ||||
| wxCouponSendService.reduceMerchantRemainInventory(wxCouponSendVo.getId(),merchantLnventory,1); | |||||
| wxCouponSendService.reduceMerchantSend(wxCouponSendVo.getId(), merchantSend, 1); | |||||
| } catch (RuntimeException e) { | } catch (RuntimeException e) { | ||||
| logger.error("此券减库存失败, couponId: " + couponIdStr); | logger.error("此券减库存失败, couponId: " + couponIdStr); | ||||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | ||||
| @@ -207,7 +207,9 @@ | |||||
| WHERE a.coupon_order_id = o.id | WHERE a.coupon_order_id = o.id | ||||
| AND a.tenant_id = #{tenantId} | AND a.tenant_id = #{tenantId} | ||||
| AND o.c_user_id = #{cUserId} | AND o.c_user_id = #{cUserId} | ||||
| AND a.coupon_id = #{couponId} | |||||
| <if test=" null != couponId "> | |||||
| AND a.coupon_id = #{couponId} | |||||
| </if> | |||||
| AND a.channel_type = #{channelType} | AND a.channel_type = #{channelType} | ||||
| and a.create_time >= #{startTime} | and a.create_time >= #{startTime} | ||||
| and a.create_time <= #{endTime} | and a.create_time <= #{endTime} | ||||
| @@ -155,10 +155,10 @@ | |||||
| where c.id = cs.coupon_id and cs.status = 0 and | where c.id = cs.coupon_id and cs.status = 0 and | ||||
| ((c.status = 1) or (c.valid_type = 1 and c.valid_end_date < now())) | ((c.status = 1) or (c.valid_type = 1 and c.valid_end_date < now())) | ||||
| </update> | </update> | ||||
| <update id="reduceMerchantRemainInventory"> | |||||
| update wx_coupon_send SET conditions = JSON_SET(conditions,'$.merchantLnventory',#{remainInventory} - #{number}) where id = #{id} and conditions->'$.merchantLnventory' = #{remainInventory} | |||||
| <update id="reduceMerchantSend"> | |||||
| update wx_coupon_send SET conditions = JSON_SET(conditions,'$.merchantSend',#{merchantSend} - #{number}) where id = #{id} and conditions->'$.merchantSend' = #{merchantSend} | |||||
| </update> | </update> | ||||
| <update id="backMerchantRemainInventory"> | |||||
| update wx_coupon_send SET conditions = JSON_SET(conditions,'$.merchantLnventory',#{remainInventory} + #{number}) where id = #{id} and conditions->'$.merchantLnventory' = #{remainInventory} | |||||
| <update id="backMerchantSend"> | |||||
| update wx_coupon_send SET conditions = JSON_SET(conditions,'$.merchantSend',#{merchantSend} + #{number}) where id = #{id} and conditions->'$.merchantSend' = #{merchantSend} | |||||
| </update> | </update> | ||||
| </mapper> | </mapper> | ||||