导入充值
This commit is contained in:
		| @ -0,0 +1,97 @@ | ||||
| package cn.iocoder.yudao.module.member.controller.admin.excelImport; | ||||
|  | ||||
| import cn.hutool.core.util.ObjectUtil; | ||||
| import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; | ||||
| import cn.iocoder.yudao.module.member.controller.admin.excelImport.vo.RechargeExcel; | ||||
| import cn.iocoder.yudao.module.member.controller.admin.excelImport.vo.RechargeExcelListener; | ||||
| import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; | ||||
| import cn.iocoder.yudao.module.member.service.amount.CashRechargeService; | ||||
| import cn.iocoder.yudao.module.member.service.user.MemberUserService; | ||||
| import com.alibaba.excel.EasyExcel; | ||||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
| import org.springframework.web.bind.annotation.PostMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| import org.springframework.web.multipart.MultipartFile; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Objects; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
|  | ||||
| @Tag(name = "管理后台 - 表格导入") | ||||
| @RestController | ||||
| @RequestMapping("/member/excelImport") | ||||
| @Validated | ||||
| public class ExcelImportController { | ||||
|  | ||||
|     @Resource | ||||
|     private MemberUserService memberUserService; | ||||
|     @Resource | ||||
|     private CashRechargeService cashRechargeService; | ||||
|  | ||||
|     @PostMapping("/easyExcelImport") | ||||
|     public void importExcel(MultipartFile file, HttpServletResponse response) { | ||||
|         if (!file.isEmpty()) { | ||||
|             //文件名称 | ||||
|             int begin = Objects.requireNonNull(file.getOriginalFilename()).indexOf("."); | ||||
|             //文件名称长度 | ||||
|             int last = file.getOriginalFilename().length(); | ||||
|             //判断文件格式是否正确 | ||||
|             String fileName = file.getOriginalFilename().substring(begin, last); | ||||
|             if (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")) { | ||||
|                 throw new IllegalArgumentException("上传文件格式错误"); | ||||
|             } | ||||
|         } else { | ||||
|             throw new IllegalArgumentException("文件不能为空"); | ||||
|         } | ||||
|         try (InputStream inputStream = file.getInputStream()) { | ||||
|             List<RechargeExcel> rechargeExcels = simpleRead(inputStream); | ||||
|             ExcelUtils.write(response, "人员不存在.xlsx", "数据", RechargeExcel.class, | ||||
|                     rechargeExcels); | ||||
|         } catch (IOException e) { | ||||
|             System.out.println(e.getMessage()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 最简单的读的监听器 | ||||
|      */ | ||||
|     public List<RechargeExcel> simpleRead(InputStream inputStream) { | ||||
|         //获取正确数据 | ||||
|         ArrayList<RechargeExcel> successArrayList = new ArrayList<>(); | ||||
|         EasyExcel.read(inputStream) | ||||
|                 .head(RechargeExcel.class) | ||||
|                 .registerReadListener(new RechargeExcelListener( | ||||
|                         // 监听器中doAfterAllAnalysed执行此方法;所有读取完成之后处理逻辑 | ||||
|                         successArrayList::addAll)) | ||||
|                 // 设置sheet,默认读取第一个 | ||||
|                 .sheet() | ||||
|                 // 设置标题(字段列表)所在行数 | ||||
|                 .headRowNumber(2) | ||||
|                 .doRead(); | ||||
|         List<String> mobiles = successArrayList.stream().map(RechargeExcel::getMobile).collect(Collectors.toList()); | ||||
|         List<MemberUserDO> users = memberUserService.getByMobiles(mobiles); | ||||
|  | ||||
|  | ||||
|         List<RechargeExcel> notExist = new ArrayList<>(); | ||||
|         for (RechargeExcel excel:successArrayList){ | ||||
|             MemberUserDO userByMobile = memberUserService.getUserByMobile(excel.getMobile()); | ||||
|             if(ObjectUtil.isEmpty(userByMobile)){ | ||||
|                 notExist.add(excel); | ||||
|                 continue; | ||||
|             } | ||||
|             List<Long> ids = new ArrayList<>(); | ||||
|             ids.add(userByMobile.getId()); | ||||
|             cashRechargeService.rechargeByAdmin(ids,null,excel.getMoney()); | ||||
|         } | ||||
|         return notExist; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,62 @@ | ||||
| package cn.iocoder.yudao.module.member.controller.admin.excelImport.vo; | ||||
|  | ||||
| import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; | ||||
| import com.alibaba.excel.annotation.ExcelProperty; | ||||
|  | ||||
| import java.math.BigDecimal; | ||||
|  | ||||
| /** | ||||
|  * @author zt | ||||
|  * @description <description class purpose> | ||||
|  * @since 2024/11/5 | ||||
|  */ | ||||
|  | ||||
| @ExcelIgnoreUnannotated | ||||
| public class RechargeExcel { | ||||
|  | ||||
|     /** | ||||
|      * 用名字去匹配,这里需要注意,如果名字重复,会导致只有一个字段读取到数据 | ||||
|      */ | ||||
|     @ExcelProperty(index = 0) | ||||
|     public String name; | ||||
|  | ||||
|     @ExcelProperty(index = 1) | ||||
|     public String mobile; | ||||
|  | ||||
|     @ExcelProperty( index = 2) | ||||
|     public BigDecimal money; | ||||
|  | ||||
|  | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
|  | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
|  | ||||
|     public String getMobile() { | ||||
|         return mobile; | ||||
|     } | ||||
|  | ||||
|     public void setMobile(String mobile) { | ||||
|         this.mobile = mobile; | ||||
|     } | ||||
|  | ||||
|     public BigDecimal getMoney() { | ||||
|         return money; | ||||
|     } | ||||
|  | ||||
|     public void setMoney(BigDecimal money) { | ||||
|         this.money = money; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "RechargeExcel{" + | ||||
|                 "name='" + name + '\'' + | ||||
|                 ", mobile='" + mobile + '\'' + | ||||
|                 ", money=" + money + | ||||
|                 '}'; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,42 @@ | ||||
| package cn.iocoder.yudao.module.member.controller.admin.excelImport.vo; | ||||
|  | ||||
|  | ||||
| import com.alibaba.excel.context.AnalysisContext; | ||||
| import com.alibaba.excel.event.AnalysisEventListener; | ||||
| import com.google.common.collect.Lists; | ||||
| import org.apache.commons.collections4.CollectionUtils; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.function.Consumer; | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * 读取excel数据 | ||||
|  */ | ||||
| public class RechargeExcelListener extends AnalysisEventListener<RechargeExcel> { | ||||
|  | ||||
|     /**临时存储正常数据集合,最大存储100*/ | ||||
|     private List<RechargeExcel> successDataList = Lists.newArrayListWithExpectedSize(300); | ||||
|  | ||||
|     /**自定义消费者函数接口用于自定义监听器中数据组装*/ | ||||
|     private final Consumer<List<RechargeExcel>> successConsumer; | ||||
|  | ||||
|     public RechargeExcelListener(Consumer<List<RechargeExcel>> successConsumer) { | ||||
|         this.successConsumer = successConsumer; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void invoke(RechargeExcel rechargeExcel, AnalysisContext analysisContext) { | ||||
|         successDataList.add(rechargeExcel); | ||||
|         System.out.println("数据:"+rechargeExcel); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     @Override | ||||
|     public void doAfterAllAnalysed(AnalysisContext analysisContext) { | ||||
|         if (CollectionUtils.isNotEmpty(successDataList)) { | ||||
|             successConsumer.accept(successDataList); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -260,4 +260,6 @@ public interface MemberUserService { | ||||
|     boolean delete(Long userId); | ||||
|  | ||||
|     String getQRCode(); | ||||
|  | ||||
|     List<MemberUserDO> getByMobiles(List<String>  mobiles); | ||||
| } | ||||
|  | ||||
| @ -92,10 +92,7 @@ import java.util.stream.Collectors; | ||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||
| import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; | ||||
| import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | ||||
| import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.setLoginUser; | ||||
| import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*; | ||||
| import static cn.iocoder.yudao.module.member.util.QRCodeWithJWTUtil.generateJWT; | ||||
| import static cn.iocoder.yudao.module.member.util.QRCodeWithJWTUtil.validateJWT; | ||||
|  | ||||
| /** | ||||
|  * 会员 User Service 实现类 | ||||
| @ -931,4 +928,11 @@ public class MemberUserServiceImpl implements MemberUserService { | ||||
|         memberUserRedisTemplate.opsForValue().set(encode,userId.toString(),180, TimeUnit.SECONDS); | ||||
|         return s; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<MemberUserDO> getByMobiles(List<String> mobiles) { | ||||
|         List<MemberUserDO> list = memberUserMapper.selectList(Wrappers.<MemberUserDO>lambdaQuery() | ||||
|                 .in(MemberUserDO::getMobile, mobiles)); | ||||
|         return list; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 zengtao01
					zengtao01