172 lines
5.9 KiB
Java
172 lines
5.9 KiB
Java
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)));
|
||
}
|
||
}
|