<?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'] : 11;
        $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; 
    }


    /**
     * op后台查询查询
     *
     */
    public static function opShopList($params)
    {
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $limit = !empty($params['page_size']) ? $params['page_size'] : 20;
        $page = ($params['page'] - 1) * $limit;

        $where['ORDER'] = ["create_time" => "DESC"];
        $where['LIMIT'] = [$page, $limit];
        $list = SubShop::getRecords($where);
        unset($where['LIMIT']);
        unset($where['ORDER']);
        $count = SubShop::getCount($where);
        $data = ['result' => $list,'count' => $count];
        return $data;
    }
    
    /**
     *
     *
     */
    public static function updateShop($params, $where)
    {
        $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'] : '';
        $phone = !empty($params['phone']) ? $params['phone'] : '';

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

        if (!empty($address_lat)) {
            $colums['address_lat'] = $address_lat;
        }

        if (!empty($address)) {
            $colums['address'] = $address;
        }

        if (!empty($phone)) {
            $colums['phone'] = $phone;
        }

        $subShop = SubShop::updateRecord($colums, $where);
        return $subShop;
    }


    public static function getShopInfo($params)
    {
        $subShopInfo = SubShop::getRecord($params);
        return $subShopInfo;
    }


    /**
     * 门店名称是否重复
     *
     */
    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] ?? [];
    }

    /**
     * 获取制定 shop_id 下的门店
     * @param $shopId
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function getSubShopList($shopId)
    {
        return SubShop::select("*", ["shop_id"=>$shopId]);
    }
}