TplController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Controllers\Admin\Flow;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Models\FlowInfoTpl;
  5. use App\Http\Models\FlowStageTpl;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 项目管理 详情下 的新版流程 模板
  9. * Class TplController
  10. * @package App\Http\Controllers\Admin\Flow
  11. */
  12. class TplController extends Controller
  13. {
  14. //新版流程 模板
  15. public function tplIndex($siteId){
  16. $tplStageList = FlowStageTpl::query()->with('infoTplList')->orderBy('sort')->get();
  17. return view('admin.flow.tpl', [
  18. 'siteId' => $siteId,
  19. 'tplStageList' => $tplStageList
  20. ]);
  21. }
  22. //模板保存
  23. public function tplSave(Request $request)
  24. {
  25. $dataList = $request->input('dataList')??[];
  26. $stepIds = array_column($dataList, 'step_id');
  27. $stageIds = FlowStageTpl::query()->pluck('id')->toArray();
  28. $delIds = array_diff($stageIds, $stepIds);
  29. if ($delIds) {
  30. FlowStageTpl::query()->whereIn('id', $delIds)->delete();
  31. }
  32. $mapInfoIds=FlowInfoTpl::query()->select(['id','stage_id'])->get()->groupBy('stage_id')->toArray();
  33. foreach ($dataList as $step) {
  34. $stepChildren=$step['children'] ?? [];
  35. if (empty($step['step_id'])) {
  36. $stage = FlowStageTpl::query()->create([
  37. 'title' => $step['step_title'],
  38. ]);
  39. foreach ($stepChildren as $item) {
  40. FlowInfoTpl::query()->create([
  41. 'stage_id' => $stage->id,
  42. 'detail_list' => ($item['children'] ?? []),
  43. ]);
  44. }
  45. } else {
  46. $infoIds=array_column($mapInfoIds[$step['step_id']]??[],'id');
  47. $dataInfoIds=array_column($stepChildren,'info_id');
  48. $infoDelIds=array_diff($infoIds,$dataInfoIds);
  49. if ($infoIds){
  50. FlowInfoTpl::query()->whereIn('id', $infoDelIds)->delete();
  51. }
  52. FlowStageTpl::query()->where(['id' => $step['step_id']])->update(['title' => $step['step_title']]);
  53. foreach ($stepChildren as $item) {
  54. if (empty($item['info_id'])) {
  55. FlowInfoTpl::query()->create([
  56. 'stage_id' => $step['step_id'],
  57. 'detail_list' => ($item['children'] ?? []),
  58. ]);
  59. } else {
  60. FlowInfoTpl::query()->where(['id' => $item['info_id']])->update([
  61. 'detail_list' => json_encode($item['children'] ?? []),
  62. ]);
  63. }
  64. }
  65. }
  66. }
  67. return response()->json(['message' => '操作成功']);
  68. }
  69. }