IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_update_loginrole]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_update_loginrole]
GO
CREATE proc [dbo].[mps_update_loginrole]
@lgu_id int ,
@lgu_cuid int,
@lgu_bcid int,
@lgu_dpid int,
@lgu_otid int,
@lgu_function int,
@lgu_managelevel int,
@lgu_professionlevel int

as
begin
  SET NOCOUNT ON

  declare @lgu_branch nvarchar(400),
  @lgu_depart nvarchar(100),
  @lgu_title nvarchar(100)

  declare @lgu_otlevel int
  if @lgu_otid != -999999999
  begin
    select @lgu_otlevel = ot_grade  from mps_title where ot_id = @lgu_otid
  end

  if @lgu_otlevel is null  
  begin
    set @lgu_otlevel=-999999999
  end

  set @lgu_branch=case when @lgu_bcid=-999999999 then '_none_' else (select bc_company from mps_branch where bc_id=@lgu_bcid) end
  set @lgu_depart=case when @lgu_dpid=-999999999 then '_none_' else (select dp_depart from mps_depart where dp_id=@lgu_dpid) end
  set @lgu_title=case when @lgu_otid=-999999999 then '_none_' else (select ot_title from mps_title where ot_id=@lgu_otid) end

  if(@lgu_branch is null)
    set @lgu_branch=''
  if(@lgu_depart is null)
    set @lgu_depart=''
  if(@lgu_title is null)
    set @lgu_title=''
  
  update mps_login_role
  set lgu_bcid = case when @lgu_bcid =-999999999 then lgu_bcid else @lgu_bcid end,
  lgu_branch = case when @lgu_branch ='_none_' then lgu_branch else @lgu_branch end,
  lgu_dpid = case when @lgu_dpid =-999999999 then lgu_dpid else @lgu_dpid end,
  lgu_depart = case when @lgu_depart ='_none_' then lgu_depart else @lgu_depart end,
  lgu_otid = case when @lgu_otid =-999999999 then lgu_otid else @lgu_otid end,
  lgu_title = case when @lgu_title ='_none_' then lgu_title else @lgu_title end,
  lgu_function = case when @lgu_function =-999999999 then lgu_function else @lgu_function end,
  lgu_otlevel = case when @lgu_otlevel =-999999999 then lgu_otlevel else @lgu_otlevel end,
  lgu_professionlevel = case when @lgu_professionlevel =-999999999 then lgu_professionlevel else @lgu_professionlevel end,
  lgu_managelevel = case when @lgu_managelevel =-999999999 then lgu_managelevel else @lgu_managelevel end
  where lgu_id=@lgu_id
  
  --update mps_login_role set lgu_branch='集团总部' where lgu_bcid=0

  --更新mps_login表中的数据,保证数据始终是正确的
  update mps_login set
      cu_account = b.cu_account,
      cu_callname = b.cu_name,
      cu_gender = b.cu_gender,
      cu_bcid = b.cu_bcid,
      cu_branch = b.cu_branch,
      cu_dpid = b.cu_dpid,
      cu_depart = b.cu_depart,
      cu_otid = b.cu_otid,
      cu_title = b.cu_title,
      cu_function = b.cu_function,
      cu_mobile = b.cu_mobile,
      cu_otlevel = b.cu_otlevel,
      cu_managelevel = b.cu_managelevel,
      cu_professionlevel = b.cu_professionlevel
  from mps_employee b
  where mps_login.cu_type = 0 and cu_objid = @lgu_cuid
  



  --判断缺省角色是否被修改,如果缺省角色被修改了,则创建一个,要保证缺省角色始终存在
  --查询数据的条数,需要保证的是employee中的那条数据始终存在
  --获取修改的对象对应的userid
  /*
  select @lgu_cuid = lgu_cuid
  from mps_login_role
  where lgu_id = @lgu_id

  declare @count int
  SELECT @count = COUNT(*) 
  FROM mps_login_role,mps_employee 
  WHERE lgu_cuid=@lgu_cuid 
  and cu_id=lgu_cuid 
  and cu_bcid=lgu_bcid 
  and cu_dpid=lgu_dpid 
  and cu_otid=lgu_otid 
  and cu_function=lgu_function
  and cu_function=lgu_function
  and cu_otlevel = lgu_otlevel
  and cu_managelevel = lgu_managelevel
  and cu_professionlevel = lgu_professionlevel

  
  --如果不存在employee表中的角色,则添加一条记录
  IF(@count=0)
  BEGIN
    INSERT INTO mps_login_role(lgu_cuid,lgu_name,lgu_bcid,lgu_branch,lgu_dpid,lgu_depart,lgu_otid,lgu_title,lgu_function)
    SELECT cu_id,cu_name,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_otid,cu_title,cu_function
    FROM mps_employee
    where cu_id=@lgu_cuid
    select * from mps_login_role where lgu_id=@@IDENTITY
  END
  */
  -- 更新登录表中的代岗位信息 author:f
  exec mps_update_logindeparttitles @lgu_cuid

  update mps_login_role set lgu_branch='集团总部' where lgu_bcid=0
end