<?php

namespace app\admin\model;

use think\Model;

/**
 * ReqOrder
 */
class ReqOrder extends Model
{
    // 表名
    protected $name = 'req_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'city_text',
    ];


    public function getCityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCityAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }

    public function getRequireItemAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setRequireItemAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getPrepayAmountAttr($value): float
    {
        return (float)$value;
    }

    public function getPrepayImageAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setPrepayImageAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getSettleAmountAttr($value): float
    {
        return (float)$value;
    }

    public function department()
    {
        return $this->belongsTo(\app\admin\model\department\Department::class, 'req_corp', 'id');
    }

    public function admin()
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'req_user_id', 'id');
    }

    public function supplier()
    {
        return $this->belongsTo(\app\admin\model\Supplier::class, 'supplier_id', 'id');
    }
}