<?php


namespace App\Services\common;

use App\Traits\ServiceAppAop;


class DataHandlerService
{
    use ServiceAppAop;
    protected $modelClass=DataHandler::class;
    public function dataHeader(array $mapKeys= [], $resultData =[]){
        $handlerData = [];
        $handlerData['mapkey'] = $mapKeys;
        if(!$resultData){return $handlerData;}
        foreach ($resultData as $data) {
            $key =  $this->getKey($mapKeys,$data);
            $handlerData[$key] = $data;
        }

        return $handlerData;
    }

    /**
     * 只支持三层的寻找
     * @param array $mapKeyArr
     * @param array $resultData
     */
    public function dataHeaders(array $mapKeyArr = [], $resultData =[]){
        $handlerData_list = [];
        $level_1_key = $mapKeyArr[0][0] ?? false; unset($mapKeyArr[0][0]);
        $level_2_key = $mapKeyArr[1][0] ?? false; unset($mapKeyArr[1][0]);
        $level_3_key = $mapKeyArr[2][0] ?? false; unset($mapKeyArr[2][0]);
        $level_1_keys =$mapKeyArr[0] ?? false;
        $level_2_keys =$mapKeyArr[1] ?? false;
        $level_3_keys =$mapKeyArr[2] ?? false;

        if($level_1_key || $level_1_key==''){
            foreach ($resultData as $level_1_data) {
                if(!$level_1_keys){continue;}
                $key = $this->getKey($level_1_keys,$level_1_data);
                if($level_1_key==''){
                    $level_2_data_list = $level_1_data;
                }else{
                    $level_2_data_list = $level_1_data[$level_1_key];
                }
                if($level_2_key || $level_2_key==''){
                    if(!$level_2_keys){continue;}
                    foreach ($level_2_data_list as $level_2_data) {
                        $key .= $this->getKey($level_2_keys, $level_2_data);
                        $level_3_data_list = $level_2_data[$level_1_key];
                        if ($level_3_key || $level_3_key=='') {
                            if (!$level_3_keys) {
                                continue;
                            }
                            foreach ($level_3_data_list as $level_3_data) {
                                $key .=  $this->getKey($level_3_keys, $level_3_data);
                                $handlerData_list[$key] =$level_3_data;
                            }
                        }else{
                            $handlerData_list[$key] =$level_2_data;
                        }
                    }
                }else{
                    $handlerData_list[$key] =$level_1_data;
                }
            }
        }
        $handlerData['mapkey'] = array_merge($level_1_keys,$level_2_keys ?? [],$level_3_keys ?? []);
        return $handlerData_list ?? null;
    }

    private function  getKey($keys,$object){
        $mapKey = '';
        foreach ($keys as $key){
            $mapKey .= $key.'='.$object[$key];
        }
        return $mapKey;
    }

    public function getKeyValue($keyValues,&$data){
         $mapkeys =  $data['mapkey'];
         $key = '';
         if(!$data){return null;}
         foreach ($mapkeys as $mapkey) {
             $key .=$mapkey.'='.$keyValues[$mapkey];
         }
         return $data[$key] ?? null;
    }
}
