diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java b/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java index 51bfafdd..a0287f29 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java @@ -1,12 +1,17 @@ package com.cweb.controller; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.hai.common.Base64Util; 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.utils.ResponseMsgUtil; +import com.hai.entity.HighDiscountAgentCode; +import com.hai.entity.HighDiscountAgentRel; import com.hai.model.ResponseData; +import com.hai.service.HighDiscountAgentCodeService; import com.hai.service.HighDiscountAgentRelService; import com.hai.service.HighDiscountCouponRelService; import com.hai.service.HighDiscountService; @@ -18,6 +23,8 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; /** * @Auther: 胡锐 @@ -37,6 +44,9 @@ public class HighDiscountController { @Resource private HighDiscountCouponRelService highDiscountCouponRelService; + @Resource + private HighDiscountAgentCodeService highDiscountAgentCodeService; + @Resource private HighDiscountService highDiscountService; @@ -45,14 +55,26 @@ public class HighDiscountController { @ApiOperation(value = "根据二维码Code查询") public ResponseData getDiscountByQrCode(@RequestParam(name = "code", required = true) String code) { try { - String relId; + String jsonData; try { - relId = AESEncodeUtil.aesDecrypt(Base64Util.decode(code)); + jsonData = AESEncodeUtil.aesDecrypt(Base64Util.decode(code)); } catch (Exception e) { log.error("HighDiscountController -> getDiscountByQrCode() error!","code解码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "code解码错误"); } - return ResponseMsgUtil.success(highDiscountAgentRelService.getRelById(Long.parseLong(relId))); + + JSONObject jsonObject = JSON.parseObject(jsonData); + String type = jsonObject.getString("type"); + Long id = jsonObject.getLong("id"); + + HighDiscountAgentCode discountAgentCode = highDiscountAgentCodeService.getCodeById(id); + if (discountAgentCode != null) { + HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); + if (rel == null) { + rel.setHighDiscountAgentCode(discountAgentCode); + } + } + return ResponseMsgUtil.success(null); } catch (Exception e) { log.error("HighDiscountController -> getDiscountByQrCode() error!",e); 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 10d2f508..2f142663 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserDiscountController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserDiscountController.java @@ -10,10 +10,12 @@ import com.hai.common.security.SessionObject; import com.hai.common.security.UserCenter; import com.hai.common.utils.ResponseMsgUtil; import com.hai.entity.HighDiscount; +import com.hai.entity.HighDiscountAgentCode; import com.hai.entity.HighDiscountCouponRel; import com.hai.entity.HighDiscountUserRel; 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 io.swagger.annotations.Api; @@ -64,13 +66,13 @@ public class HighUserDiscountController { HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); JSONObject jsonObject = JSONObject.parseObject(reqBody); - Long discountAgentId = jsonObject.getLong("discountAgentId"); - if (discountAgentId == null) { + Long codeId = jsonObject.getLong("codeId"); + if (codeId == null) { log.error("HighDiscountController -> receiveDiscount() error!", "参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } // 领取 - highDiscountUserRelService.receiveDiscount(userInfoModel.getHighUser().getId(), discountAgentId); + highDiscountUserRelService.receiveDiscount(userInfoModel.getHighUser().getId(), codeId); return ResponseMsgUtil.success("领取成功"); } catch (Exception e) { diff --git a/hai-schedule/src/main/java/com/hai/schedule/HighDiscountSchedule.java b/hai-schedule/src/main/java/com/hai/schedule/HighDiscountSchedule.java index 9abb88cf..276af633 100644 --- a/hai-schedule/src/main/java/com/hai/schedule/HighDiscountSchedule.java +++ b/hai-schedule/src/main/java/com/hai/schedule/HighDiscountSchedule.java @@ -1,7 +1,9 @@ package com.hai.schedule; +import com.hai.entity.HighDiscountAgentCode; import com.hai.entity.HighDiscountUserRel; import com.hai.entity.HighOrder; +import com.hai.service.HighDiscountAgentCodeService; import com.hai.service.HighDiscountUserRelService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,6 +26,9 @@ public class HighDiscountSchedule { @Resource private HighDiscountUserRelService highDiscountUserRelService; + @Resource + private HighDiscountAgentCodeService highDiscountAgentCodeService; + /** * @Author 胡锐 * @Description 处理过期的优惠券 @@ -36,6 +41,13 @@ public class HighDiscountSchedule { try { rel.setStatus(0); highDiscountUserRelService.updateDiscountUserRel(rel); + + // 二维码 + HighDiscountAgentCode code = highDiscountAgentCodeService.getCodeById(rel.getDiscountAgentCodeId()); + if (code != null) { + code.setStatus(4); // 状态 0:删除 1:待领取 2:待使用 3:已使用 4:已过期 + highDiscountAgentCodeService.updateCode(code); + } } catch (Exception e) { log.error("HighCouponSchedule --> expiredCoupon() error!", e); } diff --git a/hai-service/src/main/java/com/hai/model/HighDiscountAgentRelModel.java b/hai-service/src/main/java/com/hai/model/HighDiscountAgentRelModel.java index e4909a1b..3bd7e5bf 100644 --- a/hai-service/src/main/java/com/hai/model/HighDiscountAgentRelModel.java +++ b/hai-service/src/main/java/com/hai/model/HighDiscountAgentRelModel.java @@ -2,6 +2,7 @@ package com.hai.model; import com.hai.entity.HighAgent; import com.hai.entity.HighDiscount; +import com.hai.entity.HighDiscountAgentCode; /** * @Auther: 胡锐 @@ -13,6 +14,9 @@ public class HighDiscountAgentRelModel { // 优惠券信息 private HighDiscount highDiscount; + // 优惠券二维码 + private HighDiscountAgentCode highDiscountAgentCode; + // 代理商信息 private HighAgent highAgent; @@ -24,6 +28,14 @@ public class HighDiscountAgentRelModel { this.highDiscount = highDiscount; } + public HighDiscountAgentCode getHighDiscountAgentCode() { + return highDiscountAgentCode; + } + + public void setHighDiscountAgentCode(HighDiscountAgentCode highDiscountAgentCode) { + this.highDiscountAgentCode = highDiscountAgentCode; + } + public HighAgent getHighAgent() { return highAgent; } diff --git a/hai-service/src/main/java/com/hai/service/HighDiscountAgentCodeService.java b/hai-service/src/main/java/com/hai/service/HighDiscountAgentCodeService.java index ec16ac70..09b07f90 100644 --- a/hai-service/src/main/java/com/hai/service/HighDiscountAgentCodeService.java +++ b/hai-service/src/main/java/com/hai/service/HighDiscountAgentCodeService.java @@ -31,4 +31,11 @@ public interface HighDiscountAgentCodeService { * @Date 2021/4/14 22:32 **/ void updateCode(HighDiscountAgentCode highDiscountAgentCode); + + /** + * @Author 胡锐 + * @Description 根据id 查询二维码 + * @Date 2021/4/14 23:38 + **/ + HighDiscountAgentCode getCodeById(Long id); } 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 546267ab..6dc332df 100644 --- a/hai-service/src/main/java/com/hai/service/HighDiscountUserRelService.java +++ b/hai-service/src/main/java/com/hai/service/HighDiscountUserRelService.java @@ -18,7 +18,7 @@ public interface HighDiscountUserRelService { * @Description 领取优惠券 * @Date 2021/4/4 15:12 **/ - void receiveDiscount(Long userId,Long discountAgentId); + void receiveDiscount(Long userId,Long codeId); /** * @Author 胡锐 diff --git a/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java index d82b269b..0bb4c17a 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java @@ -33,4 +33,9 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe public void updateCode(HighDiscountAgentCode highDiscountAgentCode) { highDiscountAgentCodeMapper.updateByPrimaryKey(highDiscountAgentCode); } + + @Override + public HighDiscountAgentCode getCodeById(Long id) { + return highDiscountAgentCodeMapper.selectByPrimaryKey(id); + } } diff --git a/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentRelServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentRelServiceImpl.java index dc29ef8c..7be167eb 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentRelServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentRelServiceImpl.java @@ -58,7 +58,7 @@ public class HighDiscountAgentRelServiceImpl implements HighDiscountAgentRelServ code.setCreateTime(new Date()); highDiscountAgentCodeService.insertCode(code); // 二维码参数 - map.put("id", highDiscountAgentRel.getId()); + map.put("id", code.getId()); // 生成二维码 String param = "https://hsgcs.dctpay.com/wx/?action=gogogo&id=" + Base64Util.encode(AESEncodeUtil.aesEncrypt(JSON.toJSONString(map))); QRCodeGenerator.generateQRCodeImage(param, 350, 350, qrCodeUrl); 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 b6862287..1afcc70d 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 @@ -4,14 +4,8 @@ import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.dao.HighDiscountUserRelMapper; -import com.hai.entity.HighDiscount; -import com.hai.entity.HighDiscountAgentRel; -import com.hai.entity.HighDiscountUserRel; -import com.hai.entity.HighDiscountUserRelExample; -import com.hai.service.HighAgentService; -import com.hai.service.HighDiscountAgentRelService; -import com.hai.service.HighDiscountService; -import com.hai.service.HighDiscountUserRelService; +import com.hai.entity.*; +import com.hai.service.*; import org.apache.commons.collections4.MapUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; @@ -43,12 +37,26 @@ public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelServic @Resource private HighDiscountService highDiscountService; + @Resource + private HighDiscountAgentCodeService highDiscountAgentCodeService; + @Override @Transactional(propagation= Propagation.REQUIRES_NEW) - public void receiveDiscount(Long userId, Long discountAgentId) { + public void receiveDiscount(Long userId, Long codeId) { + + // 查询优惠券二维码 + HighDiscountAgentCode code = highDiscountAgentCodeService.getCodeById(codeId); + if(code == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二维码不存在"); + } + + // 状态 0:删除 1:待领取 2:待使用 3:已使用 4:已过期 + if(code.getStatus() != 1) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "二维码不存在或已被领取"); + } // 查询优惠券信息 - HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentId); + HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(code.getDiscountAgentId()); if (rel == null || rel.getHighDiscount() == null || rel.getAgentId() == null){ throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_DISCOUNT, ""); } @@ -76,6 +84,7 @@ public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelServic 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());