<?php

namespace App\Models;

use App\Models\Traits\BelongsToArea;
use App\Traits\HasStatus;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\Mall\Models\Order;
use Modules\User\Models\User;
use Modules\User\Traits\BelongsToUser;

class AreaCode extends Model
{
    use BelongsToArea, BelongsToUser, HasStatus;

    const STATUS_INIT = 0;
    const STATUS_USED = 1;
    const STATUS_SIGN = 2;

    const STATUS = [
        self::STATUS_INIT => '未提取',
        self::STATUS_USED => '已提取',
        self::STATUS_SIGN => '已核销',
    ];


    /**
     * Notes: 生成人-管理人
     *
     * @Author: 玄尘
     * @Date: 2023/1/11 13:41
     * @return BelongsTo
     */
    public function manage(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Notes: 设置使用
     *
     * @Author: 玄尘
     * @Date: 2023/1/12 14:25
     */
    public function used()
    {
        $this->status = self::STATUS_USED;
        $this->save();
    }

    /**
     * Notes: 核销
     *
     * @Author: 玄尘
     * @Date: 2023/1/16 11:15
     */
    public function sign()
    {
        $this->order->state = Order::STATUS_SIGNED;
        $this->order->save();
        $this->status = self::STATUS_SIGN;
        $this->save();
        return true;
    }

    /**
     * Notes: 关联订单
     *
     * @Author: 玄尘
     * @Date: 2023/1/12 15:02
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function order()
    {
        return $this->hasOne(Order::class);
    }


}
