<?php


namespace app\models\system;

use app\admin\model\system\SystemAdmin;
use app\models\user\User;
use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

/**
 * 门店自提 model
 * Class SystemStore
 * @package app\model\system
 */
class SystemStoreApply extends BaseModel
{

    use ModelTrait;

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

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

    protected $append = [
        'latlng'
    ];

    public static function getLatlngAttr($value, $data)
    {
        return $data['latitude'] . ',' . $data['longitude'];
    }

    /**
     * 获取门店列表
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getList($where)
    {
        $model = new self();
        if (isset($where['name']) && $where['name'] != '') {
            $model = $model->where('id|name|introduction', 'like', '%' . $where['name'] . '%');
        }
        if (isset($where['type']) && $where['type'] != '' && ($data = self::setData($where['type']))) {
            $model = $model->where($data);
        }
        $count = $model->count();
        $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
        foreach ($data as &$v) {
            $v['user'] = User::where('uid', $v['uid'])->value('nickname') . '/' . $v['uid'];
        }
        return compact('count', 'data');
    }

    /**
     * 获取连表查询条件
     * @param $type
     * @return array
     */
    public static function setData($type)
    {
        switch ((int)$type) {
            case 1:
                $data = ['status' => 0];
                break;
            case 2:
                $data = ['status' => 1];
                break;
            case 3:
                $data = ['status' => 2];
                break;
        };
        return isset($data) ? $data : [];
    }

    /**
     * 获取门店信息
     * @param int $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDetail($id)
    {

        $storeInfo = self::where('id', $id)->find();
        $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
        return $storeInfo;
    }
}