@Override
  @Transactional(readOnly = false)
  public int save(SysLogin entity, Long[] roleIds, Long deptId) throws Exception {
    String hql = "select count(*) from SysLogin as model where model.loginName= ?)";
    long count = baseDao.findLong(hql, entity.getLoginName());
    if (count > 0) {
      return ResultConstants.SAVE_FAILED_NAME_IS_EXIST;
    }

    hql = "select count(*) from SysLogin as model where model.userCode= ?)";
    count = baseDao.findLong(hql, entity.getUserCode());
    if (count > 0) {
      return ResultConstants.SAVE_FAILED_CODE_IS_EXIST;
    }

    hql = "select count(*) from SysLogin as model where model.idCard= ?)";
    count = baseDao.findLong(hql, entity.getIdCard());
    if (count > 0) {
      return ResultConstants.SAVE_FAILED_NAME_IS_EXIST;
    }

    entity.setLoginPwd(MD5Utils.toMD5(entity.getLoginPwd()));
    baseDao.save(entity);
    sysLoginRoleService.saveLoginRole(entity, roleIds);
    // 保存系统日志
    String operDesc = "【用户新增】用户名:" + entity.getLoginName();
    sysLogService.save(SysLog.OPERATE_TYPE_ADD, operDesc);
    return ResultConstants.SAVE_SUCCEED;
  }
 /**
  * 根据密码和新密码 进行密码修改
  *
  * @param loginPwd 原来的密码
  * @param newLoginPwd 新密码
  * @return 成功或者失败
  * @throws Exception 抛出异常
  */
 @Override
 @Transactional(readOnly = false, rollbackFor = Throwable.class)
 public int updPassword(String loginPwd, String newLoginPwd, HttpServletRequest request)
     throws Exception {
   LoginToken loginToken =
       (LoginToken) SessionUtils.getObjectAttribute(request, SessionNameConstants.LOGIN_TOKEN);
   if (loginToken.getSysLogin().getLoginPwd().equals(MD5Utils.toMD5(loginPwd))) {
     SysLogin login = loginToken.getSysLogin();
     // 用MD5算法加密
     login.setLoginPwd(MD5Utils.toMD5(newLoginPwd));
     baseDao.update(login);
     // 保存系统日志
     String operDesc = "【修改密码】用户名:" + login.getLoginName();
     sysLogService.save(SysLog.OPERATE_TYPE_UPDATE, operDesc);
     return ResultConstants.UPDATE_SUCCEED;
   } else return ResultConstants.UPDATE_FAILED;
 }
 @Override
 @Transactional(readOnly = false)
 public int resetPwd(Long loginId) throws Exception {
   SysLogin sysLogin = baseDao.findById(loginId);
   sysLogin.setLoginPwd(MD5Utils.toMD5(CommonConstants.DEFAULT_PWD));
   baseDao.update(sysLogin);
   // 保存系统日志
   String operDesc = "【密码重置】用户名:" + sysLogin.getLoginName();
   sysLogService.save(SysLog.OPERATE_TYPE_UPDATE, operDesc);
   return ResultConstants.UPDATE_SUCCEED;
 }
  @Override
  @Transactional(readOnly = false)
  public int update(SysLogin entity, Long[] roleIds, String newLoginPwd) throws Exception {
    // editby 孙强伟  at 20130624 , 由于是修改用户信息,此时,用户登陆名称是不可以修改的,国此必须先获得旧的记录将其用户登陆名称赋给新的entity.
    SysLogin oldSysLogin = baseDao.findById(entity.getLoginId());
    if (null == oldSysLogin) {
      return ResultConstants.UPDATE_RECORD_NOT_EXIST;
    }

    entity.setLoginName(oldSysLogin.getLoginName());

    String hql =
        "select count(*) from SysLogin as model where model.loginName= ? and model.loginId != ?)";
    long count = baseDao.findLong(hql, entity.getLoginName(), entity.getLoginId());
    if (count > 0) {
      return ResultConstants.UPDATE_FAILED_NAME_IS_EXIST;
    }

    hql = "select count(*) from SysLogin as model where model.userCode= ? and model.loginId != ?)";
    count = baseDao.findLong(hql, entity.getUserCode(), entity.getLoginId());
    if (count > 0) {
      return ResultConstants.UPDATE_FAILED_CODE_IS_EXIST;
    }

    hql = "select count(*) from SysLogin as model where model.idCard= ? and model.loginId != ?)";
    count = baseDao.findLong(hql, entity.getIdCard(), entity.getLoginId());
    if (count > 0) {
      return ResultConstants.UPDATE_FAILED_IDCARD_IS_EXIST;
    }

    if (newLoginPwd != null && !"".equals(newLoginPwd)) {
      entity.setLoginPwd(MD5Utils.toMD5(newLoginPwd));
    }

    BeanUtils.copyProperties(oldSysLogin, entity);

    baseDao.update(oldSysLogin);
    sysLoginRoleService.updateLoginRole(oldSysLogin, roleIds);
    // 保存系统日志
    String operDesc = "【用户修改】用户名:" + entity.getLoginName();
    sysLogService.save(SysLog.OPERATE_TYPE_UPDATE, operDesc);
    return ResultConstants.UPDATE_SUCCEED;
  }
  @Override
  @Transactional(readOnly = false, rollbackFor = Throwable.class)
  public int login(
      String loginName,
      String loginPwd,
      String checkCode,
      Long systemId,
      HttpServletRequest request)
      throws Exception {
    // 检查验证码是否正确
    if (!SessionUtils.checkSessionValue(
        request, SessionNameConstants.IMG_CHECK_CODE, checkCode)) // 验证失败
    {
      return ResultConstants.IMG_CODE_FAILED;
    }

    // 检查登录名和密码是否正确 MD5Utils.toMD5(loginPwd)
    SysLogin sysLogin =
        ((SysLoginDAO) baseDao).login(loginName, MD5Utils.toMD5(loginPwd), systemId);
    if (sysLogin == null) {
      return ResultConstants.LOGIN_INFO_FAILED;
    }
    if (sysLogin.getStatus().equals(CommonConstants.STATUS_INVALID)) {
      return ResultConstants.LOGIN_INFO_INVAILD;
    }

    // 加载登录用户的相关信息到登录令牌
    LoginToken loginToken = this.getAdminLoginToken(sysLogin);
    // 保存登录用户信息到http session
    SessionUtils.setObjectAttribute(request, SessionNameConstants.LOGIN_TOKEN, loginToken);

    // 修改最后登录时间
    sysLogin.setLoginLastTime(DateTimeUtil.getChar14());
    baseDao.update(sysLogin);
    // 保存系统日志
    sysLogService.save(SysLog.OPERATE_TYPE_LOGIN, "【用户登录】用户名:" + sysLogin.getLoginName());
    return ResultConstants.LOGIN_SUCCESS;
  }