<?php
/**
 * Created by PhpStorm.
 * User: pengfei@yidian-inc.com
 * Date: 2021/8/20 2:38 下午
 */

namespace App\Models\marketing\mysql;

use Api\PhpUtils\Mysql\MysqlBase;

class ColonelDistributorColonelApply extends MysqlBase
{
    const TABLE_NAME = 'colonel_distributor_colonel_apply';
    const CONFIG_INDEX = 'marketing';
    const PRIMARY_KEY = 'colonel_apply_id';

    const STATUS_AUDIT = 0;//待审核
    const STATUS_PASS = 1;//审核通过
    const STATUS_REJECT = 2;//审核驳回

    const DEFAULT_COLUMN = ['colonel_apply_id','audit_status','user_id','phone','contact_name','take_place_name','province','city','area','location','address'];

    public static function insertRecord(array $column) : int
    {
        $id = self::insert($column);
        return intval($id);
    }

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

    public static function updateRecord(array $columns, array $where)
    {
        return self::update($columns, $where);
    }

    public static function getPages(array $where = [], $column = '*', int $page, int $pageSize, array $orderBy = ['colonel_apply_id' => 'DESC'])
    {
        if (empty($column)) {
            $column = self::DEFAULT_COLUMN;
        }
        $data = $where;
        $count = self::count([], $where);
        if (empty($count)) {
            return ['list' => [],'count' => 0];
        }
        if (!empty($page) && !empty($pageSize)) {
            $offset = ($page - 1) * $pageSize;
            $data['LIMIT'] = [$offset,$pageSize];
        }
        if (!empty($orderBy)) {
            $data['ORDER'] = $orderBy;
        }
        $result = self::select($column, $data);
        return ['result' => $result,'count' => $count];
    }

    public static function existUser(string $userId) : bool
    {
        $exist = self::getRecord(['user_id' => $userId], ['colonel_apply_id']);
        return !empty($exist);
    }
}
