create procedure proc_paydoc_help()
begin
        DECLARE incount int(10) DEFAULT 0;
        
	declare attr_id varchar(32);
	declare in_projid varchar(32);
	declare in_figid varchar(200);
	declare in_filename varchar(200);
	declare out_filepath varchar(200);
	declare out_fullpath varchar(200);
	declare done boolean default false;
	declare doc_data cursor for 
	SELECT attr.id,rel.proj_id,replace(fig.doc_relation_id,',','|'),attr.filename,attr.filepath,attr.fullpath
FROM lb_docattribute attr
LEFT JOIN lb_doclibrary lib ON attr.library_id=lib.id
LEFT JOIN LB_DOCCONFIG fig ON fig.id=lib.config_id
LEFT JOIN lb_docrelative rel ON rel.id= relative_id
WHERE SUBSTRING(attr.filepath,2,10)='2018/12/22' 
 AND LEFT(attr.fullpath,4)='Disk'
AND lib.doc_name IN (
'主借身份证明',
'融资租赁申请表',
'担保人身份证',
'还款卡（正反面）',
'共借身份证明'
);
	declare continue handler for not found set done=true;
	open doc_data;
	myLoop:loop
		fetch doc_data into 
		attr_id,
		in_projid,
				      in_figid,
				      in_filename,
				      out_filepath,
				      out_fullpath;
		if done then leave myLoop;
		end if;
set out_filepath=
(SELECT max(attr.filepath) FROM lb_docattribute attr
LEFT JOIN lb_doclibrary lib ON attr.library_id=lib.id
LEFT JOIN LB_DOCCONFIG fig ON fig.id=lib.config_id
LEFT JOIN lb_docrelative rel ON rel.id= relative_id
WHERE 
rel.proj_id=in_projid
AND fig.id 
REGEXP in_figid
AND attr.filename=in_filename);		
SET out_fullpath=
(SELECT MAX(attr.fullpath) FROM lb_docattribute attr
LEFT JOIN lb_doclibrary lib ON attr.library_id=lib.id
LEFT JOIN LB_DOCCONFIG fig ON fig.id=lib.config_id
LEFT JOIN lb_docrelative rel ON rel.id= relative_id
WHERE 
rel.proj_id=in_projid
AND fig.id 
REGEXP in_figid
AND attr.filename=in_filename);
update lb_docattribute set filepath=out_filepath, fullpath=out_fullpath where id=attr_id;
set	incount=incount+1;	
	end loop myLoop;
	close doc_data;
	select incount;
end;

