diff --git a/hai-bweb/src/main/java/com/bweb/controller/HighCouponController.java b/hai-bweb/src/main/java/com/bweb/controller/HighCouponController.java index 38cb01aa..c4c027f1 100644 --- a/hai-bweb/src/main/java/com/bweb/controller/HighCouponController.java +++ b/hai-bweb/src/main/java/com/bweb/controller/HighCouponController.java @@ -1,6 +1,9 @@ package com.bweb.controller; +import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson.JSONObject; +import com.bweb.excelListener.ImportCouponListener; +import com.bweb.model.ImportCouponModel; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.hai.common.exception.ErrorCode; @@ -29,6 +32,7 @@ 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; @@ -300,17 +304,42 @@ public class HighCouponController { } - @RequestMapping(value="/importStock",method = RequestMethod.GET) + @RequestMapping(value="/importStock",method = RequestMethod.POST) @ResponseBody @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 { - Map 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 map = new HashMap<>(); + map.put("errorData", importCouponListener.getErrorData()); + map.put("errorTotal", importCouponListener.getErrorData().size()); + + return ResponseMsgUtil.success(map); } catch (Exception e) { - log.error("HighCouponController -> getCouponList() error!",e); + log.error("HighCouponController -> importStock() error!",e); return ResponseMsgUtil.exception(e); } } @@ -349,8 +378,20 @@ public class HighCouponController { 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; List list = new ArrayList<>(); for (int i = 0; i < generateNum;i++) { @@ -358,8 +399,8 @@ public class HighCouponController { highCouponCode.setCouponId(couponId); highCouponCode.setMerchantId(coupon.getMerchantId()); highCouponCode.setSalesCode(generateCode.couponSalesCode(coupon.getMerchantId(), couponId)); - highCouponCode.setSalesEndTime(salesEndTimeD); - highCouponCode.setUseEndTime(useEndTimeD); + highCouponCode.setSalesEndTime(salesEndTimeC.getTime()); + highCouponCode.setUseEndTime(useEndTimeC.getTime()); highCouponCode.setStatus(1); // 状态:1.待销售 2.未使用 3.已使用 highCouponCode.setCreateTime(new Date()); highCouponCode.setOperatorId(userInfoModel.getSecUser().getId()); diff --git a/hai-bweb/src/main/java/com/bweb/excelListener/ImportCouponListener.java b/hai-bweb/src/main/java/com/bweb/excelListener/ImportCouponListener.java new file mode 100644 index 00000000..a31f5d36 --- /dev/null +++ b/hai-bweb/src/main/java/com/bweb/excelListener/ImportCouponListener.java @@ -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 { + + List errorData = new ArrayList<>(); + List successData = new ArrayList<>(); + List 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 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 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<>()); + } + +} diff --git a/hai-bweb/src/main/java/com/bweb/model/ImportCouponModel.java b/hai-bweb/src/main/java/com/bweb/model/ImportCouponModel.java new file mode 100644 index 00000000..76a687f8 --- /dev/null +++ b/hai-bweb/src/main/java/com/bweb/model/ImportCouponModel.java @@ -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; + } +} diff --git a/hai-bweb/src/test/common/DemoDataListener.java b/hai-bweb/src/test/common/DemoDataListener.java new file mode 100644 index 00000000..1612a14b --- /dev/null +++ b/hai-bweb/src/test/common/DemoDataListener.java @@ -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 { + + private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); + + List 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("所有数据解析完成"); + } +} diff --git a/hai-bweb/src/test/common/ExcelModel.java b/hai-bweb/src/test/common/ExcelModel.java new file mode 100644 index 00000000..ed6f11db --- /dev/null +++ b/hai-bweb/src/test/common/ExcelModel.java @@ -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; + } +} diff --git a/hai-bweb/src/test/common/ExcelTest.java b/hai-bweb/src/test/common/ExcelTest.java new file mode 100644 index 00000000..d38b9892 --- /dev/null +++ b/hai-bweb/src/test/common/ExcelTest.java @@ -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 list = new ArrayList<>(); + EasyExcel.read("F:\\卡券列表记录.xlsx", ExcelModel.class, new DemoDataListener()).sheet().doRead(); + + + }catch (Exception e){ + e.printStackTrace(); + } + } + +} diff --git a/hai-bweb/src/test/common/RegionTest.java b/hai-bweb/src/test/common/RegionTest.java index 1071c6d2..b1aacb56 100644 --- a/hai-bweb/src/test/common/RegionTest.java +++ b/hai-bweb/src/test/common/RegionTest.java @@ -1,7 +1,7 @@ package common; +import com.BWebApplication; import com.alibaba.fastjson.JSON; -import com.hai.Application; import com.hai.entity.SecRegion; import com.hai.service.CommonService; @@ -26,7 +26,7 @@ import java.util.Map; * @Date 2020/12/29 **/ @RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = Application.class) +@SpringBootTest(classes = BWebApplication.class) @WebAppConfiguration public class RegionTest { diff --git a/hai-service/pom.xml b/hai-service/pom.xml index 59aa7cbd..6b21aa7e 100644 --- a/hai-service/pom.xml +++ b/hai-service/pom.xml @@ -184,17 +184,17 @@ org.apache.poi poi - 3.14 + 4.1.2 org.apache.poi poi-ooxml - 3.14 + 4.1.2 org.apache.poi poi-ooxml-schemas - 3.14 + 4.1.2 com.google.zxing @@ -271,6 +271,11 @@ weixin-java-miniapp 3.8.0 + + com.alibaba + easyexcel + 2.2.6 + diff --git a/hai-service/src/main/java/com/hai/service/HighCouponCodeService.java b/hai-service/src/main/java/com/hai/service/HighCouponCodeService.java index c27cffe2..b0badeee 100644 --- a/hai-service/src/main/java/com/hai/service/HighCouponCodeService.java +++ b/hai-service/src/main/java/com/hai/service/HighCouponCodeService.java @@ -5,6 +5,7 @@ import com.hai.model.HighCouponCodeModel; import java.util.Date; import java.util.List; +import java.util.Map; /** * @Auther: 胡锐 @@ -38,4 +39,11 @@ public interface HighCouponCodeService { **/ Integer getStockCountByCoupon(Long couponId); + /** + * @Author 胡锐 + * @Description 查询销售码 + * @Date 2021/3/21 16:37 + **/ + List getCouponCodeList(Map map); + } diff --git a/hai-service/src/main/java/com/hai/service/impl/HighCouponCodeServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighCouponCodeServiceImpl.java index 88d7892d..3c505b4a 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighCouponCodeServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighCouponCodeServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; +import java.util.Map; /** * @Auther: 胡锐 @@ -42,5 +43,14 @@ public class HighCouponCodeServiceImpl implements HighCouponCodeService { return highCouponCodeMapper.selectByExample(example).size(); } + @Override + public List getCouponCodeList(Map map) { + HighCouponCodeExample example = new HighCouponCodeExample(); + example.createCriteria(); + + example.setOrderByClause("create_time desc"); + return highCouponCodeMapper.selectByExample(example); + } + }