diff --git a/mallinkService/src/main/java/com/iformall/common/ErrorCode.java b/mallinkService/src/main/java/com/iformall/common/ErrorCode.java index 5f7879226..54fe07c25 100644 --- a/mallinkService/src/main/java/com/iformall/common/ErrorCode.java +++ b/mallinkService/src/main/java/com/iformall/common/ErrorCode.java @@ -86,12 +86,12 @@ public enum ErrorCode{ COUPON_TYPE_IS_NOT_ACTIVE(2025, "此券不能用于主动领取"), COUPON_TYPE_IS_NOT_PASSIVE(2026, "此券不能用于定向投放"), - COUPON_CHANNEL_IS_EXISTED(2027, "此券已投放过"), - COUPON_SEND_IS_EXISTED(2028, "此券已经添加到该渠道"), - COUPON_SEND_IS_INVALID(2029, "此发券渠道不存在"), + COUPON_SEND_UP_TO_1DAYLIMIT(2030, "此用户一天内发券到达限制"), + COUPON_SEND_UP_TO_COUPONLIMIT(2031, "此用户发此券到达限制"), + COUPON_SEND_NOT_INRANG(2032, "发券不在允许的时间段"), /** * 车流 2040 */ diff --git a/mallinkService/src/main/java/com/iformall/mapper/WxCouponActionLogMapper.java b/mallinkService/src/main/java/com/iformall/mapper/WxCouponActionLogMapper.java index ef4d9d768..f64f1c0f1 100644 --- a/mallinkService/src/main/java/com/iformall/mapper/WxCouponActionLogMapper.java +++ b/mallinkService/src/main/java/com/iformall/mapper/WxCouponActionLogMapper.java @@ -29,4 +29,18 @@ public interface WxCouponActionLogMapper extends CommonMapper params); + + /** + * 每人多少天不会重复收到一张券 + * @param params + * @return + */ + int getCountByUserAndCoupon(Map params); + } diff --git a/mallinkService/src/main/java/com/iformall/service/PushLimitService.java b/mallinkService/src/main/java/com/iformall/service/PushLimitService.java index a1382c6be..97a6e1f14 100644 --- a/mallinkService/src/main/java/com/iformall/service/PushLimitService.java +++ b/mallinkService/src/main/java/com/iformall/service/PushLimitService.java @@ -2,6 +2,8 @@ package com.iformall.service; import com.github.pagehelper.PageInfo; import com.iformall.domain.po.PushLimit; +import com.iformall.domain.po.WxCUser; +import com.iformall.domain.po.WxCoupon; public interface PushLimitService { @@ -44,4 +46,21 @@ public interface PushLimitService { * @param id */ void deleteById(Long id); + + /** + * 发短信检查 + * @param tenantId + * @param phone + * @return + */ + boolean checkMsg(String tenantId, String phone); + + /** + * 主动发券检查 + * @param user + * @param coupon + * @return + */ + boolean checkCoupon(WxCUser user, WxCoupon coupon); + } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java index 885133790..864142dff 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java @@ -2,25 +2,36 @@ package com.iformall.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; import com.iformall.domain.po.PushLimit; +import com.iformall.domain.po.WxCUser; +import com.iformall.domain.po.WxCoupon; +import com.iformall.exception.MallinkException; import com.iformall.mapper.PushLimitMapper; +import com.iformall.mapper.WxCouponActionLogMapper; import com.iformall.service.PushLimitService; +import com.iformall.utils.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Service public class PushLimitServiceImpl implements PushLimitService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Autowired + + @Autowired PushLimitMapper pushLimitMapper; + @Autowired + WxCouponActionLogMapper wxCouponActionLogMapper; + @Override public PageInfo listAsPage(PushLimit record, Integer pageIndex, Integer pageSize) { @@ -65,4 +76,46 @@ public class PushLimitServiceImpl implements PushLimitService { public void deleteById(Long id) { pushLimitMapper.deleteByPrimaryKey(id); } + + @Override + public boolean checkMsg(String tenantId, String phone) { + 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) { + throw new MallinkException(ErrorCode.COUPON_SEND_NOT_INRANG); + } + // TODO 3. check phone had up to limit + return true; + } + + @Override + public boolean checkCoupon(WxCUser user, WxCoupon coupon) { + Date curDate = new Date(); + // 1. get tenant limit + PushLimit pushLimit = getPushLimit(user.getTenantId()); + // 2. check time 是否在给定的时间段内 + boolean isInDate = DateUtils.isInDate(curDate, pushLimit.getTimeStart(), pushLimit.getTimeEnd()); + if (!isInDate) { + throw new MallinkException(ErrorCode.COUPON_SEND_NOT_INRANG); + } + // 3. check 每人每天多少张券 + Map params = new HashMap(); + params.put("tenantId", user.getTenantId()); + params.put("c_user_id", user.getId()); + int countForUser = wxCouponActionLogMapper.getCountByUser(params); + if (countForUser >= pushLimit.getCouponAmount()) { + throw new MallinkException(ErrorCode.COUPON_SEND_UP_TO_COUPONLIMIT); + } + // 4. check 每人多少天内不会重复收到一张券 + params.put("dayNum", pushLimit.getCouponDay()); + int countForUserCoupon = wxCouponActionLogMapper.getCountByUserAndCoupon(params); + if (countForUserCoupon >= pushLimit.getCouponDay()) { + throw new MallinkException(ErrorCode.COUPON_SEND_UP_TO_COUPONLIMIT); + } + + return true; + } } diff --git a/mallinkService/src/main/java/com/iformall/utils/DateUtils.java b/mallinkService/src/main/java/com/iformall/utils/DateUtils.java index 17242ed70..423f664b9 100755 --- a/mallinkService/src/main/java/com/iformall/utils/DateUtils.java +++ b/mallinkService/src/main/java/com/iformall/utils/DateUtils.java @@ -890,4 +890,37 @@ public class DateUtils { } } + /** + * 判断时间是否在时间段内 + * @param date + * @param timeStart + * @param timeEnd + * @return + */ + public static boolean isInDate(Date date, String timeStart, String timeEnd) { + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); + String strDate = sdf.format(date); + + // 截取当前时间时分 + int strDateH = Integer.parseInt(strDate.substring(0, 2)); + int strDateM = Integer.parseInt(strDate.substring(3, 5)); + int strDateT = strDateH * 60 + strDateM; + + // 截取开始时间时分 + int strDateBeginH = Integer.parseInt(timeStart.substring(0, 2)); + int strDateBeginM = Integer.parseInt(timeStart.substring(3, 5)); + int strDateBeginT = strDateBeginH * 60 + strDateBeginM; + // 截取结束时间时分 + int strDateEndH = Integer.parseInt(timeEnd.substring(0, 2)); + int strDateEndM = Integer.parseInt(timeEnd.substring(3, 5)); + int strDateEndT = strDateEndH * 60 + strDateEndM; + + + if (strDateT >= strDateBeginT && strDateT <= strDateEndT) { + return true; + } else { + return false; + } + } + } diff --git a/mallinkService/src/main/java/com/iformall/utils/Utility.java b/mallinkService/src/main/java/com/iformall/utils/Utility.java index 582a403f1..f1c05310d 100644 --- a/mallinkService/src/main/java/com/iformall/utils/Utility.java +++ b/mallinkService/src/main/java/com/iformall/utils/Utility.java @@ -601,4 +601,6 @@ public final class Utility { } return sb.toString(); } + + } diff --git a/mallinkService/src/main/resources/mapper/WxCouponActionLogMapper.xml b/mallinkService/src/main/resources/mapper/WxCouponActionLogMapper.xml index 1c7501ead..c8a0a2c4f 100644 --- a/mallinkService/src/main/resources/mapper/WxCouponActionLogMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCouponActionLogMapper.xml @@ -154,4 +154,23 @@ and channel_id <= #{channelId} + + + +