<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class DistInquiry extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'dist_inquiry';

    /*
     * 关联到分销商
     */
    public function distributor()
    {
        return $this->hasOne(DistAdminDistributor::class,'id', 'dist_id');
    }

    /*
     * 生成订单号
     */
    public static function generateOrderNumber()
    {
        // 获取当前日期，格式为YYYYMMDD
        $date = now()->format('Ymd');

        // 查询当天订单数量
        $todayOrderCount = self::whereDate('created_at', Carbon::today())->count();

        // 订单号：日期 + 当天订单数量 + 1
        $orderNumber = $date . str_pad($todayOrderCount + 1, 4, '0', STR_PAD_LEFT);

        return 'Q'.$orderNumber;
    }
}
