TmUserServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package com.fdkankan.fusion.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.fdkankan.fusion.common.PageInfo;
  6. import com.fdkankan.fusion.common.enums.IdPreEnum;
  7. import com.fdkankan.fusion.common.util.Base64Converter;
  8. import com.fdkankan.fusion.common.util.IdUtils;
  9. import com.fdkankan.fusion.config.SecurityUtil;
  10. import com.fdkankan.fusion.httpClient.response.FdkkLoginVo;
  11. import cn.hutool.core.collection.CollectionUtil;
  12. import com.alibaba.fastjson.JSONArray;
  13. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  14. import com.fdkankan.fusion.common.ResultCode;
  15. import com.fdkankan.fusion.common.util.RedisKeyUtil;
  16. import com.fdkankan.fusion.entity.*;
  17. import com.fdkankan.fusion.exception.BusinessException;
  18. import com.fdkankan.fusion.httpClient.client.FdKKClient;
  19. import com.fdkankan.fusion.httpClient.request.FdkkLoginRequest;
  20. import com.fdkankan.fusion.httpClient.response.FdkkResponse;
  21. import com.fdkankan.fusion.mapper.ITmUserMapper;
  22. import com.fdkankan.fusion.response.UserAddRequest;
  23. import com.fdkankan.fusion.service.*;
  24. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  25. import com.fdkankan.redis.util.RedisUtil;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.apache.ibatis.annotations.Param;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.stereotype.Service;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Set;
  33. import java.util.stream.Collectors;
  34. /**
  35. * <p>
  36. * 用户信息表 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2023-07-28
  41. */
  42. @Service
  43. public class TmUserServiceImpl extends ServiceImpl<ITmUserMapper, TmUser> implements ITmUserService {
  44. @Autowired
  45. ITmPermissionService tmPermissionService;
  46. @Autowired
  47. ITmUserRoleService tmUserRoleService;
  48. @Autowired
  49. ITmRoleService tmRoleService;
  50. @Autowired
  51. ITmRolePermissionService tmRolePermissionService;
  52. @Autowired
  53. ITmDepartmentService tmDepartmentService;
  54. @Autowired
  55. ITmCameraService tmCameraService;
  56. @Autowired
  57. FdKKClient fdKKClient;
  58. @Autowired
  59. RedisUtil redisUtil;
  60. @Autowired
  61. IUserService userService;
  62. @Override
  63. public void setRoleAndPermToRedis(TmUser tmUser) {
  64. String redisKeyMenu = String.format(RedisKeyUtil.MANAGE_PERM_USER, tmUser.getId());
  65. String redisKeyRole = String.format(RedisKeyUtil.MANAGE_ROLE_USER, tmUser.getId());
  66. List<TmUserRole> userRoles = tmUserRoleService.getByUserId(tmUser.getId());
  67. if(CollectionUtil.isEmpty(userRoles)){
  68. throw new BusinessException(ResultCode.NOT_ROLE);
  69. }
  70. List<String> roleIds = userRoles.stream().map(TmUserRole::getRoleId).collect(Collectors.toList());
  71. List<TmRole> tmRoles = tmRoleService.listByIds(roleIds);
  72. if(CollectionUtil.isEmpty(tmRoles)){
  73. throw new BusinessException(ResultCode.NOT_ROLE);
  74. }
  75. List<String> roleKeys = tmRoles.stream().map(TmRole::getRoleKey).collect(Collectors.toList());
  76. List<TmRolePermission> tmRolePermissions = tmRolePermissionService.getByRoleIds(roleIds);
  77. if(CollectionUtil.isEmpty(tmRolePermissions)){
  78. throw new BusinessException(ResultCode.NOT_PERMISSION);
  79. }
  80. List<String> permIds = tmRolePermissions.stream().map(TmRolePermission::getPermissionId).collect(Collectors.toList());
  81. List<TmPermission> tmPermissions = tmPermissionService.listByIds(permIds);
  82. if(CollectionUtil.isEmpty(tmPermissions)){
  83. throw new BusinessException(ResultCode.NOT_PERMISSION);
  84. }
  85. List<String> permsList = tmPermissions.stream().map(TmPermission::getPerms).collect(Collectors.toList());
  86. tmUser.setPermsList(permsList);
  87. tmUser.setRoleList(roleKeys);
  88. tmUser.setRoleIdList(roleIds);
  89. TmDepartment tmDepartment = tmDepartmentService.getById(tmUser.getDeptId());
  90. if(tmDepartment !=null){
  91. tmUser.setDeptName(tmDepartment.getName());
  92. tmUser.setDeptLevel(tmDepartment.getDeptType());
  93. }
  94. redisUtil.set(redisKeyMenu, JSONArray.toJSONString(permsList),RedisKeyUtil.tokenTime);
  95. redisUtil.set(redisKeyRole,JSONArray.toJSONString(roleKeys),RedisKeyUtil.tokenTime);
  96. }
  97. @Override
  98. public TmUser getByUserName(String phoneNum) {
  99. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  100. wrapper.eq(TmUser::getUserName,phoneNum);
  101. return this.getOne(wrapper);
  102. }
  103. @Override
  104. public void addUser(UserAddRequest param) {
  105. if (!StringUtils.isNoneBlank(param.getUserName(), param.getNickName(),
  106. param.getRoleId(), param.getPassword(),param.getDeptId())) {
  107. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  108. }
  109. TmUser tmUser = this.getByUserName(param.getUserName());
  110. if(tmUser != null){
  111. throw new BusinessException(ResultCode.USER_EXIST);
  112. }
  113. TmRole role = tmRoleService.getById(param.getRoleId());
  114. if(role == null){
  115. throw new BusinessException(ResultCode.NOT_ROLE);
  116. }
  117. TmDepartment tmDepartment = tmDepartmentService.getById(param.getDeptId());
  118. if(tmDepartment == null){
  119. throw new BusinessException(ResultCode.NOT_DEPT);
  120. }
  121. FdkkLoginRequest request = new FdkkLoginRequest(param.getUserName(),param.getPassword(),param.getPassword());
  122. FdkkResponse fdkkResponse = fdKKClient.fdkkRegister(request);
  123. if(fdkkResponse.getCode() == 0 ){
  124. this.addNewUser(param,role);
  125. return;
  126. }
  127. if(fdkkResponse.getCode() == 3008 ){
  128. this.addNewUser(param,role);
  129. throw new BusinessException(ResultCode.USER_IS_REGISTER);
  130. }
  131. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  132. }
  133. private void addNewUser(UserAddRequest param,TmRole tmRole) {
  134. TmUser tmUser = new TmUser();
  135. String id = IdUtils.genId(IdPreEnum.USER_PRE.getPre());
  136. tmUser.setId(id);
  137. tmUser.setDeptId(param.getDeptId());
  138. tmUser.setUserName(param.getUserName());
  139. tmUser.setNickName(param.getNickName());
  140. this.save(tmUser);
  141. tmUserRoleService.add(id,tmRole.getId());
  142. }
  143. @Override
  144. public void editUser(UserAddRequest param) {
  145. if (!StringUtils.isNoneBlank( param.getId())) {
  146. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  147. }
  148. TmUser tmUser = this.getById(param.getId());
  149. if(tmUser == null){
  150. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  151. }
  152. if(param.getDeptId() != null && !tmUser.getDeptId().equals(param.getDeptId())){
  153. List<TmCamera> cameras = tmCameraService.getByUserId(tmUser.getId());
  154. if(CollectionUtil.isNotEmpty(cameras)){
  155. throw new BusinessException(ResultCode.USER_HAVE_CAMERA);
  156. }
  157. }
  158. LambdaUpdateWrapper<TmUser> wrapper = new LambdaUpdateWrapper<>();
  159. wrapper.eq(TmUser::getId,param.getId());
  160. if(StringUtils.isNotBlank(param.getDeptId())){
  161. wrapper.set(TmUser::getDeptId,param.getDeptId());
  162. }
  163. if(StringUtils.isNotBlank(param.getNickName())){
  164. wrapper.set(TmUser::getNickName,param.getNickName());
  165. }
  166. this.update(wrapper);
  167. if(StringUtils.isNotBlank(param.getRoleId())){
  168. tmUserRoleService.updateByUserId(param.getId(),param.getRoleId());
  169. this.setRoleAndPermToRedis(tmUser);
  170. }
  171. }
  172. @Override
  173. public void changePassword(UserAddRequest param) {
  174. if(StringUtils.isBlank(param.getUserName()) || StringUtils.isBlank(param.getPassword())
  175. || StringUtils.isBlank(param.getConfirmPwd()) || StringUtils.isBlank(param.getOldPassword())){
  176. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  177. }
  178. TmUser tmUser = this.getByUserName(param.getUserName());
  179. if(tmUser == null){
  180. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  181. }
  182. User user = userService.getByUserName(param.getUserName());
  183. if(user == null){
  184. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  185. }
  186. if (!StringUtils.equals(param.getPassword(), param.getConfirmPwd())) {
  187. throw new BusinessException(ResultCode.USER_PASSWORD_ERROR);
  188. }
  189. if(StringUtils.isBlank(param.getClear()) || !param.getClear().equals("YES")) {
  190. String password = Base64Converter.decode(Base64Converter.subText(param.getOldPassword()));
  191. String passwordCode = SecurityUtil.MD5(password);
  192. if(!user.getPassword().equals(passwordCode)){
  193. throw new BusinessException(ResultCode.PROJECT_PASSWORD_ERROR);
  194. }
  195. }
  196. FdkkLoginRequest request = new FdkkLoginRequest(param.getUserName(),param.getPassword(),param.getConfirmPwd());
  197. request.setClear(param.getClear());
  198. FdkkResponse fdkkResponse = fdKKClient.fdkkChangePassword(request);
  199. if(fdkkResponse.getCode() != 0){
  200. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  201. }
  202. }
  203. @Override
  204. public FdkkResponse getMsgAuthCode(String phoneNum) {
  205. if(StringUtils.isBlank(phoneNum)){
  206. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  207. }
  208. TmUser tmUser = this.getByUserName(phoneNum);
  209. if(tmUser == null){
  210. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  211. }
  212. FdkkLoginRequest request = new FdkkLoginRequest(phoneNum);
  213. FdkkResponse fdkkResponse = fdKKClient.fdkkGetMsgAuthCode(request);
  214. if(fdkkResponse.getCode() != 0){
  215. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  216. }
  217. return fdkkResponse;
  218. }
  219. @Override
  220. public void changeStatus(UserAddRequest param) {
  221. if(StringUtils.isBlank(param.getId()) || param.getStatus() == null){
  222. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  223. }
  224. LambdaUpdateWrapper<TmUser> wrapper = new LambdaUpdateWrapper<>();
  225. wrapper.eq(TmUser::getId,param.getId());
  226. wrapper.set(TmUser::getStatus,param.getStatus());
  227. this.update(wrapper);
  228. if(redisUtil.hasKey(String.format(RedisKeyUtil.fusionLoginUser, param.getId()))){
  229. String value = redisUtil.get(String.format(RedisKeyUtil.fusionLoginUser, param.getId()));
  230. FdkkLoginVo fdkkLoginVo = JSONObject.parseObject(value,FdkkLoginVo.class);
  231. fdkkLoginVo.getTmUser().setStatus(param.getStatus());
  232. redisUtil.set(String.format(RedisKeyUtil.fusionLoginUser, param.getId()),JSONObject.toJSONString(fdkkLoginVo));
  233. }
  234. }
  235. @Override
  236. public void delUser(UserAddRequest param) {
  237. if(StringUtils.isBlank(param.getId())){
  238. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  239. }
  240. List<TmCamera> cameras = tmCameraService.getByUserId(param.getId());
  241. if(CollectionUtil.isNotEmpty(cameras)){
  242. tmCameraService.unBind(cameras);
  243. }
  244. String redisKey = String.format(RedisKeyUtil.fusionLoginUser,param.getId());
  245. if(redisUtil.hasKey(redisKey)){
  246. FdkkLoginVo fdkkLoginVo = JSONObject.parseObject(redisUtil.get(redisKey), FdkkLoginVo.class);
  247. redisUtil.del(String.format(RedisKeyUtil.fusionLoginToken,fdkkLoginVo.getToken()));
  248. redisUtil.del(String.format(RedisKeyUtil.fdToken,fdkkLoginVo.getFdToken()));
  249. redisUtil.del(redisKey);
  250. }
  251. this.removeById(param.getId());
  252. }
  253. @Override
  254. public List<TmUser> getByDeptId(String deptId) {
  255. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  256. wrapper.eq(TmUser::getDeptId,deptId);
  257. return this.list(wrapper);
  258. }
  259. @Override
  260. public TmUser getLoginUser() {
  261. return null;
  262. }
  263. @Override
  264. public PageInfo pageList(String userName,String nickName, String deptId, Integer status, Long pageNum, Long pageSize) {
  265. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  266. if(StringUtils.isNotBlank(userName)){
  267. wrapper.like(TmUser::getUserName,userName);
  268. }
  269. if(StringUtils.isNotBlank(nickName)){
  270. wrapper.like(TmUser::getNickName,nickName);
  271. }
  272. if(status != null){
  273. wrapper.eq(TmUser::getStatus,status);
  274. }
  275. List<String> deptIds = tmDepartmentService.getDeptIds();
  276. if(StringUtils.isNotBlank(deptId)){
  277. deptIds = tmDepartmentService.getSonByDeptIdAndDeptIds(deptIds,deptId);
  278. }
  279. wrapper.in(TmUser::getDeptId,deptIds);
  280. wrapper.orderByDesc(TmUser::getCreateTime);
  281. Page<TmUser> page = this.page(new Page<>(pageNum, pageSize), wrapper);
  282. Set<String> deptIdSet = page.getRecords().stream().map(TmUser::getDeptId).collect(Collectors.toSet());
  283. HashMap<String,TmDepartment> map = tmDepartmentService.getMapByDeptIds(deptIdSet);
  284. Set<String> userIds = page.getRecords().stream().map(TmUser::getId).collect(Collectors.toSet());
  285. HashMap<String,TmRole> roleMap = tmRoleService.getMapByUserIds(userIds);
  286. for (TmUser record : page.getRecords()) {
  287. TmDepartment tmDepartment = map.get(record.getDeptId());
  288. if(tmDepartment != null){
  289. record.setDeptName(tmDepartment.getName());
  290. }
  291. TmRole tmRole = roleMap.get(record.getId());
  292. if(tmRole !=null){
  293. record.setRoleId(tmRole.getId());
  294. record.setRoleKey(tmRole.getRoleKey());
  295. record.setRoleName(tmRole.getRoleName());
  296. }
  297. }
  298. return PageInfo.PageInfo(page);
  299. }
  300. @Override
  301. public Object getUserListSelect(String deptId) {
  302. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  303. if(StringUtils.isNotBlank(deptId)){
  304. wrapper.eq(TmUser::getDeptId,deptId);
  305. }
  306. List<String> deptIds = tmDepartmentService.getDeptIds();
  307. if(deptIds.size() <=0){
  308. deptIds.add("not-dept");
  309. }
  310. wrapper.in(TmUser::getDeptId,deptIds);
  311. return this.list(wrapper);
  312. }
  313. @Override
  314. public HashMap<String, TmUser> getByIds(Set<String> createorIds) {
  315. HashMap<String, TmUser> map = new HashMap<>();
  316. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  317. wrapper.in(TmUser::getId,createorIds);
  318. List<TmUser> list = this.list(wrapper);
  319. for (TmUser tmUser : list) {
  320. map.put(tmUser.getId(),tmUser);
  321. }
  322. return map;
  323. }
  324. @Override
  325. public List<TmUser> getLikeNickName(String searchKey) {
  326. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  327. wrapper.like(TmUser::getNickName,searchKey);
  328. return this.list(wrapper);
  329. }
  330. @Override
  331. public HashMap<String, TmUser> getByCamera(List<TmCamera> records) {
  332. HashMap<String, TmUser> userMap = new HashMap<>();
  333. HashMap<String, TmUser> map = new HashMap<>();
  334. List<String> userIds = records.stream().map(TmCamera::getUserId).collect(Collectors.toList());
  335. if(userIds.size() >0){
  336. List<TmUser> tmUsers = this.listByIds(userIds);
  337. tmUsers.forEach(entity ->userMap.put(entity.getId(),entity));
  338. for (TmCamera record : records) {
  339. map.put(record.getCameraSn().toUpperCase(),userMap.get(record.getUserId()));
  340. }
  341. }
  342. return map;
  343. }
  344. @Override
  345. public List<TmUser> getByDeptIds(List<String> deptIds) {
  346. LambdaQueryWrapper<TmUser> wrapper = new LambdaQueryWrapper<>();
  347. wrapper.in(TmUser::getDeptId,deptIds);
  348. return this.list(wrapper);
  349. }
  350. }