IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_40_insert]') AND type = 'TR')
DROP TRIGGER [dbo].[order_40_insert]
GO
CREATE  trigger order_40_insert on order_40 for insert as
begin
   insert into mps_sync(sk_type,sk_bizid,sk_tag2,sk_operation,sk_objid) 
   select 1,ord_bizid,0,1,ord_id from inserted 

-- 以下是用户定义的语句 
/*======================================================================*/ 
 --中间表操作时，ord_sjczlx字段有内容,此时不做处理
 if exists(select 1  from inserted where isnull(ord_sjczlx,'')>''
 or not exists(select 1 from inserted)
 ) return ;
/*插入两种数据：营运+行政，其中营运需要判断是否是管理人员，如果是管理人员，则将其放在营运部下营运团队，否则就在当前档案数据所指定的门店中。*/
declare @usersinfo table
(
ordid bigint ,
tx varchar(100),
branchid int ,
branchcode varchar(100),
branchname varchar(100),
departid int,
departcode varchar(100),
depart varchar(100),
titleid int,
titlecode varchar(100),
title varchar(100),
cuname varchar(100),
cucode varchar(100)
)

declare @yingyunmanager table
(
ordernum int,
branchid int ,
branchcode varchar(100),
branchname varchar(100),
departid int,
departcode varchar(100),
depart varchar(100),
titleid int,
titlecode varchar(100),
title varchar(100),
cuname varchar(100),
cucode varchar(100)
)


insert into @usersinfo(ordid,tx,branchname,branchcode,branchid,depart,departcode,title,titlecode,titleid,cuname,cucode)
select yg.ord_id ,'营运' tx,'重庆仟丝缘商业连锁有限公司' gs,'QSY' gscode,13 bc_id,yg.ord_zzzb dp,yg.ord_dpbh dpcode,'店员' ot_title,'YYMDDY' ot_code,null tid,yg.ord_xm,isnull(yg.ord_gh,'')
from inserted yg,ebs2java.dbo.order_188 with(nolock) 
where yg.ord_zzzk = '在职' and order_188.ord_dpzt='正常营业'
and yg.ord_dpbh = order_188.ord_dpbh
and yg.ord_rztx ='营运'
and isnull(yg.ord_gh,'')>''
union
select yg.ord_id ,'行政' tx,bc_company gs,bc_code gscode,bc_id,dp_depart dp,dp_code,ot_title,ot_code,ot_id tid,yg.ord_xm,isnull(yg.ord_gh,'')
from inserted yg 
,ebs2java.dbo.mps_depart with(nolock)
,ebs2java.dbo.mps_title with(nolock)
,ebs2java.dbo.mps_groupbranch
where yg.ord_zzzk = '在职'
and yg.ord_rztx ='行政' 
and dp_id = case when yg.ord_zbdpid>0 then yg.ord_zbdpid else yg.ord_bmdpid end
and yg.ord_zw = ot_title
and bc_cmpid = 1  
and bc_company = yg.ord_rzzx
and isnull(yg.ord_gh,'')>''
and yg.ord_bcid = dp_bcid


insert into @yingyunmanager(ordernum,branchname,branchcode,branchid,departcode,depart,title,titlecode,cuname,cucode)
select distinct 1 ordernum,'重庆仟丝缘商业连锁有限公司','QSY',13,'YYTD' ord_dpbh,'营运团队' ord_dpmc, '分公司总经理' title,'FGSZJL' tcode,ord_fz name,isnull(ord_fzcode,'') gh 
from ebs2java.dbo.order_188 with(nolock) where isnull(ord_fz,'')<>'' and ord_dpzt!='停止营业'  and ord_dpzt!='暂停营业' 
union
select distinct 2 ordernum,'重庆仟丝缘商业连锁有限公司','QSY',13,'YYTD' ord_dpbh,'营运团队' ord_dpmc,'片区总监' title,'PQZJ' tcode,ord_pqzj name,isnull(ord_pqzjcode,'') gh 
from ebs2java.dbo.order_188 with(nolock) where isnull(ord_pqzj,'')<>'' and ord_dpzt!='停止营业' and ord_dpzt!='暂停营业' 
union
select distinct 3 ordernum,'重庆仟丝缘商业连锁有限公司','QSY',13,'YYTD' ord_dpbh,'营运团队' ord_dpmc,'片区经理' title,'PQJL' tcode,ord_pqjl name,isnull(ord_pqjlcode,'') gh 
from ebs2java.dbo.order_188 with(nolock) where isnull(ord_pqjl,'')<>'' and ord_dpzt!='停止营业' and ord_dpzt!='暂停营业' 
union
select distinct 4 ordernum,'重庆仟丝缘商业连锁有限公司','QSY',13,'YYTD' ord_dpbh,'营运团队' ord_dpmc,'片区督导' title,'PQDD' tcode,ord_pqdd name,isnull(ord_pqddcode,'') gh 
from ebs2java.dbo.order_188 with(nolock) where isnull(ord_pqdd,'')<>'' and ord_dpzt!='停止营业' and ord_dpzt!='暂停营业' 
union
select distinct 5 ordernum,'重庆仟丝缘商业连锁有限公司','QSY',13,ord_dpbh,ord_dpmc,'店长' title,'YYMMDZ' tcode,ord_dz name,isnull(ord_dzcode,'') gh 
from ebs2java.dbo.order_188 with(nolock)where isnull(ord_dz,'')<>'' and ord_dpzt!='停止营业' and ord_dpzt!='暂停营业' 
union
select distinct 6 ordernum,'重庆仟丝缘商业连锁有限公司','QSY',13,ord_dpbh,ord_dpmc,'店铺负责人' title,'YYDPFZR' tcode,ord_dpfzr name,isnull(ord_fzrcode,'') gh 
from ebs2java.dbo.order_188 with(nolock) where isnull(ord_dpfzr,'')<>'' and ord_dpzt!='停止营业' and ord_dpzt!='暂停营业' 

