Authored by chenjian

订单分享领券判断当天是否领过

  1 +package com.yoho.activity.common.bo;
  2 +
  3 +public class OrderShareCouponVO {
  4 + //是否成功获取到优惠码
  5 + private boolean drawCouponSuccess = false;
  6 +
  7 + //优惠券ID
  8 + private String couponId;
  9 + //优惠券名称
  10 + private String couponName;
  11 + //优惠码
  12 + private String couponCode;
  13 +
  14 + public String getCouponId() {
  15 + return couponId;
  16 + }
  17 + public void setCouponId(String couponId) {
  18 + this.couponId = couponId;
  19 + }
  20 + public String getCouponName() {
  21 + return couponName;
  22 + }
  23 + public void setCouponName(String couponName) {
  24 + this.couponName = couponName;
  25 + }
  26 + public String getCouponCode() {
  27 + return couponCode;
  28 + }
  29 + public void setCouponCode(String couponCode) {
  30 + this.couponCode = couponCode;
  31 + }
  32 + public boolean isDrawCouponSuccess() {
  33 + return drawCouponSuccess;
  34 + }
  35 + public void setDrawCouponSuccess(boolean drawCouponSuccess) {
  36 + this.drawCouponSuccess = drawCouponSuccess;
  37 + }
  38 +}
@@ -23,7 +23,12 @@ public class Constant { @@ -23,7 +23,12 @@ public class Constant {
23 // 已注册用户领取的人数 23 // 已注册用户领取的人数
24 public final static String USED_REGISTER_GET_TIME_MEM_KEY = "yh:users:used_register_get_time_"; 24 public final static String USED_REGISTER_GET_TIME_MEM_KEY = "yh:users:used_register_get_time_";
25 25
  26 + // 用户分享领券新用户
  27 + public final static int ORDER_SHARE_USERTYPE_NEW = 0;
  28 + // 用户分享领券老用户
  29 + public final static int ORDER_SHARE_USERTYPE_OLD = 1;
26 30
  31 + //订单分享用户领券返回码
27 public final static int ORDER_SHARE_MOBILE_ERROR = 1; 32 public final static int ORDER_SHARE_MOBILE_ERROR = 1;
28 33
29 public final static int ORDER_SHARE_USER_UNREGISTER = 2; 34 public final static int ORDER_SHARE_USER_UNREGISTER = 2;
@@ -37,4 +42,7 @@ public class Constant { @@ -37,4 +42,7 @@ public class Constant {
37 public final static int ORDER_SHARE_REGISTER_ERROR = 6; 42 public final static int ORDER_SHARE_REGISTER_ERROR = 6;
38 43
39 public final static int ORDER_SHARE_SENDCOUPON_ERROR = 7; 44 public final static int ORDER_SHARE_SENDCOUPON_ERROR = 7;
  45 +
  46 + //当天已经领取过
  47 + public final static int ORDER_SHARE_ALREADYDRAW_ERROR = 8;
40 } 48 }
@@ -129,4 +129,16 @@ public final class DateUtils { @@ -129,4 +129,16 @@ public final class DateUtils {
129 return second; 129 return second;
130 } 130 }
131 131
  132 + /**
  133 + * 获取当天零点时间
  134 + * @return
  135 + */
  136 + public static int getTodayZero() {
  137 + Calendar today = Calendar.getInstance();
  138 + today.set(Calendar.HOUR, 0);
  139 + today.set(Calendar.MINUTE, 0);
  140 + today.set(Calendar.SECOND, 0);
  141 + today.set(Calendar.MILLISECOND, 0);
  142 + return (int)(today.getTime().getTime() / 1000);
  143 + }
132 } 144 }
1 package com.yoho.coupon.dal; 1 package com.yoho.coupon.dal;
2 2
  3 +import org.apache.ibatis.annotations.Param;
  4 +
