'完成卡卷导入、卡券生成'

dev-discount
199901012 4 years ago
parent 3cf248f835
commit b1238b9ce5
  1. 59
      hai-bweb/src/main/java/com/bweb/controller/HighCouponController.java
  2. 112
      hai-bweb/src/main/java/com/bweb/excelListener/ImportCouponListener.java
  3. 57
      hai-bweb/src/main/java/com/bweb/model/ImportCouponModel.java
  4. 34
      hai-bweb/src/test/common/DemoDataListener.java
  5. 33
      hai-bweb/src/test/common/ExcelModel.java
  6. 44
      hai-bweb/src/test/common/ExcelTest.java
  7. 4
      hai-bweb/src/test/common/RegionTest.java
  8. 11
      hai-service/pom.xml
  9. 8
      hai-service/src/main/java/com/hai/service/HighCouponCodeService.java
  10. 10
      hai-service/src/main/java/com/hai/service/impl/HighCouponCodeServiceImpl.java

@ -1,6 +1,9 @@
package com.bweb.controller; package com.bweb.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.bweb.excelListener.ImportCouponListener;
import com.bweb.model.ImportCouponModel;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorCode;
@ -29,6 +32,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -300,17 +304,42 @@ public class HighCouponController {
} }
@RequestMapping(value="/importStock",method = RequestMethod.GET) @RequestMapping(value="/importStock",method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "导入卡卷库存(外部卷)") @ApiOperation(value = "导入卡卷库存(外部卷)")
public ResponseData importStock(@RequestParam(name = "couponId", required = true) Long couponId) { public ResponseData importStock(@RequestParam(name = "couponId", required = true) Long couponId,
@RequestParam(value = "files" , required = true) MultipartFile files,
HttpServletRequest request) {
try { try {
Map<String, Object> map = new HashMap<>(); SessionObject sessionObject = userCenter.getSessionObject(request);
UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
// 查找商户
HighCouponModel coupon = highCouponService.getCouponById(couponId);
if (coupon == null) {
log.error("HighCouponController -> writeStock() error!","未找到商户");
throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOF_FOUND, "");
}
if (coupon.getCouponType() != 2) {
log.error("HighCouponController -> writeStock() error!","卡卷类型错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_TYPE_ERROR, "");
}
if (coupon.getStatus() != 1) {
log.error("HighCouponController -> writeStock() error!","卡卷状态错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STATUS_ERROR, "");
}
return ResponseMsgUtil.success(new PageInfo<>(highCouponService.getCouponList(map))); ImportCouponListener importCouponListener = new ImportCouponListener();
importCouponListener.initData(coupon,userInfoModel, highCouponCodeService);
EasyExcel.read(files.getInputStream(), ImportCouponModel.class, importCouponListener).sheet().doRead();
Map<String, Object> map = new HashMap<>();
map.put("errorData", importCouponListener.getErrorData());
map.put("errorTotal", importCouponListener.getErrorData().size());
return ResponseMsgUtil.success(map);
} catch (Exception e) { } catch (Exception e) {
log.error("HighCouponController -> getCouponList() error!",e); log.error("HighCouponController -> importStock() error!",e);
return ResponseMsgUtil.exception(e); return ResponseMsgUtil.exception(e);
} }
} }
@ -349,8 +378,20 @@ public class HighCouponController {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STATUS_ERROR, ""); throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STATUS_ERROR, "");
} }
Date salesEndTimeD = new Date(salesEndTime); // 销售截止时间
Date useEndTimeD =new Date(useEndTime); Calendar salesEndTimeC = Calendar.getInstance();
salesEndTimeC.setTime(new Date(salesEndTime));
salesEndTimeC.set(Calendar.HOUR_OF_DAY, 23);
salesEndTimeC.set(Calendar.MINUTE, 59);
salesEndTimeC.set(Calendar.SECOND, 59);
// 使用截止时间
Calendar useEndTimeC = Calendar.getInstance();
useEndTimeC.setTime(new Date(useEndTime));
useEndTimeC.set(Calendar.HOUR_OF_DAY, 23);
useEndTimeC.set(Calendar.MINUTE, 59);
useEndTimeC.set(Calendar.SECOND, 59);
HighCouponCode highCouponCode; HighCouponCode highCouponCode;
List<HighCouponCode> list = new ArrayList<>(); List<HighCouponCode> list = new ArrayList<>();
for (int i = 0; i < generateNum;i++) { for (int i = 0; i < generateNum;i++) {
@ -358,8 +399,8 @@ public class HighCouponController {
highCouponCode.setCouponId(couponId); highCouponCode.setCouponId(couponId);
highCouponCode.setMerchantId(coupon.getMerchantId()); highCouponCode.setMerchantId(coupon.getMerchantId());
highCouponCode.setSalesCode(generateCode.couponSalesCode(coupon.getMerchantId(), couponId)); highCouponCode.setSalesCode(generateCode.couponSalesCode(coupon.getMerchantId(), couponId));
highCouponCode.setSalesEndTime(salesEndTimeD); highCouponCode.setSalesEndTime(salesEndTimeC.getTime());
highCouponCode.setUseEndTime(useEndTimeD); highCouponCode.setUseEndTime(useEndTimeC.getTime());
highCouponCode.setStatus(1); // 状态:1.待销售 2.未使用 3.已使用 highCouponCode.setStatus(1); // 状态:1.待销售 2.未使用 3.已使用
highCouponCode.setCreateTime(new Date()); highCouponCode.setCreateTime(new Date());
highCouponCode.setOperatorId(userInfoModel.getSecUser().getId()); highCouponCode.setOperatorId(userInfoModel.getSecUser().getId());

@ -0,0 +1,112 @@
package com.bweb.excelListener;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.bweb.model.ImportCouponModel;
import com.hai.common.utils.DateUtil;
import com.hai.entity.HighCoupon;
import com.hai.entity.HighCouponCode;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCouponCodeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Auther: 胡锐
* @Description: 导入监听器
* @Date: 2021/3/20 21:03
*/
public class ImportCouponListener extends AnalysisEventListener<ImportCouponModel> {
List<ImportCouponModel> errorData = new ArrayList<>();
List<HighCouponCode> successData = new ArrayList<>();
List<HighCouponCode> allCouponCode = new ArrayList<>();
HighCouponCode highCouponCode;
HighCoupon highCoupon;
UserInfoModel userInfoModel;
private HighCouponCodeService highCouponCodeService;
/**
* @Author 胡锐
* @Description 解析到的每一条数据都会调用此方法
* @Date 2021/3/20 21:08
**/
@Override
public void invoke(ImportCouponModel importCouponModel, AnalysisContext analysisContext) {
if (StringUtils.isBlank(importCouponModel.getSalesCode())
|| StringUtils.isBlank(importCouponModel.getSalesEndTime())
|| StringUtils.isBlank(importCouponModel.getUseEndTime()) ) {
importCouponModel.setErrorMessage("有必填项未填写");
errorData.add(importCouponModel);
return;
}
List<HighCouponCode> collect = allCouponCode.stream().filter(o -> o.getSalesCode().equals(importCouponModel.getSalesCode())).collect(Collectors.toList());
if (collect != null && collect.size() > 0) {
importCouponModel.setErrorMessage("销售码已存在");
errorData.add(importCouponModel);
return;
}
highCouponCode = new HighCouponCode();
highCouponCode.setCouponId(highCoupon.getCompanyId());
highCouponCode.setMerchantId(highCoupon.getMerchantId());
highCouponCode.setSalesCode(importCouponModel.getSalesCode());
// 销售截止时间
Calendar salesEndTime = Calendar.getInstance();
salesEndTime.setTime(DateUtil.format(importCouponModel.getSalesEndTime(), "yyyy/MM/dd"));
salesEndTime.set(Calendar.HOUR_OF_DAY, 23);
salesEndTime.set(Calendar.MINUTE, 59);
salesEndTime.set(Calendar.SECOND, 59);
highCouponCode.setSalesEndTime(salesEndTime.getTime());
// 使用截止时间
Calendar useEndTime = Calendar.getInstance();
useEndTime.setTime(DateUtil.format(importCouponModel.getUseEndTime(), "yyyy/MM/dd"));
useEndTime.set(Calendar.HOUR_OF_DAY, 23);
useEndTime.set(Calendar.MINUTE, 59);
useEndTime.set(Calendar.SECOND, 59);
highCouponCode.setUseEndTime(useEndTime.getTime());
highCouponCode.setStatus(1);
highCouponCode.setOperatorId(userInfoModel.getSecUser().getId());
highCouponCode.setOperatorName(userInfoModel.getSecUser().getUserName());
highCouponCode.setCreateTime(new Date());
successData.add(highCouponCode);
allCouponCode.add(highCouponCode);
}
/**
* @Author 胡锐
* @Description 所有数据解析完成了 都会来调用此方法
* @Date 2021/3/20 21:08
**/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
if (successData.size() > 0) {
highCouponCodeService.insertList(successData);
}
this.successData.clear();
this.allCouponCode.clear();
}
public List<ImportCouponModel> getErrorData() {
return errorData;
}
public void initData(HighCoupon highCoupon, UserInfoModel userInfoModel, HighCouponCodeService highCouponCodeService){
this.highCoupon = highCoupon;
this.userInfoModel = userInfoModel;
this.highCouponCodeService = highCouponCodeService;
this.allCouponCode = this.highCouponCodeService.getCouponCodeList(new HashMap<>());
}
}

@ -0,0 +1,57 @@
package com.bweb.model;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
/**
* @Auther: 胡锐
* @Description: 卡卷导入模型
* @Date: 2021/3/20 21:03
*/
public class ImportCouponModel {
@ExcelProperty("卡券销售码")
private String salesCode;
@ExcelProperty("销售有效期")
@DateTimeFormat("yyyy/MM/dd")
private String salesEndTime;
@ExcelProperty("使用有效期")
private String useEndTime;
// 错误消息
private String errorMessage;
public String getSalesCode() {
return salesCode;
}
public void setSalesCode(String salesCode) {
this.salesCode = salesCode;
}
public String getSalesEndTime() {
return salesEndTime;
}
public void setSalesEndTime(String salesEndTime) {
this.salesEndTime = salesEndTime;
}
public String getUseEndTime() {
return useEndTime;
}
public void setUseEndTime(String useEndTime) {
this.useEndTime = useEndTime;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}

@ -0,0 +1,34 @@
package common;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/20 20:51
*/
public class DemoDataListener extends AnalysisEventListener<ExcelModel> {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
List<ExcelModel> list = new ArrayList<>();
@Override
public void invoke(ExcelModel excelModel, AnalysisContext analysisContext) {
System.out.println(JSON.toJSONString(excelModel));
list.add(excelModel);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
LOGGER.info("所有数据解析完成!");
System.out.println("所有数据解析完成");
}
}

@ -0,0 +1,33 @@
package common;
import com.alibaba.excel.annotation.ExcelProperty;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/20 20:26
*/
public class ExcelModel {
@ExcelProperty("卡券编码")
private String key;
@ExcelProperty("商品编码")
private String name;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,44 @@
package common;
import com.BWebApplication;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.hai.entity.SecRegion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/20 20:26
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BWebApplication.class)
@WebAppConfiguration
public class ExcelTest {
@Test
public void test(){
try {
List<ExcelModel> list = new ArrayList<>();
EasyExcel.read("F:\\卡券列表记录.xlsx", ExcelModel.class, new DemoDataListener()).sheet().doRead();
}catch (Exception e){
e.printStackTrace();
}
}
}

@ -1,7 +1,7 @@
package common; package common;
import com.BWebApplication;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.hai.Application;
import com.hai.entity.SecRegion; import com.hai.entity.SecRegion;
import com.hai.service.CommonService; import com.hai.service.CommonService;
@ -26,7 +26,7 @@ import java.util.Map;
* @Date 2020/12/29 * @Date 2020/12/29
**/ **/
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = BWebApplication.class)
@WebAppConfiguration @WebAppConfiguration
public class RegionTest { public class RegionTest {

@ -184,17 +184,17 @@
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>3.14</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>3.14</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId> <artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.zxing</groupId> <groupId>com.google.zxing</groupId>
@ -271,6 +271,11 @@
<artifactId>weixin-java-miniapp</artifactId> <artifactId>weixin-java-miniapp</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -5,6 +5,7 @@ import com.hai.model.HighCouponCodeModel;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Auther: 胡锐 * @Auther: 胡锐
@ -38,4 +39,11 @@ public interface HighCouponCodeService {
**/ **/
Integer getStockCountByCoupon(Long couponId); Integer getStockCountByCoupon(Long couponId);
/**
* @Author 胡锐
* @Description 查询销售码
* @Date 2021/3/21 16:37
**/
List<HighCouponCode> getCouponCodeList(Map<String,Object> map);
} }

@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Auther: 胡锐 * @Auther: 胡锐
@ -42,5 +43,14 @@ public class HighCouponCodeServiceImpl implements HighCouponCodeService {
return highCouponCodeMapper.selectByExample(example).size(); return highCouponCodeMapper.selectByExample(example).size();
} }
@Override
public List<HighCouponCode> getCouponCodeList(Map<String, Object> map) {
HighCouponCodeExample example = new HighCouponCodeExample();
example.createCriteria();
example.setOrderByClause("create_time desc");
return highCouponCodeMapper.selectByExample(example);
}
} }

Loading…
Cancel
Save