提交代码

dev-discount
胡锐 2 years ago
parent 0a787835a0
commit fabb594fd9
  1. 16
      hai-bweb/src/main/java/com/bweb/controller/CommonController.java
  2. 75
      hai-bweb/src/main/java/com/bweb/controller/HighDiscountPackageController.java
  3. 71
      hai-bweb/src/main/java/com/bweb/excelListener/ImportGiveAwayListener.java
  4. 64
      hai-bweb/src/main/java/com/bweb/model/ImportGiveAwayModel.java
  5. 6
      hai-service/src/main/java/com/hai/service/HighDiscountPackageService.java
  6. 68
      hai-service/src/main/java/com/hai/service/impl/HighDiscountPackageServiceImpl.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

@ -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<String, String> 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<String, Object> 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<String> 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);
}
}
}

@ -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<ImportGiveAwayModel> {
// 数据
private Integer serialNum = 1;
public List<ImportGiveAwayModel> dataList = new ArrayList<>();
public List<HighUser> 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<HighUser> userList) {
this.userList = userList;
}
}

@ -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;
}
}

@ -99,5 +99,11 @@ public interface HighDiscountPackageService {
*/
void freeUserDiscountPackage(Map<String , Object> map) throws Exception;
/**
* 赠送优惠券包
* @param packageId
* @param phoneList
*/
String giveAway(Long packageId, List<String> phoneList) throws Exception;
}

@ -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<String> 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<String, String> userMap = new HashMap<>();
userMap.put("status", "1");
List<HighUser> listUser = highUserService.getListUser(userMap);
String serialNum = System.currentTimeMillis()+"";
new Thread(() -> {
int alreadyProcessedNum = 0;
for (String phone : phoneList) {
try {
// 根据手机号查询用户
List<HighUser> userList = listUser.stream().filter(p -> p.getPhone().equals(phone)).collect(Collectors.toList());
if (userList.size() > 0) {
Map<String, Object> 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<String, Object> 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;
}
}

Loading…
Cancel
Save