<?php


namespace common\services;


use common\components\AjaxException;
use common\models\EmergencyResponse;
use common\util\EmergencyResponseDimensionObj;
use common\util\EmergencyResponseObj;
use yii\db\ActiveQuery;

class EmergencyResponseService
{
    /**
     * @param $id
     * @return EmergencyResponse
     * @throws AjaxException
     */
    public static function getEmergencyResponseById($id): EmergencyResponse
    {
        /** @var EmergencyResponse $EmergencyResponse */
        $EmergencyResponse = EmergencyResponse::find()->where(["delete_time" => 0, "id" => $id])->one();
        if (!$EmergencyResponse) {
            throw new AjaxException("该应急处置不存在！");
        }
        return $EmergencyResponse;
    }

    /**
     * @return ActiveQuery
     */
    public static function getEmergencyResponseQuery(): ActiveQuery
    {
        return EmergencyResponse::find()->where(["delete_time" => 0]);
    }

    /**
     * 通过案例ID删除应急处置
     * @param $accident_id
     */
    public static function deleteByAccidentCasesId($accident_id): void
    {
        EmergencyResponse::updateAll(["delete_time" => time()], ["accident_id" => $accident_id]);
    }

    /**
     * @param $data
     * @param $accident_id
     * @return array
     */
    public static function dealWithList($data, $accident_id): array
    {
        return OperationalAdjustmentsService::dealWithList($data, $accident_id);
    }

    public static function listAddDimension($data): void
    {
        $EmergencyResponseIdList = [];
        foreach ($data as $value) {
            $EmergencyResponseIdList[] = $value->id;
        }
        $newEmergencyResponseDimensionList = [];
        $EmergencyResponseDimensionList = EmergencyResponseDimensionService::getListByEmergencyResponseId($EmergencyResponseIdList);
        foreach ($EmergencyResponseDimensionList as $EmergencyResponseDimension) {
            $newEmergencyResponseDimensionList[$EmergencyResponseDimension->emergency_response_id][] = new EmergencyResponseDimensionObj($EmergencyResponseDimension);
        }
        foreach ($data as $value) {
            $value->dimension = $newEmergencyResponseDimensionList[$value->id];
        }
    }

    public static function getEmergencyResponseObjList($list)
    {
        $data = [];
        foreach ($list as $value) {
            $data[] = new EmergencyResponseObj($value);;
        }
        return $data;
    }
}