package com.ruoyi.web.controller.system; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.Security; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.entity.SysRole; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.UserUtil; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.domain.vo.SysUserExportVo; import com.ruoyi.system.domain.vo.SysUserImportVo; import com.ruoyi.system.service.ISysDeptService; import com.ruoyi.system.service.ISysRoleService; import com.ruoyi.system.service.ISysUserService; import com.ruoyi.zhdd.domain.GatewayUserDetail; import com.ruoyi.zhdd.domain.GatewayUserPage; import com.ruoyi.zhdd.domain.Resource; import com.ruoyi.zhdd.domain.bo.ResourceBo; import com.ruoyi.zhdd.feign.FeignUserManageService; import com.ruoyi.zhdd.service.IResourceService; import lombok.Data; import lombok.extern.slf4j.Slf4j; 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 javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 用户信息 * * @author ruoyi */ @RestController @RequestMapping("/system/user") @Slf4j public class SysUserController extends BaseController { @Autowired private ISysUserService userService; @Autowired private ISysRoleService roleService; @Autowired private FeignUserManageService feignUserManageService; @Autowired private ISysDeptService deptService; @Autowired private IResourceService resourceService; /** * 获取用户列表 */ @GetMapping("/list") public TableDataInfo list(SysUser user) { return userService.selectPageUserList(user); } @Log(title = "用户管理", businessType = BusinessType.EXPORT) @GetMapping("/export") public void export(SysUser user, HttpServletResponse response) { List list = userService.selectUserList(user); List listVo = BeanUtil.copyToList(list, SysUserExportVo.class); for (int i = 0; i < list.size(); i++) { SysDept dept = list.get(i).getDept(); SysUserExportVo vo = listVo.get(i); if (ObjectUtil.isNotEmpty(dept)) { vo.setDeptName(dept.getDeptName()); vo.setLeader(dept.getLeader()); } } ExcelUtil.exportExcel(listVo, "用户数据", SysUserExportVo.class, response); } @Log(title = "用户管理", businessType = BusinessType.IMPORT) @PostMapping("/importData") public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { List userListVo = ExcelUtil.importExcel(file.getInputStream(), SysUserImportVo.class); List userList = BeanUtil.copyToList(userListVo, SysUser.class); String operName = getUsername(); String message = userService.importUser(userList, updateSupport, operName); return AjaxResult.success(message); } @GetMapping("/importTemplate") public void importTemplate(HttpServletResponse response) { ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", SysUserImportVo.class, response); } /** * 根据用户编号获取详细信息 */ @GetMapping(value = {"/", "/{userId}"}) public AjaxResult getInfo(@PathVariable(value = "userId", required = false) String userId) { // userService.checkUserDataScope(userId); Map ajax = new HashMap<>(); List roles = roleService.selectRoleAll(); ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); // ajax.put("posts", postService.selectPostAll()); ajax.put("posts", new ArrayList<>()); if (StringUtils.isNotNull(userId)) { ajax.put("user", userService.selectUserById(userId)); // ajax.put("postIds", postService.selectPostListByUserId(userId)); ajax.put("postIds", new ArrayList<>()); ajax.put("roleIds", roleService.selectRoleListByUserId(userId)); } return AjaxResult.success(ajax); } /** * 新增用户 */ // @Log(title = "用户管理", businessType = BusinessType.INSERT) // @PostMapping // public AjaxResult add(@Validated @RequestBody SysUser user) { // if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) { // return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); // } else if (StringUtils.isNotEmpty(user.getPhonenumber()) // && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) { // return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,手机号码已存在"); // } else if (StringUtils.isNotEmpty(user.getEmail()) // && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) { // return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在"); // } // user.setCreateBy(getUsername()); // user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); // return toAjax(userService.insertUser(user)); // } /** * 修改用户 */ @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PutMapping @Security public AjaxResult edit(@Validated @RequestBody SysUser user) { /*userService.checkUserAllowed(user); if (StringUtils.isNotEmpty(user.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) { return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); } else if (StringUtils.isNotEmpty(user.getEmail()) && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) { return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在"); }*/ user.setUpdateBy(UserUtil.getCacheLoginUser().getUsername()); return toAjax(userService.updateUser(user)); } /** * 删除用户 */ @Log(title = "用户管理", businessType = BusinessType.DELETE) @DeleteMapping("/{userIds}") @Security public AjaxResult remove(@PathVariable String[] userIds) { if (ArrayUtil.contains(userIds, getUserId())) { return error("当前用户不能删除"); } return toAjax(userService.deleteUserByIds(userIds)); } /** * 重置密码 */ @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PutMapping("/resetPwd") public AjaxResult resetPwd(@RequestBody SysUser user) { userService.checkUserAllowed(user); user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); user.setUpdateBy(getUsername()); return toAjax(userService.resetPwd(user)); } /** * 状态修改 */ @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody SysUser user) { userService.checkUserAllowed(user); user.setUpdateBy(getUsername()); return toAjax(userService.updateUserStatus(user)); } /** * 根据用户编号获取授权角色 */ @GetMapping("/authRole/{userId}") public AjaxResult authRole(@PathVariable("userId") String userId) { SysUser user = userService.selectUserById(userId); List roles = roleService.selectRolesByUserId(userId); Map ajax = new HashMap<>(); ajax.put("user", user); ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); return AjaxResult.success(ajax); } /** * 用户授权角色 */ @Log(title = "用户管理", businessType = BusinessType.GRANT) @PutMapping("/authRole") @Security public AjaxResult insertAuthRole(String userId, String[] roleIds) { userService.insertUserAuth(userId, roleIds); return success(); } @PostMapping("/syncUc") @Security public void syncUc() { List list = userService.list(); for (SysUser sysUser : list) { this.edit(sysUser); } } @GetMapping("/searchGatewayUser") @Security public AjaxResult searchGatewayUser(@RequestParam String name) { GatewayUserPage gatewayUserPage = feignUserManageService.userPage(name, 1, 500); log.info("获取网关用户数据:{}", gatewayUserPage); List users = new ArrayList<>(); if (gatewayUserPage != null && gatewayUserPage.getCode() == 200) { int total = gatewayUserPage.getData().getTotal(); if (total > 0) { users = gatewayUserPage.getData().getRows(); for (GatewayUserPage.UserDetail row : users) { // 查询本系统是否存在该用户 SysUser sysUser = userService.selectUserById(row.getUserId()); if (sysUser != null) { row.setLocalFlag("1"); } else { row.setLocalFlag("0"); } } } } return AjaxResult.success(users); } @PostMapping("/syncGatewayUser") @Security public AjaxResult syncGatewayUser(@RequestBody JSONObject jsonObject) { String userId = jsonObject.getStr("userId"); if (StrUtil.isBlank(userId)) { return AjaxResult.error("用户id不能为空!"); } // 先查询本系统是否存在 SysUser sysUser = userService.selectUserById(userId); if (sysUser != null) { return AjaxResult.error("系统已存在,请在本系统管理该用户!"); } // 查询网关用户信息,并保存到系统 GatewayUserDetail gatewayUserDetail = feignUserManageService.userDetail(userId); log.info("四维用户详情:{}", gatewayUserDetail); // 如果非空则新增到数据库 if (gatewayUserDetail.getCode() == 200 && gatewayUserDetail.getData() != null && StrUtil.isNotBlank(gatewayUserDetail.getData().getUserId())) { GatewayUserDetail.UserDetail data = gatewayUserDetail.getData(); SysUser newUser = new SysUser(); // 如果是企业用户,则查询是否存在企业信息,存在则新增企业部门,同时往应急仓库新增一个数据 if (data.getUserGroupId() == 4) { GatewayUserDetail.EntInfo entInfo = data.getEntInfo(); if (entInfo == null || StrUtil.isBlank(entInfo.getEntName()) || StrUtil.isBlank(entInfo.getOwnerId()) || StrUtil.isBlank(entInfo.getOwnerCategory())) { throw new ServiceException("企业用户未绑定企业,不能使用此系统"); } newUser.setDeptId(entInfo.getOwnerId()); newUser.setOrgId(entInfo.getOwnerId()); SysDept sysDept = deptService.selectDeptById(entInfo.getOwnerId()); String locationType = "2"; if (sysDept == null) { sysDept = new SysDept(); sysDept.setDeptId(entInfo.getOwnerId()); sysDept.setDeptName(entInfo.getEntName()); sysDept.setFlag("0"); // 根据企业类型放置不同的目录中 if ("危货业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10001"); } else if ("普货业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10002"); } else if ("客运业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10003"); } else if ("出租车业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10004"); } else if ("公交运输业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10005"); } else if ("汽车租赁业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10006"); } else if ("检测站业户".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10007"); } else if ("网约车平台".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10008"); } else if ("维修站".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10009"); } else if ("其他".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10010"); } else if ("公路工程".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10011"); } else if ("水运工程".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10012"); locationType = "1"; } else if ("航运企业".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10013"); locationType = "1"; } else if ("港口码头".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10014"); locationType = "1"; } else if ("水上游览经营".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10015"); locationType = "1"; } else if ("货运场站".equals(entInfo.getOwnerCategory())) { sysDept.setParentId("10016"); } deptService.insertDept(sysDept); } List resourcesList = resourceService.list(Wrappers.lambdaQuery().eq(Resource::getManageUnitId, entInfo.getOwnerId())); if (resourcesList == null || resourcesList.size() == 0) { // 新增应急仓库 ResourceBo resource = new ResourceBo(); resource.setAdminOrgName(entInfo.getAdminOrgName()); resource.setResourceType(1); resource.setName(entInfo.getEntName() + "仓库"); resource.setManageUnit(entInfo.getEntName()); resource.setContactName(data.getName()); resource.setContactPhone(data.getMobile()); resource.setLocationType(locationType); resource.setManageUnitId(entInfo.getOwnerId()); // 默认为宿迁市交通局坐标 resource.setLatitude("33.961569"); resource.setLongitude("118.260139"); resourceService.insertByBo(resource); } } newUser.setUserId(data.getUserId()); newUser.setUserName(data.getUserName()); newUser.setNickName(data.getName()); newUser.setUserType(Convert.toStr(data.getUserGroupId())); newUser.setPhonenumber(data.getMobile()); newUser.setPassword(SecurityUtils.encryptPassword("tocc!suqian")); newUser.setStatus("0"); newUser.setDelFlag("0"); newUser.setUpdateBy("gateway"); newUser.setUpdateTime(new Date()); newUser.setCreateBy("gateway"); newUser.setCreateTime(new Date()); userService.insertUser(newUser); } else { return AjaxResult.error("网关未查询到该用户信息!"); } return AjaxResult.success(); } @GetMapping("/compareDept") public void compareDept() { String s = FileUtil.readUtf8String("/Users/huangcheng/Desktop/siweidept"); JSONArray jsonArray = JSONUtil.parseArray(s); List siweiDepts = JSONUtil.toList(jsonArray, SiweiDept.class); List list = deptService.list(); ///////////////////////// List siweiIds = siweiDepts.stream().map(SiweiDept::getId).collect(Collectors.toList()); List hsIds = list.stream().map(SysDept::getDeptId).collect(Collectors.toList()); System.out.println("四维存在,华设不存在" + CollUtil.subtract(siweiIds, hsIds)); System.out.println("四维不存在,华设存在" + CollUtil.subtract(hsIds, siweiIds)); ///////////////////////// Map siweiDeptIds = siweiDepts.stream().collect(Collectors.toMap(SiweiDept::getId, a -> a)); for (SysDept sysDept : list) { String deptid = sysDept.getDeptId(); SiweiDept siweiDept = siweiDeptIds.get(deptid); if (siweiDept != null) { if (StrUtil.isBlank(siweiDept.getParentId())) { siweiDept.setParentId("0"); } if (StrUtil.isNotBlank(siweiDept.getFullName())) { siweiDept.setFullName(siweiDept.getFullName().replace("-", "/")); } try { if (!sysDept.getParentId().equals(siweiDept.getParentId()) || !sysDept.getDeptName().equals(siweiDept.getName()) || !sysDept.getFlag().equals(Convert.toStr(siweiDept.getFlag())) || !sysDept.getFullName().equals(siweiDept.getFullName())) { System.out.println("华设:" + sysDept.getDeptId() + "-" + sysDept.getParentId() + "-" + sysDept.getDeptName() + "-" + sysDept.getFullName()); System.out.println("四维:" + siweiDept.getId() + "-" + siweiDept.getParentId() + "-" + siweiDept.getName() + "-" + siweiDept.getFullName()); } // if (!sysDept.getDeptName().equals(siweiDept.getName())) { // System.out.println("华设:" + sysDept.getDeptId() + "-" + sysDept.getDeptName()); // System.out.println("四维:" + siweiDept.getId() + "-" + siweiDept.getName()); // } // if (!sysDept.getParentId().equals(siweiDept.getParentId()) || !sysDept.getFullName().equals(siweiDept.getFullName())) { // System.out.println("华设:" + sysDept.getDeptId() + "-" + sysDept.getParentId() + "-" + sysDept.getFullName()); // System.out.println("四维:" + siweiDept.getId() + "-" + siweiDept.getParentId() + "-" + siweiDept.getFullName()); // } } catch (Exception e) { System.out.println("错误华设:" + sysDept); System.out.println("错误四维:" + siweiDept); } } } } @Data public static class SiweiDept { private String id; private String name; private String parentId; private Integer flag; private String fullName; } // public static void main(String[] args) { // String s = FileUtil.readUtf8String("/Users/huangcheng/Desktop/temp"); // JSONArray jsonArray = JSONUtil.parseArray(s); // List siweiDepts = JSONUtil.toList(jsonArray, SiweiDept.class); // for (SiweiDept siweiDept : siweiDepts) { // System.out.println(StrUtil.concat(true, "INSERT INTO sys_dept (dept_id, parent_id, ancestors, dept_name, flag, full_name) VALUES ('", // siweiDept.getId(), // "', '", // siweiDept.getParentId(), // "', '0,35e6872a8c46440fbca1b1c01862a380,7298cf74a4ae4b0d9181286fa6647266', '", // siweiDept.getName(), // "', '1', '", // siweiDept.getFullName().replace("-", "/"), // "');")); // } // } }