<?php

namespace Modules\OpcData\Http\Controllers\Api;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Modules\OpcData\Entities\OpcDataDict;

class OpcDataController extends Controller
{
    protected $opcConf = [
        'ventilation_zb'    => 1,
        'ventilation_bb'    => 2,
        'pump_zy'           => 3,
        'pump_690'          => 4,
        'compressed_air'    => 5,
        'belt_13'           => 6,
        'hoist'             => 7
    ];

    protected $localConf = [
        'jinjiaqu'    => [
            'ventilation_zb'    => 1,
            'ventilation_bb'    => 2,
            'pump_zy'           => 3,
            'pump_690'          => 4,
            'compressed_air'    => 5,
            'belt_13'           => 6,
            'hoist'             => 7
        ],

        'shicaocun'    => [
            'fan_1'    => 1,
            'fan_2'    => 2,
            'pump_zy'           => 3,
            'pump_690'          => 4,
            'compressed_air'    => 5,
            'belt_13'           => 6,
            'hoist'             => 7
        ],
    ];
    
    public function getData(Request $request)
    {
        $getDataType = $request->system_type;   // 获取需要的数据
        $getMineCode = $request->mine_code;     // 矿分类
        $dataArr = array();                     // 返回数据数组

        $opcConnectName   = 'mysql_opc_'.$getMineCode;

        $opcDB =  DB::connection($opcConnectName);
        
        if (array_key_exists($getDataType, $this->localConf[$getMineCode])) {
            $groupId = $this->localConf[$getMineCode][$getDataType];
            $sqlStr = "
            select t1.dev_type_id,
                    t1.dev_type_name,
                    t1.device_num,
                    t1.device_name,
                    case t.val 
                        when 'True' then 1
                        when 'False' then 2
                        else t.val
                    end val,
                    t1.json_key,
                    t1.row_number,
                    t1.unit
            from equipment t
            join tb_dev_dict_list t1 on t.selItem = t1.selItem
            where t1.group_id = ".$groupId."
            and t1.is_show = 1
            order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
            ";
            $dbResult = $opcDB->select($sqlStr);
            switch($getDataType) {
                // 中部风机
                case 'ventilation_zb':
                    $result = $this->getFanData($dbResult);
                    break;
                // 北部风机
                case 'ventilation_bb':
                    $result = $this->getFanData($dbResult);
                    break;
                case 'pump_zy':
                    // 中央水泵
                    $result = $this->getPumpData($dbResult);
                    break;
                case 'pump_690':
                    // 609水泵
                    $result = $this->getPumpData($dbResult);
                    break;
                case 'compressed_air':
                    // 压风
                    $result = $this->getCompressedAirData($dbResult);
                    break;
                case 'belt_13':
                    // 13采区胶带运输机系统
                    $result = $this->getBeltData($dbResult);
                    break;
                case 'hoist':
                    // 提升机
                    $result = $this->getHoistData($dbResult);
                    break;
                
                case 'fan_1':
                    // 风机1
                    $result = $this->getFanData($dbResult);
                    break;
                case 'fan_2':
                    // 风机1
                    $result = $this->getFanData($dbResult);
                    break;
            }
            return response()->json($result);
        }
        return;
    }

    // 风机
    public function getFanData($dbResult) {

        foreach ($dbResult as $key => $val) {
            $fanNum     = $val->dev_type_id - 1;        // 风机号
            $label      = $val->device_name;            // 设备名
            $devVal     = $val->val;                    // 数值
            $jsonKey   = $val->json_key;                // json键值
            $unit       = $val->unit;                   // 单位
            $dataArr[$fanNum]['title'] = $val->dev_type_name; // 风机名
            $valArr = [
                'label' => $label,      // 标题
                'unit'  => $unit        // 单位
            ];
            // 状态与value
            if($jsonKey == 'state_list') {
                $valArr['state'] = $devVal;
            } else {
                $valArr['value'] = $devVal;
            }
            // 根据风量判断风机开关
            if($label == '风量') {
                if ($devVal <= 0 ) {
                    $dataArr[$fanNum]['state'] = 2;
                } else {
                    $dataArr[$fanNum]['state'] = 1;
                }
            }
            $dataArr[$fanNum][$jsonKey][] = $valArr;
            
        }
        return $dataArr;
    }


