/*
 * @Author: ysh
 * @Date: 2025-01-22 16:58:56
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-01-23 11:32:05
 */
import { listBudget, addBudget, updateBudget, delBudget } from "@/api/oa/budget/budget.js";
import { listTechnical, getTechnical, delTechnical, addTechnical, updateTechnical } from "@/api/oa/technical/technical";
import { listTechnicalPlan, getTechnicalPlan, delTechnicalPlan, addTechnicalPlan, updateTechnicalPlan } from "@/api/oa/technical/technicalPlan";
import { listSafe, getSafe, delSafe, addSafe, updateSafe } from "@/api/oa/safe/safe";
import { listSubProcInstId, delDeployment } from "@/api/flowable/finished"
import { deleteResources } from "@/utils/deleteResource";

// 取消项目预算、技术方案、技术交底、安全交底流程，并且删除数据
export async function cancelSubProcess(ids, projectId) {
  try {
    const [resSafe, resBudget, resTechnical, resTechnicalPlan] = await Promise.all([
      listSafe({ pageSize: 100, projectId }),
      listBudget({ pageSize: 100, projectId }),
      listTechnical({ pageSize: 100, projectId }),
      listTechnicalPlan({ pageSize: 100, projectId })
    ]);
 
    const safeIds = extractIds(resSafe.rows,'safeId');
    const budgetIds = extractIds(resBudget.rows,'budgetId');
    const technicalIds = extractIds(resTechnical.rows,'technicalId');
    const technicalPlanIds = extractIds(resTechnicalPlan.rows,'technicalPlanId');
 
    await deleteResourcesByIds('项目预算', budgetIds);
    await deleteResourcesByIds('安全交底', safeIds);
    await deleteResourcesByIds('技术方案', technicalIds);
    await deleteResourcesByIds('技术交底', technicalPlanIds);

    await delDeployment(ids);
 
    return { code: 200 };
  } catch (error) {
    console.error('Error canceling subprocess:', error);
    throw { code: 500, message: error.message || 'An unknown error occurred' };
  }
}
 
function extractIds(rows,name) {
  return rows ? rows.map(item => item[name] /* 假设每个对象都有一个名为'id'的属性 */) : [];
}
 
async function deleteResourcesByIds(type, ids) {
  await Promise.all(ids.map(id => deleteResources(type, id)));
}