package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.base.ISystemInitService;
import cn.estsh.i3plus.core.api.iservice.busi.IPersonnelService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysRoleService;
import cn.estsh.i3plus.core.apiservice.util.HqlModelPack;
import cn.estsh.i3plus.platform.common.tool.StringTool;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.base.shirotoken.UserToken;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.model.platform.UserDetailModel;
import cn.estsh.i3plus.pojo.platform.bean.*;
import cn.estsh.i3plus.pojo.platform.repository.*;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import cn.estsh.i3plus.pojo.wms.sqlpack.WmsHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author : Adair Peng
 * @CreateDate : 2018-12-24 13:10
 * @Modify:
 **/
@Service
public class PersonnelServiceService implements IPersonnelService {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysConfigService.class);

    @Autowired
    private SysUserRepository userRDao;

    @Autowired
    private SysUserInfoRepository userInfoRDao;

    @Autowired
    private SysRoleRepository roleRDao;

    @Autowired
    private SysRefUserDepartmentRepository refUserDepartmentRDao;

    @Autowired
    private SysRefUserInfoDepartmentRepository refUserInfoDepartmentRDao;

    @Autowired
    private SysRefUserPositionRepository refUserPositionRDao;

    @Autowired
    private SysRefUserRoleRepository refUserRoleRDao;

    @Autowired
    private SysOrganizeRepository organizeRDao;

    @Autowired
    private SysDepartmentRepository departmentRDao;

    @Autowired
    private SysPositionRepository positionRDao;

    @Autowired
    private SysMenuRepository menuRDao;

    @Autowired
    private SysRefRoleMenuRepository refRoleMenuRDao;

    @Autowired
    private ISysRoleService roleService;

    @Resource(name="redisCore")
    protected ImppRedis redisCore;

    @Resource(name="redisRes")
    private ImppRedis redisRes;

    @Autowired
    private ISystemInitService systemInitService;

    @Override
    public SysUser saveSysUser(SysUser user) {
        LOGGER.debug("平台账号 SysUser user:{}", user);
        return userRDao.save(user);
    }

    @Override
    public SysUserInfo saveSysUserInfo(SysUserInfo info) {
        LOGGER.debug("平台用户 SysUserInfo info:{}", info);
        return userInfoRDao.save(info);
    }

    @Override
    public void updateSysUserInfoStatus(Long userInfoId, Integer status) {

    }

    @Override
    @ApiOperation(value = "删除账号信息")
    public void deleteSysUser(Long[] userIdList,String userName) {
        LOGGER.debug("平台账号 SysUser userIdList:{}", userIdList);
        // 删除账号信息
        userRDao.updateValidStatusByPropertyIn("id", userIdList,CommonEnumUtil.IS_VAILD.INVAILD.getValue(), userName);

        // 删除账号关系
        refUserDepartmentRDao.deleteByPropertyIn("userId",userIdList);
        refUserRoleRDao.deleteByPropertyIn("userId", userIdList);
    }

    @Override
    @ApiOperation(value = "删除用户信息")
    public void deleteSysUserInfo(Long[] infoIdList,String userName) {
        LOGGER.debug("平台用户 SysUserInfo infoIdList:{}", infoIdList);
        // 删除用户信息
        userInfoRDao.updateValidStatusByPropertyIn("id", infoIdList,CommonEnumUtil.IS_VAILD.INVAILD.getValue(), userName);

        // 删除用户关系
        refUserInfoDepartmentRDao.deleteByPropertyIn("userId",infoIdList);
        refUserPositionRDao.deleteByPropertyIn("userId",infoIdList);
    }

    @Override
    public SysUser getSysUserById(Long id) {
        return userRDao.getById(id);
    }

    @Override
    public List<SysUser> findSysUserByIds(Long[] ids) {
        LOGGER.debug("平台用户 SysUserInfo ids:{}", ids);

        String infoWhere = CoreHqlPack.packHqlIds("id", ids);
        return userRDao.findByHqlWhere(infoWhere);
    }

    @Override
    public List<SysUser> querySysUserByUserDetailModelOrIds(UserDetailModel model, List<Long> ids) {
        LOGGER.debug("平台用户 SysUser model:{} ids:{}", model, ids);
        String modeWhere = HqlModelPack.packHqlSysUserByModelOrIds(model, ids);

        return userRDao.findByHqlWhere(modeWhere);
    }

    @Override
    @ApiOperation(value = "查询账号信息")
    public ListPager<SysUser> querySysUserPagerByUserDetailModelOrIds(UserDetailModel model,Pager pager, List<Long> ids) {
        LOGGER.debug("平台用户 SysUser model:{} ids:{}", model, ids);
        String modeWhere = HqlModelPack.packHqlSysUserByModelOrIds(model, ids);

        pager = PagerHelper.getPager(pager, userRDao.findByHqlWhereCount(modeWhere));
        return new ListPager(userRDao.findByHqlWherePage(modeWhere + model.orderBy(), pager), pager);
    }

    @Override
    public SysUserInfo getSysUserInfoById(Long id) {
        return userInfoRDao.getById(id);
    }

    @Override
    public SysDepartment getSysDepartmentById(Long id) {
        return departmentRDao.getById(id);
    }

    @Override
    public SysPosition getSysPositionById(Long id) {
        return positionRDao.getById(id);
    }

    @Override
    public SysOrganize getSysOrganizeById(Long id) {
        return organizeRDao.getById(id);
    }

    @Override
    public SessionUser queryUserLogin(String userName, String password, String languageCode) {
        LOGGER.debug("平台用户 SYS_USER loginName:{} \t password:{} \t languageCode:{}", userName, password, languageCode);
        UserToken token = new UserToken(userName,password,languageCode);
        SessionUser sessionUser = AuthUtil.login(token);
        AuthUtil.setSessionUser(sessionUser);
        return sessionUser;
    }

    @Override
    public ListPager<SysUserInfo> querySysUserInfo(UserDetailModel model, Pager pager) {
        pager = PagerHelper.getPager(pager, userInfoRDao.findByHqlWhereCount(CoreHqlPack.packHqlSysUserInfo(model.getSysUserInfo())));
        return new ListPager<>(userInfoRDao.findByHqlWherePage(CoreHqlPack.packHqlSysUserInfo(model.getSysUserInfo()), pager), pager);
    }

    @Override
    public List<SysUserInfo> findSysUserInfoByIds(Long[] ids) {
        LOGGER.debug("平台用户 SysUserInfo ids:{}", ids);

        String infoWhere = CoreHqlPack.packHqlIds("id", ids);
        return userInfoRDao.findByHqlWhere(infoWhere);
    }

    @Override
    public List<SysRefUserRole> findSysRefUserRoleById(Long userId) {
        LOGGER.debug("平台账号角色 SysRefUserRole userId:{}", userId);
        return refUserRoleRDao.findByProperty("userId",userId);
    }

    @Override
    public List<SysRefUserDepartment> findSysRefUserDepartmentById(Long userId) {
        LOGGER.debug("平台账号部门 SysRefUserDepartment userId:{}", userId);
        return refUserDepartmentRDao.findByProperty("userId",userId);
    }

    @Override
    public List<SysRefUserDepartment> querySysRefUserDepartmentByUserDetailModel(UserDetailModel model) {
        if(model != null){
            StringBuffer userIdWhere = new StringBuffer();

            if(model.getUserDepartmentIdList() != null){
                HqlPack.getInPack(StringUtils.join(model.getInfoDepartmentIdList(),","),"departmentId", userIdWhere);
            }
            if(model.getUserOrganizeIdList() != null){
                HqlPack.getInPack( StringUtils.join(model.getUserOrganizeIdList(),","),"organizeId",userIdWhere);
            }

            return refUserDepartmentRDao.findByHqlWhere(userIdWhere.toString());
        }else {
            return null;
        }
    }

    @Override
    public List<SysRefUserInfoDepartment> findSysRefUserInfoDepartmentById(Long infoId) {
        LOGGER.debug("平台用户部门关系 SysRefUserInfoDepartment infoId:{}", infoId);
        return refUserInfoDepartmentRDao.findByProperty("userId",infoId);
    }

    @Override
    @ApiOperation(value = "查询用户部门关系")
    public List<SysDepartment> findSysUserInfoDepartmentByInfoId(Long infoId) {
        LOGGER.debug("平台用户部门 SysDepartment infoId:{}", infoId);
        List<SysDepartment> result = new ArrayList<>();

        List<SysRefUserInfoDepartment> refs = findSysRefUserInfoDepartmentById(infoId);
        if(refs != null && refs.size() > 0){
            Long[] ids = new Long[refs.size()];
            for (int i = 0; i < refs.size(); i++) {
                ids[i] = refs.get(i).getDepartmentId();
            }
            String idWhere = CoreHqlPack.packHqlIds("id", ids);
            return departmentRDao.findByHqlWhere(idWhere);
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询账号部门关系")
    public List<SysDepartment> findSysUserDepartmentByInfoId(Long infoId) {
        LOGGER.debug("平台用户部门 SysDepartment infoId:{}", infoId);
        List<SysDepartment> result = new ArrayList<>();

        List<SysRefUserDepartment> refs = findSysRefUserDepartmentById(infoId);
        if(refs != null && refs.size() > 0){
            Long[] ids = new Long[refs.size()];
            for (int i = 0; i < refs.size(); i++) {
                ids[i] = refs.get(i).getDepartmentId();
            }
            String idWhere = CoreHqlPack.packHqlIds("id", ids);
            return departmentRDao.findByHqlWhere(idWhere);
        }
        return result;
    }

    @Override
    public List<SysOrganize> findSysUserInfoOrganize(Long infoId) {
        LOGGER.debug("平台用户部门 SysDepartment infoId:{}", infoId);
        List<SysOrganize> result = new ArrayList<>();

        List<SysRefUserInfoDepartment> refs = findSysRefUserInfoDepartmentById(infoId);
        if(refs != null && refs.size() > 0){
            Long[] ids = new Long[refs.size()];
            for (int i = 0; i < refs.size(); i++) {
                ids[i] = refs.get(i).getOrganizeId();
            }
            String idWhere = CoreHqlPack.packHqlIds("id", ids);
            return organizeRDao.findByHqlWhere(idWhere);
        }
        return result;
    }

    @Override
    public List<SysOrganize> findSysUserOrganize(Long infoId) {
        LOGGER.debug("平台用户部门 SysDepartment infoId:{}", infoId);
        List<SysOrganize> result = new ArrayList<>();

        List<SysRefUserDepartment> refs = findSysRefUserDepartmentById(infoId);
        if(refs != null && refs.size() > 0){
            Long[] ids = new Long[refs.size()];
            for (int i = 0; i < refs.size(); i++) {
                ids[i] = refs.get(i).getOrganizeId();
            }
            String idWhere = CoreHqlPack.packHqlIds("id", ids);
            return organizeRDao.findByHqlWhere(idWhere);
        }
        return result;
    }

    @Override
    public List<SysRefUserPosition> findSysRefUserPositionById(Long infoId) {
        LOGGER.debug("平台用户岗位 SysRefUserPosition infoId:{}", infoId);
        return refUserPositionRDao.findByProperty("userId",infoId);
    }

    @Override
    public SessionUser packSessionUser(SessionUser sessionUser, SysUser user, Integer userType, String languageCode) {
        return null;
    }

    @Override
    public void checkSysUserOnly(SysUser user) {
        if(user != null){
            // 登录名唯一教研
            if(!checkSysUserLoginName(user.getUserLoginName(), user.getId() == null ? 0 : user.getId())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("用户名已存在")
                        .setErrorSolution("请重新输入用户名")
                        .build();
            }

            // 邮箱唯一教研
            if(!checkSysUserEmail(user.getUserEmail(),user.getUserInfoId() == null ? 0 : user.getUserInfoId())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("邮箱已存在")
                        .setErrorSolution("请重新输入邮箱")
                        .build();
            }

            // 手机号唯一教研
            if(!checkSysUserPhone(user.getUserPhone(),user.getUserInfoId() == null ? 0 : user.getUserInfoId())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("手机号已存在")
                        .setErrorSolution("请重新输入手机号")
                        .build();
            }

            // 工号唯一教研
            if(!checkSysUserEmpNo(user.getUserEmpNo(),user.getUserInfoId() == null ? 0 : user.getUserInfoId())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("工号号已存在")
                        .setErrorSolution("请重新输入工号")
                        .build();
            }
        }
    }

    @Override
    @ApiOperation(value = "重置密码检查")
    public Integer checkSysUserResetPassword(SysUser user) {
        LOGGER.debug("平台用户岗位 SysUser user:{}", user);
        Integer num = 0;
        if(user != null){
            String day = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.format(new Date());
            String redisKey = CommonConstWords.REDIS_PREFIX_USER_RESET_PASSWORD + "-" + day + "-" + user.getId();
            Object redisValue = redisCore.getObject(redisKey);
            num = redisValue == null ? 1 : Integer.parseInt(redisValue.toString()) + 1;

            if(num > CommonConstWords.LOCK_USER_RESET_PASSWORD){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.USER_PERMISSION_RUN_OUT.getCode())
                        .setErrorDetail("重置密码次数已使用完毕")
                        .setErrorSolution("请明日再次使用")
                        .build();
            }
            redisCore.putObject(redisKey,num,CommonConstWords.REDIS_TIME_DAY_ONE);
        }
        return num;
    }

    @Override
    public void checkSysPositionRef(Long positionId) {
        LOGGER.debug("平台岗位 SysPosition positionId:{}", positionId);

        if(positionId != null){
            List<SysPosition> positionList = positionRDao.findByProperty("parentId", positionId);
            if (positionList!= null && positionList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在岗位信息["+positionList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除子集信息在操作")
                        .build();
            }
//            long refPositionCount = refUserPositionRDao.findByPropertyCount("positionId",positionId);
//            if (refPositionCount >= 1) {
//                throw ImppExceptionBuilder.newInstance()
//                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
//                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
//                        .setErrorDetail("存在用户关系引用信息无法删除！")
//                        .setErrorSolution("请先删除用户关系信息再操作")
//                        .build();
//            }
            List<SysUserInfo> userInfoList = userInfoRDao.findByProperty("positionId", positionId);
            if (userInfoList != null && userInfoList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在用户信息["+userInfoList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除用户引用信息再操作")
                        .build();
            }
        }
    }

    @Override
    public void checkSysDepartmentRef(Long departmentId) {
        LOGGER.debug("平台部门 SysDepartment departmentId:{}", departmentId);

        if(departmentId != null){
            List<SysDepartment> departmentList = departmentRDao.findByProperty("parentId", departmentId);
            if (departmentList != null && departmentList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在相关部门信息["+departmentList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除子集信息在操作")
                        .build();
            }
//            long refPositionCount = refUserPositionRDao.findByPropertyCount("positionId",positionId);
//            if (refPositionCount >= 1) {
//                throw ImppExceptionBuilder.newInstance()
//                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
//                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
//                        .setErrorDetail("存在用户关系引用信息无法删除！")
//                        .setErrorSolution("请先删除用户关系信息再操作")
//                        .build();
//            }
            List<SysUserInfo> userInfoList = userInfoRDao.findByProperty("departmentId", departmentId);
            if (userInfoList != null && userInfoList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在用户信息["+userInfoList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除用户引用信息再操作")
                        .build();
            }

            List<SysUser> userList = userRDao.findByProperty("departmentId", departmentId);
            if (userList != null && userList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在账号信息["+userList.get(0).getUserLoginName()+"]无法删除！")
                        .setErrorSolution("请先删除账号引用信息再操作")
                        .build();
            }
        }
    }

    @Override
    public void checkSysOrganizeRef(Long organizeId) {
        LOGGER.debug("平台部门 SysOrganize organizeId:{}", organizeId);

        if(organizeId != null){
            List<SysOrganize> organizeList = organizeRDao.findByProperty("parentId", organizeId);
            if (organizeList != null && organizeList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在相关组织信息["+organizeList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除子集信息在操作")
                        .build();
            }

//            long refPositionCount = refUserPositionRDao.findByPropertyCount("positionId",positionId);
//            if (refPositionCount >= 1) {
//                throw ImppExceptionBuilder.newInstance()
//                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
//                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
//                        .setErrorDetail("存在用户关系引用信息无法删除！")
//                        .setErrorSolution("请先删除用户关系信息再操作")
//                        .build();
//            }

            List<SysDepartment> departmentList = departmentRDao.findByProperty("organizeId", organizeId);
            if (departmentList != null && departmentList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在部门信息["+departmentList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除部门引用信息再操作")
                        .build();
            }

            List<SysUserInfo> userInfoList = userInfoRDao.findByProperty("organizeId", organizeId);
            if (userInfoList != null && userInfoList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在用户信息["+userInfoList.get(0).getName()+"]无法删除！")
                        .setErrorSolution("请先删除用户引用信息再操作")
                        .build();
            }

            List<SysUser> userList = userRDao.findByProperty("organizeId", organizeId);
            if (userList != null && userList.size() >= 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("存在账号信息["+userList.get(0).getUserLoginName()+"]无法删除！")
                        .setErrorSolution("请先删除账号引用信息再操作")
                        .build();
            }
        }
    }

    @Override
    public void checkSysUserPassword(String password) {
        LOGGER.debug("平台用户 SysUser password:{}", password);

        if(StringUtils.isNotBlank(password)){
            // 密码长度校验
            String redisKey = CommonConstWords.REDIS_PREFIX_CACHE_CONFIG + "_" + CommonConstWords.CONFIG_PWD_LENGTH;
            SysConfig config = (SysConfig) systemInitService.getDataFromCache(redisKey, SysConfig.class);

            if(!checkPasswordLength(config,password)){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("密码长度不够"+config.getConfigValue()+"位")
                        .setErrorSolution("请重新输入")
                        .build();
            }

            // 密码小写校验
            redisKey = CommonConstWords.REDIS_PREFIX_CACHE_CONFIG + "_" + CommonConstWords.CONFIG_PWD_EXIST_LOWER_ENGLISH;
            config = (SysConfig) systemInitService.getDataFromCache(redisKey, SysConfig.class);

            if(!checkPasswordLowerEnglish(config,password)){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("密码必须包含小写英文")
                        .setErrorSolution("请重新输入")
                        .build();
            }

            // 密码大写校验
            redisKey = CommonConstWords.REDIS_PREFIX_CACHE_CONFIG + "_" + CommonConstWords.CONFIG_PWD_EXIST_UPPERCASE_ENGLISH;
            config = (SysConfig) systemInitService.getDataFromCache(redisKey, SysConfig.class);

            if(!checkPasswordUpperEnglish(config,password)){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("密码必须包含大写英文")
                        .setErrorSolution("请重新输入")
                        .build();
            }

            // 密码特殊字符
            redisKey = CommonConstWords.REDIS_PREFIX_CACHE_CONFIG + "_" + CommonConstWords.CONFIG_PWD_EXIST_SPECIAL_CHAR;
            config = (SysConfig) systemInitService.getDataFromCache(redisKey, SysConfig.class);

            if(!checkPasswordSpecialChar(config,password)){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("密码必须包含特殊字符")
                        .setErrorSolution("请重新输入")
                        .build();
            }

            // 密码特殊字符
            redisKey = CommonConstWords.REDIS_PREFIX_CACHE_CONFIG + "_" + CommonConstWords.CONFIG_PWD_EXIST_NUMBER;
            config = (SysConfig) systemInitService.getDataFromCache(redisKey, SysConfig.class);

            if(!checkPasswordNumber(config,password)){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("密码必须包含数字")
                        .setErrorSolution("请重新输入")
                        .build();
            }
        }
    }

    @Override
    public void refreshMenuRdd(Long menuId) {
        LOGGER.info("系统功能 SysMenu menuId:{} ",menuId);
        if(menuId != null){
            SysMenu menu = menuRDao.getById(menuId);
            if(menu != null){
                menuRDao.updateByProperties(
                        new String[]{"parentId"},
                        new Object[]{menu.getId()},
                        new String[]{"parentNameRdd"},
                        new Object[]{menu.getName()});
            }
        }
    }

    @Override
    public void refreshUpdateMenuRdd(Long menuId) {
        LOGGER.info("系统功能 SysMenu menuId:{} ",menuId);
        if(menuId != null){
            SysMenu menu = menuRDao.getById(menuId);
            if(menu != null){
                refRoleMenuRDao.updateByProperties(
                        new String[]{"menuId"},
                        new Object[]{menu.getId()},
                        new String[]{"menuNameRdd"},
                        new Object[]{menu.getName()});

                // 更新角色权限关系
                List<SysRefRoleMenu> refs = refRoleMenuRDao.findByProperty("menuId", menu.getId());
                if(refs != null && refs.size() >0){
                    Long[] idArray = null;
                    for (SysRefRoleMenu ref : refs) {
                        List<SysRefRoleMenu> userRefList = refRoleMenuRDao.findByProperty("roleId", ref.getRoleId());
                        if(userRefList != null && userRefList.size() > 0){
                            idArray = new Long[userRefList.size()];
                            for (int i = 0; i < userRefList.size(); i++) {
                                idArray[i] = userRefList.get(i).getMenuId();
                            }
                        }
                        roleService.refreshSysRoleRef(ref.getRoleId(),idArray,null);
                        idArray = null;
                    }
                }
            }
        }
    }

    @Override
    public void refreshOrganizeRdd(Long organizeId) {
        LOGGER.info("组织信息 SysOrganize organizeId:{} ",organizeId);
        if(organizeId != null){
            SysOrganize organize = organizeRDao.getById(organizeId);
            if(organize != null){
                organizeRDao.updateByProperties(
                        new String[]{"parentId"},
                        new Object[]{organize.getId()},
                        new String[]{"parentNameRdd"},
                        new Object[]{organize.getName()});
            }
        }
    }

    @Override
    public void refreshUpdateOrganizeRdd(Long organizeId) {
        LOGGER.info("组织信息 SysOrganize organizeId:{} ",organizeId);
        if(organizeId != null){
            SysOrganize organize = organizeRDao.getById(organizeId);
            if(organize != null){
                departmentRDao.updateByProperties(
                        new String[]{"organizeId"},
                        new Object[]{organize.getId()},
                        new String[]{"organizeNameRdd"},
                        new Object[]{organize.getName()});

                // 更新用户、账号 组织冗余信息
                List<SysDepartment> departmentList = departmentRDao.findByProperty("organizeId", organize.getId());
                if(departmentList != null && departmentList.size() >0 ){
                    for (SysDepartment department : departmentList) {
                        refreshUpdateRefSysUserDepartmentRdd(department.getId());
                        refreshUpdateRefSysUserInfoDepartmentRdd(department.getId());
                    }
                }
            }
        }
    }

    @Override
    public void refreshRefSysUserDepartment(Long userId, Long[] departmentIds) {
        LOGGER.info("账号部门信息 SysUser userInfoId:{} departmentIds:{}",userId,departmentIds);

        SysUser user = userRDao.getById(userId);
        if(user != null ){
            // 删除用户部门关系
            refUserDepartmentRDao.deleteByProperty("userId",user.getId());

            if(departmentIds.length > 0){
                // 查询部门信息
                StringBuffer where = new StringBuffer();
                HqlPack.getInPack(StringUtils.join(departmentIds,","),"id",where);
                List<SysDepartment> departmentList = departmentRDao.findByHqlWhere(where.toString());

                if(departmentList != null && departmentList.size() >0){
                    List<SysRefUserDepartment> list = new ArrayList<>();
                    List<String> nameList = new ArrayList<>();
                    SysRefUserDepartment ref = null;
                    for (SysDepartment department : departmentList) {
                        ref = new SysRefUserDepartment();
                        ref.setOrganizeId(department.getOrganizeId());
                        ref.setOrganizeNameRdd(department.getOrganizeNameRdd());
                        ref.setDepartmentId(department.getId());
                        ref.setDepartmentNameRdd(department.getName());
                        ref.setUserId(user.getId());
                        ref.setUserNameRdd(user.getUserName());
                        list.add(ref);

                        nameList.add(department.getName());
                    }

                    user.setDepartmentNamesRdd(String.join(",",nameList));

                    refUserDepartmentRDao.saveAll(list);
                    userRDao.save(user);

                    LOGGER.info("账号部门信息 SysUserInfo RefUserDepartmentRDao Insert Size :{}",list.size());
                }
            }
        }
    }

    @Override
    public void refreshDepartmentRdd(Long departmentId) {
        LOGGER.info("部门信息 SysDepartment departmentId:{} ",departmentId);
        if(departmentId != null){
            SysDepartment department = departmentRDao.getById(departmentId);
            if(department != null){
                departmentRDao.updateByProperties(
                        new String[]{"parentId"},
                        new Object[]{department.getId()},
                        new String[]{"parentNameRdd"},
                        new Object[]{department.getName()});
            }
        }
    }

    @Override
    public void refreshRefSysUserRole(Long userId, Long[] roleIds) {
        LOGGER.debug("账号角色关系信息 RefSysUserRole userId:{} roleIds:{} ", userId,roleIds);
        SysUser user = userRDao.getById(userId);
        if(user != null){
            // 根据IDS 查询所有的角色信息
            String where = CoreHqlPack.packHqlIds("id", roleIds);
            List<SysRole> list = roleRDao.findByHqlWhere(where);

            // 删除角色关系信息
            refUserRoleRDao.deleteByProperty("userId", user.getId()); // 删除用户角色关系

            if(null != list && list.size() > 0){
                // 新增用户角色关系
                List<SysRefUserRole> refUserRoles = new ArrayList<>();
                List<String> roleNameList = new ArrayList<>();
                SysRefUserRole refUserRole = null;
                for (SysRole role : list) {
                    roleNameList.add(role.getName());
                    // 用户角色关系创建
                    refUserRole = new SysRefUserRole();
                    refUserRole.setRoleId(role.getId());
                    refUserRole.setRoleNameRdd(role.getName());
                    refUserRole.setUserId(user.getId());
                    refUserRole.setUserNameRdd(user.getUserName());
                    refUserRole.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
                    refUserRoles.add(refUserRole);
                }
                refUserRoleRDao.saveAll(refUserRoles);

                // 角色名称冗余
                user.setRoleNamesRdd(String.join(",", roleNameList));

                userRDao.save(user);
            }
        }
    }

    @Override
    public void refreshUpdateSysRoleRdd(Long roleId) {
        LOGGER.debug("角色信息 RefSysUserRole roleId:{}", roleId);
        if(roleId != null){
            SysRole role = roleRDao.getById(roleId);
            if(role != null){
                // 更新账号角色关系冗余信息
                refUserRoleRDao.updateByProperties(
                        new String[]{"roleId"},
                        new Object[]{role.getId()},
                        new String[]{"roleNameRdd"},
                        new Object[]{role.getName()}
                );

                // 账号组织关系集合
                List<SysRefUserRole> refs = refUserRoleRDao.findByProperty("roleId", role.getId());
                if(refs != null && refs.size() >0){
                    Long[] idArray = null;
                    for (SysRefUserRole ref : refs) {
                        List<SysRefUserRole> userRefList = refUserRoleRDao.findByProperty("userId", ref.getUserId());
                        if(userRefList != null && userRefList.size() > 0){
                            idArray = new Long[userRefList.size()];
                            for (int i = 0; i < userRefList.size(); i++) {
                                idArray[i] = userRefList.get(i).getRoleId();
                            }
                        }
                        refreshRefSysUserRole(ref.getUserId(),idArray);
                        idArray = null;
                    }
                }
            }
        }
    }

    @Override
    public void refreshUpdateRefSysUserDepartmentRdd(Long departmentId) {
        LOGGER.info("部门信息 SysDepartment departmentId:{} ",departmentId);
        if(departmentId != null){
            SysDepartment department = departmentRDao.getById(departmentId);
            if(department != null){
                // 更新账号组织关系冗余信息
                refUserDepartmentRDao.updateByProperties(
                        new String[]{"departmentId"},
                        new Object[]{department.getId()},
                        new String[]{"departmentNameRdd","organizeId","organizeNameRdd"},
                        new Object[]{department.getName(),department.getOrganizeId(),department.getOrganizeNameRdd()}
                );

                // 更新账号组织冗余信息
                userRDao.updateByProperties(
                        new String[]{"departmentId"},
                        new Object[]{department.getId()},
                        new String[]{"departmentNameRdd","organizeId","organizeNameRdd"},
                        new Object[]{department.getName(),department.getOrganizeId(),department.getOrganizeNameRdd()}
                );

                // 账号组织关系集合
                List<SysRefUserDepartment> refs = refUserDepartmentRDao.findByProperty("departmentId", department.getId());
                if(refs != null && refs.size() >0){
                    Long[] idArray = null;
                    for (SysRefUserDepartment ref : refs) {
                        List<SysRefUserDepartment> userRefList = refUserDepartmentRDao.findByProperty("userId", ref.getUserId());
                        if(userRefList != null && userRefList.size() > 0){
                            idArray = new Long[userRefList.size()];
                            for (int i = 0; i < userRefList.size(); i++) {
                                idArray[i] = userRefList.get(i).getDepartmentId();
                            }
                        }
                        refreshRefSysUserDepartment(ref.getUserId(),idArray);
                        idArray = null;
                    }
                }
            }
        }
    }

    @Override
    public void refreshUpdateRefSysUserInfoDepartment(Long userInfoId, Long[] departmentIds) {
        LOGGER.info("用户部门关系信息 SysUser userInfoId:{} departmentIds:{}",userInfoId,departmentIds);

        SysUserInfo info = userInfoRDao.getById(userInfoId);
        if(info != null ){
            // 删除用户部门关系
            refUserInfoDepartmentRDao.deleteByProperty("userId",info.getId());

            if(departmentIds.length > 0){
                // 查询部门信息
                StringBuffer where = new StringBuffer();
                HqlPack.getInPack(StringUtils.join(departmentIds,","),"id",where);
                List<SysDepartment> departmentList = departmentRDao.findByHqlWhere(where.toString());

                if(departmentList != null && departmentList.size() >0){
                    List<SysRefUserInfoDepartment> list = new ArrayList<>();
                    List<String> nameList = new ArrayList<>();
                    SysRefUserInfoDepartment ref = null;
                    for (SysDepartment department : departmentList) {
                        ref = new SysRefUserInfoDepartment();
                        ref.setOrganizeId(department.getOrganizeId());
                        ref.setOrganizeNameRdd(department.getOrganizeNameRdd());
                        ref.setDepartmentId(department.getId());
                        ref.setDepartmentNameRdd(department.getName());
                        ref.setUserId(info.getId());
                        ref.setUserNameRdd(info.getName());
                        list.add(ref);

                        nameList.add(department.getName());
                    }

                    info.setDepartmentNamesRdd(String.join(",",nameList));

                    refUserInfoDepartmentRDao.saveAll(list);
                    userInfoRDao.save(info);

                    LOGGER.info("用户部门关系信息 SysUserInfo RefUserDepartmentRDao Insert Size :{}",list.size());
                }
            }
        }else {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("用户信息不存在")
                    .setErrorSolution("请重新操作")
                    .build();
        }
    }

    @Override
    public void refreshUpdateRefSysUserInfoDepartmentRdd(Long departmentId) {
        LOGGER.info("部门信息 SysInfoDepartment departmentId:{} ",departmentId);
        if(departmentId != null){
            SysDepartment department = departmentRDao.getById(departmentId);
            if(department != null){
                // 更新账号组织关系冗余信息
                refUserInfoDepartmentRDao.updateByProperties(
                        new String[]{"departmentId"},
                        new Object[]{department.getId()},
                        new String[]{"departmentNameRdd","organizeId","organizeNameRdd"},
                        new Object[]{department.getName(),department.getOrganizeId(),department.getOrganizeNameRdd()}
                );

                // 更新账号组织冗余信息
                userInfoRDao.updateByProperties(
                        new String[]{"departmentId"},
                        new Object[]{department.getId()},
                        new String[]{"departmentNameRdd","organizeId","organizeNameRdd"},
                        new Object[]{department.getName(),department.getOrganizeId(),department.getOrganizeNameRdd()}
                );

                // 账号组织关系集合
                List<SysRefUserInfoDepartment> refs = refUserInfoDepartmentRDao.findByProperty("departmentId", department.getId());
                if(refs != null && refs.size() >0){
                    Long[] idArray = null;
                    for (SysRefUserInfoDepartment ref : refs) {
                        List<SysRefUserInfoDepartment> userRefList = refUserInfoDepartmentRDao.findByProperty("userId", ref.getUserId());
                        if(userRefList != null && userRefList.size() > 0){
                            idArray = new Long[userRefList.size()];
                            for (int i = 0; i < userRefList.size(); i++) {
                                idArray[i] = userRefList.get(i).getDepartmentId();
                            }
                        }
                        refreshRefSysUserDepartment(ref.getUserId(),idArray);
                        idArray = null;
                    }
                }
            }
        }
    }

    @Override
    public void refreshPositionRdd(Long positionId) {
        LOGGER.info("岗位信息 SysPosition positionId:{} ",positionId);
        if(positionId != null){
            SysPosition position = positionRDao.getById(positionId);
            if(position != null){
                positionRDao.updateByProperties(
                        new String[]{"parentId"},
                        new Object[]{position.getId()},
                        new String[]{"parentNameRdd"},
                        new Object[]{position.getName()});
            }
        }
    }

    @Override
    public void refreshRefSysUserInfoPosition(Long userInfoId, Long[] positionIds) {
        LOGGER.info("用户岗位关系信息 RefSysUserInfoPosition userInfoId:{} positionIds:{} userName:{}",userInfoId,positionIds);

        SysUserInfo userInfo = userInfoRDao.getById(userInfoId);
        if(userInfo != null ){
            // 删除用户岗位关系
            refUserPositionRDao.deleteByProperty("userId",userInfo.getId());

            if(positionIds.length > 0){
                StringBuffer where = new StringBuffer();
                HqlPack.getInPack(StringUtils.join(positionIds,","),"id",where);  // 查询部门信息
                List<SysPosition> positionList = positionRDao.findByHqlWhere(where.toString());

                if(positionList != null && positionList.size() >0){
                    List<SysRefUserPosition> list = new ArrayList<>();
                    List<String> nameList = new ArrayList<>();
                    SysRefUserPosition ref = null;
                    for (SysPosition position : positionList) {
                        ref = new SysRefUserPosition();
                        ref.setUserId(userInfo.getId());
                        ref.setUserNameRdd(userInfo.getName());
                        ref.setPositionId(position.getId());
                        ref.setPositionNameRdd(position.getName());
                        list.add(ref);

                        nameList.add(position.getName());
                    }

                    userInfo.setPositionNamesRdd(String.join(",",nameList));

                    refUserPositionRDao.saveAll(list);
                    userInfoRDao.save(userInfo);

                    LOGGER.info("用户岗位关系信息 SysUserInfo RefUserPositionRDao Insert Size :{}",list.size());
                }
            }
        }else {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("用户信息不存在")
                    .setErrorSolution("请重新操作")
                    .build();
        }
    }

    @Override
    public void refreshUpdateRefSysUserInfoPositionRdd(Long positionId) {
        LOGGER.info("岗位信息 SysPosition positionId:{} ",positionId);
        if(positionId != null){
            SysPosition position = positionRDao.getById(positionId);
            if(position != null){
                // 更新用户岗位关系冗余信息
                refUserPositionRDao.updateByProperties(
                        new String[]{"positionId"},
                        new Object[]{position.getId()},
                        new String[]{"positionNameRdd"},
                        new Object[]{position.getName()}
                );

                // 更新用户主岗位冗余信息
                userInfoRDao.updateByProperties(
                        new String[]{"positionId"},
                        new Object[]{position.getId()},
                        new String[]{"positionNameRdd"},
                        new Object[]{position.getName()}
                );

                // 用户用户岗位关系集合
                List<SysRefUserPosition> refs = refUserPositionRDao.findByProperty("positionId", position.getId());
                if(refs != null && refs.size() >0){
                    Long[] positionIdArray = null;
                    for (SysRefUserPosition ref : refs) {
                        List<SysRefUserPosition> userRefList = refUserPositionRDao.findByProperty("userId", ref.getUserId());
                        if(userRefList != null && userRefList.size() > 0){
                            positionIdArray = new Long[userRefList.size()];
                            for (int i = 0; i < userRefList.size(); i++) {
                                positionIdArray[i] = userRefList.get(i).getPositionId();
                            }
                        }
                        refreshRefSysUserInfoPosition(ref.getUserId(),positionIdArray);
                        positionIdArray = null;
                    }
                }
            }
        }
    }

/*********************************** 检查方法封装  ***********************************/

    /**
     * 校验密码长度
     * @return
     */
    private boolean checkPasswordLength(SysConfig config,String password){
        if(config != null){
            if(config.getConfigValue() == null || config.getConfigType().equals(ImppEnumUtil.SYS_VALUE_TYPE.NUMBER.getValue())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("系统配置格式错误(密码长度)")
                        .setErrorSolution("请先配置系统参数")
                        .build();
            }else {
                int length = Integer.parseInt(config.getConfigValue());
                return password.trim().length() >= length;
            }
        }
        return true;
    }

    /**
     * 校验密码小写
     * @param password
     * @return
     */
    private boolean checkPasswordLowerEnglish(SysConfig config,String password){
        if(config != null){
            if(config.getConfigValue() == null || config.getConfigType().equals(ImppEnumUtil.SYS_VALUE_TYPE.SWITCH.getValue())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("系统配置格式错误(密码英文小写配置)")
                        .setErrorSolution("请先配置系统参数")
                        .build();
            }else {
                if(config.getConfigValue().equals(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValueStr())){
                    return StringTool.isContainLowerCase(password);
                }
            }
        }
        return true;
    }

    /**
     * 校验密码大写
     * @param password
     * @return
     */
    private boolean checkPasswordUpperEnglish(SysConfig config,String password){
        if(config != null){
            if(config.getConfigValue() == null || config.getConfigType().equals(ImppEnumUtil.SYS_VALUE_TYPE.SWITCH.getValue())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("系统配置格式错误(密码英文大写配置)")
                        .setErrorSolution("请先配置系统参数")
                        .build();
            }else {
                if(config.getConfigValue().equals(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValueStr())){
                    return StringTool.isContainUpperCase(password);
                }
            }
        }
        return true;
    }

    /**
     * 校验密码大写
     * @param password
     * @return
     */
    private boolean checkPasswordNumber(SysConfig config,String password){
        if(config != null){
            if(config.getConfigValue() == null || config.getConfigType().equals(ImppEnumUtil.SYS_VALUE_TYPE.SWITCH.getValue())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("系统配置格式错误(密码数字配置)")
                        .setErrorSolution("请先配置系统参数")
                        .build();
            }else {
                if(config.getConfigValue().equals(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValueStr())){
                    return StringTool.isContainNumber(password);
                }
            }
        }
        return true;
    }

    /**
     * 校验密码特殊字符
     * @param password
     * @return
     */
    private boolean checkPasswordSpecialChar(SysConfig config,String password){
        if(config != null){
            if(config.getConfigValue() == null || config.getConfigType().equals(ImppEnumUtil.SYS_VALUE_TYPE.SWITCH.getValue())){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.NOT_CONFIG_EXCEPTION.getCode())
                        .setErrorDetail("系统配置格式错误(密码特殊字符配置)")
                        .setErrorSolution("请先配置系统参数")
                        .build();
            }else {
                if(config.getConfigValue().equals(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValueStr())){
                    return StringTool.isConSpeCharacters(password);
                }
            }
        }
        return true;
    }

    /**
     * 登录名 唯一检查
     * @param userLoginName
     * @param userId
     * @return
     */
    private boolean checkSysUserLoginName(String userLoginName, long userId) {
        int count = 0;
        if(userId > 0){
            StringBuffer sw = new StringBuffer();
            HqlPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(),"isValid",sw);
            HqlPack.getStringEqualPack(userLoginName,"userLoginName",sw);
            HqlPack.getNumNOEqualPack(userId,"id",sw);
            count = userRDao.findByHqlWhereCount(sw.toString());

        }else{
            count = userRDao.findByPropertyCount(
                    new String[]{"userLoginName","isValid"},
                    new Object[]{userLoginName,CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        }
        return count <= 0 ;
    }

    /**
     * 用户邮箱唯一检查
     * @param email
     * @param infoId
     * @return
     */
    private boolean checkSysUserEmail(String email, long infoId) {
        int count;
        if(infoId > 0){
            StringBuffer sw = new StringBuffer();
            HqlPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(),"isValid",sw);
            HqlPack.getStringEqualPack(email,"userEmail",sw);
            HqlPack.getNumNOEqualPack(infoId,"userInfoId",sw);
            count = userRDao.findByHqlWhereCount(sw.toString());

        }else{
            count = userRDao.findByPropertyCount(
                    new String[]{"userEmail","isValid"},
                    new Object[]{email,CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        }
        return count <= 0 ;
    }

    /**
     * 手机号码 唯一校验
     * @param phone
     * @param infoId
     * @return
     */
    private boolean checkSysUserPhone(String phone, long infoId) {
        int count;
        if(infoId > 0){
            StringBuffer sw = new StringBuffer();
            HqlPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(),"isValid",sw);
            HqlPack.getStringEqualPack(phone,"userPhone",sw);
            HqlPack.getNumNOEqualPack(infoId,"userInfoId",sw);
            count = userRDao.findByHqlWhereCount(sw.toString());

        }else{
            count = userRDao.findByPropertyCount(
                    new String[]{"userPhone","isValid"},
                    new Object[]{phone,CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        }
        return count <= 0 ;
    }

    /**
     * 工号唯一检查
     * @param empNo
     * @param infoId
     * @return
     */
    private boolean checkSysUserEmpNo(String empNo, long infoId) {
        int count;
        if(infoId > 0){
            StringBuffer sw = new StringBuffer();
            HqlPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(),"isValid",sw);
            HqlPack.getStringEqualPack(empNo,"userEmpNo",sw);
            HqlPack.getNumNOEqualPack(infoId,"userInfoId",sw);
            count = userRDao.findByHqlWhereCount(sw.toString());

        }else{
            count = userRDao.findByPropertyCount(
                    new String[]{"userEmpNo","isValid"},
                    new Object[]{empNo,CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        }
        return count <= 0 ;
    }
}
