场景:异常JOB主动推送企业微信群 (企业微信机器人可百度创建,注意群不能有企业外的人员,否则接口无效)
- 事务代码:ZSM37_CHECK
- 程序名称:ZSM37_CHECK
- 程序目的: ZCSM37-后台JOB异常检查
- 自建表:
- 开发人员:
*(修改日志)-------------------------------------------------------- - 日志号 修改人 修改时间 修改说明 传输号码
-
- 001
REPORT zsm37_check.
======================================================================
- INCLUDE
======================================================================
INCLUDE zsm37_check_top.
INCLUDE zsm37_check_f01.
======================================================================
- AT SELECTION-SCREEN.
======================================================================
AT SELECTION-SCREEN OUTPUT.
======================================================================
- INITIALIZATION
======================================================================
INITIALIZATION.
======================================================================
- START-OF-SELECTION.
======================================================================
START-OF-SELECTION.
IF p_mins IS INITIAL.
p_mins = 60.
ENDIF.
PERFORM pfm_get_data.
PERFORM pfr_display_alv.
======================================================================
- END-OF-SELECTION.
======================================================================
END-OF-SELECTION.
&---------------------------------------------------------------------
*& 包含 ZSM37_CHECK_TOP
&---------------------------------------------------------------------
TABLES:tbtco,tbtcp.
DATA: BEGIN OF gt_alv OCCURS 0,
light(4) TYPE c, " 标识
jobname TYPE tbtco-jobname,
progname TYPE progname,
variant TYPE variant,
sdluname TYPE tbtco-sdluname,
sdldate TYPE tbtco-sdldate,
sdltime TYPE tbtco-sdltime,
date TYPE tbtco-sdldate,
time TYPE tbtco-sdltime,
prdmins TYPE tbtco-prdmins,
prdhours TYPE tbtco-prdhours,
prddays TYPE tbtco-prddays,
prdweeks TYPE tbtco-prdweeks,
prdmonths TYPE tbtco-prdmonths,
pl TYPE char100,
flag1 TYPE c,
flag2 TYPE c,
-
min TYPE i, min_c TYPE char100, "当前检查时间 datum_check TYPE sy-datum, uzeit_check TYPE sy-uzeit, END OF gt_alv.
DATA:gv_zuserid TYPE ztxnth-zuserid. "二级账号
DATA:it_raw TYPE truxs_t_text_data.
DATA: gs_layout TYPE lvc_s_layo,
gs_fieldcat TYPE lvc_s_fcat,
gt_fieldcat TYPE lvc_t_fcat,
stylelin TYPE lvc_s_styl,
alv_grid TYPE REF TO cl_gui_alv_grid.
CONSTANTS: cos_green_light LIKE dv70a-statusicon VALUE ‘@08@’, "绿灯
cos_yellow_light LIKE dv70a-statusicon VALUE ‘@09@’, "黄灯
cos_red_light LIKE dv70a-statusicon VALUE ‘@0A@’. "红灯
DATA: gt_events TYPE slis_t_event, "事件
gs_events LIKE LINE OF gt_events.
DATA:lv_s TYPE i,
lv_e TYPE i.
RANGES:r_date FOR sy-datum,
r_time FOR sy-uzeit.
DEFINE add_field.
gs_fieldcat-FIELDNAME = &1.
gs_fieldcat-scrtext_l = &2.
gs_fieldcat-EDIT = &3.
gs_fieldcat-HOTSPOT = &4.
gs_fieldcat-CHECKBOX = &5.
gs_fieldcat-REF_FIELD = &6.
gs_fieldcat-REF_TABLE = &7.
gs_fieldcat-ICON = &8.
APPEND gs_fieldcat TO gt_fieldcat.
CLEAR gs_fieldcat.
END-OF-DEFINITION.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002 .
SELECT-OPTIONS:s_job FOR tbtco-jobname ,
s_uname FOR tbtco-sdluname,
" s_date FOR tbtco-sdlstrtdt ,
-
s_ser FOR tbtco-reaxserver s_pname FOR tbtcp-progname .
PARAMETERS:p_mins TYPE n LENGTH 5 DEFAULT ‘60’ NO-DISPLAY.
PARAMETERS:p_sed AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b2.
&---------------------------------------------------------------------
*& 包含 ZSM37_CHECK_F01
&---------------------------------------------------------------------
&---------------------------------------------------------------------
*& Form PFM_GET_DATA
&---------------------------------------------------------------------
-
text
----------------------------------------------------------------------
- –> p1 text
- <-- p2 text
----------------------------------------------------------------------
FORM pfm_get_data .
"R 活动的(运行中)
"Y 就绪的
"P 已调度的
"S 已释放
"A 已取消(出错终止)
"F 已完成(结束)
"Z Released/Susp.(无中文)
DATA:lt_ztsm37 TYPE TABLE OF ztsm37,
ls_ztsm37 TYPE ztsm37.
DATA:lv_datum_check TYPE sy-datum.
DATA:lv_uzeit_check TYPE sy-uzeit.
"当前检查时间
lv_datum_check = sy-datum.
lv_uzeit_check = sy-uzeit.
"获取已释放状态的JOB和日期
SELECT
jobname,
sdldate,
sdltime,
sdluname,
prdmins,
prdhours,
prddays,
prdweeks,
prdmonths,
status
INTO TABLE @DATA(lt_job)
FROM tbtco
WHERE status = ‘S’ "S 已释放
AND periodic = ‘X’"周期性
AND authckman = @sy-mandt
AND jobname IN @s_job
AND sdluname IN @s_uname.
SORT lt_job BY jobname.
DELETE ADJACENT DUPLICATES FROM lt_job COMPARING jobname.
"更新自建表数据
CLEAR:lt_ztsm37,ls_ztsm37.
LOOP AT lt_job INTO DATA(ls_job).
"获取最后一次执行时间
SELECT SINGLE
MAX( strttime ) AS strttime,
strtdate,
jobname
FROM tbtco
INTO @DATA(ls_job_max)
WHERE status IN ('R','F')"R 活动的(运行中) "F 已完成(结束)
AND periodic = 'X'"周期性
AND jobname = @ls_job-jobname
AND authckman = @sy-mandt
AND strtdate = ( SELECT MAX( strtdate ) AS strtdate
FROM tbtco
WHERE status IN ('R','F')
AND periodic = 'X'"周期性
AND jobname = @ls_job-jobname
AND authckman = @sy-mandt
GROUP BY jobname
)
GROUP BY strtdate ,jobname.
IF sy-subrc = 0.
ls_job-sdldate = ls_job_max-strtdate.
ls_job-sdltime = ls_job_max-strttime.
ELSE.
CLEAR:ls_job-sdldate,
ls_job-sdltime.
ENDIF.
MOVE-CORRESPONDING ls_job TO ls_ztsm37.
ls_ztsm37-zcjrq = sy-datum.
APPEND ls_ztsm37 TO lt_ztsm37.
MODIFY lt_job FROM ls_job.
CLEAR:ls_ztsm37,ls_job.
ENDLOOP.
IF lt_ztsm37[] IS NOT INITIAL.
MODIFY ztsm37 FROM TABLE lt_ztsm37.
IF sy-subrc = 0.
COMMIT WORK.
ELSE.
ROLLBACK WORK.
ENDIF.
ENDIF.
"获取前一天的已释放JOB
DATA:lv_datum TYPE sy-datum.
lv_datum = sy-datum - 1.
SELECT
*
FROM ztsm37
INTO CORRESPONDING FIELDS OF TABLE @lt_ztsm37
WHERE jobname IN @s_job
AND sdluname IN @s_uname
AND zcjrq = @lv_datum.
"检查已释放状态是否存在
LOOP AT lt_ztsm37 INTO ls_ztsm37.
CLEAR gt_alv.
MOVE-CORRESPONDING ls_ztsm37 TO gt_alv.
READ TABLE lt_job INTO ls_job WITH KEY jobname = ls_ztsm37-jobname BINARY SEARCH.
IF sy-subrc <> 0.
gt_alv-light = cos_red_light.
gt_alv-flag1 = 'X'.
ELSE.
gt_alv-light = cos_green_light.
ENDIF.
APPEND gt_alv.
ENDLOOP.
"已释放JOB执行频率检查
LOOP AT lt_job INTO ls_job.
READ TABLE gt_alv WITH KEY jobname = ls_job-jobname .
IF sy-subrc <> 0.
MOVE-CORRESPONDING ls_job TO gt_alv.
APPEND gt_alv.
ENDIF.
ENDLOOP.
IF gt_alv[] IS NOT INITIAL.
"获取JOB名称对应的程序和变式
SELECT DISTINCT
jobname,
progname,
variant
FROM tbtcp
INTO TABLE @DATA(lt_tbtcp)
FOR ALL ENTRIES IN @gt_alv
WHERE jobname = @gt_alv-jobname
AND progname IN @s_pname.
SORT lt_tbtcp BY jobname.
ENDIF.
LOOP AT gt_alv.
- "获取最后一次执行时间
DATA:lv_date LIKE p0001-begda,
lv_months LIKE t5a4a-dlymo.
"JOB名称对应的程序名称&&变式
READ TABLE lt_tbtcp INTO DATA(ls_tbtcp) WITH KEY jobname = gt_alv-jobname BINARY SEARCH.
IF sy-subrc = 0.
gt_alv-progname = ls_tbtcp-progname.
gt_alv-variant = ls_tbtcp-variant.
ELSE.
DELETE gt_alv WHERE jobname = gt_alv-jobname.
CONTINUE.
ENDIF.
IF gt_alv-sdldate IS INITIAL.
gt_alv-light = cos_red_light.
gt_alv-flag1 = 'X'.
ELSE.
lv_date = gt_alv-sdldate.
lv_months = gt_alv-prdmonths.
"计算下次执行日期
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = lv_date
days = '00'
months = lv_months
signum = '+'
years = '00'
IMPORTING
calc_date = gt_alv-date.
gt_alv-date = gt_alv-date + gt_alv-prddays + gt_alv-prdweeks * 7.
"计算下次执行时间
DATA:lv_prdmins TYPE i . " 一项批作业的持续时期(以分钟计算)
lv_prdmins = gt_alv-prdmins + ( gt_alv-prdhours * 60 * 60 ).
IF lv_prdmins > 86400. "一天等于86400秒
gt_alv-date = gt_alv-date + 1.
ENDIF.
gt_alv-time = gt_alv-sdltime + lv_prdmins.
IF gt_alv-date < lv_datum_check.
gt_alv-light = cos_red_light.
gt_alv-flag2 = 'X'.
ELSEIF gt_alv-date = lv_datum_check.
IF gt_alv-time < lv_uzeit_check.
gt_alv-light = cos_red_light.
gt_alv-flag2 = 'X'.
ENDIF.
ENDIF.
ENDIF.
gt_alv-datum_check = lv_datum_check.
gt_alv-uzeit_check = lv_uzeit_check.
"频率显示
-
prdmonths type tbtco-prdmonths,
IF gt_alv-prdmonths IS NOT INITIAL.
IF gt_alv-prdmonths = ‘1’.
gt_alv-pl = ‘每月’.
ELSE.
gt_alv-pl = gt_alv-pl && gt_alv-prdmonths && ‘月’.
ENDIF.
ENDIF. -
prdweeks type tbtco-prdweeks,
IF gt_alv-prdweeks IS NOT INITIAL.
IF gt_alv-prdweeks = ‘1’.
gt_alv-pl = ‘每周’.
ELSE.
gt_alv-pl = gt_alv-pl && gt_alv-prdweeks && ‘周’.
ENDIF.
ENDIF. -
prddays type tbtco-prddays,
IF gt_alv-prddays IS NOT INITIAL .
IF gt_alv-prddays = ‘1’.
gt_alv-pl = ‘每天’.
ELSE.
gt_alv-pl = gt_alv-pl && gt_alv-prddays && ‘天’.
ENDIF.
ENDIF. -
prdhours type tbtco-prdhours,
IF gt_alv-prdhours IS NOT INITIAL.
IF gt_alv-prdhours = ‘1’.
gt_alv-pl = ‘每小时’.
ELSE.
gt_alv-pl = gt_alv-pl && gt_alv-prdhours && ‘小时’.
ENDIF.
ENDIF. -
prdmins type tbtco-prdmins,
IF gt_alv-prdmins IS NOT INITIAL.
IF gt_alv-prdmins = ‘1’.
gt_alv-pl = ‘每分钟’.
ELSE.
gt_alv-pl = gt_alv-pl && gt_alv-prdmins && ‘分钟’.
ENDIF.
ENDIF.
"计算超时间-分钟
"https://blog.csdn.net/FQHX_116/article/details/128150602?spm=1001.2014.3001.5501 源码
IF gt_alv-flag2 = 'X'.
CALL FUNCTION 'ZFM_DATE_DIFFERENCE'
EXPORTING
iv_date_beg = gt_alv-sdldate
iv_time_beg = gt_alv-sdltime
iv_date_end = lv_datum_check
iv_time_end = lv_uzeit_check
IMPORTING
ev_message = gt_alv-min_c.
ELSEIF gt_alv-flag1 = 'X'.
gt_alv-min_c = '未执行'.
ENDIF.
IF p_sed = 'X'.
IF gt_alv-light = cos_red_light.
DATA:lv_json TYPE string,
lv_message TYPE char100,
lv_status_c TYPE char1.
"推送企业微信格式
-
lv_json = ‘{ “msgtype”: “markdown”, “markdown”: { “content”: "### 异常JOB通知\n >JOB名称:’ &&
-
'XXXXXX \n 程序名称:XXXXXX \n 最后一次执行时间:\n 频率:<font color=“info”>5分钟 \n ’ &&
-
‘距最后一次执行时间: <font color=“warning”>50分钟 \n 检查时间:" }’.
“推送企业微信格式
lv_json = ‘{ “msgtype”: “markdown”, “markdown”: ’ &&
‘{ “content”: "### 异常JOB通知\n >JOB名称:’ && gt_alv-jobname &&
’ \n 程序名称:’ && gt_alv-progname &&
’ \n 变式名称:’ && gt_alv-variant &&
’ \n 最后一次执行时间:’
&& gt_alv-sdldate+0(4) &&‘/’&& gt_alv-sdldate+4(2) &&‘/’&& gt_alv-sdldate+6(2) && ‘,’
&& gt_alv-sdltime+0(2) &&‘:’&& gt_alv-sdltime+2(2) &&‘:’&& gt_alv-sdltime+4(2) &&
’ \n 频率:<font color=“info”>‘&& gt_alv-pl &&’ \n ’ &&
‘距最后一次执行时间: <font color=“warning”>’ && gt_alv-min_c && ' ‘&&
’ \n 检查时间:’
&& gt_alv-datum_check+0(4) &&’/‘&& gt_alv-datum_check+4(2) &&’/‘&& gt_alv-datum_check+6(2) && ‘,’
&& gt_alv-uzeit_check+0(2) &&’:‘&& gt_alv-uzeit_check+2(2) &&’:'&& gt_alv-uzeit_check+4(2) &&
'” }'.
"https://blog.csdn.net/FQHX_116/article/details/128150650?spm=1001.2014.3001.5502 源码
CALL FUNCTION ‘ZFM_SEND_CHECK_JOB’
EXPORTING
iv_json = lv_json
IMPORTING
ev_message = lv_message
ev_status_c = lv_status_c.
IF lv_status_c = 'S'.
MESSAGE '异常JOB推送企业微信成功' TYPE 'S'.
ELSE.
MESSAGE '异常JOB推送企业微信失败:'&& lv_message TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDIF.
ENDIF.
MODIFY gt_alv.
ENDLOOP.
ENDFORM.
&---------------------------------------------------------------------
*& Form PFR_DISPLAY_ALV
&---------------------------------------------------------------------
-
ALV展示
----------------------------------------------------------------------
FORM pfr_display_alv .
PERFORM frm_set_layout.
PERFORM frm_fieldcat.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC’
EXPORTING
i_callback_program = sy-repid "当前程序
is_layout_lvc = gs_layout “界面格式”
it_fieldcat_lvc = gt_fieldcat “字段属性”
-
i_callback_pf_status_set = 'FRM_SET_PF_STATUS' "状态
-
i_callback_user_command = 'FRM_USER_COMMAND' "控制指令
-
it_event_exit = gt_event_exit
-
i_grid_title = ls_title
-
TABLESi_grid_settings = i_grid_settings
t_outtab = gt_alv
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE ‘S’ NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE ‘E’.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_SET_LAYOUT_
&---------------------------------------------------------------------
-
ALV设置
----------------------------------------------------------------------
----------------------------------------------------------------------
FORM frm_set_layout .
CLEAR gs_layout.
gs_layout-zebra = ‘X’ . " 使ALV界面呈现颜色交替
" GS_LAYOUT-SEL_MODE = ‘A’ . " 选择模式,"A"在最左端有选择按钮
gs_layout-cwidth_opt = ‘X’ . " 自动优化列宽
ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_FIELDCAT
&---------------------------------------------------------------------
-
整单删除ALV字段设置
----------------------------------------------------------------------
----------------------------------------------------------------------
FORM frm_fieldcat.
add_field ‘LIGHT’ ‘状态灯’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘X’.
add_field ‘JOBNAME’ ‘作业名’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PROGNAME’ ‘程序名称’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘VARIANT’ ‘变式名称’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘SDLUNAME’ ‘作业创建者’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘SDLDATE’ ‘实际最后一次执行开始’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘SDLTIME’ ‘实际最后一次执行时间’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘DATE’ ‘下次执行开始’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘TIME’ ‘下次执行时间’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘MIN_C’ ‘距最后一次执行时间’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PL’ ‘频率’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PRDMINS’ ‘频率:分钟’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PRDHOURS’ ‘频率:小时’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PRDDAYS’ ‘频率:天’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PRDWEEKS’ ‘频率:周’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘PRDMONTHS’ ‘频率:月’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘FLAG1’ ‘已释放状态检查’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘FLAG2’ ‘执行时间检查’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘DATUM_CHECK’ ‘检查日期’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
add_field ‘UZEIT_CHECK’ ‘检查时间’ ‘’ ‘’ ‘’ ‘’ ‘’ ‘’.
ENDFORM.