    // 水泵
    public function getPumpData($dbResult) {

        $timeArr = array();     // 用于拼接时分秒
        $timeJsonKey;           // 记录time字段key名
        // dd($dbResult);
        foreach ($dbResult as $key => $val) {
            $devName    = $val->dev_type_name;          // 泵名
            $rowNumber  = $val->row_number;             // 排序
            $jsonKey    = $val->json_key;               // json键值
            $label      = $val->device_name;            // 设备名
            $devNum     = $val->dev_type_id - 1;        // 设备号
            $unit       = $val->unit;                   // 单位
            $devVal     = $val->val;                    // 数值

            if ($jsonKey == 'common') {
                // 公共数据
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr[$jsonKey][] = $valArr;

            } else if ($jsonKey == 'water_pump') {
                // 水泵、状态
                $dataArr[$jsonKey][$devNum]['label'] = $devName;
                if (array_key_exists($jsonKey, $dataArr[$jsonKey][$devNum])) {
                    $state = $dataArr[$jsonKey][$devNum]['state'];
                    $dataArr[$jsonKey][$devNum]['state'] = max($devVal, $state);
                } else {
                    $dataArr[$jsonKey][$devNum]['state'] = $devVal;
                }
            } else if ($jsonKey == 'parameter' && $unit == 'HH' && $unit == 'MM' && $unit == 'SS') {
                // 处理时长
                $timeArr[$label][$devNum][$unit] = $devVal;
                $timeJsonKey = $jsonKey;

            } else {
                // 水泵参数
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];

                $dataArr['water_pump'][$devNum][$jsonKey][] = $valArr;

            }
            
        }

