with('infoTplList')->orderBy('sort')->get(); return view('admin.flow.tpl', [ 'siteId' => $siteId, 'tplStageList' => $tplStageList ]); } //模板保存 public function tplSave(Request $request) { $dataList = $request->input('dataList')??[]; $stepIds = array_column($dataList, 'step_id'); $stageIds = FlowStageTpl::query()->pluck('id')->toArray(); $delIds = array_diff($stageIds, $stepIds); if ($delIds) { FlowStageTpl::query()->whereIn('id', $delIds)->delete(); } $mapInfoIds=FlowInfoTpl::query()->select(['id','stage_id'])->get()->groupBy('stage_id')->toArray(); foreach ($dataList as $step) { $stepChildren=$step['children'] ?? []; if (empty($step['step_id'])) { $stage = FlowStageTpl::query()->create([ 'title' => $step['step_title'], ]); foreach ($stepChildren as $item) { FlowInfoTpl::query()->create([ 'stage_id' => $stage->id, 'detail_list' => ($item['children'] ?? []), ]); } } else { $infoIds=array_column($mapInfoIds[$step['step_id']]??[],'id'); $dataInfoIds=array_column($stepChildren,'info_id'); $infoDelIds=array_diff($infoIds,$dataInfoIds); if ($infoIds){ FlowInfoTpl::query()->whereIn('id', $infoDelIds)->delete(); } FlowStageTpl::query()->where(['id' => $step['step_id']])->update(['title' => $step['step_title']]); foreach ($stepChildren as $item) { if (empty($item['info_id'])) { FlowInfoTpl::query()->create([ 'stage_id' => $step['step_id'], 'detail_list' => ($item['children'] ?? []), ]); } else { FlowInfoTpl::query()->where(['id' => $item['info_id']])->update([ 'detail_list' => json_encode($item['children'] ?? []), ]); } } } } return response()->json(['message' => '操作成功']); } }