<?php
/**
 * Created by PhpStorm.
 * User: pengfei@yidian-inc.com
 * Date: 2021/8/23 1:46 下午
 */

namespace App\Models\marketing\mysql;

use Api\PhpUtils\Mysql\MysqlBase;

class ColonelDistributorColonel extends MysqlBase
{
    const TABLE_NAME = 'colonel_distributor_colonel';
    const CONFIG_INDEX = 'marketing';
    const PRIMARY_KEY = 'colonel_id';
    const COLONEL_SOURCE_APPLY = 1; //用户申请
    const COLONEL_SOURCE_ADMIN = 2; //运营添加

    const DEFAULT_COLUMN = ['colonel_id','user_id','take_place_id','phone','contact_name','colonel_source'];


    public static function getColonelUserIds()
    {
        $colonelUserData = ColonelDistributorColonel::select(['user_id']);
        return array_column((array)$colonelUserData, 'user_id');
    }

    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 updateRecord(array $columns, array $where)
    {
        return self::update($columns, $where);
    }

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

    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];
    }
}
