前言
现需求是在原有的导出功能基础上,新增两列。
实现
因为新增两列不是数据库表中字段。因此,需要需要建立一个VO类。
原本想用若依继承导出,如下所示:
@Data
public class ThemeActivityUserVO extends ThemeActivityUser {private static final long serialVersionUID = 1L;private ThemeActivityUser themeActivityUser;/*** 可以领取的礼品名称*/@Excel(name = "可以领取的礼品名称", type = Excel.Type.EXPORT)private String giftNames;/*** 可以领取的礼品总数量*/@Excel(name = "可以领取的礼品总数量", type = Excel.Type.EXPORT)int giftNumber;}
但想起来Java中不能继承祖父类的私有属性,所以就把VO改成如所示:
ThemeActivityUserVO
/*** @Description: 活动用户群体对象VO*/
@Data
public class ThemeActivityUserVO implements Serializable {private static final long serialVersionUID = 1L;@Excel(name = "领取单号", type = Excel.Type.EXPORT)private String id;/*** 活动ID*/@Excel(name = "活动ID", type = Excel.Type.EXPORT)private String activityId;/*** 用户ID*/@Excel(name = "用户ID", type = Excel.Type.EXPORT)private String userId;/*** 工号*/@Excel(name = "工号", type = Excel.Type.EXPORT)private String workNo;/*** 手机号*/@Excel(name = "手机号", type = Excel.Type.EXPORT)private String phone;/*** 用户姓名*/@Excel(name = "用户姓名", type = Excel.Type.EXPORT)private String userName;/*** 公司*/@Excel(name = "公司", type = Excel.Type.EXPORT)private String company;/*** 部门*/@Excel(name = "部门", type = Excel.Type.EXPORT)private String department;/*** 收货人名称*/@Excel(name = "收货人名称", type = Excel.Type.EXPORT)private String receiveName;/*** 收货人手机号*/@Excel(name = "收货人手机号", type = Excel.Type.EXPORT)private String receivePhone;/*** 收货地区*/@Excel(name = "收货地区", type = Excel.Type.EXPORT)private String receiveRegion;/*** 收货地址*/@Excel(name = "收货地址", type = Excel.Type.EXPORT)private String receiveAddress;/*** 快递单号*/@Excel(name = "快递单号", type = Excel.Type.EXPORT)private String logisticsNo;/*** 物流公司*/@Excel(name = "物流公司", type = Excel.Type.EXPORT)private String logisticsCompany;/*** 快递联系方式*/@Excel(name = "快递联系方式", type = Excel.Type.EXPORT)private String logisticsPhone;/*** 发货时间*/@Excel(name = "发货时间", type = Excel.Type.EXPORT, dateFormat = "yyyy-MM-dd HH:mm:ss")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date deliveryTime;/*** 领取状态:0-待领取 1-已领取 2-已发货 9-补发待领取*/@Excel(name = "领取状态", type = Excel.Type.EXPORT, readConverterExp = "0-待领取,1=已领取,2=已发货,9=补发待领取")private String userStatus;/*** 领取时间*/@Excel(name = "领取时间", type = Excel.Type.EXPORT, dateFormat = "yyyy-MM-dd HH:mm:ss")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date receiveTime;/*** 领取数*/@Excel(name = "领取数", type = Excel.Type.EXPORT)private Integer receiveNum;/*** 领取的礼品ID*/private String receiveGiftIds;/*** 已经领取的礼品名称*/@Excel(name = "已经领取的礼品名称", type = Excel.Type.EXPORT)private String receiveGiftNames;/*** 可以领取的礼品名称*/@Excel(name = "可以领取的礼品名称", type = Excel.Type.EXPORT)private String giftNames;/*** 可以领取的礼品总数量*/@Excel(name = "可以领取的礼品总数量", type = Excel.Type.EXPORT)int giftNumber;}
Contorller
/*** @Description: 导出活动用户列表*/@SysLog("导出活动用户列表")@PostMapping("/export")public void export(HttpServletResponse response, ThemeActivityUser themeActivityUser,ThemeActivityGift themeActivityGift) {List<ThemeActivityUser> list = themeActivityUserService.list(Wrappers.query(themeActivityUser));List<ThemeActivityGift> themeActivityGiftList = themeActivityGiftService.list(Wrappers.query(themeActivityGift));List<ThemeActivityUserVO> themeActivityUserList = themeActivityUserService.wrapReceiveGiftNames(list,themeActivityGiftList);ExcelUtil<ThemeActivityUserVO> util = new ExcelUtil<>(ThemeActivityUserVO.class);util.exportExcel(response, themeActivityUserList, "活动目标用户列表");}
ThemeActivityUserService
/*** @Description: 增加领取礼品的名称*/List<ThemeActivityUserVO> wrapReceiveGiftNames(List<ThemeActivityUser> list,List<ThemeActivityGift> themeActivityGiftList);
ThemeActivityUserServiceImpl
@Overridepublic List<ThemeActivityUserVO> wrapReceiveGiftNames(List<ThemeActivityUser> list, List<ThemeActivityGift> themeActivityGiftList) {if (CollUtil.isEmpty(list)) {return null;}Map<String, String> themeActivityGiftsMap = new HashMap<String, String>();themeActivityGiftList.stream().forEach(themeActivityGift -> {themeActivityGiftsMap.put(themeActivityGift.getId(), themeActivityGift.getName());});List<ThemeActivityUserVO> themeActivityUserVOList = new ArrayList<>();list.stream().forEach(themeActivityUser -> {ThemeActivityUserVO themeActivityUserVO = new ThemeActivityUserVO();//用一个map存可以领取的礼品名称和总数量Map<String, String> allThemeActivityGiftsMap = new HashMap<String, String>();//获取可以领取的礼品名称和总数量if (ObjectUtil.isNotNull(themeActivityUser.getGiftIds())) {String giftIds = themeActivityUser.getGiftIds();if (giftIds.contains(ThemeActivityConst.COMMA)) {log.info("可以领取多礼品 giftIds={} ", giftIds);String[] giftIdsArr = giftIds.split(ThemeActivityConst.COMMA);StringBuilder giftNames = new StringBuilder();for (String giftId : giftIdsArr) {for (Map.Entry<String, String> entry : themeActivityGiftsMap.entrySet()) {if (giftId.equals(entry.getKey())) {giftNames.append(entry.getValue()).append(ThemeActivityConst.COMMA);//获得可领取礼品的id和NameallThemeActivityGiftsMap.put(entry.getKey(), entry.getValue());}}}themeActivityUserVO.setGiftNames(giftNames.toString());themeActivityUserVO.setGiftNumber(giftIdsArr.length);BeanUtil.copyProperties(themeActivityUser, themeActivityUserVO);} else {log.info("可以领取单礼品 giftIds={} ", giftIds);for (Map.Entry<String, String> entry : themeActivityGiftsMap.entrySet()) {if (giftIds.equals(entry.getKey())) {themeActivityUserVO.setGiftNames(entry.getValue());themeActivityUserVO.setGiftNumber(1);BeanUtil.copyProperties(themeActivityUser, themeActivityUserVO);}//获得可领取礼品的id和NameallThemeActivityGiftsMap.put(entry.getKey(), entry.getValue());}}}//获取已经领取的礼品名称String receiveGiftIds = themeActivityUser.getReceiveGiftIds();List<String> receiveGiftIdsArr = StrUtil.split(receiveGiftIds, ThemeActivityConst.COMMA);//排除用户还没有领取礼品的场景if (ObjectUtil.isNotNull(receiveGiftIds)) {if (receiveGiftIds.contains(ThemeActivityConst.COMMA)) {//取已经领取的礼品名称和可以领取的礼品名称的交集Collection<String> themeActivityGiftsHashKeys = CollUtil.intersection(receiveGiftIdsArr, allThemeActivityGiftsMap.keySet());ArrayList<String> themeActivityGiftsHashValues = new ArrayList<>();for (String key : themeActivityGiftsHashKeys) {themeActivityGiftsHashValues.add(allThemeActivityGiftsMap.get(key));}String receiveGiftNames = CollUtil.join(themeActivityGiftsHashValues, ThemeActivityConst.COMMA);themeActivityUserVO.setReceiveGiftNames(receiveGiftNames);} else {log.info("单礼品 receiveGiftIds={} ", receiveGiftIds);for (Map.Entry<String, String> entry : allThemeActivityGiftsMap.entrySet()) {if (receiveGiftIds.equals(entry.getKey())) {themeActivityUserVO.setReceiveGiftNames(entry.getValue());}}}}themeActivityUserVOList.add(themeActivityUserVO);});return themeActivityUserVOList;}