<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\service_area;


use app\common\model\BaseModel;



/**
 * ServiceArea模型
 * Class ServiceArea
 * @package app\common\model
 */
class ServiceArea extends BaseModel
{
    
    protected $name = 'service_area';

    protected $type = [
        'electronic_fence' => 'array',
    ];

    /**
     * 判断是否在某服务区 返回服务区城市信息
     * @param $params
     * @return bool
     */
    public static function serviceAreaId($params):int
    {
        if (empty($params['lon']) || empty($params['lat'])) {
            return 0;
        }
        // 查询服务区所有的地点
        $rules = ServiceArea::field(['id', 'province', 'city','county', 'area_name', 'electronic_fence'])->select()->toArray();
        $point=['lng'=> $params['lon'],'lat'=> $params['lat']];
        foreach ($rules as $value){
            foreach ($value['electronic_fence'] as $polygon) {
                if (isPointInPolygon($point, $polygon)) {
                    return $value['id'];
                }
            }
        }
        return 0;
    }

    /**
     * 根据经纬度，返回服务区城市信息
     * @param $params
     * @return bool
     */
    public static function serviceArea($params):array
    {
        if (empty($params['lon']) || empty($params['lat'])) {
            return [];
        }
        // 查询服务区所有的地点
        $rules = ServiceArea::field(['id', 'province', 'city','county', 'area_name', 'electronic_fence'])->select()->toArray();
        $point=['lng'=> $params['lon'],'lat'=> $params['lat']];
        foreach ($rules as $value){
            foreach ($value['electronic_fence'] as $polygon) {
                if (isPointInPolygon($point, $polygon)) {
                    return $value;
                }
            }
        }
        return [];
    }
}