<?php


namespace app\models\merchant;


use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;

class Merchant extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'merchant';

    use ModelTrait;

    public static function getList($where = null)
    {
        $data = ($data = self::setWhere($where)->select()) && count($data) ? $data->toArray() : [];
        foreach ($data as &$item) {
            $item['industry_name'] = Industry::where('id', 'IN', $item['industry_id'])->column('name');
            $item['industry_name'] = $item['industry_name'] ? implode(',', $item['industry_name']) : [];
            $item['industry_id'] = $item['industry_id'] ? explode(',', $item['industry_id']) : [];
            // $item['address'] = $item['address'] ? explode(',', $item['address']) : [];
            // $item['facilities'] = $item['facilities'] ? explode(',', $item['facilities']) : [];
            // $item['day_time'] = $item['day_time'] ? explode(' - ', $item['day_time']) : [];
        }
        $count = self::setWhere($where)->count();
        return ['count' => $count, 'list' => $data];
    }

    public static function setWhere($where)
    {
        $model = (new self);
        if ($where['key_word']) {
            $model = $model->where('name|phone', 'like', '%' . $where['key_word'] . '%');
        }
        if ($where['industry_id']) {
            $model = $model->where('industry_id', $where['industry_id']);
        }
        if ($where['version']) {
            $model = $model->where('version', $where['version']);
        }
        if ($where['data']) {
            $model = self::getModelTime($where, $model, 'add_time');
        }
        return $model;
    }
}