33333333333
This commit is contained in:
@@ -0,0 +1,171 @@
|
|||||||
|
package com.ruoyi.cai.controller;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
|
import cn.hutool.core.util.NumberUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.ruoyi.cai.domain.BatchAddUser;
|
||||||
|
import com.ruoyi.cai.domain.User;
|
||||||
|
import com.ruoyi.cai.service.BatchAddUserService;
|
||||||
|
import com.ruoyi.cai.service.UserService;
|
||||||
|
import com.ruoyi.cai.service.impl.BatchAddUserServiceImpl;
|
||||||
|
import com.ruoyi.common.annotation.Log;
|
||||||
|
import com.ruoyi.common.annotation.RepeatSubmit;
|
||||||
|
import com.ruoyi.common.core.controller.BaseController;
|
||||||
|
import com.ruoyi.common.core.domain.BaseEntity;
|
||||||
|
import com.ruoyi.common.core.domain.PageQuery;
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.ruoyi.common.core.page.TableDataInfo;
|
||||||
|
import com.ruoyi.common.enums.BusinessType;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量新增用户脚本
|
||||||
|
*
|
||||||
|
* @author 777
|
||||||
|
* @date 2025-02-12
|
||||||
|
*/
|
||||||
|
@Validated
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/cai/batchAddUser")
|
||||||
|
public class BatchAddUserController extends BaseController {
|
||||||
|
|
||||||
|
private final BatchAddUserService batchAddUserService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询批量新增用户脚本列表
|
||||||
|
*/
|
||||||
|
@SaCheckPermission("cai:batchAddUser:list")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public TableDataInfo<BatchAddUser> list(BatchAddUser bo, PageQuery pageQuery) {
|
||||||
|
Page<BatchAddUser> page = batchAddUserService.page(pageQuery.build(), Wrappers.lambdaQuery(bo).orderByDesc(BatchAddUser::getCreateTime));
|
||||||
|
return TableDataInfo.build(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取批量新增用户脚本详细信息
|
||||||
|
*
|
||||||
|
* @param id 主键
|
||||||
|
*/
|
||||||
|
@SaCheckPermission("cai:batchAddUser:query")
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public R<BatchAddUser> getInfo(@NotNull(message = "主键不能为空")
|
||||||
|
@PathVariable String id) {
|
||||||
|
return R.ok(batchAddUserService.getById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增批量新增用户脚本
|
||||||
|
*/
|
||||||
|
@SaCheckPermission("cai:batchAddUser:add")
|
||||||
|
@Log(title = "新增用户", businessType = BusinessType.INSERT)
|
||||||
|
@RepeatSubmit()
|
||||||
|
@PostMapping()
|
||||||
|
public R<Void> add(@RequestBody BatchAddUser bo) {
|
||||||
|
if(bo.getPhonePrefix()==null || bo.getPhonePrefix().trim().length() != 7){
|
||||||
|
return R.fail("手机前缀必须为7位数字");
|
||||||
|
}
|
||||||
|
boolean number = NumberUtil.isNumber(bo.getPhonePrefix());
|
||||||
|
if(!number){
|
||||||
|
return R.fail("手机前缀必须为数字");
|
||||||
|
}
|
||||||
|
if("0".equals(bo.getPhonePrefix().substring(0,1))){
|
||||||
|
return R.fail("手机前缀不和规范");
|
||||||
|
}
|
||||||
|
if(StringUtils.isNotBlank(bo.getInviteUserCode())){
|
||||||
|
User user = userService.getByUserCode(bo.getInviteUserCode());
|
||||||
|
if(user == null){
|
||||||
|
return R.fail("邀请人不存在,请重现检查邀请人Code");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toAjax(batchAddUserService.save(bo));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@PostMapping("/exportUser")
|
||||||
|
public R<Void> exportUser(Long id, HttpServletResponse response) {
|
||||||
|
BatchAddUser user = batchAddUserService.getById(id);
|
||||||
|
if(user == null){
|
||||||
|
return R.fail("无数据");
|
||||||
|
}
|
||||||
|
String runResult = user.getRunResult();
|
||||||
|
List<BatchAddUserServiceImpl.RunResult> runResults = JSON.parseArray(runResult, BatchAddUserServiceImpl.RunResult.class);
|
||||||
|
response.setContentType("text/plain;charset=GB2312");
|
||||||
|
response.setCharacterEncoding("GB2312");
|
||||||
|
response.setHeader("content-disposition", "attachment;filename=导出用户" + user.getId() + ".txt");
|
||||||
|
BufferedOutputStream buff = null;
|
||||||
|
ServletOutputStream outSTr = null;
|
||||||
|
String enter = "\r\n";
|
||||||
|
String txt = "账号:%s 密码:%s";
|
||||||
|
try {
|
||||||
|
outSTr = response.getOutputStream();
|
||||||
|
buff = new BufferedOutputStream(outSTr);
|
||||||
|
for (BatchAddUserServiceImpl.RunResult r : runResults) {
|
||||||
|
String sql = String.format(txt, r.getPhone(), r.getPassword());
|
||||||
|
buff.write((sql+enter).getBytes("GB2312"));
|
||||||
|
}
|
||||||
|
buff.flush();
|
||||||
|
buff.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return R.fail("导出失败");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if(buff != null){
|
||||||
|
buff.close();
|
||||||
|
}
|
||||||
|
if(outSTr != null){
|
||||||
|
outSTr.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改批量新增用户脚本
|
||||||
|
*/
|
||||||
|
@SaCheckPermission("cai:batchAddUser:edit")
|
||||||
|
@Log(title = "执行新增用户", businessType = BusinessType.UPDATE)
|
||||||
|
@RepeatSubmit()
|
||||||
|
@PutMapping("/run")
|
||||||
|
public R<Void> edit(@RequestBody BatchAddUser bo) {
|
||||||
|
batchAddUserService.run(bo.getId());
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除批量新增用户脚本
|
||||||
|
*
|
||||||
|
* @param ids 主键串
|
||||||
|
*/
|
||||||
|
@SaCheckPermission("cai:batchAddUser:remove")
|
||||||
|
@Log(title = "新增用户", businessType = BusinessType.DELETE)
|
||||||
|
@DeleteMapping("/{ids}")
|
||||||
|
public R<Void> remove(@NotEmpty(message = "主键不能为空")
|
||||||
|
@PathVariable String[] ids) {
|
||||||
|
return toAjax(batchAddUserService.removeBatchByIds(Arrays.asList(ids)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.ruoyi.cai.domain;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.ruoyi.common.core.domain.BaseEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量新增用户脚本对象 cai_batch_add_user
|
||||||
|
*
|
||||||
|
* @author 777
|
||||||
|
* @date 2025-02-12
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("cai_batch_add_user")
|
||||||
|
public class BatchAddUser implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID=1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@TableId(value = "id")
|
||||||
|
private String id;
|
||||||
|
/**
|
||||||
|
* 新增用户数
|
||||||
|
*/
|
||||||
|
private Integer addNum;
|
||||||
|
/**
|
||||||
|
* 1-女 2-男
|
||||||
|
*/
|
||||||
|
private Integer gender;
|
||||||
|
/**
|
||||||
|
* 邀请人code
|
||||||
|
*/
|
||||||
|
private String inviteUserCode;
|
||||||
|
/**
|
||||||
|
* 手机前缀(前7位)
|
||||||
|
*/
|
||||||
|
private String phonePrefix;
|
||||||
|
/**
|
||||||
|
* 手机后缀起点(后4位)
|
||||||
|
*/
|
||||||
|
private Integer phoneSuffixStart;
|
||||||
|
/**
|
||||||
|
* 密码前缀
|
||||||
|
*/
|
||||||
|
private String passwordPrefix;
|
||||||
|
/**
|
||||||
|
* 密码后缀起点
|
||||||
|
*/
|
||||||
|
private Integer passwordSuffixStart;
|
||||||
|
/**
|
||||||
|
* 执行状态
|
||||||
|
*/
|
||||||
|
private Integer runStatus;
|
||||||
|
/**
|
||||||
|
* 执行失败原因
|
||||||
|
*/
|
||||||
|
private String runFail;
|
||||||
|
/**
|
||||||
|
* 执行结果
|
||||||
|
*/
|
||||||
|
private String runResult;
|
||||||
|
/**
|
||||||
|
* 执行时间
|
||||||
|
*/
|
||||||
|
private LocalDateTime runTime;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.ruoyi.cai.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.ruoyi.cai.domain.BatchAddUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量新增用户脚本Mapper接口
|
||||||
|
*
|
||||||
|
* @author 777
|
||||||
|
* @date 2025-02-12
|
||||||
|
*/
|
||||||
|
public interface BatchAddUserMapper extends BaseMapper<BatchAddUser> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.ruoyi.cai.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.ruoyi.cai.domain.BatchAddUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量新增用户脚本Service接口
|
||||||
|
*
|
||||||
|
* @author 777
|
||||||
|
* @date 2025-02-12
|
||||||
|
*/
|
||||||
|
public interface BatchAddUserService extends IService<BatchAddUser> {
|
||||||
|
|
||||||
|
void run(String id);
|
||||||
|
|
||||||
|
void addUser(String id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package com.ruoyi.cai.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.ruoyi.cai.auth.CaiLoginManager;
|
||||||
|
import com.ruoyi.cai.auth.CaiRegisterUser;
|
||||||
|
import com.ruoyi.cai.domain.BatchAddUser;
|
||||||
|
import com.ruoyi.cai.domain.User;
|
||||||
|
import com.ruoyi.cai.enums.GenderEnum;
|
||||||
|
import com.ruoyi.cai.enums.SystemConfigEnum;
|
||||||
|
import com.ruoyi.cai.manager.SystemConfigManager;
|
||||||
|
import com.ruoyi.cai.mapper.BatchAddUserMapper;
|
||||||
|
import com.ruoyi.cai.service.AnchorService;
|
||||||
|
import com.ruoyi.cai.service.BatchAddUserService;
|
||||||
|
import com.ruoyi.cai.service.UserService;
|
||||||
|
import com.ruoyi.common.exception.base.BaseException;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量新增用户脚本Service业务层处理
|
||||||
|
*
|
||||||
|
* @author 777
|
||||||
|
* @date 2025-02-12
|
||||||
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class BatchAddUserServiceImpl extends ServiceImpl<BatchAddUserMapper,BatchAddUser> implements BatchAddUserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CaiLoginManager caiLoginManager;
|
||||||
|
@Autowired
|
||||||
|
private SystemConfigManager systemConfigManager;
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
@Autowired
|
||||||
|
private AnchorService anchorService;
|
||||||
|
@Autowired
|
||||||
|
private BatchAddUserService batchAddUserService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String id) {
|
||||||
|
boolean update = this.update(Wrappers.lambdaUpdate(BatchAddUser.class).eq(BatchAddUser::getId, id)
|
||||||
|
.eq(BatchAddUser::getRunStatus, 0).set(BatchAddUser::getRunStatus, 1));
|
||||||
|
if(!update){
|
||||||
|
throw new BaseException("无法重复执行");
|
||||||
|
}
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
batchAddUserService.addUser(id);
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error("新增用户失败 id={}", id,e);
|
||||||
|
this.update(Wrappers.lambdaUpdate(BatchAddUser.class).eq(BatchAddUser::getId, id)
|
||||||
|
.set(BatchAddUser::getRunTime, LocalDateTime.now())
|
||||||
|
.set(BatchAddUser::getRunStatus, 2)
|
||||||
|
.set(BatchAddUser::getRunFail, e.getMessage()));
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void addUser(String id){
|
||||||
|
BatchAddUser batchAddUser = this.getById(id);
|
||||||
|
Integer phoneSuffixStart = batchAddUser.getPhoneSuffixStart();
|
||||||
|
List<RunResult> runResults = new ArrayList<>();
|
||||||
|
for (int i = 0; i < batchAddUser.getAddNum(); i++) {
|
||||||
|
String phone = String.format("%s%04d", batchAddUser.getPhonePrefix(), phoneSuffixStart+i);
|
||||||
|
if(phone.length() != 11){
|
||||||
|
log.error("新增用户:手机号非法 mobile={}",phone);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String password = String.format("%s%s", batchAddUser.getPasswordPrefix(), batchAddUser.getPasswordSuffixStart()+i);
|
||||||
|
String code = systemConfigManager.getSystemConfig(SystemConfigEnum.SMS_CODE_ADMIN);
|
||||||
|
CaiRegisterUser registerUser = new CaiRegisterUser();
|
||||||
|
registerUser.setUsername(phone);
|
||||||
|
registerUser.setPassword(password);
|
||||||
|
registerUser.setPasswordCheck(password);
|
||||||
|
registerUser.setCode(code);
|
||||||
|
registerUser.setInviteId(batchAddUser.getInviteUserCode());
|
||||||
|
registerUser.setSystemOp(true);
|
||||||
|
caiLoginManager.register(registerUser);
|
||||||
|
User user = userService.getByUsername(phone);
|
||||||
|
userService.update(Wrappers.lambdaUpdate(User.class).eq(User::getId,user.getId())
|
||||||
|
.set(User::getBirthday, LocalDate.now().plusYears(-18))
|
||||||
|
.set(User::getAge, 18)
|
||||||
|
.set(User::getGender, GenderEnum.WOMEN.getCode())
|
||||||
|
.set(User::getCityId, "140882")
|
||||||
|
.set(User::getCity, "河津"));
|
||||||
|
anchorService.joinAnchor(user.getId(),true);
|
||||||
|
runResults.add(new RunResult(phone,password));
|
||||||
|
}
|
||||||
|
this.update(Wrappers.lambdaUpdate(BatchAddUser.class).eq(BatchAddUser::getId,id)
|
||||||
|
.set(BatchAddUser::getRunStatus, 3)
|
||||||
|
.set(BatchAddUser::getRunResult, JSON.toJSONString(runResults))
|
||||||
|
.set(BatchAddUser::getRunTime, LocalDateTime.now()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class RunResult {
|
||||||
|
private String phone;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public RunResult() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public RunResult(String phone, String password) {
|
||||||
|
this.phone = phone;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.ruoyi.cai.mapper.BatchAddUserMapper">
|
||||||
|
|
||||||
|
<resultMap type="com.ruoyi.cai.domain.BatchAddUser" id="BatchAddUserResult">
|
||||||
|
<result property="id" column="id"/>
|
||||||
|
<result property="addNum" column="add_num"/>
|
||||||
|
<result property="gender" column="gender"/>
|
||||||
|
<result property="inviteUserCode" column="invite_user_code"/>
|
||||||
|
<result property="phonePrefix" column="phone_prefix"/>
|
||||||
|
<result property="phoneSuffixStart" column="phone_suffix_start"/>
|
||||||
|
<result property="passwordPrefix" column="password_prefix"/>
|
||||||
|
<result property="passwordSuffixStart" column="password_suffix_start"/>
|
||||||
|
<result property="runStatus" column="run_status"/>
|
||||||
|
<result property="runFail" column="run_fail"/>
|
||||||
|
<result property="runResult" column="run_result"/>
|
||||||
|
<result property="createTime" column="create_time"/>
|
||||||
|
<result property="runTime" column="run_time"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user