| package com.dy.pipIrrBase.user; | 
|   | 
|   | 
| import com.alibaba.fastjson2.JSON; | 
| import com.alibaba.fastjson2.JSONArray; | 
| import com.alibaba.fastjson2.JSONObject; | 
| import com.dy.common.webUtil.QueryResultVo; | 
| import com.dy.pipIrrGlobal.daoBa.BaRolePermissionsMapper; | 
| import com.dy.pipIrrGlobal.daoBa.BaUserMapper; | 
| import com.dy.pipIrrGlobal.daoBa.BaUserRoleMapper; | 
| import com.dy.pipIrrGlobal.pojoBa.BaRolePermissions; | 
| import com.dy.pipIrrGlobal.pojoBa.BaUser; | 
| import com.dy.pipIrrGlobal.pojoBa.BaUserRole; | 
| import com.dy.pipIrrGlobal.voBa.VoUserInfo; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.dubbo.common.utils.PojoUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.util.*; | 
|   | 
| @Slf4j | 
| @Service | 
| public class UserSv { | 
|   | 
|     private BaUserMapper dao; | 
|     private BaUserRoleMapper urDao; | 
|     private BaRolePermissionsMapper baRolePermissionsMapper; | 
|   | 
|     @Autowired | 
|     private void setDao(BaUserMapper dao) { | 
|         this.dao = dao; | 
|     } | 
|   | 
|     @Autowired | 
|     private void setDao(BaUserRoleMapper dao) { | 
|         this.urDao = dao; | 
|     } | 
|   | 
|     @Autowired | 
|     private void setDao(BaRolePermissionsMapper dao) { | 
|         this.baRolePermissionsMapper = dao; | 
|     } | 
|   | 
|     /** | 
|      * 得到一个用户 | 
|      * | 
|      * @param id 用户ID | 
|      * @return 用户实体 | 
|      */ | 
|     public BaUser selectById(Long id) { | 
|         return this.dao.selectById(id); | 
|     } | 
|   | 
|     /** | 
|      * 获取用户列表 | 
|      */ | 
|     public QueryResultVo<List<VoUserInfo>> selectSome(QueryVo queryVo) { | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         Long itemTotal = this.dao.getRecordCount(params); | 
|   | 
|         QueryResultVo<List<VoUserInfo>> rsVo = new QueryResultVo<>() ; | 
|         rsVo.pageSize = queryVo.pageSize ; | 
|         rsVo.pageCurr = queryVo.pageCurr ; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|   | 
|         /** | 
|          * 获取用户列表,取用户ID | 
|          * 根据用户ID获取roleId列表,并添加到返回对象中 | 
|          * 根据用户ID获取roleName列表,并添加到返回对象中 | 
|          */ | 
|         List<VoUserInfo> list_users = Optional.ofNullable(this.dao.getUsers(params)).orElse(new ArrayList<>()); | 
|         if(list_users.size() > 0) { | 
|             for(int i = 0; i < list_users.size(); i++) { | 
|                 VoUserInfo voUserInfo = list_users.get(i); | 
|                 String userId = voUserInfo.getUserId(); | 
|   | 
|                 JSONArray array_roleIds = new JSONArray(); | 
|                 List<Map<String, Object>> list_roleIds = Optional.ofNullable(this.dao.getRoleIdsByUserId(Long.parseLong(userId))).orElse(new ArrayList<>()); | 
|                 for (int j = 0; j < list_roleIds.size(); j++) { | 
|                     Map map_roleId = list_roleIds.get(j); | 
|                     if(map_roleId != null) { | 
|                         array_roleIds.add(map_roleId.get("roleId").toString()); | 
|                     } | 
|   | 
|                 } | 
|                 List<Map<String, Object>> list_roleId = (List<Map<String, Object>>) JSON.parse(array_roleIds.toJSONString()); | 
|   | 
|                 JSONArray array_roleNames = new JSONArray(); | 
|                 List<Map<String, Object>> list_roleNames = Optional.ofNullable(this.dao.getRoleNamesByUserId(Long.parseLong(userId))).orElse(new ArrayList<>()); | 
|                 for (int j = 0; j < list_roleNames.size(); j++) { | 
|                     Map map_roleName = list_roleNames.get(j); | 
|                     if(map_roleName != null) { | 
|                         array_roleNames.add(map_roleName.get("roleName").toString()); | 
|                     } | 
|                 } | 
|                 List<Map<String, Object>> list_roleName = (List<Map<String, Object>>) JSON.parse(array_roleNames.toJSONString()); | 
|   | 
|                 voUserInfo.setRoleIds(list_roleId); | 
|                 voUserInfo.setRoleNames(list_roleName); | 
|             } | 
|         } | 
|         rsVo.obj = list_users; | 
|         return rsVo ; | 
|     } | 
|   | 
|     /** | 
|      * 保存实体 | 
|      * @param po 实体 | 
|      * @return 影响记录数量 | 
|      */ | 
|     @Transactional | 
|     public Long save(BaUser po){ | 
|         //return this.dao.putin(po) ; | 
|         this.dao.putin(po) ; | 
|         return po.getUserId(); | 
|     } | 
|   | 
|     /** | 
|      * 保存修改实体 | 
|      * @param po 实体 | 
|      * @return 影响记录数量 | 
|      */ | 
|     @Transactional | 
|     public int update(BaUser po) { | 
|         return this.dao.updateByPrimaryKeySelective(po); | 
|     } | 
|   | 
|     /** | 
|      * 修改密码 | 
|      * | 
|      * @param id       用户ID | 
|      * @param password 新密码 | 
|      * @return 影响记录数量 | 
|      */ | 
|     public int changePassword(Long id, String password) { | 
|         return this.dao.changePassword(id, password); | 
|     } | 
|   | 
|     /** | 
|      * 设置用户角色 | 
|      * | 
|      * @param userId  用户id | 
|      * @param roleIds 选择的角色id集合 | 
|      * @return 插入用户与角色关联记录数量 | 
|      */ | 
|     public int setRoles(Long userId, Long[] roleIds) { | 
|         this.urDao.deleteByUserId(userId); | 
|         int count = 0; | 
|         if (roleIds != null && roleIds.length > 0) { | 
|             for (Long roleId : roleIds) { | 
|                 count += this.urDao.insertSelective(new BaUserRole(userId, roleId)); | 
|             } | 
|         } | 
|         return count ; | 
|     } | 
|   | 
|     /** | 
|      * 设置用户角色 | 
|      * | 
|      * @param userId  用户id | 
|      * @param roleIds 选择的角色id集合 | 
|      * @return 插入用户与角色关联记录数量 | 
|      */ | 
|     public int setRoles(Long userId, String[] roleIds) { | 
|         this.urDao.deleteByUserId(userId); | 
|         int count = 0; | 
|         if (roleIds != null && roleIds.length > 0) { | 
|             for (String roleId : roleIds) { | 
|                 count += this.urDao.insertSelective(new BaUserRole(userId, Long.parseLong(roleId))); | 
|             } | 
|         } | 
|         return count; | 
|     } | 
|   | 
|     /** | 
|      * 保存修改实体 | 
|      * @param id 实体ID | 
|      * @return 影响记录数量 | 
|      */ | 
|     @Transactional | 
|     public int delete(Long id) { | 
|         return this.dao.deleteLogicById(id); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 生成指定位数随机数字密码 | 
|      * | 
|      * @param length 多长随机数 | 
|      * @return 随机数 | 
|      */ | 
|     public String getStringRandom(int length) { | 
|         String val = ""; | 
|         Random random = new Random(); | 
|         for (int i = 0; i < length; i++) { | 
|             String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; | 
|             if ("char".equalsIgnoreCase(charOrNum)) { | 
|                 int temp = random.nextInt(2) % 2 == 0 ? 65 : 97; | 
|                 val += (char) (random.nextInt(26) + temp); | 
|             } else if ("num".equalsIgnoreCase(charOrNum)) { | 
|                 val += String.valueOf(random.nextInt(10)); | 
|             } | 
|         } | 
|         return val; | 
|     } | 
|   | 
|     /** | 
|      * 根据用户编号获取用户信息 | 
|      * @param userId 用户编号 | 
|      * @return 用户信息列表 | 
|      */ | 
|     public VoUserInfo getUserInfos(Long userId) { | 
|         VoUserInfo voUserInfo = new VoUserInfo(); | 
|         JSONArray array_permission = new JSONArray(); | 
|         Map map = Optional.ofNullable(dao.getUserInfoById(userId)).orElse(new HashMap()); | 
|         if(map.size() == 0) { | 
|             return null; | 
|         } | 
|         voUserInfo.setUserId(String.valueOf(userId)); | 
|         voUserInfo.setUserName(map.get("userName").toString()); | 
|         voUserInfo.setPhone(map.get("phone").toString()); | 
|         voUserInfo.setBlockId(map.get("blockId").toString()); | 
|         voUserInfo.setBlockName(map.get("blockName").toString()); | 
|   | 
|         /** | 
|          * 添加角色编号列表、角色名称列表、权限列表 | 
|          * 如果当前用户是超级管理员,则上述三项均为admin | 
|          */ | 
|         List list_roleIds = new ArrayList(); | 
|         List list_roleNames = new ArrayList(); | 
|         if(voUserInfo.getUserName().equals("超级管理员")) { | 
|             array_permission.add("admin"); | 
|             list_roleIds.add("admin"); | 
|             list_roleNames.add("admin"); | 
|         } else { | 
|             List<Map<String, Object>> list = Optional.ofNullable(urDao.getPermissionsByUserId(userId)).orElse(new ArrayList<>()); | 
|             if(list.size() > 0) { | 
|                 JSONArray array= JSONArray.parseArray(JSON.toJSONString(list)); | 
|                 for (int i = 0; i < array.size(); i++) { | 
|                     JSONObject job = array.getJSONObject(i); | 
|                     list_roleIds.add(job.getLong("roleId").toString()); | 
|                     list_roleNames.add(job.getString("roleName")); | 
|   | 
|                     Long perId = Optional.ofNullable(job.getLong("perId")).orElse(0L); | 
|                     BaRolePermissions baRolePermissions = baRolePermissionsMapper.selectByPrimaryKey(perId); | 
|                     if(baRolePermissions != null) { | 
|                         array_permission.addAll(baRolePermissions.getPermissions()); | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|         voUserInfo.setRoleIds(list_roleIds); | 
|         voUserInfo.setRoleNames(list_roleNames); | 
|         voUserInfo.setPermissions(array_permission); | 
|   | 
|   | 
|         return voUserInfo; | 
|     } | 
| } |