<?php

namespace App\Services;

use App\Events\BroadcastToStation;
use App\Station;
use App\Traits\ServiceAppAop;
use App\StationCacheShelfGrid;
use Illuminate\Support\Facades\Http;

class StationCacheShelfGridService
{
    use ServiceAppAop;

    protected $modelClass = StationCacheShelfGrid::class;

    /**
     * processGrid 格口上放入料箱
     * lightOff 解析HaiQ格口灭灯请求  完成格口上的任务 进行灭灯完成后的广播
     * cancelTask 完成格口上的任务
     * lightOn 发送给HaiQ的格口亮灯
     */

    /**
     * 修改格口的状态
     * @param $stationCacheShelfGrid
     * @param $station
     * @param $materialBox
     */
    public function processGrid($stationCacheShelfGrid, $station, $materialBox)
    {
        $stationCacheShelfGrid->update(['station_id' => $station['id'], 'material_box_id' => $materialBox['id'], 'status' => 1]);
    }

    /**
     * 清空任务
     * @param $grids
     * @return bool
     */
    public function cancelTask($grids): bool
    {

        return StationCacheShelfGrid::query()->whereIn('id',data_get($grids,'*.id'))->update(['material_box_id' => null, 'status' => 0]);
    }

    /**
     * 格口灭灯完成后的广播
     * @param $locCode
     * @param $PTLAction
     */
    public function lightOff($locCode, $PTLAction)
    {
        if ($PTLAction == 0) {
            list($stationCode, $gridId, $x, $y) = StationCacheShelfGrid::getGridByCode($locCode);
            $station = Station::query()->where('code', $stationCode)->first();
            $gird = StationCacheShelfGrid::query()->where('station_id',$station['id'])->where('grid_id',$gridId)->get();
            $this->cancelTask($gird);
            $json = json_encode([
                'code' => $stationCode,
                'x' => $x,
                'y' => $y,
                'id' => $station['id'] ?? '',
                'grid_id' => $gridId,
            ]);
            broadcast(new BroadcastToStation($station['id'] ?? '', $json));
        }
    }

    /**
     * 通知HaiQ亮灯
     * @param Station $station
     * @param $pointX
     * @param $pointY
     * @return string
     */
    public function lightOn(Station $station, $pointX, $pointY)
    {
        $locCode = 'HAI' . $station['code'] . '-0' . $pointX . '-0' . $pointY;
        $params = [
            "areaCode" => "1004",
            "PTLAction" => 1, //1是开，0是关
            "PTLSettings" => [
                "color" => 1,
                "frequency" => 1
            ],
            "displayInfo" => [
                "detail01" => "detail01",
                "detail02" => "detail02",
                "detail03" => "detail03",
                "qrCode" => "qrCode",
                "qty00" => "11",
                "qty01" => 1,
                "qty02" => 2,
                "title" => "title",
                "uomDesc01" => "uo",
                "uomDesc02" => "uo"
            ],
            "locCode" => $locCode//灯条口，B1\B2=设备编号，中间号码代表从右往左数的列，右边号码时从下往上数
        ];
//        LogService::log(__METHOD__,'海柔亮灯请求',json_encode($params));
        $response = Http::post(config('api.haiq.storage.light'), $params);
//        LogService::log(__METHOD__,'海柔亮灯请求返回',json_encode($params));
        return $response->body();
    }
}