update @yingyunmanager 
set departid= dp_id
from ebs2java.dbo.mps_depart with(nolock)
where dp_code = departcode
and dp_bcid = branchid

update @yingyunmanager 
set titlecode= ot_code,
titleid = ot_id
from ebs2java.dbo.mps_title with(nolock)
where ot_title = title

--确定是否是管理人员
update d
set d.title = s.title
,d.titlecode = s.titlecode
,d.titleid = s.titleid
,d.depart = s.depart
,d.departcode = s.departcode
,d.departid = s.departid
from  @yingyunmanager s,@usersinfo d
where s.cucode = d.cucode
and d.tx = '营运'
 
 ----更新岗位
update d
set d.title = s.ot_title
,d.titlecode = s.ot_code
,d.titleid = s.ot_id
from  ebs2java.dbo.mps_title s with(nolock),@usersinfo d
where s.ot_code = d.titlecode
and d.titleid is null

 
--更新部门id
update d
set d.departid = s.dp_id
from  ebs2java.dbo.mps_depart s with(rowlock),@usersinfo d
where s.dp_code = d.departcode
and d.departid is null
and dp_bcid = branchid

delete @usersinfo from ebs2java.dbo.order_40 with(nolock)
where isnull(ord_gh,'') = cucode 
and ordid != ord_id

--select * from  @yingyunmanager

--select* from  @usersinfo

  --开始往员工表插入数据
  insert into ebs2java.dbo.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)
  select 1 cmpid,cucode,cucode,cuname,case when isnull(ord_xb,'') ='男' then 1 else  0 end ,ord_lxdh,
departid,depart,branchid,branchname,ord_ygzt,ord_sr,ord_yx,'',ord_sfzszd,'','',titleid,
case when tx='营运' then 5 else 6 end ,title,ord_posid,1,
isnull((select top 1 mg_grade from ebs2java.dbo.mps_manage_grade  where mg_cmpid = 1 and mg_caption = ord_gldj) ,1)
,0
  from @usersinfo,inserted --ebs2java.dbo.order_40 
  where isnull(ord_gh,'') = cucode
  and departid is not null
  and titleid  is not null
  and not exists(select 1 from ebs2java.dbo.mps_employee with(nolock) where cucode = cu_account)

 
  insert into ebs2java.dbo.mps_login(
cu_id,cu_objid,cu_cmpid,cu_type,cu_account,cu_callname,cu_gender,cu_function,
cu_pass
,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_title,cu_otid,cu_otlevel,cu_managelevel,cu_professionlevel,
cu_qyh,cu_mobile)
   select cu_id,cu_id,cu_cmpid,0,cucode,cuname,case when isnull(ord_xb,'') ='男' then 1 else  0 end ,cu_function,
case when tx ='营运' then  'd41d8cd98f00b204e9800998ecf8427e'/**/ else  '2e7cf164d920a3a04afec65e76ab90df' /*qsyyg*/ end 
,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_title,cu_otid,cu_otlevel,
isnull((select top 1 mg_grade from ebs2java.dbo.mps_manage_grade  where mg_cmpid = 1 and mg_caption = ord_gldj) ,1)
,cu_professionlevel
,case when ismobile = 1 and upper(accounttype) = 'QYH' then [text] else 'wx64bb4ed8bfd6d90a|' + cucode end,cu_mobile
  from @usersinfo
,inserted  --,ebs2java.dbo.order_40 with(nolock)
,ebs2java.dbo.mps_employee with(nolocK)  left join mps_weixinident on cuid = cu_Id 
  where isnull(ord_gh,'') = cucode
  and isnull(ord_gh,'') = cu_code
  and departid is not null
  and titleid  is not null
  and not exists(select 1 from ebs2java.dbo.mps_login d with(nolock) where cucode = d.cu_account)
  
  --插入登录角色表
  insert into ebs2java.dbo.[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,cuname,cu_bcid,cu_branch,cu_dpid,
cu_depart,cu_otid,cu_title,cu_function,
isnull((select top 1 mg_grade from ebs2java.dbo.mps_manage_grade  where mg_cmpid = 1 and mg_caption = ord_gldj) ,1),
cu_professionlevel,cu_otlevel,0
  from @usersinfo
,inserted --ebs2java.dbo.order_40 with(nolock),
,ebs2java.dbo.mps_employee with(nolocK)
  where isnull(ord_gh,'') = cucode
  and isnull(ord_gh,'') = cu_code
  and departid is not null
  and titleid  is not null
/*======================================================================*/ 
-- 用户定义的语句完毕 

end