import { StateTypeEnum, CreateStateQuery, UpdateStateQuery, RemoveStateQuery, StateInfo, } from '@easytwin/core'; import apis from '@/constants/api.swagger-constants'; import { request } from '@/utils'; import { initStateData } from '.'; /** * 创建状态 * @author xuek * @since 2022-03-21 * @param { CreateStateQuery } query * @returns { Promise } */ export async function create(query: CreateStateQuery): Promise { const { type, config, bizId, name } = query; try { if (type === StateTypeEnum.PROJECT) { const body = { name: name ?? '状态', config: { ...initStateData[type], ...config, }, }; const info = await request(apis.scenesStatesPost, { params: { sceneId: bizId, }, body, }); if (!info) return; return { ...info, ...body, rank: 0 }; } } catch (error) { console.error('createState:', error); return Promise.reject('创建状态失败!'); } } /** * 更新状态 * @author xuek * @since 2022-03-21 * @param { UpdateStateQuery } query * @returns { Promise } */ export async function update(query: UpdateStateQuery): Promise { try { const { name, type, id, bizId, config } = query; const body = { name, config, }; if (!name) delete body.name; if (!config) delete body.config; if (type === StateTypeEnum.PROJECT) { const result = await request(apis.scenesStatePatch, { params: { sceneId: bizId, id, }, body, }); return result; } } catch (error) { console.error('updateState:', error); return Promise.reject('更新状态失败!'); } } /** * 删除状态 * @author xuek * @since 2022-03-21 * @param { RemoveStateQuery } query * @returns { Promise } */ export async function remove(query: RemoveStateQuery): Promise { try { const { type, id, bizId } = query; if (type === StateTypeEnum.PROJECT) { const result = await request(apis.scenesStateDelete, { params: { sceneId: bizId, id, }, }); return result; } } catch (error) { console.error('removeState:', error); return Promise.reject('删除状态失败!'); } }