|
- <?php
-
- namespace App\Models\HtpmsCustomer\Existing;
-
- use App\Models\Cast;
- use App\Models\ColumnName;
- use App\Util\DateUtil;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Support\Facades\DB;
-
- class DiscountTicket extends Model
- {
- const COL_NAME_PARK_ID = 'park_id';
- const COL_NAME_TICKET_NAME = 'ticketname';
- const COL_NAME_DISCOUNT_TICKET_CODE = 'lineno';
- const COL_NAME_START_DATETIME = 'start_datetime';
-
- protected $connection = 'htpms_customer';
- protected $table = 'tbl2_discountticket';
- protected $fillable = []; // 参照専用
-
- protected $visible = [
- self::COL_NAME_TICKET_NAME,
- self::COL_NAME_DISCOUNT_TICKET_CODE,
- "ticket_name",
- ColumnName::DISCOUNT_TICKET_CODE,
- ];
-
- protected $casts = [
- self::COL_NAME_START_DATETIME => Cast::DATETIME,
- ];
-
-
- public static function getBuilder(string $name = 'main'): Builder
- {
- $now = DateUtil::now();
- $instance = new static();
- $tablename = self::getTableName();
- $sub = function (Builder $query) use ($tablename) {
- $query->selectRaw('*, rank() over (partition by park_id, adjuster_id, lineno order by start_datetime desc) as rank')
- ->from($tablename)
- ->where('start_datetime', '<=', DB::raw('CURRENT_TIMESTAMP'));
- };
- return DB::connection($instance->getConnectionName())->table($sub, $name)->where($name . '.rank', 1);
- }
-
- public static function getTableName(): string
- {
- return (new static)->getTable();
- }
- }
|