Oracle --登陆

create or replace procedure SP_LOGIN(i_account        in t_account.acc_id%type, --登陆帐号
                                     i_password       in t_account.password%type, --登陆密码
                                     o_cur_resultInfo out sys_refcursor, --登录成功返回消息
                                     o_errNumber      out integer) --错误码
 is
  --帐号表数据
  l_rtyAccount t_account%rowtype;
  --区域表数据
  l_zoneData t_zone%rowtype;
  --经销商数据
  l_fchsData t_franchiser%rowtype;
  --门店数据
  l_storeData t_store%rowtype;
  --销售员数据
  l_selrData t_seller%rowtype;
  --异常
  l_exception exception;
  --错误消息
  l_error_msg varchar2(100);
  /**********
  *creater:       lxl
  *craeateTime:   2015-07-23
  *function:      Login
  **********/
begin
  /******
  errorNumber 
  0:登陆成功
  1:帐号不存在
  2:帐号存在但无效
  3:密码不正确
  4:总部登陆查询出错
  5:区域状态错误
  6:经销商登陆查询出错
  7:门店登陆查询出错
  8:销售员登陆查询出错
  9:登陆异常
  *******/
  pkg_utility.sp_writeLog(i_log_type => 'INFO',
                          i_acc_id   => i_account,
                          i_sp_name  => 'SP_LOGIN',
                          i_log_desc => '开始');

  o_errNumber := PKG_CONSTANTS.C_RTN_SUCCESS;
  l_error_msg := '';

  --1.判断帐号是否存在且启用
  begin
    select *
      into l_rtyAccount
      from t_account
     where acc_id = trim(i_Account);
  exception
    when no_data_found then
      --帐号不存在
      o_errNumber := 1;
      l_error_msg := '帐号不存在';
    
      --在这里加个异常处理,处理游标值
      raise l_exception;
  end;

  --2.判断帐号是否已启用
  if l_rtyAccount.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
    o_errNumber := 2;
    l_error_msg := '帐号无效';
  
    --在这里加个异常处理,处理游标值
    raise l_exception;
  end if;

  --3.判断密码是否正确可用用输入的密码和原密码相比较
  if l_rtyAccount.Password != trim(i_password) then
    o_errNumber := 3;
    l_error_msg := '密码错误';
  
    --在这里加个异常处理,处理游标值
    raise l_exception;
  end if;

  --4.帐号正确,密码正确
  /***
  直接判断操做状态便可
  0:总部
  1:经销商
  2:门店
  3:销售员
  ***/
  --总部
  if l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_HEAD then
    begin
      open o_cur_resultInfo for
        select acc_id as accId,
               role_type as roleType,
               role_id as roleId,
               status as status,
               spcl_acc_flg as spclAccFlag,
               '' as zoneId,
               '' as zoneName,
               '' as zoneState,
               '' as fchsId,
               '' as fchsName,
               '' as fchsContacts,
               '' as fchsTelephone1,
               '' as fchsTelephone2,
               '' as fchsState,
               '' as storeId,
               '' as storeName,
               '' as storeAddr,
               '' as storeContacts,
               '' as storeTelephone1,
               '' as storeTelephone2,
               '' as storeState,
               '' as sellerId,
               '' as sellerName,
               '' as sellerTelephone,
               '' as sellerState
          from t_account
         where acc_id = trim(i_account)
           and status = PKG_CONSTANTS.C_STATUS1_ENABLE
           and password = trim(i_password);
    exception
      when others then
        --总部登陆查询出错
        o_errNumber := 4;
        l_error_msg := '总部登陆查询出错';
    end;
    --经销商
  elsif l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_FCHS then
    begin
      --1.根据角色代码去经销商表取数据
      select *
        into l_fchsData
        from t_franchiser tf
       where tf.fchs_id = l_rtyAccount.Role_Id;
    
      --2.判断经销商状态是否可用
      if l_fchsData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
        o_errNumber := 6;
        l_error_msg := '经销商状态不可用';
      
        --在这里加个异常处理,处理游标值
        raise l_exception;
      else
        --查询区域全部数据
        begin
          select *
            into l_zoneData
            from t_zone tz
           where tz.zone_id = l_fchsData.Zone_Id;
        
          --判断区域状态是否可用
          if l_zoneData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
            o_errNumber := 5;
            l_error_msg := '区域状态不可用';
          
            --在这里加个异常处理,处理游标值
            raise l_exception;
          else
            begin
              --返回数据
              open o_cur_resultInfo for
                SELECT TA.acc_id as accId,
                       TA.role_type as roleType,
                       ta.role_id as roleId,
                       TA.status as status,
                       TA.spcl_acc_flg as spclAccFlag,
                       TZ.zone_id as zoneId,
                       TZ.zone_name as zoneName,
                       TZ.status as zoneState,
                       TF.fchs_id as fchsId,
                       TF.fchs_name as fchsName,
                       TF.contacts as fchsContacts,
                       TF.telephone1 as fchsTelephone1,
                       TF.telephone2 as fchsTelephone2,
                       TF.status as fchsState,
                       '' as storeId,
                       '' as storeName,
                       '' as storeAddr,
                       '' as storeContacts,
                       '' as storeTelephone1,
                       '' as storeTelephone2,
                       '' as storeState,
                       '' as sellerId,
                       '' as sellerName,
                       '' as sellerTelephone,
                       '' as sellerState
                  FROM T_zone tz, t_franchiser tf, t_account ta
                 WHERE TZ.zone_id = l_fchsData.Zone_Id
                   and tz.status = PKG_CONSTANTS.C_STATUS1_ENABLE
                   AND TF.fchs_id = l_rtyAccount.Role_Id
                   and tf.status = PKG_CONSTANTS.C_STATUS1_ENABLE
                   and TA.acc_id = trim(i_account)
                   and TA.status = PKG_CONSTANTS.C_STATUS1_ENABLE
                   and TA.password = trim(i_password);
            exception
              when others then
                --经销商登陆查询出错
                o_errNumber := 6;
                l_error_msg := '经销商登陆查询出错';
            end;
          end if;
        end;
      end if;
    end;
    --门店
  elsif l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_STORE then
    begin
    
      --1.根据角色代码去门店表取数据
      select *
        into l_storeData
        from t_store ts
       where ts.store_id = l_rtyAccount.Role_Id;
    
      --2.判断门店状态是否可用
      if l_storeData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
        o_errNumber := 7;
        l_error_msg := '门店状态不可用';
      
        raise l_exception;
      else
        begin
          --3.查询经销商数据
          select *
            into l_fchsData
            from t_franchiser tf
           where tf.fchs_id = l_storeData.Fchs_Id;
        
          --判断经销商状态
          if l_fchsData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
            o_errNumber := 6;
            l_error_msg := '经销商状态不可用';
          
            raise l_exception;
          else
            --查询区域全部数据
            begin
              select *
                into l_zoneData
                from t_zone tz
               where tz.zone_id = l_fchsData.Zone_Id;
            
              --判断区域状态是否可用
              if l_zoneData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
                o_errNumber := 5;
                l_error_msg := '区域状态不可用';
              
                raise l_exception;
              else
                begin
                  open o_cur_resultInfo for
                    SELECT TA.acc_id as accId,
                           TA.role_type as roleType,
                           TA.role_id as roleId,
                           TA.status as status,
                           TA.spcl_acc_flg as spclAccFlag,
                           TZ.zone_id as zoneId,
                           TZ.zone_name as zoneName,
                           TZ.status as zoneState,
                           TF.fchs_id as fchsId,
                           TF.fchs_name as fchsName,
                           TF.contacts as fchsContacts,
                           TF.telephone1 as fchsTelephone1,
                           TF.telephone2 as fchsTelephone2,
                           TF.status as fchsState,
                           TSR.STORE_ID as storeId,
                           TSR.STORE_NAME as storeName,
                           TSR.STORE_ADDR as storeAddr,
                           TSR.CONTACTS as storeContacts,
                           TSR.TELEPHONE1 as storeTelephone1,
                           TSR.TELEPHONE2 as storeTelephone2,
                           TSR.STATUS as storeState,
                           '' as sellerId,
                           '' as sellerName,
                           '' as sellerTelephone,
                           '' as sellerState
                      FROM t_zone       tz,
                           t_franchiser tf,
                           t_store      tsr,
                           t_account    ta
                     WHERE TZ.zone_id = l_fchsData.Zone_Id
                       AND TZ.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                       AND TF.fchs_id = l_storeData.Fchs_Id
                       AND TF.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                       AND tsr.store_id = l_rtyAccount.Role_Id
                       AND TSR.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                       and TA.acc_id = trim(i_account)
                       and TA.status = PKG_CONSTANTS.C_STATUS1_ENABLE
                       and TA.password = trim(i_password);
                exception
                  when others then
                    --门店登陆查询出错
                    o_errNumber := 7;
                    l_error_msg := '门店登陆查询出错';
                end;
              end if;
            end;
          end if;
        end;
      end if;
    end;
    --销售员
  elsif l_rtyAccount.Role_Type = PKG_CONSTANTS.C_ROLETYPE_SELLER then
    begin
      --1.根据销售员代码去门店表取数据
      select *
        into l_selrData
        from t_seller tsr
       where tsr.selr_id = l_rtyAccount.Role_Id;
    
      --2.判断销售员状态是否可用
      if l_selrData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
        o_errNumber := 8;
        l_error_msg := '销售员状态不可用';
      
        raise l_exception;
      else
        begin
          --查询门店数据
          --1.根据角色代码去门店表取数据
          select *
            into l_storeData
            from t_store ts
           where ts.store_id = l_selrData.Store_Id;
        
          --2.判断门店状态是否可用
          if l_storeData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
            o_errNumber := 7;
            l_error_msg := '门店状态不可用';
          
            raise l_exception;
          else
            begin
              --3.查询经销商数据
              select *
                into l_fchsData
                from t_franchiser tf
               where tf.fchs_id = l_storeData.Fchs_Id;
            
              --判断经销商状态
              if l_fchsData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
                o_errNumber := 6;
                l_error_msg := '经销商状态不可用';
              
                raise l_exception;
              else
                --查询区域全部数据
                begin
                  select *
                    into l_zoneData
                    from t_zone tz
                   where tz.zone_id = l_fchsData.Zone_Id;
                
                  --判断区域状态是否可用
                  if l_zoneData.Status != PKG_CONSTANTS.C_STATUS1_ENABLE then
                    o_errNumber := 5;
                    l_error_msg := '区域状态不可用';
                  
                    raise l_exception;
                  else
                    begin
                      open o_cur_resultInfo for
                        SELECT TA.acc_id as accId,
                               TA.role_type as roleType,
                               ta.role_id as roleId,
                               TA.status as status,
                               TA.spcl_acc_flg as spclAccFlag,
                               TZ.zone_id as zoneId,
                               TZ.zone_name as zoneName,
                               TZ.status as zoneState,
                               TF.fchs_id as fchsId,
                               TF.fchs_name as fchsName,
                               TF.contacts as fchsContacts,
                               TF.telephone1 as fchsTelephone1,
                               TF.telephone2 as fchsTelephone2,
                               TF.status as fchsState,
                               TSR.STORE_ID as storeId,
                               tsr.STORE_NAME as storeName,
                               TSR.STORE_ADDR as storeAddr,
                               TSR.CONTACTS as storeContacts,
                               TSR.TELEPHONE1 as storeTelephone1,
                               TSR.TELEPHONE2 as storeTelephone2,
                               TSR.STATUS as storeState,
                               TS.SELR_ID as sellerId,
                               TS.SELR_NAME as sellerName,
                               TS.TELEPHONE as sellerTelephone,
                               TS.STATUS as sellerState
                          FROM T_zone       tz,
                               t_franchiser tf,
                               t_store      tsr,
                               t_seller     ts,
                               t_account    ta
                         WHERE TZ.zone_id = l_fchsData.Zone_Id
                           AND TZ.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                           AND TF.fchs_id = l_storeData.Fchs_Id
                           AND TF.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                           AND tsr.store_id = l_selrData.Store_Id
                           AND TSR.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                           AND TS.selr_id = l_rtyAccount.Role_Id
                           AND TS.STATUS = PKG_CONSTANTS.C_STATUS1_ENABLE
                           and TA.acc_id = trim(i_account)
                           and TA.status = PKG_CONSTANTS.C_STATUS1_ENABLE
                           and TA.password = trim(i_password);
                    exception
                      when others then
                        --销售员登陆查询出错
                        o_errNumber := 8;
                        l_error_msg := '销售员登陆查询出错';
                    end;
                  end if;
                end;
              end if;
            end;
          end if;
        end;
      end if;
    end;
  end if; --总部 

  pkg_utility.sp_writeLog(i_log_type => 'INFO',
                          i_acc_id   => i_account,
                          i_sp_name  => 'SP_LOGIN',
                          i_log_desc => '结束');
exception
  when l_exception then
    --异常时也须要向游标里面装值
    open o_cur_resultInfo for
      select '' as accId,
             '' as roleType,
             '' as roleId,
             '' as status,
             '' as spclAccFlag,
             '' as zoneId,
             '' as zoneName,
             '' as zoneState,
             '' as fchsId,
             '' as fchsName,
             '' as fchsContacts,
             '' as fchsTelephone1,
             '' as fchsTelephone2,
             '' as fchsState,
             '' as storeId,
             '' as storeName,
             '' as storeAddr,
             '' as storeContacts,
             '' as storeTelephone1,
             '' as storeTelephone2,
             '' as storeState,
             '' as sellerId,
             '' as sellerName,
             '' as sellerTelephone,
             '' as sellerState
        from dual --这里用dual不用其它的表,由于dual里面确定有一条记录,而其它的表不必定会有一条记录
       where rownum = 1;
  
  when others then
    o_errNumber := 9;
    l_error_msg := '登陆异常';
    pkg_utility.sp_writeLog(i_log_type => 'ERROR',
                            i_acc_id   => i_account,
                            i_sp_name  => 'SP_LOGIN',
                            i_log_desc => l_error_msg);
end SP_LOGIN;
相关文章
相关标签/搜索