$PBExportHeader$f_kjls_getfj.srf
global type f_kjls_getfj from function_object
end type

forward prototypes
global function string f_kjls_getfj (long arg_pkid)
end prototypes

global function string f_kjls_getfj (long arg_pkid);Long arr_fileid[], ll_fileid, i = 0, row
String arr_filename[], arr_filetype[], ls_filename, ls_filetype, Pathname
Blob ls_filedata
String errmsg

String ls_guid
SELECT top 1 newid()
	INTO :ls_guid
	From u_user;
IF sqlca.SQLCode <> 0 THEN
	RETURN ''
END IF

DECLARE cur1 CURSOR FOR
	SELECT fileid, DisplayName,FileType
	FROM u_file
	WHERE billtype = 1466
	AND relid = :arg_pkid
	AND (filetype = 'bmp' OR filetype = 'rle' OR filetype = 'wmf'
	OR filetype = 'tif' OR filetype = 'jpg' OR filetype = 'gif' OR filetype = 'jpeg')
	Using sys_fileDB_sqlca;
	
OPEN cur1;
FETCH cur1 Into :ll_fileid, :ls_filename, :ls_filetype;
DO WHILE sys_fileDB_sqlca.SQLCode = 0
	i++
	arr_fileid[i] = ll_fileid
	arr_filename[i] = ls_filename
	arr_filetype[i] = ls_filetype
	FETCH cur1 Into :ll_fileid, :ls_filename, :ls_filetype;
LOOP
CLOSE cur1;
Long printid = 0
FOR i = 1 To UpperBound(arr_fileid)
	Pathname = sys_cur_path+ins_fjtemppath+'\' + String(arr_fileid[i])+ '_' + arr_filename[i]+'.'+arr_filetype[i]
	IF Trim(Pathname) <> '' THEN
		IF Not FileExists ( Pathname ) THEN
			SetNull(ls_filedata)
			SelectBlob filedata
			Into:ls_filedata
			From u_file
			Where fileid = :arr_fileid[i] Using sys_fileDB_sqlca;
			IF sys_fileDB_sqlca.SQLCode = 0 THEN
				IF f_saveblobtofile(ls_filedata,Pathname,errmsg) <> 0 THEN
					// DONE: 插入记录
					printid++
					INSERT Into u_print_files(guid, printid, Pathname) Values(:ls_guid, :printid, :Pathname);
					IF sqlca.SQLCode <> 0 THEN
						ROLLBACK;
						RETURN ''
					END IF
				END IF
			END IF
		ELSE
			printid++
			INSERT Into u_print_files(guid, printid, Pathname) Values(:ls_guid, :printid, :Pathname);
			IF sqlca.SQLCode <> 0 THEN
				ROLLBACK;
				RETURN ''
			END IF
		END IF
	END IF
NEXT

COMMIT;
RETURN ls_guid



end function

