<?php

namespace App\Models\marketing\mysql;

use Api\PhpUtils\Mysql\MysqlBase;
use App\Services\marketing\MarketingService;

/**
 * 自提点
 * Class TakePlace
 * @package App\Models\marketing\mysql
 */
class TakePlace extends MysqlBase
{
    const TABLE_NAME = 'take_place';
    const CONFIG_INDEX = 'marketing';
    const PRIMARY_KEY = 'take_place_id';

    const STATUS_NORMAL = 0;    // 正常
    const STATUS_DELETE = 1;    // 删除

    /**
     * 获取
     * @param $params
     * @return mixed
     */
    public static function searchList($params)
    {
        $lifeAccountId = $params['life_account_id'] ?? MarketingService::getPublicLifeAccountId();
        $keywords = $params['keywords'] ?? '';
        $offset = $params['offset'] ?? 0;
        $limit = $params['limit'] ?? 20;
        $takePlaceIds = $params['take_place_id'] ?? [];

        if(!empty($takePlaceIds)) {
            $where['take_place_id'] = $takePlaceIds;
        }
        if($lifeAccountId) {
            $where['life_account_id'] = $lifeAccountId;
        }
        if($keywords) {
            $where['OR'] = [
                'take_place_name[~]' => $keywords,
                'contact_name[~]' => $keywords,
            ];
        }
        $where['is_delete'] = TakePlace::STATUS_NORMAL;
        $where['ORDER'] = ['take_place_id' => 'DESC'];
        $where['LIMIT'] = [$offset, $limit];

        $res['list'] = self::select('*', $where);
        unset($where['ORDER'],$where['LIMIT']);
        $res['total'] = self::count('*', $where);

        return $res;
    }

    public static function getRecord(array $where, $column = '*')
    {
        return self::get($column, $where);
    }

    public static function getRecords(array $where, $column = '*')
    {
        return self::select($column, $where);
    }

    public static function existTakePlaceName(string $takePlaceName) : bool
    {
        $exist = self::getRecord(['take_place_name' => $takePlaceName], ['take_place_id']);
        return !empty($exist);
    }
}
