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


-- 以下是用户定义的语句 
--=========================================================================================
 --中间表操作时，ord_sjczlx字段有内容,此时不做处理
 if(exists(select 1 from inserted as i,deleted as d where i.ord_id = d.ord_id and i.ord_sjczlx <> d.ord_sjczlx)
  or not exists(select 1 from inserted)
 )
    return ;

--=========创建营运分公司
declare @depart table(id int primary key identity(1,1), dpname varchar(100),dpcode varchar(30))
insert into @depart(dpname,dpcode)
select distinct isnull(ord_szfgs,'') ,dbo.GetPY(ord_szfgs)+upper(substring(sys.fn_sqlvarbasetostr(HashBytes('MD5',ord_szfgs)),3,6)) code 
from order_188 left join ebs2java.dbo.mps_depart with(nolock)
on dp_depart = isnull(ord_szfgs,'')
where  dp_code is null
and ord_dpzt != '停止营业'

insert into ebs2java.dbo.mps_depart(
dp_bcid,dp_cmpid,dp_code,dp_isdepart,dp_depart,dp_fullname,dp_display,dp_fdpid,dp_level,
dp_province,dp_municipal,dp_county,dp_location,dp_note,dp_status,dp_use,dp_feebudget,dp_salebudget,
dp_manager6,dp_mcuid6,
dp_manager5,dp_mcuid5,
dp_manager4,dp_mcuid4,
dp_manager3,dp_mcuid3,
dp_manager2,dp_mcuid2,
dp_manager1,dp_mcuid1
,dp_manager10 --= '江昆笛'
,dp_mcuid10  --= 282
,dp_managearea  
,dp_order
)
select 13,1,dpcode,0,dpname,dpname,dpname,244,1,
'','','','','分公司','',0,0,0,
'',0,
'',0,
'',0,
'',0,
'',0,
'',0,
'江昆笛',282,'',37
from @depart    
where dpname !=''
--=========创建营运分公司


 update ebs2java.dbo.mps_depart with(rowlock)
set  dp_code = i.ord_dpbh
,dp_depart = i.ord_dpmc
,dp_fullname = i.ord_dpmc
,dp_display = i.ord_dpmc
,dp_province =i.ord_sf
,dp_municipal = i.ord_cs
,dp_county = i.ord_qx
,dp_location = i.ord_dpdz
,dp_note = i.ord_dplx+'门店'
,dp_manager1 = isnull(i.ord_dpfzr,'')
,dp_mcuid1 = isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(i.ord_fzrcode,'') and isnull(cu_account,'')>''),0)
,dp_manager2 = isnull(i.ord_dz,'')
,dp_mcuid2 = isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(i.ord_dzcode,'') and isnull(cu_account,'')>''),0)
,dp_manager3 = i.ord_pqdd
,dp_mcuid3 = isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(i.ord_pqddcode,'') and isnull(cu_account,'')>''),0)
,dp_manager4 = i.ord_pqjl 
,dp_mcuid4 = isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(i.ord_pqjlcode,'') and isnull(cu_account,'')>''),0)
,dp_manager5 = i.ord_pqzj
,dp_mcuid5 = isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(i.ord_pqzjcode,'') and isnull(cu_account,'')>''),0)
,dp_manager6 = i.ord_fz
,dp_mcuid6 =  isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(i.ord_fzcode,'') and isnull(cu_account,'')>''),0)
,dp_status = i.ord_dpzt
,dp_manager10 = '江昆笛'
,dp_mcuid10  = 282
,dp_use = case when isnull(i.ord_dpzt,'')='正常营业'  then 0 else 1 end
,dp_managearea = isnull(d.ord_szpq,'')
,dp_manager = isnull(d.ord_szpq,'')
,dp_mcuid =isnull((select top 1 cu_id from ebs2java.dbo.mps_employee where cu_name =isnull(d.ord_szpq,'')and cu_title in ('分公司总经理','片区总监','片区经理','片区督导','管理专员') ),0)
,dp_fdpid = ISNULL((select top 1 dp_id from mps_depart with(nolock) where dp_depart = isnull(d.ord_szfgs,'')),2575) 
 from inserted i ,deleted d
 where dp_code = d.ord_dpbh
 and d.ord_id = i.ord_id 
 --and 操作类型 ='初始化' 

