<?php

namespace App\Domains\Virtual\Repositories;

use App\Core\Repository;
use App\Models\Virtual\Package as Model;

class PackageRepository extends Repository
{
    /**
     * 是否关闭缓存
     *
     * @var boolean
     */
    protected $cacheSkip = false;

    /**
     * 是否开启数据转化
     *
     * @var bool
     */
    protected $needTransform = false;

    /**
     * @var array
     */
    protected $fieldSearchable = [
        'id'               => '=',
        'type'             => '=',
        'sn'               => 'like',
        'name'             => 'like',
        'carrier_operator' => '=',
    ];

    public function model()
    {
        return Model::class;
    }

    /**
     * 数据格式化
     *
     * @param mixed $result
     *
     * @return mixed
     */
    public function transform($model)
    {
        return $model->toArray();
    }

    /**
     * 查询条件
     *
     * @return void
     */
    public function withConditions(array $conditions = [])
    {
        if (isset($conditions['id'])) {
            $conditions['id'] = array_wrap($conditions['id']);
            $this->model = $this->model->whereIn('id', $conditions['id']);
        }

        if (isset($conditions['type'])) {
            $this->model = $this->model->where('type', $conditions['type']);
        }

        if (!empty($conditions['sn'])) {
            $this->model = $this->model->where('sn', 'like', "%{$conditions['sn']}%");
        }

        if (!empty($conditions['name'])) {
            $this->model = $this->model->where('name', 'like', "%{$conditions['name']}%");
        }

        if (isset($conditions['carrier_operator'])) {
            $this->model = $this->model->where('carrier_operator', $conditions['carrier_operator']);
        }

        if (isset($conditions['status'])) {
            $this->model = $this->model->where('status', $conditions['status']);
        }

        if (isset($conditions['flowed'])) {
            $this->model = $this->model->where('flowed', $conditions['flowed']);
        }

        return $this;
    }
}
