C223生产版本,调用HTTP,实时传输生产版本
C223增强点:CM_FV_PROD_VERS_DB_UPDATE,3个地方要改,新建、修改、删除。所以把调用http接口的代码封装成了接口,方便调用。
同时为了不增加前台等待时间,这里采用后台作业的方式。直接调用Z_RFC_PLM_037。
登陆并获取token值的,为了方便后续调用其它接口,也单独包在了接口Z_RFC_PLM_036中。
涉及到的表结构
Z_RFC_PLM_037:
Z_RFC_PLM_037 源码:
FUNCTION z_rfc_plm_037. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(P_VERID) TYPE VERID OPTIONAL *" VALUE(P_PLNNR) TYPE PLNNR OPTIONAL *" VALUE(P_WERKS) TYPE WERKS_D OPTIONAL *" VALUE(P_MATNR) TYPE MATNR OPTIONAL *" VALUE(LV_ZJOBCOUNTR) TYPE BTCJOBCNT OPTIONAL *" EXPORTING *" VALUE(LV_ZJOBCOUNT) TYPE BTCJOBCNT *"---------------------------------------------------------------------- DATA: lv_jobname TYPE tbtcjob-jobname, ls_print_parameters TYPE pri_params, lv_jobcount TYPE tbtcjob-jobcount. DATA:lt_rspar TYPE TABLE OF rsparams, lw_line LIKE LINE OF lt_rspar. DATA: ls_ztjob_count TYPE ztjob_count, lt_ztjob_count TYPE TABLE OF ztjob_count. "如果是要传给PARAMETER 根据需求设置屏幕参数 lw_line-selname = 'P_VERID'. lw_line-kind = 'P'. lw_line-sign = 'I'. lw_line-option = 'EQ'. lw_line-low = p_verid. APPEND lw_line TO lt_rspar. lw_line-selname = 'P_PLNNR'. lw_line-kind = 'P'. lw_line-sign = 'I'. lw_line-option = 'EQ'. lw_line-low = p_plnnr. APPEND lw_line TO lt_rspar. lw_line-selname = 'P_WERKS'. lw_line-kind = 'P'. lw_line-sign = 'I'. lw_line-option = 'EQ'. lw_line-low = p_werks. APPEND lw_line TO lt_rspar. lw_line-selname = 'P_MATNR'. lw_line-kind = 'P'. lw_line-sign = 'I'. lw_line-option = 'EQ'. lw_line-low = p_matnr. APPEND lw_line TO lt_rspar. *&--------新建作业名 IF lv_zjobcountr IS INITIAL. SELECT MAX( jobcount ) INTO @lv_zjobcount FROM ztjob_count WHERE programm = 'ZPPR065' AND zerdat EQ @sy-datum. ELSE. lv_zjobcount = lv_zjobcountr. ENDIF. lv_zjobcount = lv_zjobcount + 1. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = lv_zjobcount IMPORTING output = lv_zjobcount. CONCATENATE 'ZPPR065' lv_zjobcount INTO lv_jobname SEPARATED BY '_'. "打开JOB 通过JOB name 获得JOB号 CALL FUNCTION 'JOB_OPEN' EXPORTING jobname = lv_jobname IMPORTING jobcount = lv_zjobcount EXCEPTIONS cant_create_job = 1 invalid_job_data = 2 jobname_missing = 3 OTHERS = 4. IF syst-subrc = 0. "调用需要跑JOB的程序 SUBMIT zppr065 WITH SELECTION-TABLE lt_rspar USER syst-uname " mandatory VIA JOB lv_jobname NUMBER lv_zjobcount AND RETURN. *&--------设置作业周期信息 CALL FUNCTION 'JOB_CLOSE' EXPORTING jobcount = lv_jobcount jobname = lv_jobname sdlstrtdt = sy-datum sdlstrttm = sy-uzeit " * PRDMINS = 10 "每10分钟执行一次 strtimmed = 'X' "立刻开始 EXCEPTIONS cant_start_immediate = 1 invalid_startdate = 2 jobname_missing = 3 job_close_failed = 4 job_nosteps = 5 job_notex = 6 lock_failed = 7 invalid_target = 8 OTHERS = 9. IF sy-subrc <> 0. ENDIF. *&--------记录到表 ls_ztjob_count-programm = 'ZPPR065'. ls_ztjob_count-jobcount = lv_zjobcount. ls_ztjob_count-zerdat = sy-datum. MODIFY ztjob_count FROM ls_ztjob_count. * IF sy-subrc EQ 0. * COMMIT WORK AND WAIT. * ELSE. * ROLLBACK WORK. * ENDIF. ENDIF. ENDFUNCTION.
接口Z_RFC_PLM_037中调用了程序ZPPR065,
ZPPR065源码:
*&---------------------------------------------------------------------* *& Report ZPPR065 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT zppr065. TABLES: mkal. DATA: lv_token TYPE string. DATA: BEGIN OF ls_vbom2, other9 TYPE string, other10 TYPE string, END OF ls_vbom2. DATA: lc_http_client TYPE REF TO if_http_client, l_url TYPE string, l_json_data TYPE string, l_result_string TYPE string. DATA: lc_json TYPE REF TO cl_fdt_json_serializer. DATA: lv_len TYPE i. SELECTION-SCREEN: BEGIN OF BLOCK b01 WITH FRAME TITLE TEXT-001. PARAMETERS: p_verid TYPE mkal-verid, p_plnnr TYPE mkal-plnnr, p_werks TYPE mkal-werks, p_matnr TYPE mkal-matnr. SELECTION-SCREEN: END OF BLOCK b01. START-OF-SELECTION. CHECK p_verid IS NOT INITIAL. CHECK p_plnnr IS NOT INITIAL. CHECK p_werks IS NOT INITIAL. CHECK p_matnr IS NOT INITIAL. "获取token CALL FUNCTION 'Z_RFC_PLM_036' IMPORTING lv_token = lv_token. CHECK lv_token IS NOT INITIAL. "806是测试的,805是正式的 IF sy-mandt EQ '886'. l_url = 'http://111.111.11.111:805/API/pimDesignBOMItem/BOMServer/doUpdatePartMaster?PartCode='."更新生产版本号 ELSE. l_url = 'http://111.111.11.111:806/API/pimDesignBOMItem/BOMServer/doUpdatePartMaster?PartCode='."更新生产版本号 ENDIF. l_url = |{ l_url }{ p_matnr }_{ p_werks }|. ls_vbom2-other9 = p_plnnr. ls_vbom2-other10 = p_verid. *****转换json格式 CREATE OBJECT lc_json. CALL METHOD lc_json->serialize EXPORTING data = ls_vbom2 "lt_vldata RECEIVING r_json = l_json_data. *****创建http服务 CALL METHOD cl_http_client=>create_by_url EXPORTING url = l_url IMPORTING client = lc_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. EXIT. ENDIF. CALL METHOD lc_http_client->request->set_header_field EXPORTING name = 'Authorization' value = lv_token. * CALL METHOD lc_http_client->request->set_header_field EXPORTING name = 'Content-Type' value = 'application/JSON; charset=utf-8'. "设置待传输内容 lv_len = strlen( l_json_data ). CALL METHOD lc_http_client->request->set_cdata EXPORTING data = l_json_data offset = 0 length = lv_len. CALL METHOD lc_http_client->request->set_method( 'PUT' ). *****发送http服务 CALL METHOD lc_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4 OTHERS = 5. IF sy-subrc <> 0. EXIT. ENDIF. *****接收返回数据 CALL METHOD lc_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. l_result_string = lc_http_client->response->get_cdata( ).
ZPPR065调用了接口Z_RFC_PLM_036(登陆,并获取token值,时效2小时,这里为了保险,每天超过1.5小时都重新获取token值),
Z_RFC_PLM_036:
Z_RFC_PLM_036源代码:
FUNCTION z_rfc_plm_036. *"---------------------------------------------------------------------- *"*"本地接口: *" EXPORTING *" VALUE(LV_TOKEN) TYPE STRING *"---------------------------------------------------------------------- DATA: lt_ztrfc_token TYPE TABLE OF ztrfc_token, ls_ztrfc_token TYPE ztrfc_token. DATA: lv_uzeit TYPE sy-uzeit. DATA: lv_check TYPE c. DATA: lc_json TYPE REF TO cl_fdt_json_serializer, lc_json2 TYPE REF TO cl_fdt_json_serializer. "json格式转换 DATA: lc_http_client2 TYPE REF TO if_http_client, l_url2 TYPE string, l_json_data2 TYPE string, l_result_string2 TYPE string. IF sy-uzeit LE '013000'. lv_uzeit = sy-uzeit. ENDIF. * CHECK sy-mandt = '886'. "正式机 SELECT SINGLE a~* INTO @ls_ztrfc_token FROM ztrfc_token AS a WHERE EXISTS ( SELECT MAX( b~zuzeit ) FROM ztrfc_token AS b WHERE b~zerdat EQ @sy-datum ). IF ls_ztrfc_token-zdata IS INITIAL OR lv_uzeit > ls_ztrfc_token-zuzeit. "今天未获取,或者失效超出,需重新登陆PLM,获取TOKEN IF sy-mandt EQ '886'. l_url2 = 'http://111.111.11.111:805/login?UserName=111&PassWord=111'."获取token值 ,806是测试的 ELSE. l_url2 = 'http://111.111.11.111:806/login?UserName=111&PassWord=111'."获取token值 ,806是测试的 ENDIF. CALL METHOD cl_http_client=>create_by_url EXPORTING url = l_url2 IMPORTING client = lc_http_client2 EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. EXIT. ENDIF. CALL METHOD lc_http_client2->request->set_method( 'GET' ). *****发送http服务 CALL METHOD lc_http_client2->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4 OTHERS = 5. IF sy-subrc <> 0. EXIT. ENDIF. *****接收返回数据 CALL METHOD lc_http_client2->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. l_result_string2 = lc_http_client2->response->get_cdata( ). DATA: lt_str LIKE TABLE OF l_result_string2 WITH HEADER LINE. * DATA: lv_token TYPE string. SPLIT l_result_string2 AT '"' INTO TABLE lt_str. READ TABLE lt_str INTO DATA(ls_str) INDEX 4. IF sy-subrc EQ 0. lv_token = ls_str. ls_ztrfc_token-zerdat = sy-datum. ls_ztrfc_token-zuzeit = sy-uzeit. ls_ztrfc_token-zdata = ls_str. MODIFY ztrfc_token FROM ls_ztrfc_token. IF sy-subrc EQ 0. COMMIT WORK AND WAIT. ENDIF. ENDIF. ELSE. lv_token = ls_ztrfc_token-zdata. ENDIF. ENDFUNCTION.