From 14704881a758b1b8c9a2c69e6b2e0494f139e790 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Sat, 9 Jul 2022 11:43:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HighDiscountAgentRelController.java | 20 +++++ .../controller/HighDiscountController.java | 2 + .../HighUserDiscountController.java | 65 +++++++++++++++-- .../java/com/hai/dao/HighDiscountMapper.java | 29 ++++---- .../com/hai/dao/HighDiscountSqlProvider.java | 14 ++++ .../hai/dao/HighDiscountUserRelMapperExt.java | 4 + .../java/com/hai/entity/HighDiscount.java | 16 ++++ .../com/hai/entity/HighDiscountExample.java | 60 +++++++++++++++ .../com/hai/service/HighDiscountService.java | 7 ++ .../service/HighDiscountUserRelService.java | 16 +++- .../service/impl/HighDiscountServiceImpl.java | 11 +++ .../impl/HighDiscountUserRelServiceImpl.java | 73 ++++++++++++++++++- 12 files changed, 297 insertions(+), 20 deletions(-) diff --git a/hai-bweb/src/main/java/com/bweb/controller/HighDiscountAgentRelController.java b/hai-bweb/src/main/java/com/bweb/controller/HighDiscountAgentRelController.java index 02745192..1044f0b5 100644 --- a/hai-bweb/src/main/java/com/bweb/controller/HighDiscountAgentRelController.java +++ b/hai-bweb/src/main/java/com/bweb/controller/HighDiscountAgentRelController.java @@ -16,6 +16,7 @@ import com.hai.common.security.UserCenter; import com.hai.common.utils.DateUtil; import com.hai.common.utils.IDGenerator; import com.hai.common.utils.ResponseMsgUtil; +import com.hai.config.CommonSysConst; import com.hai.entity.*; import com.hai.enum_type.QrCodeType; import com.hai.model.ResponseData; @@ -31,6 +32,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.net.URLEncoder; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -139,6 +141,24 @@ public class HighDiscountAgentRelController { } } + @RequestMapping(value="/getReceiveLink",method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "获取领取链接") + public ResponseData getReceiveLink(@RequestParam(name = "id", required = true) Long id) { + try { + + String param = URLEncoder.encode(AESEncodeUtil.aesEncrypt(id.toString()), "UTF-8"); + + String url = CommonSysConst.getSysConfig().getHsgDomainName() + "?p=" + param; + + return ResponseMsgUtil.success(url); + + } catch (Exception e) { + log.error("HighDiscountController -> getReceiveLink() error!",e); + return ResponseMsgUtil.exception(e); + } + } + @RequestMapping(value="/getDiscountAgentList",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "查询列表") diff --git a/hai-bweb/src/main/java/com/bweb/controller/HighDiscountController.java b/hai-bweb/src/main/java/com/bweb/controller/HighDiscountController.java index 81e5744c..c7efd61b 100644 --- a/hai-bweb/src/main/java/com/bweb/controller/HighDiscountController.java +++ b/hai-bweb/src/main/java/com/bweb/controller/HighDiscountController.java @@ -63,6 +63,7 @@ public class HighDiscountController { || highDiscount.getDiscountType() == null || highDiscount.getDiscountPrice() == null || highDiscount.getEffectiveDay() == null + || highDiscount.getReceiveNumber() == null || highDiscount.getUseScope() == null || highDiscount.getSalesEndTime() == null) { log.error("HighDiscountController -> insertDiscount() error!","参数错误"); @@ -113,6 +114,7 @@ public class HighDiscountController { if (highDiscount.getId() == null || StringUtils.isBlank(highDiscount.getDiscountName()) || StringUtils.isBlank(highDiscount.getDiscountImg()) + || highDiscount.getReceiveNumber() == null || highDiscount.getEffectiveDay() == null || highDiscount.getSalesEndTime() == null) { log.error("HighDiscountController -> updateDiscount() error!","参数错误"); diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserDiscountController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserDiscountController.java index 3e36638f..6fde72c8 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserDiscountController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserDiscountController.java @@ -6,18 +6,17 @@ import com.github.pagehelper.PageInfo; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; +import com.hai.common.security.AESEncodeUtil; import com.hai.common.security.SessionObject; import com.hai.common.security.UserCenter; import com.hai.common.utils.ResponseMsgUtil; import com.hai.entity.*; import com.hai.model.HighUserModel; import com.hai.model.ResponseData; -import com.hai.service.HighDiscountAgentCodeService; -import com.hai.service.HighDiscountCouponRelService; -import com.hai.service.HighDiscountUserRelService; -import com.hai.service.HighOpenApiService; +import com.hai.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -27,6 +26,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.net.URLDecoder; import java.util.*; import java.util.stream.Collectors; @@ -45,6 +45,12 @@ public class HighUserDiscountController { @Autowired private UserCenter userCenter; + @Resource + private HighDiscountService discountService; + + @Resource + private HighDiscountAgentRelService discountAgentRelService; + @Resource private HighDiscountUserRelService highDiscountUserRelService; @@ -56,7 +62,7 @@ public class HighUserDiscountController { @RequestMapping(value="/receiveDiscount",method = RequestMethod.POST) @ResponseBody - @ApiOperation(value = "用户领取优惠券") + @ApiOperation(value = "用户领取优惠券码【领取优惠码值】") public ResponseData receiveDiscount(@RequestBody String reqBody, HttpServletRequest request) { try { @@ -70,6 +76,7 @@ public class HighUserDiscountController { log.error("HighDiscountController -> receiveDiscount() error!", "参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } + // 领取 highDiscountUserRelService.receiveDiscount(userInfoModel.getHighUser().getId(), codeId); return ResponseMsgUtil.success("领取成功"); @@ -84,6 +91,54 @@ public class HighUserDiscountController { } } + @RequestMapping(value="/receive",method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "用户领取优惠券【领取优惠】") + public ResponseData receive(@RequestBody String reqBody, HttpServletRequest request) { + try { + + // 用户 + SessionObject sessionObject = userCenter.getSessionObject(request); + HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); + + JSONObject jsonObject = JSONObject.parseObject(reqBody); + String discountAgentCode = jsonObject.getString("code"); + if (StringUtils.isNotBlank(discountAgentCode)) { + log.error("HighDiscountController -> receiveDiscount() error!", "参数错误"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + + // 解密 + String discountAgentId = AESEncodeUtil.aesDecrypt(URLDecoder.decode(discountAgentCode, "UTF-8")); + + HighDiscountAgentRel discountAgentRel = discountAgentRelService.getRelById(Long.parseLong(discountAgentId)); + if (discountAgentRel == null) { + log.error("HighDiscountController -> receiveDiscount() error!", "参数错误"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); + } + // 查询优惠券信息 + HighDiscount discount = discountService.getDiscountById(discountAgentRel.getDiscountId()); + if (discount == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券"); + } + if (!discount.getStatus().equals(2)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期"); + } + + // 领取 + highDiscountUserRelService.receive(userInfoModel.getHighUser().getId(), discountAgentRel.getId()); + return ResponseMsgUtil.success("领取成功"); + + } catch (DeadlockLoserDataAccessException deadlockLoserDataAccessException) { + log.error("HighActivityController -> userLottery() error!", "服务器繁忙"); + return ResponseMsgUtil.builderResponse(ErrorCode.SERVER_BUSY_ERROR.getCode(),ErrorCode.SERVER_BUSY_ERROR.getMsg(),null); + + } catch (Exception e) { + log.error("HighDiscountController -> receiveDiscount() error!",e); + return ResponseMsgUtil.exception(e); + } + } + @RequestMapping(value="/getUserDiscountList",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "获取用户的优惠券列表") diff --git a/hai-service/src/main/java/com/hai/dao/HighDiscountMapper.java b/hai-service/src/main/java/com/hai/dao/HighDiscountMapper.java index ed572474..065d24f5 100644 --- a/hai-service/src/main/java/com/hai/dao/HighDiscountMapper.java +++ b/hai-service/src/main/java/com/hai/dao/HighDiscountMapper.java @@ -44,21 +44,21 @@ public interface HighDiscountMapper extends HighDiscountMapperExt { "discount_name, discount_img, ", "discount_carousel_img, discount_type, ", "discount_condition, discount_price, ", - "effective_day, sales_end_time, ", - "create_time, update_time, ", - "operator_id, operator_name, ", - "`status`, ext_1, ext_2, ", - "ext_3)", + "receive_number, effective_day, ", + "sales_end_time, create_time, ", + "update_time, operator_id, ", + "operator_name, `status`, ", + "ext_1, ext_2, ext_3)", "values (#{companyId,jdbcType=BIGINT}, #{discountKey,jdbcType=VARCHAR}, ", "#{usingRange,jdbcType=INTEGER}, #{useScope,jdbcType=INTEGER}, ", "#{discountName,jdbcType=VARCHAR}, #{discountImg,jdbcType=VARCHAR}, ", "#{discountCarouselImg,jdbcType=VARCHAR}, #{discountType,jdbcType=INTEGER}, ", "#{discountCondition,jdbcType=DECIMAL}, #{discountPrice,jdbcType=DECIMAL}, ", - "#{effectiveDay,jdbcType=INTEGER}, #{salesEndTime,jdbcType=TIMESTAMP}, ", - "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ", - "#{operatorId,jdbcType=BIGINT}, #{operatorName,jdbcType=VARCHAR}, ", - "#{status,jdbcType=INTEGER}, #{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR}, ", - "#{ext3,jdbcType=VARCHAR})" + "#{receiveNumber,jdbcType=INTEGER}, #{effectiveDay,jdbcType=INTEGER}, ", + "#{salesEndTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}, ", + "#{updateTime,jdbcType=TIMESTAMP}, #{operatorId,jdbcType=BIGINT}, ", + "#{operatorName,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, ", + "#{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR}, #{ext3,jdbcType=VARCHAR})" }) @Options(useGeneratedKeys=true,keyProperty="id") int insert(HighDiscount record); @@ -80,6 +80,7 @@ public interface HighDiscountMapper extends HighDiscountMapperExt { @Result(column="discount_type", property="discountType", jdbcType=JdbcType.INTEGER), @Result(column="discount_condition", property="discountCondition", jdbcType=JdbcType.DECIMAL), @Result(column="discount_price", property="discountPrice", jdbcType=JdbcType.DECIMAL), + @Result(column="receive_number", property="receiveNumber", jdbcType=JdbcType.INTEGER), @Result(column="effective_day", property="effectiveDay", jdbcType=JdbcType.INTEGER), @Result(column="sales_end_time", property="salesEndTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP), @@ -96,9 +97,9 @@ public interface HighDiscountMapper extends HighDiscountMapperExt { @Select({ "select", "id, company_id, discount_key, using_range, use_scope, discount_name, discount_img, ", - "discount_carousel_img, discount_type, discount_condition, discount_price, effective_day, ", - "sales_end_time, create_time, update_time, operator_id, operator_name, `status`, ", - "ext_1, ext_2, ext_3", + "discount_carousel_img, discount_type, discount_condition, discount_price, receive_number, ", + "effective_day, sales_end_time, create_time, update_time, operator_id, operator_name, ", + "`status`, ext_1, ext_2, ext_3", "from high_discount", "where id = #{id,jdbcType=BIGINT}" }) @@ -114,6 +115,7 @@ public interface HighDiscountMapper extends HighDiscountMapperExt { @Result(column="discount_type", property="discountType", jdbcType=JdbcType.INTEGER), @Result(column="discount_condition", property="discountCondition", jdbcType=JdbcType.DECIMAL), @Result(column="discount_price", property="discountPrice", jdbcType=JdbcType.DECIMAL), + @Result(column="receive_number", property="receiveNumber", jdbcType=JdbcType.INTEGER), @Result(column="effective_day", property="effectiveDay", jdbcType=JdbcType.INTEGER), @Result(column="sales_end_time", property="salesEndTime", jdbcType=JdbcType.TIMESTAMP), @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP), @@ -148,6 +150,7 @@ public interface HighDiscountMapper extends HighDiscountMapperExt { "discount_type = #{discountType,jdbcType=INTEGER},", "discount_condition = #{discountCondition,jdbcType=DECIMAL},", "discount_price = #{discountPrice,jdbcType=DECIMAL},", + "receive_number = #{receiveNumber,jdbcType=INTEGER},", "effective_day = #{effectiveDay,jdbcType=INTEGER},", "sales_end_time = #{salesEndTime,jdbcType=TIMESTAMP},", "create_time = #{createTime,jdbcType=TIMESTAMP},", diff --git a/hai-service/src/main/java/com/hai/dao/HighDiscountSqlProvider.java b/hai-service/src/main/java/com/hai/dao/HighDiscountSqlProvider.java index 2fb12c2b..523d6f76 100644 --- a/hai-service/src/main/java/com/hai/dao/HighDiscountSqlProvider.java +++ b/hai-service/src/main/java/com/hai/dao/HighDiscountSqlProvider.java @@ -68,6 +68,10 @@ public class HighDiscountSqlProvider { sql.VALUES("discount_price", "#{discountPrice,jdbcType=DECIMAL}"); } + if (record.getReceiveNumber() != null) { + sql.VALUES("receive_number", "#{receiveNumber,jdbcType=INTEGER}"); + } + if (record.getEffectiveDay() != null) { sql.VALUES("effective_day", "#{effectiveDay,jdbcType=INTEGER}"); } @@ -128,6 +132,7 @@ public class HighDiscountSqlProvider { sql.SELECT("discount_type"); sql.SELECT("discount_condition"); sql.SELECT("discount_price"); + sql.SELECT("receive_number"); sql.SELECT("effective_day"); sql.SELECT("sales_end_time"); sql.SELECT("create_time"); @@ -199,6 +204,10 @@ public class HighDiscountSqlProvider { sql.SET("discount_price = #{record.discountPrice,jdbcType=DECIMAL}"); } + if (record.getReceiveNumber() != null) { + sql.SET("receive_number = #{record.receiveNumber,jdbcType=INTEGER}"); + } + if (record.getEffectiveDay() != null) { sql.SET("effective_day = #{record.effectiveDay,jdbcType=INTEGER}"); } @@ -258,6 +267,7 @@ public class HighDiscountSqlProvider { sql.SET("discount_type = #{record.discountType,jdbcType=INTEGER}"); sql.SET("discount_condition = #{record.discountCondition,jdbcType=DECIMAL}"); sql.SET("discount_price = #{record.discountPrice,jdbcType=DECIMAL}"); + sql.SET("receive_number = #{record.receiveNumber,jdbcType=INTEGER}"); sql.SET("effective_day = #{record.effectiveDay,jdbcType=INTEGER}"); sql.SET("sales_end_time = #{record.salesEndTime,jdbcType=TIMESTAMP}"); sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}"); @@ -318,6 +328,10 @@ public class HighDiscountSqlProvider { sql.SET("discount_price = #{discountPrice,jdbcType=DECIMAL}"); } + if (record.getReceiveNumber() != null) { + sql.SET("receive_number = #{receiveNumber,jdbcType=INTEGER}"); + } + if (record.getEffectiveDay() != null) { sql.SET("effective_day = #{effectiveDay,jdbcType=INTEGER}"); } diff --git a/hai-service/src/main/java/com/hai/dao/HighDiscountUserRelMapperExt.java b/hai-service/src/main/java/com/hai/dao/HighDiscountUserRelMapperExt.java index bb221edb..4e81ee5d 100644 --- a/hai-service/src/main/java/com/hai/dao/HighDiscountUserRelMapperExt.java +++ b/hai-service/src/main/java/com/hai/dao/HighDiscountUserRelMapperExt.java @@ -39,4 +39,8 @@ public interface HighDiscountUserRelMapperExt { " ORDER BY hdur.create_time desc" + ""}) List getUserCouponsList(@Param("map") Map map); + + + @Select({" 100 ]]> "}) + Integer receiveDiscountCount(@Param("userId") Long userId, @Param("discountId") Long discountId); } diff --git a/hai-service/src/main/java/com/hai/entity/HighDiscount.java b/hai-service/src/main/java/com/hai/entity/HighDiscount.java index 24986d9c..b577a2e5 100644 --- a/hai-service/src/main/java/com/hai/entity/HighDiscount.java +++ b/hai-service/src/main/java/com/hai/entity/HighDiscount.java @@ -69,6 +69,11 @@ public class HighDiscount implements Serializable { */ private BigDecimal discountPrice; + /** + * 领取次数 + */ + private Integer receiveNumber; + /** * 领取有效天数 */ @@ -200,6 +205,14 @@ public class HighDiscount implements Serializable { this.discountPrice = discountPrice; } + public Integer getReceiveNumber() { + return receiveNumber; + } + + public void setReceiveNumber(Integer receiveNumber) { + this.receiveNumber = receiveNumber; + } + public Integer getEffectiveDay() { return effectiveDay; } @@ -303,6 +316,7 @@ public class HighDiscount implements Serializable { && (this.getDiscountType() == null ? other.getDiscountType() == null : this.getDiscountType().equals(other.getDiscountType())) && (this.getDiscountCondition() == null ? other.getDiscountCondition() == null : this.getDiscountCondition().equals(other.getDiscountCondition())) && (this.getDiscountPrice() == null ? other.getDiscountPrice() == null : this.getDiscountPrice().equals(other.getDiscountPrice())) + && (this.getReceiveNumber() == null ? other.getReceiveNumber() == null : this.getReceiveNumber().equals(other.getReceiveNumber())) && (this.getEffectiveDay() == null ? other.getEffectiveDay() == null : this.getEffectiveDay().equals(other.getEffectiveDay())) && (this.getSalesEndTime() == null ? other.getSalesEndTime() == null : this.getSalesEndTime().equals(other.getSalesEndTime())) && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) @@ -330,6 +344,7 @@ public class HighDiscount implements Serializable { result = prime * result + ((getDiscountType() == null) ? 0 : getDiscountType().hashCode()); result = prime * result + ((getDiscountCondition() == null) ? 0 : getDiscountCondition().hashCode()); result = prime * result + ((getDiscountPrice() == null) ? 0 : getDiscountPrice().hashCode()); + result = prime * result + ((getReceiveNumber() == null) ? 0 : getReceiveNumber().hashCode()); result = prime * result + ((getEffectiveDay() == null) ? 0 : getEffectiveDay().hashCode()); result = prime * result + ((getSalesEndTime() == null) ? 0 : getSalesEndTime().hashCode()); result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); @@ -360,6 +375,7 @@ public class HighDiscount implements Serializable { sb.append(", discountType=").append(discountType); sb.append(", discountCondition=").append(discountCondition); sb.append(", discountPrice=").append(discountPrice); + sb.append(", receiveNumber=").append(receiveNumber); sb.append(", effectiveDay=").append(effectiveDay); sb.append(", salesEndTime=").append(salesEndTime); sb.append(", createTime=").append(createTime); diff --git a/hai-service/src/main/java/com/hai/entity/HighDiscountExample.java b/hai-service/src/main/java/com/hai/entity/HighDiscountExample.java index 7e6bca34..f5708b9d 100644 --- a/hai-service/src/main/java/com/hai/entity/HighDiscountExample.java +++ b/hai-service/src/main/java/com/hai/entity/HighDiscountExample.java @@ -826,6 +826,66 @@ public class HighDiscountExample { return (Criteria) this; } + public Criteria andReceiveNumberIsNull() { + addCriterion("receive_number is null"); + return (Criteria) this; + } + + public Criteria andReceiveNumberIsNotNull() { + addCriterion("receive_number is not null"); + return (Criteria) this; + } + + public Criteria andReceiveNumberEqualTo(Integer value) { + addCriterion("receive_number =", value, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberNotEqualTo(Integer value) { + addCriterion("receive_number <>", value, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberGreaterThan(Integer value) { + addCriterion("receive_number >", value, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberGreaterThanOrEqualTo(Integer value) { + addCriterion("receive_number >=", value, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberLessThan(Integer value) { + addCriterion("receive_number <", value, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberLessThanOrEqualTo(Integer value) { + addCriterion("receive_number <=", value, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberIn(List values) { + addCriterion("receive_number in", values, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberNotIn(List values) { + addCriterion("receive_number not in", values, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberBetween(Integer value1, Integer value2) { + addCriterion("receive_number between", value1, value2, "receiveNumber"); + return (Criteria) this; + } + + public Criteria andReceiveNumberNotBetween(Integer value1, Integer value2) { + addCriterion("receive_number not between", value1, value2, "receiveNumber"); + return (Criteria) this; + } + public Criteria andEffectiveDayIsNull() { addCriterion("effective_day is null"); return (Criteria) this; diff --git a/hai-service/src/main/java/com/hai/service/HighDiscountService.java b/hai-service/src/main/java/com/hai/service/HighDiscountService.java index 8661c42b..e727f3b3 100644 --- a/hai-service/src/main/java/com/hai/service/HighDiscountService.java +++ b/hai-service/src/main/java/com/hai/service/HighDiscountService.java @@ -33,6 +33,13 @@ public interface HighDiscountService { **/ HighDiscount getDiscountById(Long id); + /** + * 根据优惠券key查询 + * @param discountKey + * @return + */ + HighDiscount getDiscountByKey(String discountKey); + /** * @Author 胡锐 * @Description 查询优惠券列表 diff --git a/hai-service/src/main/java/com/hai/service/HighDiscountUserRelService.java b/hai-service/src/main/java/com/hai/service/HighDiscountUserRelService.java index b3cfb23f..becdd024 100644 --- a/hai-service/src/main/java/com/hai/service/HighDiscountUserRelService.java +++ b/hai-service/src/main/java/com/hai/service/HighDiscountUserRelService.java @@ -15,11 +15,18 @@ public interface HighDiscountUserRelService { /** * @Author 胡锐 - * @Description 领取优惠券 + * @Description 领取优惠券code * @Date 2021/4/4 15:12 **/ void receiveDiscount(Long userId,Long codeId); + /** + * @Author 胡锐 + * @Description 领取优惠券 + * @Date 2021/4/4 15:12 + **/ + void receive(Long userId, Long discountAgentId); + /** * @Author 胡锐 * @Description 修改 @@ -62,5 +69,12 @@ public interface HighDiscountUserRelService { **/ List getExpiredDiscount(); + /** + * 领取优惠券统计 + * @param userId + * @param discountId + * @return + */ + Integer receiveDiscountCount(Long userId,Long discountId); } diff --git a/hai-service/src/main/java/com/hai/service/impl/HighDiscountServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighDiscountServiceImpl.java index 56be673e..949804e8 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighDiscountServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighDiscountServiceImpl.java @@ -39,6 +39,17 @@ public class HighDiscountServiceImpl implements HighDiscountService { return highDiscountMapper.selectByPrimaryKey(id); } + @Override + public HighDiscount getDiscountByKey(String discountKey) { + HighDiscountExample example = new HighDiscountExample(); + example.createCriteria().andDiscountKeyEqualTo(discountKey).andStatusNotEqualTo(0); + List list = highDiscountMapper.selectByExample(example); + if (list.size() > 0) { + return list.get(0); + } + return null; + } + @Override public List getDiscount(Map map) { HighDiscountExample example = new HighDiscountExample(); diff --git a/hai-service/src/main/java/com/hai/service/impl/HighDiscountUserRelServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighDiscountUserRelServiceImpl.java index 282748af..f7b9360c 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighDiscountUserRelServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighDiscountUserRelServiceImpl.java @@ -51,7 +51,6 @@ public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelServic if(code == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二维码不存在"); } - // 状态 0:删除 1:待领取 2:待使用 3:已使用 4:已过期 5: 已分配 if(code.getStatus() != 1 && code.getStatus() != 5) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二维码不存在或已被领取"); @@ -67,6 +66,18 @@ public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelServic rel.setStockCount(rel.getStockCount() - 1); highDiscountAgentRelService.updateDiscountAgentRel(rel); + // 查询优惠券信息 + HighDiscount discount = highDiscountService.getDiscountById(rel.getDiscountId()); + if (discount == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券信息"); + } + if (!discount.getStatus().equals(2)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期"); + } + if (discount.getReceiveNumber() > 0 && this.receiveDiscountCount(userId, discount.getId()) < discount.getReceiveNumber()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券已达到上线"); + } + // 校验卡卷状态 if (rel.getHighDiscount().getStatus() != 2) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券已过期"); @@ -106,6 +117,61 @@ public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelServic } + @Override + @Transactional(propagation= Propagation.REQUIRES_NEW,isolation= Isolation.SERIALIZABLE) + public void receive(Long userId, Long discountAgentId) { + // 查询优惠券与代理商关系 + HighDiscountAgentRel discountAgentRel = highDiscountAgentRelService.getRelById(discountAgentId); + if (discountAgentRel == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); + } + // 查询优惠券信息 + HighDiscount discount = discountAgentRel.getHighDiscount(); + if (discount == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); + } + if (!discount.getStatus().equals(2)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法领取,优惠券活动已过期"); + } + if (discount.getReceiveNumber() > 0 && this.receiveDiscountCount(userId, discount.getId()) < discount.getReceiveNumber()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券已达到上线"); + } + // 查询可分配的优惠券code + List discountAgentCodeList = highDiscountAgentCodeService.getDiscountCodeByDiscountAgentId(discountAgentRel.getId()); + if (discountAgentCodeList.size() == 0) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券已无库存"); + } + HighDiscountAgentCode code = discountAgentCodeList.get(0); + code.setStatus(2); + highDiscountAgentCodeService.updateCode(code); + + // 查询优惠券信息 + discountAgentRel.setStockCount(discountAgentRel.getStockCount() - 1); + highDiscountAgentRelService.updateDiscountAgentRel(discountAgentRel); + + HighDiscountUserRel userRel = new HighDiscountUserRel(); + userRel.setDiscountId(discountAgentRel.getDiscountId()); + userRel.setAgentId(discountAgentRel.getAgentId()); + userRel.setUserId(userId); + userRel.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用 + userRel.setCreateTime(new Date()); + userRel.setDiscountAgentCodeId(code.getId()); + // 计算使用有效期 + Calendar userEndTime = Calendar.getInstance(); + userEndTime.setTime(new Date()); + userEndTime.set(Calendar.HOUR_OF_DAY, 23); + userEndTime.set(Calendar.MINUTE, 59); + userEndTime.set(Calendar.SECOND, 59); + userEndTime.add(Calendar.DATE, discountAgentRel.getHighDiscount().getEffectiveDay()); + + if (userEndTime.getTime().compareTo(discountAgentRel.getHighDiscount().getSalesEndTime()) == 1) { + userRel.setUseEndTime(discountAgentRel.getHighDiscount().getSalesEndTime()); + } else { + userRel.setUseEndTime(userEndTime.getTime()); + } + highDiscountUserRelMapper.insert(userRel); + } + @Override public void updateDiscountUserRel(HighDiscountUserRel highDiscountUserRel) { highDiscountUserRelMapper.updateByPrimaryKey(highDiscountUserRel); @@ -171,4 +237,9 @@ public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelServic example.createCriteria().andStatusEqualTo(1).andUseEndTimeLessThanOrEqualTo(new Date()); return highDiscountUserRelMapper.selectByExample(example); } + + @Override + public Integer receiveDiscountCount(Long userId, Long discountId) { + return highDiscountUserRelMapper.receiveDiscountCount(userId, discountId); + } }