Company.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Models\Movie;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\DTO\SearchData;
  6. use App\Http\Traits\PagingTrait;
  7. use App\Http\Traits\CommonTrait;
  8. class Company extends Model
  9. {
  10. use CommonTrait;
  11. use PagingTrait;
  12. protected $table = 'tb_company';
  13. protected $primaryKey = 'id';
  14. public $keyType = 'int';
  15. public $incrementing = true;
  16. public $timestamps = true;
  17. const CREATED_AT = 'created_at';
  18. const UPDATED_AT = 'updated_at';
  19. const DELETED_AT = null;
  20. protected $guarded = [];
  21. /**
  22. * 영화사 목록 조회 (검색 + 페이징)
  23. */
  24. public function list(SearchData $params): object
  25. {
  26. $where = $this->buildWhereQuery($params);
  27. $sql = sprintf("
  28. SELECT
  29. COUNT(*) AS total
  30. FROM tb_company CMP
  31. WHERE TRUE %s;
  32. ", $where);
  33. $total = DB::selectOne($sql)->total;
  34. $sql = sprintf("
  35. SELECT
  36. CMP.company_cd AS companyCd,
  37. CMP.company_nm AS companyNm,
  38. CMP.company_nm_en AS companyNmEn,
  39. CMP.ceo_nm AS ceoNm,
  40. CMP.company_part_names AS companyPartNames,
  41. CMP.filmo_names AS filmoNames
  42. FROM tb_company CMP
  43. WHERE TRUE %s
  44. ORDER BY
  45. CMP.company_nm ASC
  46. LIMIT ?, ?;
  47. ", $where);
  48. $list = $this->getPaginator(
  49. DB::select($sql, [$params->offset, $params->perPage]),
  50. $total, $params->perPage, $params->page, $params->path
  51. );
  52. $rows = $list->count();
  53. return (object)[
  54. 'total' => $total,
  55. 'rows' => $rows,
  56. 'list' => $list
  57. ];
  58. }
  59. /**
  60. * 영화사 상세 조회 (기본정보 + 분류(parts) + 필모그래피)
  61. */
  62. public function info(string $companyCd): ?object
  63. {
  64. $sql = "
  65. SELECT
  66. company_cd AS companyCd,
  67. company_nm AS companyNm,
  68. company_nm_en AS companyNmEn,
  69. ceo_nm AS ceoNm,
  70. parts
  71. FROM tb_company
  72. WHERE company_cd = ?
  73. LIMIT 1;
  74. ";
  75. $row = DB::selectOne($sql, [$companyCd]);
  76. if (!$row) {
  77. return null;
  78. }
  79. $row->parts = $row->parts ? json_decode($row->parts) : [];
  80. $filmos = DB::select("
  81. SELECT
  82. movie_cd AS movieCd,
  83. movie_nm AS movieNm,
  84. company_part_nm AS companyPartNm
  85. FROM tb_company_filmo
  86. WHERE company_cd = ?
  87. ORDER BY id ASC;
  88. ", [$companyCd]);
  89. $row->filmos = $filmos;
  90. return $row;
  91. }
  92. /**
  93. * 영화사 존재 여부 (상세 정보 수집 완료 기준: parts NOT NULL)
  94. */
  95. public function isExists(string $companyCd): int
  96. {
  97. $sql = "SELECT IF(COUNT(*) > 0, 1, 0) AS isExists FROM tb_company WHERE company_cd = ? AND parts IS NOT NULL;";
  98. return DB::selectOne($sql, [$companyCd])->isExists;
  99. }
  100. private function buildWhereQuery(SearchData $params): string
  101. {
  102. $where = "";
  103. if ($params->keyword) {
  104. $where .= sprintf(" AND (CMP.company_nm LIKE '%%%s%%' OR CMP.company_nm_en LIKE '%%%s%%') ", $params->keyword, $params->keyword);
  105. }
  106. if ($params->ceo) {
  107. $where .= sprintf(" AND CMP.ceo_nm LIKE '%%%s%%' ", $params->ceo);
  108. }
  109. return $where;
  110. }
  111. }