import dash import time import uuid from dash import dcc from dash.dependencies import Input, Output, State, ALL from dash.exceptions import PreventUpdate import feffery_utils_components as fuc from server import app from utils.common import validate_data_not_empty from api.dscatalog import get_dscatalog_tree_api from api.ndstand import get_all_ndstand_api, get_ndstand_list_api, get_ndstand_detail_api, add_ndstand_api, edit_ndstand_api, delete_ndstand_api, export_ndstand_list_api,batch_import_ndstand_api,download_ndstand_import_template_api #判断节点层级和名称 def find_title_and_path(data, key, path=None, level=0): if path is None: path = [] if 'key' in data and data['key'] == key: # 当前层级的 title 加入路径 path.append(data['title']) return data['title'], level, path elif 'children' in data: for child in data['children']: title, child_level, updated_path = find_title_and_path(child, key, path + [data['title']], level + 1) if title is not None: return title, child_level, updated_path return None, -1, path #树形搜索 app.clientside_callback( """(value) => value""", Output('ndstand-tree', 'searchKeyword'), Input('ndstand-input-search', 'value'), ) #处理树内节点值 def update_value_with_title(node): node['value'] = node['title'] # 更新当前节点的 value if 'children' in node: for child in node['children']: update_value_with_title(child) # 递归更新子节点 @app.callback( output=dict( ndstand_table_data=Output('ndstand-list-table', 'data', allow_duplicate=True), ndstand_table_pagination=Output('ndstand-list-table', 'pagination', allow_duplicate=True), ndstand_table_key=Output('ndstand-list-table', 'key'), ndstand_table_selectedrowkeys=Output('ndstand-list-table', 'selectedRowKeys'), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True) ), inputs=dict( selected_ndstand_tree=Input('ndstand-tree', 'selectedKeys'), search_click=Input('ndstand-search', 'nClicks'), refresh_click=Input('ndstand-refresh', 'nClicks'), pagination=Input('ndstand-list-table', 'pagination'), operations=Input('ndstand-operations-store', 'data') ), state=dict( tree_data=State('ndstand-tree', 'treeData'), data_std_no=State('ndstand-data_std_no-input', 'value'), data_std_cn_name=State('ndstand-data_std_cn_name-input', 'value'), status_select=State('ndstand-status-select', 'value'), button_perms=State('ndstand-button-perms-container', 'data') ), prevent_initial_call=True ) def get_ndstand_table_data(tree_data,selected_ndstand_tree,search_click, refresh_click, pagination, operations, data_std_no, data_std_cn_name, status_select, button_perms): """ 获取数据标准数据回调(进行表格相关增删查改操作后均会触发此回调) """ std_clas1=None std_clas2=None std_clas3=None std_clas4=None std_clas5=None select_key = None if selected_ndstand_tree == None: select_key = 100 else: select_key = selected_ndstand_tree[0] #print(666667,selected_ndstand_tree) title,level, path = find_title_and_path(tree_data[0],select_key) if level == 4 : std_clas1 = path[0] std_clas2 = path[1] std_clas3 = path[2] std_clas4 = path[3] std_clas5 = path[4] if level == 3 : std_clas1 = path[0] std_clas2 = path[1] std_clas3 = path[2] std_clas4 = path[3] if level == 2 : std_clas1 = path[0] std_clas2 = path[1] std_clas3 = path[2] if level == 1 : std_clas1 = path[0] std_clas2 = path[1] if level == 0 : std_clas1 = path[0] #print(std_clas1,std_clas2,std_clas3,std_clas4,std_clas5,999999999999,title,level, path) query_params = dict( data_std_schm=std_clas2, std_pri_clas=std_clas3, std_scd_clas=std_clas4, std_thre_clas=std_clas5, data_std_no=data_std_no, data_std_cn_name=data_std_cn_name, status_select=status_select, page_num=1, page_size=10 ) triggered_id = dash.ctx.triggered_id if triggered_id == 'ndstand-list-table': query_params = dict( data_std_schm=std_clas2, std_pri_clas=std_clas3, std_scd_clas=std_clas4, std_thre_clas=std_clas5, data_std_no=data_std_no, data_std_cn_name=data_std_cn_name, status_select=status_select, page_num=pagination['current'], page_size=pagination['pageSize'] ) if search_click or refresh_click or pagination or operations: table_info = get_ndstand_list_api(query_params) if table_info['code'] == 200: table_data = table_info['data']['rows'] table_pagination = dict( pageSize=table_info['data']['page_size'], current=table_info['data']['page_num'], showSizeChanger=True, pageSizeOptions=[10, 30, 50, 100], showQuickJumper=True, total=table_info['data']['total'] ) for item in table_data: if item['status'] == '0': item['status'] = dict(checked=True) else: item['status'] = dict(checked=False) item['key'] = str(item['onum']) item['fddict_col_no'] = { 'content': item['fddict_col_no'], 'type': 'link', } item['operation'] = [ { 'content': '修改', 'type': 'link', 'icon': 'antd-edit' } if 'dasset:ndstand:edit' in button_perms else {}, { 'content': '删除', 'type': 'link', 'icon': 'antd-delete' } if 'dasset:ndstand:remove' in button_perms else {}, ] return dict( ndstand_table_data=table_data, ndstand_table_pagination=table_pagination, ndstand_table_key=str(uuid.uuid4()), ndstand_table_selectedrowkeys=None, api_check_token_trigger={'timestamp': time.time()} ) return dict( ndstand_table_data=dash.no_update, ndstand_table_pagination=dash.no_update, ndstand_table_key=dash.no_update, ndstand_table_selectedrowkeys=dash.no_update, api_check_token_trigger={'timestamp': time.time()} ) raise PreventUpdate # 重置数据标准搜索表单数据回调 app.clientside_callback( ''' (reset_click) => { if (reset_click) { return [null, null, null, {'type': 'reset'}] } return window.dash_clientside.no_update; } ''', [Output('ndstand-data_std_no-input', 'value'), Output('ndstand-data_std_cn_name-input', 'value'), Output('ndstand-status-select', 'value'), Output('ndstand-operations-store', 'data')], Input('ndstand-reset', 'nClicks'), prevent_initial_call=True ) # 隐藏/显示数据标准搜索表单回调 app.clientside_callback( ''' (hidden_click, hidden_status) => { if (hidden_click) { return [ !hidden_status, hidden_status ? '隐藏搜索' : '显示搜索' ] } return window.dash_clientside.no_update; } ''', [Output('ndstand-search-form-container', 'hidden'), Output('ndstand-hidden-tooltip', 'title')], Input('ndstand-hidden', 'nClicks'), State('ndstand-search-form-container', 'hidden'), prevent_initial_call=True ) @app.callback( Output({'type': 'ndstand-operation-button', 'index': 'edit'}, 'disabled'), Input('ndstand-list-table', 'selectedRowKeys'), prevent_initial_call=True ) def change_ndstand_edit_button_status(table_rows_selected): """ 根据选择的数据标准行数控制编辑按钮状态回调 """ outputs_list = dash.ctx.outputs_list if outputs_list: if table_rows_selected: if len(table_rows_selected) > 1: return True return False return True raise PreventUpdate @app.callback( Output({'type': 'ndstand-operation-button', 'index': 'delete'}, 'disabled'), Input('ndstand-list-table', 'selectedRowKeys'), prevent_initial_call=True ) def change_ndstand_delete_button_status(table_rows_selected): """ 根据选择的数据标准行数控制删除按钮状态回调 """ outputs_list = dash.ctx.outputs_list if outputs_list: if table_rows_selected: return False return True raise PreventUpdate @app.callback( output=dict( modal_visible=Output('ndstand-modal', 'visible', allow_duplicate=True), modal_title=Output('ndstand-modal', 'title'), form_value=Output({'type': 'ndstand-form-value', 'index': ALL}, 'value'), form_label_validate_status=Output({'type': 'ndstand-form-label', 'index': ALL, 'required': True}, 'validateStatus', allow_duplicate=True), form_label_validate_info=Output({'type': 'ndstand-form-label', 'index': ALL, 'required': True}, 'help', allow_duplicate=True), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), edit_row_info=Output('ndstand-edit-id-store', 'data'), modal_type=Output('ndstand-operations-store-bk', 'data') ), inputs=dict( operation_click=Input({'type': 'ndstand-operation-button', 'index': ALL}, 'nClicks'), button_click=Input('ndstand-list-table', 'nClicksButton') ), state=dict( selected_row_keys=State('ndstand-list-table', 'selectedRowKeys'), clicked_content=State('ndstand-list-table', 'clickedContent'), recently_button_clicked_row=State('ndstand-list-table', 'recentlyButtonClickedRow') ), prevent_initial_call=True ) def add_edit_ndstand_modal(operation_click, button_click, selected_row_keys, clicked_content, recently_button_clicked_row): """ 显示新增或编辑数据标准弹窗回调 """ trigger_id = dash.ctx.triggered_id if trigger_id == {'index': 'add', 'type': 'ndstand-operation-button'} \ or trigger_id == {'index': 'edit', 'type': 'ndstand-operation-button'} \ or (trigger_id == 'ndstand-list-table' and clicked_content == '修改'): # 获取所有输出表单项对应value的index form_value_list = [x['id']['index'] for x in dash.ctx.outputs_list[2]] # 获取所有输出表单项对应label的index form_label_list = [x['id']['index'] for x in dash.ctx.outputs_list[3]] if trigger_id == {'index': 'add', 'type': 'ndstand-operation-button'}: ndstand_info = dict(data_std_cn_name=None, data_std_no=None,data_std_eng_name=None,belt_scop=None,busi_defn=None,data_std_schm=None,std_pri_clas=None,std_scd_clas=None,std_thre_clas=None,data_sec_cifd=None,data_clas=None,val_scop=None,fddict_col_no=None,status='0', remark=None) return dict( modal_visible=True, modal_title='新增数据标准', form_value=[ndstand_info.get(k) for k in form_value_list], form_label_validate_status=[None] * len(form_label_list), form_label_validate_info=[None] * len(form_label_list), api_check_token_trigger=dash.no_update, edit_row_info=None, modal_type={'type': 'add'} ) elif trigger_id == {'index': 'edit', 'type': 'ndstand-operation-button'} or (trigger_id == 'ndstand-list-table' and clicked_content == '修改'): if trigger_id == {'index': 'edit', 'type': 'ndstand-operation-button'}: onum = int(','.join(selected_row_keys)) else: onum = int(recently_button_clicked_row['key']) ndstand_info_res = get_ndstand_detail_api(onum=onum) if ndstand_info_res['code'] == 200: ndstand_info = ndstand_info_res['data'] return dict( modal_visible=True, modal_title='编辑数据标准', form_value=[ndstand_info.get(k) for k in form_value_list], form_label_validate_status=[None] * len(form_label_list), form_label_validate_info=[None] * len(form_label_list), api_check_token_trigger={'timestamp': time.time()}, edit_row_info=ndstand_info if ndstand_info else None, modal_type={'type': 'edit'} ) return dict( modal_visible=dash.no_update, modal_title=dash.no_update, form_value=[dash.no_update] * len(form_value_list), form_label_validate_status=[dash.no_update] * len(form_label_list), form_label_validate_info=[dash.no_update] * len(form_label_list), api_check_token_trigger={'timestamp': time.time()}, edit_row_info=None, modal_type=None ) raise PreventUpdate @app.callback( output=dict( form_label_validate_status=Output({'type': 'ndstand-form-label', 'index': ALL, 'required': True}, 'validateStatus', allow_duplicate=True), form_label_validate_info=Output({'type': 'ndstand-form-label', 'index': ALL, 'required': True}, 'help', allow_duplicate=True), modal_visible=Output('ndstand-modal', 'visible'), operations=Output('ndstand-operations-store', 'data', allow_duplicate=True), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), global_message_container=Output('global-message-container', 'children', allow_duplicate=True) ), inputs=dict( confirm_trigger=Input('ndstand-modal', 'okCounts') ), state=dict( modal_type=State('ndstand-operations-store-bk', 'data'), edit_row_info=State('ndstand-edit-id-store', 'data'), form_value=State({'type': 'ndstand-form-value', 'index': ALL}, 'value'), form_label=State({'type': 'ndstand-form-label', 'index': ALL, 'required': True}, 'label') ), prevent_initial_call=True ) def ndstand_confirm(confirm_trigger, modal_type, edit_row_info, form_value, form_label): """ 新增或编辑数据标准弹窗确认回调,实现新增或编辑操作 """ if confirm_trigger: # 获取所有输出表单项对应label的index form_label_output_list = [x['id']['index'] for x in dash.ctx.outputs_list[0]] # 获取所有输入表单项对应的value及label form_value_state = {x['id']['index']: x.get('value') for x in dash.ctx.states_list[-2]} form_value_state2 = {x['id']['index']: x.get('value') for x in dash.ctx.states_list[-2]} print(11111,form_value_state,222222,form_value_state2) form_label_state = {x['id']['index']: x.get('value') for x in dash.ctx.states_list[-1]} if all(validate_data_not_empty(item) for item in [form_value_state.get(k) for k in form_label_output_list]): params_add = form_value_state params_edit = params_add.copy() params_edit['onum'] = edit_row_info.get('onum') if edit_row_info else None api_res = {} modal_type = modal_type.get('type') if modal_type == 'add': api_res = add_ndstand_api(params_add) if modal_type == 'edit': api_res = edit_ndstand_api(params_edit) if api_res.get('code') == 200: if modal_type == 'add': return dict( form_label_validate_status=[None] * len(form_label_output_list), form_label_validate_info=[None] * len(form_label_output_list), modal_visible=False, operations={'type': 'add'}, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('新增成功', type='success') ) if modal_type == 'edit': return dict( form_label_validate_status=[None] * len(form_label_output_list), form_label_validate_info=[None] * len(form_label_output_list), modal_visible=False, operations={'type': 'edit'}, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('编辑成功', type='success') ) return dict( form_label_validate_status=[None] * len(form_label_output_list), form_label_validate_info=[None] * len(form_label_output_list), modal_visible=dash.no_update, operations=dash.no_update, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('处理失败', type='error') ) return dict( form_label_validate_status=[None if validate_data_not_empty(form_value_state.get(k)) else 'error' for k in form_label_output_list], form_label_validate_info=[None if validate_data_not_empty(form_value_state.get(k)) else f'{form_label_state.get(k)}不能为空!' for k in form_label_output_list], modal_visible=dash.no_update, operations=dash.no_update, api_check_token_trigger=dash.no_update, global_message_container=fuc.FefferyFancyMessage('处理失败', type='error') ) raise PreventUpdate @app.callback( [Output('ndstand-delete-text', 'children'), Output('ndstand-delete-confirm-modal', 'visible'), Output('ndstand-delete-ids-store', 'data')], [Input({'type': 'ndstand-operation-button', 'index': ALL}, 'nClicks'), Input('ndstand-list-table', 'nClicksButton')], [State('ndstand-list-table', 'selectedRowKeys'), State('ndstand-list-table', 'clickedContent'), State('ndstand-list-table', 'recentlyButtonClickedRow')], prevent_initial_call=True ) def ndstand_delete_modal(operation_click, button_click, selected_row_keys, clicked_content, recently_button_clicked_row): """ 显示删除数据标准二次确认弹窗回调 """ trigger_id = dash.ctx.triggered_id if trigger_id == {'index': 'delete', 'type': 'ndstand-operation-button'} or (trigger_id == 'ndstand-list-table' and clicked_content == '删除'): if trigger_id == {'index': 'delete', 'type': 'ndstand-operation-button'}: onums = ','.join(selected_row_keys) else: if clicked_content == '删除': onums = recently_button_clicked_row['key'] else: raise PreventUpdate return [ f'是否确认删除编号为{onums}的数据标准?', True, {'onums': onums} ] raise PreventUpdate @app.callback( [Output('ndstand-operations-store', 'data', allow_duplicate=True), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], Input('ndstand-delete-confirm-modal', 'okCounts'), State('ndstand-delete-ids-store', 'data'), prevent_initial_call=True ) def ndstand_delete_confirm(delete_confirm, onums_data): """ 删除数据标准弹窗确认回调,实现删除操作 """ if delete_confirm: params = onums_data delete_button_info = delete_ndstand_api(params) if delete_button_info['code'] == 200: return [ {'type': 'delete'}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('删除成功', type='success') ] return [ dash.no_update, {'timestamp': time.time()}, fuc.FefferyFancyMessage('删除失败', type='error') ] raise PreventUpdate # 显示数据标准导入弹窗及重置上传弹窗组件状态回调 app.clientside_callback( ''' (nClicks) => { if (nClicks) { return [ true, [], [], false ]; } return [ false, window.dash_clientside.no_update, window.dash_clientside.no_update, window.dash_clientside.no_update ]; } ''', [Output('ndstand-tree', 'selectedKeys'), Output('ndstand-import-confirm-modal', 'visible'), Output('ndstand-upload-choose', 'listUploadTaskRecord'), Output('ndstand-upload-choose', 'defaultFileList'), Output('ndstand-import-update-check', 'checked')], Input('ndstand-import', 'nClicks'), prevent_initial_call=True ) @app.callback( output=dict( confirm_loading=Output('ndstand-import-confirm-modal', 'confirmLoading'), modal_visible=Output('batch-result-modal', 'visible',allow_duplicate=True), #add batch_result=Output('batch-result-content', 'children',allow_duplicate=True), #add operations=Output('ndstand-operations-store', 'data', allow_duplicate=True), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True), global_message_container=Output('global-message-container', 'children', allow_duplicate=True) ), inputs=dict( import_confirm=Input('ndstand-import-confirm-modal', 'okCounts') ), state=dict( list_upload_task_record=State('ndstand-upload-choose', 'listUploadTaskRecord'), is_update=State('ndstand-import-update-check', 'checked') ), prevent_initial_call=True ) def ndstand_import_confirm(import_confirm, list_upload_task_record, is_update): """ 数据标准导入弹窗确认回调,实现批量导入用户操作 """ if import_confirm: if list_upload_task_record: url = list_upload_task_record[-1].get('url') batch_param = dict(url=url, is_update=is_update) batch_import_result = batch_import_ndstand_api(batch_param) if batch_import_result.get('code') == 200: return dict( confirm_loading=False, modal_visible=True if batch_import_result.get('message') else False, batch_result=batch_import_result.get('message'), operations={'type': 'batch-import'}, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('导入成功', type='success') ) return dict( confirm_loading=False, modal_visible=True, batch_result=batch_import_result.get('message'), operations=dash.no_update, api_check_token_trigger={'timestamp': time.time()}, global_message_container=fuc.FefferyFancyMessage('导入失败', type='error') ) else: return dict( confirm_loading=False, modal_visible=dash.no_update, batch_result=dash.no_update, operations=dash.no_update, api_check_token_trigger=dash.no_update, global_message_container=fuc.FefferyFancyMessage('请上传需要导入的文件', type='error') ) raise PreventUpdate @app.callback( [Output('ndstand-export-container', 'data', allow_duplicate=True), Output('ndstand-export-complete-judge-container', 'data'), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], [Input('ndstand-export', 'nClicks'), Input('download-ndstand-import-template', 'nClicks')], prevent_initial_call=True ) def export_ndstand_list(export_click, download_click): """ 导出数据标准信息回调 """ trigger_id = dash.ctx.triggered_id if export_click or download_click: if trigger_id == 'ndstand-export': export_ndstand_res = export_ndstand_list_api({}) if export_ndstand_res.status_code == 200: export_ndstand = export_ndstand_res.content return [ dcc.send_bytes(export_ndstand, f'数据标准信息_{time.strftime("%Y%m%d%H%M%S", time.localtime())}.xlsx'), {'timestamp': time.time()}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('导出成功', type='success') ] return [ dash.no_update, dash.no_update, {'timestamp': time.time()}, fuc.FefferyFancyMessage('导出失败', type='error') ] if trigger_id == 'download-ndstand-import-template': download_template_res = download_ndstand_import_template_api() if download_template_res.status_code == 200: download_template = download_template_res.content return [ dcc.send_bytes(download_template, f'数据标准导入模板_{time.strftime("%Y%m%d%H%M%S", time.localtime())}.xlsx'), {'timestamp': time.time()}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('下载成功', type='success') ] return [ dash.no_update, dash.no_update, {'timestamp': time.time()}, fuc.FefferyFancyMessage('下载失败', type='error') ] raise PreventUpdate @app.callback( Output('ndstand-export-container', 'data', allow_duplicate=True), Input('ndstand-export-complete-judge-container', 'data'), prevent_initial_call=True ) def reset_ndstand_export_status(data): """ 导出完成后重置下载组件数据回调,防止重复下载文件 """ time.sleep(0.5) if data: return None raise PreventUpdate @app.callback( output=dict( nfddict_data_modal_visible=Output('ndstand_to_nfddict_data-modal', 'visible'), nfddict_data_modal_title=Output('ndstand_to_nfddict_data-modal', 'title'), nfddict_data_select_value=Output('ndstand_to_nfddict_data-modal', 'content'), api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True) ), inputs=dict( button_click=Input('ndstand-list-table', 'nClicksButton') ), state=dict( clicked_content=State('ndstand-list-table', 'clickedContent'), recently_button_clicked_row=State('ndstand-list-table', 'recentlyButtonClickedRow') ), prevent_initial_call=True ) def ndstand_to_nfddict_data_modal(button_click, clicked_content, recently_button_clicked_row): """ 显示数据标准对应代码数据表格弹窗回调 """ if button_click and clicked_content == recently_button_clicked_row.get('fddict_col_no').get('content'): all_ndstand_info = get_all_ndstand_api({}) if all_ndstand_info.get('code') == 200: all_ndstand = all_ndstand_info.get('data') return dict( nfddict_data_modal_visible=True, nfddict_data_modal_title='代码表数据', nfddict_data_select_value=recently_button_clicked_row.get('fddict_col_no').get('content'), api_check_token_trigger={'timestamp': time.time()} ) return dict( nfddict_data_modal_visible=True, nfddict_data_modal_title='代码表数据', nfddict_data_select_value=recently_button_clicked_row.get('fddict_col_no').get('content'), api_check_token_trigger={'timestamp': time.time()} ) raise PreventUpdate @app.callback( [Output('ndstand-operations-store', 'data', allow_duplicate=True), Output('api-check-token', 'data', allow_duplicate=True), Output('global-message-container', 'children', allow_duplicate=True)], [Input('ndstand-list-table', 'recentlySwitchDataIndex'), Input('ndstand-list-table', 'recentlySwitchStatus'), Input('ndstand-list-table', 'recentlySwitchRow')], prevent_initial_call=True ) def table_switch_ndstand_status(recently_switch_data_index, recently_switch_status, recently_switch_row): """ 表格内切换标准状态回调 """ if recently_switch_data_index: if recently_switch_status: params = dict(onum=int(recently_switch_row['key']), status='0', type='status') else: params = dict(onum=int(recently_switch_row['key']), status='1', type='status') edit_button_result = edit_ndstand_api(params) if edit_button_result['code'] == 200: return [ {'type': 'switch-status'}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('修改成功', type='success') ] return [ {'type': 'switch-status'}, {'timestamp': time.time()}, fuc.FefferyFancyMessage('修改失败', type='error') ] raise PreventUpdate