3 import com.yoho.coupon.dal.model.UserCouponHistory; 5 import com.yoho.coupon.dal.model.UserCouponHistory;
4 6
5 public interface IUserCouponHistoryDAO { 7 public interface IUserCouponHistoryDAO {
@@ -21,4 +23,13 @@ public interface IUserCouponHistoryDAO { @@ -21,4 +23,13 @@ public interface IUserCouponHistoryDAO {
21 * @return 23 * @return
22 */ 24 */
23 UserCouponHistory selectBySelective(UserCouponHistory record); 25 UserCouponHistory selectBySelective(UserCouponHistory record);
  26 +
  27 + /**
  28 + * 查看某用户某段时间以来领券次数
  29 + * @param uid
  30 + * @param activityId
  31 + * @param createTime
  32 + * @return
  33 + */
  34 + int selectCountAfter(@Param("uid") int uid, @Param("activityId") int activityId, @Param("createTime") int createTime);
24 } 35 }
@@ -20,6 +20,13 @@ @@ -20,6 +20,13 @@
20 from user_coupon_history 20 from user_coupon_history
21 where id = #{id,jdbcType=INTEGER} 21 where id = #{id,jdbcType=INTEGER}
22 </select> 22 </select>
  23 + <select id="selectCountAfter" resultType="java.lang.Integer">
  24 + select count(1)
  25 + from user_coupon_history
  26 + where uid = #{uid,jdbcType=INTEGER}
  27 + and activity_id = #{activityId,jdbcType=INTEGER}
  28 + and create_time > #{createTime,jdbcType=INTEGER}
  29 + </select>
23 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> 30 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
24 delete from user_coupon_history 31 delete from user_coupon_history
25 where id = #{id,jdbcType=INTEGER} 32 where id = #{id,jdbcType=INTEGER}
@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
16 import com.yoho.activity.common.ApiResponse; 16 import com.yoho.activity.common.ApiResponse;
17 import com.yoho.activity.common.bo.OrderShareActivityBO; 17 import com.yoho.activity.common.bo.OrderShareActivityBO;
18 import com.yoho.activity.common.bo.OrderShareBO; 18 import com.yoho.activity.common.bo.OrderShareBO;
  19 +import com.yoho.activity.common.bo.OrderShareCouponVO;
19 import com.yoho.activity.common.constatns.Constant; 20 import com.yoho.activity.common.constatns.Constant;
20 import com.yoho.activity.common.convert.OrderShareActivityConvert; 21 import com.yoho.activity.common.convert.OrderShareActivityConvert;
21 import com.yoho.activity.common.helper.SendSMSHelper; 22 import com.yoho.activity.common.helper.SendSMSHelper;
@@ -265,6 +266,14 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService @@ -265,6 +266,14 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
265 return respBO; 266 return respBO;
266 } 267 }
267 268
  269 + //判断老用户今天是否已经领券过
  270 + if(isDrawCouponToday(profileInfo.getUid(), activityInfo.getId())) {
  271 + respBO.setReturnCode(Constant.ORDER_SHARE_ALREADYDRAW_ERROR);
  272 + respBO.setReturnMsg("您今天已经领过咯,速度去有货购潮流!");
  273 + log.warn("unable to draw more today, uid: {}, activityId: {}", profileInfo.getUid(), activityInfo.getId());
  274 + return respBO;
  275 + }
  276 +