        foreach ($timeArr as $key => $value) {
            for ($i=0; $i < count($value); $i++) {
                $timeFormatStr = $value[$i]['HH'].'时'.$value[$i]['MM'].'分'.$value[$i]['SS'].'秒';
                $valArr = [
                    'label' => $key,                // 标题
                    'value' => $timeFormatStr,      // 值
                    'unit'  => ''                   // 单位
                ];
                $dataArr['water_pump'][$i][$timeJsonKey][] = $valArr;
            }
        }
        return $dataArr;
    }


    // 压风
    public function getCompressedAirData($dbResult) {

        $timeArr = array();     // 用于拼接时分秒
        $timeJsonKey;           // 记录time字段key名
        // dd($dbResult);
        foreach ($dbResult as $key => $val) {
            $devName    = $val->dev_type_name;          // 泵名
            $rowNumber  = $val->row_number;             // 排序
            $jsonKey    = $val->json_key;               // json键值
            $label      = $val->device_name;            // 设备名
            $devNum     = $val->dev_type_id - 1;        // 设备号
            $unit       = $val->unit;                   // 单位
            $devVal     = $val->val;                    // 数值

            // $valArr[$jsonKey] = $devVal;

            if ($jsonKey == 'state') {
                $dataArr['compressed_air'][$devNum]['name'] = $devName;
                $dataArr['compressed_air'][$devNum]['state'] = $devVal;
            } else if ($jsonKey == 'run_parameter') {

                $valArr = [
                    'label' => $label,      // 标题
                    'state' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                    // ,'json' => $jsonKey
                ];
                $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
                
            } else if ($jsonKey == 'common') {
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr[$jsonKey][] = $valArr;
            } else if ($jsonKey == 'drain_value_state') {
                // 开关状态单独处理
                $dataArr['compressed_air'][$devNum][$jsonKey] = $devVal;
                if (array_key_exists($jsonKey, $dataArr['compressed_air'][$devNum])) {
                    $state = $dataArr['compressed_air'][$devNum][$jsonKey];
                    $dataArr['compressed_air'][$devNum][$jsonKey] = max($devVal, $state);
                } else {
                    $dataArr['compressed_air'][$devNum][$jsonKey] = $devVal;
                }
            }  else if ($jsonKey == 'compressed_air') {
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
            } else if ($jsonKey == 'warn_state') {
                $valArr = [
                    'label' => $label,      // 标题
                    'state' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
            }
            
        }
        return $dataArr;
    }



    public function getBeltData($dbResult) {

        $valDict = array();
        foreach ($dbResult as $key => $val) {
            $devName    = $val->dev_type_name;          // 泵名
            $rowNumber  = $val->row_number;             // 排序
            $jsonKey    = $val->json_key;               // json键值
            $label      = $val->device_name;            // 设备名
            $devTypeNum = $val->dev_type_id - 1;        // 设备号
            $devNum     = $val->device_num - 1;         // 设备号
            $unit       = $val->unit;                   // 单位
            $devVal     = $val->val;                    // 数值

            // $valArr[$jsonKey] = $devVal;

            if ($jsonKey == 'common') {
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr[$jsonKey][] = $valArr;
            } else if ($jsonKey == 'state') {
                $dataArr['state'] = $devVal;
                $dataArr['name'] = $label;
            }  else if ($jsonKey == 'electric_parameter' or $jsonKey == 'converter_parameter') {
                $valArr = [
                    // 'devNum' => $devNum,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                    // ,'label'  => $label        // 单位
                ];
                $valDict[$jsonKey][$label][] = $valArr;
            } else {
                $valArr = [
                    'label' => $label,      // 标题
                    'state' => $devVal,     // 值
                    // 'unit'  => $unit        // 单位
                ];
                $dataArr[$jsonKey][] = $valArr;
            }
            
        }
        foreach ($valDict as $key => $val) {
            $i = 0;
            foreach ($val as $valKey => $valVal) { 
                $dataArr[$key][$i]['label'] = $valKey;
                $dataArr[$key][$i]['value_list'] = $valVal;
                $i++;
            }
        }
        // dd($dataArr);
        return $dataArr;
    }



    public function getHoistData($dbResult) {

        $valDict = array();
        $state = 2;
        // dd($dbResult);
        foreach ($dbResult as $key => $val) {
            $devName    = $val->dev_type_name;          // 泵名
            $rowNumber  = $val->row_number;             // 排序
            $jsonKey    = $val->json_key;               // json键值
            $label      = $val->device_name;            // 设备名
            $devTypeNum = $val->dev_type_id;        // 设备号
            $devNum     = $val->device_num - 1;         // 设备号
            $unit       = $val->unit;                   // 单位
            $devVal     = $val->val;                    // 数值

            if ($jsonKey == 'position') {
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr['common'][$jsonKey][] = $valArr;
            } else if ($jsonKey == 'direction') {
                
                $valArr = [
                    'label' => $label,      // 标题
                    'state' => $devVal,     // 值
                ];
                $dataArr['common'][$jsonKey][] = $valArr;
                if ($devVal == 1) {
                    $state = 1;
                }
            } else if ($jsonKey == 'run_parameter') {
                $valArr = [
                    'label' => $label,      // 标题
                    'value' => $devVal,     // 值
                    'unit'  => $unit        // 单位
                ];
                $dataArr['hoist'][$devTypeNum][$jsonKey][] = $valArr;
                $dataArr['hoist'][$devTypeNum]['name'] = '中部副立井';
                // $dataArr['common']['direction']
            } else {
                $valArr = [
                    'label' => $label,      // 标题
                    'state' => $devVal,     // 值
                ];
                $dataArr['hoist'][$devTypeNum][$jsonKey][] = $valArr;
            }
            
        }
        $dataArr['hoist'][0]['state'] = $state;
        return $dataArr;
    }
}
