import React, { useState } from 'react';
import { StatusColorEnum } from '@/components/BadgeStatus';
import { Ellipsis, TableCard } from '@/components';
import '@/style/common.less';
import '../../index.less';
import styles from './index.less';
import type { SceneItem } from '@/pages/rule-engine/Scene/typings';
import { CheckOutlined, DownOutlined, UpOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import { ActionsType, BranchesThen } from '@/pages/rule-engine/Scene/typings';
import MyTooltip from './MyTooltip';
const imageMap = new Map();
imageMap.set('timer', require('/public/images/scene/scene-timer.png'));
imageMap.set('manual', require('/public/images/scene/scene-hand.png'));
imageMap.set('device', require('/public/images/scene/scene-device.png'));
const iconMap = new Map();
iconMap.set('timer', require('/public/images/scene/trigger-type-icon/timing.png'));
iconMap.set('manual', require('/public/images/scene/trigger-type-icon/manual.png'));
iconMap.set('device', require('/public/images/scene/trigger-type-icon/device.png'));
// @ts-ignore
export interface SceneCardProps extends SceneItem {
detail?: React.ReactNode;
tools?: React.ReactNode[];
avatarSize?: number;
className?: string;
onUnBind?: (e: any) => void;
showBindBtn?: boolean;
cardType?: 'bind' | 'unbind';
showTool?: boolean;
onClick?: () => void;
}
enum TriggerWayType {
manual = '手动触发',
timer = '定时触发',
device = '设备触发',
}
enum UnitEnum {
seconds = '秒',
minutes = '分',
hours = '小时',
}
// const selectorRender = (obj: any) => {
// switch (obj?.selector) {
// case 'all':
// return (
//
// 所有的{obj?.productId}
//
// );
// case 'fixed':
// return (
//
// 设备
//
// {(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}
//
//
// );
// case 'org':
// return (
//
// 部门
//
// {(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}
//
//
// );
// default:
// return '';
// }
// };
// const selectorContextRender = (obj: any) => {
// switch (obj?.selector) {
// case 'all':
// return `所有的${obj?.productId}`;
// case 'fixed':
// return `设备${(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}`;
// case 'org':
// return `部门${(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}`;
// default:
// return '';
// }
// };
// const timerRender = (timer: any) => {
// if (timer?.trigger && timer?.mod) {
// const trigger = timer?.trigger;
// const mod = timer?.mod;
// const str: string = trigger === 'week' ? '星期' : trigger === 'month' ? '月' : timer?.cron;
// if (mod === 'once') {
// return `,每${str}${timer.when.join('/')},${timer?.once.time}执行一次`;
// } else {
// return `每${str}${timer.when.join('/')},${timer?.period?.from}-${timer?.period.to},每${
// timer?.period.every
// }${UnitEnum[timer?.period?.unit]}执行一次`;
// }
// }
// return '';
// };
// const operatorRender = (operation: any) => {
// switch (operation?.operator) {
// case 'online':
// return '上线';
// case 'offline':
// return '离线';
// case 'reportEvent':
// return `上报事件${operation?.options?.eventName}`;
// case 'reportProperty':
// return `上报属性${(operation?.options?.propertiesName || []).join(',')}`;
// case 'readProperty':
// return `读取属性${(operation?.options?.propertiesName || []).join(',')}`;
// case 'writeProperty':
// return `修改属性${(operation?.options?.propertiesName || []).join(',')}`;
// case 'invokeFunction':
// return `调用功能${operation?.options?.functionName}`;
// default:
// return '';
// }
// };
const notifyRender = (data: ActionsType | undefined) => {
switch (data?.notify?.notifyType) {
case 'dingTalk':
return (
向{data?.options?.notifierName || data?.notify?.notifierId}
通过钉钉发送
{data?.options?.templateName || data?.notify?.templateId}
);
case 'weixin':
return (
向{data?.options?.sendTo || ''}
{data?.options?.orgName || ''}
{data?.options?.tagName || ''}
通过微信发送
{data?.options?.templateName || data?.notify?.templateId}
);
case 'email':
return (
向{data?.options?.sendTo || ''}
通过邮件发送
{data?.options?.templateName || data?.notify?.templateId}
);
case 'voice':
return (
向{data?.options?.calledNumber || ''}
通过语音发送
{data?.options?.templateName || data?.notify?.templateId}
);
case 'sms':
return (
向{data?.options?.sendTo || ''}通过短信发送
{data?.options?.templateName || data?.notify?.templateId}
);
case 'webhook':
return (
通过webhook发送
{data?.options?.templateName || data?.notify?.templateId}
);
default:
return null;
}
};
const deviceRender = (data: ActionsType | undefined) => {
switch (data?.device?.selector) {
case 'fixed':
return (
{data?.options?.type}
{data?.options?.name}
{data?.options?.properties}
);
case 'tag':
return (
{data?.options?.type}
{data.options?.taglist.map((item: any) => (
{item.type}
{item.name}
{item.value}
))}
{data?.options?.productName}
{data?.options?.properties}
);
case 'relation':
return (
{data?.options?.type}与
{data?.options?.name}具有相同
{data?.options?.relationName}的{data?.options?.productName}设备的
{data?.options?.properties}
);
default:
return null;
}
};
const actionRender = (action: ActionsType, index: number) => {
switch (action?.executor) {
case 'notify':
return (
);
case 'delay':
return (
{action?.delay?.time}
{UnitEnum[action?.delay?.unit || '']}
后执行后续动作
);
case 'device':
return (
);
case 'alarm':
return (
);
default:
return null;
}
};
const conditionsRender = (when: any[], index: number) => {
if (when.length) {
return (when[index]?.terms || []).join('');
}
return '';
};
const branchesActionRender = (actions: any[]) => {
if (actions && actions?.length) {
const list: any[] = [];
actions.map((item, index) => {
const dt = actionRender(item, index);
list.push(dt);
});
return list.map((item, index) => (
{item}
{actions[index]?.options?.terms && (
动作{index + 1}
{actions[index]?.options?.terms}
)}
));
}
return '';
};
const ContentRender = (data: SceneCardProps) => {
const [visible, setVisible] = useState(false);
const type = data.triggerType;
if (!!type && (data.branches || [])?.length) {
const trigger = data?.options?.trigger;
const text =
(trigger?.name || '') +
(trigger?.productName || '') +
(trigger?.when || '') +
(trigger?.time || '') +
(trigger?.extraTime || '') +
(trigger?.type || '');
return (
{trigger?.name || ''}
{trigger?.productName || ''}
{(trigger?.when || '') +
(trigger?.time || '') +
(trigger?.extraTime || '') +
(trigger?.type || '')}
{(visible ? data.branches || [] : (data?.branches || []).slice(0, 2)).map(
(item: any, index) => {
return (
{type === 'device' ? (index === 0 ? '当' : '否则') : '执行'}
{type === 'device' && (
{conditionsRender(data.options?.terms || [], index)}
{item.shakeLimit?.enabled && (
({item.shakeLimit?.time}秒内发生{item.shakeLimit?.threshold}
次以上时执行一次)
)}
)}
{(item?.then || []).map((i: BranchesThen, _index: number) => (
{i.parallel ? '并行执行' : '串行执行'}
{branchesActionRender(Array.isArray(i?.actions) ? i?.actions : [])}
))}
);
},
)}
{(data?.branches || []).length > 2 && (
{
e.stopPropagation();
setVisible(!visible);
}}
>
展开更多{!visible ? : }
)}
);
} else {
return 未配置规则
;
}
};
export const ExtraSceneCard = (props: SceneCardProps) => {
return (
})
{TriggerWayType[props.triggerType]}
);
};
export default (props: SceneCardProps) => {
return (
})
{TriggerWayType[props.triggerType]}
);
};