IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[proj_153_loaduser]') AND type = 'P')
DROP PROCEDURE [dbo].[proj_153_loaduser]
GO
CREATE proc [dbo].[proj_153_loaduser] (
@conditions nvarchar(1000),
@ordid  bigint
) 
AS
begin
set nocount on ;
--清除本业务单存在的人员信息
delete from order_153_102 where ord2_ordid = @ordid
--筛选条件是直营店员
if(@conditions = '直营人员')
begin
--插入明细表(order_153_102)，去除重复
/*营运管理层*/
insert into order_153_102(ord2_ordid,ord2_tag,ord2_gh,ord2_xm,ord2_szdp,ord2_sfzh,ord2_sjh,ord2_tsrcuid)
select distinct @ordid,
102,
m.cu_account as 工号,
m.cu_callname as 姓名,
o.ord_zzzb as 所在店铺,
o.ord_sfzhm as 身份证号,
o.ord_lxdh as 手机号,
m.cu_id as CUID
from mps_login m,order_40 o
where o.ord_spwpry = '否' and o.ord_wpdp =''and o.ord_gh = m.cu_account and o.ord_zzzk = '在职' 
and o.ord_zzzb not like '%测试%' and o.ord_zhij in ('督导','区域经理','片区总监','分公司总经理')
and m.cu_dpid not in(3785,3786,3787,3788)

/*直营店铺(店长，员工)三级和试用期员工除外*/
insert into order_153_102(ord2_ordid,ord2_tag,ord2_gh,ord2_xm,ord2_szdp,ord2_sfzh,ord2_sjh,ord2_tsrcuid)
select distinct @ordid,
102,
m.cu_account as 工号,
m.cu_callname as 姓名,
o.ord_zzzb as 所在店铺,
o.ord_sfzhm as 身份证号,
o.ord_lxdh as 手机号,
m.cu_id as CUID
from mps_login m,order_40 o,mps_depart d,order_188 p
where o.ord_spwpry = '否' and o.ord_wpdp ='' and o.ord_gh = m.cu_account and o.ord_zzzk = '在职' 
and o.ord_zzzb not like '%测试%' and m.cu_dpid = d.dp_id and d.dp_code = p.ord_dpbh 
and p.ord_dplx = '直营' and ord_zhij not in ('试用期员工','三级店员') 
and d.dp_id not in(3785,3786,3787,3788)

/*外派人员(外派店铺是直营)*/
insert into order_153_102(ord2_ordid,ord2_tag,ord2_gh,ord2_xm,ord2_szdp,ord2_sfzh,ord2_sjh,ord2_tsrcuid)
select distinct @ordid,
102,
m.cu_account as 工号,
m.cu_callname as 姓名,
o.ord_zzzb as 所在店铺,
o.ord_sfzhm as 身份证号,
o.ord_lxdh as 手机号,
m.cu_id as CUID
from mps_login m,order_40 o,order_188 p
where o.ord_spwpry='是' and o.ord_wpdp <> '' and m.cu_account = o.ord_gh and o.ord_wpdp = p.ord_dpmc 
and p.ord_dplx = '直营' and o.ord_zzzk ='在职' and o.ord_zzzb not like '%测试%'
and ord_zhij not in ('试用期员工','三级店员') and m.cu_dpid not in(3785,3786,3787,3788)
end

--筛选条件是加盟商
else if(@conditions = '加盟商')

begin
--插入明细表(order_153_102)，去除重复
/*加盟商(加盟商员工，加盟商)*/
insert into order_153_102(ord2_ordid,ord2_tag,ord2_gh,ord2_xm,ord2_szdp,ord2_sfzh,ord2_sjh,ord2_tsrcuid)
select distinct
@ordid,
102,
m.cu_account as 工号,
m.cu_callname as 姓名,
o.ord_zzzb as 所在店铺,
o.ord_sfzhm as 身份证号,
o.ord_lxdh as 手机号,
m.cu_id as CUID
from mps_login m ,order_40 o,mps_depart d,order_188 p
where o.ord_spwpry = '否' and o.ord_wpdp =''and o.ord_gh = m.cu_account and o.ord_zzzk = '在职' 
and o.ord_zzzb not like '%测试%' and m.cu_dpid = d.dp_id and d.dp_code = p.ord_dpbh 
and p.ord_dplx = '加盟' and d.dp_id not in(3785,3786,3787,3788)

/*外派人员(外派店铺为加盟)*/
insert into order_153_102(ord2_ordid,ord2_tag,ord2_gh,ord2_xm,ord2_szdp,ord2_sfzh,ord2_sjh,ord2_tsrcuid)
select distinct
@ordid,
102,
m.cu_account as 工号,
m.cu_callname as 姓名,
o.ord_zzzb as 所在店铺,
o.ord_sfzhm as 身份证号,
o.ord_lxdh as 手机号,
m.cu_id as CUID
from mps_login m ,order_40 o,mps_depart d,order_188 p
where o.ord_spwpry = '是' and o.ord_zzzk = '在职' and o.ord_zzzb not like '%测试%' 
and m.cu_account = o.ord_gh and o.ord_wpdp = p.ord_dpmc and p.ord_dplx = '加盟' 
and d.dp_id not in(3785,3786,3787,3788)

/*外派人员(外派店铺为空)*/
insert into order_153_102(ord2_ordid,ord2_tag,ord2_gh,ord2_xm,ord2_szdp,ord2_sfzh,ord2_sjh,ord2_tsrcuid)
select distinct
@ordid,
102,
m.cu_account as 工号,
m.cu_callname as 姓名,
o.ord_zzzb as 所在店铺,
o.ord_sfzhm as 身份证号,
o.ord_lxdh as 手机号,
m.cu_id as CUID
from mps_login m ,order_40 o,mps_depart d,order_188 p
where o.ord_spwpry = '是' and o.ord_zzzk = '在职' and o.ord_zzzb not like '%测试%' 
and m.cu_account = o.ord_gh and o.ord_wpdp = '' and d.dp_id not in(3785,3786,3787,3788)
end

select 'ok' result
set nocount off;
end