<?php
namespace app\adminapi\service;

use app\common\model\master_worker\MasterWorker;
use app\common\model\master_worker\MasterWorkerRule;
use DateInterval;
use DateTime;

/**
 * 分销leaflets服务
 */
class DistributeLeafletsService
{

    public static function locationIds($lon, $lat,$resType=0,$ids = [])
    {
        $where = [];
        if(!empty($ids)) $where[] = ['id','IN',$ids];
        // 查出所有的工程师 -> 计算距离 -> 按距离排序 -> 分页
        $allList = MasterWorker::where($where)->select()->toArray();
        if(!empty($lon) && !empty($lat)){
            $mastersDistances = self::mastersDistance($allList,$lon,$lat);
        }else{
            $mastersDistances = array_column($allList, 'distance','id');
        }
        // id => distance
        if(empty($resType))
            return $mastersDistances;
        else
            return array_keys($mastersDistances);
    }
    public static function goodsCategoryIds($goods_category_id)
    {
        if($goods_category_id){
            return MasterWorker::whereFindInSet('category_ids',$goods_category_id)->column('id');
        }else{
            return MasterWorker::column('id');
        }
    }
    public static function protectionPeriodIds()
    {
        $ruleInfo = MasterWorkerRule::where('id',1)->findOrEmpty()->toArray();
        //$ruleInfo['description']
        $createTime = time() - (86400 * ($ruleInfo['description']??0));
        return MasterWorker::where([
            ['create_time','>=',$createTime],
            ['is_disable','=',0],
            ['work_status','=',0]
        ])->column('id');
    }
    public static function availableAmountIds()
    {
        $protectionRuleInfo = MasterWorkerRule::where('id',1)->findOrEmpty()->toArray();
        $ruleInfo = MasterWorkerRule::where('id',2)->findOrEmpty()->toArray();
        //$protectionRuleInfo['description']
        //$ruleInfo['description']
        $createTime = time() - (86400 * ($protectionRuleInfo['description']??0));
        return MasterWorker::where([
            ['create_time','<',$createTime],
            ['earnest_money_usable','>=',(float)$ruleInfo['description']??0],
            ['is_disable','=',0],
            ['work_status','=',0]
        ])->column('id');
    }
    public static function getIntersection($goods_category_id,$lon = 0, $lat = 0)
    {
        //self::protectionPeriodIds();
        //self::availableAmountIds();
        $mergeIds = array_merge(self::protectionPeriodIds(),self::availableAmountIds());

        //self::locationIds($lon, $lat,1);
        //self::goodsCategoryIds($goods_category_id);
        //$intersectIds = array_intersect(self::locationIds($lon, $lat,1),self::goodsCategoryIds($goods_category_id));
        //$result=array_intersect($mergeIds,$intersectIds);

        $categoryIds = self::goodsCategoryIds($goods_category_id);

        $result=array_intersect($mergeIds,$categoryIds);
        return array_values($result);
    }
    public static function mastersDistance($masters,$customer_lon,$customer_lat,$radius = 0)
    {
        $masters = array_column($masters, null, 'id');
        $nearbyMasters = [];
        foreach ($masters as $master) {
            $distance = self::haversineDistance($customer_lat, $customer_lon, $master['lat'], $master['lon']);
            if ($radius>0) {
                ($distance <= $radius) && $nearbyMasters[$master['id']] = $distance;
            }else{
                $nearbyMasters[$master['id']] = $distance;
            }
        }
        asort($nearbyMasters);
        return $nearbyMasters;
    }
    public static function haversineDistance($lat1, $lon1, $lat2, $lon2) {
        $lat1 = deg2rad($lat1);
        $lon1 = deg2rad($lon1);
        $lat2 = deg2rad($lat2);
        $lon2 = deg2rad($lon2);

        $dlon = $lon2 - $lon1;
        $dlat = $lat2 - $lat1;
        $a = sin($dlat / 2)**2 + cos($lat1) * cos($lat2) * sin($dlon / 2)**2;
        $c = 2 * asin(sqrt($a));
        $r = 6371*1000; // 地球平均半径，单位为公里/米
        $distance = $c * $r;

        return $distance;
    }
}