IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_prdctgbase]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_prdctgbase]
GO
create PROCEDURE [dbo].[mps_save_prdctgbase]
-- Add the parameters for the stored procedure here
@pc_id int,
@cmpid int,
@pc_father int,
@pc_bmcodevarchar(200),
@pc_typenvarchar(100),
@pc_code nvarchar(255),
@pc_iconvarchar(200),
@pc_shareint,
@pc_keysnvarchar(4000),
@pc_databasevarchar(50),
@pc_groupfieldsnvarchar(200),
@pc_securityint,
@pc_orderint,
@pc_querytimeint,
@pc_fullnvarchar(500),
@pc_searchfieldsvarchar(2000),
@pc_selectfuncsnvarchar(500),
@pc_selectstyleint,
@pc_funcsvarchar(200),
@pc_flowsnvarchar(2000),
@pc_paramsvarchar(500),
@pc_viewscopeint,
@pc_viewsint,
@pc_selectnvarchar(4000),
@pc_fromnvarchar(2000),
@pc_wherenvarchar(4000),
@pc_orderbynvarchar(2000) ,
@pc_taggroupfieldsnvarchar(200),
@pc_tagquerytimeint,
@pc_tagsearchfieldsvarchar(2000),
@pc_tagselectstyleint,
@pc_tagselectnvarchar(4000),
@pc_tagfromnvarchar(2000),
@pc_tagwherenvarchar(4000),
@pc_tagorderbynvarchar(2000)  

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @pcfather bigint
DECLARE @order int,@level1 int,@order1 int
select @pcfather = pc_father,@order1 = pc_order from mps_prdctg where pc_id = @pc_id

declare @id1 int = 0,@id2 int
select top 1 @id1 = pc_id from mps_prdctg where pc_father = @pc_id
if(@id1>0 and @pc_father != @pcfather and @pc_Father != -999999999)
begin 
select 'error' error,'保存失败！该分类下存在子类，请先移除子类' errorinfo
return
end

    -- Insert statements for procedure here
UPDATE mps_prdctg SET 
pc_bmcode =  CASE WHEN @pc_bmcode = '_none_' THEN pc_bmcode ELSE @pc_bmcode END,
pc_code =  CASE WHEN @pc_code = '_none_' THEN pc_code ELSE @pc_code END,
pc_type =  CASE WHEN @pc_type = '_none_' THEN pc_type ELSE @pc_type END,
pc_icon =  CASE WHEN @pc_icon = '_none_' THEN pc_icon ELSE @pc_icon END,
pc_share = CASE WHEN @pc_share = -999999999 THEN pc_share ELSE @pc_share END,
pc_father = CASE WHEN @pc_father = -999999999 THEN pc_father ELSE @pc_father END,
pc_keys = CASE WHEN @pc_keys = '_none_' THEN '' ELSE @pc_keys END,
pc_database=  CASE WHEN @pc_database = '_none_' THEN pc_database ELSE @pc_database END,
pc_groupfields=  CASE WHEN @pc_groupfields = '_none_' THEN pc_groupfields ELSE @pc_groupfields END,
pc_taggroupfields=  CASE WHEN @pc_taggroupfields = '_none_' THEN pc_taggroupfields ELSE @pc_taggroupfields END,
pc_security= CASE WHEN @pc_security = -999999999 THEN pc_security ELSE @pc_security END,
pc_order= CASE WHEN @pc_order = -999999999 THEN pc_order ELSE @pc_order END,
pc_querytime= CASE WHEN @pc_querytime = -999999999 THEN pc_querytime ELSE @pc_querytime END,
pc_tagquerytime= CASE WHEN @pc_tagquerytime = -999999999 THEN pc_tagquerytime ELSE @pc_tagquerytime END,
pc_full=  CASE WHEN @pc_full = '_none_' THEN pc_full ELSE @pc_full END,
pc_searchfields=  CASE WHEN @pc_searchfields = '_none_' THEN pc_searchfields ELSE @pc_searchfields END,
pc_tagsearchfields=  CASE WHEN @pc_tagsearchfields = '_none_' THEN pc_tagsearchfields ELSE @pc_tagsearchfields END,
pc_selectfuncs=  CASE WHEN @pc_selectfuncs = '_none_' THEN pc_selectfuncs ELSE @pc_selectfuncs END,
pc_selectstyle= CASE WHEN @pc_selectstyle = -999999999 THEN pc_selectstyle ELSE @pc_selectstyle END,
pc_tagselectstyle= CASE WHEN @pc_tagselectstyle = -999999999 THEN pc_tagselectstyle ELSE @pc_tagselectstyle END,
pc_funcs=  CASE WHEN @pc_funcs = '_none_' THEN pc_funcs ELSE @pc_funcs END,
pc_flows=  CASE WHEN @pc_flows = '_none_' THEN pc_flows ELSE @pc_flows END,
pc_params=  CASE WHEN @pc_params = '_none_' THEN pc_params ELSE @pc_params END,
pc_viewscope= CASE WHEN @pc_viewscope = -999999999 THEN pc_viewscope ELSE @pc_viewscope END,
pc_views= CASE WHEN @pc_views = -999999999 THEN pc_views ELSE @pc_views END,
pc_select=  CASE WHEN @pc_select = '_none_' THEN pc_select ELSE @pc_select END,
pc_from=  CASE WHEN @pc_from = '_none_' THEN pc_from ELSE @pc_from END,
pc_where=  CASE WHEN @pc_where = '_none_' THEN pc_where ELSE @pc_where END,
pc_orderby=  CASE WHEN @pc_orderby = '_none_' THEN pc_orderby ELSE @pc_orderby END ,
pc_tagselect=  CASE WHEN @pc_tagselect = '_none_' THEN pc_tagselect ELSE @pc_tagselect END,
pc_tagfrom=  CASE WHEN @pc_tagfrom = '_none_' THEN pc_tagfrom ELSE @pc_tagfrom END,
pc_tagwhere=  CASE WHEN @pc_tagwhere = '_none_' THEN pc_tagwhere ELSE @pc_tagwhere END,
pc_tagorderby=  CASE WHEN @pc_tagorderby = '_none_' THEN pc_tagorderby ELSE @pc_tagorderby END 
WHERE pc_id = @pc_id AND pc_cmpid = @cmpid