insert into ebs2java.dbo.mps_depart(
dp_bcid,dp_cmpid,dp_code,dp_isdepart,dp_depart,dp_fullname,dp_display,dp_fdpid,dp_level,
dp_province,dp_municipal,dp_county,dp_location,dp_note,dp_status,dp_use,
dp_manager6,dp_mcuid6,
dp_manager5,dp_mcuid5,
dp_manager4,dp_mcuid4,
dp_manager3,dp_mcuid3,
dp_manager2,dp_mcuid2,
dp_manager1,dp_mcuid1
,dp_manager10 --= '江昆笛'
,dp_mcuid10  --= 282
,dp_managearea
,dp_manager
,dp_mcuid
)
select 13,1,ord_dpbh,1,ord_dpmc,ord_dpmc,ord_dpmc,ISNULL((select top 1 dp_id from mps_depart with(nolock) where dp_depart = isnull(ord_szfgs,'')),2575),1,--营运部
ord_sf,ord_cs,ord_qx,ord_dpdz,ord_dplx+'门店',ord_dpzt,case when isnull(ord_dpzt,'')='正常营业'  then 0 else 1 end,
ord_fz,isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(ord_fzcode,'') and isnull(cu_account,'')>''),0) fzcuid,
ord_pqzj,isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(ord_pqzjcode,'') and isnull(cu_account,'')>''),0) zjid,
ord_pqjl,isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(ord_pqjlcode,'') and isnull(cu_account,'')>''),0) jlcuid,
ord_pqdd,isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(ord_pqddcode,'') and isnull(cu_account,'')>''),0) ddcuid,
ord_dz,isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(ord_dzcode,'') and isnull(cu_account,'')>''),0) dzcuid,
ord_dpfzr,isnull((select top 1 cu_id from ebs2java.dbo.mps_employee with(nolock) where isnull(cu_account,'') = isnull(ord_fzrcode,'') and isnull(cu_account,'')>''),0) fzrcudid,
'江昆笛',282,isnull(ord_szpq,''),isnull(ord_szpq,''),isnull((select top 1 cu_id from ebs2java.dbo.mps_employee where cu_name =isnull(ord_szpq,'') and cu_title in ('分公司总经理','片区总监','片区经理','片区督导','管理专员') ),0)
from inserted left join ebs2java.dbo.mps_depart with(nolock)
on dp_code = isnull(ord_dpbh,'')-- or dp_depart = isnull(店仓名称,'')
where  '正常营业' =  ord_dpzt
and dp_code is null


--添加门店店长、店员岗位到门店。
insert into ebs2java.dbo.mps_depart_title(dt_dpid,dt_otid)
select dp_id,207  --店长
from inserted ,
ebs2java.dbo.mps_depart with(nolock)
where dp_code = ord_dpbh
and not exists(select 1 from ebs2java.dbo.mps_depart_title with(nolock) 
where dt_dpid = dp_id and dt_otid = 207
)

insert into ebs2java.dbo.mps_depart_title(dt_dpid,dt_otid)
select dp_id,213  --门店负责人
from inserted ,
ebs2java.dbo.mps_depart with(nolock)
where dp_code = ord_dpbh
and not exists(select 1 from ebs2java.dbo.mps_depart_title with(nolock) 
where dt_dpid = dp_id and dt_otid = 213
)

insert into ebs2java.dbo.mps_depart_title(dt_dpid,dt_otid)
select dp_id,208  --店员
from inserted ,
ebs2java.dbo.mps_depart with(nolock)
where dp_code = ord_dpbh
and not exists(select 1 from ebs2java.dbo.mps_depart_title with(nolock) 
where dt_dpid = dp_id and dt_otid = 208
)

--=========================================================================================
-- 用户定义的语句完毕 

end