<?php


namespace App\Services\shop;

use App\Models\shop\mysql\Shop;
use App\Models\shop\mysql\SubShop;
use Api\PhpUtils\Validate\Validate;
use Api\PhpServices\Idgen\Idgen;
// use Validate\ShopValidate;
use App\Exception\custom\ShopException;
use Api\PhpServices\Sensitive\Sensitive;


class ShopService
{
   

    public static function addShop($params)
    {
        $life_account_id = !empty($params['life_account_id']) ? $params['life_account_id'] : '';
        $phone = !empty($params['phone']) ? $params['phone'] : '';
        $shop_id = !empty($params['shop_id']) ? $params['shop_id'] : '';
        $name = !empty($params['name']) ? $params['name'] : '';
        $address_lng = !empty($params['address_lng']) ? $params['address_lng'] : '';
        $address_lat = !empty($params['address_lat']) ? $params['address_lat'] : '';
        $address = !empty($params['address']) ? $params['address'] : '';

        if (empty($life_account_id)) {
            throw new ShopException(['cus'=>0]);
        }

        if (empty($phone)) {
            throw new ShopException(['cus'=>1]);
        }

        // $is_mobile =  ( new Validate)->isMobile($phone);
        // if (empty($is_mobile)) {
        //      throw new ShopException(['cus'=>2]);
        // }

        if (empty($name)) {
            throw new ShopException(['cus'=>3]);
        }

        if (self::utf8Strlen($name) > 20) {
            throw new ShopException(['cus'=>8]);
        }

        $detectParams = [
            'businessId' => 1,
            'text' => !empty($name) ? $name : '',
            'scene' => 1,
        ];
        $sensitive =  (new Sensitive)->detect($detectParams);
        if (!empty($sensitive['data'])) {
            throw new ShopException(['cus'=>4]);
        }

        //判断店铺重复
        if (!self::isName($name)) {
            throw new ShopException(['cus'=>6]);
        }
        
        //判断手机号重复
        if (!self::isPhone($phone)) {
            throw new ShopException(['cus'=>7]);
        }
        $res = self::getIdgenId(1, 'goods', $count = 1);
        $stores_id = !empty($res[0]) ? $res[0] : '';
        $colums = [
            'stores_id' => $stores_id,
            'life_account_id' => $life_account_id,
            'shop_id' => $shop_id,
            'name' => $name,
            'address_lng' => $address_lng,
            'address_lat' => $address_lat,
            'address' => $address,
            'phone' => $phone,
            'create_time' => date("Y-m-d H:i:s"),
        ];
        $subShop = SubShop::insertRecord($colums);
        return $subShop;
    }

    public static function getShopList($params)
    {
        $life_account_id = !empty($params['life_account_id']) ? $params['life_account_id'] : '';
        $phone = !empty($params['phone']) ? $params['phone'] : '';
        $shop_id = !empty($params['shop_id']) ? $params['shop_id'] : '';
        $name = !empty($params['name']) ? $params['name'] : '';
        $address_lng = !empty($params['address_lng']) ? $params['address_lng'] : '';
        $address_lat = !empty($params['address_lat']) ? $params['address_lat'] : '';
        $address = !empty($params['address']) ? $params['address'] : '';
        $where['life_account_id'] =  $life_account_id;
        $where['ORDER'] = ["create_time" => "DESC"];
        $subShopList = SubShop::getRecords($where);
        return $subShopList; 
    }


    /**
     * 门店名称是否重复
     *
     */
    public static function isName($name='')
    {
        if (empty($name)) {
            return false;
        }
        $colums = [
            'name' => $name
        ];
        $subShop = SubShop::getRecord($colums);
        if (!empty($subShop)) {
            return false;
        }
        return true;
    }

    /**
     * 门店手机号是否重复
     *
     */
    public static function isPhone($phone='')
    {
        if (empty($phone)) {
            return false;
        }
        $colums = [
            'phone' => $phone
        ];
        $subShop = SubShop::getRecord($colums);
        if (!empty($subShop)) {
            return false;
        }
        return true;
    } 
   
   /**
    *判断长度
    *
    */
    public static function utf8Strlen($string = null) {
        // 将字符串分解为单元
        preg_match_all("/./us", $string, $match);
        // // 返回单元个数
        return count($match[0]);
    }

     /**
     * 通过发号器拿 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] ?? [];
    }
}