fix:78300

This commit is contained in:
梁琦涛
2023-11-08 15:38:03 +08:00
parent b8f3ab7a0e
commit 18bd843e92
4 changed files with 325 additions and 351 deletions
@@ -1,9 +1,5 @@
package com.jero.modules.sys.controller;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
@@ -15,6 +11,7 @@ import com.jero.common.util.ImportExcelUtil;
import com.jero.common.util.MessageUtils;
import com.jero.modules.laws.common.constant.ResultCommon;
import com.jero.modules.sys.entity.LawsWorkingGroupUser;
import com.jero.modules.sys.entity.LawsWorkingGroupUserImport;
import com.jero.modules.sys.service.ILawsWorkingGroupUserService;
import com.jero.modules.system.entity.SysUser;
import com.jero.modules.system.service.ISysUserService;
@@ -25,380 +22,331 @@ import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
/**
* @Description: 内部工作组人员关联表
* @Author: jero-boot
* @Date: 2023-09-06
* @Date: 2023-09-06
* @Version: V1.0
*/
@Api(tags="内部工作组人员关联表")
@Api(tags = "内部工作组人员关联表")
@RestController
@RequestMapping("/sys/lawsWorkingGroupUser")
@Slf4j
public class LawsWorkingGroupUserController extends JeroController<LawsWorkingGroupUser, ILawsWorkingGroupUserService> {
@Autowired
private ILawsWorkingGroupUserService lawsWorkingGroupUserService;
@Autowired
private ISysUserService sysUserService;
@Autowired
private ILawsWorkingGroupUserService lawsWorkingGroupUserService;
@Autowired
private ISysUserService sysUserService;
/**
* 分页列表查询
*/
@RequiresPermissions("sys:innerWorkingGroup:search")
@AutoLog(value = "内部工作组人员关联表-分页列表查询")
@ApiOperation(value="内部工作组人员关联表-分页列表查询", notes="内部工作组人员关联表-分页列表查询")
@GetMapping(value = "/page")
public Result<IPage<LawsWorkingGroupUser>> queryPageList(LawsWorkingGroupUser lawsWorkingGroupUser,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
lawsWorkingGroupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
IPage<LawsWorkingGroupUser> pageList = lawsWorkingGroupUserService.queryPage(lawsWorkingGroupUser, pageNo, pageSize, req);
lawsWorkingGroupUserService.userAssignment(pageList.getRecords());
return Result.OK(pageList);
}
/**
* 列表查询
*/
@RequiresPermissions("sys:innerWorkingGroup:search")
@AutoLog(value = "内部工作组人员关联表-列表查询")
@ApiOperation(value="内部工作组人员关联表-列表查询", notes="内部工作组人员关联表-列表查询")
@GetMapping(value = "/list")
public Result<List<LawsWorkingGroupUser>> queryList(LawsWorkingGroupUser lawsWorkingGroupUser, HttpServletRequest req) {
lawsWorkingGroupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
* 分页列表查询
*/
@RequiresPermissions("sys:innerWorkingGroup:search")
@AutoLog(value = "内部工作组人员关联表-分页列表查询")
@ApiOperation(value = "内部工作组人员关联表-分页列表查询", notes = "内部工作组人员关联表-分页列表查询")
@GetMapping(value = "/page")
public Result<IPage<LawsWorkingGroupUser>> queryPageList(LawsWorkingGroupUser lawsWorkingGroupUser,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest req) {
lawsWorkingGroupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
IPage<LawsWorkingGroupUser> pageList = lawsWorkingGroupUserService.queryPage(lawsWorkingGroupUser, pageNo, pageSize, req);
lawsWorkingGroupUserService.userAssignment(pageList.getRecords());
return Result.OK(pageList);
}
/**
* 列表查询
*/
@RequiresPermissions("sys:innerWorkingGroup:search")
@AutoLog(value = "内部工作组人员关联表-列表查询")
@ApiOperation(value = "内部工作组人员关联表-列表查询", notes = "内部工作组人员关联表-列表查询")
@GetMapping(value = "/list")
public Result<List<LawsWorkingGroupUser>> queryList(LawsWorkingGroupUser lawsWorkingGroupUser, HttpServletRequest req) {
lawsWorkingGroupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
List<LawsWorkingGroupUser> list = lawsWorkingGroupUserService.queryList(lawsWorkingGroupUser, req);
lawsWorkingGroupUserService.userAssignment(list);
lawsWorkingGroupUserService.userAssignment(list);
return Result.OK(list);
}
}
/**
* 添加
*/
@RequiresPermissions("sys:innerWorkingGroup:add")
@AutoLog(value = "内部工作组人员关联表-添加")
@ApiOperation(value="内部工作组人员关联表-添加", notes="内部工作组人员关联表-添加")
@PostMapping(value = "/add")
public Result<T> add(@Validated @RequestBody LawsWorkingGroupUser lawsWorkingGroupUser) {
if (StringUtils.isBlank(lawsWorkingGroupUser.getWorkingGroupId()) || StringUtils.isBlank(lawsWorkingGroupUser.getUserId())){
throw new JeroBootException(ResultCommon.PLEASE_SELECT_PERSONNEL_AND_WORKGROUP);
}
String userId = lawsWorkingGroupUser.getUserId();
List<String> asList = Arrays.asList(userId.split(","));
LambdaQueryWrapper<LawsWorkingGroupUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LawsWorkingGroupUser::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
wrapper.in(LawsWorkingGroupUser::getUserId, asList);
wrapper.eq(LawsWorkingGroupUser::getWorkingGroupId, lawsWorkingGroupUser.getWorkingGroupId());
List<LawsWorkingGroupUser> list = lawsWorkingGroupUserService.list(wrapper);
if (list.size() > 0){
String workNo;
List<String> collect = list.stream().map(LawsWorkingGroupUser::getUserId).distinct().collect(Collectors.toList());
StringJoiner joiner = new StringJoiner(",");
for (String id : collect) {
SysUser sysUser = sysUserService.getById(id);
if (sysUser != null && StringUtils.isNotBlank(sysUser.getUsername())) {
joiner.add(sysUser.getUsername());
}
}
workNo = joiner.toString();
throw new JeroBootException(ResultCommon.NUMBER_WORK_NO_ALREADY_EXISTS, workNo);
}
List<LawsWorkingGroupUser> groupUsers = new ArrayList<>();
for (String id : asList) {
LawsWorkingGroupUser workingGroupUser = new LawsWorkingGroupUser();
workingGroupUser.setWorkingGroupId(lawsWorkingGroupUser.getWorkingGroupId());
workingGroupUser.setUserId(id);
workingGroupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
groupUsers.add(workingGroupUser);
}
lawsWorkingGroupUserService.saveBatch(groupUsers);
return Result.OK(MessageUtils.getMessage(ResultCommon.OK));
}
/**
* 通过id删除
*/
@RequiresPermissions("sys:innerWorkingGroup:delete")
@AutoLog(value = "内部工作组人员关联表-通过id删除")
@ApiOperation(value="内部工作组人员关联表-通过id删除", notes="内部工作组人员关联表-通过id删除")
@PostMapping(value = "/delete")
public Result<T> delete(@RequestBody Map<String, String> map) {
if(!map.containsKey("id") || StringUtils.isEmpty(map.get("id"))){
throw new JeroBootException(ResultCommon.PLEASE_SELECT_DATA);
/**
* 添加
*/
@RequiresPermissions("sys:innerWorkingGroup:add")
@AutoLog(value = "内部工作组人员关联表-添加")
@ApiOperation(value = "内部工作组人员关联表-添加", notes = "内部工作组人员关联表-添加")
@PostMapping(value = "/add")
public Result<T> add(@Validated @RequestBody LawsWorkingGroupUser lawsWorkingGroupUser) {
if (StringUtils.isBlank(lawsWorkingGroupUser.getWorkingGroupId()) || StringUtils.isBlank(lawsWorkingGroupUser.getUserId())) {
throw new JeroBootException(ResultCommon.PLEASE_SELECT_PERSONNEL_AND_WORKGROUP);
}
LawsWorkingGroupUser groupUser = lawsWorkingGroupUserService.getById(map.get("id"));
groupUser.setDelFlag(CommonConstant.DEL_FLAG_1.toString());
lawsWorkingGroupUserService.updateById(groupUser);
return Result.OK(MessageUtils.getMessage(ResultCommon.OK));
}
String userId = lawsWorkingGroupUser.getUserId();
List<String> asList = Arrays.asList(userId.split(","));
LambdaQueryWrapper<LawsWorkingGroupUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LawsWorkingGroupUser::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
wrapper.in(LawsWorkingGroupUser::getUserId, asList);
wrapper.eq(LawsWorkingGroupUser::getWorkingGroupId, lawsWorkingGroupUser.getWorkingGroupId());
List<LawsWorkingGroupUser> list = lawsWorkingGroupUserService.list(wrapper);
if (CollectionUtils.isEmpty(list)) {
String workNo;
List<String> collect = list.stream().map(LawsWorkingGroupUser::getUserId).distinct().collect(Collectors.toList());
StringJoiner joiner = new StringJoiner(",");
for (String id : collect) {
SysUser sysUser = sysUserService.getById(id);
if (sysUser != null && StringUtils.isNotBlank(sysUser.getUsername())) {
joiner.add(sysUser.getUsername());
}
}
workNo = joiner.toString();
throw new JeroBootException(ResultCommon.NUMBER_WORK_NO_ALREADY_EXISTS, workNo);
}
List<LawsWorkingGroupUser> groupUsers = new ArrayList<>();
for (String id : asList) {
LawsWorkingGroupUser workingGroupUser = new LawsWorkingGroupUser();
workingGroupUser.setWorkingGroupId(lawsWorkingGroupUser.getWorkingGroupId());
workingGroupUser.setUserId(id);
workingGroupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
groupUsers.add(workingGroupUser);
}
lawsWorkingGroupUserService.saveBatch(groupUsers);
return Result.OK(MessageUtils.getMessage(ResultCommon.OK));
}
/**
* 通过id删除
*/
@RequiresPermissions("sys:innerWorkingGroup:delete")
@AutoLog(value = "内部工作组人员关联表-通过id删除")
@ApiOperation(value = "内部工作组人员关联表-通过id删除", notes = "内部工作组人员关联表-通过id删除")
@PostMapping(value = "/delete")
public Result<T> delete(@RequestBody Map<String, String> map) {
if (!map.containsKey("id") || StringUtils.isEmpty(map.get("id"))) {
throw new JeroBootException(ResultCommon.PLEASE_SELECT_DATA);
}
lawsWorkingGroupUserService.deleteById(map.get("id"));
return Result.OK(MessageUtils.getMessage(ResultCommon.OK));
}
/**
* 通过excel导入数据
*/
@RequiresPermissions("sys:innerWorkingGroup:import")
@AutoLog(value = "内部工作组人员关联表-导入")
@ApiOperation(value="内部工作组人员关联表-导入", notes="内部工作组人员关联表-导入")
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importFile(MultipartFile file, String workingGroupId) throws Exception {
String fileName = file.getOriginalFilename();
// 上传文件为空
if (StringUtils.isEmpty(fileName)) {
throw new JeroBootException(ResultCommon.NO_FILES_IMPORTED);
}
if (StringUtils.isBlank(workingGroupId)) {
throw new JeroBootException(ResultCommon.PLEASE_SELECT_A_WORKGROUP_TO_IMPORT);
}
// 上传文件名格式不正确
if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) {
throw new JeroBootException(ResultCommon.PLEASE_USE_A_FILE_WITH_THE_SUFFIX_XLSX);
}
boolean b;
try {
b = ImportExcelUtil.checkTemplateTitle(file, LawsWorkingGroupUser.class, 0, 0);
} catch (Exception e) {
e.printStackTrace();
throw new JeroBootException(ResultCommon.PLEASE_USE_A_FILE_WITH_THE_SUFFIX_XLSX);
}
// 校验excel 字段是否正确
if (!b) {
throw new JeroBootException(ResultCommon.HEADER_INCONSISTENT_WITH_TEMPLATE);
}
/**
* 通过excel导入数据
*/
@RequiresPermissions("sys:innerWorkingGroup:import")
@AutoLog(value = "内部工作组人员关联表-导入")
@ApiOperation(value = "内部工作组人员关联表-导入", notes = "内部工作组人员关联表-导入")
@PostMapping(value = "/importExcel")
public Result<String> importFile(MultipartFile file, String workingGroupId) throws Exception {
String fileName = file.getOriginalFilename();
// 上传文件为空
if (StringUtils.isEmpty(fileName)) {
throw new JeroBootException(ResultCommon.NO_FILES_IMPORTED);
}
if (StringUtils.isBlank(workingGroupId)) {
throw new JeroBootException(ResultCommon.PLEASE_SELECT_A_WORKGROUP_TO_IMPORT);
}
// 上传文件名格式不正确
if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) {
throw new JeroBootException(ResultCommon.PLEASE_USE_A_FILE_WITH_THE_SUFFIX_XLSX);
}
boolean b;
try {
b = ImportExcelUtil.checkTemplateTitle(file, LawsWorkingGroupUserImport.class, 0, 0);
} catch (Exception e) {
e.printStackTrace();
throw new JeroBootException(ResultCommon.PLEASE_USE_A_FILE_WITH_THE_SUFFIX_XLSX);
}
// 校验excel 字段是否正确
if (!b) {
throw new JeroBootException(ResultCommon.HEADER_INCONSISTENT_WITH_TEMPLATE);
}
InputStream inputStream = file.getInputStream();
List<LawsWorkingGroupUser> all;
try {
ExcelReader excelReader = ExcelUtil.getReader(inputStream, "sheet1");
List<Map<String, Object>> maps = excelReader.readAll();
if (maps.size() < 1) {
throw new JeroBootException(ResultCommon.IMPORT_DATA_CANNOT_BE_EMPTY);
}
// 校验表头,同时映射对象字段
if (!validateHeader(excelReader)) {
throw new JeroBootException(ResultCommon.HEADER_INCONSISTENT_WITH_TEMPLATE);
}
all = excelReader.readAll(LawsWorkingGroupUser.class);
} catch (Exception e) {
throw new JeroBootException(ResultCommon.ERROR);
}
//校验导入正确性,msg是错误信息
String msg = importExcelCheck(all);
if (StringUtils.isNotBlank(msg)) {
return Result.error(msg);
}
for (LawsWorkingGroupUser groupUser : all) {
String username = groupUser.getUsername();
SysUser userByWorkNo = sysUserService.getUserByName(username);
groupUser.setUserId(userByWorkNo.getId());
groupUser.setWorkingGroupId(workingGroupId);
groupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
}
lawsWorkingGroupUserService.saveBatch(all);
return Result.OK(MessageUtils.getMessage(ResultCommon.OK));
}
List<LawsWorkingGroupUser> all = new ArrayList<>();
try {
ImportParams params = new ImportParams();
params.setTitleRows(0);
params.setHeadRows(1);
params.setNeedSave(true);
List<LawsWorkingGroupUserImport> list = ExcelImportUtil.importExcel(file.getInputStream(), LawsWorkingGroupUserImport.class, params);
if (CollectionUtils.isEmpty(list)) {
throw new JeroBootException(ResultCommon.IMPORT_DATA_CANNOT_BE_EMPTY);
}
for (LawsWorkingGroupUserImport lawsWorkingGroupUserImport : list) {
LawsWorkingGroupUser lawsWorkingGroupUser = new LawsWorkingGroupUser();
lawsWorkingGroupUser.setUsername(lawsWorkingGroupUserImport.getUsername());
lawsWorkingGroupUser.setRealname(lawsWorkingGroupUserImport.getRealname());
all.add(lawsWorkingGroupUser);
}
} catch (Exception e) {
throw new JeroBootException(ResultCommon.ERROR);
}
//校验导入正确性,msg是错误信息
String msg = importExcelCheck(all,workingGroupId);
if (StringUtils.isNotBlank(msg)) {
return Result.error(msg);
}
for (LawsWorkingGroupUser groupUser : all) {
String username = groupUser.getUsername();
SysUser userByWorkNo = sysUserService.getUserByName(username);
groupUser.setUserId(userByWorkNo.getId());
groupUser.setWorkingGroupId(workingGroupId);
groupUser.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
}
lawsWorkingGroupUserService.saveBatch(all);
return Result.OK(MessageUtils.getMessage(ResultCommon.OK));
}
/**
* 导出
*/
@RequiresPermissions("sys:innerWorkingGroup:export")
@AutoLog(value = "内部工作组人员关联表-导出")
@ApiOperation(value="内部工作组人员关联表-导出", notes="内部工作组人员关联表-导出")
@GetMapping(value = "/export")
public Result<?> template(@RequestParam(required = false) String workingGroupId, @RequestParam(required = false) String realname, @RequestParam(required = false) String username, @RequestParam(required = false) String userRoleName, @RequestParam(required = false) String userPostName, HttpServletRequest request, HttpServletResponse response) {
if (StringUtils.isBlank(workingGroupId)){
throw new JeroBootException(ResultCommon.PLEASE_SELECT_A_WORKGROUP_TO_EXPORT);
}
String outFileName = "内部工作组人员表" + System.currentTimeMillis() + ".xlsx";
String ENCODING = "UTF-8";
ExcelWriter writer = ExcelUtil.getWriter(true);
/**
* 导出
*/
@RequiresPermissions("sys:innerWorkingGroup:export")
@AutoLog(value = "内部工作组人员关联表-导出")
@ApiOperation(value = "内部工作组人员关联表-导出", notes = "内部工作组人员关联表-导出")
@GetMapping(value = "/export")
public ModelAndView template(@RequestParam(required = false) String workingGroupId, @RequestParam(required = false) String realname, @RequestParam(required = false) String username, @RequestParam(required = false) String userRoleName, @RequestParam(required = false) String userPostName, HttpServletRequest request, HttpServletResponse response) {
if (StringUtils.isBlank(workingGroupId)) {
throw new JeroBootException(ResultCommon.PLEASE_SELECT_A_WORKGROUP_TO_EXPORT);
}
// 通过工具类创建writer并且进行别名
assembleWriter(writer);
//获取数据,分为三种情况:1、选中ID;2、没选ID且选了姓名、工号、角色、岗位四个筛选框;3、没选ID且没选四个筛选框
List<LawsWorkingGroupUser> dos = null;
//获取选中数据
String selections = request.getParameter("selections");
//获取数据,分为三种情况:1、选中ID;2、没选ID且选了姓名、工号、角色、岗位四个筛选框;3、没选ID且没选四个筛选框
List<LawsWorkingGroupUser> dos = null;
//获取选中数据
String selections = request.getParameter("selections");
if (StringUtils.isNotBlank(selections)) {
dos = this.queryDataByUserId(selections);
}
if (StringUtils.isBlank(selections) && !StringUtils.isAllBlank(realname, username, userRoleName, userPostName)) {
dos = this.queryDataByConditions(realname, username, userRoleName, userPostName, workingGroupId);
}
if (StringUtils.isBlank(selections) && StringUtils.isAllBlank(realname, username, userRoleName, userPostName)) {
dos = this.queryDataByTree(workingGroupId);
}
lawsWorkingGroupUserService.userAssignment(dos);
if (StringUtils.isNotBlank(selections)){
dos = this.queryDataByUserId(selections);
}
if (StringUtils.isBlank(selections) && !StringUtils.isAllBlank(realname, username, userRoleName, userPostName)){
dos = this.queryDataByConditions(realname, username, userRoleName, userPostName, workingGroupId);
}
if (StringUtils.isBlank(selections) && StringUtils.isAllBlank(realname, username, userRoleName, userPostName)){
dos = this.queryDataByTree(workingGroupId);
}
// AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
String title = "内部工作组人员表";
//此处设置的filename无效 ,前端会重更新设置一下
mv.addObject(FILE_NAME, title);
mv.addObject(CLASS, LawsWorkingGroupUser.class);
ExportParams exportParams = new ExportParams(null, title);
// 导出xlsx
exportParams.setType(ExcelType.XSSF);
mv.addObject(PARAMS, exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, dos);
return mv;
}
lawsWorkingGroupUserService.userAssignment(dos);
Collections.reverse(dos);
//导出表头只保留别名中的字段
writer.setOnlyAlias(true);
//表格高度
writer.setDefaultRowHeight(18);
//表格宽度
writer.setColumnWidth(-1, 20);
//放入数据
writer.write(dos, true);
try {
// 获取我们的输出流
final OutputStream output = response.getOutputStream();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + outFileName + ";filename*=utf-8"
+ URLEncoder.encode(outFileName, ENCODING));
writer.flush(output, true);
writer.close();
// 这里可以自行关闭资源或者写一个关闭资源的工具类
IoUtil.close(output);
} catch (Exception e) {
log.error("导出失败:", e);
}
return null;
}
private List<LawsWorkingGroupUser> queryDataByUserId(String userId) {
return lawsWorkingGroupUserService.listByIds(Arrays.asList(userId.split(",")));
}
private List<LawsWorkingGroupUser> queryDataByConditions(String userName, String userWorkNo, String userRoleName, String userPostName, String workingGroupId) {
List<String> ids = lawsWorkingGroupUserService.lawsWorkingGroupUserSearch(userName, userWorkNo, userRoleName, userPostName);
List<LawsWorkingGroupUser> list = new ArrayList<>();
if (!ids.isEmpty()) {
LambdaQueryWrapper<LawsWorkingGroupUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LawsWorkingGroupUser::getWorkingGroupId, workingGroupId);
wrapper.eq(LawsWorkingGroupUser::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
wrapper.in(LawsWorkingGroupUser::getUserId, ids);
list = lawsWorkingGroupUserService.list(wrapper);
}
return list;
}
private List<LawsWorkingGroupUser> queryDataByTree(String workingGroupId) {
LambdaQueryWrapper<LawsWorkingGroupUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LawsWorkingGroupUser::getWorkingGroupId, workingGroupId);
wrapper.eq(LawsWorkingGroupUser::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
return lawsWorkingGroupUserService.list(wrapper);
}
private List<LawsWorkingGroupUser> queryDataByUserId(String userId) {
LambdaQueryWrapper<LawsWorkingGroupUser> queryLawsWorkingGroupUser = new LambdaQueryWrapper<>();
queryLawsWorkingGroupUser.in(LawsWorkingGroupUser::getUserId,Arrays.asList(userId.split(",")));
queryLawsWorkingGroupUser.orderByDesc(LawsWorkingGroupUser::getCreateTime);
return lawsWorkingGroupUserService.list(queryLawsWorkingGroupUser);
}
private List<LawsWorkingGroupUser> queryDataByConditions(String userName, String userWorkNo, String userRoleName, String userPostName, String workingGroupId) {
List<String> ids = lawsWorkingGroupUserService.lawsWorkingGroupUserSearch(userName, userWorkNo, userRoleName, userPostName);
List<LawsWorkingGroupUser> list = new ArrayList<>();
if (!ids.isEmpty()) {
LambdaQueryWrapper<LawsWorkingGroupUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LawsWorkingGroupUser::getWorkingGroupId, workingGroupId);
wrapper.eq(LawsWorkingGroupUser::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
wrapper.in(LawsWorkingGroupUser::getUserId, ids);
wrapper.orderByDesc(LawsWorkingGroupUser::getCreateTime);
list = lawsWorkingGroupUserService.list(wrapper);
}
return list;
}
/**
* 校验表头,同时映射对象字段
*/
private boolean validateHeader(ExcelReader excelReader) {
List<Map<String, Object>> read = excelReader.read(0, 1, 1);
Map<String, Object> map = read.get(0);
assembleReader(excelReader);
Map<String, String> headerAlias = excelReader.getHeaderAlias();
boolean isSameKeys = map.keySet().stream().allMatch(key -> headerAlias.containsKey(key));
return isSameKeys;
}
private List<LawsWorkingGroupUser> queryDataByTree(String workingGroupId) {
LambdaQueryWrapper<LawsWorkingGroupUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LawsWorkingGroupUser::getWorkingGroupId, workingGroupId);
wrapper.eq(LawsWorkingGroupUser::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
wrapper.orderByDesc(LawsWorkingGroupUser::getCreateTime);
return lawsWorkingGroupUserService.list(wrapper);
}
/**
* 导入数据校验错误信息
*/
public String importExcelCheck(List<LawsWorkingGroupUser> all) {
List<String> errorMessages = validateWorkNo(all);
return String.join(";", errorMessages);
}
private List<String> validateWorkNo(List<LawsWorkingGroupUser> all) {
List<SysUser> listUser = sysUserService.list();
all.stream().forEach(p->{
p.setUsername("");
if(!CollectionUtils.isEmpty(listUser)){
Optional<SysUser> op = listUser.stream().filter(o->Objects.equals(o.getUsername(),p.getUsername())).findFirst();
if(op.isPresent()){
p.setUsername(op.get().getId());
}
}
});
Set<String> existingWorkNos = lawsWorkingGroupUserService.list().stream().map(LawsWorkingGroupUser::getUserId).collect(Collectors.toSet());
return IntStream.range(0, all.size())
.mapToObj(i -> {
LawsWorkingGroupUser lawsWorkingGroupUser = all.get(i);
String username = lawsWorkingGroupUser.getUsername();
if (username == null || username.trim().isEmpty()) {
return MessageUtils.getMessage(ResultCommon.THE_JOB_NUMBER_CANNOT_BE_EMPTY_EXPORT,(i+1));
}
if (!isValidWorkNo(username)) {
return MessageUtils.getMessage(ResultCommon.JOB_NUMBER_DOES_NOT_EXIST_EXPORT,(i+1));
}
if (!existingWorkNos.add(username)) {
return MessageUtils.getMessage(ResultCommon.DUPLICATE_JOB_NUMBER_EXPORT,(i+1));
}
return null;
}).filter(error -> error != null).collect(Collectors.toList());
}
/**
* 判断工号在用户表中是否存在
*/
private boolean isValidWorkNo(String userWorkNo) {
SysUser sysUser = sysUserService.getUserByName(userWorkNo);
return sysUser != null;
}
/**
* 下载模板
*/
@AutoLog(value = "内部工作组人员关联表-下载模板")
@ApiOperation(value="内部工作组人员关联表-下载模板", notes="内部工作组人员关联表-下载模板")
@RequestMapping(value = "/download/template", method = RequestMethod.GET)
public void template(HttpServletResponse response) {
ExcelWriter writer = ExcelUtil.getWriter(true);
List<String> headList = new ArrayList<>();
headList.add("姓名");
headList.add("工号");
headList.add("角色");
headList.add("岗位");
//表格高度
writer.setDefaultRowHeight(18);
//表格宽度
writer.setColumnWidth(-1, 20);
try {
writer.writeHeadRow(headList).write(Collections.emptyList());
//设置content—type
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
//Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。
response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode("内部工作组人员表模板","UTF-8")+".xlsx");
ServletOutputStream outputStream= response.getOutputStream();
//将Writer刷新到OutPut
writer.flush(outputStream,true);
outputStream.close();
writer.close();
} catch (Exception e) {
log.error("下载模板失败:", e);
}
}
//assembleWriter方法和assembleReader方法可以通过实体类加@Alias注解实现,这里为了避免导入导出数据不统一,使用硬编码实现
private void assembleWriter(ExcelWriter writer) {
writer.addHeaderAlias("realname", "姓名");
writer.addHeaderAlias("username", "工号");
writer.addHeaderAlias("userRoleName", "角色");
writer.addHeaderAlias("userPostName", "岗位");
}
private void assembleReader(ExcelReader reader) {
reader.addHeaderAlias("姓名", "realname");
reader.addHeaderAlias("工号", "username");
reader.addHeaderAlias("角色", "userRoleName");
reader.addHeaderAlias("岗位", "userPostName");
}
/**
* 导入数据校验错误信息
*/
public String importExcelCheck(List<LawsWorkingGroupUser> all,String workingGroupId) {
List<String> errorMessages = validateWorkNo(all,workingGroupId);
return String.join(";", errorMessages);
}
private List<String> validateWorkNo(List<LawsWorkingGroupUser> all,String workingGroupId) {
List<SysUser> listSysUser = sysUserService.list();
LambdaQueryWrapper<LawsWorkingGroupUser> queryLawsWorkingGroupUser = new LambdaQueryWrapper<>();
queryLawsWorkingGroupUser.eq(LawsWorkingGroupUser::getWorkingGroupId,workingGroupId);
Set<String> existingWorkNos = lawsWorkingGroupUserService.list(queryLawsWorkingGroupUser).stream().map(LawsWorkingGroupUser::getUserId).collect(Collectors.toSet());
return IntStream.range(0, all.size())
.mapToObj(i -> {
LawsWorkingGroupUser lawsWorkingGroupUser = all.get(i);
String username = lawsWorkingGroupUser.getUsername();
String realname = lawsWorkingGroupUser.getRealname();
if (StringUtils.isBlank(username)) {
return MessageUtils.getMessage(ResultCommon.THE_JOB_NUMBER_CANNOT_BE_EMPTY_EXPORT, (i + 1));
}
if (StringUtils.isBlank(realname)) {
return "第(" + (i + 1) + ")行,姓名不能为空";
}
if(!CollectionUtils.isEmpty(listSysUser)){
String finalUsername = username;
Optional<SysUser> op = listSysUser.stream().filter(o->Objects.equals(o.getUsername(), finalUsername)).findFirst();
if(!op.isPresent()){
return MessageUtils.getMessage(ResultCommon.JOB_NUMBER_DOES_NOT_EXIST_EXPORT, (i + 1));
}
username = op.get().getId();
if(!Objects.equals(op.get().getRealname(),realname)){
return "第(" + (i + 1) + ")行,姓名与工号不匹配";
}
}
if (!existingWorkNos.add(username)) {
return MessageUtils.getMessage(ResultCommon.DUPLICATE_JOB_NUMBER_EXPORT, (i + 1));
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
}
/**
* 下载模板
*/
@AutoLog(value = "内部工作组人员关联表-下载模板")
@ApiOperation(value = "内部工作组人员关联表-下载模板", notes = "内部工作组人员关联表-下载模板")
@GetMapping(value = "/download/template")
public ModelAndView template() {
// AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
String title = "内部工作组人员表模板";
//此处设置的filename无效 ,前端会重更新设置一下
mv.addObject(FILE_NAME, title);
mv.addObject(CLASS, LawsWorkingGroupUserImport.class);
ExportParams exportParams = new ExportParams(null, title);
// 导出xlsx
exportParams.setType(ExcelType.XSSF);
mv.addObject(PARAMS, exportParams);
mv.addObject(NormalExcelConstants.DATA_LIST, new ArrayList<T>());
return mv;
}
}
@@ -4,10 +4,7 @@ import java.io.Serializable;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
@@ -74,6 +71,7 @@ public class LawsWorkingGroup implements Serializable {
/**删除状态(0-正常,1-删除)*/
@ApiModelProperty(value = "删除状态(0-正常,1-删除)")
@TableLogic
private String delFlag;
@@ -3,10 +3,7 @@ package com.jero.modules.sys.entity;
import java.io.Serializable;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.jeecgframework.poi.excel.annotation.Excel;
@@ -70,25 +67,26 @@ public class LawsWorkingGroupUser implements Serializable {
/**删除状态(0-正常,1-删除)*/
@ApiModelProperty(value = "删除状态(0-正常,1-删除)")
@TableLogic
private String delFlag;
@TableField(exist = false)
@ApiModelProperty(value = "姓名")
@Excel(name = "姓名")
@Excel(name = "姓名",width = 15)
private String realname;
@TableField(exist = false)
@ApiModelProperty(value = "工号")
@Excel(name = "工号")
@Excel(name = "工号",width = 15)
private String username;
@TableField(exist = false)
@ApiModelProperty(value = "角色")
@Excel(name = "角色")
@Excel(name = "角色",width = 15)
private String userRoleName;
@TableField(exist = false)
@ApiModelProperty(value = "岗位")
@Excel(name = "岗位")
@Excel(name = "岗位",width = 15)
private String userPostName;
}
@@ -0,0 +1,30 @@
package com.jero.modules.sys.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import java.io.Serializable;
/**
* @Description: 内部工作组人员导入
* @Author: jero-boot
* @Date: 2023-09-06
* @Version: V1.0
*/
@Data
public class LawsWorkingGroupUserImport implements Serializable {
private static final long serialVersionUID = 1L;
@TableField(exist = false)
@ApiModelProperty(value = "姓名")
@Excel(name = "姓名",width = 15)
private String realname;
@TableField(exist = false)
@ApiModelProperty(value = "工号")
@Excel(name = "工号",width = 15)
private String username;
}