IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_employee]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_employee]
GO

CREATE procedure [dbo].[mps_new_employee](
@cu_cmpid int,
@cu_code varchar(32) ,
@cu_account varchar(32) ,
@cu_pass nvarchar(32),
@cu_name nvarchar(50) ,
@cu_gender int ,
@cu_mobile varchar(20) ,
@cu_dpid int ,
@cu_depart nvarchar(50) ,
@cu_bcid int,
@cu_birth varchar(10),
@cu_email varchar(100),
@cu_contact nvarchar(100),
@cu_address nvarchar(100),
@cu_major nvarchar(30),
@cu_qq varchar(20),
@cu_otid int,
@cu_function int,
@cu_title varchar(30),
@cu_managelevel int,
@cu_professionlevel int,
@cu_realationshipid  bigint = 0,
@cu_ccphonenumber varchar(50)
)
AS
BEGIN
SET NOCOUNT ON;

declare @count int
select @count=count(*) from mps_employee   with(nolock)  where cu_code=@cu_code
if(@count>0)
begin
select '1' as flag --flag=1 表示员工编号重复
return
end
else begin
--判断employee表是否重复
select @count=count(*) from mps_employee    with(nolock)  where cu_account=@cu_account
if(@count>0)
begin
select '2' as flag --flag=2
return
end
select @count=count(*) from mps_login    with(nolock)  where cu_account=@cu_account
if(@count>0)
begin
select '2' as flag --flag=2
return
end else begin
declare @cu_otlevel int
select @cu_otlevel=ot_grade from mps_title where ot_id=@cu_otid
--开始往员工表插入数据
insert into mps_employee(cu_cmpid,cu_code,cu_account,cu_name,cu_gender,cu_mobile,cu_dpid,cu_depart,cu_bcid,cu_branch,cu_status,cu_birth,cu_email,cu_contact,cu_address,cu_major,cu_qq,cu_otid,cu_function,cu_title,cu_relationshipobj1,cu_otlevel,cu_managelevel,cu_professionlevel,cu_ccphonenumber)
values(@cu_cmpid,@cu_code,@cu_account,@cu_name,@cu_gender,@cu_mobile,@cu_dpid,@cu_depart,@cu_bcid,isnull((select top 1 bc_company from mps_branch where bc_id = @cu_bcid),'集团总部'),'在职',@cu_birth,@cu_email,@cu_contact,@cu_address,@cu_major,@cu_qq,@cu_otid,@cu_function,@cu_title,@cu_realationshipid,@cu_otlevel,@cu_managelevel,@cu_professionlevel,@cu_ccphonenumber);
--获取刚才插入的数据
declare @cu_id int=0
select @cu_id = cu_id 
from mps_employee 
where cu_cmpid = @cu_cmpid 
and cu_code = @cu_code 
and cu_account = @cu_account 
and cu_name = @cu_name and 
cu_mobile = @cu_mobile
--更新mps_employe中的cu_power字段
if @cu_function = 4 
update mps_employee set cu_power = 'all' where cu_id = @count
else 
update mps_employee set cu_power = '' where cu_id = @count
--将数据插入到登录表
insert into mps_login(cu_id,cu_objid,cu_cmpid,cu_type,cu_account,cu_callname,cu_gender,cu_pass,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_otlevel,cu_managelevel,cu_professionlevel,cu_mobile)
select @cu_id,@cu_id,cu_cmpid,0,cu_account,cu_name,cu_gender,@cu_pass,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_otlevel,cu_managelevel,cu_professionlevel,cu_mobile
from mps_employee    with(nolock)
where cu_id=@cu_id
--将数据插入到登录角色表 (注意,这里创建,需要保证他的lgu_defaultrole=0)
insert into [mps_login_role](
 [lgu_cuid]
,[lgu_name]
,[lgu_bcid]
,[lgu_branch]
,[lgu_dpid]
,[lgu_depart]
,[lgu_otid]
,[lgu_title]
,[lgu_function]
,[lgu_managelevel]
,[lgu_professionlevel]
,[lgu_otlevel]
,lgu_defaultrole)
select
@cu_id,
cu_name,
cu_bcid,
cu_branch,
cu_dpid,
cu_depart,
cu_otid,
cu_title,
cu_function,
cu_otlevel,
cu_managelevel,
cu_professionlevel,
0
from mps_employee with(nolock)
where cu_id=@cu_id
-- 生成login表代岗位信息 author:f
exec mps_update_logindeparttitles @cu_id
-- 返回最新数据
select cu_id,cu_code,cu_cmpid,cu_account,cu_name,cu_gender,cu_bcid,cu_dpid,cu_depart,cu_title,'0' as flag
from mps_employee
where cu_id=@cu_id
end
end
SET NOCOUNT OFF
--select * from mps_login
--在mps_login 中插入数据
END