From fabb594fd9be1d5301d1c97a8135103568ccc7a2 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Tue, 26 Jul 2022 18:39:40 +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 --- .../com/bweb/controller/CommonController.java | 16 ++++ .../HighDiscountPackageController.java | 75 ++++++++++++++++--- .../excelListener/ImportGiveAwayListener.java | 71 ++++++++++++++++++ .../com/bweb/model/ImportGiveAwayModel.java | 64 ++++++++++++++++ .../service/HighDiscountPackageService.java | 6 ++ .../impl/HighDiscountPackageServiceImpl.java | 68 +++++++++++++++++ 6 files changed, 289 insertions(+), 11 deletions(-) create mode 100644 hai-bweb/src/main/java/com/bweb/excelListener/ImportGiveAwayListener.java create mode 100644 hai-bweb/src/main/java/com/bweb/model/ImportGiveAwayModel.java diff --git a/hai-bweb/src/main/java/com/bweb/controller/CommonController.java b/hai-bweb/src/main/java/com/bweb/controller/CommonController.java index 99f7aa39..979370b4 100644 --- a/hai-bweb/src/main/java/com/bweb/controller/CommonController.java +++ b/hai-bweb/src/main/java/com/bweb/controller/CommonController.java @@ -5,6 +5,7 @@ import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.utils.DateUtil; +import com.hai.common.utils.RedisUtil; import com.hai.common.utils.ResponseMsgUtil; import com.hai.config.TuanYouConfig; import com.hai.entity.SecConfig; @@ -41,6 +42,21 @@ public class CommonController { @Resource private SecConfigService secConfigService; + @Resource + private RedisUtil redisUtil; + + @RequestMapping(value="/getRedisValueByType",method= RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "查询redis中的value") + public ResponseData getRedisValueByType(@RequestParam(name = "key", required = true) String key){ + try { + + return ResponseMsgUtil.success(redisUtil.get(key)); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } @RequestMapping(value="/getProvinceList",method= RequestMethod.GET) @ResponseBody diff --git a/hai-bweb/src/main/java/com/bweb/controller/HighDiscountPackageController.java b/hai-bweb/src/main/java/com/bweb/controller/HighDiscountPackageController.java index 67f476a4..e3467158 100644 --- a/hai-bweb/src/main/java/com/bweb/controller/HighDiscountPackageController.java +++ b/hai-bweb/src/main/java/com/bweb/controller/HighDiscountPackageController.java @@ -1,6 +1,14 @@ package com.bweb.controller; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.fastjson.JSONObject; +import com.bweb.config.SysConfig; +import com.bweb.config.SysConst; +import com.bweb.excelListener.ImportGiveAwayListener; +import com.bweb.excelListener.ImportStoreListener; +import com.bweb.model.ImportGiveAwayModel; +import com.bweb.model.ImportStoreModel; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.hai.common.exception.ErrorCode; @@ -8,6 +16,7 @@ import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.security.SessionObject; import com.hai.common.security.UserCenter; +import com.hai.common.utils.MemberValidateUtil; import com.hai.common.utils.ResponseMsgUtil; import com.hai.entity.*; import com.hai.model.ResponseData; @@ -22,13 +31,14 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.*; /** * @Author Sum1Dream @@ -46,6 +56,9 @@ public class HighDiscountPackageController { @Autowired private UserCenter userCenter; + @Resource + private HighUserService userService; + @Resource private HighDiscountPackageService highDiscountPackageService; @@ -623,23 +636,55 @@ public class HighDiscountPackageController { } } - @RequestMapping(value = "getPhoneListByParseExcel", method = RequestMethod.POST) + @RequestMapping(value = "/getPhoneListByFileUrl", method = RequestMethod.POST) @ResponseBody - @ApiOperation(value = "解析Excel获取手机号列表") - public ResponseData getPhoneListByParseExcel(@RequestBody JSONObject body) { + @ApiOperation(value = "解析Excel文件,获取手机号列表") + public ResponseData getPhoneListByFileUrl(@RequestBody JSONObject body) { try { + if (body == null || StringUtils.isBlank(body.getString("fileUrl"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + + String fileUrl = SysConst.getSysConfig().getFileUrl() + File.separator + body.getString("fileUrl"); + Map param = new HashMap<>(); + param.put("status", "1"); + ImportGiveAwayListener giveAwayListener = new ImportGiveAwayListener(); + giveAwayListener.initData(userService.getListUser(param)); + EasyExcel.read(fileUrl, ImportGiveAwayModel.class, giveAwayListener).sheet().doRead(); - return ResponseMsgUtil.success("赠送成功"); + Map map = new HashMap<>(); + map.put("data", giveAwayListener.dataList); + return ResponseMsgUtil.success(map); + } catch (ExcelAnalysisException e) { + log.error("HighDiscountPackageController --> getPhoneListByParseExcel() error!", e); + return ResponseMsgUtil.builderResponse(ErrorCode.COMMON_ERROR.getCode(), "文件地址错误", null); } catch (Exception e) { log.error("HighDiscountPackageController --> getPhoneListByParseExcel() error!", e); return ResponseMsgUtil.exception(e); } } - @RequestMapping(value = "giveAway", method = RequestMethod.POST) + @RequestMapping(value = "/getPhoneRegisterStatus", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "查询手机号注册状态") + public ResponseData getPhoneRegisterStatus(@RequestBody JSONObject body) { + try { + if (body == null || StringUtils.isBlank(body.getString("phone"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + + return ResponseMsgUtil.success(userService.findByPhone(body.getString("phone")) != null?true:false); + + } catch (Exception e) { + log.error("HighDiscountPackageController --> getPhoneRegisterStatus() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/giveAway", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "赠送") public ResponseData giveAway(@RequestBody JSONObject body) { @@ -657,15 +702,23 @@ public class HighDiscountPackageController { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "没有需要赠送的用户"); } + List phoneList = new ArrayList<>(); + for (Object o : body.getJSONArray("phoneArray")) { - String phone = (String) o; + JSONObject phone = (JSONObject) o; + if (StringUtils.isNotBlank(phone.getString("phone")) && MemberValidateUtil.validatePhone(phone.getString("phone"))) { + if (phoneList.stream().filter(p -> p.equals(phone)).count() == 0) { + phoneList.add(phone.getString("phone")); + } + } } - return ResponseMsgUtil.success("赠送成功"); + return ResponseMsgUtil.success(highDiscountPackageService.giveAway(body.getLong("packageId"), phoneList)); } catch (Exception e) { log.error("HighDiscountPackageController --> giveAway() error!", e); return ResponseMsgUtil.exception(e); } } + } diff --git a/hai-bweb/src/main/java/com/bweb/excelListener/ImportGiveAwayListener.java b/hai-bweb/src/main/java/com/bweb/excelListener/ImportGiveAwayListener.java new file mode 100644 index 00000000..81d436cf --- /dev/null +++ b/hai-bweb/src/main/java/com/bweb/excelListener/ImportGiveAwayListener.java @@ -0,0 +1,71 @@ +package com.bweb.excelListener; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.bweb.model.ImportGiveAwayModel; +import com.hai.common.utils.MemberValidateUtil; +import com.hai.entity.*; +import com.hai.model.HighCouponHandselModel; +import org.apache.commons.lang3.StringUtils; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * + */ +public class ImportGiveAwayListener extends AnalysisEventListener { + + // 数据 + private Integer serialNum = 1; + public List dataList = new ArrayList<>(); + public List userList; + + /** + * @Author 胡锐 + * @Description 解析到的每一条数据都会调用此方法 + * @Date 2021/3/20 21:08 + **/ + @Override + public void invoke(ImportGiveAwayModel giveAwayModel, AnalysisContext analysisContext) { + giveAwayModel.setSerialNum(serialNum); + serialNum++; + + // 校验账户手机号 + if (StringUtils.isNotBlank(giveAwayModel.getPhone())) { + if (!MemberValidateUtil.validatePhone(giveAwayModel.getPhone())) { + giveAwayModel.setRegisterStatus(false); + giveAwayModel.setErrorStatus(true); + giveAwayModel.setErrorMsg("手机号格式错误"); + dataList.add(giveAwayModel); + return; + } + // 查询是否注册 + long count = userList.stream().filter(o -> o.getPhone().equals(giveAwayModel.getPhone())).count(); + giveAwayModel.setRegisterStatus(count == 0?false:true); + + if (this.dataList.stream().filter(o -> o.getPhone().equals(giveAwayModel.getPhone())).count() >= 1) { + giveAwayModel.setErrorStatus(true); + giveAwayModel.setErrorMsg("手机号重复增加"); + dataList.add(giveAwayModel); + return; + } + giveAwayModel.setErrorStatus(false); + dataList.add(giveAwayModel); + } + } + + /** + * @Author 胡锐 + * @Description 所有数据解析完成了 都会来调用此方法 + * @Date 2021/3/20 21:08 + **/ + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + this.dataList = dataList.stream().sorted(Comparator.comparing(ImportGiveAwayModel::getErrorStatus).reversed()).collect(Collectors.toList()); + } + + public void initData(List userList) { + this.userList = userList; + } +} diff --git a/hai-bweb/src/main/java/com/bweb/model/ImportGiveAwayModel.java b/hai-bweb/src/main/java/com/bweb/model/ImportGiveAwayModel.java new file mode 100644 index 00000000..3cb2fbf1 --- /dev/null +++ b/hai-bweb/src/main/java/com/bweb/model/ImportGiveAwayModel.java @@ -0,0 +1,64 @@ +package com.bweb.model; + +import com.alibaba.excel.annotation.ExcelProperty; + +/** + * 导入优惠卷包赠送用户模型 + */ +public class ImportGiveAwayModel { + + @ExcelProperty("用户手机号") + private String phone; + + // 数据序号 + private Integer serialNum; + + // 注册状态 true:已注册 false: 未注册 + private Boolean registerStatus; + + // 错误状态 true: 有错误 false:无错误 + private Boolean errorStatus; + + // 错误消息 + private String errorMsg; + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public Integer getSerialNum() { + return serialNum; + } + + public void setSerialNum(Integer serialNum) { + this.serialNum = serialNum; + } + + public Boolean getRegisterStatus() { + return registerStatus; + } + + public void setRegisterStatus(Boolean registerStatus) { + this.registerStatus = registerStatus; + } + + public Boolean getErrorStatus() { + return errorStatus; + } + + public void setErrorStatus(Boolean errorStatus) { + this.errorStatus = errorStatus; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } +} diff --git a/hai-service/src/main/java/com/hai/service/HighDiscountPackageService.java b/hai-service/src/main/java/com/hai/service/HighDiscountPackageService.java index 02597108..a68c8d07 100644 --- a/hai-service/src/main/java/com/hai/service/HighDiscountPackageService.java +++ b/hai-service/src/main/java/com/hai/service/HighDiscountPackageService.java @@ -99,5 +99,11 @@ public interface HighDiscountPackageService { */ void freeUserDiscountPackage(Map map) throws Exception; + /** + * 赠送优惠券包 + * @param packageId + * @param phoneList + */ + String giveAway(Long packageId, List phoneList) throws Exception; } diff --git a/hai-service/src/main/java/com/hai/service/impl/HighDiscountPackageServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighDiscountPackageServiceImpl.java index c82fc1a2..f1b3286a 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighDiscountPackageServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighDiscountPackageServiceImpl.java @@ -12,6 +12,7 @@ import com.hai.entity.*; import com.hai.model.UserInfoModel; import com.hai.service.*; import com.hai.service.HighDiscountPackageService; +import io.lettuce.core.RedisURI; import org.apache.commons.collections4.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,7 +23,9 @@ import org.springframework.util.StopWatch; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.math.BigDecimal; import java.util.*; +import java.util.stream.Collectors; /** * @serviceName HighDiscountPackageServiceImpl.java @@ -326,4 +329,69 @@ public class HighDiscountPackageServiceImpl implements HighDiscountPackageServic discountPackageRecord.setCreatedTime(new Date()); highDiscountPackageRecordMapper.insert(discountPackageRecord); } + + @Override + @Transactional(propagation= Propagation.REQUIRES_NEW) + public String giveAway(Long packageId, List phoneList) throws Exception { + // 查询优惠券包 + HighDiscountPackage discountPackage = findDiscountPackageById(packageId.intValue()); + if (discountPackage == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "没有找到券包信息"); + } + if (!discountPackage.getStatus().equals(1)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "券包状态错误"); + } + // 查询卷包库存 + Long stockCount = discountPackageActualService.getDiscountPackageNum(packageId.intValue()); + if (phoneList.size() > stockCount.intValue()) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "卷包库存不足"); + } + // 查询出来系统中的用户 + Map userMap = new HashMap<>(); + userMap.put("status", "1"); + List listUser = highUserService.getListUser(userMap); + + String serialNum = System.currentTimeMillis()+""; + + new Thread(() -> { + int alreadyProcessedNum = 0; + for (String phone : phoneList) { + try { + // 根据手机号查询用户 + List userList = listUser.stream().filter(p -> p.getPhone().equals(phone)).collect(Collectors.toList()); + if (userList.size() > 0) { + Map freeMap = new HashMap<>(); + freeMap.put("discountPackageId", packageId); + freeMap.put("userId", userList.get(0).getId()); + freeMap.put("userPhone", userList.get(0).getPhone()); + freeUserDiscountPackage(freeMap); + } else { + // 注册用户信息 + HighUser user = new HighUser(); + user.setName("用户" + IDGenerator.nextId(5)); + user.setPhone(phone); + user.setRegTime(new Date()); + user.setGold(0); + user.setStatus(1); + user.setInfoCompleteStatus(1); // 用户信息完整状态 0:完整 1:需要填写用户信息 2:需要填写手机号 + highUserService.insertUser(user); + + // 赠送卷包 + Map freeMap = new HashMap<>(); + freeMap.put("discountPackageId", packageId); + freeMap.put("userId", user.getId()); + freeMap.put("userPhone", user.getPhone()); + freeUserDiscountPackage(freeMap); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + alreadyProcessedNum++; + redisUtil.set(serialNum, alreadyProcessedNum); + } + } + redisUtil.del(serialNum); + }).start(); + return serialNum; + } }