EsgController.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\EsgHistory;
  5. use App\Models\UploadFile;
  6. use App\Supports\Response;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * @group Lottery Prize
  13. */
  14. class EsgController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. }
  19. public function histories($locale = 'tw')
  20. {
  21. $locale = $locale == "tw" ? "zh_TW" : $locale;
  22. $data = EsgHistory::where("visible", 1)->orderByDesc("selected_year", "desc")->orderByDesc("selected_month")->get();
  23. $yearList = EsgHistory::select("selected_year", \DB::raw("concat(selected_year, '年') as lable"))->distinct()->orderBy("selected_year", "desc")->pluck('lable', 'selected_year');
  24. $result = [];
  25. $result["yearList"] = $yearList;
  26. foreach ($data as $item) {
  27. $result["list"][$item->selected_year][] = [
  28. "operateMonth " => $item->selected_year . "." . str_pad($item->selected_month, 2, "0", STR_PAD_LEFT),
  29. "title" => $item->getTranslation("title", $locale),
  30. "description" => $item->getTranslation("description", $locale)
  31. ];
  32. }
  33. return Response::ok($result);
  34. }
  35. public function uploadFiles($locale = "tw")
  36. {
  37. $locale = $locale == "tw" ? "zh_TW" : $locale;
  38. // 定義「取得時間」的翻譯
  39. $awardDateLabels = [
  40. 'zh_TW' => '取得時間',
  41. 'en' => 'Awarded',
  42. ];
  43. // 取得對應的標籤,如果找不到則使用預設值
  44. $awardDateLabel = $awardDateLabels[$locale] ?? $awardDateLabels['zh_TW'];
  45. $request_ordering = $request->ordering ?? "desc";
  46. $fileList = UploadFile::where("type", 1)
  47. ->select("title", "upload_link", "award_date")
  48. ->orderBy("award_date", $request_ordering)
  49. ->orderBy("order", $request_ordering)->get()->map(function ($record) use ($awardDateLabel, $locale) {
  50. return [
  51. "title" => $record->getTranslation("title", $locale),
  52. "uploadLink" => $record->upload_file_link,
  53. "awardDate" => $awardDateLabel . " : " . date("Y/m/d", strtotime($record->award_date))
  54. ];
  55. });
  56. return Response::ok($fileList);
  57. }
  58. }