<?php

namespace App\Domains\Virtual\Repositories;

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

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

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

    /**
     * @var array
     */
    protected $fieldSearchable = [
        'id'         => '=',
        'created_at' => 'like',
    ];

    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 = [])
    {
        $this->model = $this->model->where(function ($query) use ($conditions) {
            if (isset($conditions['id'])) {
                $conditions['id'] = array_wrap($conditions['id']);
                $query->whereIn('id', $conditions['id']);
            }

            if (isset($conditions['type'])) {
                $conditions['type'] = array_map('intval', array_wrap($conditions['type']));
                $query->whereIn('type', $conditions['type']);
            }

            if (isset($conditions['company_id'])) {
                $query->where('company_id', $conditions['company_id']);
            }

            if (isset($conditions['package_id'])) {
                $query->where('package_id', $conditions['package_id']);
            }

            if (isset($conditions['price'])) {
                $query->where('price', $conditions['price']);
            }

            if (isset($conditions['status'])) {
                $query->where(function ($subQuery) use ($conditions) {
                    $subQuery->where('status', $conditions['status']);

                    if ($conditions['status'] == 1) {
                        $subQuery->orWhereHas('package', function ($relation) use ($conditions) {
                            $relation->withTrashed()->where('status', $conditions['status']);
                        });
                    }
                });
            }

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

            if (!empty($conditions['package_name'])) {
                $query->whereHas('package', function ($relation) use ($conditions) {
                    $relation->withTrashed()->where('name', $conditions['package_name']);
                });
            }

            if (isset($conditions['carrier_operator'])) {
                $query->whereHas('package', function ($relation) use ($conditions) {
                    $relation->withTrashed()->where('carrier_operator', $conditions['carrier_operator']);
                });
            }

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

        return $this;
    }
}