--pc_father changed

declare @pctag int 
declare @ffpcid int
select top 1 @pctag=pc_tag from mps_prdctg WHERE pc_id = @pc_id AND pc_cmpid = @cmpid 
if(@pc_father != 0 and @pc_Father != -999999999)
begin
if(@pc_father = 0) 
begin
select @ffpcid = pc_ffpcid from mps_prdctg where pc_id = @pc_id
select @order = max(pc_order) from mps_prdctg where pc_ffpcid = @ffpcid
set @level1 = 0
end else
begin
SELECT TOP 1 @order=pc_order,@level1 = pc_level,@ffpcid = case when pc_ffpcid =0 then @pc_father else  pc_ffpcid end
FROM mps_prdctg WHERE pc_id = @pc_father
set @level1 = ISNULL(@level1+1,0)
end
SET @order = ISNULL(@order,0)
update mps_prdctg set pc_order = pc_order +2 where pc_cmpid=@cmpid AND pc_order > @order and pc_id != @pc_father

SET @order = ISNULL(@order+1,0)
update mps_prdctg SET 
pc_order = @order,
pc_level = @level1,
pc_ffpcid = case when @pc_father = 0 then  0 else @ffpcid end
where pc_id = @pc_id AND pc_cmpid = @cmpid

select @cmpid as cmpid,@pctag as pctag
exec mps_gen_prdctg_power @cmpid,@pc_id,@pc_father
end
exec mps_update_prdctginfo @cmpid,@pctag


UPDATE mps_prdctg SET 
pc_taggroupfields=  CASE WHEN @pc_taggroupfields = '_none_' THEN pc_taggroupfields ELSE @pc_taggroupfields END,
pc_tagquerytime= CASE WHEN @pc_tagquerytime = -999999999 THEN pc_tagquerytime ELSE @pc_tagquerytime END,
pc_tagsearchfields=  CASE WHEN @pc_tagsearchfields = '_none_' THEN pc_tagsearchfields ELSE @pc_tagsearchfields END,
pc_tagselectstyle= CASE WHEN @pc_tagselectstyle = -999999999 THEN pc_tagselectstyle ELSE @pc_tagselectstyle END,
pc_tagselect=  CASE WHEN @pc_tagselect = '_none_' THEN pc_tagselect ELSE @pc_tagselect END,
pc_tagfrom=  CASE WHEN @pc_tagfrom = '_none_' THEN pc_tagfrom ELSE @pc_tagfrom END,
pc_tagwhere=  CASE WHEN @pc_tagwhere = '_none_' THEN pc_tagwhere ELSE @pc_tagwhere END,
pc_tagorderby=  CASE WHEN @pc_tagorderby = '_none_' THEN pc_tagorderby ELSE @pc_tagorderby END 
WHERE pc_tag = @pctag AND pc_cmpid = @cmpid



END