| @@ -89,9 +89,9 @@ public enum ErrorCode{ | |||||
| COUPON_CHANNEL_IS_EXISTED(2027, "此券已投放过"), | COUPON_CHANNEL_IS_EXISTED(2027, "此券已投放过"), | ||||
| COUPON_SEND_IS_EXISTED(2028, "此券已经添加到该渠道"), | COUPON_SEND_IS_EXISTED(2028, "此券已经添加到该渠道"), | ||||
| COUPON_SEND_IS_INVALID(2029, "此发券渠道不存在"), | COUPON_SEND_IS_INVALID(2029, "此发券渠道不存在"), | ||||
| COUPON_SEND_UP_TO_1DAYLIMIT(2030, "此用户一天内发券到达限制"), | |||||
| COUPON_SEND_UP_TO_COUPONLIMIT(2031, "此用户发此券到达限制"), | |||||
| COUPON_SEND_NOT_INRANG(2032, "发券不在允许的时间段"), | |||||
| PUSH_LIMIT_UP_TO_1DAYLIMIT(2030, "此用户一天内发券到达疲劳度限制"), | |||||
| PUSH_LIMIT_UP_TO_COUPONLIMIT(2031, "此用户发此券到达疲劳度限制"), | |||||
| PUSH_LIMIT_NOT_INRANG(2032, "不在疲劳度允许的时间段"), | |||||
| /** | /** | ||||
| * 车流 2040 | * 车流 2040 | ||||
| */ | */ | ||||
| @@ -18,7 +18,7 @@ public interface PushLimitService { | |||||
| PageInfo<PushLimit> listAsPage(PushLimit record, Integer pageIndex, Integer pageSize); | PageInfo<PushLimit> listAsPage(PushLimit record, Integer pageIndex, Integer pageSize); | ||||
| /** | /** | ||||
| * 获取租户的ScoreRules | |||||
| * 获取租户的PushLimit | |||||
| * | * | ||||
| * @param tenantId | * @param tenantId | ||||
| * @return | * @return | ||||
| @@ -47,6 +47,13 @@ public interface PushLimitService { | |||||
| */ | */ | ||||
| void deleteById(Long id); | void deleteById(Long id); | ||||
| /** | |||||
| * 检查发送时间 | |||||
| * @param tenantId | |||||
| * @return | |||||
| */ | |||||
| boolean checkSendTime(String tenantId); | |||||
| /** | /** | ||||
| * 发短信检查 | * 发短信检查 | ||||
| * @param tenantId | * @param tenantId | ||||
| @@ -125,15 +125,27 @@ public class CouponInjectServiceImpl implements CouponInjectService { | |||||
| record.setSendAmount(cUsers.size()); | record.setSendAmount(cUsers.size()); | ||||
| couponInjectMapper.insertSelective(record); | couponInjectMapper.insertSelective(record); | ||||
| if(record.getSendType().equals(EnumCouponInjectSendType.IMMEDIATE.getCode())) { | if(record.getSendType().equals(EnumCouponInjectSendType.IMMEDIATE.getCode())) { | ||||
| sendNow(wxCoupon,cUsers,record); | |||||
| try { | |||||
| sendNow(wxCoupon, cUsers, record); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| if (e.getErrorCode() == ErrorCode.PUSH_LIMIT_NOT_INRANG.getCode()) { | |||||
| return new ResultData(ErrorCode.PUSH_LIMIT_NOT_INRANG); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| return new ResultData(); | return new ResultData(); | ||||
| } | } | ||||
| private void sendMsg(CouponInject record, List<WxCUser> cUsers) { | private void sendMsg(CouponInject record, List<WxCUser> cUsers) { | ||||
| if (!pushLimitService.checkMsg(record.getTenantId())) { | |||||
| logger.error("发送短信时间在疲劳度允许时间范围外"); | |||||
| boolean checkMsg = false; | |||||
| try { | |||||
| checkMsg = pushLimitService.checkMsg(record.getTenantId()); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| } | |||||
| if (!checkMsg) { | |||||
| return; | return; | ||||
| } | } | ||||
| @@ -208,19 +220,28 @@ public class CouponInjectServiceImpl implements CouponInjectService { | |||||
| } | } | ||||
| private void sendNow(WxCoupon wxCoupon,List<WxCUser> cUsers,CouponInject record){ | private void sendNow(WxCoupon wxCoupon,List<WxCUser> cUsers,CouponInject record){ | ||||
| //查询标签用户 | |||||
| int sendRealAmount=0; | |||||
| List<WxCUser> sentUsers = new ArrayList<WxCUser>(); | |||||
| for (WxCUser tempCUser : cUsers) { | |||||
| boolean bResult = sendCouponToUser(tempCUser,wxCoupon,record.getId()); | |||||
| if (bResult) { | |||||
| sentUsers.add(tempCUser); | |||||
| sendRealAmount += 1; | |||||
| } | |||||
| boolean checkTime = false; | |||||
| try { | |||||
| checkTime = pushLimitService.checkSendTime(wxCoupon.getTenantId()); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| throw new MallinkException(e.getErrorCode(), e.getMessage()); | |||||
| } | } | ||||
| if (checkTime) { | |||||
| //查询标签用户 | |||||
| int sendRealAmount=0; | |||||
| List<WxCUser> sentUsers = new ArrayList<WxCUser>(); | |||||
| for (WxCUser tempCUser : cUsers) { | |||||
| boolean bResult = sendCouponToUser(tempCUser,wxCoupon,record.getId()); | |||||
| if (bResult) { | |||||
| sentUsers.add(tempCUser); | |||||
| sendRealAmount += 1; | |||||
| } | |||||
| } | |||||
| //发送短信 | |||||
| sendMsg(record,sentUsers); | |||||
| //发送短信 | |||||
| sendMsg(record,sentUsers); | |||||
| } | |||||
| } | } | ||||
| @@ -77,6 +77,20 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| pushLimitMapper.deleteByPrimaryKey(id); | pushLimitMapper.deleteByPrimaryKey(id); | ||||
| } | } | ||||
| @Override | |||||
| public boolean checkSendTime(String tenantId) { | |||||
| Date curDate = new Date(); | |||||
| // 1. get tenant limit | |||||
| PushLimit pushLimit = getPushLimit(tenantId); | |||||
| // 2. check time | |||||
| boolean isInDate = DateUtils.isInDate(curDate, pushLimit.getTimeStart(), pushLimit.getTimeEnd()); | |||||
| if (!isInDate) { | |||||
| logger.error("短信已达到每天上限"); | |||||
| throw new MallinkException(ErrorCode.PUSH_LIMIT_NOT_INRANG); | |||||
| } | |||||
| return isInDate; | |||||
| } | |||||
| @Override | @Override | ||||
| public boolean checkMsg(String tenantId) { | public boolean checkMsg(String tenantId) { | ||||
| Date curDate = new Date(); | Date curDate = new Date(); | ||||
| @@ -86,7 +100,7 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| boolean isInDate = DateUtils.isInDate(curDate, pushLimit.getTimeStart(), pushLimit.getTimeEnd()); | boolean isInDate = DateUtils.isInDate(curDate, pushLimit.getTimeStart(), pushLimit.getTimeEnd()); | ||||
| if (!isInDate) { | if (!isInDate) { | ||||
| logger.error("短信已达到每天上限"); | logger.error("短信已达到每天上限"); | ||||
| throw new MallinkException(ErrorCode.COUPON_SEND_NOT_INRANG); | |||||
| throw new MallinkException(ErrorCode.PUSH_LIMIT_NOT_INRANG); | |||||
| } | } | ||||
| // TODO 3. check phone had up to limit | // TODO 3. check phone had up to limit | ||||
| return true; | return true; | ||||
| @@ -100,7 +114,7 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| // 2. check time 是否在给定的时间段内 | // 2. check time 是否在给定的时间段内 | ||||
| boolean isInDate = DateUtils.isInDate(curDate, pushLimit.getTimeStart(), pushLimit.getTimeEnd()); | boolean isInDate = DateUtils.isInDate(curDate, pushLimit.getTimeStart(), pushLimit.getTimeEnd()); | ||||
| if (!isInDate) { | if (!isInDate) { | ||||
| throw new MallinkException(ErrorCode.COUPON_SEND_NOT_INRANG); | |||||
| throw new MallinkException(ErrorCode.PUSH_LIMIT_NOT_INRANG); | |||||
| } | } | ||||
| // 3. check 每人每天多少张券 | // 3. check 每人每天多少张券 | ||||
| Map params = new HashMap<String, Object>(); | Map params = new HashMap<String, Object>(); | ||||
| @@ -110,7 +124,7 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| if (countForUser >= pushLimit.getCouponAmount()) { | if (countForUser >= pushLimit.getCouponAmount()) { | ||||
| String errMsg = "每人每天领券已达到疲劳度上限: " + pushLimit.getCouponAmount(); | String errMsg = "每人每天领券已达到疲劳度上限: " + pushLimit.getCouponAmount(); | ||||
| logger.error(errMsg); | logger.error(errMsg); | ||||
| throw new MallinkException(ErrorCode.COUPON_SEND_UP_TO_COUPONLIMIT.getCode(), errMsg); | |||||
| throw new MallinkException(ErrorCode.PUSH_LIMIT_UP_TO_1DAYLIMIT.getCode(), errMsg); | |||||
| } | } | ||||
| // 4. check 每人多少天内不会重复收到一张券 | // 4. check 每人多少天内不会重复收到一张券 | ||||
| params.put("couponId", couponId); | params.put("couponId", couponId); | ||||
| @@ -119,7 +133,7 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| if (countForUserCoupon >= 1) { | if (countForUserCoupon >= 1) { | ||||
| String errMsg = "每人" + pushLimit.getCouponDay() + "天领一张券已到疲劳度上限"; | String errMsg = "每人" + pushLimit.getCouponDay() + "天领一张券已到疲劳度上限"; | ||||
| logger.error(errMsg); | logger.error(errMsg); | ||||
| throw new MallinkException(ErrorCode.COUPON_SEND_UP_TO_COUPONLIMIT.getCode(), errMsg); | |||||
| throw new MallinkException(ErrorCode.PUSH_LIMIT_UP_TO_COUPONLIMIT.getCode(), errMsg); | |||||
| } | } | ||||
| return true; | return true; | ||||
| @@ -104,24 +104,34 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { | |||||
| } | } | ||||
| } | } | ||||
| List<WxCouponSend> wxCouponSends = wxCouponSendMapper.findList(wxCouponSendQuery); | |||||
| for (WxCouponSend send : wxCouponSends) { | |||||
| boolean checkLimit = false; | |||||
| // 检查疲劳度 | |||||
| try { | |||||
| checkLimit = pushLimitService.checkCoupon(tenantId, cUserId, send.getCouponId()); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| } | |||||
| if (!checkLimit) { | |||||
| continue; | |||||
| } | |||||
| // 发放免费券 | |||||
| WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId()); | |||||
| if (couponOrder != null) { | |||||
| wxCouponActionLogService.addOne(tenantId, send.getCouponId(), couponOrder.getId(), actionType, send.getId()); | |||||
| boolean checkTime = false; | |||||
| try { | |||||
| checkTime = pushLimitService.checkSendTime(tenantId); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| return; | |||||
| } | |||||
| if (checkTime) { | |||||
| List<WxCouponSend> wxCouponSends = wxCouponSendMapper.findList(wxCouponSendQuery); | |||||
| for (WxCouponSend send : wxCouponSends) { | |||||
| boolean checkLimit = false; | |||||
| // 检查疲劳度 | |||||
| try { | |||||
| checkLimit = pushLimitService.checkCoupon(tenantId, cUserId, send.getCouponId()); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| } | |||||
| if (!checkLimit) { | |||||
| continue; | |||||
| } | |||||
| // 发放免费券 | |||||
| WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cUserId, send.getCouponId()); | |||||
| if (couponOrder != null) { | |||||
| wxCouponActionLogService.addOne(tenantId, send.getCouponId(), couponOrder.getId(), actionType, send.getId()); | |||||
| } | |||||
| wxCouponService.reduceInventory(send.getCouponId(), 1); | |||||
| } | } | ||||
| wxCouponService.reduceInventory(send.getCouponId(), 1); | |||||
| } | } | ||||
| } | } | ||||