SysUserController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package com.ruoyi.web.controller.system;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.convert.Convert;
  5. import cn.hutool.core.io.FileUtil;
  6. import cn.hutool.core.util.ArrayUtil;
  7. import cn.hutool.core.util.ObjectUtil;
  8. import cn.hutool.core.util.StrUtil;
  9. import cn.hutool.json.JSONArray;
  10. import cn.hutool.json.JSONObject;
  11. import cn.hutool.json.JSONUtil;
  12. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  13. import com.ruoyi.common.annotation.Log;
  14. import com.ruoyi.common.annotation.Security;
  15. import com.ruoyi.common.core.controller.BaseController;
  16. import com.ruoyi.common.core.domain.AjaxResult;
  17. import com.ruoyi.common.core.domain.entity.SysDept;
  18. import com.ruoyi.common.core.domain.entity.SysRole;
  19. import com.ruoyi.common.core.domain.entity.SysUser;
  20. import com.ruoyi.common.core.page.TableDataInfo;
  21. import com.ruoyi.common.enums.BusinessType;
  22. import com.ruoyi.common.exception.ServiceException;
  23. import com.ruoyi.common.utils.SecurityUtils;
  24. import com.ruoyi.common.utils.StringUtils;
  25. import com.ruoyi.common.utils.UserUtil;
  26. import com.ruoyi.common.utils.poi.ExcelUtil;
  27. import com.ruoyi.system.domain.vo.SysUserExportVo;
  28. import com.ruoyi.system.domain.vo.SysUserImportVo;
  29. import com.ruoyi.system.service.ISysDeptService;
  30. import com.ruoyi.system.service.ISysRoleService;
  31. import com.ruoyi.system.service.ISysUserService;
  32. import com.ruoyi.zhdd.domain.GatewayUserDetail;
  33. import com.ruoyi.zhdd.domain.GatewayUserPage;
  34. import com.ruoyi.zhdd.domain.Resource;
  35. import com.ruoyi.zhdd.domain.bo.ResourceBo;
  36. import com.ruoyi.zhdd.feign.FeignUserManageService;
  37. import com.ruoyi.zhdd.service.IResourceService;
  38. import lombok.Data;
  39. import lombok.extern.slf4j.Slf4j;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.validation.annotation.Validated;
  42. import org.springframework.web.bind.annotation.*;
  43. import org.springframework.web.multipart.MultipartFile;
  44. import javax.servlet.http.HttpServletResponse;
  45. import java.util.ArrayList;
  46. import java.util.Date;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Map;
  50. import java.util.stream.Collectors;
  51. /**
  52. * 用户信息
  53. *
  54. * @author ruoyi
  55. */
  56. @RestController
  57. @RequestMapping("/system/user")
  58. @Slf4j
  59. public class SysUserController extends BaseController {
  60. @Autowired
  61. private ISysUserService userService;
  62. @Autowired
  63. private ISysRoleService roleService;
  64. @Autowired
  65. private FeignUserManageService feignUserManageService;
  66. @Autowired
  67. private ISysDeptService deptService;
  68. @Autowired
  69. private IResourceService resourceService;
  70. /**
  71. * 获取用户列表
  72. */
  73. @GetMapping("/list")
  74. public TableDataInfo list(SysUser user) {
  75. return userService.selectPageUserList(user);
  76. }
  77. @Log(title = "用户管理", businessType = BusinessType.EXPORT)
  78. @GetMapping("/export")
  79. public void export(SysUser user, HttpServletResponse response) {
  80. List<SysUser> list = userService.selectUserList(user);
  81. List<SysUserExportVo> listVo = BeanUtil.copyToList(list, SysUserExportVo.class);
  82. for (int i = 0; i < list.size(); i++) {
  83. SysDept dept = list.get(i).getDept();
  84. SysUserExportVo vo = listVo.get(i);
  85. if (ObjectUtil.isNotEmpty(dept)) {
  86. vo.setDeptName(dept.getDeptName());
  87. vo.setLeader(dept.getLeader());
  88. }
  89. }
  90. ExcelUtil.exportExcel(listVo, "用户数据", SysUserExportVo.class, response);
  91. }
  92. @Log(title = "用户管理", businessType = BusinessType.IMPORT)
  93. @PostMapping("/importData")
  94. public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
  95. List<SysUserImportVo> userListVo = ExcelUtil.importExcel(file.getInputStream(), SysUserImportVo.class);
  96. List<SysUser> userList = BeanUtil.copyToList(userListVo, SysUser.class);
  97. String operName = getUsername();
  98. String message = userService.importUser(userList, updateSupport, operName);
  99. return AjaxResult.success(message);
  100. }
  101. @GetMapping("/importTemplate")
  102. public void importTemplate(HttpServletResponse response) {
  103. ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", SysUserImportVo.class, response);
  104. }
  105. /**
  106. * 根据用户编号获取详细信息
  107. */
  108. @GetMapping(value = {"/", "/{userId}"})
  109. public AjaxResult getInfo(@PathVariable(value = "userId", required = false) String userId) {
  110. // userService.checkUserDataScope(userId);
  111. Map<String, Object> ajax = new HashMap<>();
  112. List<SysRole> roles = roleService.selectRoleAll();
  113. ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
  114. // ajax.put("posts", postService.selectPostAll());
  115. ajax.put("posts", new ArrayList<>());
  116. if (StringUtils.isNotNull(userId)) {
  117. ajax.put("user", userService.selectUserById(userId));
  118. // ajax.put("postIds", postService.selectPostListByUserId(userId));
  119. ajax.put("postIds", new ArrayList<>());
  120. ajax.put("roleIds", roleService.selectRoleListByUserId(userId));
  121. }
  122. return AjaxResult.success(ajax);
  123. }
  124. /**
  125. * 新增用户
  126. */
  127. // @Log(title = "用户管理", businessType = BusinessType.INSERT)
  128. // @PostMapping
  129. // public AjaxResult add(@Validated @RequestBody SysUser user) {
  130. // if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
  131. // return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
  132. // } else if (StringUtils.isNotEmpty(user.getPhonenumber())
  133. // && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
  134. // return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
  135. // } else if (StringUtils.isNotEmpty(user.getEmail())
  136. // && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
  137. // return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在");
  138. // }
  139. // user.setCreateBy(getUsername());
  140. // user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  141. // return toAjax(userService.insertUser(user));
  142. // }
  143. /**
  144. * 修改用户
  145. */
  146. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  147. @PutMapping
  148. @Security
  149. public AjaxResult edit(@Validated @RequestBody SysUser user) {
  150. /*userService.checkUserAllowed(user);
  151. if (StringUtils.isNotEmpty(user.getPhonenumber())
  152. && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
  153. return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
  154. } else if (StringUtils.isNotEmpty(user.getEmail())
  155. && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
  156. return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
  157. }*/
  158. user.setUpdateBy(UserUtil.getCacheLoginUser().getUsername());
  159. return toAjax(userService.updateUser(user));
  160. }
  161. /**
  162. * 删除用户
  163. */
  164. @Log(title = "用户管理", businessType = BusinessType.DELETE)
  165. @DeleteMapping("/{userIds}")
  166. @Security
  167. public AjaxResult remove(@PathVariable String[] userIds) {
  168. if (ArrayUtil.contains(userIds, getUserId())) {
  169. return error("当前用户不能删除");
  170. }
  171. return toAjax(userService.deleteUserByIds(userIds));
  172. }
  173. /**
  174. * 重置密码
  175. */
  176. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  177. @PutMapping("/resetPwd")
  178. public AjaxResult resetPwd(@RequestBody SysUser user) {
  179. userService.checkUserAllowed(user);
  180. user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  181. user.setUpdateBy(getUsername());
  182. return toAjax(userService.resetPwd(user));
  183. }
  184. /**
  185. * 状态修改
  186. */
  187. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  188. @PutMapping("/changeStatus")
  189. public AjaxResult changeStatus(@RequestBody SysUser user) {
  190. userService.checkUserAllowed(user);
  191. user.setUpdateBy(getUsername());
  192. return toAjax(userService.updateUserStatus(user));
  193. }
  194. /**
  195. * 根据用户编号获取授权角色
  196. */
  197. @GetMapping("/authRole/{userId}")
  198. public AjaxResult authRole(@PathVariable("userId") String userId) {
  199. SysUser user = userService.selectUserById(userId);
  200. List<SysRole> roles = roleService.selectRolesByUserId(userId);
  201. Map<String, Object> ajax = new HashMap<>();
  202. ajax.put("user", user);
  203. ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
  204. return AjaxResult.success(ajax);
  205. }
  206. /**
  207. * 用户授权角色
  208. */
  209. @Log(title = "用户管理", businessType = BusinessType.GRANT)
  210. @PutMapping("/authRole")
  211. @Security
  212. public AjaxResult insertAuthRole(String userId, String[] roleIds) {
  213. userService.insertUserAuth(userId, roleIds);
  214. return success();
  215. }
  216. @PostMapping("/syncUc")
  217. @Security
  218. public void syncUc() {
  219. List<SysUser> list = userService.list();
  220. for (SysUser sysUser : list) {
  221. this.edit(sysUser);
  222. }
  223. }
  224. @GetMapping("/searchGatewayUser")
  225. @Security
  226. public AjaxResult searchGatewayUser(@RequestParam String name) {
  227. GatewayUserPage gatewayUserPage = feignUserManageService.userPage(name, 1, 500);
  228. log.info("获取网关用户数据:{}", gatewayUserPage);
  229. List<GatewayUserPage.UserDetail> users = new ArrayList<>();
  230. if (gatewayUserPage != null && gatewayUserPage.getCode() == 200) {
  231. int total = gatewayUserPage.getData().getTotal();
  232. if (total > 0) {
  233. users = gatewayUserPage.getData().getRows();
  234. for (GatewayUserPage.UserDetail row : users) {
  235. // 查询本系统是否存在该用户
  236. SysUser sysUser = userService.selectUserById(row.getUserId());
  237. if (sysUser != null) {
  238. row.setLocalFlag("1");
  239. } else {
  240. row.setLocalFlag("0");
  241. }
  242. }
  243. }
  244. }
  245. return AjaxResult.success(users);
  246. }
  247. @PostMapping("/syncGatewayUser")
  248. @Security
  249. public AjaxResult<Void> syncGatewayUser(@RequestBody JSONObject jsonObject) {
  250. String userId = jsonObject.getStr("userId");
  251. if (StrUtil.isBlank(userId)) {
  252. return AjaxResult.error("用户id不能为空!");
  253. }
  254. // 先查询本系统是否存在
  255. SysUser sysUser = userService.selectUserById(userId);
  256. if (sysUser != null) {
  257. return AjaxResult.error("系统已存在,请在本系统管理该用户!");
  258. }
  259. // 查询网关用户信息,并保存到系统
  260. GatewayUserDetail gatewayUserDetail = feignUserManageService.userDetail(userId);
  261. log.info("四维用户详情:{}", gatewayUserDetail);
  262. // 如果非空则新增到数据库
  263. if (gatewayUserDetail.getCode() == 200 && gatewayUserDetail.getData() != null && StrUtil.isNotBlank(gatewayUserDetail.getData().getUserId())) {
  264. GatewayUserDetail.UserDetail data = gatewayUserDetail.getData();
  265. SysUser newUser = new SysUser();
  266. // 如果是企业用户,则查询是否存在企业信息,存在则新增企业部门,同时往应急仓库新增一个数据
  267. if (data.getUserGroupId() == 4) {
  268. GatewayUserDetail.EntInfo entInfo = data.getEntInfo();
  269. if (entInfo == null || StrUtil.isBlank(entInfo.getEntName()) || StrUtil.isBlank(entInfo.getOwnerId()) || StrUtil.isBlank(entInfo.getOwnerCategory())) {
  270. throw new ServiceException("企业用户未绑定企业,不能使用此系统");
  271. }
  272. newUser.setDeptId(entInfo.getOwnerId());
  273. newUser.setOrgId(entInfo.getOwnerId());
  274. SysDept sysDept = deptService.selectDeptById(entInfo.getOwnerId());
  275. String locationType = "2";
  276. if (sysDept == null) {
  277. sysDept = new SysDept();
  278. sysDept.setDeptId(entInfo.getOwnerId());
  279. sysDept.setDeptName(entInfo.getEntName());
  280. sysDept.setFlag("0");
  281. // 根据企业类型放置不同的目录中
  282. if ("危货业户".equals(entInfo.getOwnerCategory())) {
  283. sysDept.setParentId("10001");
  284. } else if ("普货业户".equals(entInfo.getOwnerCategory())) {
  285. sysDept.setParentId("10002");
  286. } else if ("客运业户".equals(entInfo.getOwnerCategory())) {
  287. sysDept.setParentId("10003");
  288. } else if ("出租车业户".equals(entInfo.getOwnerCategory())) {
  289. sysDept.setParentId("10004");
  290. } else if ("公交运输业户".equals(entInfo.getOwnerCategory())) {
  291. sysDept.setParentId("10005");
  292. } else if ("汽车租赁业户".equals(entInfo.getOwnerCategory())) {
  293. sysDept.setParentId("10006");
  294. } else if ("检测站业户".equals(entInfo.getOwnerCategory())) {
  295. sysDept.setParentId("10007");
  296. } else if ("网约车平台".equals(entInfo.getOwnerCategory())) {
  297. sysDept.setParentId("10008");
  298. } else if ("维修站".equals(entInfo.getOwnerCategory())) {
  299. sysDept.setParentId("10009");
  300. } else if ("其他".equals(entInfo.getOwnerCategory())) {
  301. sysDept.setParentId("10010");
  302. } else if ("公路工程".equals(entInfo.getOwnerCategory())) {
  303. sysDept.setParentId("10011");
  304. } else if ("水运工程".equals(entInfo.getOwnerCategory())) {
  305. sysDept.setParentId("10012");
  306. locationType = "1";
  307. } else if ("航运企业".equals(entInfo.getOwnerCategory())) {
  308. sysDept.setParentId("10013");
  309. locationType = "1";
  310. } else if ("港口码头".equals(entInfo.getOwnerCategory())) {
  311. sysDept.setParentId("10014");
  312. locationType = "1";
  313. } else if ("水上游览经营".equals(entInfo.getOwnerCategory())) {
  314. sysDept.setParentId("10015");
  315. locationType = "1";
  316. } else if ("货运场站".equals(entInfo.getOwnerCategory())) {
  317. sysDept.setParentId("10016");
  318. }
  319. deptService.insertDept(sysDept);
  320. }
  321. List<Resource> resourcesList = resourceService.list(Wrappers.<Resource>lambdaQuery().eq(Resource::getManageUnitId, entInfo.getOwnerId()));
  322. if (resourcesList == null || resourcesList.size() == 0) {
  323. // 新增应急仓库
  324. ResourceBo resource = new ResourceBo();
  325. resource.setAdminOrgName(entInfo.getAdminOrgName());
  326. resource.setResourceType(1);
  327. resource.setName(entInfo.getEntName() + "仓库");
  328. resource.setManageUnit(entInfo.getEntName());
  329. resource.setContactName(data.getName());
  330. resource.setContactPhone(data.getMobile());
  331. resource.setLocationType(locationType);
  332. resource.setManageUnitId(entInfo.getOwnerId());
  333. // 默认为宿迁市交通局坐标
  334. resource.setLatitude("33.961569");
  335. resource.setLongitude("118.260139");
  336. resourceService.insertByBo(resource);
  337. }
  338. }
  339. newUser.setUserId(data.getUserId());
  340. newUser.setUserName(data.getUserName());
  341. newUser.setNickName(data.getName());
  342. newUser.setUserType(Convert.toStr(data.getUserGroupId()));
  343. newUser.setPhonenumber(data.getMobile());
  344. newUser.setPassword(SecurityUtils.encryptPassword("tocc!suqian"));
  345. newUser.setStatus("0");
  346. newUser.setDelFlag("0");
  347. newUser.setUpdateBy("gateway");
  348. newUser.setUpdateTime(new Date());
  349. newUser.setCreateBy("gateway");
  350. newUser.setCreateTime(new Date());
  351. userService.insertUser(newUser);
  352. } else {
  353. return AjaxResult.error("网关未查询到该用户信息!");
  354. }
  355. return AjaxResult.success();
  356. }
  357. @GetMapping("/compareDept")
  358. public void compareDept() {
  359. String s = FileUtil.readUtf8String("/Users/huangcheng/Desktop/siweidept");
  360. JSONArray jsonArray = JSONUtil.parseArray(s);
  361. List<SiweiDept> siweiDepts = JSONUtil.toList(jsonArray, SiweiDept.class);
  362. List<SysDept> list = deptService.list();
  363. /////////////////////////
  364. List<String> siweiIds = siweiDepts.stream().map(SiweiDept::getId).collect(Collectors.toList());
  365. List<String> hsIds = list.stream().map(SysDept::getDeptId).collect(Collectors.toList());
  366. System.out.println("四维存在,华设不存在" + CollUtil.subtract(siweiIds, hsIds));
  367. System.out.println("四维不存在,华设存在" + CollUtil.subtract(hsIds, siweiIds));
  368. /////////////////////////
  369. Map<String, SiweiDept> siweiDeptIds = siweiDepts.stream().collect(Collectors.toMap(SiweiDept::getId, a -> a));
  370. for (SysDept sysDept : list) {
  371. String deptid = sysDept.getDeptId();
  372. SiweiDept siweiDept = siweiDeptIds.get(deptid);
  373. if (siweiDept != null) {
  374. if (StrUtil.isBlank(siweiDept.getParentId())) {
  375. siweiDept.setParentId("0");
  376. }
  377. if (StrUtil.isNotBlank(siweiDept.getFullName())) {
  378. siweiDept.setFullName(siweiDept.getFullName().replace("-", "/"));
  379. }
  380. try {
  381. if (!sysDept.getParentId().equals(siweiDept.getParentId()) || !sysDept.getDeptName().equals(siweiDept.getName()) || !sysDept.getFlag().equals(Convert.toStr(siweiDept.getFlag())) || !sysDept.getFullName().equals(siweiDept.getFullName())) {
  382. System.out.println("华设:" + sysDept.getDeptId() + "-" + sysDept.getParentId() + "-" + sysDept.getDeptName() + "-" + sysDept.getFullName());
  383. System.out.println("四维:" + siweiDept.getId() + "-" + siweiDept.getParentId() + "-" + siweiDept.getName() + "-" + siweiDept.getFullName());
  384. }
  385. // if (!sysDept.getDeptName().equals(siweiDept.getName())) {
  386. // System.out.println("华设:" + sysDept.getDeptId() + "-" + sysDept.getDeptName());
  387. // System.out.println("四维:" + siweiDept.getId() + "-" + siweiDept.getName());
  388. // }
  389. // if (!sysDept.getParentId().equals(siweiDept.getParentId()) || !sysDept.getFullName().equals(siweiDept.getFullName())) {
  390. // System.out.println("华设:" + sysDept.getDeptId() + "-" + sysDept.getParentId() + "-" + sysDept.getFullName());
  391. // System.out.println("四维:" + siweiDept.getId() + "-" + siweiDept.getParentId() + "-" + siweiDept.getFullName());
  392. // }
  393. } catch (Exception e) {
  394. System.out.println("错误华设:" + sysDept);
  395. System.out.println("错误四维:" + siweiDept);
  396. }
  397. }
  398. }
  399. }
  400. @Data
  401. public static class SiweiDept {
  402. private String id;
  403. private String name;
  404. private String parentId;
  405. private Integer flag;
  406. private String fullName;
  407. }
  408. // public static void main(String[] args) {
  409. // String s = FileUtil.readUtf8String("/Users/huangcheng/Desktop/temp");
  410. // JSONArray jsonArray = JSONUtil.parseArray(s);
  411. // List<SiweiDept> siweiDepts = JSONUtil.toList(jsonArray, SiweiDept.class);
  412. // for (SiweiDept siweiDept : siweiDepts) {
  413. // System.out.println(StrUtil.concat(true, "INSERT INTO sys_dept (dept_id, parent_id, ancestors, dept_name, flag, full_name) VALUES ('",
  414. // siweiDept.getId(),
  415. // "', '",
  416. // siweiDept.getParentId(),
  417. // "', '0,35e6872a8c46440fbca1b1c01862a380,7298cf74a4ae4b0d9181286fa6647266', '",
  418. // siweiDept.getName(),
  419. // "', '1', '",
  420. // siweiDept.getFullName().replace("-", "/"),
  421. // "');"));
  422. // }
  423. // }
  424. }