NameRuleController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\NamingRule\AutonameRequest;
  5. use App\Http\Requests\API\NamingRule\CombinationSettingRequest;
  6. use App\Http\Requests\API\NamingRule\CreateOrUpdateRequest;
  7. use App\Http\Resources\API\NamingRuleResource;
  8. use App\Models\NamingRule;
  9. use App\Models\Project;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. class NameRuleController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. */
  17. public function index(Request $request)
  18. {
  19. $namingRules = NamingRule::allowed()->with(['company'])->filter($request->all())->paginate();
  20. return NamingRuleResource::collection($namingRules);
  21. }
  22. /**
  23. * Store a newly created resource in storage.
  24. */
  25. public function store(CreateOrUpdateRequest $request)
  26. {
  27. $formData = $request->only(['name', 'status', 'company_id', 'global']);
  28. if (! Auth::user()->super_admin) {
  29. $formData['company_id'] = Auth::user()->company_id;
  30. unset($formData['global']);
  31. }
  32. NamingRule::create($formData);
  33. return $this->created();
  34. }
  35. /**
  36. * Display the specified resource.
  37. */
  38. public function show(string $id)
  39. {
  40. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  41. return new NamingRuleResource($namingRule);
  42. }
  43. /**
  44. * Update the specified resource in storage.
  45. */
  46. public function update(CreateOrUpdateRequest $request, string $id)
  47. {
  48. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  49. $namingRule->fill($request->only([
  50. 'name', 'status', 'global'
  51. ]));
  52. $namingRule->save();
  53. return $this->noContent();
  54. }
  55. /**
  56. * Remove the specified resource from storage.
  57. */
  58. public function destroy(string $id)
  59. {
  60. }
  61. public function enabled()
  62. {
  63. $namingRules = NamingRule::where('status', 1)->where(function ($query) {
  64. return $query->where("company_id", Auth::user()->company_id)->orWhere([
  65. 'company_id' => 0,
  66. 'global' => 1
  67. ]);
  68. })->select(['id', 'name'])->get();
  69. return $this->success([
  70. 'data' => $namingRules
  71. ]);
  72. }
  73. public function combinationSetting(CombinationSettingRequest $request, string $id)
  74. {
  75. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  76. $namingRule->combination_rules = $request->all();
  77. $namingRule->save();
  78. return $this->noContent();
  79. }
  80. public function autoname(AutonameRequest $request, string $id)
  81. {
  82. $namingRule = NamingRule::query()->allowed()->findOrFail($id);
  83. if (! $namingRule->combination_rules) {
  84. return $this->badRequest("Please set naming combination rules");
  85. }
  86. $code = match ($request->code_type) {
  87. "project" => Project::query()->find($request->code_id)?->code,
  88. default => ""
  89. };
  90. if (! $code) {
  91. return $this->badRequest("The supported type or selected item does not exist");
  92. }
  93. $name = $code;
  94. foreach ($namingRule->combination_rules as $rule) {
  95. if (! isset($request->fields[$rule['field']]) || !$request->fields[$rule['field']]) {
  96. return $this->badRequest(sprintf("%s field is required", $rule['field']));
  97. }
  98. $name .= $rule['link'] . $request->fields[$rule['field']];
  99. }
  100. return $this->success([
  101. 'data' => $name
  102. ]);
  103. }
  104. }