<?php

namespace app\model\system;

use library\traits\ModelTrait;
use library\basic\BaseModel;

/**
 * TODO 商家Model
 * Class Merchant
 * @package app\model\system
 */
class Merchant extends BaseModel
{
	/**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

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

    use ModelTrait;

    /**
     * 商家列表
     * @param $where
     * @return array
     */
    public static function systemPage($where)
    {
        $model = self::setWhere($where);
        $count = $model->count();
        $list = $model->page((int)$where['page'], (int)$where['limit'])
            ->select()
            ->each(function ($item) {
                $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            });
        return compact('count', 'list');
    }

    /**
     * 设置商家 where 条件
     * @param $where
     * @param null $model
     * @return mixed
     */
    public static function setWhere($where, $model = null)
    {
        $model = $model === null ? new self() : $model;
        $model = $model->alias('c');
        $model = $model->field('c.*');
        if (isset($where['name']) && $where['name'] != '') $model = $model->where('c.id|c.name', 'LIKE', "%$where[name]%");
        if (isset($where['main']) && $where['main'] != '') $model = $model->where('c.main', $where['main']);
        return $model->order('c.id desc')->where('c.is_del', 0);
    }

    /**
     * 详情
     */
    public static function getOne($id)
    {
        $info = self::get($id);
        return $info;
    }
}
