class ZCL_COPY_TABLE definition
public
final
create public .
public section.
methods CONSTRUCTOR
importing
!IS_COPY type ZSTABLE_COPY .
methods DO_COPY
returning
value(RV_RETURN) type BAPIRET2 .
protected section.
private section.
data GS_COPY type ZSTABLE_COPY .
data GC_LOCKTIME type INT4 value 100 ##NO_TEXT.
methods WRITE_LOG
importing
!IV_COUNT type INT4 .
methods CHECK_INTERVAL
returning
value(RV_DO) type CHAR1 .
methods LOCK_COPY
importing
!IV_TABNAME type TABNAME
returning
value(RV_SUBRC) type I .
methods UNLOCK_COPY
importing
!IV_TABNAME type TABNAME .
methods DEFINE_DYNAMIC_ITAB
importing
!TABNAME type TABNAME
returning
value(CRT_DATA) type ref to DATA .
ENDCLASS.
CLASS ZCL_COPY_TABLE IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_COPY_TABLE->CHECK_INTERVAL
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RV_DO TYPE CHAR1
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD check_interval.
DATA: lv_last TYPE bp_timestamp.
DATA: lv_now TYPE bp_timestamp.
rv_do = ''.
CASE gs_copy-interval.
WHEN 'D' ." 按天检查(检查是否又当天的复制)
SELECT SINGLE * FROM ztcopy_table_log INTO @DATA(ls_log)
WHERE tabname_ori = @gs_copy-tabname_ori
AND crdat = @sy-datum.
IF sy-subrc <> 0.
rv_do = 'X'.
ENDIF.
WHEN 'I' . "按时间间隔检查,检查上次复制距离当期的秒数
"读取表复制的最大时间戳
SELECT MAX( timestamp ) INTO lv_last FROM ztcopy_table_log
WHERE tabname_ori = gs_copy-tabname_ori .
GET TIME.
lv_now = sy-datum && sy-uzeit.
DATA: lv_sec TYPE i.
CALL FUNCTION 'Z_BC_TIMESTAMP_GET_SECOND_S'
EXPORTING
in_tmbeg = lv_last
in_tmend = lv_now
IMPORTING
o_ltsec = lv_sec.
IF lv_sec > gs_copy-seconds.
rv_do = 'X'.
ENDIF.
WHEN OTHERS. " 不检查
rv_do = 'X'.
ENDCASE.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_COPY_TABLE->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_COPY TYPE ZSTABLE_COPY
* +--------------------------------------------------------------------------------------</SIGNATURE>
method CONSTRUCTOR.
gs_copy = is_copy.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_COPY_TABLE->DEFINE_DYNAMIC_ITAB
* +-------------------------------------------------------------------------------------------------+
* | [--->] TABNAME TYPE TABNAME
* | [<-()] CRT_DATA TYPE REF TO DATA
* +--------------------------------------------------------------------------------------</SIGNATURE>
method DEFINE_DYNAMIC_ITAB.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_COPY_TABLE->DO_COPY
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RV_RETURN TYPE BAPIRET2
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD do_copy.
DATA: lc_error TYPE REF TO cx_root.
DATA: lv_err TYPE string.
DATA: lrt_ori TYPE REF TO data.
DATA: lrt_tar TYPE REF TO data,
lrs_tar TYPE REF TO data.
DATA: lv_subrc TYPE i.
CLEAR lv_subrc.
FIELD-SYMBOLS:
<lt_ori> TYPE STANDARD TABLE,
<lt_tar> TYPE STANDARD TABLE,
<ls_tar> TYPE any.
DATA: lv_count TYPE i.
"先尝试锁定表的复制处理,
lv_subrc = lock_copy( gs_copy-tabname_ori ).
CHECK lv_subrc = 0. "如果锁定失败, 不执行后续动作.
IF check_interval( ) = '' . "检查是否需要再次复制,如果无需复制, 解锁后,退出处理
unlock_copy( gs_copy-tabname_ori ).
RETURN.
ENDIF.
"定义源,目标
TRY.
lrt_ori = zcl_define_hashed_table=>create_standard_table_new2( iv_strucname = gs_copy-tabname_ori iv_method = '1' ).
ASSIGN lrt_ori->* TO <lt_ori>.
lrt_tar = zcl_define_hashed_table=>create_standard_table_new2( iv_strucname = gs_copy-tabname_tar iv_method = '1' ).
ASSIGN lrt_tar->* TO <lt_tar>.
CREATE DATA lrs_tar LIKE LINE OF <lt_tar>.
ASSIGN lrs_tar->* TO <ls_tar>.
CATCH cx_root INTO lc_error.
lv_err = lc_error->get_text( ).
lv_subrc = 4.
ENDTRY.
IF lv_subrc = 0.
"获取初始序号
IF gs_copy-fname_key IS NOT INITIAL.
ASSIGN COMPONENT gs_copy-fname_key OF STRUCTURE <ls_tar> TO FIELD-SYMBOL(<lv_key>).
IF sy-subrc = 0.
<lv_key> = 1.
ENDIF.
ENDIF.
"清空表中数据
DELETE FROM (gs_copy-tabname_tar).
"获取分包信息
SELECT COUNT( * ) FROM (gs_copy-tabname_ori) INTO lv_count.
DATA(lt_pack) = zcl_process_group=>get_group_for_select( iv_count = lv_count iv_package_num = gs_copy-package_num ).
"分包处理
LOOP AT lt_pack INTO DATA(ls_pack).
"读取数据
TRY.
"视图读取
DATA(lv_from) = zcl_rep_comm_sales=>get_cds_view( iv_viewname = gs_copy-tabname_ori ).
SELECT * FROM (lv_from)
ORDER BY PRIMARY KEY
INTO CORRESPONDING FIELDS OF TABLE @<lt_ori> "如果不用corresponding fields of 可能会导致读书错误
OFFSET @ls_pack-zoff
UP TO @ls_pack-zrows ROWS
.
CATCH cx_root INTO lc_error.
lv_err = lc_error->get_text( ).
lv_subrc = 4.
ENDTRY.
"复制到目标内表
IF lv_subrc = 0.
TRY.
IF gs_copy-same_fields = ''. "字段不相同
MOVE-CORRESPONDING <lt_ori> TO <lt_tar>.
ENDIF.
CATCH cx_root INTO lc_error.
lv_err = lc_error->get_text( ).
lv_subrc = 4.
ENDTRY.
ELSE.
rv_return-type = 'E'.
MESSAGE e001(00) WITH '读取表错误:'(001) lv_err INTO rv_return-message.
EXIT.
ENDIF.
IF lv_subrc = 0.
TRY.
IF gs_copy-same_fields = ''. "字段不相同
"添加自增长号码
IF gs_copy-fname_key IS NOT INITIAL.
* ASSIGN COMPONENT gs_copy-fname_key OF STRUCTURE <ls_tar> TO FIELD-SYMBOL(<lv_key>).
* IF sy-subrc = 0.
* <lv_key> = 1.
* ENDIF.
* DATA: lv_quan TYPE inri-quantity.
* lv_quan = ls_pack-zrows . "lines( <lt_tar> ). "使用分包中的条目数
* CALL FUNCTION 'NUMBER_GET_NEXT'
* EXPORTING
* nr_range_nr = gs_copy-nrnr
* object = gs_copy-nrobj
* quantity = lv_quan
* IMPORTING
* number = <lv_key>
* QUANTITY = lv_quan
** RETURNCODE =
* .
DATA: lv_tabix TYPE i.
LOOP AT <lt_tar> ASSIGNING FIELD-SYMBOL(<lw_tar>).
ASSIGN COMPONENT gs_copy-fname_key OF STRUCTURE <lw_tar> TO FIELD-SYMBOL(<lv_key_mod>).
IF sy-subrc = 0.
<lv_key_mod> = <lv_key> .
<lv_key> = <lv_key> + 1.
ENDIF.
ENDLOOP.
ENDIF.
MODIFY (gs_copy-tabname_tar) FROM TABLE <lt_tar>.
ELSE.
MODIFY (gs_copy-tabname_tar) FROM TABLE <lt_ori>.
ENDIF.
CATCH cx_root INTO lc_error.
lv_err = lc_error->get_text( ).
lv_subrc = 4.
ENDTRY.
IF sy-subrc <> 0.
rv_return-type = 'E'.
MESSAGE e001(00) WITH '写入表错误:'(002) lv_err INTO rv_return-message.
EXIT.
ENDIF.
ELSE.
IF gs_copy-same_fields = ''. "字段不相同
rv_return-type = 'E'.
MESSAGE e001(00) WITH '赋值源内表到目标内表错误:'(003) lv_err INTO rv_return-message.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
ELSE.
rv_return-type = 'E'.
MESSAGE e001(00) WITH '定义内表:'(004) lv_err INTO rv_return-message.
ENDIF.
IF rv_return-type = 'E'.
ROLLBACK WORK.
ELSE.
rv_return-type = 'S'.
write_log( lv_count ). "记录日志
COMMIT WORK AND WAIT.
ENDIF.
unlock_copy( gs_copy-tabname_ori ).
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_COPY_TABLE->LOCK_COPY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_TABNAME TYPE TABNAME
* | [<-()] RV_SUBRC TYPE I
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD lock_copy.
rv_subrc = 4.
DO GC_LOCKTIME TIMES.
CALL FUNCTION 'ENQUEUE_EZSTABLE_COPY'
EXPORTING
* MODE_ZSTABLE_COPY = 'E'
tabname_ori = iv_tabname
* X_TABNAME_ORI = ' '
_scope = '2'
* _WAIT = ' '
* _COLLECT = ' '
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
IF sy-subrc = 0.
rv_subrc = 0.
EXIT.
else.
wait up to 1 SECONDS.
ENDIF.
ENDDO.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_COPY_TABLE->UNLOCK_COPY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_TABNAME TYPE TABNAME
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD unlock_copy.
CALL FUNCTION 'DEQUEUE_EZSTABLE_COPY'
EXPORTING
* MODE_ZSTABLE_COPY = 'E'
tabname_ori = iv_tabname
* X_TABNAME_ORI = ' '
* _SCOPE = '3'
* _SYNCHRON = ' '
* _COLLECT = ' '
.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZCL_COPY_TABLE->WRITE_LOG
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_COUNT TYPE INT4
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD write_log.
DATA: ls_save TYPE ztcopy_table_log.
MOVE-CORRESPONDING gs_copy TO ls_save.
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = '01'
object = 'ZLOGID'
IMPORTING
number = ls_save-logid.
ls_save-RECORDS = iv_count.
CALL FUNCTION 'Z_BC_GET_ZSBC001'
EXPORTING
iv_updkz = 'I'
CHANGING
cs_data = ls_save.
.
MODIFY ztcopy_table_log FROM LS_SAVE.
ENDMETHOD.
ENDCLASS.
源码-复制视图
最新推荐文章于 2024-08-24 17:09:16 发布