268 // 4、判断老用户是否达到分享限制 277 // 4、判断老用户是否达到分享限制
269 if (isUpToOrderShareLimit(orderCode, activityInfo.getId())) { 278 if (isUpToOrderShareLimit(orderCode, activityInfo.getId())) {
270 respBO.setReturnCode(Constant.ORDER_SHARE_OLDUSER_UPLIMIT); 279 respBO.setReturnCode(Constant.ORDER_SHARE_OLDUSER_UPLIMIT);
@@ -275,18 +284,18 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService @@ -275,18 +284,18 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
275 284
276 // 发送优惠券 285 // 发送优惠券
277 // 获取老人优惠券 286 // 获取老人优惠券
278 - OrderShareCoupon coupon = orderShareCouponDAO.selectCouponByDateAndType(DateUtils.getToday("yyyy-MM-dd"), 1);  
279 - String couponCode = sendCoupon(coupon.getCouponId(), String.valueOf(profileInfo.getUid()));  
280 - if (StringUtils.isEmpty(couponCode)) { 287 + OrderShareCouponVO couponVO = drawCoupon(profileInfo.getUid(), Constant.ORDER_SHARE_USERTYPE_OLD);
  288 + if (!couponVO.isDrawCouponSuccess()) {
281 respBO.setReturnCode(Constant.ORDER_SHARE_SENDCOUPON_ERROR); 289 respBO.setReturnCode(Constant.ORDER_SHARE_SENDCOUPON_ERROR);
282 respBO.setReturnMsg("优惠券已发光"); 290 respBO.setReturnMsg("优惠券已发光");
283 - log.error("orderShare sendCoupon failed, uid: {}, couponId: {}", profileInfo.getUid(), coupon.getCouponId()); 291 + log.error("orderShare sendCoupon failed, uid: {}, couponId: {}", profileInfo.getUid(), couponVO.getCouponId());
284 return respBO; 292 return respBO;
285 } 293 }
  294 + log.info("orderShare drawCoupon success, uid: {}, couponCode: {}", profileInfo.getUid(), couponVO.getCouponCode());
286 295
287 // 4、记录用户领券的记录 296 // 4、记录用户领券的记录
288 - respBO.setCoupon(couponCode);  
289 - updateOrderShareRecord(activityInfo.getId(), profileInfo.getUid(), orderCode, coupon.getCouponId(), 1); 297 + respBO.setCoupon(couponVO.getCouponCode());
  298 + updateOrderShareRecord(activityInfo.getId(), profileInfo.getUid(), orderCode, couponVO.getCouponCode(), Constant.ORDER_SHARE_USERTYPE_OLD);
290 299
291 return respBO; 300 return respBO;
292 } 301 }
@@ -333,20 +342,20 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService @@ -333,20 +342,20 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
333 342
334 // 3、发送优惠券 343 // 3、发送优惠券
335 // 获取新人优惠券 344 // 获取新人优惠券
336 - OrderShareCoupon coupon = orderShareCouponDAO.selectCouponByDateAndType(null, 0);  
337 - String couponCode = sendCoupon(coupon.getCouponId(), String.valueOf(model.getUid()));  
338 - if (StringUtils.isEmpty(couponCode)) { 345 + OrderShareCouponVO couponVO = drawCoupon(model.getUid(), Constant.ORDER_SHARE_USERTYPE_NEW);
  346 + if (!couponVO.isDrawCouponSuccess()) {
339 respBO.setReturnCode(Constant.ORDER_SHARE_SENDCOUPON_ERROR); 347 respBO.setReturnCode(Constant.ORDER_SHARE_SENDCOUPON_ERROR);
340 respBO.setReturnMsg("优惠券已发光"); 348 respBO.setReturnMsg("优惠券已发光");
341 - log.error("orderShare sendCoupon failed, uid: {}, couponId: {}", coupon.getCouponId(), model.getUid()); 349 + log.error("orderShare drawCoupon failed, uid: {}", model.getUid());
342 return respBO; 350 return respBO;
343 } 351 }
  352 + log.info("orderShare drawCoupon success, uid: {}, couponCode: {}", model.getUid(), couponVO.getCouponCode());
344 // 4、记录用户领券的记录 353 // 4、记录用户领券的记录
345 - respBO.setCoupon(couponCode);  
346 - updateOrderShareRecord(activityInfo.getId(), model.getUid(), orderCode, coupon.getCouponId(), 0); 354 + respBO.setCoupon(couponVO.getCouponCode());
  355 + updateOrderShareRecord(activityInfo.getId(), model.getUid(), orderCode, couponVO.getCouponCode(), Constant.ORDER_SHARE_USERTYPE_NEW);
