| | |
| | | import cn.afterturn.easypoi.excel.entity.ExportParams; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.TemplateExportParams; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.PhoneUtil; |
| | | import cn.hutool.core.io.resource.ClassPathResource; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | |
| | | import io.swagger.annotations.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType; |
| | | import org.apache.shardingsphere.transaction.core.TransactionType; |
| | | import org.springframework.context.MessageSource; |
| | | import org.springframework.context.i18n.LocaleContextHolder; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | private MessageSource messageSource; |
| | | |
| | | private static final Pattern CHINESE_PATTERN = Pattern.compile("\\p{IsHan}"); |
| | | private static final long INVITATION_CODE_LIMIT = 500L; |
| | | |
| | | @ApiOperation(value = "邀请码管理分页列表",response = XpInvitationCode.class) |
| | | @PostMapping("/api/v1/invitation/pageList") |
| | |
| | | @PostMapping("/api/v1/invitation/add") |
| | | @ResponseBody |
| | | @ApiOperation(value = "生成邀请码") |
| | | @Transactional |
| | | public R add(@RequestBody InvitationAddDto dto){ |
| | | long existing = xpInvitationCodeService.count(); |
| | | long incoming = Arrays.stream(Optional.ofNullable(dto.getPhones()).orElse("").split(",")) |
| | | .map(String::trim) |
| | | .filter(StringUtils::hasLength) |
| | | .count(); |
| | | if (existing >= INVITATION_CODE_LIMIT || existing + incoming > INVITATION_CODE_LIMIT) { |
| | | String msg = messageSource.getMessage(I18nKeyConstant.INVITATION_LIMIT_EXCEEDED, |
| | | new Object[]{INVITATION_CODE_LIMIT, existing, incoming}, |
| | | I18nKeyConstant.INVITATION_LIMIT_EXCEEDED, |
| | | LocaleContextHolder.getLocale()); |
| | | return R.fail(msg); |
| | | } |
| | | return xpInvitationCodeService.add(dto.getPhones()); |
| | | } |
| | | /** |
| | |
| | | } |
| | | @ApiOperation(value = "邀请码导入") |
| | | @PostMapping("/base/v1/invitation/importExcel") |
| | | public R<String> importExcel(@RequestPart("file") MultipartFile file) { |
| | | public R<String> importExcel(@RequestParam("file") MultipartFile file) { |
| | | ImportParams params = new ImportParams(); |
| | | params.setHeadRows(1); //表头行数 |
| | | InputStream inputStream = null; |
| | |
| | | return R.fail(messageSource.getMessage(I18nKeyConstant.INVITATION_IMPORT_DATA_EMPTY, null, I18nKeyConstant.INVITATION_IMPORT_DATA_EMPTY, LocaleContextHolder.getLocale())); |
| | | } |
| | | |
| | | long existing = xpInvitationCodeService.count(); |
| | | long incoming = invitationCodeImportExcels.size(); |
| | | if (existing >= INVITATION_CODE_LIMIT || existing + incoming > INVITATION_CODE_LIMIT) { |
| | | String msg = messageSource.getMessage(I18nKeyConstant.INVITATION_LIMIT_EXCEEDED, |
| | | new Object[]{INVITATION_CODE_LIMIT, existing, incoming}, |
| | | I18nKeyConstant.INVITATION_LIMIT_EXCEEDED, |
| | | LocaleContextHolder.getLocale()); |
| | | return R.fail(msg); |
| | | } |
| | | |
| | | // 批量校验:账号必须为手机号;邀请码不能为空且不能包含中文;同时校验文件内重复/数据库已存在 |
| | | List<String> errors = new ArrayList<>(); |
| | | Set<String> importCodes = new HashSet<>(); |
| | |
| | | |
| | | if (!StringUtils.hasLength(bindAccount)) { |
| | | errors.add(messageSource.getMessage(I18nKeyConstant.INVITATION_IMPORT_ACCOUNT_EMPTY, new Object[]{rowNum}, I18nKeyConstant.INVITATION_IMPORT_ACCOUNT_EMPTY, LocaleContextHolder.getLocale())); |
| | | } else if (!PhoneUtil.isPhone(bindAccount)) { |
| | | } else if (!PhoneUtil.isMobile(bindAccount)) { |
| | | errors.add(messageSource.getMessage(I18nKeyConstant.INVITATION_IMPORT_ACCOUNT_INVALID, new Object[]{rowNum, bindAccount}, I18nKeyConstant.INVITATION_IMPORT_ACCOUNT_INVALID, LocaleContextHolder.getLocale())); |
| | | } |
| | | |
| | |
| | | xpInvitationCodeService.removeBatchByIds(longs); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "校验生成邀请码的手机号是否合法") |
| | | @GetMapping("/api/v1/invitation/validateCode") |
| | | public R<List<String>> validateCode(@RequestParam String phone){ |
| | | List<String> phoneList = Arrays.asList(phone.split(",")); |
| | | List<String> existPhones = phoneList.stream().filter(item -> xpInvitationCodeService.lambdaQuery() |
| | | .in(XpInvitationCode::getBindAccount, item) |
| | | .count() > 0).collect(Collectors.toList()); |
| | | return R.ok(existPhones); |
| | | } |
| | | } |