ludc
2024-06-26 79dd20bae9e8af17d5d66b67da4ca6ebc56cd9dd
Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/frameworkcore/compatibility/impl/SmUserQueryServiceImpl.java
@@ -1,16 +1,17 @@
package com.vci.frameworkcore.compatibility.impl;
import com.vci.client.common.objects.UserObject;
import com.vci.client.common.providers.ClientServiceProvider;
import com.vci.common.util.ThreeDES;
import com.vci.corba.common.PLException;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.UserInfo;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.frameworkcore.compatibility.OrgDeptQueryServiceI;
import com.vci.frameworkcore.compatibility.SmPwdStrategyQueryServiceI;
import com.vci.frameworkcore.compatibility.SmRoleQueryServiceI;
import com.vci.frameworkcore.compatibility.SmUserQueryServiceI;
import com.vci.frameworkcore.dto.SmUserDTO;
import com.vci.frameworkcore.model.dto.SmUserDTO;
import com.vci.frameworkcore.model.SmUserDO;
import com.vci.frameworkcore.pagemodel.OrgDepartmentVO;
import com.vci.frameworkcore.pagemodel.SmPasswordStrategyVO;
import com.vci.frameworkcore.pagemodel.SmRoleVO;
@@ -26,11 +27,8 @@
import com.vci.starter.web.util.BeanUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.VciDateUtil;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
import com.vci.web.enumpck.UserTypeEnum;
import com.vci.web.model.SmPasswordStrategyDO;
import com.vci.web.model.SmUserDO;
import com.vci.web.service.WebBoServiceI;
import com.vci.web.util.Func;
import com.vci.web.util.PlatformClientUtil;
@@ -38,9 +36,11 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.vci.frameworkcore.constant.FrameWorkBusLangCodeConstant.DATA_OID_NOT_EXIST;
@@ -171,7 +171,7 @@
    * @throws VciBaseException 查询出错的时候会抛出异常
    */
   private SmUserVO getUserByField(String queryField,String queryValue) throws VciBaseException{
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(null,SmUserDO.class,null,true);
      VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(null, SmUserDO.class,null,true);
      queryWrapper.eq(queryWrapper.getTableNick() + "." +queryField,queryValue);
      queryWrapper.setDistinct(true);
      queryWrapper.wrapperSql();
@@ -277,7 +277,7 @@
    * @param userInfoArr 业务数据数组
    * @return 显示对象集合
    */
   private List<SmUserVO> userInfoArr2VO(UserInfo[] userInfoArr) {
   private List<SmUserVO> userInfoArr2VO(UserInfo[] userInfoArr) throws PLException {
      List<SmUserVO> userVOList = new ArrayList<>();
      for(UserInfo userInfo : userInfoArr){
         userVOList.add(userInfo2VO(userInfo));
@@ -290,7 +290,10 @@
    * @param userInfo 平台返回的业务数据
    * @return 用户显示对象
    */
   private SmUserVO userInfo2VO(UserInfo userInfo) {
   private SmUserVO userInfo2VO(UserInfo userInfo) throws PLException {
      if(Func.isBlank(userInfo.id)){
         return new SmUserVO();
      }
      SmUserVO smUserVO = new SmUserVO();
      smUserVO.setOid(userInfo.id);
      smUserVO.setId(userInfo.userName);
@@ -302,7 +305,13 @@
      smUserVO.setDescription(userInfo.desc);
      smUserVO.setEmail(userInfo.email);
      //用户所属部门的查询设置
      List<OrgDepartmentVO> orgDepartmentVOList = orgDepartmentVOMap.get(userInfo.id);
      List<OrgDepartmentVO> orgDepartmentVOList;
      //查看全局变量中是否存在部门信息,存在的情况最主要是针对多条用户查询的时候避免重复查询的
      if(Func.isNotEmpty(orgDepartmentVOMap)){
         orgDepartmentVOList = orgDepartmentVOMap.get(userInfo.id);
      }else {
         orgDepartmentVOList = orgDeptQueryService.listDeptByUserOid(userInfo.id,null);
      }
      smUserVO.setPkDepartment(
            Func.isEmpty(orgDepartmentVOList) ?
                  null:orgDepartmentVOList.stream().map(OrgDepartmentVO::getOid).collect(Collectors.joining(","))
@@ -312,11 +321,23 @@
                  null:orgDepartmentVOList.stream().map(OrgDepartmentVO::getName).collect(Collectors.joining(","))
      );
      //密码策略查询设置
      SmPasswordStrategyVO smPasswordStrategyVO = smPwdStrategyVOMap.getOrDefault(userInfo.id,new SmPasswordStrategyVO());
      SmPasswordStrategyVO smPasswordStrategyVO;
      if(Func.isNotEmpty(smPwdStrategyVOMap)){
         smPasswordStrategyVO = smPwdStrategyVOMap.getOrDefault(userInfo.id,new SmPasswordStrategyVO());
      }else {
         smPasswordStrategyVO = smPwdStrategyQueryService.getPasswordStrategyVOByUserOid(userInfo.id);
         //如果不存在就获取默认的
         smPasswordStrategyVO = Func.isNotEmpty(smPasswordStrategyVO) ? smPasswordStrategyVO:smPwdStrategyQueryService.getPasswordStrategyVOByDefault();
      }
      smUserVO.setPkPasswordStrategy(smPasswordStrategyVO.getOid());
      smUserVO.setPkPasswordStrategyName(smPasswordStrategyVO.getName());
      //角色查询设置
      List<SmRoleVO> smRoleVOList = smRoleVOMap.get(userInfo.id);
      List<SmRoleVO> smRoleVOList;
      if (Func.isNotEmpty(smRoleVOMap)) {
         smRoleVOList = smRoleVOMap.get(userInfo.id);
      }else {
         smRoleVOList = smRoleQueryService.listRoleByUserOid(userInfo.id,null);
      }
      smUserVO.setPkPerson(
            Func.isEmpty(smRoleVOList) ?
                  null:smRoleVOList.stream().map(SmRoleVO::getOid).collect(Collectors.joining(","))
@@ -495,6 +516,7 @@
         pageHelper = new PageHelper(-1);
      }
      pageHelper.addDefaultAsc("PLTRUENAME");
      //TODO:为了方便调试,所以这儿先注释写死后面记得更改
      //String userName = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserName();
      //分页查询
      UserInfo[] userInfos = platformClientUtil.getFrameworkService().fetchUserInfoByCondition(
@@ -557,6 +579,7 @@
      SmUserVO userVO = getUserByUserId(userId);
      return userVO == null?"":userVO.getName();
   }
    /**
     * 根据用户主键获取用户的姓名
     * @param userOid 用户主键
@@ -928,7 +951,6 @@
      return false;
   }
    /**
     * 设置某个用户是锁定状态
     * @param userId 用户名
@@ -960,6 +982,7 @@
     * @param confirmPassword 确认密码
     */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void changePassword(String userOid, String password,
         String confirmPassword) throws VciBaseException {
      WebUtil.alertNotNull(userOid,"用户主键",password,"密码",confirmPassword,"确认密码");
@@ -1017,40 +1040,38 @@
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean addUser(SmUserDTO smUserDTO) throws PLException {
      //判空
      VciBaseUtil.alertNotNull(
            smUserDTO,"添加的用户对象",
            smUserDTO.getId(),"账号",
            smUserDTO.getId(),"用户名",
            smUserDTO.getPassword(),"密码",
            smUserDTO.getConfirmPassword(),"确认密码",
            smUserDTO.getName(),"姓名"
      );
      //确认密码
      String confirmPassword = smUserDTO.getConfirmPassword();
      //比对密码是是否一致
      if(!smUserDTO.getPassword().equals(confirmPassword)){
         throw new VciBaseException("两次密码不一致,请重新填写!");
      }
      //先用户名(账号)查重
      SmUserVO dbSmUserVO = getUserByUserId(smUserDTO.getId());
      if(Func.isNotEmpty(dbSmUserVO) && Func.isNotBlank(dbSmUserVO.getOid())){
         throw new VciBaseException("该用户名在系统中已经存在,请修改!");
      }
      //密码加密
      ThreeDES des = new ThreeDES();
      des.getKey("daliantan0v0");// 生成密匙
      //第二次MD5加密
      String md5Password2 = des.getEncString(smUserDTO.getPassword());
      //校验
      check(smUserDTO,true);
      //生成存储的DO对象
      smUserDTO.setOid(VciBaseUtil.getPk());
      smUserDTO.setPassword(md5Password2);
      smUserDTO.setLockFlag("0");
      BusinessObject user = ClientServiceProvider.getBOFService().initBusinessObject("user");
      SmUserDO smUserDO = new SmUserDO();
      BeanUtil.convert(user,smUserDO);
      UserInfo userInfo = new UserInfo();
      BeanUtil.convert(smUserDO,userInfo);
      return platformClientUtil.getFrameworkService().saveOrUpdateUser(userInfo, null);
      Date date = new Date();
      smUserDTO.setPwdUpdateTime(date);
      smUserDTO.setStatus((short) 0);
      smUserDTO.setCreateTime(date);
      smUserDTO.setLastModifyTime(date);
      //userObject.setCreateUser(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserName());
      smUserDTO.setCreator("developer");
      //userObject.setUpdateUser(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserName());
      smUserDTO.setLastModifier("developer");
      UserInfo userInfo = changeUserObjectToUserInfo(smUserDTO);
      UserEntityInfo userEntityInfo = new UserEntityInfo("developer", "");
      String oid = platformClientUtil.getFrameworkService().saveUser(userInfo, userEntityInfo);
      if (Func.isEmpty(oid)) {
         return false;
      }
      if(Func.isNotBlank(smUserDTO.getPkDepartment())){
         platformClientUtil.getFrameworkService().saveUserDept(new String[]{oid}, smUserDTO.getPkDepartment(), userEntityInfo);
      }
      return true;
   }
   /**
@@ -1059,10 +1080,129 @@
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean updateUser(SmUserDTO smUserDTO) throws PLException {
      //判空
      VciBaseUtil.alertNotNull(
            smUserDTO,"修改的用户对象",
            smUserDTO.getOid(),"用户主键",
            smUserDTO.getId(),"用户名",
            smUserDTO.getPassword(),"密码",
            smUserDTO.getConfirmPassword(),"确认密码",
            smUserDTO.getName(),"姓名"
      );
      //校验
      check(smUserDTO,false);
      //查询数据库中的
      SmUserVO dbSmUserVO = getUserByUserId(smUserDTO.getId());
      //根据用户名查询到了用户,但是oid不是当前修改的用户的oid,说明修改的用户名重复
      if(Func.isNotEmpty(dbSmUserVO)
         && !dbSmUserVO.getOid().equals(smUserDTO.getOid())
      ){
         throw new PLException("用户名已存在,不能将当前用户的用户名修改为" + dbSmUserVO.getId(),new String[0]);
      }
      smUserDTO.setLastModifyTime(new Date());
      //userObject.setUpdateUser(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserName());
      smUserDTO.setLastModifier("developer");
      UserInfo userInfo = changeUserObjectToUserInfo(smUserDTO);
      boolean updateBoolean = platformClientUtil.getFrameworkService().updateUser(userInfo, new UserEntityInfo("developer", null));
      //用户关联部门有所更改
      if(Func.isNotEmpty(smUserDTO.getPkDepartment()) && !smUserDTO.getPkDepartment().equals(dbSmUserVO.getPkDepartment())){
         //界面传递过来的已关联的部门oid
         List<String> updatePkDept = Func.toStrList(smUserDTO.getPkDepartment(), ",");
         //用户已关联的部门oid
         List<String> associatedPkDept = Func.toStrList(dbSmUserVO.getPkDepartment(), ",");
         //移除
         updatePkDept.removeAll(associatedPkDept);
         //执行保存用户部门关联关系
         orgDeptQueryService.saveUserDepts(dbSmUserVO.getOid(),updatePkDept);
      }
      return updateBoolean;
   }
   /**
    * 检查用户信息是否符合规范
    * @param smUserDTO
    * @param isAdd
    */
   private void check(SmUserDTO smUserDTO, boolean isAdd){
      if(!smUserDTO.getPassword().equals(smUserDTO.getConfirmPassword())){
         throw new VciBaseException("密码和确认密码不相等");
      }
      if(smUserDTO.getId().getBytes().length > 128){
         throw new VciBaseException("用户名长度超过上限");
      }
      if(smUserDTO.getPassword().getBytes().length > 128){
         throw new VciBaseException("密码长度超过上限");
      }
      if(smUserDTO.getName().getBytes().length > 64){
         throw new VciBaseException("姓名长度超过上限");
      }
      if (Func.isNotBlank(smUserDTO.getSpecialties()) && smUserDTO.getSpecialties().getBytes().length > 255){
         throw new VciBaseException("专业长度超过上限");
      }
      if (Func.isNotBlank(smUserDTO.getEmail()) && smUserDTO.getEmail().getBytes().length > 128){
         throw new VciBaseException("电子邮箱长度超过上限");
      }
      if (Func.isNotBlank(smUserDTO.getDescription()) && smUserDTO.getDescription().getBytes().length > 255 ){
         throw new VciBaseException("描述长度超过上限");
      }
      if (!smUserDTO.getName().matches("^[A-Za-z0-9_]+$")) {
         throw new VciBaseException("用户名必须是由A-Z a-z 0-9 _组成");
      }
      if (Func.isNotBlank(smUserDTO.getEmail()) && !smUserDTO.getEmail().matches("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$")){
         throw new VciBaseException("电子邮箱格式错误");
      }
      //是新增才做用户名查重处理
      if(isAdd){
         //根据用户名(账号)查重
         SmUserVO dbSmUserVO = getUserByUserId(smUserDTO.getId());
         if(Func.isNotEmpty(dbSmUserVO) && Func.isNotBlank(dbSmUserVO.getOid())){
            throw new VciBaseException("该用户名在系统中已经存在,请修改!");
         }
      }
      //根据当前创建这个用户的人所绑定密码策略来进行密码校验
      try {
         //TODO:为了方便调试,所以这儿先注释写死后面记得更改
         //String userName = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserName();
         String userName = "developer";
         String error = platformClientUtil.getFrameworkService().checkPasswordStrategyByUserId(userName, smUserDTO.getPassword(),null);
         if (!StringUtils.isBlank(error)) {
            throw new VciBaseException("当前设置的密码,密码策略校验未通过");
         }
      } catch (PLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         throw new VciBaseException("检查密码策略符合情况失败!2");
      }
   }
   /***
    * 用户从DTO对象到corba对象
    * @param user
    * @return
    */
   public UserInfo changeUserObjectToUserInfo(SmUserDTO user) {
      UserInfo userInfo = new UserInfo();
      BeanUtil.convert(smUserDTO,userInfo);
      return platformClientUtil.getFrameworkService().updateUser(userInfo, null);
      userInfo.id = user.getOid() == null ? "" : user.getOid();
      userInfo.userName = user.getId() == null ? "" : user.getId();
      userInfo.pwd = user.getPassword() == null ? "" : user.getPassword();
      userInfo.trueName = user.getName() == null ? "" : user.getName();
      userInfo.specialties = user.getSpecialties() == null ? "" : user.getSpecialties();
      userInfo.email = user.getEmail() == null ? "" : user.getEmail();
      userInfo.desc = user.getDescription() == null ? "" : user.getDescription();
      userInfo.userType = user.getUserType();
      userInfo.status = user.getStatus();
      userInfo.createTime = user.getCreateTime().getTime();
      userInfo.createUser = user.getCreator() == null ? "" : user.getCreator();
      userInfo.updateTime = user.getLastModifyTime().getTime();
      userInfo.updateUser = user.getLastModifier() == null ? "" : user.getLastModifier();
      userInfo.pwdUpdateTime = user.getPwdUpdateTime().getTime();
      userInfo.grantor = user.getGrantor() == null ? "" : user.getGrantor();
      userInfo.isDeptLeader = user.getIsDeptLeader() == null ? "0" : user.getIsDeptLeader();
      return userInfo;
   }
   /***
@@ -1098,6 +1238,7 @@
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean deleteUser(String[] ids) throws PLException {
      VciBaseUtil.alertNotNull(ids,"待删除的用户id列表不能为空!");
      return platformClientUtil.getFrameworkService().deleteUser(ids, null);
@@ -1110,6 +1251,7 @@
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean disableOrEnableUsers(String[] ids, boolean flag) throws PLException {
      VciBaseUtil.alertNotNull(ids,"停用/启用的用户id列表");
      return platformClientUtil.getFrameworkService().stopUsers(ids, flag,null);