<?php


namespace App\Services\distribution;

use App\Models\distribution\mysql\Distributor;
use Api\PhpUtils\Validate\Validate;
use Api\PhpServices\Idgen\Idgen;
use App\Exception\custom\DistributorException;

class DistributorService
{
    /**
     * 查询
     *
     */
    public static function distributorList($params)
    {
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $limit = !empty($params['page_size']) ? $params['page_size'] : 20;
        $page = ($params['page'] - 1) * $limit;

        if (!empty($params['audit_status'])) {
            $where['audit_status'] = $params['audit_status'];
        }

        if (!empty($params['audit_user_name'])) {
            $where['audit_user_name'] = $params['audit_user_name'];
        }

        $where['ORDER'] = ["create_time" => "DESC"];
        $where['LIMIT'] = [$page, $limit];
        $list = Distributor::getRecords($where);
        unset($where['LIMIT']);
        unset($where['ORDER']);
        $count = Distributor::getCount($where);
        $data = ['result' => $list,'count' => $count];
        return $data;
    }

    public static function distributorInfo($params)
    {
        $where['distributor_id'] = !empty($params['distributor_id']) ? $params['distributor_id'] : '';
        $info = Distributor::getRecord($where);
        return $info;
    }

    /**
     * 更新数据
     *
     */
    public static function updateDistributor($params,$where)
    {
        $where['distributor_id'] = !empty($where['distributor_id']) ? $where['distributor_id'] : '';
        $info = Distributor::updateRecord($params, $where);
        return $info;
    }
    
    /**
     * 通过发号器拿 id （非雪花）
     * number，获取店铺shop_id用生活号id后两位，商品相关的这里是shop_id的后两位
     * @param $number
     * @param $type
     * @param int $count
     * @return array|mixed
     */
    public static function getIdgenId($number, $type, $count = 1)
    {
        $res = Idgen::get(appConfig('idgen.partner'), appConfig('idgen.key'), [], [[
            "type" => $type,
            'number' => (int)$number,
            "count" => $count]]);
        return $res['id_datetime'][$type] ?? [];
    }

    public static function getSnowIdgenId($type, $count = 1)
    {
        $res = Idgen::get(appConfig('idgen.partner'), appConfig('idgen.key'), [[
            "type" => $type,
            "count" => $count]], []);
        return $res['id_snow'][$type] ?? [];
    }
}