347 356
348 //发送短信 357 //发送短信
349 - sendSMSHelper.sendNoticeSms(mobile, "【Yoho!Buy有货】恭喜您获得一张"+ coupon.getCouponName() +",您的登录账户是"+ mobile +",密码是"+ registerReqBO.getPassword() +";下载Yoho!Buy有货手机端http://a.app.qq.com/o/simple.jsp?pkgname=com.yoho,更多惊喜等着您!"); 358 + sendSMSHelper.sendNoticeSms(mobile, "【Yoho!Buy有货】恭喜您获得一张"+ couponVO.getCouponName() +",您的登录账户是"+ mobile +",密码是"+ registerReqBO.getPassword() +";下载Yoho!Buy有货手机端http://a.app.qq.com/o/simple.jsp?pkgname=com.yoho,更多惊喜等着您!");
350 359
351 return respBO; 360 return respBO;
352 } 361 }
@@ -427,6 +436,23 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService @@ -427,6 +436,23 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
427 } 436 }
428 437
429 /** 438 /**
  439 + * 检查老用户当天是否已经领取过用户券
  440 + * @param uid
  441 + * @param activityId
  442 + * @return
  443 + */
  444 + private boolean isDrawCouponToday(int uid, int activityId) {
  445 + int todayZero = DateUtils.getTodayZero();
  446 + int count = userCouponHistoryDAO.selectCountAfter(uid, activityId, todayZero);
  447 + if(count > 0) {
  448 + log.warn("the user have draw coupon today, uid: {}, count: {}", uid, count);
  449 + return false;
  450 + }
  451 + log.info("the user haven't draw coupon today, uid: {}, fromTime: {}", uid, todayZero);
  452 + return true;
  453 + }
  454 +
  455 + /**
430 * 检查老用户是否超出领取优惠券限制(默认限制5次) 456 * 检查老用户是否超出领取优惠券限制(默认限制5次)
431 * 457 *
432 * @param shareHistoryInfo 458 * @param shareHistoryInfo
@@ -448,6 +474,14 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService @@ -448,6 +474,14 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
448 return false; 474 return false;
449 } 475 }
450 476
  477 + /**
  478 + * 修改领券记录
  479 + * @param activityId
  480 + * @param uid
  481 + * @param orderCode
  482 + * @param couponId
  483 + * @param userType
  484 + */
451 private void updateOrderShareRecord(int activityId, int uid, long orderCode, String couponId, int userType) { 485 private void updateOrderShareRecord(int activityId, int uid, long orderCode, String couponId, int userType) {
452 addCouponHistory(activityId, uid, String.valueOf(orderCode), couponId, userType); 486 addCouponHistory(activityId, uid, String.valueOf(orderCode), couponId, userType);
453 UserShareHistory shareHistoryInfo = getShareHistoryByOrderAndActivity(orderCode, activityId); 487 UserShareHistory shareHistoryInfo = getShareHistoryByOrderAndActivity(orderCode, activityId);
@@ -457,4 +491,44 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService @@ -457,4 +491,44 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
457 } 491 }
458 updateShareHistory(shareHistoryInfo.getId(), userType); 492 updateShareHistory(shareHistoryInfo.getId(), userType);
459 } 493 }
  494 +
  495 + /**
  496 + * 领取券
  497 + * @param uid
  498 + * @param userType
  499 + * @return
  500 + */
  501 + private OrderShareCouponVO drawCoupon(int uid, int userType) {
  502 + log.info("draw orderShare coupon: uid: {}, userType: {}", uid, userType);
  503 + OrderShareCouponVO couponVO = new OrderShareCouponVO();
  504 +
  505 + //新人券、老人全不一样
  506 + OrderShareCoupon coupon = null;
  507 + if(userType == Constant.ORDER_SHARE_USERTYPE_OLD) {
  508 + coupon = orderShareCouponDAO.selectCouponByDateAndType(DateUtils.getToday("yyyy-MM-dd"), Constant.ORDER_SHARE_USERTYPE_OLD);
  509 + }
  510 + else if(userType == Constant.ORDER_SHARE_USERTYPE_NEW) {
  511 + coupon = orderShareCouponDAO.selectCouponByDateAndType(null, Constant.ORDER_SHARE_USERTYPE_NEW);
  512 + }
  513 +
  514 + if(coupon == null) {
  515 + log.error("no orderShare coupon for user, uid: {}, userType: {}", uid, userType);
  516 + return couponVO;
  517 + }
  518 + couponVO.setCouponId(coupon.getCouponId());
  519 + couponVO.setCouponName(coupon.getCouponName());
  520 +
  521 + //发送优惠券
  522 + log.info("orderShare sendCoupon, uid: {}, couponId: {}", uid, coupon.getCouponId());
  523 + String couponCode = sendCoupon(coupon.getCouponId(), String.valueOf(uid));
  524 + if(StringUtils.isEmpty(couponCode)) {
  525 + log.error("sendCoupon failed, uid: {}, couponId: {}", uid, coupon.getCouponId());
  526 + return couponVO;
  527 + }
  528 + couponVO.setCouponCode(couponCode);
  529 + couponVO.setDrawCouponSuccess(true); //领券成功
  530 + log.info("draw orderShare coupon result: uid: {}, coupon code: {}", uid, couponCode);
  531 + return couponVO;
  532 + }
  533 +
460 } 534 }