CouponController.java
4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.diligrp.etrade.coupon.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.diligrp.etrade.core.domain.Message;
import com.diligrp.etrade.core.domain.PageMessage;
import com.diligrp.etrade.core.util.JsonUtils;
import com.diligrp.etrade.coupon.consotant.CouponConstant;
import com.diligrp.etrade.coupon.consotant.RedisKeyConstant;
import com.diligrp.etrade.coupon.domain.*;
import com.diligrp.etrade.coupon.lisenter.ExcelListener;
import com.diligrp.etrade.coupon.model.CouponBindUserDo;
import com.diligrp.etrade.coupon.model.CouponQueryDo;
import com.diligrp.etrade.coupon.model.CouponResponseDo;
import com.diligrp.etrade.coupon.model.CouponTemplateDo;
import com.diligrp.etrade.coupon.service.BusinessLogService;
import com.diligrp.etrade.coupon.service.CouponService;
import com.diligrp.etrade.coupon.service.ExcelService;
import com.diligrp.etrade.coupon.util.TUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.List;
/**
* @Author jy
* @Date 2023/8/28 16:23
* @Version 1.0
*/
@RestController
@RequestMapping("/coupon")
public class CouponController implements IControllerHandler {
Logger log = LoggerFactory.getLogger(CouponController.class);
@Resource
private ExcelService excelService;
@Resource
private CouponService couponService;
@Resource
private BusinessLogService businessLogService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/upload")
public Message<?> upload(MultipartHttpServletRequest request) throws IOException {
MultipartFile file = request.getFile(CouponConstant.FILE);
String template = request.getParameter(CouponConstant.TEMPLATE);
TUtils.isTure(ObjectUtils.isEmpty(file)).throwMessage("未上传文件");
TUtils.isTure(ObjectUtils.isEmpty(template)).throwMessage("模板信息未传入");
String name = file.getOriginalFilename().toLowerCase();
TUtils.isTure(!(name.endsWith(CouponConstant.XLS) || name.endsWith(CouponConstant.XLSX))).throwMessage("文件格式不对,上传失败");
CouponTemplateDto couponTemplateDto = JsonUtils.fromJsonString(template, CouponTemplateDto.class);
//对模版进行验证
couponTemplateDto = couponService.checkTemplate(couponTemplateDto);
buildOperatorInfo(couponTemplateDto);
log.info("批量发放优惠劵对应模板信息,{}", couponTemplateDto);
try {
long start = System.currentTimeMillis();
EasyExcel.read(file.getInputStream(), CouponPhoneDto.class, new ExcelListener(couponTemplateDto, excelService, businessLogService)).sheet().doRead();
long end = System.currentTimeMillis();
log.info("上传花费时间:{}", (end - start));
} catch (Exception e) {
CouponTemplateDto finalCouponTemplateDto = couponTemplateDto;
TUtils.presentHandler(e.getCause() instanceof ExcelDataConvertException).presentHandle(()->{
stringRedisTemplate.delete(RedisKeyConstant.TEMPLATE_NO_SEND + finalCouponTemplateDto.getTemplateNo());
});
return Message.failure(e.getMessage());
}
return Message.success("文件上传成功");
}
/**
* 优惠劵列表查询
*
* @param queryDto
* @return
*/
@PostMapping("/page")
public PageMessage<CouponQueryDo> page(@RequestBody CouponQueryDto queryDto) {
log.info("优惠劵表查询入参,{}", queryDto);
int pageNo = ObjectUtils.isEmpty(queryDto.getPageNo()) ? 1 : queryDto.getPageNo();
int pageSize = ObjectUtils.isEmpty(queryDto.getPageSize()) ? 20 : queryDto.getPageSize();
queryDto.setPageNo(pageNo);
queryDto.setPageSize(pageSize);
queryDto.setMarketId(getUserTicket().getFirmId());
List<CouponQueryDo> list = couponService.page(queryDto);
return convertPageResult(list);
}
/**
* 导出
*/
@PostMapping("export")
public PageMessage<CouponQueryDo> export(@RequestBody CouponQueryDto queryDto) {
log.info("模板列表导出入参,{}", queryDto);
queryDto.setPageNo(1);
queryDto.setPageSize(Integer.MAX_VALUE);
queryDto.setExport(true);
queryDto.setMarketId(getUserTicket().getFirmId());
List<CouponQueryDo> list = couponService.page(queryDto);
return convertPageResult(list);
}
}