Перейти к основному содержанию
`Ctrl`+`K`

# Генерация проектных спецификаций

Создает или связывает проектные спецификации на основе рабочих спецификаций, проекта и версии проекта. Используется для массового восстановления проектных документов по данным MCT.

Место применения: `Сервис > Инструменты > Выполнить JEXL-скрипт`

Внимание

Скрипт привязан к объектам конкретного модуля и требует проверки на целевой базе. Перед запуском проверьте идентификаторы, SQL-запросы, API-классы и условия обработки.

Тип: JEXL-скрипт

    var nvCount = sql(`
    	select count(*) as "nCount"
    	from (
    		select s.sdesignation
    		from mct_specification s
    		join bs_prj p on s.idprj = p.id
    		join bs_prjVer pv on s.idprjver = pv.id
    		join btk_objectType ot on s.idobjecttype = ot.id
    		join mct_objecttype mot on mot.idbtkobjecttype = ot.id
    		join mct_specificationver sv on sv.idmctdocument = s.id and sv.bislast = 1
    		left join mct_specification sp on sp.scode = s.sdesignation and sp.idPrj = s.idPrj
    		left join mct_objectType motsp on motsp.idbtkobjecttype = sp.idobjecttype
    		left join mct_objectType motp on s.idobjecttype = motp.idwrkdoc
    		left join mct_specification sop on sop.scode = s.sdesignation and sop.idPrj != s.idPrj
    		left join mct_document d on d.scode = s.sdesignation and getgidclass(d.gidref) != cast(44751 as bigint)
    		where coalesce(p.bnotactive, 0) = 0
    			and coalesce(pv.bnotactive, 0) = 0
    			and pv.scode != 'ОЗР'
    			and pv.scode != '00000'
    			and coalesce(mot.bisprjdoc, 0) = 0
    			and ot.scode != 'UnitProduct'
    			and s.gidprjdoc is null
    			and sv.idstatemc < 400
    			and s.scode != s.sdesignation
    			and s.sdesignation != ''
    			and (motsp.id is null or motsp.bisprjdoc = 1)
    			and sop.id is null
    			and d.gidref is null
    		group by
    			s.sdesignation
    			,sp.id
    			,s.idPrj
    			,s.idDepOwner
    	) t
    	`).asSingle().nCount;
    var nvIter = nvCount / 100 + 1;
    for (i : (1 .. nvIter)){
    	var select = sql(`
    		select
    			s.sdesignation as "sDesignation"
    			,sp.id as "idPrjSpec"
    			,max(motp.idbtkobjecttype) as "idPrjObjectType"
    			,array_agg(s.id) as "idaWrkDoc"
    			,s.idPrj as "idPrj"
    			,s.idDepOwner as "idDepOwner"
    			,max(s.scaption) as "sCaption"
    		from mct_specification s
    		join bs_prj p on s.idprj = p.id
    		join bs_prjVer pv on s.idprjver = pv.id
    		join btk_objectType ot on s.idobjecttype = ot.id
    		join mct_objecttype mot on mot.idbtkobjecttype = ot.id
    		join mct_specificationver sv on sv.idmctdocument = s.id and sv.bislast = 1
    		left join mct_specification sp on sp.scode = s.sdesignation and sp.idPrj = s.idPrj
    		left join mct_objectType motsp on motsp.idbtkobjecttype = sp.idobjecttype
    		left join mct_objectType motp on s.idobjecttype = motp.idwrkdoc
    		left join mct_specification sop on sop.scode = s.sdesignation and sop.idPrj != s.idPrj
    		left join mct_document d on d.scode = s.sdesignation and getgidclass(d.gidref) != cast(44751 as bigint)
    		where coalesce(p.bnotactive, 0) = 0
    		and coalesce(pv.bnotactive, 0) = 0
    		and pv.scode != 'ОЗР'
    		and pv.scode != '00000'
    		and coalesce(mot.bisprjdoc, 0) = 0
    		and ot.scode != 'UnitProduct'
    		and s.gidprjdoc is null
    		and sv.idstatemc < 400
    		and s.scode != s.sdesignation
    		and s.sdesignation != ''
    		and (motsp.id is null or motsp.bisprjdoc = 1)
    		and sop.id is null
    		and d.gidref is null
    		group by
    			s.sdesignation
    			,sp.id
    			,s.idPrj
    			,s.idDepOwner
    		order by s.sdesignation
    		limit 100
    		`);
    	select.foreach(function(r){
    		var idvPrjSpec;
    		if(r.idPrjSpec == null){
    			var t = Mct_DocPkg.createSpecification(r.idPrjObjectType, r.idPrj, r.idDepOwner, null, null, null, null, null);
    			var rop = Mct_SpecificationApi.load(t);
    			Mct_SpecificationApi.setsCode(rop, r.sDesignation);
    			Mct_SpecificationApi.setsCaption(rop, r.sCaption);
    			idvPrjSpec = t;
    		} else {
    			idvPrjSpec = r.idPrjSpec
    		};
    		var ropPrjSpec = Mct_SpecificationApi.load(idvPrjSpec);
    		var gidvPrjLastVer = Mct_DocumentApi.getGidLastVersionByDoc(ropPrjSpec.gid(), null, false, false);
    		var ropList = sql(`
    			select
    				s.id
    			from mct_specification s
    			join bs_prj p on s.idprj = p.id
    			join bs_prjVer pv on s.idprjver = pv.id
    			join btk_objectType ot on s.idobjecttype = ot.id
    			join mct_objecttype mot on mot.idbtkobjecttype = ot.id
    			join mct_specificationver sv on sv.idmctdocument = s.id and sv.bislast = 1
    			where sdesignation = '`+r.sDesignation+`'
    			and idPrj =`+r.idPrj+`
    			and coalesce(p.bnotactive, 0) = 0
    			and coalesce(pv.bnotactive, 0) = 0
    			and pv.scode != 'ОЗР'
    			and pv.scode != '00000'
    			and coalesce(mot.bisprjdoc, 0) = 0
    			and ot.scode != 'UnitProduct'
    			and s.gidprjdoc is null
    			and sv.idstatemc < 400
    			and s.scode != s.sdesignation
    		`).batchObjLoad(Mct_SpecificationApi, "id");
    		for (ropCurWorkSpec : ropList){
    			var gidvWorkSpecVer = Mct_DocumentApi.getGidLastVersionByDoc(ropCurWorkSpec.gid(), null, false, false);
    			Mct_SpecificationApi.setgidPrjDoc(ropCurWorkSpec, ropPrjSpec.gid());
    			//Создадим записи в связанных документах проектной СП
    			Mct_LinkApi.registerByVer(ropPrjSpec.gid(),gidvWorkSpecVer, null, null);
    			//Создадим записи в связанных документах рабочей СП
    			Mct_LinkApi.registerByVer(ropCurWorkSpec.gid(),gidvPrjLastVer, null, null);
    		};
    	});
    	commit();
    }